cls_5byte.c 2.6 KB

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