tcalext2.pp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. { %cpu=i386 }
  2. { %KNOWNRUNERROR=2,i386 long double array problem }
  3. {****************************************************************}
  4. { CODE GENERATOR TEST PROGRAM }
  5. {****************************************************************}
  6. { NODE TESTED : secondcalln() }
  7. {****************************************************************}
  8. { PRE-REQUISITES: secondload() }
  9. { secondassign() }
  10. { secondcalln() }
  11. { secondadd() }
  12. { secondtypeconv() }
  13. {****************************************************************}
  14. { DEFINES: }
  15. {****************************************************************}
  16. { REMARKS: This tests a subset of the secondcalln() , it }
  17. { verifies the usage of external cdecl }
  18. { modules compiled with C compilers. }
  19. {****************************************************************}
  20. {$ifndef USE_PASCAL_OBJECT}
  21. {$MODE OBJFPC}
  22. {$STATIC ON}
  23. {$R+}
  24. uses strings;
  25. {$L ctest.o}
  26. {$endif USE_PASCAL_OBJECT}
  27. { Use C alignment of records }
  28. {$PACKRECORDS C}
  29. const
  30. RESULT_U8BIT = $55;
  31. RESULT_U16BIT = $500F;
  32. RESULT_U32BIT = $500F0000;
  33. RESULT_U64BIT = $1BCDABCD;
  34. RESULT_S16BIT = -12;
  35. RESULT_S32BIT = -120;
  36. RESULT_S64BIT = -12000;
  37. RESULT_FLOAT = 14.54;
  38. RESULT_DOUBLE = 15.54;
  39. RESULT_LONGDOUBLE = 16.54;
  40. RESULT_PCHAR = 'Hello world';
  41. type
  42. _1byte_ = record
  43. u8 : byte;
  44. end;
  45. _3byte_ = record
  46. u8 : byte;
  47. u16 : word;
  48. end;
  49. _3byte_s = record
  50. u16 : word;
  51. w8 : byte;
  52. end;
  53. _5byte_ = record
  54. u8 : byte;
  55. u32 : cardinal;
  56. end;
  57. _7byte_ = record
  58. u8: byte;
  59. s64: int64;
  60. u16: word;
  61. end;
  62. byte_array = array [0..1] of byte;
  63. word_array = array [0..1] of word;
  64. cardinal_array = array [0..1] of cardinal;
  65. qword_array = array [0..1] of qword;
  66. smallint_array = array [0..1] of smallint;
  67. longint_array = array [0..1] of longint;
  68. int64_array = array [0..1] of int64;
  69. single_array = array [0..1] of single;
  70. double_array = array [0..1] of double;
  71. extended_array = array [0..1] of extended;
  72. { simple parameter passing }
  73. procedure test_param_u8(x: byte); cdecl; external;
  74. procedure test_param_u16(x : word); cdecl; external;
  75. procedure test_param_u32(x: cardinal); cdecl; external;
  76. procedure test_param_u64(x: qword); cdecl; external;
  77. procedure test_param_s16(x : smallint); cdecl; external;
  78. procedure test_param_s32(x: longint); cdecl; external;
  79. procedure test_param_s64(x: int64); cdecl; external;
  80. procedure test_param_float(x : single); cdecl; external;
  81. procedure test_param_double(x: double); cdecl; external;
  82. {$ifdef FPC_HAS_TYPE_EXTENDED}
  83. procedure test_param_longdouble(x: extended); cdecl; external;
  84. {$endif FPC_HAS_TYPE_EXTENDED}
  85. procedure test_param_var_u8(var x: byte); cdecl; external;
  86. { array parameter passing }
  87. procedure test_array_param_u8(x: byte_array); cdecl; external;
  88. procedure test_array_param_u16(x : word_array); cdecl; external;
  89. procedure test_array_param_u32(x: cardinal_array); cdecl; external;
  90. procedure test_array_param_u64(x: qword_array); cdecl; external;
  91. procedure test_array_param_s16(x :smallint_array); cdecl; external;
  92. procedure test_array_param_s32(x: longint_array); cdecl; external;
  93. procedure test_array_param_s64(x: int64_array); cdecl; external;
  94. procedure test_array_param_float(x : single_array); cdecl; external;
  95. procedure test_array_param_double(x: double_array); cdecl; external;
  96. {$ifdef FPC_HAS_TYPE_EXTENDED}
  97. procedure test_array_param_longdouble(x: extended_array); cdecl; external;
  98. {$endif FPC_HAS_TYPE_EXTENDED}
  99. { mixed parameter passing }
  100. procedure test_param_mixed_u16(z: byte; x : word; y :byte); cdecl; external;
  101. procedure test_param_mixed_u32(z: byte; x: cardinal; y: byte); cdecl; external;
  102. procedure test_param_mixed_s64(z: byte; x: int64; y: byte); cdecl; external;
  103. procedure test_param_mixed_float(x: single; y: byte); cdecl; external;
  104. procedure test_param_mixed_double(x: double; y: byte); cdecl; external;
  105. procedure test_param_mixed_long_double(x: extended; y: byte); cdecl; external;
  106. procedure test_param_mixed_var_u8(var x: byte;y:byte); cdecl; external;
  107. { structure parameter testing }
  108. procedure test_param_struct_tiny(buffer : _1BYTE_); cdecl; external;
  109. procedure test_param_struct_small(buffer : _3BYTE_); cdecl; external;
  110. procedure test_param_struct_small_s(buffer : _3BYTE_S); cdecl; external;
  111. procedure test_param_struct_medium(buffer : _5BYTE_); cdecl; external;
  112. procedure test_param_struct_large(buffer : _7BYTE_); cdecl; external;
  113. { mixed with structure parameter testing }
  114. procedure test_param_mixed_struct_tiny(buffer : _1BYTE_; y :byte); cdecl; external;
  115. procedure test_param_mixed_struct_small(buffer : _3BYTE_; y :byte); cdecl; external;
  116. procedure test_param_mixed_struct_small_s(buffer : _3BYTE_S; y :byte); cdecl; external;
  117. procedure test_param_mixed_struct_medium(buffer : _5BYTE_; y :byte); cdecl; external;
  118. procedure test_param_mixed_struct_large(buffer : _7BYTE_; y :byte); cdecl; external;
  119. { function result value testing }
  120. function test_function_u8: byte; cdecl; external;
  121. function test_function_u16: word; cdecl; external;
  122. function test_function_u32: cardinal; cdecl; external;
  123. function test_function_u64: qword; cdecl; external;
  124. function test_function_s16: smallint; cdecl; external;
  125. function test_function_s32: longint; cdecl; external;
  126. function test_function_s64: int64; cdecl; external;
  127. function test_function_pchar: pchar; cdecl; external;
  128. function test_function_float : single; cdecl; external;
  129. function test_function_double : double; cdecl; external;
  130. {$ifdef FPC_HAS_TYPE_EXTENDED}
  131. function test_function_longdouble: extended; cdecl; external;
  132. {$endif FPC_HAS_TYPE_EXTENDED}
  133. function test_function_tiny_struct : _1byte_; cdecl; external;
  134. function test_function_small_struct : _3byte_; cdecl; external;
  135. function test_function_small_struct_s : _3byte_s; cdecl; external;
  136. function test_function_medium_struct : _5byte_; cdecl; external;
  137. function test_function_struct : _7byte_; cdecl; external;
  138. var
  139. global_u8bit : byte; cvar; external;
  140. global_u16bit : word; cvar; external;
  141. global_u32bit : cardinal; cvar;external;
  142. global_u64bit : qword; cvar; external;
  143. global_s16bit : smallint; cvar; external;
  144. global_s32bit : longint; cvar;external;
  145. global_s64bit : int64; cvar; external;
  146. global_float : single; cvar;external;
  147. global_double : double; cvar;external;
  148. global_long_double : extended; cvar; external;
  149. value_u8bit : byte;
  150. value_s16bit : smallint;
  151. value_s32bit : longint;
  152. value_s64bit : int64;
  153. value_u16bit : word;
  154. value_u32bit : cardinal;
  155. value_u64bit : qword;
  156. value_float : single;
  157. value_double : double;
  158. value_long_double : extended;
  159. array_u8bit : array [0..1] of byte;
  160. array_s16bit : array [0..1] of smallint;
  161. array_s32bit : array [0..1] of longint;
  162. array_s64bit : array [0..1] of int64;
  163. array_u16bit : array [0..1] of word;
  164. array_u32bit : array [0..1] of cardinal;
  165. array_u64bit : array [0..1] of qword;
  166. array_float : array [0..1] of single;
  167. array_double : array [0..1] of double;
  168. array_long_double : array [0..1] of extended;
  169. procedure clear_globals;
  170. begin
  171. global_u8bit := 0;
  172. global_u16bit := 0;
  173. global_u32bit := 0;
  174. global_u64bit := 0;
  175. global_s16bit := 0;
  176. global_s32bit := 0;
  177. global_s64bit := 0;
  178. global_float := 0.0;
  179. global_double := 0.0;
  180. global_long_double := 0.0;
  181. end;
  182. procedure clear_values;
  183. begin
  184. value_u8bit := 0;
  185. value_u16bit := 0;
  186. value_u32bit := 0;
  187. value_u64bit := 0;
  188. value_s16bit := 0;
  189. value_s32bit := 0;
  190. value_s64bit := 0;
  191. value_float := 0.0;
  192. value_double := 0.0;
  193. value_long_double := 0.0;
  194. end;
  195. const
  196. has_errors : boolean = false;
  197. known_bug_about_extended_array_present : boolean = false;
  198. procedure fail;
  199. begin
  200. WriteLn('Failed!');
  201. has_errors:=true;
  202. end;
  203. procedure dotest;
  204. var failed : boolean;
  205. tinystruct : _1BYTE_;
  206. smallstruct : _3BYTE_;
  207. smallstruct_s : _3BYTE_S;
  208. mediumstruct : _5BYTE_;
  209. bigstruct : _7BYTE_;
  210. pc: pchar;
  211. begin
  212. failed := false;
  213. clear_values;
  214. clear_globals;
  215. {$ifdef FPC_HAS_TYPE_EXTENDED}
  216. array_long_double[1] := RESULT_LONGDOUBLE;
  217. test_array_param_longdouble(array_long_double);
  218. if trunc(global_long_double) <> trunc(RESULT_LONGDOUBLE) then
  219. begin
  220. if sizeof(global_long_double)=10 then
  221. begin
  222. writeln('extended size is incompatible with C');
  223. writeln('this will lead to failures if long doubles');
  224. writeln('are used as arrays of members of packed structures');
  225. halt(2);
  226. end
  227. else
  228. failed := true;
  229. end;
  230. If failed then
  231. fail
  232. else
  233. WriteLn('Passed!');
  234. {$endif FPC_HAS_TYPE_EXTENDED}
  235. if has_errors then
  236. Halt(1);
  237. end;
  238. begin
  239. dotest;
  240. end.