cls_3_1byte.c 2.6 KB

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