2
0

pstatmnt.pas 43 KB

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