ptest.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. {
  2. Program to test linking between C and pascal units.
  3. Pascal counter part
  4. }
  5. unit ptest;
  6. interface
  7. { Use C alignment of records }
  8. {$PACKRECORDS C}
  9. const
  10. RESULT_U8BIT = $55;
  11. RESULT_U16BIT = $500F;
  12. RESULT_U32BIT = $500F0000;
  13. RESULT_U64BIT = $1BCDABCD;
  14. RESULT_S16BIT = -12;
  15. RESULT_S32BIT = -120;
  16. RESULT_S64BIT = -12000;
  17. RESULT_FLOAT = 14.54;
  18. RESULT_DOUBLE = 15.54;
  19. RESULT_LONGDOUBLE = 16.54;
  20. RESULT_PCHAR = 'Hello world';
  21. type
  22. _1byte_ = record
  23. u8 : byte;
  24. end;
  25. _3byte_ = record
  26. u8 : byte;
  27. u16 : word;
  28. end;
  29. _3byte_s = record
  30. u16 : word;
  31. w8 : byte;
  32. end;
  33. _5byte_ = record
  34. u8 : byte;
  35. u32 : cardinal;
  36. end;
  37. _7byte_ = record
  38. u8: byte;
  39. s64: int64;
  40. u16: word;
  41. end;
  42. byte_array = array [0..1] of byte;
  43. word_array = array [0..1] of word;
  44. cardinal_array = array [0..1] of cardinal;
  45. qword_array = array [0..1] of qword;
  46. smallint_array = array [0..1] of smallint;
  47. longint_array = array [0..1] of longint;
  48. int64_array = array [0..1] of int64;
  49. single_array = array [0..1] of single;
  50. double_array = array [0..1] of double;
  51. extended_array = array [0..1] of extended;
  52. var
  53. global_u8bit : byte; cvar;
  54. global_u16bit : word; cvar;
  55. global_u32bit : cardinal; cvar;
  56. global_u64bit : qword; cvar;
  57. global_s16bit : smallint; cvar;
  58. global_s32bit : longint; cvar;
  59. global_s64bit : int64; cvar;
  60. global_float : single; cvar;
  61. global_double : double; cvar;
  62. global_long_double : extended; cvar;
  63. { simple parameter passing }
  64. procedure test_param_u8(x: byte); cdecl; public;
  65. procedure test_param_u16(x : word); cdecl; public;
  66. procedure test_param_u32(x: cardinal); cdecl; public;
  67. procedure test_param_u64(x: qword); cdecl; public;
  68. procedure test_param_s16(x : smallint); cdecl; public;
  69. procedure test_param_s32(x: longint); cdecl; public;
  70. procedure test_param_s64(x: int64); cdecl; public;
  71. procedure test_param_float(x : single); cdecl; public;
  72. procedure test_param_double(x: double); cdecl; public;
  73. procedure test_param_longdouble(x: extended); cdecl; public;
  74. procedure test_param_var_u8(var x: byte); cdecl; public;
  75. { array parameter passing }
  76. procedure test_array_param_u8(x: byte_array); cdecl; public;
  77. procedure test_array_param_u16(x : word_array); cdecl; public;
  78. procedure test_array_param_u32(x: cardinal_array); cdecl; public;
  79. procedure test_array_param_u64(x: qword_array); cdecl; public;
  80. procedure test_array_param_s16(x :smallint_array); cdecl; public;
  81. procedure test_array_param_s32(x: longint_array); cdecl; public;
  82. procedure test_array_param_s64(x: int64_array); cdecl; public;
  83. procedure test_array_param_float(x : single_array); cdecl; public;
  84. procedure test_array_param_double(x: double_array); cdecl; public;
  85. procedure test_array_param_longdouble(x: extended_array); cdecl; public;
  86. { mixed parameter passing }
  87. procedure test_param_mixed_u16(z: byte; x : word; y :byte); cdecl; public;
  88. procedure test_param_mixed_u32(z: byte; x: cardinal; y: byte); cdecl; public;
  89. procedure test_param_mixed_s64(z: byte; x: int64; y: byte); cdecl; public;
  90. procedure test_param_mixed_float(x: single; y: byte); cdecl; public;
  91. procedure test_param_mixed_double(x: double; y: byte); cdecl; public;
  92. procedure test_param_mixed_long_double(x: extended; y: byte); cdecl; public;
  93. procedure test_param_mixed_var_u8(var x: byte;y:byte); cdecl; public;
  94. { structure parameter testing }
  95. procedure test_param_struct_tiny(buffer : _1BYTE_); cdecl; public;
  96. procedure test_param_struct_small(buffer : _3BYTE_); cdecl; public;
  97. procedure test_param_struct_small_s(buffer : _3BYTE_S); cdecl; public;
  98. procedure test_param_struct_medium(buffer : _5BYTE_); cdecl; public;
  99. procedure test_param_struct_large(buffer : _7BYTE_); cdecl; public;
  100. { mixed with structure parameter testing }
  101. procedure test_param_mixed_struct_tiny(buffer : _1BYTE_; y :byte); cdecl; public;
  102. procedure test_param_mixed_struct_small(buffer : _3BYTE_; y :byte); cdecl; public;
  103. procedure test_param_mixed_struct_small_s(buffer : _3BYTE_S; y :byte); cdecl; public;
  104. procedure test_param_mixed_struct_medium(buffer : _5BYTE_; y :byte); cdecl; public;
  105. procedure test_param_mixed_struct_large(buffer : _7BYTE_; y :byte); cdecl; public;
  106. { function result value testing }
  107. function test_function_u8: byte; cdecl; public;
  108. function test_function_u16: word; cdecl; public;
  109. function test_function_u32: cardinal; cdecl; public;
  110. function test_function_u64: qword; cdecl; public;
  111. function test_function_s16: smallint; cdecl; public;
  112. function test_function_s32: longint; cdecl; public;
  113. function test_function_s64: int64; cdecl; public;
  114. function test_function_pchar: pchar; cdecl; public;
  115. function test_function_float : single; cdecl; public;
  116. function test_function_double : double; cdecl; public;
  117. function test_function_longdouble: extended; cdecl; public;
  118. function test_function_tiny_struct : _1byte_; cdecl; public;
  119. function test_function_small_struct : _3byte_; cdecl; public;
  120. function test_function_small_struct_s : _3byte_s; cdecl; public;
  121. function test_function_medium_struct : _5byte_; cdecl; public;
  122. function test_function_struct : _7byte_; cdecl; public;
  123. implementation
  124. { simple parameter passing }
  125. procedure test_param_u8(x: byte); cdecl; public;
  126. begin
  127. global_u8bit:=x;
  128. end;
  129. procedure test_param_u16(x : word); cdecl; public;
  130. begin
  131. global_u16bit:=x;
  132. end;
  133. procedure test_param_u32(x: cardinal); cdecl; public;
  134. begin
  135. global_u32bit:=x;
  136. end;
  137. procedure test_param_u64(x: qword); cdecl; public;
  138. begin
  139. global_u64bit:=x;
  140. end;
  141. procedure test_param_s16(x : smallint); cdecl; public;
  142. begin
  143. global_s16bit:=x;
  144. end;
  145. procedure test_param_s32(x: longint); cdecl; public;
  146. begin
  147. global_s32bit:=x;
  148. end;
  149. procedure test_param_s64(x: int64); cdecl; public;
  150. begin
  151. global_s64bit:=x;
  152. end;
  153. procedure test_param_float(x : single); cdecl; public;
  154. begin
  155. global_float:=x;
  156. end;
  157. procedure test_param_double(x: double); cdecl; public;
  158. begin
  159. global_double:=x;
  160. end;
  161. procedure test_param_longdouble(x: extended); cdecl; public;
  162. begin
  163. global_long_double:=x;
  164. end;
  165. procedure test_param_var_u8(var x: byte); cdecl; public;
  166. begin
  167. x:=RESULT_U8BIT;
  168. end;
  169. { array parameter passing }
  170. procedure test_array_param_u8(x: byte_array); cdecl; public;
  171. begin
  172. global_u8bit:=x[1];
  173. end;
  174. procedure test_array_param_u16(x : word_array); cdecl; public;
  175. begin
  176. global_u16bit:=x[1];
  177. end;
  178. procedure test_array_param_u32(x: cardinal_array); cdecl; public;
  179. begin
  180. global_u32bit:=x[1];
  181. end;
  182. procedure test_array_param_u64(x: qword_array); cdecl; public;
  183. begin
  184. global_u64bit:=x[1];
  185. end;
  186. procedure test_array_param_s16(x :smallint_array); cdecl; public;
  187. begin
  188. global_s16bit:=x[1];
  189. end;
  190. procedure test_array_param_s32(x: longint_array); cdecl; public;
  191. begin
  192. global_s32bit:=x[1];
  193. end;
  194. procedure test_array_param_s64(x: int64_array); cdecl; public;
  195. begin
  196. global_s64bit:=x[1];
  197. end;
  198. procedure test_array_param_float(x : single_array); cdecl; public;
  199. begin
  200. global_float:=x[1];
  201. end;
  202. procedure test_array_param_double(x: double_array); cdecl; public;
  203. begin
  204. global_double:=x[1];
  205. end;
  206. procedure test_array_param_longdouble(x: extended_array); cdecl; public;
  207. begin
  208. global_long_double:=x[1];
  209. end;
  210. { mixed parameter passing }
  211. procedure test_param_mixed_u16(z: byte; x : word; y :byte); cdecl; public;
  212. begin
  213. global_u16bit:=x;
  214. global_u8bit:=y;
  215. end;
  216. procedure test_param_mixed_u32(z: byte; x: cardinal; y: byte); cdecl; public;
  217. begin
  218. global_u32bit:=x;
  219. global_u8bit:=y;
  220. end;
  221. procedure test_param_mixed_s64(z: byte; x: int64; y: byte); cdecl; public;
  222. begin
  223. global_s64bit:=x;
  224. global_u8bit:=y;
  225. end;
  226. procedure test_param_mixed_float(x: single; y: byte); cdecl; public;
  227. begin
  228. global_float:=x;
  229. global_u8bit:=y;
  230. end;
  231. procedure test_param_mixed_double(x: double; y: byte); cdecl; public;
  232. begin
  233. global_double:=x;
  234. global_u8bit:=y;
  235. end;
  236. procedure test_param_mixed_long_double(x: extended; y: byte); cdecl; public;
  237. begin
  238. global_long_double:=x;
  239. global_u8bit:=y;
  240. end;
  241. procedure test_param_mixed_var_u8(var x: byte;y:byte); cdecl; public;
  242. begin
  243. x:=RESULT_U8BIT;
  244. global_u8bit:=y;
  245. end;
  246. { structure parameter testing }
  247. procedure test_param_struct_tiny(buffer : _1BYTE_); cdecl; public;
  248. begin
  249. global_u8bit:=buffer.u8;
  250. end;
  251. procedure test_param_struct_small(buffer : _3BYTE_); cdecl; public;
  252. begin
  253. global_u8bit:=buffer.u8;
  254. global_u16bit:=buffer.u16;
  255. end;
  256. procedure test_param_struct_small_s(buffer : _3BYTE_S); cdecl; public;
  257. begin
  258. global_u8bit:=buffer.w8;
  259. global_u16bit:=buffer.u16;
  260. end;
  261. procedure test_param_struct_medium(buffer : _5BYTE_); cdecl; public;
  262. begin
  263. global_u8bit:=buffer.u8;
  264. global_u32bit:=buffer.u32;
  265. end;
  266. procedure test_param_struct_large(buffer : _7BYTE_); cdecl; public;
  267. begin
  268. global_u8bit:=buffer.u8;
  269. global_u16bit:=buffer.u16;
  270. global_s64bit:=buffer.s64;
  271. end;
  272. { mixed with structure parameter testing }
  273. procedure test_param_mixed_struct_tiny(buffer : _1BYTE_; y :byte); cdecl; public;
  274. begin
  275. global_u8bit := y;
  276. end;
  277. procedure test_param_mixed_struct_small(buffer : _3BYTE_; y :byte); cdecl; public;
  278. begin
  279. global_u8bit := y;
  280. global_u16bit := buffer.u16;
  281. end;
  282. procedure test_param_mixed_struct_small_s(buffer : _3BYTE_S; y :byte); cdecl; public;
  283. begin
  284. global_u8bit := y;
  285. global_u16bit := buffer.u16;
  286. end;
  287. procedure test_param_mixed_struct_medium(buffer : _5BYTE_; y :byte); cdecl; public;
  288. begin
  289. global_u8bit := y;
  290. global_u32bit := buffer.u32;
  291. end;
  292. procedure test_param_mixed_struct_large(buffer : _7BYTE_; y :byte); cdecl; public;
  293. begin
  294. global_u8bit:=y;
  295. global_u16bit:=buffer.u16;
  296. global_s64bit:=buffer.s64;
  297. end;
  298. { function result value testing }
  299. function test_function_u8: byte; cdecl; public;
  300. begin
  301. test_function_u8:=RESULT_U8BIT;
  302. end;
  303. function test_function_u16: word; cdecl; public;
  304. begin
  305. test_function_u16:=RESULT_U16BIT;
  306. end;
  307. function test_function_u32: cardinal; cdecl; public;
  308. begin
  309. test_function_u32:=RESULT_U32BIT;
  310. end;
  311. function test_function_u64: qword; cdecl; public;
  312. begin
  313. test_function_u64:=RESULT_U64BIT;
  314. end;
  315. function test_function_s16: smallint; cdecl; public;
  316. begin
  317. test_function_s16:=RESULT_S16BIT;
  318. end;
  319. function test_function_s32: longint; cdecl; public;
  320. begin
  321. test_function_s32:=RESULT_S32BIT;
  322. end;
  323. function test_function_s64: int64; cdecl; public;
  324. begin
  325. test_function_s64:=RESULT_S64BIT;
  326. end;
  327. function test_function_pchar: pchar; cdecl; public;
  328. begin
  329. test_function_pchar:=RESULT_PCHAR;
  330. end;
  331. function test_function_float : single; cdecl; public;
  332. begin
  333. test_function_float:=RESULT_FLOAT;
  334. end;
  335. function test_function_double : double; cdecl; public;
  336. begin
  337. test_function_double:=RESULT_DOUBLE;
  338. end;
  339. function test_function_longdouble: extended; cdecl; public;
  340. begin
  341. test_function_longdouble:=RESULT_LONGDOUBLE;
  342. end;
  343. function test_function_tiny_struct : _1byte_; cdecl; public;
  344. begin
  345. test_function_tiny_struct.u8:=RESULT_U8BIT;
  346. end;
  347. function test_function_small_struct : _3byte_; cdecl; public;
  348. begin
  349. test_function_small_struct.u8:=RESULT_U8BIT;
  350. test_function_small_struct.u16:=RESULT_U16BIT;
  351. end;
  352. function test_function_small_struct_s : _3byte_s; cdecl; public;
  353. begin
  354. test_function_small_struct_s.w8:=RESULT_U8BIT;
  355. test_function_small_struct_s.u16:=RESULT_U16BIT;
  356. end;
  357. function test_function_medium_struct : _5byte_; cdecl; public;
  358. begin
  359. test_function_medium_struct.u8:=RESULT_U8BIT;
  360. test_function_medium_struct.u32:=RESULT_U32BIT;
  361. end;
  362. function test_function_struct : _7byte_; cdecl; public;
  363. begin
  364. test_function_struct.u8:=RESULT_U8BIT;
  365. test_function_struct.u16:=RESULT_U16BIT;
  366. test_function_struct.s64:=RESULT_S64BIT;
  367. end;
  368. end.