pstatmnt.pas 42 KB

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