cls_7byte.c 2.7 KB

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