cls_1_1byte.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Area: ffi_call, closure_call
  2. Purpose: Check structure passing with different structure size.
  3. Especially with small structures which may fit in one
  4. register. Depending on the ABI.
  5. Limitations: none.
  6. PR: none.
  7. Originator: <andreast@gcc.gnu.org> 20030902 */
  8. /* { dg-do run } */
  9. #include "ffitest.h"
  10. typedef struct cls_struct_1_1byte {
  11. unsigned char a;
  12. } cls_struct_1_1byte;
  13. cls_struct_1_1byte cls_struct_1_1byte_fn(struct cls_struct_1_1byte a1,
  14. struct cls_struct_1_1byte a2)
  15. {
  16. struct cls_struct_1_1byte result;
  17. result.a = a1.a + a2.a;
  18. printf("%d %d: %d\n", a1.a, a2.a, result.a);
  19. return result;
  20. }
  21. static void
  22. cls_struct_1_1byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  23. void* userdata __UNUSED__)
  24. {
  25. struct cls_struct_1_1byte a1, a2;
  26. a1 = *(struct cls_struct_1_1byte*)(args[0]);
  27. a2 = *(struct cls_struct_1_1byte*)(args[1]);
  28. *(cls_struct_1_1byte*)resp = cls_struct_1_1byte_fn(a1, a2);
  29. }
  30. int main (void)
  31. {
  32. ffi_cif cif;
  33. void *code;
  34. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  35. void* args_dbl[5];
  36. ffi_type* cls_struct_fields[2];
  37. ffi_type cls_struct_type;
  38. ffi_type* dbl_arg_types[5];
  39. struct cls_struct_1_1byte g_dbl = { 12 };
  40. struct cls_struct_1_1byte f_dbl = { 178 };
  41. struct cls_struct_1_1byte res_dbl;
  42. cls_struct_type.size = 0;
  43. cls_struct_type.alignment = 0;
  44. cls_struct_type.type = FFI_TYPE_STRUCT;
  45. cls_struct_type.elements = cls_struct_fields;
  46. cls_struct_fields[0] = &ffi_type_uchar;
  47. cls_struct_fields[1] = NULL;
  48. dbl_arg_types[0] = &cls_struct_type;
  49. dbl_arg_types[1] = &cls_struct_type;
  50. dbl_arg_types[2] = NULL;
  51. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  52. dbl_arg_types) == FFI_OK);
  53. args_dbl[0] = &g_dbl;
  54. args_dbl[1] = &f_dbl;
  55. args_dbl[2] = NULL;
  56. ffi_call(&cif, FFI_FN(cls_struct_1_1byte_fn), &res_dbl, args_dbl);
  57. /* { dg-output "12 178: 190" } */
  58. printf("res: %d\n", res_dbl.a);
  59. /* { dg-output "\nres: 190" } */
  60. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_1_1byte_gn, NULL, code) == FFI_OK);
  61. res_dbl = ((cls_struct_1_1byte(*)(cls_struct_1_1byte, cls_struct_1_1byte))(code))(g_dbl, f_dbl);
  62. /* { dg-output "\n12 178: 190" } */
  63. printf("res: %d\n", res_dbl.a);
  64. /* { dg-output "\nres: 190" } */
  65. exit(0);
  66. }