cls_4byte.c 2.3 KB

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