pstatmnt.pas 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.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,
  30. { global }
  31. globtype,globals,verbose,
  32. systems,
  33. { aasm }
  34. cpubase,aasmbase,aasmtai,
  35. { symtable }
  36. symconst,symbase,symtype,symdef,symsym,symtable,defutil,defcmp,
  37. paramgr,symutil,
  38. { pass 1 }
  39. pass_1,htypechk,
  40. nutils,nbas,nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  41. { parser }
  42. scanner,
  43. pbase,pexpr,
  44. { codegen }
  45. procinfo,cgbase,
  46. { assembler reader }
  47. rabase
  48. ;
  49. function statement : tnode;forward;
  50. function if_statement : tnode;
  51. var
  52. ex,if_a,else_a : tnode;
  53. begin
  54. consume(_IF);
  55. ex:=comp_expr(true);
  56. consume(_THEN);
  57. if token<>_ELSE then
  58. if_a:=statement
  59. else
  60. if_a:=nil;
  61. if try_to_consume(_ELSE) then
  62. else_a:=statement
  63. else
  64. else_a:=nil;
  65. result:=cifnode.create(ex,if_a,else_a);
  66. end;
  67. { creates a block (list) of statements, til the next END token }
  68. function statements_til_end : tnode;
  69. var
  70. first,last : tstatementnode;
  71. begin
  72. first:=nil;
  73. while token<>_END do
  74. begin
  75. if first=nil then
  76. begin
  77. last:=cstatementnode.create(statement,nil);
  78. first:=last;
  79. end
  80. else
  81. begin
  82. last.right:=cstatementnode.create(statement,nil);
  83. last:=tstatementnode(last.right);
  84. end;
  85. if not try_to_consume(_SEMICOLON) then
  86. break;
  87. consume_emptystats;
  88. end;
  89. consume(_END);
  90. statements_til_end:=cblocknode.create(first);
  91. end;
  92. function case_statement : tnode;
  93. var
  94. casedef : tdef;
  95. caseexpr,p : tnode;
  96. blockid : longint;
  97. hl1,hl2 : TConstExprInt;
  98. casedeferror : boolean;
  99. casenode : tcasenode;
  100. begin
  101. consume(_CASE);
  102. caseexpr:=comp_expr(true);
  103. { determines result type }
  104. do_resulttypepass(caseexpr);
  105. set_varstate(caseexpr,vs_used,true);
  106. casedeferror:=false;
  107. casedef:=caseexpr.resulttype.def;
  108. if (not assigned(casedef)) or
  109. not(is_ordinal(casedef)) then
  110. begin
  111. CGMessage(type_e_ordinal_expr_expected);
  112. { create a correct tree }
  113. caseexpr.free;
  114. caseexpr:=cordconstnode.create(0,u32inttype,false);
  115. { set error flag so no rangechecks are done }
  116. casedeferror:=true;
  117. end;
  118. { Create casenode }
  119. casenode:=ccasenode.create(caseexpr);
  120. consume(_OF);
  121. { Parse all case blocks }
  122. blockid:=0;
  123. repeat
  124. { maybe an instruction has more case labels }
  125. repeat
  126. p:=expr;
  127. if is_widechar(casedef) then
  128. begin
  129. if (p.nodetype=rangen) then
  130. begin
  131. trangenode(p).left:=ctypeconvnode.create(trangenode(p).left,cwidechartype);
  132. trangenode(p).right:=ctypeconvnode.create(trangenode(p).right,cwidechartype);
  133. do_resulttypepass(trangenode(p).left);
  134. do_resulttypepass(trangenode(p).right);
  135. end
  136. else
  137. begin
  138. p:=ctypeconvnode.create(p,cwidechartype);
  139. do_resulttypepass(p);
  140. end;
  141. end;
  142. hl1:=0;
  143. hl2:=0;
  144. if (p.nodetype=rangen) then
  145. begin
  146. { type checking for case statements }
  147. if is_subequal(casedef, trangenode(p).left.resulttype.def) and
  148. is_subequal(casedef, trangenode(p).right.resulttype.def) then
  149. begin
  150. hl1:=get_ordinal_value(trangenode(p).left);
  151. hl2:=get_ordinal_value(trangenode(p).right);
  152. if hl1>hl2 then
  153. CGMessage(parser_e_case_lower_less_than_upper_bound);
  154. if not casedeferror then
  155. begin
  156. testrange(casedef,hl1,false);
  157. testrange(casedef,hl2,false);
  158. end;
  159. end
  160. else
  161. CGMessage(parser_e_case_mismatch);
  162. casenode.addlabel(blockid,hl1,hl2);
  163. end
  164. else
  165. begin
  166. { type checking for case statements }
  167. if not is_subequal(casedef, p.resulttype.def) then
  168. CGMessage(parser_e_case_mismatch);
  169. hl1:=get_ordinal_value(p);
  170. if not casedeferror then
  171. testrange(casedef,hl1,false);
  172. casenode.addlabel(blockid,hl1,hl1);
  173. end;
  174. p.free;
  175. if token=_COMMA then
  176. consume(_COMMA)
  177. else
  178. break;
  179. until false;
  180. consume(_COLON);
  181. { add instruction block }
  182. casenode.addblock(blockid,statement);
  183. { next block }
  184. inc(blockid);
  185. if not(token in [_ELSE,_OTHERWISE,_END]) then
  186. consume(_SEMICOLON);
  187. until (token in [_ELSE,_OTHERWISE,_END]);
  188. if (token in [_ELSE,_OTHERWISE]) then
  189. begin
  190. if not try_to_consume(_ELSE) then
  191. consume(_OTHERWISE);
  192. casenode.addelseblock(statements_til_end);
  193. end
  194. else
  195. consume(_END);
  196. result:=casenode;
  197. end;
  198. function repeat_statement : tnode;
  199. var
  200. first,last,p_e : tnode;
  201. begin
  202. consume(_REPEAT);
  203. first:=nil;
  204. while token<>_UNTIL do
  205. begin
  206. if first=nil then
  207. begin
  208. last:=cstatementnode.create(statement,nil);
  209. first:=last;
  210. end
  211. else
  212. begin
  213. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  214. last:=tstatementnode(last).right;
  215. end;
  216. if not try_to_consume(_SEMICOLON) then
  217. break;
  218. consume_emptystats;
  219. end;
  220. consume(_UNTIL);
  221. first:=cblocknode.create(first);
  222. p_e:=comp_expr(true);
  223. result:=cwhilerepeatnode.create(p_e,first,false,true);
  224. end;
  225. function while_statement : tnode;
  226. var
  227. p_e,p_a : tnode;
  228. begin
  229. consume(_WHILE);
  230. p_e:=comp_expr(true);
  231. consume(_DO);
  232. p_a:=statement;
  233. result:=cwhilerepeatnode.create(p_e,p_a,true,false);
  234. end;
  235. function for_statement : tnode;
  236. procedure check_range(hp:tnode);
  237. begin
  238. {$ifndef cpu64bit}
  239. if hp.nodetype=ordconstn then
  240. begin
  241. if (tordconstnode(hp).value<low(longint)) or
  242. (tordconstnode(hp).value>high(longint)) then
  243. begin
  244. CGMessage(parser_e_range_check_error);
  245. { recover, prevent more warnings/errors }
  246. tordconstnode(hp).value:=0;
  247. end;
  248. end;
  249. {$endif cpu64bit}
  250. end;
  251. var
  252. hp,
  253. hloopvar,
  254. hblock,
  255. hto,hfrom : tnode;
  256. backward : boolean;
  257. loopvarsym : tabstractvarsym;
  258. begin
  259. { parse loop header }
  260. consume(_FOR);
  261. hloopvar:=factor(false);
  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_used,true);
  362. resulttypepass(hto);
  363. set_varstate(hto,vs_used,true);
  364. resulttypepass(hloopvar);
  365. set_varstate(hloopvar,vs_used,false);
  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. set_varstate(p,vs_used,false);
  391. right:=nil;
  392. if (not codegenerror) and
  393. (p.resulttype.def.deftype in [objectdef,recorddef]) then
  394. begin
  395. newblock:=nil;
  396. { ignore nodes that don't add instructions in the tree }
  397. hp:=p;
  398. while { equal type conversions }
  399. (
  400. (hp.nodetype=typeconvn) and
  401. (ttypeconvnode(hp).convtype=tc_equal)
  402. ) or
  403. { constant array index }
  404. (
  405. (hp.nodetype=vecn) and
  406. (tvecnode(hp).right.nodetype=ordconstn)
  407. ) do
  408. hp:=tunarynode(hp).left;
  409. if (hp.nodetype=loadn) and
  410. (
  411. (tloadnode(hp).symtable=current_procinfo.procdef.localst) or
  412. (tloadnode(hp).symtable=current_procinfo.procdef.parast) or
  413. (tloadnode(hp).symtable.symtabletype in [staticsymtable,globalsymtable])
  414. ) then
  415. begin
  416. { simple load, we can reference direct }
  417. loadp:=nil;
  418. refp:=p;
  419. end
  420. else
  421. begin
  422. calltempp:=nil;
  423. { complex load, load in temp first }
  424. newblock:=internalstatements(newstatement);
  425. { when right is a call then load it first in a temp }
  426. if p.nodetype=calln then
  427. begin
  428. calltempp:=ctempcreatenode.create(p.resulttype,p.resulttype.def.size,tt_persistent,false);
  429. addstatement(newstatement,calltempp);
  430. addstatement(newstatement,cassignmentnode.create(
  431. ctemprefnode.create(calltempp),
  432. p));
  433. p:=ctemprefnode.create(calltempp);
  434. resulttypepass(p);
  435. end;
  436. { classes and interfaces have implicit dereferencing }
  437. hasimplicitderef:=is_class_or_interface(p.resulttype.def);
  438. if hasimplicitderef then
  439. htype:=p.resulttype
  440. else
  441. htype.setdef(tpointerdef.create(p.resulttype));
  442. {$ifdef WITHNODEDEBUG}
  443. { we can't generate debuginfo for a withnode stored in a }
  444. { register }
  445. if (cs_debuginfo in aktmoduleswitches) then
  446. loadp:=ctempcreatenode.create(htype,sizeof(aint),tt_persistent,false)
  447. else
  448. {$endif WITHNODEDEBUG}
  449. loadp:=ctempcreatenode.create(htype,sizeof(aint),tt_persistent,true);
  450. resulttypepass(loadp);
  451. if hasimplicitderef then
  452. begin
  453. hp:=p;
  454. refp:=ctemprefnode.create(loadp);
  455. end
  456. else
  457. begin
  458. hp:=caddrnode.create_internal(p);
  459. refp:=cderefnode.create(ctemprefnode.create(loadp));
  460. end;
  461. addstatement(newstatement,loadp);
  462. addstatement(newstatement,cassignmentnode.create(
  463. ctemprefnode.create(loadp),
  464. hp));
  465. resulttypepass(refp);
  466. end;
  467. case p.resulttype.def.deftype of
  468. objectdef :
  469. begin
  470. obj:=tobjectdef(p.resulttype.def);
  471. withsymtable:=twithsymtable.Create(obj,obj.symtable.symsearch,refp);
  472. { include also all parent symtables }
  473. levelcount:=1;
  474. obj:=obj.childof;
  475. symtab:=withsymtable;
  476. while assigned(obj) do
  477. begin
  478. { keep the original tobjectdef as owner, because that is used for
  479. visibility of the symtable }
  480. symtab.next:=twithsymtable.create(tobjectdef(p.resulttype.def),obj.symtable.symsearch,refp.getcopy);
  481. symtab:=symtab.next;
  482. obj:=obj.childof;
  483. inc(levelcount);
  484. end;
  485. symtab.next:=symtablestack;
  486. symtablestack:=withsymtable;
  487. end;
  488. recorddef :
  489. begin
  490. symtab:=trecorddef(p.resulttype.def).symtable;
  491. levelcount:=1;
  492. withsymtable:=twithsymtable.create(trecorddef(p.resulttype.def),symtab.symsearch,refp);
  493. withsymtable.next:=symtablestack;
  494. symtablestack:=withsymtable;
  495. end;
  496. end;
  497. if try_to_consume(_COMMA) then
  498. right:=_with_statement()
  499. else
  500. begin
  501. consume(_DO);
  502. if token<>_SEMICOLON then
  503. right:=statement
  504. else
  505. right:=cerrornode.create;
  506. end;
  507. { remove symtables from the stack }
  508. for i:=1 to levelcount do
  509. symtablestack:=symtablestack.next;
  510. p:=cwithnode.create(right,twithsymtable(withsymtable),levelcount,refp);
  511. { Finalize complex withnode with destroy of temp }
  512. if assigned(newblock) then
  513. begin
  514. addstatement(newstatement,p);
  515. addstatement(newstatement,ctempdeletenode.create(loadp));
  516. if assigned(calltempp) then
  517. addstatement(newstatement,ctempdeletenode.create(calltempp));
  518. p:=newblock;
  519. end;
  520. _with_statement:=p;
  521. end
  522. else
  523. begin
  524. p.free;
  525. Message(parser_e_false_with_expr);
  526. { try to recover from error }
  527. if try_to_consume(_COMMA) then
  528. begin
  529. hp:=_with_statement();
  530. if (hp=nil) then; { remove warning about unused }
  531. end
  532. else
  533. begin
  534. consume(_DO);
  535. { ignore all }
  536. if token<>_SEMICOLON then
  537. statement;
  538. end;
  539. _with_statement:=nil;
  540. end;
  541. end;
  542. function with_statement : tnode;
  543. begin
  544. consume(_WITH);
  545. with_statement:=_with_statement();
  546. end;
  547. function raise_statement : tnode;
  548. var
  549. p,pobj,paddr,pframe : tnode;
  550. begin
  551. pobj:=nil;
  552. paddr:=nil;
  553. pframe:=nil;
  554. consume(_RAISE);
  555. if not(token in endtokens) then
  556. begin
  557. { object }
  558. pobj:=comp_expr(true);
  559. if try_to_consume(_AT) then
  560. begin
  561. paddr:=comp_expr(true);
  562. if try_to_consume(_COMMA) then
  563. pframe:=comp_expr(true);
  564. end;
  565. end
  566. else
  567. begin
  568. if (block_type<>bt_except) then
  569. Message(parser_e_no_reraise_possible);
  570. end;
  571. p:=craisenode.create(pobj,paddr,pframe);
  572. raise_statement:=p;
  573. end;
  574. function try_statement : tnode;
  575. var
  576. p_try_block,p_finally_block,first,last,
  577. p_default,p_specific,hp : tnode;
  578. ot : ttype;
  579. sym : tlocalvarsym;
  580. old_block_type : tblock_type;
  581. exceptsymtable : tsymtable;
  582. objname,objrealname : stringid;
  583. srsym : tsym;
  584. srsymtable : tsymtable;
  585. oldaktexceptblock: integer;
  586. begin
  587. include(current_procinfo.flags,pi_uses_exceptions);
  588. p_default:=nil;
  589. p_specific:=nil;
  590. { read statements to try }
  591. consume(_TRY);
  592. first:=nil;
  593. inc(exceptblockcounter);
  594. oldaktexceptblock := aktexceptblock;
  595. aktexceptblock := exceptblockcounter;
  596. while (token<>_FINALLY) and (token<>_EXCEPT) do
  597. begin
  598. if first=nil then
  599. begin
  600. last:=cstatementnode.create(statement,nil);
  601. first:=last;
  602. end
  603. else
  604. begin
  605. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  606. last:=tstatementnode(last).right;
  607. end;
  608. if not try_to_consume(_SEMICOLON) then
  609. break;
  610. consume_emptystats;
  611. end;
  612. p_try_block:=cblocknode.create(first);
  613. if try_to_consume(_FINALLY) then
  614. begin
  615. inc(exceptblockcounter);
  616. aktexceptblock := exceptblockcounter;
  617. p_finally_block:=statements_til_end;
  618. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  619. end
  620. else
  621. begin
  622. consume(_EXCEPT);
  623. old_block_type:=block_type;
  624. block_type:=bt_except;
  625. inc(exceptblockcounter);
  626. aktexceptblock := exceptblockcounter;
  627. ot:=generrortype;
  628. p_specific:=nil;
  629. if (idtoken=_ON) then
  630. { catch specific exceptions }
  631. begin
  632. repeat
  633. consume(_ID);
  634. if token=_ID then
  635. begin
  636. objname:=pattern;
  637. objrealname:=orgpattern;
  638. { can't use consume_sym here, because we need already
  639. to check for the colon }
  640. searchsym(objname,srsym,srsymtable);
  641. consume(_ID);
  642. { is a explicit name for the exception given ? }
  643. if try_to_consume(_COLON) then
  644. begin
  645. consume_sym(srsym,srsymtable);
  646. if (srsym.typ=typesym) and
  647. is_class(ttypesym(srsym).restype.def) then
  648. begin
  649. ot:=ttypesym(srsym).restype;
  650. sym:=tlocalvarsym.create(objrealname,vs_value,ot,[]);
  651. end
  652. else
  653. begin
  654. sym:=tlocalvarsym.create(objrealname,vs_value,generrortype,[]);
  655. if (srsym.typ=typesym) then
  656. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename)
  657. else
  658. Message1(type_e_class_type_expected,ot.def.typename);
  659. end;
  660. exceptsymtable:=tstt_exceptsymtable.create;
  661. exceptsymtable.insert(sym);
  662. { insert the exception symtable stack }
  663. exceptsymtable.next:=symtablestack;
  664. symtablestack:=exceptsymtable;
  665. end
  666. else
  667. begin
  668. { check if type is valid, must be done here because
  669. with "e: Exception" the e is not necessary }
  670. if srsym=nil then
  671. begin
  672. identifier_not_found(objrealname);
  673. srsym:=generrorsym;
  674. end;
  675. { support unit.identifier }
  676. if srsym.typ=unitsym then
  677. begin
  678. consume(_POINT);
  679. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,pattern);
  680. if srsym=nil then
  681. begin
  682. identifier_not_found(orgpattern);
  683. srsym:=generrorsym;
  684. end;
  685. consume(_ID);
  686. end;
  687. { check if type is valid, must be done here because
  688. with "e: Exception" the e is not necessary }
  689. if (srsym.typ=typesym) and
  690. is_class(ttypesym(srsym).restype.def) then
  691. ot:=ttypesym(srsym).restype
  692. else
  693. begin
  694. ot:=generrortype;
  695. if (srsym.typ=typesym) then
  696. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename)
  697. else
  698. Message1(type_e_class_type_expected,ot.def.typename);
  699. end;
  700. exceptsymtable:=nil;
  701. end;
  702. end
  703. else
  704. consume(_ID);
  705. consume(_DO);
  706. hp:=connode.create(nil,statement);
  707. if ot.def.deftype=errordef then
  708. begin
  709. hp.free;
  710. hp:=cerrornode.create;
  711. end;
  712. if p_specific=nil then
  713. begin
  714. last:=hp;
  715. p_specific:=last;
  716. end
  717. else
  718. begin
  719. tonnode(last).left:=hp;
  720. last:=tonnode(last).left;
  721. end;
  722. { set the informations }
  723. { only if the creation of the onnode was succesful, it's possible }
  724. { that last and hp are errornodes (JM) }
  725. if last.nodetype = onn then
  726. begin
  727. tonnode(last).excepttype:=tobjectdef(ot.def);
  728. tonnode(last).exceptsymtable:=exceptsymtable;
  729. end;
  730. { remove exception symtable }
  731. if assigned(exceptsymtable) then
  732. begin
  733. symtablestack:=symtablestack.next;
  734. if last.nodetype <> onn then
  735. exceptsymtable.free;
  736. end;
  737. if not try_to_consume(_SEMICOLON) then
  738. break;
  739. consume_emptystats;
  740. until (token in [_END,_ELSE]);
  741. if try_to_consume(_ELSE) then
  742. begin
  743. { catch the other exceptions }
  744. p_default:=statements_til_end;
  745. end
  746. else
  747. consume(_END);
  748. end
  749. else
  750. begin
  751. { catch all exceptions }
  752. p_default:=statements_til_end;
  753. end;
  754. block_type:=old_block_type;
  755. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  756. end;
  757. aktexceptblock := oldaktexceptblock;
  758. end;
  759. function _asm_statement : tnode;
  760. var
  761. asmstat : tasmnode;
  762. Marker : tai;
  763. reg : tregister;
  764. asmreader : tbaseasmreader;
  765. begin
  766. Inside_asm_statement:=true;
  767. if assigned(asmmodeinfos[aktasmmode]) then
  768. begin
  769. asmreader:=asmmodeinfos[aktasmmode]^.casmreader.create;
  770. asmstat:=casmnode.create(asmreader.assemble as taasmoutput);
  771. asmreader.free;
  772. end
  773. else
  774. Message(parser_f_assembler_reader_not_supported);
  775. { Mark procedure that it has assembler blocks }
  776. include(current_procinfo.flags,pi_has_assembler_block);
  777. { Read first the _ASM statement }
  778. consume(_ASM);
  779. { END is read, got a list of changed registers? }
  780. if try_to_consume(_LECKKLAMMER) then
  781. begin
  782. asmstat.used_regs_fpu:=[0..first_fpu_imreg-1];
  783. if token<>_RECKKLAMMER then
  784. begin
  785. repeat
  786. { it's possible to specify the modified registers }
  787. reg:=std_regnum_search(lower(pattern));
  788. if reg<>NR_NO then
  789. begin
  790. if getregtype(reg)=R_INTREGISTER then
  791. include(asmstat.used_regs_int,getsupreg(reg));
  792. end
  793. else
  794. Message(asmr_e_invalid_register);
  795. consume(_CSTRING);
  796. if not try_to_consume(_COMMA) then
  797. break;
  798. until false;
  799. end;
  800. consume(_RECKKLAMMER);
  801. end
  802. else
  803. begin
  804. asmstat.used_regs_int:=paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  805. asmstat.used_regs_fpu:=paramanager.get_volatile_registers_fpu(current_procinfo.procdef.proccalloption);
  806. end;
  807. { mark the start and the end of the assembler block
  808. this is needed for the optimizer }
  809. If Assigned(AsmStat.p_asm) Then
  810. Begin
  811. Marker := Tai_Marker.Create(AsmBlockStart);
  812. AsmStat.p_asm.Insert(Marker);
  813. Marker := Tai_Marker.Create(AsmBlockEnd);
  814. AsmStat.p_asm.Concat(Marker);
  815. End;
  816. Inside_asm_statement:=false;
  817. _asm_statement:=asmstat;
  818. end;
  819. function statement : tnode;
  820. var
  821. p : tnode;
  822. code : tnode;
  823. filepos : tfileposinfo;
  824. srsym : tsym;
  825. srsymtable : tsymtable;
  826. s : stringid;
  827. begin
  828. filepos:=akttokenpos;
  829. case token of
  830. _GOTO :
  831. begin
  832. if not(cs_support_goto in aktmoduleswitches)then
  833. Message(sym_e_goto_and_label_not_supported);
  834. consume(_GOTO);
  835. if (token<>_INTCONST) and (token<>_ID) then
  836. begin
  837. Message(sym_e_label_not_found);
  838. code:=cerrornode.create;
  839. end
  840. else
  841. begin
  842. if token=_ID then
  843. consume_sym(srsym,srsymtable)
  844. else
  845. begin
  846. searchsym(pattern,srsym,srsymtable);
  847. if srsym=nil then
  848. begin
  849. identifier_not_found(pattern);
  850. srsym:=generrorsym;
  851. srsymtable:=nil;
  852. end;
  853. consume(token);
  854. end;
  855. if srsym.typ<>labelsym then
  856. begin
  857. Message(sym_e_id_is_no_label_id);
  858. code:=cerrornode.create;
  859. end
  860. else
  861. begin
  862. { goto is only allowed to labels within the current scope }
  863. if srsym.owner<>current_procinfo.procdef.localst then
  864. CGMessage(parser_e_goto_outside_proc);
  865. code:=cgotonode.create(tlabelsym(srsym));
  866. tgotonode(code).labsym:=tlabelsym(srsym);
  867. { set flag that this label is used }
  868. tlabelsym(srsym).used:=true;
  869. end;
  870. end;
  871. end;
  872. _BEGIN :
  873. code:=statement_block(_BEGIN);
  874. _IF :
  875. code:=if_statement;
  876. _CASE :
  877. code:=case_statement;
  878. _REPEAT :
  879. code:=repeat_statement;
  880. _WHILE :
  881. code:=while_statement;
  882. _FOR :
  883. code:=for_statement;
  884. _WITH :
  885. code:=with_statement;
  886. _TRY :
  887. code:=try_statement;
  888. _RAISE :
  889. code:=raise_statement;
  890. { semicolons,else until and end are ignored }
  891. _SEMICOLON,
  892. _ELSE,
  893. _UNTIL,
  894. _END:
  895. code:=cnothingnode.create;
  896. _FAIL :
  897. begin
  898. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  899. Message(parser_e_fail_only_in_constructor);
  900. consume(_FAIL);
  901. code:=call_fail_node;
  902. end;
  903. _ASM :
  904. code:=_asm_statement;
  905. _EOF :
  906. Message(scan_f_end_of_file);
  907. else
  908. begin
  909. p:=expr;
  910. { When a colon follows a intconst then transform it into a label }
  911. if (p.nodetype=ordconstn) and
  912. try_to_consume(_COLON) then
  913. begin
  914. s:=tostr(tordconstnode(p).value);
  915. p.free;
  916. searchsym(s,srsym,srsymtable);
  917. if assigned(srsym) and
  918. (srsym.typ=labelsym) then
  919. begin
  920. if tlabelsym(srsym).defined then
  921. Message(sym_e_label_already_defined);
  922. tlabelsym(srsym).defined:=true;
  923. p:=clabelnode.create(tlabelsym(srsym),nil);
  924. end
  925. else
  926. begin
  927. Message1(sym_e_label_used_and_not_defined,s);
  928. p:=cnothingnode.create;
  929. end;
  930. end;
  931. if p.nodetype=labeln then
  932. begin
  933. { the pointer to the following instruction }
  934. { isn't a very clean way }
  935. if token in endtokens then
  936. tlabelnode(p).left:=cnothingnode.create
  937. else
  938. tlabelnode(p).left:=statement();
  939. { be sure to have left also resulttypepass }
  940. resulttypepass(tlabelnode(p).left);
  941. end
  942. else
  943. { change a load of a procvar to a call. this is also
  944. supported in fpc mode }
  945. if p.nodetype in [vecn,derefn,typeconvn,subscriptn,loadn] then
  946. maybe_call_procvar(p,false);
  947. { blockn support because a read/write is changed into a blocknode }
  948. { with a separate statement for each read/write operation (JM) }
  949. { the same is true for val() if the third parameter is not 32 bit }
  950. if not(p.nodetype in [nothingn,calln,ifn,assignn,breakn,inlinen,
  951. continuen,labeln,blockn,exitn]) then
  952. Message(parser_e_illegal_expression);
  953. { Specify that we don't use the value returned by the call.
  954. This is used for :
  955. - dispose of temp stack space
  956. - dispose on FPU stack }
  957. if (p.nodetype=calln) then
  958. exclude(tcallnode(p).callnodeflags,cnf_return_value_used);
  959. code:=p;
  960. end;
  961. end;
  962. if assigned(code) then
  963. begin
  964. resulttypepass(code);
  965. code.fileinfo:=filepos;
  966. end;
  967. statement:=code;
  968. end;
  969. function statement_block(starttoken : ttoken) : tnode;
  970. var
  971. first,last : tnode;
  972. filepos : tfileposinfo;
  973. begin
  974. first:=nil;
  975. filepos:=akttokenpos;
  976. consume(starttoken);
  977. while not(token in [_END,_FINALIZATION]) do
  978. begin
  979. if first=nil then
  980. begin
  981. last:=cstatementnode.create(statement,nil);
  982. first:=last;
  983. end
  984. else
  985. begin
  986. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  987. last:=tstatementnode(last).right;
  988. end;
  989. if (token in [_END,_FINALIZATION]) then
  990. break
  991. else
  992. begin
  993. { if no semicolon, then error and go on }
  994. if token<>_SEMICOLON then
  995. begin
  996. consume(_SEMICOLON);
  997. consume_all_until(_SEMICOLON);
  998. end;
  999. consume(_SEMICOLON);
  1000. end;
  1001. consume_emptystats;
  1002. end;
  1003. { don't consume the finalization token, it is consumed when
  1004. reading the finalization block, but allow it only after
  1005. an initalization ! }
  1006. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1007. consume(_END);
  1008. last:=cblocknode.create(first);
  1009. last.fileinfo:=filepos;
  1010. statement_block:=last;
  1011. end;
  1012. function assembler_block : tnode;
  1013. var
  1014. p : tnode;
  1015. locals : longint;
  1016. begin
  1017. { Rename the funcret so that recursive calls are possible }
  1018. if not is_void(current_procinfo.procdef.rettype.def) then
  1019. symtablestack.rename(current_procinfo.procdef.resultname,'$hiddenresult');
  1020. { delphi uses register calling for assembler methods }
  1021. if (m_delphi in aktmodeswitches) and
  1022. (po_assembler in current_procinfo.procdef.procoptions) and
  1023. not(po_hascallingconvention in current_procinfo.procdef.procoptions) then
  1024. current_procinfo.procdef.proccalloption:=pocall_register;
  1025. { force the asm statement }
  1026. if token<>_ASM then
  1027. consume(_ASM);
  1028. include(current_procinfo.flags,pi_is_assembler);
  1029. p:=_asm_statement;
  1030. {$ifndef sparc}
  1031. {$ifndef arm}
  1032. if (po_assembler in current_procinfo.procdef.procoptions) then
  1033. begin
  1034. { set the framepointer to esp for assembler functions when the
  1035. following conditions are met:
  1036. - if the are no local variables and parameters (except the allocated result)
  1037. - no reference to the result variable (refcount<=1)
  1038. - result is not stored as parameter
  1039. - target processor has optional frame pointer save
  1040. (vm, i386, vm only currently)
  1041. }
  1042. locals:=0;
  1043. current_procinfo.procdef.localst.foreach_static(@count_locals,@locals);
  1044. current_procinfo.procdef.parast.foreach_static(@count_locals,@locals);
  1045. if (locals=0) and
  1046. (current_procinfo.procdef.owner.symtabletype<>objectsymtable) and
  1047. (not assigned(current_procinfo.procdef.funcretsym) or
  1048. (tabstractvarsym(current_procinfo.procdef.funcretsym).refcount<=1)) and
  1049. not(paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption)) then
  1050. begin
  1051. { Only need to set the framepointer, the locals will
  1052. be inserted with the correct reference in tcgasmnode.pass_2 }
  1053. current_procinfo.framepointer:=NR_STACK_POINTER_REG;
  1054. end;
  1055. end;
  1056. {$endif arm}
  1057. {$endif sparc}
  1058. { Flag the result as assigned when it is returned in a
  1059. register.
  1060. }
  1061. if assigned(current_procinfo.procdef.funcretsym) and
  1062. (not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption)) then
  1063. tabstractvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_assigned;
  1064. { because the END is already read we need to get the
  1065. last_endtoken_filepos here (PFV) }
  1066. last_endtoken_filepos:=akttokenpos;
  1067. assembler_block:=p;
  1068. end;
  1069. end.
  1070. {
  1071. $Log$
  1072. Revision 1.152 2005-02-03 17:10:58 peter
  1073. * check for-loop constants ranges
  1074. Revision 1.151 2005/01/31 20:23:53 peter
  1075. * set varstate before parsing the instruction block in for statements
  1076. Revision 1.150 2005/01/31 16:16:21 peter
  1077. * for-node cleanup, checking for uninitialzed from and to values
  1078. is now supported
  1079. Revision 1.149 2004/12/26 16:22:01 peter
  1080. * fix lineinfo for with blocks
  1081. Revision 1.148 2004/12/07 16:11:52 peter
  1082. * set vo_explicit_paraloc flag
  1083. Revision 1.147 2004/12/05 12:28:11 peter
  1084. * procvar handling for tp procvar mode fixed
  1085. * proc to procvar moved from addrnode to typeconvnode
  1086. * inlininginfo is now allocated only for inline routines that
  1087. can be inlined, introduced a new flag po_has_inlining_info
  1088. Revision 1.146 2004/11/30 18:13:39 jonas
  1089. * patch from Peter to fix inlining of case statements
  1090. Revision 1.145 2004/11/21 17:54:59 peter
  1091. * ttempcreatenode.create_reg merged into .create with parameter
  1092. whether a register is allowed
  1093. * funcret_paraloc renamed to funcretloc
  1094. Revision 1.144 2004/11/08 22:09:59 peter
  1095. * tvarsym splitted
  1096. Revision 1.143 2004/10/15 10:35:23 mazen
  1097. * remove non needed parathesys as in 1.140
  1098. Revision 1.141 2004/09/27 15:15:52 peter
  1099. * register loopvarsym for fields instead of record variable
  1100. * don't allow class fields as loop var
  1101. Revision 1.140 2004/09/26 17:45:30 peter
  1102. * simple regvar support, not yet finished
  1103. Revision 1.139 2004/09/21 17:25:12 peter
  1104. * paraloc branch merged
  1105. Revision 1.138 2004/09/21 16:00:50 peter
  1106. * no difference for withnode when debuginfo is generated
  1107. Revision 1.137 2004/09/13 20:28:27 peter
  1108. * for loop variable assignment is not allowed anymore
  1109. Revision 1.136.4.1 2004/09/21 16:01:54 peter
  1110. * withnode debug disabled
  1111. Revision 1.136 2004/06/20 08:55:30 florian
  1112. * logs truncated
  1113. Revision 1.135 2004/06/16 20:07:09 florian
  1114. * dwarf branch merged
  1115. Revision 1.134 2004/05/23 18:28:41 peter
  1116. * methodpointer is loaded into a temp when it was a calln
  1117. Revision 1.133 2004/05/23 11:39:38 peter
  1118. * give error when goto jumps to label outside current proc scope
  1119. Revision 1.132.2.2 2004/05/01 16:02:09 peter
  1120. * POINTER_SIZE replaced with sizeof(aint)
  1121. * aint,aword,tconst*int moved to globtype
  1122. Revision 1.132.2.1 2004/04/28 19:55:52 peter
  1123. * new warning for ordinal-pointer when size is different
  1124. * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
  1125. }