cls_7_1_byte.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_7byte {
  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. unsigned char g;
  17. } cls_struct_7byte;
  18. cls_struct_7byte cls_struct_7byte_fn(struct cls_struct_7byte a1,
  19. struct cls_struct_7byte a2)
  20. {
  21. struct cls_struct_7byte result;
  22. result.a = a1.a + a2.a;
  23. result.b = a1.b + a2.b;
  24. result.c = a1.c + a2.c;
  25. result.d = a1.d + a2.d;
  26. result.e = a1.e + a2.e;
  27. result.f = a1.f + a2.f;
  28. result.g = a1.g + a2.g;
  29. printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d: %d %d %d %d %d %d %d\n",
  30. a1.a, a1.b, a1.c, a1.d, a1.e, a1.f, a1.g,
  31. a2.a, a2.b, a2.c, a2.d, a2.e, a2.f, a2.g,
  32. result.a, result.b, result.c, result.d, result.e, result.f, result.g);
  33. return result;
  34. }
  35. static void
  36. cls_struct_7byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  37. void* userdata __UNUSED__)
  38. {
  39. struct cls_struct_7byte a1, a2;
  40. a1 = *(struct cls_struct_7byte*)(args[0]);
  41. a2 = *(struct cls_struct_7byte*)(args[1]);
  42. *(cls_struct_7byte*)resp = cls_struct_7byte_fn(a1, a2);
  43. }
  44. int main (void)
  45. {
  46. ffi_cif cif;
  47. void *code;
  48. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  49. void* args_dbl[5];
  50. ffi_type* cls_struct_fields[8];
  51. ffi_type cls_struct_type;
  52. ffi_type* dbl_arg_types[5];
  53. struct cls_struct_7byte g_dbl = { 127, 120, 1, 3, 4, 5, 6 };
  54. struct cls_struct_7byte f_dbl = { 12, 128, 9, 3, 4, 5, 6 };
  55. struct cls_struct_7byte res_dbl = { 0, 0, 0, 0, 0, 0, 0 };
  56. cls_struct_type.size = 0;
  57. cls_struct_type.alignment = 0;
  58. cls_struct_type.type = FFI_TYPE_STRUCT;
  59. cls_struct_type.elements = cls_struct_fields;
  60. cls_struct_fields[0] = &ffi_type_uchar;
  61. cls_struct_fields[1] = &ffi_type_uchar;
  62. cls_struct_fields[2] = &ffi_type_uchar;
  63. cls_struct_fields[3] = &ffi_type_uchar;
  64. cls_struct_fields[4] = &ffi_type_uchar;
  65. cls_struct_fields[5] = &ffi_type_uchar;
  66. cls_struct_fields[6] = &ffi_type_uchar;
  67. cls_struct_fields[7] = NULL;
  68. dbl_arg_types[0] = &cls_struct_type;
  69. dbl_arg_types[1] = &cls_struct_type;
  70. dbl_arg_types[2] = NULL;
  71. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  72. dbl_arg_types) == FFI_OK);
  73. args_dbl[0] = &g_dbl;
  74. args_dbl[1] = &f_dbl;
  75. args_dbl[2] = NULL;
  76. ffi_call(&cif, FFI_FN(cls_struct_7byte_fn), &res_dbl, args_dbl);
  77. /* { dg-output "127 120 1 3 4 5 6 12 128 9 3 4 5 6: 139 248 10 6 8 10 12" } */
  78. printf("res: %d %d %d %d %d %d %d\n", res_dbl.a, res_dbl.b, res_dbl.c,
  79. res_dbl.d, res_dbl.e, res_dbl.f, res_dbl.g);
  80. /* { dg-output "\nres: 139 248 10 6 8 10 12" } */
  81. res_dbl.a = 0;
  82. res_dbl.b = 0;
  83. res_dbl.c = 0;
  84. res_dbl.d = 0;
  85. res_dbl.e = 0;
  86. res_dbl.f = 0;
  87. res_dbl.g = 0;
  88. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_7byte_gn, NULL, code) == FFI_OK);
  89. res_dbl = ((cls_struct_7byte(*)(cls_struct_7byte, cls_struct_7byte))(code))(g_dbl, f_dbl);
  90. /* { dg-output "\n127 120 1 3 4 5 6 12 128 9 3 4 5 6: 139 248 10 6 8 10 12" } */
  91. printf("res: %d %d %d %d %d %d %d\n", res_dbl.a, res_dbl.b, res_dbl.c,
  92. res_dbl.d, res_dbl.e, res_dbl.f, res_dbl.g);
  93. /* { dg-output "\nres: 139 248 10 6 8 10 12" } */
  94. exit(0);
  95. }