cls_6_1_byte.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Area: ffi_call, closure_call
  2. Purpose: Check structure passing with different structure size.
  3. Depending on the ABI. Check overlapping.
  4. Limitations: none.
  5. PR: none.
  6. Originator: <andreast@gcc.gnu.org> 20050708 */
  7. /* { dg-do run } */
  8. #include "ffitest.h"
  9. typedef struct cls_struct_6byte {
  10. unsigned char a;
  11. unsigned char b;
  12. unsigned char c;
  13. unsigned char d;
  14. unsigned char e;
  15. unsigned char f;
  16. } cls_struct_6byte;
  17. cls_struct_6byte cls_struct_6byte_fn(struct cls_struct_6byte a1,
  18. struct cls_struct_6byte a2)
  19. {
  20. struct cls_struct_6byte result;
  21. result.a = a1.a + a2.a;
  22. result.b = a1.b + a2.b;
  23. result.c = a1.c + a2.c;
  24. result.d = a1.d + a2.d;
  25. result.e = a1.e + a2.e;
  26. result.f = a1.f + a2.f;
  27. printf("%d %d %d %d %d %d %d %d %d %d %d %d: %d %d %d %d %d %d\n",
  28. a1.a, a1.b, a1.c, a1.d, a1.e, a1.f,
  29. a2.a, a2.b, a2.c, a2.d, a2.e, a2.f,
  30. result.a, result.b, result.c, result.d, result.e, result.f);
  31. return result;
  32. }
  33. static void
  34. cls_struct_6byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  35. void* userdata __UNUSED__)
  36. {
  37. struct cls_struct_6byte a1, a2;
  38. a1 = *(struct cls_struct_6byte*)(args[0]);
  39. a2 = *(struct cls_struct_6byte*)(args[1]);
  40. *(cls_struct_6byte*)resp = cls_struct_6byte_fn(a1, a2);
  41. }
  42. int main (void)
  43. {
  44. ffi_cif cif;
  45. void *code;
  46. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  47. void* args_dbl[5];
  48. ffi_type* cls_struct_fields[7];
  49. ffi_type cls_struct_type;
  50. ffi_type* dbl_arg_types[5];
  51. struct cls_struct_6byte g_dbl = { 127, 120, 1, 3, 4, 5 };
  52. struct cls_struct_6byte f_dbl = { 12, 128, 9, 3, 4, 5 };
  53. struct cls_struct_6byte res_dbl = { 0, 0, 0, 0, 0, 0 };
  54. cls_struct_type.size = 0;
  55. cls_struct_type.alignment = 0;
  56. cls_struct_type.type = FFI_TYPE_STRUCT;
  57. cls_struct_type.elements = cls_struct_fields;
  58. cls_struct_fields[0] = &ffi_type_uchar;
  59. cls_struct_fields[1] = &ffi_type_uchar;
  60. cls_struct_fields[2] = &ffi_type_uchar;
  61. cls_struct_fields[3] = &ffi_type_uchar;
  62. cls_struct_fields[4] = &ffi_type_uchar;
  63. cls_struct_fields[5] = &ffi_type_uchar;
  64. cls_struct_fields[6] = NULL;
  65. dbl_arg_types[0] = &cls_struct_type;
  66. dbl_arg_types[1] = &cls_struct_type;
  67. dbl_arg_types[2] = NULL;
  68. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  69. dbl_arg_types) == FFI_OK);
  70. args_dbl[0] = &g_dbl;
  71. args_dbl[1] = &f_dbl;
  72. args_dbl[2] = NULL;
  73. ffi_call(&cif, FFI_FN(cls_struct_6byte_fn), &res_dbl, args_dbl);
  74. /* { dg-output "127 120 1 3 4 5 12 128 9 3 4 5: 139 248 10 6 8 10" } */
  75. printf("res: %d %d %d %d %d %d\n", res_dbl.a, res_dbl.b, res_dbl.c,
  76. res_dbl.d, res_dbl.e, res_dbl.f);
  77. /* { dg-output "\nres: 139 248 10 6 8 10" } */
  78. res_dbl.a = 0;
  79. res_dbl.b = 0;
  80. res_dbl.c = 0;
  81. res_dbl.d = 0;
  82. res_dbl.e = 0;
  83. res_dbl.f = 0;
  84. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_6byte_gn, NULL, code) == FFI_OK);
  85. res_dbl = ((cls_struct_6byte(*)(cls_struct_6byte, cls_struct_6byte))(code))(g_dbl, f_dbl);
  86. /* { dg-output "\n127 120 1 3 4 5 12 128 9 3 4 5: 139 248 10 6 8 10" } */
  87. printf("res: %d %d %d %d %d %d\n", res_dbl.a, res_dbl.b, res_dbl.c,
  88. res_dbl.d, res_dbl.e, res_dbl.f);
  89. /* { dg-output "\nres: 139 248 10 6 8 10" } */
  90. exit(0);
  91. }