cls_align_pointer.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Area: ffi_call, closure_call
  2. Purpose: Check structure alignment of pointer.
  3. Limitations: none.
  4. PR: none.
  5. Originator: <hos@tamanegi.org> 20031203 */
  6. /* { dg-do run } */
  7. #include "ffitest.h"
  8. typedef struct cls_struct_align {
  9. unsigned char a;
  10. void *b;
  11. unsigned char c;
  12. } cls_struct_align;
  13. cls_struct_align cls_struct_align_fn(struct cls_struct_align a1,
  14. struct cls_struct_align a2)
  15. {
  16. struct cls_struct_align result;
  17. result.a = a1.a + a2.a;
  18. result.b = (void *)((uintptr_t)a1.b + (uintptr_t)a2.b);
  19. result.c = a1.c + a2.c;
  20. printf("%d %" PRIuPTR " %d %d %" PRIuPTR " %d: %d %" PRIuPTR " %d\n",
  21. a1.a, (uintptr_t)a1.b, a1.c,
  22. a2.a, (uintptr_t)a2.b, a2.c,
  23. result.a, (uintptr_t)result.b,
  24. result.c);
  25. return result;
  26. }
  27. static void
  28. cls_struct_align_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  29. void* userdata __UNUSED__)
  30. {
  31. struct cls_struct_align a1, a2;
  32. a1 = *(struct cls_struct_align*)(args[0]);
  33. a2 = *(struct cls_struct_align*)(args[1]);
  34. *(cls_struct_align*)resp = cls_struct_align_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_align g_dbl = { 12, (void *)4951, 127 };
  46. struct cls_struct_align f_dbl = { 1, (void *)9320, 13 };
  47. struct cls_struct_align 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_pointer;
  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_align_fn), &res_dbl, args_dbl);
  65. /* { dg-output "12 4951 127 1 9320 13: 13 14271 140" } */
  66. printf("res: %d %" PRIuPTR " %d\n", res_dbl.a, (uintptr_t)res_dbl.b, res_dbl.c);
  67. /* { dg-output "\nres: 13 14271 140" } */
  68. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_align_gn, NULL, code) == FFI_OK);
  69. res_dbl = ((cls_struct_align(*)(cls_struct_align, cls_struct_align))(code))(g_dbl, f_dbl);
  70. /* { dg-output "\n12 4951 127 1 9320 13: 13 14271 140" } */
  71. printf("res: %d %" PRIuPTR " %d\n", res_dbl.a, (uintptr_t)res_dbl.b, res_dbl.c);
  72. /* { dg-output "\nres: 13 14271 140" } */
  73. exit(0);
  74. }