ptest.pp 12 KB

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