tcalvar4.pp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. {****************************************************************}
  2. { CODE GENERATOR TEST PROGRAM }
  3. { By Carl Eric Codere }
  4. {****************************************************************}
  5. { NODE TESTED : secondcallparan() }
  6. {****************************************************************}
  7. { PRE-REQUISITES: secondload() }
  8. { secondassign() }
  9. { secondtypeconv() }
  10. { secondtryexcept() }
  11. { secondcalln() }
  12. { secondadd() }
  13. {****************************************************************}
  14. { DEFINES: }
  15. { FPC = Target is FreePascal compiler }
  16. {****************************************************************}
  17. { REMARKS: This tests a subset of the secondcalln() node }
  18. { (var parameters with cdecl calling convention) }
  19. {****************************************************************}
  20. program tcalvar4;
  21. {$ifdef fpc}
  22. {$mode objfpc}
  23. {$INLINE ON}
  24. {$endif}
  25. {$R+}
  26. {$P-}
  27. {$V+}
  28. {$ifdef VER70}
  29. {$define tp}
  30. {$endif}
  31. { REAL should map to single or double }
  32. { so it is not checked, since single }
  33. { double nodes are checked. }
  34. { assumes that enumdef is the same as orddef (same storage format) }
  35. const
  36. { should be defined depending on CPU target }
  37. {$ifdef fpc}
  38. {$ifdef cpu68k}
  39. BIG_INDEX = 8000;
  40. SMALL_INDEX = 13;
  41. {$else}
  42. BIG_INDEX = 33000;
  43. SMALL_INDEX = 13; { value should not be aligned! }
  44. {$endif}
  45. {$else}
  46. BIG_INDEX = 33000;
  47. SMALL_INDEX = 13; { value should not be aligned! }
  48. {$endif}
  49. RESULT_U8BIT = $55;
  50. RESULT_U16BIT = $500F;
  51. RESULT_S32BIT = $500F0000;
  52. RESULT_S64BIT = $500F0000;
  53. RESULT_S32REAL = 1777.12;
  54. RESULT_S64REAL = 3444.24;
  55. RESULT_BOOL8BIT = 1;
  56. RESULT_BOOL16BIT = 1;
  57. RESULT_BOOL32BIT = 1;
  58. RESULT_PCHAR = 'Hello world';
  59. RESULT_BIGSTRING = 'Hello world';
  60. RESULT_SMALLSTRING = 'H';
  61. RESULT_CHAR = 'I';
  62. RESULT_BOOLEAN = TRUE;
  63. type
  64. {$ifdef fpc}
  65. pbytearr=^byte;
  66. {$else}
  67. pbytearr=^tbytearr;
  68. tbytearr=array[0..$fffffff] of byte;
  69. {$endif}
  70. tclass1 = class
  71. end;
  72. tprocedure = procedure;
  73. tsmallrecord =
  74. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  75. packed
  76. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  77. record
  78. b: byte;
  79. w: word;
  80. end;
  81. tlargerecord = packed record
  82. b: array[1..BIG_INDEX] of byte;
  83. end;
  84. tsmallarray = packed array[1..SMALL_INDEX] of byte;
  85. tsmallsetenum =
  86. (A_A,A_B,A_C,A_D);
  87. tsmallset = set of tsmallsetenum;
  88. tlargeset = set of char;
  89. tsmallstring = string[2];
  90. var
  91. global_u8bit : byte;
  92. global_u16bit : word;
  93. global_s32bit : longint;
  94. global_s64bit : int64;
  95. global_s32real : single;
  96. global_s64real : double;
  97. global_ptr : pchar;
  98. global_proc : tprocedure;
  99. global_class : tclass1;
  100. global_bigstring : shortstring;
  101. global_boolean : boolean;
  102. global_char : char;
  103. value_u8bit : byte;
  104. value_u16bit : word;
  105. value_s32bit : longint;
  106. value_s64bit : int64;
  107. value_s32real : single;
  108. value_s64real : double;
  109. value_proc : tprocedure;
  110. value_ptr : pchar;
  111. value_class : tclass1;
  112. value_smallrec : tsmallrecord;
  113. value_largerec : tlargerecord;
  114. value_smallset : tsmallset;
  115. value_smallstring : tsmallstring;
  116. value_bigstring : shortstring;
  117. value_largeset : tlargeset;
  118. value_smallarray : tsmallarray;
  119. value_boolean : boolean;
  120. value_char : char;
  121. procedure fail;
  122. begin
  123. WriteLn('Failure.');
  124. halt(1);
  125. end;
  126. procedure clear_globals;
  127. begin
  128. global_u8bit := 0;
  129. global_u16bit := 0;
  130. global_s32bit := 0;
  131. global_s64bit := 0;
  132. global_s32real := 0.0;
  133. global_s64real := 0.0;
  134. global_ptr := nil;
  135. global_proc := nil;
  136. global_class := nil;
  137. global_bigstring := '';
  138. global_boolean := false;
  139. global_char := #0;
  140. end;
  141. procedure clear_values;
  142. begin
  143. value_u8bit := 0;
  144. value_u16bit := 0;
  145. value_s32bit := 0;
  146. value_s64bit := 0;
  147. value_s32real := 0.0;
  148. value_s64real := 0.0;
  149. value_proc := nil;
  150. value_ptr := nil;
  151. value_class := nil;
  152. fillchar(value_smallrec, sizeof(value_smallrec), #0);
  153. fillchar(value_largerec, sizeof(value_largerec), #0);
  154. value_smallset := [];
  155. value_smallstring := '';
  156. value_bigstring := '';
  157. value_largeset := [];
  158. fillchar(value_smallarray, sizeof(value_smallarray), #0);
  159. value_boolean := false;
  160. value_char:=#0;
  161. end;
  162. procedure testprocedure;
  163. begin
  164. end;
  165. function getu8bit : byte;
  166. begin
  167. getu8bit:=RESULT_U8BIT;
  168. end;
  169. function getu16bit: word;
  170. begin
  171. getu16bit:=RESULT_U16BIT;
  172. end;
  173. function gets32bit: longint;
  174. begin
  175. gets32bit:=RESULT_S32BIT;
  176. end;
  177. function gets64bit: int64;
  178. begin
  179. gets64bit:=RESULT_S64BIT;
  180. end;
  181. function gets32real: single;
  182. begin
  183. gets32real:=RESULT_S32REAL;
  184. end;
  185. function gets64real: double;
  186. begin
  187. gets64real:=RESULT_S64REAL;
  188. end;
  189. {************************************************************************}
  190. { VAR PARAMETERS }
  191. {************************************************************************}
  192. procedure proc_var_s32bit(var v : longint);cdecl;
  193. begin
  194. v:=RESULT_S32BIT;
  195. end;
  196. procedure proc_var_s64bit(var v: int64);cdecl;
  197. begin
  198. v:=RESULT_S64BIT;
  199. end;
  200. procedure proc_var_u8bit(var v: byte);cdecl;
  201. begin
  202. v:=RESULT_U8BIT;
  203. end;
  204. procedure proc_var_smallrecord(var smallrec : tsmallrecord);cdecl;
  205. begin
  206. smallrec.b := RESULT_U8BIT;
  207. smallrec.w := RESULT_U16BIT;
  208. end;
  209. procedure proc_var_largerecord(var largerec : tlargerecord);cdecl;
  210. begin
  211. largerec.b[1] := RESULT_U8BIT;
  212. largerec.b[2] := RESULT_U8BIT;
  213. end;
  214. procedure proc_var_smallset(var smallset : tsmallset);cdecl;
  215. begin
  216. smallset := [A_A,A_D];
  217. end;
  218. procedure proc_var_largeset(var largeset : tlargeset);cdecl;
  219. begin
  220. largeset:= largeset + ['I'];
  221. end;
  222. procedure proc_var_smallstring(var s:tsmallstring);cdecl;
  223. begin
  224. s:=RESULT_SMALLSTRING;
  225. end;
  226. procedure proc_var_bigstring(var s:shortstring);cdecl;
  227. begin
  228. s:=RESULT_BIGSTRING;
  229. end;
  230. procedure proc_var_openstring(var s: OpenString);cdecl;
  231. begin
  232. global_u8bit := {high(s) is not available with cdecl}255;
  233. s:=RESULT_SMALLSTRING;
  234. end;
  235. procedure proc_var_smallarray(var arr : tsmallarray);cdecl;
  236. begin
  237. arr[SMALL_INDEX] := RESULT_U8BIT;
  238. arr[1] := RESULT_U8BIT;
  239. end;
  240. procedure proc_var_formaldef_array(var buf);cdecl;
  241. var
  242. p: pbytearr;
  243. begin
  244. { array is indexed from 1 }
  245. p := @buf;
  246. p[SMALL_INDEX-1] := RESULT_U8BIT;
  247. p[0] := RESULT_U8BIT;
  248. end;
  249. procedure proc_var_formaldef_string(var buf);cdecl;
  250. var
  251. p: pbytearr;
  252. begin
  253. { array is indexed from 1 }
  254. p := @buf;
  255. p[SMALL_INDEX-1] := RESULT_U8BIT;
  256. p[0] := RESULT_U8BIT;
  257. end;
  258. {************************************************************************}
  259. { MIXED VAR PARAMETERS }
  260. {************************************************************************}
  261. procedure proc_var_s32bit_mixed(b1 : byte;var v : longint; b2: byte);cdecl;
  262. begin
  263. v:=RESULT_S32BIT;
  264. value_u8bit := RESULT_U8BIT;
  265. end;
  266. procedure proc_var_s64bit_mixed(b1 : byte;var v: int64; b2: byte);cdecl;
  267. begin
  268. v:=RESULT_S64BIT;
  269. value_u8bit := RESULT_U8BIT;
  270. end;
  271. procedure proc_var_u8bit_mixed(b1 : byte;var v: byte; b2: byte);cdecl;
  272. begin
  273. v:=RESULT_U8BIT;
  274. value_u8bit := RESULT_U8BIT;
  275. end;
  276. procedure proc_var_smallrecord_mixed(b1 : byte; var smallrec : tsmallrecord; b2: byte);cdecl;
  277. begin
  278. smallrec.b := RESULT_U8BIT;
  279. smallrec.w := RESULT_U16BIT;
  280. value_u8bit := RESULT_U8BIT;
  281. end;
  282. procedure proc_var_largerecord_mixed(b1 : byte; var largerec : tlargerecord; b2: byte);cdecl;
  283. begin
  284. largerec.b[1] := RESULT_U8BIT;
  285. largerec.b[2] := RESULT_U8BIT;
  286. value_u8bit := RESULT_U8BIT;
  287. end;
  288. procedure proc_var_smallset_mixed(b1 : byte; var smallset : tsmallset; b2: byte);cdecl;
  289. begin
  290. smallset := [A_A,A_D];
  291. value_u8bit := RESULT_U8BIT;
  292. end;
  293. procedure proc_var_largeset_mixed(b1 : byte; var largeset : tlargeset; b2: byte);cdecl;
  294. begin
  295. largeset:= largeset + ['I'];
  296. value_u8bit := RESULT_U8BIT;
  297. end;
  298. procedure proc_var_smallstring_mixed(b1 : byte; var s:tsmallstring; b2: byte);cdecl;
  299. begin
  300. s:=RESULT_SMALLSTRING;
  301. value_u8bit := RESULT_U8BIT;
  302. end;
  303. procedure proc_var_bigstring_mixed(b1 : byte; var s:shortstring; b2: byte);cdecl;
  304. begin
  305. s:=RESULT_BIGSTRING;
  306. value_u8bit := RESULT_U8BIT;
  307. end;
  308. procedure proc_var_openstring_mixed(b1 : byte; var s: OpenString; b2: byte);cdecl;
  309. begin
  310. global_u8bit := {high(s) is not available with cdecl}255;
  311. s:=RESULT_SMALLSTRING;
  312. value_u8bit := RESULT_U8BIT;
  313. end;
  314. procedure proc_var_smallarray_mixed(b1 : byte; var arr : tsmallarray; b2: byte);cdecl;
  315. begin
  316. arr[SMALL_INDEX] := RESULT_U8BIT;
  317. arr[1] := RESULT_U8BIT;
  318. value_u8bit := RESULT_U8BIT;
  319. end;
  320. procedure proc_var_formaldef_array_mixed(b1 : byte; var buf; b2: byte);cdecl;
  321. var
  322. p: pbytearr;
  323. begin
  324. { array is indexed from 1 }
  325. p := @buf;
  326. p[SMALL_INDEX-1] := RESULT_U8BIT;
  327. p[0] := RESULT_U8BIT;
  328. value_u8bit := RESULT_U8BIT;
  329. end;
  330. procedure proc_var_formaldef_string_mixed(b1 : byte; var buf; b2: byte);cdecl;
  331. var
  332. p: pbytearr;
  333. begin
  334. { array is indexed from 1 }
  335. p := @buf;
  336. p[SMALL_INDEX-1] := RESULT_U8BIT;
  337. p[0] := RESULT_U8BIT;
  338. value_u8bit := RESULT_U8BIT;
  339. end;
  340. var
  341. failed: boolean;
  342. pp : ^pchar;
  343. begin
  344. {***************************** NORMAL TESTS *******************************}
  345. clear_globals;
  346. clear_values;
  347. failed:=false;
  348. write('Var parameter test (src : LOC_REFERENCE (orddef)))...');
  349. proc_var_s32bit(global_s32bit);
  350. if global_s32bit <> RESULT_S32BIT then
  351. failed:=true;
  352. clear_globals;
  353. clear_values;
  354. proc_var_s64bit(global_s64bit);
  355. if global_s64bit <> RESULT_S64BIT then
  356. failed:=true;
  357. clear_globals;
  358. clear_values;
  359. proc_var_u8bit(global_u8bit);
  360. if global_u8bit <> RESULT_U8BIT then
  361. failed:=true;
  362. if failed then
  363. fail
  364. else
  365. WriteLn('Passed!');
  366. write('Var parameter test (src : LOC_REFERENCE (recorddef)))...');
  367. clear_globals;
  368. clear_values;
  369. failed := false;
  370. proc_var_smallrecord(value_smallrec);
  371. if (value_smallrec.b <> RESULT_U8BIT) or (value_smallrec.w <> RESULT_U16BIT) then
  372. failed := true;
  373. clear_globals;
  374. clear_values;
  375. proc_var_largerecord(value_largerec);
  376. if (value_largerec.b[1] <> RESULT_U8BIT) or (value_largerec.b[2] <> RESULT_U8BIT) then
  377. failed := true;
  378. if failed then
  379. fail
  380. else
  381. WriteLn('Passed!');
  382. write('var parameter test (src : LOC_REFERENCE (setdef)))...');
  383. clear_globals;
  384. clear_values;
  385. failed := false;
  386. proc_var_smallset(value_smallset);
  387. if (not (A_A in value_smallset)) or (not (A_D in value_smallset)) then
  388. failed := true;
  389. clear_globals;
  390. clear_values;
  391. proc_var_largeset(value_largeset);
  392. if not ('I' in value_largeset) then
  393. failed := true;
  394. if failed then
  395. fail
  396. else
  397. WriteLn('Passed!');
  398. write('var parameter test (src : LOC_REFERENCE (stringdef)))...');
  399. clear_globals;
  400. clear_values;
  401. failed := false;
  402. proc_var_smallstring(value_smallstring);
  403. if value_smallstring <> RESULT_SMALLSTRING then
  404. failed := true;
  405. clear_globals;
  406. clear_values;
  407. proc_var_bigstring(value_bigstring);
  408. if value_bigstring <> RESULT_BIGSTRING then
  409. failed := true;
  410. clear_globals;
  411. clear_values;
  412. proc_var_openstring(value_smallstring);
  413. if (value_smallstring <> RESULT_SMALLSTRING) or
  414. { high is not passed to cdecl'ared functions thus
  415. value_smallstring should be 255 on retyurn PM }
  416. (global_u8bit <> {high(value_smallstring)}255) then
  417. failed := true;
  418. if failed then
  419. fail
  420. else
  421. WriteLn('Passed!');
  422. write('Var parameter test (src : LOC_REFERENCE (formaldef)))...');
  423. clear_globals;
  424. clear_values;
  425. failed:=false;
  426. proc_var_formaldef_array(value_smallarray);
  427. if (value_smallarray[SMALL_INDEX] <> RESULT_U8BIT) or (value_smallarray[1] <> RESULT_U8BIT) then
  428. failed := true;
  429. if failed then
  430. fail
  431. else
  432. WriteLn('Passed!');
  433. write('Var parameter test (src : LOC_REFERENCE (arraydef)))...');
  434. clear_globals;
  435. clear_values;
  436. failed:=false;
  437. value_smallarray[SMALL_INDEX] := RESULT_U8BIT;
  438. proc_var_smallarray(value_smallarray);
  439. if (value_smallarray[SMALL_INDEX] <> RESULT_U8BIT) or (value_smallarray[1] <> RESULT_U8BIT) then
  440. failed := true;
  441. if failed then
  442. fail
  443. else
  444. WriteLn('Passed!');
  445. {***************************** MIXED TESTS *******************************}
  446. clear_globals;
  447. clear_values;
  448. failed:=false;
  449. write('Var parameter test (src : LOC_REFERENCE (orddef)))...');
  450. proc_var_s32bit_mixed(RESULT_U8BIT, global_s32bit, RESULT_U8BIT);
  451. if global_s32bit <> RESULT_S32BIT then
  452. failed:=true;
  453. if value_u8bit <> RESULT_U8BIT then
  454. failed := true;
  455. clear_globals;
  456. clear_values;
  457. proc_var_s64bit_mixed(RESULT_U8BIT, global_s64bit, RESULT_U8BIT);
  458. if global_s64bit <> RESULT_S64BIT then
  459. failed:=true;
  460. if value_u8bit <> RESULT_U8BIT then
  461. failed := true;
  462. clear_globals;
  463. clear_values;
  464. proc_var_u8bit_mixed(RESULT_U8BIT, global_u8bit, RESULT_U8BIT);
  465. if global_u8bit <> RESULT_U8BIT then
  466. failed:=true;
  467. if value_u8bit <> RESULT_U8BIT then
  468. failed := true;
  469. if failed then
  470. fail
  471. else
  472. WriteLn('Passed!');
  473. write('Var parameter test (src : LOC_REFERENCE (recorddef)))...');
  474. clear_globals;
  475. clear_values;
  476. failed := false;
  477. proc_var_smallrecord_mixed(RESULT_U8BIT,value_smallrec, RESULT_U8BIT);
  478. if (value_smallrec.b <> RESULT_U8BIT) or (value_smallrec.w <> RESULT_U16BIT) then
  479. failed := true;
  480. if value_u8bit <> RESULT_U8BIT then
  481. failed := true;
  482. clear_globals;
  483. clear_values;
  484. proc_var_largerecord_mixed(RESULT_U8BIT, value_largerec, RESULT_U8BIT);
  485. if (value_largerec.b[1] <> RESULT_U8BIT) or (value_largerec.b[2] <> RESULT_U8BIT) then
  486. failed := true;
  487. if value_u8bit <> RESULT_U8BIT then
  488. failed := true;
  489. if failed then
  490. fail
  491. else
  492. WriteLn('Passed!');
  493. write('var parameter test (src : LOC_REFERENCE (setdef)))...');
  494. clear_globals;
  495. clear_values;
  496. failed := false;
  497. proc_var_smallset_mixed(RESULT_U8BIT, value_smallset, RESULT_U8BIT);
  498. if (not (A_A in value_smallset)) or (not (A_D in value_smallset)) then
  499. failed := true;
  500. if value_u8bit <> RESULT_U8BIT then
  501. failed := true;
  502. clear_globals;
  503. clear_values;
  504. proc_var_largeset_mixed(RESULT_U8BIT, value_largeset, RESULT_U8BIT);
  505. if not ('I' in value_largeset) then
  506. failed := true;
  507. if value_u8bit <> RESULT_U8BIT then
  508. failed := true;
  509. if failed then
  510. fail
  511. else
  512. WriteLn('Passed!');
  513. write('var parameter test (src : LOC_REFERENCE (stringdef)))...');
  514. clear_globals;
  515. clear_values;
  516. failed := false;
  517. proc_var_smallstring_mixed(RESULT_U8BIT, value_smallstring, RESULT_U8BIT);
  518. if value_smallstring <> RESULT_SMALLSTRING then
  519. failed := true;
  520. if value_u8bit <> RESULT_U8BIT then
  521. failed := true;
  522. clear_globals;
  523. clear_values;
  524. proc_var_bigstring_mixed(RESULT_U8BIT, value_bigstring,RESULT_U8BIT);
  525. if value_bigstring <> RESULT_BIGSTRING then
  526. failed := true;
  527. if value_u8bit <> RESULT_U8BIT then
  528. failed := true;
  529. clear_globals;
  530. clear_values;
  531. proc_var_openstring_mixed(RESULT_U8BIT, value_smallstring, RESULT_U8BIT);
  532. if (value_smallstring <> RESULT_SMALLSTRING) or
  533. { high is not passed to cdecl'ared functions thus
  534. value_smallstring should be 255 on retyurn PM }
  535. (global_u8bit <> {high(value_smallstring)}255) then
  536. failed := true;
  537. if value_u8bit <> RESULT_U8BIT then
  538. failed := true;
  539. if failed then
  540. fail
  541. else
  542. WriteLn('Passed!');
  543. write('Var parameter test (src : LOC_REFERENCE (formaldef)))...');
  544. clear_globals;
  545. clear_values;
  546. failed:=false;
  547. proc_var_formaldef_array_mixed(RESULT_U8BIT, value_smallarray, RESULT_U8BIT);
  548. if (value_smallarray[SMALL_INDEX] <> RESULT_U8BIT) or (value_smallarray[1] <> RESULT_U8BIT) then
  549. failed := true;
  550. if value_u8bit <> RESULT_U8BIT then
  551. failed := true;
  552. if failed then
  553. fail
  554. else
  555. WriteLn('Passed!');
  556. write('Var parameter test (src : LOC_REFERENCE (arraydef)))...');
  557. clear_globals;
  558. clear_values;
  559. failed:=false;
  560. value_smallarray[SMALL_INDEX] := RESULT_U8BIT;
  561. proc_var_smallarray_mixed(RESULT_U8BIT, value_smallarray, RESULT_U8BIT);
  562. if (value_smallarray[SMALL_INDEX] <> RESULT_U8BIT) or (value_smallarray[1] <> RESULT_U8BIT) then
  563. failed := true;
  564. if value_u8bit <> RESULT_U8BIT then
  565. failed := true;
  566. if failed then
  567. fail
  568. else
  569. WriteLn('Passed!');
  570. end.