pstatmnt.pas 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl
  4. Does the parsing of the statements
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pstatmnt;
  19. interface
  20. uses tree;
  21. var
  22. { true, if we are in a except block }
  23. in_except_block : boolean;
  24. { reads a block }
  25. function block(islibrary : boolean) : ptree;
  26. { reads an assembler block }
  27. function assembler_block : ptree;
  28. implementation
  29. uses
  30. cobjects,scanner,globals,symtable,aasm,pass_1,
  31. types,hcodegen,files,verbose
  32. { processor specific stuff }
  33. {$ifdef i386}
  34. ,i386
  35. ,rai386
  36. ,ratti386
  37. ,radi386
  38. ,tgeni386
  39. {$endif}
  40. {$ifdef m68k}
  41. ,m68k
  42. ,tgen68k
  43. ,ag68kmit
  44. ,ra68k
  45. ,ag68kgas
  46. ,ag68kmot
  47. {$endif}
  48. { parser specific stuff, be careful consume is also defined to }
  49. { read assembler tokens }
  50. ,pbase,pexpr,pdecl;
  51. function statement : ptree;forward;
  52. function if_statement : ptree;
  53. var
  54. ex,if_a,else_a : ptree;
  55. begin
  56. consume(_IF);
  57. ex:=expr;
  58. consume(_THEN);
  59. if token<>_ELSE then
  60. if_a:=statement
  61. else
  62. if_a:=nil;
  63. if token=_ELSE then
  64. begin
  65. consume(_ELSE);
  66. else_a:=statement;
  67. end
  68. else
  69. else_a:=nil;
  70. if_statement:=genloopnode(ifn,ex,if_a,else_a,false);
  71. end;
  72. { creates a block (list) of statements, til the next END token }
  73. function statements_til_end : ptree;
  74. var
  75. first,last : ptree;
  76. begin
  77. first:=nil;
  78. while token<>_END do
  79. begin
  80. if first=nil then
  81. begin
  82. last:=gennode(anwein,nil,statement);
  83. first:=last;
  84. end
  85. else
  86. begin
  87. last^.left:=gennode(anwein,nil,statement);
  88. last:=last^.left;
  89. end;
  90. if token<>SEMICOLON then
  91. break
  92. else
  93. consume(SEMICOLON);
  94. while token=SEMICOLON do
  95. consume(SEMICOLON);
  96. end;
  97. consume(_END);
  98. statements_til_end:=gensinglenode(blockn,first);
  99. end;
  100. function case_statement : ptree;
  101. var
  102. { contains the label number of currently parsed case block }
  103. aktcaselabel : plabel;
  104. wurzel : pcaserecord;
  105. { the typ of the case expression }
  106. casedef : pdef;
  107. procedure newcaselabel(l,h : longint);
  108. var
  109. hcaselabel : pcaserecord;
  110. procedure insertlabel(var p : pcaserecord);
  111. begin
  112. if p=nil then p:=hcaselabel
  113. else
  114. if (p^._low>hcaselabel^._low) and
  115. (p^._low>hcaselabel^._high) then
  116. insertlabel(p^.less)
  117. else if (p^._high<hcaselabel^._low) and
  118. (p^._high<hcaselabel^._high) then
  119. insertlabel(p^.greater)
  120. else Message(parser_e_double_caselabel);
  121. end;
  122. begin
  123. new(hcaselabel);
  124. hcaselabel^.less:=nil;
  125. hcaselabel^.greater:=nil;
  126. hcaselabel^.statement:=aktcaselabel;
  127. getlabel(hcaselabel^._at);
  128. hcaselabel^._low:=l;
  129. hcaselabel^._high:=h;
  130. insertlabel(wurzel);
  131. end;
  132. var
  133. code,caseexpr,p,instruc,elseblock : ptree;
  134. hl1,hl2 : longint;
  135. ranges : boolean;
  136. begin
  137. consume(_CASE);
  138. caseexpr:=expr;
  139. { determines result type }
  140. cleartempgen;
  141. do_firstpass(caseexpr);
  142. casedef:=caseexpr^.resulttype;
  143. if not(is_ordinal(casedef)) then
  144. Message(parser_e_ordinal_expected);
  145. consume(_OF);
  146. wurzel:=nil;
  147. ranges:=false;
  148. instruc:=nil;
  149. repeat
  150. getlabel(aktcaselabel);
  151. {aktcaselabel^.is_used:=true; }
  152. { an instruction has may be more case labels }
  153. repeat
  154. p:=expr;
  155. cleartempgen;
  156. do_firstpass(p);
  157. if (p^.treetype=rangen) then
  158. begin
  159. { type checking for case statements }
  160. if not is_subequal(casedef, p^.left^.resulttype) then
  161. Message(parser_e_case_mismatch);
  162. { type checking for case statements }
  163. if not is_subequal(casedef, p^.right^.resulttype) then
  164. Message(parser_e_case_mismatch);
  165. hl1:=get_ordinal_value(p^.left);
  166. hl2:=get_ordinal_value(p^.right);
  167. testrange(casedef,hl1);
  168. testrange(casedef,hl2);
  169. newcaselabel(hl1,hl2);
  170. ranges:=true;
  171. end
  172. else
  173. begin
  174. { type checking for case statements }
  175. if not is_subequal(casedef, p^.resulttype) then
  176. Message(parser_e_case_mismatch);
  177. hl1:=get_ordinal_value(p);
  178. testrange(casedef,hl1);
  179. newcaselabel(hl1,hl1);
  180. end;
  181. disposetree(p);
  182. if token=COMMA then consume(COMMA)
  183. else break;
  184. until false;
  185. consume(COLON);
  186. { handles instruction block }
  187. p:=gensinglenode(labeln,statement);
  188. p^.labelnr:=aktcaselabel;
  189. { concats instruction }
  190. instruc:=gennode(anwein,instruc,p);
  191. if not((token=_ELSE) or (token=_OTHERWISE) or (token=_END)) then
  192. consume(SEMICOLON);
  193. until (token=_ELSE) or (token=_OTHERWISE) or (token=_END);
  194. if (token=_ELSE) or (token=_OTHERWISE) then
  195. begin
  196. if token=_ELSE then consume(_ELSE)
  197. else consume(_OTHERWISE);
  198. elseblock:=statements_til_end;
  199. end
  200. else
  201. begin
  202. elseblock:=nil;
  203. consume(_END);
  204. end;
  205. code:=gencasenode(caseexpr,instruc,wurzel);
  206. code^.elseblock:=elseblock;
  207. case_statement:=code;
  208. end;
  209. function repeat_statement : ptree;
  210. var
  211. first,last,p_e : ptree;
  212. begin
  213. consume(_REPEAT);
  214. first:=nil;
  215. while token<>_UNTIL do
  216. begin
  217. if first=nil then
  218. begin
  219. last:=gennode(anwein,nil,statement);
  220. first:=last;
  221. end
  222. else
  223. begin
  224. last^.left:=gennode(anwein,nil,statement);
  225. last:=last^.left;
  226. end;
  227. if token<>SEMICOLON then
  228. break;
  229. consume(SEMICOLON);
  230. while token=SEMICOLON do
  231. consume(SEMICOLON);
  232. end;
  233. consume(_UNTIL);
  234. first:=gensinglenode(blockn,first);
  235. p_e:=expr;
  236. repeat_statement:=genloopnode(repeatn,p_e,first,nil,false);
  237. end;
  238. function while_statement : ptree;
  239. var
  240. p_e,p_a : ptree;
  241. begin
  242. consume(_WHILE);
  243. p_e:=expr;
  244. consume(_DO);
  245. p_a:=statement;
  246. while_statement:=genloopnode(whilen,p_e,p_a,nil,false);
  247. end;
  248. function for_statement : ptree;
  249. var
  250. p_e,tovalue,p_a : ptree;
  251. backward : boolean;
  252. begin
  253. { parse loop header }
  254. consume(_FOR);
  255. p_e:=expr;
  256. if token=_DOWNTO then
  257. begin
  258. consume(_DOWNTO);
  259. backward:=true;
  260. end
  261. else
  262. begin
  263. consume(_TO);
  264. backward:=false;
  265. end;
  266. tovalue:=expr;
  267. consume(_DO);
  268. { ... now the instruction }
  269. p_a:=statement;
  270. for_statement:=genloopnode(forn,p_e,tovalue,p_a,backward);
  271. end;
  272. function _with_statement : ptree;
  273. var
  274. right,hp,p : ptree;
  275. i,levelcount : longint;
  276. withsymtable,symtab : psymtable;
  277. obj : pobjectdef;
  278. begin
  279. Must_be_valid:=false;
  280. p:=expr;
  281. do_firstpass(p);
  282. right:=nil;
  283. case p^.resulttype^.deftype of
  284. objectdef : begin
  285. obj:=pobjectdef(p^.resulttype);
  286. levelcount:=0;
  287. while assigned(obj) do
  288. begin
  289. symtab:=obj^.publicsyms;
  290. withsymtable:=new(psymtable,init(symtable.withsymtable));
  291. withsymtable^.wurzel:=symtab^.wurzel;
  292. withsymtable^.next:=symtablestack;
  293. symtablestack:=withsymtable;
  294. obj:=obj^.childof;
  295. inc(levelcount);
  296. end;
  297. end;
  298. recorddef : begin
  299. symtab:=precdef(p^.resulttype)^.symtable;
  300. levelcount:=1;
  301. withsymtable:=new(psymtable,init(symtable.withsymtable));
  302. withsymtable^.wurzel:=symtab^.wurzel;
  303. withsymtable^.next:=symtablestack;
  304. symtablestack:=withsymtable;
  305. end;
  306. else
  307. begin
  308. Message(parser_e_false_with_expr);
  309. { try to recover from error }
  310. if token=COMMA then
  311. begin
  312. consume(COMMA);
  313. {$ifdef tp}
  314. hp:=_with_statement;
  315. {$else}
  316. hp:=_with_statement();
  317. {$endif}
  318. end
  319. else
  320. begin
  321. consume(_DO);
  322. { ignore all }
  323. if token<>SEMICOLON then
  324. statement;
  325. end;
  326. _with_statement:=nil;
  327. exit;
  328. end;
  329. end;
  330. if token=COMMA then
  331. begin
  332. consume(COMMA);
  333. {$ifdef tp}
  334. right:=_with_statement;
  335. {$else}
  336. right:=_with_statement();
  337. {$endif}
  338. end
  339. else
  340. begin
  341. consume(_DO);
  342. if token<>SEMICOLON then
  343. right:=statement
  344. else
  345. right:=nil;
  346. end;
  347. for i:=1 to levelcount do
  348. symtablestack:=symtablestack^.next;
  349. _with_statement:=genwithnode(withsymtable,p,right,levelcount);
  350. end;
  351. function with_statement : ptree;
  352. begin
  353. consume(_WITH);
  354. with_statement:=_with_statement;
  355. end;
  356. function raise_statement : ptree;
  357. var
  358. p1,p2 : ptree;
  359. begin
  360. p1:=nil;
  361. p2:=nil;
  362. consume(_RAISE);
  363. if token<>SEMICOLON then
  364. begin
  365. p1:=expr;
  366. if (token=ID) and (pattern='AT') then
  367. begin
  368. consume(ID);
  369. p2:=expr;
  370. end;
  371. end
  372. else
  373. begin
  374. if not(in_except_block) then
  375. Message(parser_e_no_reraise_possible);
  376. end;
  377. raise_statement:=gennode(raisen,p1,p2);
  378. end;
  379. function try_statement : ptree;
  380. var
  381. p_try_block,p_finally_block,first,last,
  382. p_default,e1,e2,p_specific : ptree;
  383. old_in_except_block : boolean;
  384. begin
  385. p_default:=nil;
  386. p_specific:=nil;
  387. { read statements to try }
  388. consume(_TRY);
  389. first:=nil;
  390. while (token<>_FINALLY) and (token<>_EXCEPT) do
  391. begin
  392. if first=nil then
  393. begin
  394. last:=gennode(anwein,nil,statement);
  395. first:=last;
  396. end
  397. else
  398. begin
  399. last^.left:=gennode(anwein,nil,statement);
  400. last:=last^.left;
  401. end;
  402. if token<>SEMICOLON then
  403. break;
  404. consume(SEMICOLON);
  405. emptystats;
  406. end;
  407. p_try_block:=gensinglenode(blockn,first);
  408. if token=_FINALLY then
  409. begin
  410. consume(_FINALLY);
  411. p_finally_block:=statements_til_end;
  412. try_statement:=gennode(tryfinallyn,p_try_block,p_finally_block);
  413. end
  414. else
  415. begin
  416. consume(_EXCEPT);
  417. old_in_except_block:=in_except_block;
  418. in_except_block:=true;
  419. if token=_ON then
  420. { catch specific exceptions }
  421. begin
  422. repeat
  423. consume(_ON);
  424. e1:=expr;
  425. if token=COLON then
  426. begin
  427. consume(COLON);
  428. e2:=expr;
  429. { !!!!! }
  430. end
  431. else
  432. begin
  433. { !!!!! }
  434. end;
  435. consume(_DO);
  436. statement;
  437. if token<>SEMICOLON then
  438. break;
  439. emptystats;
  440. until false;
  441. if token=_ELSE then
  442. { catch the other exceptions }
  443. begin
  444. consume(_ELSE);
  445. p_default:=statements_til_end;
  446. end;
  447. end
  448. else
  449. { catch all exceptions }
  450. begin
  451. p_default:=statements_til_end;
  452. end;
  453. in_except_block:=old_in_except_block;
  454. try_statement:=genloopnode(tryexceptn,p_try_block,p_specific,p_default,false);
  455. end;
  456. end;
  457. function exit_statement : ptree;
  458. var
  459. p : ptree;
  460. begin
  461. consume(_EXIT);
  462. if token=LKLAMMER then
  463. begin
  464. consume(LKLAMMER);
  465. p:=expr;
  466. consume(RKLAMMER);
  467. if procinfo.retdef=pdef(voiddef) then
  468. Message(parser_e_void_function);
  469. end
  470. else
  471. p:=nil;
  472. exit_statement:=gensinglenode(exitn,p);
  473. end;
  474. {$ifdef i386}
  475. function _asm_statement : ptree;
  476. begin
  477. case aktasmmode of
  478. I386_ATT : _asm_statement:=ratti386.assemble;
  479. I386_INTEL : _asm_statement:=rai386.assemble;
  480. I386_DIRECT : _asm_statement:=radi386.assemble;
  481. else internalerror(30004);
  482. end;
  483. { Erst am Ende _ASM konsumieren, da der Scanner sonst die }
  484. { erste Assemblerstatement zu lesen versucht! }
  485. consume(_ASM);
  486. { (END is read) }
  487. if token=LECKKLAMMER then
  488. begin
  489. { it's possible to specify the modified registers }
  490. consume(LECKKLAMMER);
  491. if token<>RECKKLAMMER then
  492. repeat
  493. pattern:=upper(pattern);
  494. if pattern='EAX' then
  495. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  496. else if pattern='EBX' then
  497. usedinproc:=usedinproc or ($80 shr byte(R_EBX))
  498. else if pattern='ECX' then
  499. usedinproc:=usedinproc or ($80 shr byte(R_ECX))
  500. else if pattern='EDX' then
  501. usedinproc:=usedinproc or ($80 shr byte(R_EDX))
  502. else if pattern='ESI' then
  503. usedinproc:=usedinproc or ($80 shr byte(R_ESI))
  504. else if pattern='EDI' then
  505. usedinproc:=usedinproc or ($80 shr byte(R_EDI))
  506. else consume(RECKKLAMMER);
  507. consume(CSTRING);
  508. if token=COMMA then consume(COMMA)
  509. else break;
  510. until false;
  511. consume(RECKKLAMMER);
  512. end
  513. else usedinproc:=$ff;
  514. end;
  515. {$endif}
  516. {$ifdef m68k}
  517. function _asm_statement : ptree;
  518. begin
  519. _asm_statement:= ra68k.assemble;
  520. { Erst am Ende _ASM konsumieren, da der Scanner sonst die }
  521. { erste Assemblerstatement zu lesen versucht! }
  522. consume(_ASM);
  523. { (END is read) }
  524. if token=LECKKLAMMER then
  525. begin
  526. { it's possible to specify the modified registers }
  527. { we only check the registers which are not reserved }
  528. { and which can be used. This is done for future }
  529. { optimizations. }
  530. consume(LECKKLAMMER);
  531. if token<>RECKKLAMMER then
  532. repeat
  533. pattern:=upper(pattern);
  534. if pattern='D0' then
  535. usedinproc:=usedinproc or ($800 shr word(R_D0))
  536. else if pattern='D1' then
  537. usedinproc:=usedinproc or ($800 shr word(R_D1))
  538. else if pattern='D6' then
  539. usedinproc:=usedinproc or ($800 shr word(R_D6))
  540. else if pattern='A0' then
  541. usedinproc:=usedinproc or ($800 shr word(R_A0))
  542. else if pattern='A1' then
  543. usedinproc:=usedinproc or ($800 shr word(R_A1))
  544. else consume(RECKKLAMMER);
  545. consume(CSTRING);
  546. if token=COMMA then consume(COMMA)
  547. else break;
  548. until false;
  549. consume(RECKKLAMMER);
  550. end
  551. else usedinproc:=$ffff;
  552. end;
  553. {$endif}
  554. function new_dispose_statement : ptree;
  555. var
  556. p,p2 : ptree;
  557. ht : ttoken;
  558. again : boolean; { dummy for do_proc_call }
  559. destrukname : stringid;
  560. sym : psym;
  561. classh : pobjectdef;
  562. pd,pd2 : pdef;
  563. store_valid : boolean;
  564. tt : ttreetyp;
  565. begin
  566. ht:=token;
  567. if token=_NEW then consume(_NEW)
  568. else consume(_DISPOSE);
  569. if ht=_NEW then
  570. tt:=hnewn
  571. else
  572. tt:=hdisposen;
  573. consume(LKLAMMER);
  574. p:=expr;
  575. { calc return type }
  576. cleartempgen;
  577. Store_valid := Must_be_valid;
  578. Must_be_valid := False;
  579. do_firstpass(p);
  580. Must_be_valid := Store_valid;
  581. {var o:Pobject;
  582. begin
  583. new(o,init); (*Also a valid new statement*)
  584. end;}
  585. if token=COMMA then
  586. begin
  587. { extended syntax of new and dispose }
  588. { function styled new is handled in factor }
  589. consume(COMMA);
  590. { destructors have no parameters }
  591. destrukname:=pattern;
  592. consume(ID);
  593. pd:=p^.resulttype;
  594. pd2:=pd;
  595. if (p^.resulttype = nil) or (pd^.deftype<>pointerdef) then
  596. begin
  597. Message(parser_e_pointer_type_expected);
  598. p:=factor(false);
  599. consume(RKLAMMER);
  600. new_dispose_statement:=genzeronode(errorn);
  601. exit;
  602. end;
  603. { first parameter must be an object or class }
  604. if ppointerdef(pd)^.definition^.deftype<>objectdef then
  605. begin
  606. Message(parser_e_pointer_to_class_expected);
  607. new_dispose_statement:=factor(false);
  608. consume_all_until(RKLAMMER);
  609. consume(RKLAMMER);
  610. exit;
  611. end;
  612. { check, if the first parameter is a pointer to a _class_ }
  613. classh:=pobjectdef(ppointerdef(pd)^.definition);
  614. if (classh^.options and oois_class)<>0 then
  615. begin
  616. Message(parser_e_no_new_or_dispose_for_classes);
  617. new_dispose_statement:=factor(false);
  618. { while token<>RKLAMMER do
  619. consume(token); }
  620. consume_all_until(RKLAMMER);
  621. consume(RKLAMMER);
  622. exit;
  623. end;
  624. { search cons-/destructor, also in parent classes }
  625. sym:=nil;
  626. while assigned(classh) do
  627. begin
  628. sym:=classh^.publicsyms^.search(pattern);
  629. srsymtable:=classh^.publicsyms;
  630. if assigned(sym) then
  631. break;
  632. classh:=classh^.childof;
  633. end;
  634. { the second parameter of new/dispose must be a call }
  635. { to a cons-/destructor }
  636. if (sym^.typ<>procsym) then
  637. begin
  638. Message(parser_e_expr_have_to_be_destructor_call);
  639. new_dispose_statement:=genzeronode(errorn);
  640. end
  641. else
  642. begin
  643. p2:=gensinglenode(tt,p);
  644. if ht=_NEW then
  645. begin
  646. { Constructors can take parameters.}
  647. p2^.resulttype:=ppointerdef(pd)^.definition;
  648. do_member_read(sym,p2,pd,again);
  649. end
  650. else
  651. { destructors can't.}
  652. p2:=genmethodcallnode(pprocsym(sym),srsymtable,p2);
  653. { we need the real called method }
  654. cleartempgen;
  655. do_firstpass(p2);
  656. if (ht=_NEW) and ((p2^.procdefinition^.options and poconstructor)=0) then
  657. Message(parser_e_expr_have_to_be_constructor_call);
  658. if (ht=_DISPOSE) and ((p2^.procdefinition^.options and podestructor)=0) then
  659. Message(parser_e_expr_have_to_be_destructor_call);
  660. if ht=_NEW then
  661. begin
  662. p2:=gennode(assignn,getcopy(p),gensinglenode(newn,p2));
  663. p2^.right^.resulttype:=pd2;
  664. end;
  665. new_dispose_statement:=p2;
  666. end;
  667. end
  668. else
  669. begin
  670. if (p^.resulttype=nil) or (p^.resulttype^.deftype<>pointerdef) then
  671. Begin
  672. Message(parser_e_pointer_type_expected);
  673. new_dispose_statement:=genzeronode(errorn);
  674. end
  675. else
  676. begin
  677. if (ppointerdef(p^.resulttype)^.definition^.deftype=objectdef) then
  678. Message(parser_w_use_extended_syntax_for_objects);
  679. case ht of
  680. _NEW : new_dispose_statement:=gensinglenode(simplenewn,p);
  681. _DISPOSE : new_dispose_statement:=gensinglenode(simpledisposen,p);
  682. end;
  683. end;
  684. end;
  685. consume(RKLAMMER);
  686. end;
  687. function statement_block : ptree;
  688. var
  689. first,last : ptree;
  690. begin
  691. first:=nil;
  692. consume(_BEGIN);
  693. while token<>_END do
  694. begin
  695. if first=nil then
  696. begin
  697. last:=gennode(anwein,nil,statement);
  698. first:=last;
  699. end
  700. else
  701. begin
  702. last^.left:=gennode(anwein,nil,statement);
  703. last:=last^.left;
  704. end;
  705. if token=_END then
  706. break
  707. else
  708. begin
  709. { if no semicolon, then error and go on }
  710. if token<>SEMICOLON then
  711. begin
  712. consume(SEMICOLON);
  713. { while token<>SEMICOLON do
  714. consume(token); }
  715. consume_all_until(SEMICOLON);
  716. end;
  717. consume(SEMICOLON);
  718. end;
  719. emptystats;
  720. end;
  721. consume(_END);
  722. first:=gensinglenode(blockn,first);
  723. statement_block:=first;
  724. end;
  725. function statement : ptree;
  726. var
  727. p : ptree;
  728. code : ptree;
  729. labelnr : plabel;
  730. label
  731. ready;
  732. begin
  733. case token of
  734. _GOTO : begin
  735. if not(cs_support_goto in aktswitches)then
  736. Message(sym_e_goto_and_label_not_supported);
  737. consume(_GOTO);
  738. if (token<>INTCONST) and (token<>ID) then
  739. begin
  740. Message(sym_e_label_not_found);
  741. code:=genzeronode(errorn);
  742. end
  743. else
  744. begin
  745. getsym(pattern,true);
  746. consume(token);
  747. if srsym^.typ<>labelsym then
  748. begin
  749. Message(sym_e_id_is_no_label_id);
  750. code:=genzeronode(errorn);
  751. end
  752. else
  753. code:=genlabelnode(goton,
  754. plabelsym(srsym)^.number);
  755. end;
  756. end;
  757. _BEGIN : code:=statement_block;
  758. _IF : code:=if_statement;
  759. _CASE : code:=case_statement;
  760. _REPEAT : code:=repeat_statement;
  761. _WHILE : code:=while_statement;
  762. _FOR : code:=for_statement;
  763. _NEW,_DISPOSE : code:=new_dispose_statement;
  764. _WITH : code:=with_statement;
  765. _TRY : code:=try_statement;
  766. _RAISE : code:=raise_statement;
  767. { semicolons,else until and end are ignored }
  768. SEMICOLON,
  769. _ELSE,
  770. _UNTIL,
  771. _END : code:=genzeronode(niln);
  772. _CONTINUE : begin
  773. consume(_CONTINUE);
  774. code:=genzeronode(continuen);
  775. end;
  776. _FAIL : begin
  777. { internalerror(100); }
  778. if (aktprocsym^.definition^.options and poconstructor)=0 then
  779. Message(parser_e_fail_only_in_constructor);
  780. consume(_FAIL);
  781. code:=genzeronode(failn);
  782. end;
  783. _BREAK:
  784. begin
  785. consume(_BREAK);
  786. code:=genzeronode(breakn);
  787. end;
  788. _EXIT : code:=exit_statement;
  789. _ASM : code:=_asm_statement;
  790. else
  791. begin
  792. if (token=INTCONST) or
  793. ((token=ID) and
  794. not((cs_delphi2_compatible in aktswitches) and
  795. (pattern='RESULT'))) then
  796. begin
  797. getsym(pattern,false);
  798. if assigned(srsym) and (srsym^.typ=labelsym) then
  799. begin
  800. consume(token);
  801. consume(COLON);
  802. if plabelsym(srsym)^.defined then
  803. Message(sym_e_label_already_defined);
  804. plabelsym(srsym)^.defined:=true;
  805. { statement modifies srsym }
  806. labelnr:=plabelsym(srsym)^.number;
  807. { the pointer to the following instruction }
  808. { isn't a very clean way }
  809. {$ifdef tp}
  810. code:=gensinglenode(labeln,statement);
  811. {$else}
  812. code:=gensinglenode(labeln,statement());
  813. {$endif}
  814. code^.labelnr:=labelnr;
  815. { sorry, but there is a jump the easiest way }
  816. goto ready;
  817. end;
  818. end;
  819. p:=expr;
  820. if (p^.treetype<>calln) and
  821. (p^.treetype<>assignn) and
  822. (p^.treetype<>inlinen) then
  823. Message(cg_e_illegal_expression);
  824. code:=p;
  825. end;
  826. end;
  827. ready:
  828. statement:=code;
  829. end;
  830. function block(islibrary : boolean) : ptree;
  831. {$ifdef TEST_FUNCRET }
  832. var
  833. funcretsym : pfuncretsym;
  834. {$endif TEST_FUNCRET }
  835. begin
  836. {$ifdef TEST_FUNCRET }
  837. if procinfo.retdef<>pdef(voiddef) then
  838. begin
  839. { if the current is a function aktprocsym is non nil }
  840. { and there is a local symtable set }
  841. funcretsym:=new(pfuncretsym,init(aktprocsym^.name),@procinfo);
  842. { insert in local symtable }
  843. symtablestack^.insert(funcretsym);
  844. end;
  845. {$endif TEST_FUNCRET }
  846. read_declarations(islibrary);
  847. { temporary space is set, while the BEGIN of the procedure }
  848. if (symtablestack^.symtabletype=localsymtable) then
  849. procinfo.firsttemp := -symtablestack^.datasize
  850. else procinfo.firsttemp := 0;
  851. { space for the return value }
  852. { !!!!! this means that we can not set the return value
  853. in a subfunction !!!!! }
  854. { because we don't know yet where the address is }
  855. if procinfo.retdef<>pdef(voiddef) then
  856. begin
  857. if ret_in_acc(procinfo.retdef) or (procinfo.retdef^.deftype=floatdef) then
  858. { if (procinfo.retdef^.deftype=orddef) or
  859. (procinfo.retdef^.deftype=pointerdef) or
  860. (procinfo.retdef^.deftype=enumdef) or
  861. (procinfo.retdef^.deftype=procvardef) or
  862. (procinfo.retdef^.deftype=floatdef) or
  863. (
  864. (procinfo.retdef^.deftype=setdef) and
  865. (psetdef(procinfo.retdef)^.settype=smallset)
  866. ) then }
  867. begin
  868. {$ifdef TEST_FUNCRET }
  869. { the space has been set in the local symtable }
  870. procinfo.retoffset:=-funcretsym^.address;
  871. strdispose(funcretsym^._name);
  872. { lowercase name unreachable }
  873. { as it is handled differently }
  874. funcretsym^._name:=strpnew('func_result');
  875. {$else TEST_FUNCRET }
  876. procinfo.retoffset:=procinfo.firsttemp-procinfo.retdef^.size;
  877. procinfo.firsttemp:=procinfo.retoffset;
  878. {$endif TEST_FUNCRET }
  879. if (procinfo.flags and pooperator)<>0 then
  880. {opsym^.address:=procinfo.call_offset; is wrong PM }
  881. opsym^.address:=procinfo.retoffset;
  882. { eax is modified by a function }
  883. {$ifdef i386}
  884. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  885. {$endif}
  886. {$ifdef m68k}
  887. usedinproc:=usedinproc or ($800 shr word(R_D0))
  888. {$endif}
  889. end;
  890. end;
  891. {Unit initialization?.}
  892. if (lexlevel=1) then
  893. if (token=_END) then
  894. begin
  895. consume(_END);
  896. block:=nil;
  897. end
  898. else
  899. begin
  900. current_module^.flags:=current_module^.flags or
  901. uf_init;
  902. block:=statement_block;
  903. end
  904. else
  905. block:=statement_block;
  906. end;
  907. function assembler_block : ptree;
  908. begin
  909. read_declarations(false);
  910. { temporary space is set, while the BEGIN of the procedure }
  911. if symtablestack^.symtabletype=localsymtable then
  912. procinfo.firsttemp := -symtablestack^.datasize
  913. else procinfo.firsttemp := 0;
  914. { assembler code does not allocate }
  915. { space for the return value }
  916. if procinfo.retdef<>pdef(voiddef) then
  917. begin
  918. if ret_in_acc(procinfo.retdef) then
  919. begin
  920. { in assembler code the result should be directly in %eax
  921. procinfo.retoffset:=procinfo.firsttemp-procinfo.retdef^.size;
  922. procinfo.firsttemp:=procinfo.retoffset; }
  923. {$ifdef i386}
  924. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  925. {$endif}
  926. {$ifdef m68k}
  927. usedinproc:=usedinproc or ($800 shr word(R_D0))
  928. {$endif}
  929. end
  930. else
  931. { should we allow assembler functions of big elements ? }
  932. Message(parser_e_asm_incomp_with_function_return);
  933. end;
  934. { set the framepointer to esp for assembler functions }
  935. { but only if the are no local variables }
  936. if ((aktprocsym^.definition^.options and poassembler)<>0) and
  937. (aktprocsym^.definition^.localst^.datasize=0) then
  938. begin
  939. {$ifdef i386}
  940. procinfo.framepointer:=R_ESP;
  941. {$endif}
  942. {$ifdef m68k}
  943. procinfo.framepointer:=R_SP;
  944. {$endif}
  945. { set the right value for parameters }
  946. dec(aktprocsym^.definition^.parast^.call_offset,4);
  947. dec(procinfo.call_offset,4);
  948. end;
  949. assembler_block:=_asm_statement;
  950. end;
  951. end.
  952. {
  953. $Log$
  954. Revision 1.3 1998-03-28 23:09:56 florian
  955. * secondin bugfix (m68k and i386)
  956. * overflow checking bugfix (m68k and i386) -- pretty useless in
  957. secondadd, since everything is done using 32-bit
  958. * loading pointer to routines hopefully fixed (m68k)
  959. * flags problem with calls to RTL internal routines fixed (still strcmp
  960. to fix) (m68k)
  961. * #ELSE was still incorrect (didn't take care of the previous level)
  962. * problem with filenames in the command line solved
  963. * problem with mangledname solved
  964. * linking name problem solved (was case insensitive)
  965. * double id problem and potential crash solved
  966. * stop after first error
  967. * and=>test problem removed
  968. * correct read for all float types
  969. * 2 sigsegv fixes and a cosmetic fix for Internal Error
  970. * push/pop is now correct optimized (=> mov (%esp),reg)
  971. Revision 1.2 1998/03/26 11:18:31 florian
  972. - switch -Sa removed
  973. - support of a:=b:=0 removed
  974. Revision 1.1.1.1 1998/03/25 11:18:15 root
  975. * Restored version
  976. Revision 1.21 1998/03/10 16:27:42 pierre
  977. * better line info in stabs debug
  978. * symtabletype and lexlevel separated into two fields of tsymtable
  979. + ifdef MAKELIB for direct library output, not complete
  980. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  981. working
  982. + ifdef TESTFUNCRET for setting func result in underfunction, not
  983. working
  984. Revision 1.20 1998/03/10 04:18:26 carl
  985. * wrong units were being used with m68k target
  986. Revision 1.19 1998/03/10 01:17:25 peter
  987. * all files have the same header
  988. * messages are fully implemented, EXTDEBUG uses Comment()
  989. + AG... files for the Assembler generation
  990. Revision 1.18 1998/03/06 00:52:46 peter
  991. * replaced all old messages from errore.msg, only ExtDebug and some
  992. Comment() calls are left
  993. * fixed options.pas
  994. Revision 1.17 1998/03/02 01:49:07 peter
  995. * renamed target_DOS to target_GO32V1
  996. + new verbose system, merged old errors and verbose units into one new
  997. verbose.pas, so errors.pas is obsolete
  998. Revision 1.16 1998/02/22 23:03:30 peter
  999. * renamed msource->mainsource and name->unitname
  1000. * optimized filename handling, filename is not seperate anymore with
  1001. path+name+ext, this saves stackspace and a lot of fsplit()'s
  1002. * recompiling of some units in libraries fixed
  1003. * shared libraries are working again
  1004. + $LINKLIB <lib> to support automatic linking to libraries
  1005. + libraries are saved/read from the ppufile, also allows more libraries
  1006. per ppufile
  1007. Revision 1.15 1998/02/21 03:33:54 carl
  1008. + mit assembler syntax support
  1009. Revision 1.14 1998/02/13 10:35:29 daniel
  1010. * Made Motorola version compilable.
  1011. * Fixed optimizer
  1012. Revision 1.13 1998/02/12 11:50:30 daniel
  1013. Yes! Finally! After three retries, my patch!
  1014. Changes:
  1015. Complete rewrite of psub.pas.
  1016. Added support for DLL's.
  1017. Compiler requires less memory.
  1018. Platform units for each platform.
  1019. Revision 1.12 1998/02/11 21:56:39 florian
  1020. * bugfixes: bug0093, bug0053, bug0088, bug0087, bug0089
  1021. Revision 1.11 1998/02/07 09:39:26 florian
  1022. * correct handling of in_main
  1023. + $D,$T,$X,$V like tp
  1024. Revision 1.10 1998/01/31 00:42:26 carl
  1025. +* Final bugfix #60 (working!) Type checking in case statements
  1026. Revision 1.7 1998/01/21 02:18:28 carl
  1027. * bugfix 79 (assembler_block now chooses the correct framepointer and
  1028. offset).
  1029. Revision 1.6 1998/01/16 22:34:43 michael
  1030. * Changed 'conversation' to 'conversion'. Waayyy too much chatting going on
  1031. in this compiler :)
  1032. Revision 1.5 1998/01/12 14:51:18 carl
  1033. - temporariliy removed case type checking until i know where the bug
  1034. comes from!
  1035. Revision 1.4 1998/01/11 19:23:49 carl
  1036. * bug fix number 60 (case statements type checking)
  1037. Revision 1.3 1998/01/11 10:54:25 florian
  1038. + generic library support
  1039. Revision 1.2 1998/01/09 09:10:02 michael
  1040. + Initial implementation, second try
  1041. }