pstatmnt.pas 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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,true);
  798. if 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 (aktexprlevel<9) and (p^.treetype<>calln)
  821. and (p^.treetype<>assignn) and (p^.treetype<>inlinen) then
  822. Message(cg_e_illegal_expression);
  823. code:=p;
  824. end;
  825. end;
  826. ready:
  827. statement:=code;
  828. end;
  829. function block(islibrary : boolean) : ptree;
  830. {$ifdef TEST_FUNCRET }
  831. var
  832. funcretsym : pfuncretsym;
  833. {$endif TEST_FUNCRET }
  834. begin
  835. {$ifdef TEST_FUNCRET }
  836. if procinfo.retdef<>pdef(voiddef) then
  837. begin
  838. { if the current is a function aktprocsym is non nil }
  839. { and there is a local symtable set }
  840. funcretsym:=new(pfuncretsym,init(aktprocsym^.name),@procinfo);
  841. { insert in local symtable }
  842. symtablestack^.insert(funcretsym);
  843. end;
  844. {$endif TEST_FUNCRET }
  845. read_declarations(islibrary);
  846. { temporary space is set, while the BEGIN of the procedure }
  847. if (symtablestack^.symtabletype=localsymtable) then
  848. procinfo.firsttemp := -symtablestack^.datasize
  849. else procinfo.firsttemp := 0;
  850. { space for the return value }
  851. { !!!!! this means that we can not set the return value
  852. in a subfunction !!!!! }
  853. { because we don't know yet where the address is }
  854. if procinfo.retdef<>pdef(voiddef) then
  855. begin
  856. if ret_in_acc(procinfo.retdef) or (procinfo.retdef^.deftype=floatdef) then
  857. { if (procinfo.retdef^.deftype=orddef) or
  858. (procinfo.retdef^.deftype=pointerdef) or
  859. (procinfo.retdef^.deftype=enumdef) or
  860. (procinfo.retdef^.deftype=procvardef) or
  861. (procinfo.retdef^.deftype=floatdef) or
  862. (
  863. (procinfo.retdef^.deftype=setdef) and
  864. (psetdef(procinfo.retdef)^.settype=smallset)
  865. ) then }
  866. begin
  867. {$ifdef TEST_FUNCRET }
  868. { the space has been set in the local symtable }
  869. procinfo.retoffset:=-funcretsym^.address;
  870. strdispose(funcretsym^._name);
  871. { lowercase name unreachable }
  872. { as it is handled differently }
  873. funcretsym^._name:=strpnew('func_result');
  874. {$else TEST_FUNCRET }
  875. procinfo.retoffset:=procinfo.firsttemp-procinfo.retdef^.size;
  876. procinfo.firsttemp:=procinfo.retoffset;
  877. {$endif TEST_FUNCRET }
  878. if (procinfo.flags and pooperator)<>0 then
  879. {opsym^.address:=procinfo.call_offset; is wrong PM }
  880. opsym^.address:=procinfo.retoffset;
  881. { eax is modified by a function }
  882. {$ifdef i386}
  883. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  884. {$endif}
  885. {$ifdef m68k}
  886. usedinproc:=usedinproc or ($800 shr word(R_D0))
  887. {$endif}
  888. end;
  889. end;
  890. {Unit initialization?.}
  891. if (lexlevel=1) then
  892. if (token=_END) then
  893. begin
  894. consume(_END);
  895. block:=nil;
  896. end
  897. else
  898. begin
  899. current_module^.flags:=current_module^.flags or
  900. uf_init;
  901. block:=statement_block;
  902. end
  903. else
  904. block:=statement_block;
  905. end;
  906. function assembler_block : ptree;
  907. begin
  908. read_declarations(false);
  909. { temporary space is set, while the BEGIN of the procedure }
  910. if symtablestack^.symtabletype=localsymtable then
  911. procinfo.firsttemp := -symtablestack^.datasize
  912. else procinfo.firsttemp := 0;
  913. { assembler code does not allocate }
  914. { space for the return value }
  915. if procinfo.retdef<>pdef(voiddef) then
  916. begin
  917. if ret_in_acc(procinfo.retdef) then
  918. begin
  919. { in assembler code the result should be directly in %eax
  920. procinfo.retoffset:=procinfo.firsttemp-procinfo.retdef^.size;
  921. procinfo.firsttemp:=procinfo.retoffset; }
  922. {$ifdef i386}
  923. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  924. {$endif}
  925. {$ifdef m68k}
  926. usedinproc:=usedinproc or ($800 shr word(R_D0))
  927. {$endif}
  928. end
  929. else
  930. { should we allow assembler functions of big elements ? }
  931. Message(parser_e_asm_incomp_with_function_return);
  932. end;
  933. { set the framepointer to esp for assembler functions }
  934. { but only if the are no local variables }
  935. if ((aktprocsym^.definition^.options and poassembler)<>0) and
  936. (aktprocsym^.definition^.localst^.datasize=0) then
  937. begin
  938. {$ifdef i386}
  939. procinfo.framepointer:=R_ESP;
  940. {$endif}
  941. {$ifdef m68k}
  942. procinfo.framepointer:=R_SP;
  943. {$endif}
  944. { set the right value for parameters }
  945. dec(aktprocsym^.definition^.parast^.call_offset,4);
  946. dec(procinfo.call_offset,4);
  947. end;
  948. assembler_block:=_asm_statement;
  949. end;
  950. end.
  951. {
  952. $Log$
  953. Revision 1.1 1998-03-25 11:18:15 root
  954. Initial revision
  955. Revision 1.21 1998/03/10 16:27:42 pierre
  956. * better line info in stabs debug
  957. * symtabletype and lexlevel separated into two fields of tsymtable
  958. + ifdef MAKELIB for direct library output, not complete
  959. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  960. working
  961. + ifdef TESTFUNCRET for setting func result in underfunction, not
  962. working
  963. Revision 1.20 1998/03/10 04:18:26 carl
  964. * wrong units were being used with m68k target
  965. Revision 1.19 1998/03/10 01:17:25 peter
  966. * all files have the same header
  967. * messages are fully implemented, EXTDEBUG uses Comment()
  968. + AG... files for the Assembler generation
  969. Revision 1.18 1998/03/06 00:52:46 peter
  970. * replaced all old messages from errore.msg, only ExtDebug and some
  971. Comment() calls are left
  972. * fixed options.pas
  973. Revision 1.17 1998/03/02 01:49:07 peter
  974. * renamed target_DOS to target_GO32V1
  975. + new verbose system, merged old errors and verbose units into one new
  976. verbose.pas, so errors.pas is obsolete
  977. Revision 1.16 1998/02/22 23:03:30 peter
  978. * renamed msource->mainsource and name->unitname
  979. * optimized filename handling, filename is not seperate anymore with
  980. path+name+ext, this saves stackspace and a lot of fsplit()'s
  981. * recompiling of some units in libraries fixed
  982. * shared libraries are working again
  983. + $LINKLIB <lib> to support automatic linking to libraries
  984. + libraries are saved/read from the ppufile, also allows more libraries
  985. per ppufile
  986. Revision 1.15 1998/02/21 03:33:54 carl
  987. + mit assembler syntax support
  988. Revision 1.14 1998/02/13 10:35:29 daniel
  989. * Made Motorola version compilable.
  990. * Fixed optimizer
  991. Revision 1.13 1998/02/12 11:50:30 daniel
  992. Yes! Finally! After three retries, my patch!
  993. Changes:
  994. Complete rewrite of psub.pas.
  995. Added support for DLL's.
  996. Compiler requires less memory.
  997. Platform units for each platform.
  998. Revision 1.12 1998/02/11 21:56:39 florian
  999. * bugfixes: bug0093, bug0053, bug0088, bug0087, bug0089
  1000. Revision 1.11 1998/02/07 09:39:26 florian
  1001. * correct handling of in_main
  1002. + $D,$T,$X,$V like tp
  1003. Revision 1.10 1998/01/31 00:42:26 carl
  1004. +* Final bugfix #60 (working!) Type checking in case statements
  1005. Revision 1.7 1998/01/21 02:18:28 carl
  1006. * bugfix 79 (assembler_block now chooses the correct framepointer and
  1007. offset).
  1008. Revision 1.6 1998/01/16 22:34:43 michael
  1009. * Changed 'conversation' to 'conversion'. Waayyy too much chatting going on
  1010. in this compiler :)
  1011. Revision 1.5 1998/01/12 14:51:18 carl
  1012. - temporariliy removed case type checking until i know where the bug
  1013. comes from!
  1014. Revision 1.4 1998/01/11 19:23:49 carl
  1015. * bug fix number 60 (case statements type checking)
  1016. Revision 1.3 1998/01/11 10:54:25 florian
  1017. + generic library support
  1018. Revision 1.2 1998/01/09 09:10:02 michael
  1019. + Initial implementation, second try
  1020. }