pstatmnt.pas 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$i defines.inc}
  20. interface
  21. uses
  22. tokens,node;
  23. function statement_block(starttoken : ttoken) : tnode;
  24. { reads an assembler block }
  25. function assembler_block : tnode;
  26. implementation
  27. uses
  28. { common }
  29. cutils,cobjects,
  30. { global }
  31. globtype,globals,verbose,
  32. systems,cpuinfo,
  33. { aasm }
  34. cpubase,aasm,
  35. { symtable }
  36. symconst,symbase,symtype,symdef,symsym,symtable,types,
  37. { pass 1 }
  38. pass_1,htypechk,
  39. nbas,nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  40. { parser }
  41. scanner,
  42. pbase,pexpr,
  43. { codegen }
  44. tgcpu,hcodegen
  45. {$ifdef i386}
  46. {$ifndef NoRa386Int}
  47. ,ra386int
  48. {$endif NoRa386Int}
  49. {$ifndef NoRa386Att}
  50. ,ra386att
  51. {$endif NoRa386Att}
  52. {$ifndef NoRa386Dir}
  53. ,ra386dir
  54. {$endif NoRa386Dir}
  55. {$endif i386}
  56. {$ifdef m68k}
  57. {$ifndef NoRa68kMot}
  58. ,ra68kmot
  59. {$endif NoRa68kMot}
  60. {$endif m68k}
  61. { codegen }
  62. {$ifdef newcg}
  63. ,cgbase
  64. {$endif newcg}
  65. ;
  66. const
  67. statement_level : longint = 0;
  68. function statement : tnode;forward;
  69. function if_statement : tnode;
  70. var
  71. ex,if_a,else_a : tnode;
  72. begin
  73. consume(_IF);
  74. ex:=comp_expr(true);
  75. consume(_THEN);
  76. if token<>_ELSE then
  77. if_a:=statement
  78. else
  79. if_a:=nil;
  80. if try_to_consume(_ELSE) then
  81. else_a:=statement
  82. else
  83. else_a:=nil;
  84. if_statement:=genloopnode(ifn,ex,if_a,else_a,false);
  85. end;
  86. { creates a block (list) of statements, til the next END token }
  87. function statements_til_end : tnode;
  88. var
  89. first,last : tstatementnode;
  90. begin
  91. first:=nil;
  92. while token<>_END do
  93. begin
  94. if first=nil then
  95. begin
  96. last:=cstatementnode.create(nil,statement);
  97. first:=last;
  98. end
  99. else
  100. begin
  101. last.left:=cstatementnode.create(nil,statement);
  102. last:=tstatementnode(last.left);
  103. end;
  104. if not try_to_consume(_SEMICOLON) then
  105. break;
  106. emptystats;
  107. end;
  108. consume(_END);
  109. statements_til_end:=cblocknode.create(first);
  110. end;
  111. function case_statement : tnode;
  112. var
  113. { contains the label number of currently parsed case block }
  114. aktcaselabel : pasmlabel;
  115. firstlabel : boolean;
  116. root : pcaserecord;
  117. { the typ of the case expression }
  118. casedef : pdef;
  119. procedure newcaselabel(l,h : TConstExprInt;first:boolean);
  120. var
  121. hcaselabel : pcaserecord;
  122. procedure insertlabel(var p : pcaserecord);
  123. begin
  124. if p=nil then p:=hcaselabel
  125. else
  126. if (p^._low>hcaselabel^._low) and
  127. (p^._low>hcaselabel^._high) then
  128. if (hcaselabel^.statement = p^.statement) and
  129. (p^._low = hcaselabel^._high + 1) then
  130. begin
  131. p^._low := hcaselabel^._low;
  132. dispose(hcaselabel);
  133. end
  134. else
  135. insertlabel(p^.less)
  136. else
  137. if (p^._high<hcaselabel^._low) and
  138. (p^._high<hcaselabel^._high) then
  139. if (hcaselabel^.statement = p^.statement) and
  140. (p^._high+1 = hcaselabel^._low) then
  141. begin
  142. p^._high := hcaselabel^._high;
  143. dispose(hcaselabel);
  144. end
  145. else
  146. insertlabel(p^.greater)
  147. else Message(parser_e_double_caselabel);
  148. end;
  149. begin
  150. new(hcaselabel);
  151. hcaselabel^.less:=nil;
  152. hcaselabel^.greater:=nil;
  153. hcaselabel^.statement:=aktcaselabel;
  154. hcaselabel^.firstlabel:=first;
  155. getlabel(hcaselabel^._at);
  156. hcaselabel^._low:=l;
  157. hcaselabel^._high:=h;
  158. insertlabel(root);
  159. end;
  160. var
  161. code,caseexpr,p,instruc,elseblock : tnode;
  162. hl1,hl2 : TConstExprInt;
  163. casedeferror : boolean;
  164. begin
  165. consume(_CASE);
  166. caseexpr:=comp_expr(true);
  167. { determines result type }
  168. cleartempgen;
  169. do_firstpass(caseexpr);
  170. casedeferror:=false;
  171. casedef:=caseexpr.resulttype;
  172. if (not assigned(casedef)) or
  173. not(is_ordinal(casedef)) then
  174. begin
  175. CGMessage(type_e_ordinal_expr_expected);
  176. { create a correct tree }
  177. caseexpr.free;
  178. caseexpr:=genordinalconstnode(0,u32bitdef);
  179. { set error flag so no rangechecks are done }
  180. casedeferror:=true;
  181. end;
  182. consume(_OF);
  183. inc(statement_level);
  184. root:=nil;
  185. instruc:=nil;
  186. repeat
  187. getlabel(aktcaselabel);
  188. firstlabel:=true;
  189. { may be an instruction has more case labels }
  190. repeat
  191. p:=expr;
  192. cleartempgen;
  193. do_firstpass(p);
  194. hl1:=0;
  195. hl2:=0;
  196. if (p.nodetype=rangen) then
  197. begin
  198. { type checking for case statements }
  199. if is_subequal(casedef, trangenode(p).left.resulttype) and
  200. is_subequal(casedef, trangenode(p).right.resulttype) then
  201. begin
  202. hl1:=get_ordinal_value(trangenode(p).left);
  203. hl2:=get_ordinal_value(trangenode(p).right);
  204. if hl1>hl2 then
  205. CGMessage(parser_e_case_lower_less_than_upper_bound);
  206. if not casedeferror then
  207. begin
  208. testrange(casedef,hl1);
  209. testrange(casedef,hl2);
  210. end;
  211. end
  212. else
  213. CGMessage(parser_e_case_mismatch);
  214. newcaselabel(hl1,hl2,firstlabel);
  215. end
  216. else
  217. begin
  218. { type checking for case statements }
  219. if not is_subequal(casedef, p.resulttype) then
  220. CGMessage(parser_e_case_mismatch);
  221. hl1:=get_ordinal_value(p);
  222. if not casedeferror then
  223. testrange(casedef,hl1);
  224. newcaselabel(hl1,hl1,firstlabel);
  225. end;
  226. p.free;
  227. if token=_COMMA then
  228. consume(_COMMA)
  229. else
  230. break;
  231. firstlabel:=false;
  232. until false;
  233. consume(_COLON);
  234. { handles instruction block }
  235. p:=clabelnode.create(aktcaselabel,statement);
  236. { concats instruction }
  237. instruc:=cstatementnode.create(instruc,p);
  238. if not((token=_ELSE) or (token=_OTHERWISE) or (token=_END)) then
  239. consume(_SEMICOLON);
  240. until (token=_ELSE) or (token=_OTHERWISE) or (token=_END);
  241. if (token=_ELSE) or (token=_OTHERWISE) then
  242. begin
  243. if not try_to_consume(_ELSE) then
  244. consume(_OTHERWISE);
  245. elseblock:=statements_til_end;
  246. end
  247. else
  248. begin
  249. elseblock:=nil;
  250. consume(_END);
  251. end;
  252. dec(statement_level);
  253. code:=ccasenode.create(caseexpr,instruc,root);
  254. tcasenode(code).elseblock:=elseblock;
  255. case_statement:=code;
  256. end;
  257. function repeat_statement : tnode;
  258. var
  259. first,last,p_e : tnode;
  260. begin
  261. consume(_REPEAT);
  262. first:=nil;
  263. inc(statement_level);
  264. while token<>_UNTIL do
  265. begin
  266. if first=nil then
  267. begin
  268. last:=cstatementnode.create(nil,statement);
  269. first:=last;
  270. end
  271. else
  272. begin
  273. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  274. last:=tstatementnode(last).left;
  275. end;
  276. if not try_to_consume(_SEMICOLON) then
  277. break;
  278. emptystats;
  279. end;
  280. consume(_UNTIL);
  281. dec(statement_level);
  282. first:=cblocknode.create(first);
  283. p_e:=comp_expr(true);
  284. repeat_statement:=genloopnode(repeatn,p_e,first,nil,false);
  285. end;
  286. function while_statement : tnode;
  287. var
  288. p_e,p_a : tnode;
  289. begin
  290. consume(_WHILE);
  291. p_e:=comp_expr(true);
  292. consume(_DO);
  293. p_a:=statement;
  294. while_statement:=genloopnode(whilen,p_e,p_a,nil,false);
  295. end;
  296. function for_statement : tnode;
  297. var
  298. p_e,tovalue,p_a : tnode;
  299. backward : boolean;
  300. begin
  301. { parse loop header }
  302. consume(_FOR);
  303. p_e:=expr;
  304. if token=_DOWNTO then
  305. begin
  306. consume(_DOWNTO);
  307. backward:=true;
  308. end
  309. else
  310. begin
  311. consume(_TO);
  312. backward:=false;
  313. end;
  314. tovalue:=comp_expr(true);
  315. consume(_DO);
  316. { ... now the instruction }
  317. p_a:=statement;
  318. for_statement:=genloopnode(forn,p_e,tovalue,p_a,backward);
  319. end;
  320. function _with_statement : tnode;
  321. var
  322. right,p : tnode;
  323. i,levelcount : longint;
  324. withsymtable,symtab : psymtable;
  325. obj : pobjectdef;
  326. hp : tnode;
  327. begin
  328. p:=comp_expr(true);
  329. do_firstpass(p);
  330. set_varstate(p,false);
  331. right:=nil;
  332. if (not codegenerror) and
  333. (p.resulttype^.deftype in [objectdef,recorddef]) then
  334. begin
  335. case p.resulttype^.deftype of
  336. objectdef : begin
  337. obj:=pobjectdef(p.resulttype);
  338. withsymtable:=new(pwithsymtable,init);
  339. withsymtable^.symsearch:=obj^.symtable^.symsearch;
  340. withsymtable^.defowner:=obj;
  341. symtab:=withsymtable;
  342. if (p.nodetype=loadn) and
  343. (tloadnode(p).symtable=aktprocsym^.definition^.localst) then
  344. pwithsymtable(symtab)^.direct_with:=true;
  345. {symtab^.withnode:=p; not yet allocated !! }
  346. pwithsymtable(symtab)^.withrefnode:=p;
  347. levelcount:=1;
  348. obj:=obj^.childof;
  349. while assigned(obj) do
  350. begin
  351. symtab^.next:=new(pwithsymtable,init);
  352. symtab:=symtab^.next;
  353. symtab^.symsearch:=obj^.symtable^.symsearch;
  354. if (p.nodetype=loadn) and
  355. (tloadnode(p).symtable=aktprocsym^.definition^.localst) then
  356. pwithsymtable(symtab)^.direct_with:=true;
  357. {symtab^.withnode:=p; not yet allocated !! }
  358. pwithsymtable(symtab)^.withrefnode:=p;
  359. symtab^.defowner:=obj;
  360. obj:=obj^.childof;
  361. inc(levelcount);
  362. end;
  363. symtab^.next:=symtablestack;
  364. symtablestack:=withsymtable;
  365. end;
  366. recorddef : begin
  367. symtab:=precorddef(p.resulttype)^.symtable;
  368. levelcount:=1;
  369. withsymtable:=new(pwithsymtable,init);
  370. withsymtable^.symsearch:=symtab^.symsearch;
  371. if (p.nodetype=loadn) and
  372. (tloadnode(p).symtable=aktprocsym^.definition^.localst) then
  373. pwithsymtable(withsymtable)^.direct_with:=true;
  374. {symtab^.withnode:=p; not yet allocated !! }
  375. pwithsymtable(withsymtable)^.withrefnode:=p;
  376. withsymtable^.defowner:=precorddef(p.resulttype);
  377. withsymtable^.next:=symtablestack;
  378. symtablestack:=withsymtable;
  379. end;
  380. end;
  381. if token=_COMMA then
  382. begin
  383. consume(_COMMA);
  384. right:=_with_statement{$ifdef FPCPROCVAR}(){$endif};
  385. end
  386. else
  387. begin
  388. consume(_DO);
  389. if token<>_SEMICOLON then
  390. right:=statement
  391. else
  392. right:=nil;
  393. end;
  394. for i:=1 to levelcount do
  395. symtablestack:=symtablestack^.next;
  396. _with_statement:=genwithnode(pwithsymtable(withsymtable),p,right,levelcount);
  397. end
  398. else
  399. begin
  400. Message(parser_e_false_with_expr);
  401. { try to recover from error }
  402. if token=_COMMA then
  403. begin
  404. consume(_COMMA);
  405. hp:=_with_statement{$ifdef FPCPROCVAR}(){$endif};
  406. if (hp=nil) then; { remove warning about unused }
  407. end
  408. else
  409. begin
  410. consume(_DO);
  411. { ignore all }
  412. if token<>_SEMICOLON then
  413. statement;
  414. end;
  415. _with_statement:=nil;
  416. end;
  417. end;
  418. function with_statement : tnode;
  419. begin
  420. consume(_WITH);
  421. with_statement:=_with_statement;
  422. end;
  423. function raise_statement : tnode;
  424. var
  425. p,pobj,paddr,pframe : tnode;
  426. begin
  427. pobj:=nil;
  428. paddr:=nil;
  429. pframe:=nil;
  430. consume(_RAISE);
  431. if not(token in [_SEMICOLON,_END]) then
  432. begin
  433. { object }
  434. pobj:=comp_expr(true);
  435. if try_to_consume(_AT) then
  436. begin
  437. paddr:=comp_expr(true);
  438. if try_to_consume(_COMMA) then
  439. pframe:=comp_expr(true);
  440. end;
  441. end
  442. else
  443. begin
  444. if (block_type<>bt_except) then
  445. Message(parser_e_no_reraise_possible);
  446. end;
  447. p:=craisenode.create(pobj,paddr,pframe);
  448. raise_statement:=p;
  449. end;
  450. function try_statement : tnode;
  451. var
  452. p_try_block,p_finally_block,first,last,
  453. p_default,p_specific,hp : tnode;
  454. ot : pobjectdef;
  455. sym : pvarsym;
  456. old_block_type : tblock_type;
  457. exceptsymtable : psymtable;
  458. objname : stringid;
  459. srsym : psym;
  460. srsymtable : psymtable;
  461. begin
  462. procinfo^.flags:=procinfo^.flags or
  463. pi_uses_exceptions;
  464. p_default:=nil;
  465. p_specific:=nil;
  466. { read statements to try }
  467. consume(_TRY);
  468. first:=nil;
  469. inc(statement_level);
  470. while (token<>_FINALLY) and (token<>_EXCEPT) do
  471. begin
  472. if first=nil then
  473. begin
  474. last:=cstatementnode.create(nil,statement);
  475. first:=last;
  476. end
  477. else
  478. begin
  479. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  480. last:=tstatementnode(last).left;
  481. end;
  482. if not try_to_consume(_SEMICOLON) then
  483. break;
  484. emptystats;
  485. end;
  486. p_try_block:=cblocknode.create(first);
  487. if try_to_consume(_FINALLY) then
  488. begin
  489. p_finally_block:=statements_til_end;
  490. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  491. dec(statement_level);
  492. end
  493. else
  494. begin
  495. consume(_EXCEPT);
  496. old_block_type:=block_type;
  497. block_type:=bt_except;
  498. ot:=pobjectdef(generrordef);
  499. p_specific:=nil;
  500. if (token=_ID) and (idtoken=_ON) then
  501. { catch specific exceptions }
  502. begin
  503. repeat
  504. consume(_ID);
  505. if token=_ID then
  506. begin
  507. objname:=pattern;
  508. { can't use consume_sym here, because we need already
  509. to check for the colon }
  510. searchsym(objname,srsym,srsymtable);
  511. consume(_ID);
  512. { is a explicit name for the exception given ? }
  513. if try_to_consume(_COLON) then
  514. begin
  515. consume_sym(srsym,srsymtable);
  516. if (srsym^.typ=typesym) and
  517. is_class(ptypesym(srsym)^.restype.def) then
  518. begin
  519. ot:=pobjectdef(ptypesym(srsym)^.restype.def);
  520. sym:=new(pvarsym,initdef(objname,ot));
  521. end
  522. else
  523. begin
  524. sym:=new(pvarsym,initdef(objname,new(perrordef,init)));
  525. if (srsym^.typ=typesym) then
  526. Message1(type_e_class_type_expected,ptypesym(srsym)^.restype.def^.typename)
  527. else
  528. Message1(type_e_class_type_expected,ot^.typename);
  529. end;
  530. exceptsymtable:=new(pstoredsymtable,init(stt_exceptsymtable));
  531. exceptsymtable^.insert(sym);
  532. { insert the exception symtable stack }
  533. exceptsymtable^.next:=symtablestack;
  534. symtablestack:=exceptsymtable;
  535. end
  536. else
  537. begin
  538. { check if type is valid, must be done here because
  539. with "e: Exception" the e is not necessary }
  540. if srsym=nil then
  541. begin
  542. identifier_not_found(objname);
  543. srsym:=generrorsym;
  544. end;
  545. { support unit.identifier }
  546. if srsym^.typ=unitsym then
  547. begin
  548. consume(_POINT);
  549. srsym:=searchsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  550. consume(_ID);
  551. if srsym=nil then
  552. begin
  553. identifier_not_found(objname);
  554. srsym:=generrorsym;
  555. end;
  556. end;
  557. { check if type is valid, must be done here because
  558. with "e: Exception" the e is not necessary }
  559. if (srsym^.typ=typesym) and
  560. is_class(ptypesym(srsym)^.restype.def) then
  561. ot:=pobjectdef(ptypesym(srsym)^.restype.def)
  562. else
  563. begin
  564. ot:=pobjectdef(generrordef);
  565. if (srsym^.typ=typesym) then
  566. Message1(type_e_class_type_expected,ptypesym(srsym)^.restype.def^.typename)
  567. else
  568. Message1(type_e_class_type_expected,ot^.typename);
  569. end;
  570. exceptsymtable:=nil;
  571. end;
  572. end
  573. else
  574. consume(_ID);
  575. consume(_DO);
  576. hp:=connode.create(nil,statement);
  577. if ot^.deftype=errordef then
  578. begin
  579. hp.free;
  580. hp:=cerrornode.create;
  581. end;
  582. if p_specific=nil then
  583. begin
  584. last:=hp;
  585. p_specific:=last;
  586. end
  587. else
  588. begin
  589. tonnode(last).left:=hp;
  590. last:=tonnode(last).left;
  591. end;
  592. { set the informations }
  593. { only if the creation of the onnode was succesful, it's possible }
  594. { that last and hp are errornodes (JM) }
  595. if last.nodetype = onn then
  596. begin
  597. tonnode(last).excepttype:=ot;
  598. tonnode(last).exceptsymtable:=exceptsymtable;
  599. end;
  600. { remove exception symtable }
  601. if assigned(exceptsymtable) then
  602. begin
  603. dellexlevel;
  604. if last.nodetype <> onn then
  605. dispose(exceptsymtable,done);
  606. end;
  607. if not try_to_consume(_SEMICOLON) then
  608. break;
  609. emptystats;
  610. until (token=_END) or (token=_ELSE);
  611. if token=_ELSE then
  612. { catch the other exceptions }
  613. begin
  614. consume(_ELSE);
  615. p_default:=statements_til_end;
  616. end
  617. else
  618. consume(_END);
  619. end
  620. else
  621. { catch all exceptions }
  622. begin
  623. p_default:=statements_til_end;
  624. end;
  625. dec(statement_level);
  626. block_type:=old_block_type;
  627. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  628. end;
  629. end;
  630. function exit_statement : tnode;
  631. var
  632. p : tnode;
  633. begin
  634. consume(_EXIT);
  635. if try_to_consume(_LKLAMMER) then
  636. begin
  637. p:=comp_expr(true);
  638. consume(_RKLAMMER);
  639. if (block_type=bt_except) then
  640. Message(parser_e_exit_with_argument_not__possible);
  641. if procinfo^.returntype.def=pdef(voiddef) then
  642. Message(parser_e_void_function);
  643. end
  644. else
  645. p:=nil;
  646. p:=cexitnode.create(p);
  647. // p.resulttype:=procinfo^.returntype.def;
  648. p.resulttype:=voiddef;
  649. exit_statement:=p;
  650. end;
  651. function _asm_statement : tnode;
  652. var
  653. asmstat : tasmnode;
  654. Marker : tai;
  655. begin
  656. Inside_asm_statement:=true;
  657. case aktasmmode of
  658. asmmode_none : ; { just be there to allow to a compile without
  659. any assembler readers }
  660. {$ifdef i386}
  661. {$ifndef NoRA386Att}
  662. asmmode_i386_att:
  663. asmstat:=tasmnode(ra386att.assemble);
  664. {$endif NoRA386Att}
  665. {$ifndef NoRA386Int}
  666. asmmode_i386_intel:
  667. asmstat:=tasmnode(ra386int.assemble);
  668. {$endif NoRA386Int}
  669. {$ifndef NoRA386Dir}
  670. asmmode_i386_direct:
  671. begin
  672. if not target_asm.allowdirect then
  673. Message(parser_f_direct_assembler_not_allowed);
  674. if (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  675. Begin
  676. Message1(parser_w_not_supported_for_inline,'direct asm');
  677. Message(parser_w_inlining_disabled);
  678. exclude(aktprocsym^.definition^.proccalloptions,pocall_inline);
  679. End;
  680. asmstat:=tasmnode(ra386dir.assemble);
  681. end;
  682. {$endif NoRA386Dir}
  683. {$endif}
  684. {$ifdef m68k}
  685. {$ifndef NoRA68kMot}
  686. asmmode_m68k_mot:
  687. asmstat:=tasmnode(ra68kmot.assemble);
  688. {$endif NoRA68kMot}
  689. {$endif}
  690. else
  691. Message(parser_f_assembler_reader_not_supported);
  692. end;
  693. { Read first the _ASM statement }
  694. consume(_ASM);
  695. {$ifndef newcg}
  696. { END is read }
  697. if try_to_consume(_LECKKLAMMER) then
  698. begin
  699. { it's possible to specify the modified registers }
  700. include(asmstat.flags,nf_object_preserved);
  701. if token<>_RECKKLAMMER then
  702. repeat
  703. { uppercase, because it's a CSTRING }
  704. uppervar(pattern);
  705. {$ifdef i386}
  706. if pattern='EAX' then
  707. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  708. else if pattern='EBX' then
  709. usedinproc:=usedinproc or ($80 shr byte(R_EBX))
  710. else if pattern='ECX' then
  711. usedinproc:=usedinproc or ($80 shr byte(R_ECX))
  712. else if pattern='EDX' then
  713. usedinproc:=usedinproc or ($80 shr byte(R_EDX))
  714. else if pattern='ESI' then
  715. begin
  716. usedinproc:=usedinproc or ($80 shr byte(R_ESI));
  717. exclude(asmstat.flags,nf_object_preserved);
  718. end
  719. else if pattern='EDI' then
  720. usedinproc:=usedinproc or ($80 shr byte(R_EDI))
  721. {$endif i386}
  722. {$ifdef m68k}
  723. if pattern='D0' then
  724. usedinproc:=usedinproc or ($800 shr word(R_D0))
  725. else if pattern='D1' then
  726. usedinproc:=usedinproc or ($800 shr word(R_D1))
  727. else if pattern='D6' then
  728. usedinproc:=usedinproc or ($800 shr word(R_D6))
  729. else if pattern='A0' then
  730. usedinproc:=usedinproc or ($800 shr word(R_A0))
  731. else if pattern='A1' then
  732. usedinproc:=usedinproc or ($800 shr word(R_A1))
  733. {$endif m68k}
  734. else consume(_RECKKLAMMER);
  735. consume(_CSTRING);
  736. if not try_to_consume(_COMMA) then
  737. break;
  738. until false;
  739. consume(_RECKKLAMMER);
  740. end
  741. else usedinproc:=$ff;
  742. {$endif newcg}
  743. { mark the start and the end of the assembler block
  744. this is needed for the optimizer }
  745. If Assigned(AsmStat.p_asm) Then
  746. Begin
  747. Marker := Tai_Marker.Create(AsmBlockStart);
  748. AsmStat.p_asm.Insert(Marker);
  749. Marker := Tai_Marker.Create(AsmBlockEnd);
  750. AsmStat.p_asm.Concat(Marker);
  751. End;
  752. Inside_asm_statement:=false;
  753. _asm_statement:=asmstat;
  754. end;
  755. function new_dispose_statement : tnode;
  756. var
  757. p,p2 : tnode;
  758. again : boolean; { dummy for do_proc_call }
  759. destructorname : stringid;
  760. sym : psym;
  761. classh : pobjectdef;
  762. pd,pd2 : pdef;
  763. destructorpos,
  764. storepos : tfileposinfo;
  765. is_new : boolean;
  766. begin
  767. if try_to_consume(_NEW) then
  768. is_new:=true
  769. else
  770. begin
  771. consume(_DISPOSE);
  772. is_new:=false;
  773. end;
  774. consume(_LKLAMMER);
  775. p:=comp_expr(true);
  776. { calc return type }
  777. cleartempgen;
  778. do_firstpass(p);
  779. set_varstate(p,(not is_new));
  780. { constructor,destructor specified }
  781. if try_to_consume(_COMMA) then
  782. begin
  783. { extended syntax of new and dispose }
  784. { function styled new is handled in factor }
  785. { destructors have no parameters }
  786. destructorname:=pattern;
  787. destructorpos:=akttokenpos;
  788. consume(_ID);
  789. pd:=p.resulttype;
  790. if pd=nil then
  791. pd:=generrordef;
  792. pd2:=pd;
  793. if (pd^.deftype<>pointerdef) then
  794. begin
  795. Message1(type_e_pointer_type_expected,pd^.typename);
  796. p.free;
  797. p:=factor(false);
  798. p.free;
  799. consume(_RKLAMMER);
  800. new_dispose_statement:=cerrornode.create;
  801. exit;
  802. end;
  803. { first parameter must be an object or class }
  804. if ppointerdef(pd)^.pointertype.def^.deftype<>objectdef then
  805. begin
  806. Message(parser_e_pointer_to_class_expected);
  807. p.free;
  808. new_dispose_statement:=factor(false);
  809. consume_all_until(_RKLAMMER);
  810. consume(_RKLAMMER);
  811. exit;
  812. end;
  813. { check, if the first parameter is a pointer to a _class_ }
  814. classh:=pobjectdef(ppointerdef(pd)^.pointertype.def);
  815. if is_class(classh) then
  816. begin
  817. Message(parser_e_no_new_or_dispose_for_classes);
  818. new_dispose_statement:=factor(false);
  819. consume_all_until(_RKLAMMER);
  820. consume(_RKLAMMER);
  821. exit;
  822. end;
  823. { search cons-/destructor, also in parent classes }
  824. storepos:=akttokenpos;
  825. akttokenpos:=destructorpos;
  826. sym:=search_class_member(classh,destructorname);
  827. akttokenpos:=storepos;
  828. { the second parameter of new/dispose must be a call }
  829. { to a cons-/destructor }
  830. if (not assigned(sym)) or (sym^.typ<>procsym) then
  831. begin
  832. if is_new then
  833. Message(parser_e_expr_have_to_be_constructor_call)
  834. else
  835. Message(parser_e_expr_have_to_be_destructor_call);
  836. p.free;
  837. new_dispose_statement:=cerrornode.create;
  838. end
  839. else
  840. begin
  841. if is_new then
  842. p2:=chnewnode.create
  843. else
  844. p2:=chdisposenode.create(p);
  845. if is_new then
  846. begin
  847. { Constructors can take parameters.}
  848. p2.resulttype:=ppointerdef(pd)^.pointertype.def;
  849. do_member_read(false,sym,p2,pd,again);
  850. end
  851. else
  852. begin
  853. if (m_tp in aktmodeswitches) then
  854. begin
  855. { Constructors can take parameters.}
  856. p2.resulttype:=ppointerdef(pd)^.pointertype.def;
  857. do_member_read(false,sym,p2,pd,again);
  858. end
  859. else
  860. begin
  861. p2:=ccallnode.create(pprocsym(sym),sym^.owner,p2);
  862. { support dispose(p,done()); }
  863. if try_to_consume(_LKLAMMER) then
  864. begin
  865. if not try_to_consume(_RKLAMMER) then
  866. begin
  867. Message(parser_e_no_paras_for_destructor);
  868. consume_all_until(_RKLAMMER);
  869. consume(_RKLAMMER);
  870. end;
  871. end;
  872. end;
  873. end;
  874. { we need the real called method }
  875. cleartempgen;
  876. do_firstpass(p2);
  877. if not codegenerror then
  878. begin
  879. if is_new then
  880. begin
  881. if (tcallnode(p2).procdefinition^.proctypeoption<>potype_constructor) then
  882. Message(parser_e_expr_have_to_be_constructor_call);
  883. p2:=cassignmentnode.create(p,cnewnode.create(p2));
  884. tassignmentnode(p2).right.resulttype:=pd2;
  885. end
  886. else
  887. begin
  888. if (tcallnode(p2).procdefinition^.proctypeoption<>potype_destructor) then
  889. Message(parser_e_expr_have_to_be_destructor_call);
  890. end;
  891. end;
  892. new_dispose_statement:=p2;
  893. end;
  894. end
  895. else
  896. begin
  897. if p.resulttype=nil then
  898. p.resulttype:=generrordef;
  899. if (p.resulttype^.deftype<>pointerdef) then
  900. Begin
  901. Message1(type_e_pointer_type_expected,p.resulttype^.typename);
  902. new_dispose_statement:=cerrornode.create;
  903. end
  904. else
  905. begin
  906. if (ppointerdef(p.resulttype)^.pointertype.def^.deftype=objectdef) and
  907. (oo_has_vmt in pobjectdef(ppointerdef(p.resulttype)^.pointertype.def)^.objectoptions) then
  908. Message(parser_w_use_extended_syntax_for_objects);
  909. if (ppointerdef(p.resulttype)^.pointertype.def^.deftype=orddef) and
  910. (porddef(ppointerdef(p.resulttype)^.pointertype.def)^.typ=uvoid) then
  911. begin
  912. if (m_tp in aktmodeswitches) or
  913. (m_delphi in aktmodeswitches) then
  914. Message(parser_w_no_new_dispose_on_void_pointers)
  915. else
  916. Message(parser_e_no_new_dispose_on_void_pointers);
  917. end;
  918. if is_new then
  919. new_dispose_statement:=csimplenewdisposenode.create(simplenewn,p)
  920. else
  921. new_dispose_statement:=csimplenewdisposenode.create(simpledisposen,p);
  922. end;
  923. end;
  924. consume(_RKLAMMER);
  925. end;
  926. function statement : tnode;
  927. var
  928. p : tnode;
  929. code : tnode;
  930. filepos : tfileposinfo;
  931. srsym : psym;
  932. srsymtable : psymtable;
  933. begin
  934. filepos:=akttokenpos;
  935. case token of
  936. _GOTO :
  937. begin
  938. if not(cs_support_goto in aktmoduleswitches)then
  939. Message(sym_e_goto_and_label_not_supported);
  940. consume(_GOTO);
  941. if (token<>_INTCONST) and (token<>_ID) then
  942. begin
  943. Message(sym_e_label_not_found);
  944. code:=cerrornode.create;
  945. end
  946. else
  947. begin
  948. consume_sym(srsym,srsymtable);
  949. if srsym^.typ<>labelsym then
  950. begin
  951. Message(sym_e_id_is_no_label_id);
  952. code:=cerrornode.create;
  953. end
  954. else
  955. begin
  956. code:=cgotonode.create(plabelsym(srsym)^.lab);
  957. tgotonode(code).labsym:=plabelsym(srsym);
  958. { set flag that this label is used }
  959. plabelsym(srsym)^.used:=true;
  960. end;
  961. end;
  962. end;
  963. _BEGIN :
  964. code:=statement_block(_BEGIN);
  965. _IF :
  966. code:=if_statement;
  967. _CASE :
  968. code:=case_statement;
  969. _REPEAT :
  970. code:=repeat_statement;
  971. _WHILE :
  972. code:=while_statement;
  973. _FOR :
  974. code:=for_statement;
  975. _NEW,
  976. _DISPOSE :
  977. code:=new_dispose_statement;
  978. _WITH :
  979. code:=with_statement;
  980. _TRY :
  981. code:=try_statement;
  982. _RAISE :
  983. code:=raise_statement;
  984. { semicolons,else until and end are ignored }
  985. _SEMICOLON,
  986. _ELSE,
  987. _UNTIL,
  988. _END:
  989. code:=cnothingnode.create;
  990. _FAIL :
  991. begin
  992. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  993. Message(parser_e_fail_only_in_constructor);
  994. consume(_FAIL);
  995. code:=cfailnode.create;
  996. end;
  997. _EXIT :
  998. code:=exit_statement;
  999. _ASM :
  1000. code:=_asm_statement;
  1001. _EOF :
  1002. Message(scan_f_end_of_file);
  1003. else
  1004. begin
  1005. p:=expr;
  1006. if p.nodetype=labeln then
  1007. begin
  1008. { the pointer to the following instruction }
  1009. { isn't a very clean way }
  1010. tlabelnode(p).left:=statement{$ifdef FPCPROCVAR}(){$endif};
  1011. end;
  1012. if not(p.nodetype in [calln,assignn,breakn,inlinen,continuen,labeln]) then
  1013. Message(cg_e_illegal_expression);
  1014. { specify that we don't use the value returned by the call }
  1015. { Question : can this be also improtant
  1016. for inlinen ??
  1017. it is used for :
  1018. - dispose of temp stack space
  1019. - dispose on FPU stack }
  1020. if p.nodetype=calln then
  1021. exclude(p.flags,nf_return_value_used);
  1022. code:=p;
  1023. end;
  1024. end;
  1025. if assigned(code) then
  1026. code.set_tree_filepos(filepos);
  1027. statement:=code;
  1028. end;
  1029. function statement_block(starttoken : ttoken) : tnode;
  1030. var
  1031. first,last : tnode;
  1032. filepos : tfileposinfo;
  1033. begin
  1034. first:=nil;
  1035. filepos:=akttokenpos;
  1036. consume(starttoken);
  1037. inc(statement_level);
  1038. while not(token in [_END,_FINALIZATION]) do
  1039. begin
  1040. if first=nil then
  1041. begin
  1042. last:=cstatementnode.create(nil,statement);
  1043. first:=last;
  1044. end
  1045. else
  1046. begin
  1047. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  1048. last:=tstatementnode(last).left;
  1049. end;
  1050. if (token in [_END,_FINALIZATION]) then
  1051. break
  1052. else
  1053. begin
  1054. { if no semicolon, then error and go on }
  1055. if token<>_SEMICOLON then
  1056. begin
  1057. consume(_SEMICOLON);
  1058. consume_all_until(_SEMICOLON);
  1059. end;
  1060. consume(_SEMICOLON);
  1061. end;
  1062. emptystats;
  1063. end;
  1064. { don't consume the finalization token, it is consumed when
  1065. reading the finalization block, but allow it only after
  1066. an initalization ! }
  1067. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1068. consume(_END);
  1069. dec(statement_level);
  1070. last:=cblocknode.create(first);
  1071. last.set_tree_filepos(filepos);
  1072. statement_block:=last;
  1073. end;
  1074. function assembler_block : tnode;
  1075. begin
  1076. { temporary space is set, while the BEGIN of the procedure }
  1077. if symtablestack^.symtabletype=localsymtable then
  1078. procinfo^.firsttemp_offset := -symtablestack^.datasize
  1079. else
  1080. procinfo^.firsttemp_offset := 0;
  1081. { assembler code does not allocate }
  1082. { space for the return value }
  1083. if procinfo^.returntype.def<>pdef(voiddef) then
  1084. begin
  1085. if ret_in_acc(procinfo^.returntype.def) then
  1086. begin
  1087. { in assembler code the result should be directly in %eax
  1088. procinfo^.retoffset:=procinfo^.firsttemp-procinfo^.retdef^.size;
  1089. procinfo^.firsttemp:=procinfo^.retoffset; }
  1090. {$ifndef newcg}
  1091. {$ifdef i386}
  1092. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  1093. {$endif}
  1094. {$ifdef m68k}
  1095. usedinproc:=usedinproc or ($800 shr word(R_D0))
  1096. {$endif}
  1097. {$endif newcg}
  1098. end
  1099. {
  1100. else if not is_fpu(procinfo^.retdef) then
  1101. should we allow assembler functions of big elements ?
  1102. YES (FK)!!
  1103. Message(parser_e_asm_incomp_with_function_return);
  1104. }
  1105. end;
  1106. { set the framepointer to esp for assembler functions }
  1107. { but only if the are no local variables }
  1108. { added no parameter also (PM) }
  1109. { disable for methods, because self pointer is expected }
  1110. { at -8(%ebp) (JM) }
  1111. { why if se use %esp then self is still at the correct address PM }
  1112. if {not(assigned(procinfo^._class)) and}
  1113. (po_assembler in aktprocsym^.definition^.procoptions) and
  1114. (aktprocsym^.definition^.localst^.datasize=0) and
  1115. (aktprocsym^.definition^.parast^.datasize=0) and
  1116. not(ret_in_param(aktprocsym^.definition^.rettype.def)) then
  1117. begin
  1118. procinfo^.framepointer:=stack_pointer;
  1119. { set the right value for parameters }
  1120. dec(aktprocsym^.definition^.parast^.address_fixup,target_os.size_of_pointer);
  1121. dec(procinfo^.para_offset,target_os.size_of_pointer);
  1122. end;
  1123. { force the asm statement }
  1124. if token<>_ASM then
  1125. consume(_ASM);
  1126. procinfo^.Flags := procinfo^.Flags Or pi_is_assembler;
  1127. assembler_block:=_asm_statement;
  1128. { becuase the END is already read we need to get the
  1129. last_endtoken_filepos here (PFV) }
  1130. last_endtoken_filepos:=akttokenpos;
  1131. end;
  1132. end.
  1133. {
  1134. $Log$
  1135. Revision 1.21 2001-03-22 22:35:42 florian
  1136. + support for type a = (a=1); in Delphi mode added
  1137. + procedure p(); in Delphi mode supported
  1138. + on isn't keyword anymore, it can be used as
  1139. id etc. now
  1140. Revision 1.20 2001/03/11 22:58:50 peter
  1141. * getsym redesign, removed the globals srsym,srsymtable
  1142. Revision 1.19 2000/12/25 00:07:27 peter
  1143. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1144. tlinkedlist objects)
  1145. Revision 1.18 2000/12/23 19:59:35 peter
  1146. * object to class for ow/og objects
  1147. * split objectdata from objectoutput
  1148. Revision 1.17 2000/12/16 22:45:55 jonas
  1149. * fixed case statements with int64 values
  1150. Revision 1.16 2000/11/29 00:30:37 florian
  1151. * unused units removed from uses clause
  1152. * some changes for widestrings
  1153. Revision 1.15 2000/11/27 15:47:19 jonas
  1154. * fix for web bug 1251 (example 1)
  1155. Revision 1.14 2000/11/22 22:43:34 peter
  1156. * fixed crash with exception without sysutils (merged)
  1157. Revision 1.13 2000/11/04 14:25:21 florian
  1158. + merged Attila's changes for interfaces, not tested yet
  1159. Revision 1.12 2000/10/31 22:02:50 peter
  1160. * symtable splitted, no real code changes
  1161. Revision 1.11 2000/10/14 21:52:56 peter
  1162. * fixed memory leaks
  1163. Revision 1.10 2000/10/14 10:14:52 peter
  1164. * moehrendorf oct 2000 rewrite
  1165. Revision 1.9 2000/10/01 19:48:25 peter
  1166. * lot of compile updates for cg11
  1167. Revision 1.8 2000/09/24 21:19:50 peter
  1168. * delphi compile fixes
  1169. Revision 1.7 2000/09/24 15:06:24 peter
  1170. * use defines.inc
  1171. Revision 1.6 2000/08/27 16:11:52 peter
  1172. * moved some util functions from globals,cobjects to cutils
  1173. * splitted files into finput,fmodule
  1174. Revision 1.5 2000/08/12 15:41:15 peter
  1175. * fixed bug 1096 (merged)
  1176. Revision 1.4 2000/08/12 06:46:06 florian
  1177. + case statement for int64/qword implemented
  1178. Revision 1.3 2000/07/13 12:08:27 michael
  1179. + patched to 1.1.0 with former 1.09patch from peter
  1180. Revision 1.2 2000/07/13 11:32:45 michael
  1181. + removed logs
  1182. }