cls_3byte1.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. Check overlapping.
  5. Limitations: none.
  6. PR: none.
  7. Originator: <andreast@gcc.gnu.org> 20030828 */
  8. /* { dg-do run } */
  9. #include "ffitest.h"
  10. typedef struct cls_struct_3byte {
  11. unsigned short a;
  12. unsigned char b;
  13. } cls_struct_3byte;
  14. cls_struct_3byte cls_struct_3byte_fn(struct cls_struct_3byte a1,
  15. struct cls_struct_3byte a2)
  16. {
  17. struct cls_struct_3byte result;
  18. result.a = a1.a + a2.a;
  19. result.b = a1.b + a2.b;
  20. printf("%d %d %d %d: %d %d\n", a1.a, a1.b, a2.a, a2.b, result.a, result.b);
  21. return result;
  22. }
  23. static void
  24. cls_struct_3byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  25. void* userdata __UNUSED__)
  26. {
  27. struct cls_struct_3byte a1, a2;
  28. a1 = *(struct cls_struct_3byte*)(args[0]);
  29. a2 = *(struct cls_struct_3byte*)(args[1]);
  30. *(cls_struct_3byte*)resp = cls_struct_3byte_fn(a1, a2);
  31. }
  32. int main (void)
  33. {
  34. ffi_cif cif;
  35. void *code;
  36. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  37. void* args_dbl[5];
  38. ffi_type* cls_struct_fields[4];
  39. ffi_type cls_struct_type;
  40. ffi_type* dbl_arg_types[5];
  41. struct cls_struct_3byte g_dbl = { 12, 119 };
  42. struct cls_struct_3byte f_dbl = { 1, 15 };
  43. struct cls_struct_3byte res_dbl;
  44. cls_struct_type.size = 0;
  45. cls_struct_type.alignment = 0;
  46. cls_struct_type.type = FFI_TYPE_STRUCT;
  47. cls_struct_type.elements = cls_struct_fields;
  48. cls_struct_fields[0] = &ffi_type_ushort;
  49. cls_struct_fields[1] = &ffi_type_uchar;
  50. cls_struct_fields[2] = NULL;
  51. dbl_arg_types[0] = &cls_struct_type;
  52. dbl_arg_types[1] = &cls_struct_type;
  53. dbl_arg_types[2] = NULL;
  54. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  55. dbl_arg_types) == FFI_OK);
  56. args_dbl[0] = &g_dbl;
  57. args_dbl[1] = &f_dbl;
  58. args_dbl[2] = NULL;
  59. ffi_call(&cif, FFI_FN(cls_struct_3byte_fn), &res_dbl, args_dbl);
  60. /* { dg-output "12 119 1 15: 13 134" } */
  61. printf("res: %d %d\n", res_dbl.a, res_dbl.b);
  62. /* { dg-output "\nres: 13 134" } */
  63. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_3byte_gn, NULL, code) == FFI_OK);
  64. res_dbl = ((cls_struct_3byte(*)(cls_struct_3byte, cls_struct_3byte))(code))(g_dbl, f_dbl);
  65. /* { dg-output "\n12 119 1 15: 13 134" } */
  66. printf("res: %d %d\n", res_dbl.a, res_dbl.b);
  67. /* { dg-output "\nres: 13 134" } */
  68. exit(0);
  69. }