pstatmnt.pas 39 KB

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