cls_12byte.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Area: ffi_call, closure_call
  2. Purpose: Check structure passing with different structure size.
  3. Limitations: none.
  4. PR: none.
  5. Originator: <andreast@gcc.gnu.org> 20030828 */
  6. /* { dg-do run } */
  7. #include "ffitest.h"
  8. typedef struct cls_struct_12byte {
  9. int a;
  10. int b;
  11. int c;
  12. } cls_struct_12byte;
  13. cls_struct_12byte cls_struct_12byte_fn(struct cls_struct_12byte b1,
  14. struct cls_struct_12byte b2)
  15. {
  16. struct cls_struct_12byte result;
  17. result.a = b1.a + b2.a;
  18. result.b = b1.b + b2.b;
  19. result.c = b1.c + b2.c;
  20. printf("%d %d %d %d %d %d: %d %d %d\n", b1.a, b1.b, b1.c, b2.a, b2.b, b2.c,
  21. result.a, result.b, result.c);
  22. return result;
  23. }
  24. static void cls_struct_12byte_gn(ffi_cif* cif __UNUSED__, void* resp,
  25. void** args , void* userdata __UNUSED__)
  26. {
  27. struct cls_struct_12byte b1, b2;
  28. b1 = *(struct cls_struct_12byte*)(args[0]);
  29. b2 = *(struct cls_struct_12byte*)(args[1]);
  30. *(cls_struct_12byte*)resp = cls_struct_12byte_fn(b1, b2);
  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_12byte h_dbl = { 7, 4, 9 };
  42. struct cls_struct_12byte j_dbl = { 1, 5, 3 };
  43. struct cls_struct_12byte 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_sint;
  49. cls_struct_fields[1] = &ffi_type_sint;
  50. cls_struct_fields[2] = &ffi_type_sint;
  51. cls_struct_fields[3] = NULL;
  52. dbl_arg_types[0] = &cls_struct_type;
  53. dbl_arg_types[1] = &cls_struct_type;
  54. dbl_arg_types[2] = NULL;
  55. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  56. dbl_arg_types) == FFI_OK);
  57. args_dbl[0] = &h_dbl;
  58. args_dbl[1] = &j_dbl;
  59. args_dbl[2] = NULL;
  60. ffi_call(&cif, FFI_FN(cls_struct_12byte_fn), &res_dbl, args_dbl);
  61. /* { dg-output "7 4 9 1 5 3: 8 9 12" } */
  62. printf("res: %d %d %d\n", res_dbl.a, res_dbl.b, res_dbl.c);
  63. /* { dg-output "\nres: 8 9 12" } */
  64. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_12byte_gn, NULL, code) == FFI_OK);
  65. res_dbl.a = 0;
  66. res_dbl.b = 0;
  67. res_dbl.c = 0;
  68. res_dbl = ((cls_struct_12byte(*)(cls_struct_12byte, cls_struct_12byte))(code))(h_dbl, j_dbl);
  69. /* { dg-output "\n7 4 9 1 5 3: 8 9 12" } */
  70. printf("res: %d %d %d\n", res_dbl.a, res_dbl.b, res_dbl.c);
  71. /* { dg-output "\nres: 8 9 12" } */
  72. exit(0);
  73. }