pstatmnt.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does the parsing of the statements
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pstatmnt;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,node;
  22. function statement_block(starttoken : ttoken) : tnode;
  23. { reads an assembler block }
  24. function assembler_block : tnode;
  25. implementation
  26. uses
  27. { common }
  28. cutils,
  29. { global }
  30. globtype,globals,verbose,
  31. systems,
  32. { aasm }
  33. cpubase,aasmbase,aasmtai,
  34. { symtable }
  35. symconst,symbase,symtype,symdef,symsym,symtable,defutil,defcmp,
  36. paramgr,symutil,
  37. { pass 1 }
  38. pass_1,htypechk,
  39. nutils,nbas,nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  40. { parser }
  41. scanner,
  42. pbase,pexpr,
  43. { codegen }
  44. procinfo,cgbase,
  45. { assembler reader }
  46. rabase
  47. ;
  48. function statement : tnode;forward;
  49. function if_statement : tnode;
  50. var
  51. ex,if_a,else_a : tnode;
  52. begin
  53. consume(_IF);
  54. ex:=comp_expr(true);
  55. consume(_THEN);
  56. if token<>_ELSE then
  57. if_a:=statement
  58. else
  59. if_a:=nil;
  60. if try_to_consume(_ELSE) then
  61. else_a:=statement
  62. else
  63. else_a:=nil;
  64. result:=cifnode.create(ex,if_a,else_a);
  65. end;
  66. { creates a block (list) of statements, til the next END token }
  67. function statements_til_end : tnode;
  68. var
  69. first,last : tstatementnode;
  70. begin
  71. first:=nil;
  72. while token<>_END do
  73. begin
  74. if first=nil then
  75. begin
  76. last:=cstatementnode.create(statement,nil);
  77. first:=last;
  78. end
  79. else
  80. begin
  81. last.right:=cstatementnode.create(statement,nil);
  82. last:=tstatementnode(last.right);
  83. end;
  84. if not try_to_consume(_SEMICOLON) then
  85. break;
  86. consume_emptystats;
  87. end;
  88. consume(_END);
  89. statements_til_end:=cblocknode.create(first);
  90. end;
  91. function case_statement : tnode;
  92. var
  93. casedef : tdef;
  94. caseexpr,p : tnode;
  95. blockid : longint;
  96. hl1,hl2 : TConstExprInt;
  97. casedeferror : boolean;
  98. casenode : tcasenode;
  99. begin
  100. consume(_CASE);
  101. caseexpr:=comp_expr(true);
  102. { determines result type }
  103. do_resulttypepass(caseexpr);
  104. set_varstate(caseexpr,vs_read,[vsf_must_be_valid]);
  105. casedeferror:=false;
  106. casedef:=caseexpr.resulttype.def;
  107. if (not assigned(casedef)) or
  108. not(is_ordinal(casedef)) then
  109. begin
  110. CGMessage(type_e_ordinal_expr_expected);
  111. { create a correct tree }
  112. caseexpr.free;
  113. caseexpr:=cordconstnode.create(0,u32inttype,false);
  114. { set error flag so no rangechecks are done }
  115. casedeferror:=true;
  116. end;
  117. { Create casenode }
  118. casenode:=ccasenode.create(caseexpr);
  119. consume(_OF);
  120. { Parse all case blocks }
  121. blockid:=0;
  122. repeat
  123. { maybe an instruction has more case labels }
  124. repeat
  125. p:=expr;
  126. if is_widechar(casedef) then
  127. begin
  128. if (p.nodetype=rangen) then
  129. begin
  130. trangenode(p).left:=ctypeconvnode.create(trangenode(p).left,cwidechartype);
  131. trangenode(p).right:=ctypeconvnode.create(trangenode(p).right,cwidechartype);
  132. do_resulttypepass(trangenode(p).left);
  133. do_resulttypepass(trangenode(p).right);
  134. end
  135. else
  136. begin
  137. p:=ctypeconvnode.create(p,cwidechartype);
  138. do_resulttypepass(p);
  139. end;
  140. end;
  141. hl1:=0;
  142. hl2:=0;
  143. if (p.nodetype=rangen) then
  144. begin
  145. { type checking for case statements }
  146. if is_subequal(casedef, trangenode(p).left.resulttype.def) and
  147. is_subequal(casedef, trangenode(p).right.resulttype.def) then
  148. begin
  149. hl1:=get_ordinal_value(trangenode(p).left);
  150. hl2:=get_ordinal_value(trangenode(p).right);
  151. if hl1>hl2 then
  152. CGMessage(parser_e_case_lower_less_than_upper_bound);
  153. if not casedeferror then
  154. begin
  155. testrange(casedef,hl1,false);
  156. testrange(casedef,hl2,false);
  157. end;
  158. end
  159. else
  160. CGMessage(parser_e_case_mismatch);
  161. casenode.addlabel(blockid,hl1,hl2);
  162. end
  163. else
  164. begin
  165. { type checking for case statements }
  166. if not is_subequal(casedef, p.resulttype.def) then
  167. CGMessage(parser_e_case_mismatch);
  168. hl1:=get_ordinal_value(p);
  169. if not casedeferror then
  170. testrange(casedef,hl1,false);
  171. casenode.addlabel(blockid,hl1,hl1);
  172. end;
  173. p.free;
  174. if token=_COMMA then
  175. consume(_COMMA)
  176. else
  177. break;
  178. until false;
  179. consume(_COLON);
  180. { add instruction block }
  181. casenode.addblock(blockid,statement);
  182. { next block }
  183. inc(blockid);
  184. if not(token in [_ELSE,_OTHERWISE,_END]) then
  185. consume(_SEMICOLON);
  186. until (token in [_ELSE,_OTHERWISE,_END]);
  187. if (token in [_ELSE,_OTHERWISE]) then
  188. begin
  189. if not try_to_consume(_ELSE) then
  190. consume(_OTHERWISE);
  191. casenode.addelseblock(statements_til_end);
  192. end
  193. else
  194. consume(_END);
  195. result:=casenode;
  196. end;
  197. function repeat_statement : tnode;
  198. var
  199. first,last,p_e : tnode;
  200. begin
  201. consume(_REPEAT);
  202. first:=nil;
  203. while token<>_UNTIL do
  204. begin
  205. if first=nil then
  206. begin
  207. last:=cstatementnode.create(statement,nil);
  208. first:=last;
  209. end
  210. else
  211. begin
  212. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  213. last:=tstatementnode(last).right;
  214. end;
  215. if not try_to_consume(_SEMICOLON) then
  216. break;
  217. consume_emptystats;
  218. end;
  219. consume(_UNTIL);
  220. first:=cblocknode.create(first);
  221. p_e:=comp_expr(true);
  222. result:=cwhilerepeatnode.create(p_e,first,false,true);
  223. end;
  224. function while_statement : tnode;
  225. var
  226. p_e,p_a : tnode;
  227. begin
  228. consume(_WHILE);
  229. p_e:=comp_expr(true);
  230. consume(_DO);
  231. p_a:=statement;
  232. result:=cwhilerepeatnode.create(p_e,p_a,true,false);
  233. end;
  234. function for_statement : tnode;
  235. procedure check_range(hp:tnode);
  236. begin
  237. {$ifndef cpu64bit}
  238. if hp.nodetype=ordconstn then
  239. begin
  240. if (tordconstnode(hp).value<low(longint)) or
  241. (tordconstnode(hp).value>high(longint)) then
  242. begin
  243. CGMessage(parser_e_range_check_error);
  244. { recover, prevent more warnings/errors }
  245. tordconstnode(hp).value:=0;
  246. end;
  247. end;
  248. {$endif cpu64bit}
  249. end;
  250. var
  251. hp,
  252. hloopvar,
  253. hblock,
  254. hto,hfrom : tnode;
  255. backward : boolean;
  256. loopvarsym : tabstractvarsym;
  257. begin
  258. { parse loop header }
  259. consume(_FOR);
  260. hloopvar:=factor(false);
  261. valid_for_assignment(hloopvar,true);
  262. { Check loop variable }
  263. loopvarsym:=nil;
  264. { variable must be an ordinal, int64 is not allowed for 32bit targets }
  265. if not(is_ordinal(hloopvar.resulttype.def))
  266. {$ifndef cpu64bit}
  267. or is_64bitint(hloopvar.resulttype.def)
  268. {$endif cpu64bit}
  269. then
  270. MessagePos(hloopvar.fileinfo,type_e_ordinal_expr_expected);
  271. hp:=hloopvar;
  272. while assigned(hp) and
  273. (
  274. { record/object fields are allowed in tp7 mode only }
  275. (
  276. (m_tp7 in aktmodeswitches) and
  277. (hp.nodetype=subscriptn) and
  278. ((tsubscriptnode(hp).left.resulttype.def.deftype=recorddef) or
  279. is_object(tsubscriptnode(hp).left.resulttype.def))
  280. ) or
  281. { constant array index }
  282. (
  283. (hp.nodetype=vecn) and
  284. is_constintnode(tvecnode(hp).right)
  285. ) or
  286. { equal typeconversions }
  287. (
  288. (hp.nodetype=typeconvn) and
  289. (ttypeconvnode(hp).convtype=tc_equal)
  290. )
  291. ) do
  292. begin
  293. { Use the recordfield for loopvarsym }
  294. if not assigned(loopvarsym) and
  295. (hp.nodetype=subscriptn) then
  296. loopvarsym:=tsubscriptnode(hp).vs;
  297. hp:=tunarynode(hp).left;
  298. end;
  299. if assigned(hp) and
  300. (hp.nodetype=loadn) then
  301. begin
  302. case tloadnode(hp).symtableentry.typ of
  303. globalvarsym,
  304. localvarsym,
  305. paravarsym :
  306. begin
  307. { we need a simple loadn and the load must be in a global symtable or
  308. in the same level as the para of the current proc }
  309. if (
  310. (tloadnode(hp).symtable.symtablelevel=main_program_level) or
  311. (tloadnode(hp).symtable.symtablelevel=current_procinfo.procdef.parast.symtablelevel)
  312. ) and
  313. not(
  314. ((tabstractvarsym(tloadnode(hp).symtableentry).varspez in [vs_var,vs_out]) or
  315. (vo_is_thread_var in tabstractvarsym(tloadnode(hp).symtableentry).varoptions))
  316. ) then
  317. begin
  318. { Assigning for-loop variable is only allowed in tp7 }
  319. if not(m_tp7 in aktmodeswitches) then
  320. begin
  321. if not assigned(loopvarsym) then
  322. loopvarsym:=tabstractvarsym(tloadnode(hp).symtableentry);
  323. include(loopvarsym.varoptions,vo_is_loop_counter);
  324. end;
  325. end
  326. else
  327. MessagePos(hp.fileinfo,type_e_illegal_count_var);
  328. end;
  329. typedconstsym :
  330. begin
  331. { Bad programming, only allowed in tp7 mode }
  332. if not(m_tp7 in aktmodeswitches) then
  333. MessagePos(hp.fileinfo,type_e_illegal_count_var);
  334. end;
  335. else
  336. MessagePos(hp.fileinfo,type_e_illegal_count_var);
  337. end;
  338. end
  339. else
  340. MessagePos(hloopvar.fileinfo,type_e_illegal_count_var);
  341. consume(_ASSIGNMENT);
  342. hfrom:=comp_expr(true);
  343. if try_to_consume(_DOWNTO) then
  344. backward:=true
  345. else
  346. begin
  347. consume(_TO);
  348. backward:=false;
  349. end;
  350. hto:=comp_expr(true);
  351. consume(_DO);
  352. { Check if the constants fit in the range }
  353. check_range(hfrom);
  354. check_range(hto);
  355. { first set the varstate for from and to, so
  356. uses of loopvar in those expressions will also
  357. trigger a warning when it is not used yet. This
  358. needs to be done before the instruction block is
  359. parsed to have a valid hloopvar }
  360. resulttypepass(hfrom);
  361. set_varstate(hfrom,vs_read,[vsf_must_be_valid]);
  362. resulttypepass(hto);
  363. set_varstate(hto,vs_read,[vsf_must_be_valid]);
  364. resulttypepass(hloopvar);
  365. set_varstate(hloopvar,vs_readwritten,[]);
  366. { ... now the instruction block }
  367. hblock:=statement;
  368. { variable is not used for loop counter anymore }
  369. if assigned(loopvarsym) then
  370. exclude(loopvarsym.varoptions,vo_is_loop_counter);
  371. result:=cfornode.create(hloopvar,hfrom,hto,hblock,backward);
  372. end;
  373. function _with_statement : tnode;
  374. var
  375. right,p : tnode;
  376. i,levelcount : longint;
  377. withsymtable,symtab : tsymtable;
  378. obj : tobjectdef;
  379. hp : tnode;
  380. newblock : tblocknode;
  381. newstatement : tstatementnode;
  382. calltempp,
  383. loadp : ttempcreatenode;
  384. refp : tnode;
  385. htype : ttype;
  386. hasimplicitderef : boolean;
  387. begin
  388. p:=comp_expr(true);
  389. do_resulttypepass(p);
  390. right:=nil;
  391. if (not codegenerror) and
  392. (p.resulttype.def.deftype in [objectdef,recorddef]) then
  393. begin
  394. newblock:=nil;
  395. { ignore nodes that don't add instructions in the tree }
  396. hp:=p;
  397. while { equal type conversions }
  398. (
  399. (hp.nodetype=typeconvn) and
  400. (ttypeconvnode(hp).convtype=tc_equal)
  401. ) or
  402. { constant array index }
  403. (
  404. (hp.nodetype=vecn) and
  405. (tvecnode(hp).right.nodetype=ordconstn)
  406. ) do
  407. hp:=tunarynode(hp).left;
  408. if (hp.nodetype=loadn) and
  409. (
  410. (tloadnode(hp).symtable=current_procinfo.procdef.localst) or
  411. (tloadnode(hp).symtable=current_procinfo.procdef.parast) or
  412. (tloadnode(hp).symtable.symtabletype in [staticsymtable,globalsymtable])
  413. ) then
  414. begin
  415. { simple load, we can reference direct }
  416. loadp:=nil;
  417. refp:=p;
  418. end
  419. else
  420. begin
  421. calltempp:=nil;
  422. { complex load, load in temp first }
  423. newblock:=internalstatements(newstatement);
  424. { when right is a call then load it first in a temp }
  425. if p.nodetype=calln then
  426. begin
  427. calltempp:=ctempcreatenode.create(p.resulttype,p.resulttype.def.size,tt_persistent,false);
  428. addstatement(newstatement,calltempp);
  429. addstatement(newstatement,cassignmentnode.create(
  430. ctemprefnode.create(calltempp),
  431. p));
  432. p:=ctemprefnode.create(calltempp);
  433. resulttypepass(p);
  434. end;
  435. { classes and interfaces have implicit dereferencing }
  436. hasimplicitderef:=is_class_or_interface(p.resulttype.def);
  437. if hasimplicitderef then
  438. htype:=p.resulttype
  439. else
  440. htype.setdef(tpointerdef.create(p.resulttype));
  441. {$ifdef WITHNODEDEBUG}
  442. { we can't generate debuginfo for a withnode stored in a }
  443. { register }
  444. if (cs_debuginfo in aktmoduleswitches) then
  445. loadp:=ctempcreatenode.create(htype,sizeof(aint),tt_persistent,false)
  446. else
  447. {$endif WITHNODEDEBUG}
  448. loadp:=ctempcreatenode.create(htype,sizeof(aint),tt_persistent,true);
  449. resulttypepass(loadp);
  450. if hasimplicitderef then
  451. begin
  452. hp:=p;
  453. refp:=ctemprefnode.create(loadp);
  454. end
  455. else
  456. begin
  457. hp:=caddrnode.create_internal(p);
  458. refp:=cderefnode.create(ctemprefnode.create(loadp));
  459. end;
  460. addstatement(newstatement,loadp);
  461. addstatement(newstatement,cassignmentnode.create(
  462. ctemprefnode.create(loadp),
  463. hp));
  464. resulttypepass(refp);
  465. end;
  466. case p.resulttype.def.deftype of
  467. objectdef :
  468. begin
  469. obj:=tobjectdef(p.resulttype.def);
  470. withsymtable:=twithsymtable.Create(obj,obj.symtable.symsearch,refp);
  471. { include also all parent symtables }
  472. levelcount:=1;
  473. obj:=obj.childof;
  474. symtab:=withsymtable;
  475. while assigned(obj) do
  476. begin
  477. { keep the original tobjectdef as owner, because that is used for
  478. visibility of the symtable }
  479. symtab.next:=twithsymtable.create(tobjectdef(p.resulttype.def),obj.symtable.symsearch,refp.getcopy);
  480. symtab:=symtab.next;
  481. obj:=obj.childof;
  482. inc(levelcount);
  483. end;
  484. symtab.next:=symtablestack;
  485. symtablestack:=withsymtable;
  486. end;
  487. recorddef :
  488. begin
  489. symtab:=trecorddef(p.resulttype.def).symtable;
  490. levelcount:=1;
  491. withsymtable:=twithsymtable.create(trecorddef(p.resulttype.def),symtab.symsearch,refp);
  492. withsymtable.next:=symtablestack;
  493. symtablestack:=withsymtable;
  494. end;
  495. end;
  496. if try_to_consume(_COMMA) then
  497. right:=_with_statement()
  498. else
  499. begin
  500. consume(_DO);
  501. if token<>_SEMICOLON then
  502. right:=statement
  503. else
  504. right:=cerrornode.create;
  505. end;
  506. { remove symtables from the stack }
  507. for i:=1 to levelcount do
  508. symtablestack:=symtablestack.next;
  509. p:=cwithnode.create(right,twithsymtable(withsymtable),levelcount,refp);
  510. { Finalize complex withnode with destroy of temp }
  511. if assigned(newblock) then
  512. begin
  513. addstatement(newstatement,p);
  514. addstatement(newstatement,ctempdeletenode.create(loadp));
  515. if assigned(calltempp) then
  516. addstatement(newstatement,ctempdeletenode.create(calltempp));
  517. p:=newblock;
  518. end;
  519. _with_statement:=p;
  520. end
  521. else
  522. begin
  523. p.free;
  524. Message(parser_e_false_with_expr);
  525. { try to recover from error }
  526. if try_to_consume(_COMMA) then
  527. begin
  528. hp:=_with_statement();
  529. if (hp=nil) then; { remove warning about unused }
  530. end
  531. else
  532. begin
  533. consume(_DO);
  534. { ignore all }
  535. if token<>_SEMICOLON then
  536. statement;
  537. end;
  538. _with_statement:=nil;
  539. end;
  540. end;
  541. function with_statement : tnode;
  542. begin
  543. consume(_WITH);
  544. with_statement:=_with_statement();
  545. end;
  546. function raise_statement : tnode;
  547. var
  548. p,pobj,paddr,pframe : tnode;
  549. begin
  550. pobj:=nil;
  551. paddr:=nil;
  552. pframe:=nil;
  553. consume(_RAISE);
  554. if not(token in endtokens) then
  555. begin
  556. { object }
  557. pobj:=comp_expr(true);
  558. if try_to_consume(_AT) then
  559. begin
  560. paddr:=comp_expr(true);
  561. if try_to_consume(_COMMA) then
  562. pframe:=comp_expr(true);
  563. end;
  564. end
  565. else
  566. begin
  567. if (block_type<>bt_except) then
  568. Message(parser_e_no_reraise_possible);
  569. end;
  570. p:=craisenode.create(pobj,paddr,pframe);
  571. raise_statement:=p;
  572. end;
  573. function try_statement : tnode;
  574. var
  575. p_try_block,p_finally_block,first,last,
  576. p_default,p_specific,hp : tnode;
  577. ot : ttype;
  578. sym : tlocalvarsym;
  579. old_block_type : tblock_type;
  580. exceptsymtable : tsymtable;
  581. objname,objrealname : stringid;
  582. srsym : tsym;
  583. srsymtable : tsymtable;
  584. oldaktexceptblock: integer;
  585. begin
  586. include(current_procinfo.flags,pi_uses_exceptions);
  587. p_default:=nil;
  588. p_specific:=nil;
  589. { read statements to try }
  590. consume(_TRY);
  591. first:=nil;
  592. inc(exceptblockcounter);
  593. oldaktexceptblock := aktexceptblock;
  594. aktexceptblock := exceptblockcounter;
  595. while (token<>_FINALLY) and (token<>_EXCEPT) do
  596. begin
  597. if first=nil then
  598. begin
  599. last:=cstatementnode.create(statement,nil);
  600. first:=last;
  601. end
  602. else
  603. begin
  604. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  605. last:=tstatementnode(last).right;
  606. end;
  607. if not try_to_consume(_SEMICOLON) then
  608. break;
  609. consume_emptystats;
  610. end;
  611. p_try_block:=cblocknode.create(first);
  612. if try_to_consume(_FINALLY) then
  613. begin
  614. inc(exceptblockcounter);
  615. aktexceptblock := exceptblockcounter;
  616. p_finally_block:=statements_til_end;
  617. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  618. end
  619. else
  620. begin
  621. consume(_EXCEPT);
  622. old_block_type:=block_type;
  623. block_type:=bt_except;
  624. inc(exceptblockcounter);
  625. aktexceptblock := exceptblockcounter;
  626. ot:=generrortype;
  627. p_specific:=nil;
  628. if (idtoken=_ON) then
  629. { catch specific exceptions }
  630. begin
  631. repeat
  632. consume(_ON);
  633. if token=_ID then
  634. begin
  635. objname:=pattern;
  636. objrealname:=orgpattern;
  637. { can't use consume_sym here, because we need already
  638. to check for the colon }
  639. searchsym(objname,srsym,srsymtable);
  640. consume(_ID);
  641. { is a explicit name for the exception given ? }
  642. if try_to_consume(_COLON) then
  643. begin
  644. consume_sym(srsym,srsymtable);
  645. if (srsym.typ=typesym) and
  646. is_class(ttypesym(srsym).restype.def) then
  647. begin
  648. ot:=ttypesym(srsym).restype;
  649. sym:=tlocalvarsym.create(objrealname,vs_value,ot,[]);
  650. end
  651. else
  652. begin
  653. sym:=tlocalvarsym.create(objrealname,vs_value,generrortype,[]);
  654. if (srsym.typ=typesym) then
  655. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename)
  656. else
  657. Message1(type_e_class_type_expected,ot.def.typename);
  658. end;
  659. exceptsymtable:=tstt_exceptsymtable.create;
  660. exceptsymtable.insert(sym);
  661. { insert the exception symtable stack }
  662. exceptsymtable.next:=symtablestack;
  663. symtablestack:=exceptsymtable;
  664. end
  665. else
  666. begin
  667. { check if type is valid, must be done here because
  668. with "e: Exception" the e is not necessary }
  669. if srsym=nil then
  670. begin
  671. identifier_not_found(objrealname);
  672. srsym:=generrorsym;
  673. end;
  674. { support unit.identifier }
  675. if srsym.typ=unitsym then
  676. begin
  677. consume(_POINT);
  678. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,pattern);
  679. if srsym=nil then
  680. begin
  681. identifier_not_found(orgpattern);
  682. srsym:=generrorsym;
  683. end;
  684. consume(_ID);
  685. end;
  686. { check if type is valid, must be done here because
  687. with "e: Exception" the e is not necessary }
  688. if (srsym.typ=typesym) and
  689. is_class(ttypesym(srsym).restype.def) then
  690. ot:=ttypesym(srsym).restype
  691. else
  692. begin
  693. ot:=generrortype;
  694. if (srsym.typ=typesym) then
  695. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename)
  696. else
  697. Message1(type_e_class_type_expected,ot.def.typename);
  698. end;
  699. exceptsymtable:=nil;
  700. end;
  701. end
  702. else
  703. consume(_ID);
  704. consume(_DO);
  705. hp:=connode.create(nil,statement);
  706. if ot.def.deftype=errordef then
  707. begin
  708. hp.free;
  709. hp:=cerrornode.create;
  710. end;
  711. if p_specific=nil then
  712. begin
  713. last:=hp;
  714. p_specific:=last;
  715. end
  716. else
  717. begin
  718. tonnode(last).left:=hp;
  719. last:=tonnode(last).left;
  720. end;
  721. { set the informations }
  722. { only if the creation of the onnode was succesful, it's possible }
  723. { that last and hp are errornodes (JM) }
  724. if last.nodetype = onn then
  725. begin
  726. tonnode(last).excepttype:=tobjectdef(ot.def);
  727. tonnode(last).exceptsymtable:=exceptsymtable;
  728. end;
  729. { remove exception symtable }
  730. if assigned(exceptsymtable) then
  731. begin
  732. symtablestack:=symtablestack.next;
  733. if last.nodetype <> onn then
  734. exceptsymtable.free;
  735. end;
  736. if not try_to_consume(_SEMICOLON) then
  737. break;
  738. consume_emptystats;
  739. until (token in [_END,_ELSE]);
  740. if try_to_consume(_ELSE) then
  741. begin
  742. { catch the other exceptions }
  743. p_default:=statements_til_end;
  744. end
  745. else
  746. consume(_END);
  747. end
  748. else
  749. begin
  750. { catch all exceptions }
  751. p_default:=statements_til_end;
  752. end;
  753. block_type:=old_block_type;
  754. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  755. end;
  756. aktexceptblock := oldaktexceptblock;
  757. end;
  758. function _asm_statement : tnode;
  759. var
  760. asmstat : tasmnode;
  761. Marker : tai;
  762. reg : tregister;
  763. asmreader : tbaseasmreader;
  764. begin
  765. Inside_asm_statement:=true;
  766. if assigned(asmmodeinfos[aktasmmode]) then
  767. begin
  768. asmreader:=asmmodeinfos[aktasmmode]^.casmreader.create;
  769. asmstat:=casmnode.create(asmreader.assemble as taasmoutput);
  770. asmreader.free;
  771. end
  772. else
  773. Message(parser_f_assembler_reader_not_supported);
  774. { Mark procedure that it has assembler blocks }
  775. include(current_procinfo.flags,pi_has_assembler_block);
  776. { Read first the _ASM statement }
  777. consume(_ASM);
  778. { END is read, got a list of changed registers? }
  779. if try_to_consume(_LECKKLAMMER) then
  780. begin
  781. asmstat.used_regs_fpu:=[0..first_fpu_imreg-1];
  782. if token<>_RECKKLAMMER then
  783. begin
  784. repeat
  785. { it's possible to specify the modified registers }
  786. reg:=std_regnum_search(lower(pattern));
  787. if reg<>NR_NO then
  788. begin
  789. if getregtype(reg)=R_INTREGISTER then
  790. include(asmstat.used_regs_int,getsupreg(reg));
  791. end
  792. else
  793. Message(asmr_e_invalid_register);
  794. consume(_CSTRING);
  795. if not try_to_consume(_COMMA) then
  796. break;
  797. until false;
  798. end;
  799. consume(_RECKKLAMMER);
  800. end
  801. else
  802. begin
  803. asmstat.used_regs_int:=paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  804. asmstat.used_regs_fpu:=paramanager.get_volatile_registers_fpu(current_procinfo.procdef.proccalloption);
  805. end;
  806. { mark the start and the end of the assembler block
  807. this is needed for the optimizer }
  808. If Assigned(AsmStat.p_asm) Then
  809. Begin
  810. Marker := Tai_Marker.Create(AsmBlockStart);
  811. AsmStat.p_asm.Insert(Marker);
  812. Marker := Tai_Marker.Create(AsmBlockEnd);
  813. AsmStat.p_asm.Concat(Marker);
  814. End;
  815. Inside_asm_statement:=false;
  816. _asm_statement:=asmstat;
  817. end;
  818. function statement : tnode;
  819. var
  820. p : tnode;
  821. code : tnode;
  822. filepos : tfileposinfo;
  823. srsym : tsym;
  824. srsymtable : tsymtable;
  825. s : stringid;
  826. begin
  827. filepos:=akttokenpos;
  828. case token of
  829. _GOTO :
  830. begin
  831. if not(cs_support_goto in aktmoduleswitches)then
  832. Message(sym_e_goto_and_label_not_supported);
  833. consume(_GOTO);
  834. if (token<>_INTCONST) and (token<>_ID) then
  835. begin
  836. Message(sym_e_label_not_found);
  837. code:=cerrornode.create;
  838. end
  839. else
  840. begin
  841. if token=_ID then
  842. consume_sym(srsym,srsymtable)
  843. else
  844. begin
  845. searchsym(pattern,srsym,srsymtable);
  846. if srsym=nil then
  847. begin
  848. identifier_not_found(pattern);
  849. srsym:=generrorsym;
  850. srsymtable:=nil;
  851. end;
  852. consume(token);
  853. end;
  854. if srsym.typ<>labelsym then
  855. begin
  856. Message(sym_e_id_is_no_label_id);
  857. code:=cerrornode.create;
  858. end
  859. else
  860. begin
  861. { goto is only allowed to labels within the current scope }
  862. if srsym.owner<>current_procinfo.procdef.localst then
  863. CGMessage(parser_e_goto_outside_proc);
  864. code:=cgotonode.create_sym(tlabelsym(srsym));
  865. tgotonode(code).labelsym:=tlabelsym(srsym);
  866. { set flag that this label is used }
  867. tlabelsym(srsym).used:=true;
  868. end;
  869. end;
  870. end;
  871. _BEGIN :
  872. code:=statement_block(_BEGIN);
  873. _IF :
  874. code:=if_statement;
  875. _CASE :
  876. code:=case_statement;
  877. _REPEAT :
  878. code:=repeat_statement;
  879. _WHILE :
  880. code:=while_statement;
  881. _FOR :
  882. code:=for_statement;
  883. _WITH :
  884. code:=with_statement;
  885. _TRY :
  886. code:=try_statement;
  887. _RAISE :
  888. code:=raise_statement;
  889. { semicolons,else until and end are ignored }
  890. _SEMICOLON,
  891. _ELSE,
  892. _UNTIL,
  893. _END:
  894. code:=cnothingnode.create;
  895. _FAIL :
  896. begin
  897. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  898. Message(parser_e_fail_only_in_constructor);
  899. consume(_FAIL);
  900. code:=call_fail_node;
  901. end;
  902. _ASM :
  903. code:=_asm_statement;
  904. _EOF :
  905. Message(scan_f_end_of_file);
  906. else
  907. begin
  908. p:=expr;
  909. { save the pattern here for latter usage, the label could be "000",
  910. even if we read an expression, the pattern is still valid if it's really
  911. a label (FK)
  912. if you want to mess here, take care of
  913. tests/webtbs/tw3546.pp
  914. }
  915. s:=pattern;
  916. { When a colon follows a intconst then transform it into a label }
  917. if (p.nodetype=ordconstn) and
  918. try_to_consume(_COLON) then
  919. begin
  920. p.free;
  921. searchsym(s,srsym,srsymtable);
  922. if assigned(srsym) and
  923. (srsym.typ=labelsym) then
  924. begin
  925. if tlabelsym(srsym).defined then
  926. Message(sym_e_label_already_defined);
  927. tlabelsym(srsym).defined:=true;
  928. p:=clabelnode.create(nil);
  929. tlabelsym(srsym).code:=p;
  930. end
  931. else
  932. begin
  933. Message1(sym_e_label_used_and_not_defined,s);
  934. p:=cnothingnode.create;
  935. end;
  936. end;
  937. if p.nodetype=labeln then
  938. begin
  939. { the pointer to the following instruction }
  940. { isn't a very clean way }
  941. if token in endtokens then
  942. tlabelnode(p).left:=cnothingnode.create
  943. else
  944. tlabelnode(p).left:=statement();
  945. { be sure to have left also resulttypepass }
  946. resulttypepass(tlabelnode(p).left);
  947. end
  948. else
  949. { change a load of a procvar to a call. this is also
  950. supported in fpc mode }
  951. if p.nodetype in [vecn,derefn,typeconvn,subscriptn,loadn] then
  952. maybe_call_procvar(p,false);
  953. { blockn support because a read/write is changed into a blocknode }
  954. { with a separate statement for each read/write operation (JM) }
  955. { the same is true for val() if the third parameter is not 32 bit }
  956. if not(p.nodetype in [nothingn,calln,ifn,assignn,breakn,inlinen,
  957. continuen,labeln,blockn,exitn]) then
  958. Message(parser_e_illegal_expression);
  959. { Specify that we don't use the value returned by the call.
  960. This is used for :
  961. - dispose of temp stack space
  962. - dispose on FPU stack }
  963. if (p.nodetype=calln) then
  964. exclude(tcallnode(p).callnodeflags,cnf_return_value_used);
  965. code:=p;
  966. end;
  967. end;
  968. if assigned(code) then
  969. begin
  970. resulttypepass(code);
  971. code.fileinfo:=filepos;
  972. end;
  973. statement:=code;
  974. end;
  975. function statement_block(starttoken : ttoken) : tnode;
  976. var
  977. first,last : tnode;
  978. filepos : tfileposinfo;
  979. begin
  980. first:=nil;
  981. filepos:=akttokenpos;
  982. consume(starttoken);
  983. while not(token in [_END,_FINALIZATION]) do
  984. begin
  985. if first=nil then
  986. begin
  987. last:=cstatementnode.create(statement,nil);
  988. first:=last;
  989. end
  990. else
  991. begin
  992. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  993. last:=tstatementnode(last).right;
  994. end;
  995. if (token in [_END,_FINALIZATION]) then
  996. break
  997. else
  998. begin
  999. { if no semicolon, then error and go on }
  1000. if token<>_SEMICOLON then
  1001. begin
  1002. consume(_SEMICOLON);
  1003. consume_all_until(_SEMICOLON);
  1004. end;
  1005. consume(_SEMICOLON);
  1006. end;
  1007. consume_emptystats;
  1008. end;
  1009. { don't consume the finalization token, it is consumed when
  1010. reading the finalization block, but allow it only after
  1011. an initalization ! }
  1012. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1013. consume(_END);
  1014. last:=cblocknode.create(first);
  1015. last.fileinfo:=filepos;
  1016. statement_block:=last;
  1017. end;
  1018. function assembler_block : tnode;
  1019. var
  1020. p : tnode;
  1021. locals : longint;
  1022. begin
  1023. { Rename the funcret so that recursive calls are possible }
  1024. if not is_void(current_procinfo.procdef.rettype.def) then
  1025. symtablestack.rename(current_procinfo.procdef.resultname,'$hiddenresult');
  1026. { delphi uses register calling for assembler methods }
  1027. if (m_delphi in aktmodeswitches) and
  1028. (po_assembler in current_procinfo.procdef.procoptions) and
  1029. not(po_hascallingconvention in current_procinfo.procdef.procoptions) then
  1030. current_procinfo.procdef.proccalloption:=pocall_register;
  1031. { force the asm statement }
  1032. if token<>_ASM then
  1033. consume(_ASM);
  1034. include(current_procinfo.flags,pi_is_assembler);
  1035. p:=_asm_statement;
  1036. {$ifndef sparc}
  1037. {$ifndef arm}
  1038. if (po_assembler in current_procinfo.procdef.procoptions) then
  1039. begin
  1040. { set the framepointer to esp for assembler functions when the
  1041. following conditions are met:
  1042. - if the are no local variables and parameters (except the allocated result)
  1043. - no reference to the result variable (refcount<=1)
  1044. - result is not stored as parameter
  1045. - target processor has optional frame pointer save
  1046. (vm, i386, vm only currently)
  1047. }
  1048. locals:=0;
  1049. current_procinfo.procdef.localst.foreach_static(@count_locals,@locals);
  1050. current_procinfo.procdef.parast.foreach_static(@count_locals,@locals);
  1051. if (locals=0) and
  1052. (current_procinfo.procdef.owner.symtabletype<>objectsymtable) and
  1053. (not assigned(current_procinfo.procdef.funcretsym) or
  1054. (tabstractvarsym(current_procinfo.procdef.funcretsym).refcount<=1)) and
  1055. not(paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption)) then
  1056. begin
  1057. { Only need to set the framepointer, the locals will
  1058. be inserted with the correct reference in tcgasmnode.pass_2 }
  1059. current_procinfo.framepointer:=NR_STACK_POINTER_REG;
  1060. end;
  1061. end;
  1062. {$endif arm}
  1063. {$endif sparc}
  1064. { Flag the result as assigned when it is returned in a
  1065. register.
  1066. }
  1067. if assigned(current_procinfo.procdef.funcretsym) and
  1068. (not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption)) then
  1069. tabstractvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_initialised;
  1070. { because the END is already read we need to get the
  1071. last_endtoken_filepos here (PFV) }
  1072. last_endtoken_filepos:=akttokenpos;
  1073. assembler_block:=p;
  1074. end;
  1075. end.