va_1.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Area: ffi_call
  2. Purpose: Test passing struct in variable argument lists.
  3. Limitations: none.
  4. PR: none.
  5. Originator: ARM Ltd. */
  6. /* { dg-do run } */
  7. /* { dg-output "" { xfail avr32*-*-* m68k-*-* alpha-*-* } } */
  8. #include "ffitest.h"
  9. #include <stdarg.h>
  10. struct small_tag
  11. {
  12. unsigned char a;
  13. unsigned char b;
  14. };
  15. struct large_tag
  16. {
  17. unsigned a;
  18. unsigned b;
  19. unsigned c;
  20. unsigned d;
  21. unsigned e;
  22. };
  23. static int
  24. test_fn (int n, ...)
  25. {
  26. va_list ap;
  27. struct small_tag s1;
  28. struct small_tag s2;
  29. struct large_tag l;
  30. unsigned char uc;
  31. signed char sc;
  32. unsigned short us;
  33. signed short ss;
  34. unsigned int ui;
  35. signed int si;
  36. unsigned long ul;
  37. signed long sl;
  38. float f;
  39. double d;
  40. va_start (ap, n);
  41. s1 = va_arg (ap, struct small_tag);
  42. l = va_arg (ap, struct large_tag);
  43. s2 = va_arg (ap, struct small_tag);
  44. uc = va_arg (ap, unsigned);
  45. sc = va_arg (ap, signed);
  46. us = va_arg (ap, unsigned);
  47. ss = va_arg (ap, signed);
  48. ui = va_arg (ap, unsigned int);
  49. si = va_arg (ap, signed int);
  50. ul = va_arg (ap, unsigned long);
  51. sl = va_arg (ap, signed long);
  52. f = va_arg (ap, double); /* C standard promotes float->double
  53. when anonymous */
  54. d = va_arg (ap, double);
  55. printf ("%u %u %u %u %u %u %u %u %u uc=%u sc=%d %u %d %u %d %lu %ld %f %f\n",
  56. s1.a, s1.b, l.a, l.b, l.c, l.d, l.e,
  57. s2.a, s2.b,
  58. uc, sc,
  59. us, ss,
  60. ui, si,
  61. ul, sl,
  62. f, d);
  63. va_end (ap);
  64. return n + 1;
  65. }
  66. int
  67. main (void)
  68. {
  69. ffi_cif cif;
  70. void* args[15];
  71. ffi_type* arg_types[15];
  72. ffi_type s_type;
  73. ffi_type *s_type_elements[3];
  74. ffi_type l_type;
  75. ffi_type *l_type_elements[6];
  76. struct small_tag s1;
  77. struct small_tag s2;
  78. struct large_tag l1;
  79. int n;
  80. ffi_arg res;
  81. unsigned char uc;
  82. signed char sc;
  83. unsigned short us;
  84. signed short ss;
  85. unsigned int ui;
  86. signed int si;
  87. unsigned long ul;
  88. signed long sl;
  89. double d1;
  90. double f1;
  91. s_type.size = 0;
  92. s_type.alignment = 0;
  93. s_type.type = FFI_TYPE_STRUCT;
  94. s_type.elements = s_type_elements;
  95. s_type_elements[0] = &ffi_type_uchar;
  96. s_type_elements[1] = &ffi_type_uchar;
  97. s_type_elements[2] = NULL;
  98. l_type.size = 0;
  99. l_type.alignment = 0;
  100. l_type.type = FFI_TYPE_STRUCT;
  101. l_type.elements = l_type_elements;
  102. l_type_elements[0] = &ffi_type_uint;
  103. l_type_elements[1] = &ffi_type_uint;
  104. l_type_elements[2] = &ffi_type_uint;
  105. l_type_elements[3] = &ffi_type_uint;
  106. l_type_elements[4] = &ffi_type_uint;
  107. l_type_elements[5] = NULL;
  108. arg_types[0] = &ffi_type_sint;
  109. arg_types[1] = &s_type;
  110. arg_types[2] = &l_type;
  111. arg_types[3] = &s_type;
  112. arg_types[4] = &ffi_type_uchar;
  113. arg_types[5] = &ffi_type_schar;
  114. arg_types[6] = &ffi_type_ushort;
  115. arg_types[7] = &ffi_type_sshort;
  116. arg_types[8] = &ffi_type_uint;
  117. arg_types[9] = &ffi_type_sint;
  118. arg_types[10] = &ffi_type_ulong;
  119. arg_types[11] = &ffi_type_slong;
  120. arg_types[12] = &ffi_type_double;
  121. arg_types[13] = &ffi_type_double;
  122. arg_types[14] = NULL;
  123. CHECK(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 14, &ffi_type_sint, arg_types) == FFI_OK);
  124. s1.a = 5;
  125. s1.b = 6;
  126. l1.a = 10;
  127. l1.b = 11;
  128. l1.c = 12;
  129. l1.d = 13;
  130. l1.e = 14;
  131. s2.a = 7;
  132. s2.b = 8;
  133. n = 41;
  134. uc = 9;
  135. sc = 10;
  136. us = 11;
  137. ss = 12;
  138. ui = 13;
  139. si = 14;
  140. ul = 15;
  141. sl = 16;
  142. f1 = 2.12;
  143. d1 = 3.13;
  144. args[0] = &n;
  145. args[1] = &s1;
  146. args[2] = &l1;
  147. args[3] = &s2;
  148. args[4] = &uc;
  149. args[5] = &sc;
  150. args[6] = &us;
  151. args[7] = &ss;
  152. args[8] = &ui;
  153. args[9] = &si;
  154. args[10] = &ul;
  155. args[11] = &sl;
  156. args[12] = &f1;
  157. args[13] = &d1;
  158. args[14] = NULL;
  159. ffi_call(&cif, FFI_FN(test_fn), &res, args);
  160. /* { dg-output "5 6 10 11 12 13 14 7 8 uc=9 sc=10 11 12 13 14 15 16 2.120000 3.130000" } */
  161. printf("res: %d\n", (int) res);
  162. /* { dg-output "\nres: 42" } */
  163. return 0;
  164. }