cls_5_1_byte.c 3.0 KB

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