pstatmnt.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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,[vsf_must_be_valid]);
  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,[vsf_must_be_valid]);
  362. resulttypepass(hto);
  363. set_varstate(hto,vs_used,[vsf_must_be_valid]);
  364. resulttypepass(hloopvar);
  365. set_varstate(hloopvar,vs_used,[]);
  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(_ID);
  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(tlabelsym(srsym));
  865. tgotonode(code).labsym:=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(tlabelsym(srsym),nil);
  929. end
  930. else
  931. begin
  932. Message1(sym_e_label_used_and_not_defined,s);
  933. p:=cnothingnode.create;
  934. end;
  935. end;
  936. if p.nodetype=labeln then
  937. begin
  938. { the pointer to the following instruction }
  939. { isn't a very clean way }
  940. if token in endtokens then
  941. tlabelnode(p).left:=cnothingnode.create
  942. else
  943. tlabelnode(p).left:=statement();
  944. { be sure to have left also resulttypepass }
  945. resulttypepass(tlabelnode(p).left);
  946. end
  947. else
  948. { change a load of a procvar to a call. this is also
  949. supported in fpc mode }
  950. if p.nodetype in [vecn,derefn,typeconvn,subscriptn,loadn] then
  951. maybe_call_procvar(p,false);
  952. { blockn support because a read/write is changed into a blocknode }
  953. { with a separate statement for each read/write operation (JM) }
  954. { the same is true for val() if the third parameter is not 32 bit }
  955. if not(p.nodetype in [nothingn,calln,ifn,assignn,breakn,inlinen,
  956. continuen,labeln,blockn,exitn]) then
  957. Message(parser_e_illegal_expression);
  958. { Specify that we don't use the value returned by the call.
  959. This is used for :
  960. - dispose of temp stack space
  961. - dispose on FPU stack }
  962. if (p.nodetype=calln) then
  963. exclude(tcallnode(p).callnodeflags,cnf_return_value_used);
  964. code:=p;
  965. end;
  966. end;
  967. if assigned(code) then
  968. begin
  969. resulttypepass(code);
  970. code.fileinfo:=filepos;
  971. end;
  972. statement:=code;
  973. end;
  974. function statement_block(starttoken : ttoken) : tnode;
  975. var
  976. first,last : tnode;
  977. filepos : tfileposinfo;
  978. begin
  979. first:=nil;
  980. filepos:=akttokenpos;
  981. consume(starttoken);
  982. while not(token in [_END,_FINALIZATION]) do
  983. begin
  984. if first=nil then
  985. begin
  986. last:=cstatementnode.create(statement,nil);
  987. first:=last;
  988. end
  989. else
  990. begin
  991. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  992. last:=tstatementnode(last).right;
  993. end;
  994. if (token in [_END,_FINALIZATION]) then
  995. break
  996. else
  997. begin
  998. { if no semicolon, then error and go on }
  999. if token<>_SEMICOLON then
  1000. begin
  1001. consume(_SEMICOLON);
  1002. consume_all_until(_SEMICOLON);
  1003. end;
  1004. consume(_SEMICOLON);
  1005. end;
  1006. consume_emptystats;
  1007. end;
  1008. { don't consume the finalization token, it is consumed when
  1009. reading the finalization block, but allow it only after
  1010. an initalization ! }
  1011. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1012. consume(_END);
  1013. last:=cblocknode.create(first);
  1014. last.fileinfo:=filepos;
  1015. statement_block:=last;
  1016. end;
  1017. function assembler_block : tnode;
  1018. var
  1019. p : tnode;
  1020. locals : longint;
  1021. begin
  1022. { Rename the funcret so that recursive calls are possible }
  1023. if not is_void(current_procinfo.procdef.rettype.def) then
  1024. symtablestack.rename(current_procinfo.procdef.resultname,'$hiddenresult');
  1025. { delphi uses register calling for assembler methods }
  1026. if (m_delphi in aktmodeswitches) and
  1027. (po_assembler in current_procinfo.procdef.procoptions) and
  1028. not(po_hascallingconvention in current_procinfo.procdef.procoptions) then
  1029. current_procinfo.procdef.proccalloption:=pocall_register;
  1030. { force the asm statement }
  1031. if token<>_ASM then
  1032. consume(_ASM);
  1033. include(current_procinfo.flags,pi_is_assembler);
  1034. p:=_asm_statement;
  1035. {$ifndef sparc}
  1036. {$ifndef arm}
  1037. if (po_assembler in current_procinfo.procdef.procoptions) then
  1038. begin
  1039. { set the framepointer to esp for assembler functions when the
  1040. following conditions are met:
  1041. - if the are no local variables and parameters (except the allocated result)
  1042. - no reference to the result variable (refcount<=1)
  1043. - result is not stored as parameter
  1044. - target processor has optional frame pointer save
  1045. (vm, i386, vm only currently)
  1046. }
  1047. locals:=0;
  1048. current_procinfo.procdef.localst.foreach_static(@count_locals,@locals);
  1049. current_procinfo.procdef.parast.foreach_static(@count_locals,@locals);
  1050. if (locals=0) and
  1051. (current_procinfo.procdef.owner.symtabletype<>objectsymtable) and
  1052. (not assigned(current_procinfo.procdef.funcretsym) or
  1053. (tabstractvarsym(current_procinfo.procdef.funcretsym).refcount<=1)) and
  1054. not(paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption)) then
  1055. begin
  1056. { Only need to set the framepointer, the locals will
  1057. be inserted with the correct reference in tcgasmnode.pass_2 }
  1058. current_procinfo.framepointer:=NR_STACK_POINTER_REG;
  1059. end;
  1060. end;
  1061. {$endif arm}
  1062. {$endif sparc}
  1063. { Flag the result as assigned when it is returned in a
  1064. register.
  1065. }
  1066. if assigned(current_procinfo.procdef.funcretsym) and
  1067. (not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption)) then
  1068. tabstractvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_assigned;
  1069. { because the END is already read we need to get the
  1070. last_endtoken_filepos here (PFV) }
  1071. last_endtoken_filepos:=akttokenpos;
  1072. assembler_block:=p;
  1073. end;
  1074. end.
  1075. {
  1076. $Log$
  1077. Revision 1.156 2005-03-25 22:20:19 peter
  1078. * add hint when passing an uninitialized variable to a var parameter
  1079. Revision 1.155 2005/02/14 17:13:07 peter
  1080. * truncate log
  1081. Revision 1.154 2005/02/11 16:24:59 peter
  1082. * don't set varstate to vs_used for record in with at start yet
  1083. Revision 1.153 2005/02/08 21:25:14 florian
  1084. * fixed usage of 000 as label
  1085. Revision 1.152 2005/02/03 17:10:58 peter
  1086. * check for-loop constants ranges
  1087. Revision 1.151 2005/01/31 20:23:53 peter
  1088. * set varstate before parsing the instruction block in for statements
  1089. Revision 1.150 2005/01/31 16:16:21 peter
  1090. * for-node cleanup, checking for uninitialzed from and to values
  1091. is now supported
  1092. }