cls_4_1byte.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_4_1byte {
  11. unsigned char a;
  12. unsigned char b;
  13. unsigned char c;
  14. unsigned char d;
  15. } cls_struct_4_1byte;
  16. cls_struct_4_1byte cls_struct_4_1byte_fn(struct cls_struct_4_1byte a1,
  17. struct cls_struct_4_1byte a2)
  18. {
  19. struct cls_struct_4_1byte 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. printf("%d %d %d %d %d %d %d %d: %d %d %d %d\n", a1.a, a1.b, a1.c, a1.d,
  25. a2.a, a2.b, a2.c, a2.d,
  26. result.a, result.b, result.c, result.d);
  27. return result;
  28. }
  29. static void
  30. cls_struct_4_1byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  31. void* userdata __UNUSED__)
  32. {
  33. struct cls_struct_4_1byte a1, a2;
  34. a1 = *(struct cls_struct_4_1byte*)(args[0]);
  35. a2 = *(struct cls_struct_4_1byte*)(args[1]);
  36. *(cls_struct_4_1byte*)resp = cls_struct_4_1byte_fn(a1, a2);
  37. }
  38. int main (void)
  39. {
  40. ffi_cif cif;
  41. void *code;
  42. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  43. void* args_dbl[5];
  44. ffi_type* cls_struct_fields[5];
  45. ffi_type cls_struct_type;
  46. ffi_type* dbl_arg_types[5];
  47. struct cls_struct_4_1byte g_dbl = { 12, 13, 14, 15 };
  48. struct cls_struct_4_1byte f_dbl = { 178, 179, 180, 181 };
  49. struct cls_struct_4_1byte res_dbl;
  50. cls_struct_type.size = 0;
  51. cls_struct_type.alignment = 0;
  52. cls_struct_type.type = FFI_TYPE_STRUCT;
  53. cls_struct_type.elements = cls_struct_fields;
  54. cls_struct_fields[0] = &ffi_type_uchar;
  55. cls_struct_fields[1] = &ffi_type_uchar;
  56. cls_struct_fields[2] = &ffi_type_uchar;
  57. cls_struct_fields[3] = &ffi_type_uchar;
  58. cls_struct_fields[4] = NULL;
  59. dbl_arg_types[0] = &cls_struct_type;
  60. dbl_arg_types[1] = &cls_struct_type;
  61. dbl_arg_types[2] = NULL;
  62. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  63. dbl_arg_types) == FFI_OK);
  64. args_dbl[0] = &g_dbl;
  65. args_dbl[1] = &f_dbl;
  66. args_dbl[2] = NULL;
  67. ffi_call(&cif, FFI_FN(cls_struct_4_1byte_fn), &res_dbl, args_dbl);
  68. /* { dg-output "12 13 14 15 178 179 180 181: 190 192 194 196" } */
  69. printf("res: %d %d %d %d\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
  70. /* { dg-output "\nres: 190 192 194 196" } */
  71. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_4_1byte_gn, NULL, code) == FFI_OK);
  72. res_dbl = ((cls_struct_4_1byte(*)(cls_struct_4_1byte, cls_struct_4_1byte))(code))(g_dbl, f_dbl);
  73. /* { dg-output "\n12 13 14 15 178 179 180 181: 190 192 194 196" } */
  74. printf("res: %d %d %d %d\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
  75. /* { dg-output "\nres: 190 192 194 196" } */
  76. exit(0);
  77. }