2
0

pstatmnt.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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,constexp,
  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,ngenutil,nbas,nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  40. { parser }
  41. scanner,
  42. pbase,ptype,pexpr,
  43. { codegen }
  44. procinfo,cgbase,
  45. { assembler reader }
  46. rabase,
  47. { wide- and unicodestrings}
  48. widestr
  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,false);
  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. result:=cifnode.create(ex,if_a,else_a);
  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. casedef : tdef;
  96. caseexpr,p : tnode;
  97. blockid : longint;
  98. hl1,hl2 : TConstExprInt;
  99. sl1,sl2 : tstringconstnode;
  100. casedeferror, caseofstring : boolean;
  101. casenode : tcasenode;
  102. begin
  103. consume(_CASE);
  104. caseexpr:=comp_expr(true,false);
  105. { determines result type }
  106. do_typecheckpass(caseexpr);
  107. { variants must be accepted, but first they must be converted to integer }
  108. if caseexpr.resultdef.typ=variantdef then
  109. begin
  110. caseexpr:=ctypeconvnode.create_internal(caseexpr,sinttype);
  111. do_typecheckpass(caseexpr);
  112. end;
  113. set_varstate(caseexpr,vs_read,[vsf_must_be_valid]);
  114. casedeferror:=false;
  115. casedef:=caseexpr.resultdef;
  116. { case of string must be rejected in delphi-, }
  117. { tp7/bp7-, mac-compatibility modes. }
  118. caseofstring :=
  119. ([m_delphi, m_mac, m_tp7] * current_settings.modeswitches = []) and
  120. is_string(casedef);
  121. if (not assigned(casedef)) or
  122. ( not(is_ordinal(casedef)) and (not caseofstring) ) then
  123. begin
  124. CGMessage(type_e_ordinal_or_string_expr_expected);
  125. { create a correct tree }
  126. caseexpr.free;
  127. caseexpr:=cordconstnode.create(0,u32inttype,false);
  128. { set error flag so no rangechecks are done }
  129. casedeferror:=true;
  130. end;
  131. { Create casenode }
  132. casenode:=ccasenode.create(caseexpr);
  133. consume(_OF);
  134. { Parse all case blocks }
  135. blockid:=0;
  136. repeat
  137. { maybe an instruction has more case labels }
  138. repeat
  139. p:=expr(true);
  140. if is_widechar(casedef) then
  141. begin
  142. if (p.nodetype=rangen) then
  143. begin
  144. trangenode(p).left:=ctypeconvnode.create(trangenode(p).left,cwidechartype);
  145. trangenode(p).right:=ctypeconvnode.create(trangenode(p).right,cwidechartype);
  146. do_typecheckpass(trangenode(p).left);
  147. do_typecheckpass(trangenode(p).right);
  148. end
  149. else
  150. begin
  151. p:=ctypeconvnode.create(p,cwidechartype);
  152. do_typecheckpass(p);
  153. end;
  154. end
  155. else
  156. begin
  157. if is_char(casedef) and is_widechar(p.resultdef) then
  158. begin
  159. if (p.nodetype=ordconstn) then
  160. begin
  161. p:=ctypeconvnode.create(p,cansichartype);
  162. do_typecheckpass(p);
  163. end
  164. else if (p.nodetype=rangen) then
  165. begin
  166. trangenode(p).left:=ctypeconvnode.create(trangenode(p).left,cansichartype);
  167. trangenode(p).right:=ctypeconvnode.create(trangenode(p).right,cansichartype);
  168. do_typecheckpass(trangenode(p).left);
  169. do_typecheckpass(trangenode(p).right);
  170. end;
  171. end;
  172. end;
  173. hl1:=0;
  174. hl2:=0;
  175. sl1:=nil;
  176. sl2:=nil;
  177. if (p.nodetype=rangen) then
  178. begin
  179. { type check for string case statements }
  180. if caseofstring and
  181. is_conststring_or_constcharnode(trangenode(p).left) and
  182. is_conststring_or_constcharnode(trangenode(p).right) then
  183. begin
  184. { we need stringconstnodes, even if expression contains single chars }
  185. sl1 := get_string_value(trangenode(p).left, tstringdef(casedef));
  186. sl2 := get_string_value(trangenode(p).right, tstringdef(casedef));
  187. if sl1.fullcompare(sl2) > 0 then
  188. CGMessage(parser_e_case_lower_less_than_upper_bound);
  189. end
  190. { type checking for ordinal case statements }
  191. else if (not caseofstring) and
  192. is_subequal(casedef, trangenode(p).left.resultdef) and
  193. is_subequal(casedef, trangenode(p).right.resultdef) then
  194. begin
  195. hl1:=get_ordinal_value(trangenode(p).left);
  196. hl2:=get_ordinal_value(trangenode(p).right);
  197. if hl1>hl2 then
  198. CGMessage(parser_e_case_lower_less_than_upper_bound);
  199. if not casedeferror then
  200. begin
  201. testrange(casedef,hl1,false,false);
  202. testrange(casedef,hl2,false,false);
  203. end;
  204. end
  205. else
  206. CGMessage(parser_e_case_mismatch);
  207. if caseofstring then
  208. casenode.addlabel(blockid,sl1,sl2)
  209. else
  210. casenode.addlabel(blockid,hl1,hl2);
  211. end
  212. else
  213. begin
  214. { type check for string case statements }
  215. if (caseofstring and (not is_conststring_or_constcharnode(p))) or
  216. { type checking for ordinal case statements }
  217. ((not caseofstring) and (not is_subequal(casedef, p.resultdef))) then
  218. CGMessage(parser_e_case_mismatch);
  219. if caseofstring then
  220. begin
  221. sl1:=get_string_value(p, tstringdef(casedef));
  222. casenode.addlabel(blockid,sl1,sl1);
  223. end
  224. else
  225. begin
  226. hl1:=get_ordinal_value(p);
  227. if not casedeferror then
  228. testrange(casedef,hl1,false,false);
  229. casenode.addlabel(blockid,hl1,hl1);
  230. end;
  231. end;
  232. p.free;
  233. sl1.free;
  234. sl2.free;
  235. if token=_COMMA then
  236. consume(_COMMA)
  237. else
  238. break;
  239. until false;
  240. consume(_COLON);
  241. { add instruction block }
  242. casenode.addblock(blockid,statement);
  243. { next block }
  244. inc(blockid);
  245. if not(token in [_ELSE,_OTHERWISE,_END]) then
  246. consume(_SEMICOLON);
  247. until (token in [_ELSE,_OTHERWISE,_END]);
  248. if (token in [_ELSE,_OTHERWISE]) then
  249. begin
  250. if not try_to_consume(_ELSE) then
  251. consume(_OTHERWISE);
  252. casenode.addelseblock(statements_til_end);
  253. end
  254. else
  255. consume(_END);
  256. result:=casenode;
  257. end;
  258. function repeat_statement : tnode;
  259. var
  260. first,last,p_e : tnode;
  261. begin
  262. consume(_REPEAT);
  263. first:=nil;
  264. while token<>_UNTIL do
  265. begin
  266. if first=nil then
  267. begin
  268. last:=cstatementnode.create(statement,nil);
  269. first:=last;
  270. end
  271. else
  272. begin
  273. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  274. last:=tstatementnode(last).right;
  275. end;
  276. if not try_to_consume(_SEMICOLON) then
  277. break;
  278. consume_emptystats;
  279. end;
  280. consume(_UNTIL);
  281. first:=cblocknode.create(first);
  282. p_e:=comp_expr(true,false);
  283. result:=cwhilerepeatnode.create(p_e,first,false,true);
  284. end;
  285. function while_statement : tnode;
  286. var
  287. p_e,p_a : tnode;
  288. begin
  289. consume(_WHILE);
  290. p_e:=comp_expr(true,false);
  291. consume(_DO);
  292. p_a:=statement;
  293. result:=cwhilerepeatnode.create(p_e,p_a,true,false);
  294. end;
  295. function for_statement : tnode;
  296. procedure check_range(hp:tnode; fordef: tdef);
  297. begin
  298. if (hp.nodetype=ordconstn) and
  299. (fordef.typ<>errordef) then
  300. testrange(fordef,tordconstnode(hp).value,false,true);
  301. end;
  302. function for_loop_create(hloopvar: tnode): tnode;
  303. var
  304. hp,
  305. hblock,
  306. hto,hfrom : tnode;
  307. backward : boolean;
  308. loopvarsym : tabstractvarsym;
  309. begin
  310. { Check loop variable }
  311. loopvarsym:=nil;
  312. { variable must be an ordinal, int64 is not allowed for 32bit targets }
  313. if not(is_ordinal(hloopvar.resultdef))
  314. {$ifndef cpu64bitaddr}
  315. or is_64bitint(hloopvar.resultdef)
  316. {$endif not cpu64bitaddr}
  317. then
  318. MessagePos(hloopvar.fileinfo,type_e_ordinal_expr_expected);
  319. hp:=hloopvar;
  320. while assigned(hp) and
  321. (
  322. { record/object fields and array elements are allowed }
  323. { in tp7 mode only }
  324. (
  325. (m_tp7 in current_settings.modeswitches) and
  326. (
  327. ((hp.nodetype=subscriptn) and
  328. ((tsubscriptnode(hp).left.resultdef.typ=recorddef) or
  329. is_object(tsubscriptnode(hp).left.resultdef))
  330. ) or
  331. { constant array index }
  332. (
  333. (hp.nodetype=vecn) and
  334. is_constintnode(tvecnode(hp).right)
  335. )
  336. )
  337. ) or
  338. { equal typeconversions }
  339. (
  340. (hp.nodetype=typeconvn) and
  341. (ttypeconvnode(hp).convtype=tc_equal)
  342. )
  343. ) do
  344. begin
  345. { Use the recordfield for loopvarsym }
  346. if not assigned(loopvarsym) and
  347. (hp.nodetype=subscriptn) then
  348. loopvarsym:=tsubscriptnode(hp).vs;
  349. hp:=tunarynode(hp).left;
  350. end;
  351. if assigned(hp) and
  352. (hp.nodetype=loadn) then
  353. begin
  354. case tloadnode(hp).symtableentry.typ of
  355. staticvarsym,
  356. localvarsym,
  357. paravarsym :
  358. begin
  359. { we need a simple loadn:
  360. 1. The load must be in a global symtable or
  361. in the same level as the para of the current proc.
  362. 2. value variables (no const,out or var)
  363. 3. No threadvar, readonly or typedconst
  364. }
  365. if (
  366. (tloadnode(hp).symtable.symtablelevel=main_program_level) or
  367. (tloadnode(hp).symtable.symtablelevel=current_procinfo.procdef.parast.symtablelevel)
  368. ) and
  369. (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_value) and
  370. ([vo_is_thread_var,vo_is_typed_const] * tabstractvarsym(tloadnode(hp).symtableentry).varoptions=[]) then
  371. begin
  372. { Assigning for-loop variable is only allowed in tp7 and macpas }
  373. if ([m_tp7,m_mac] * current_settings.modeswitches = []) then
  374. begin
  375. if not assigned(loopvarsym) then
  376. loopvarsym:=tabstractvarsym(tloadnode(hp).symtableentry);
  377. include(loopvarsym.varoptions,vo_is_loop_counter);
  378. end;
  379. end
  380. else
  381. begin
  382. { Typed const is allowed in tp7 }
  383. if not(m_tp7 in current_settings.modeswitches) or
  384. not(vo_is_typed_const in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  385. MessagePos(hp.fileinfo,type_e_illegal_count_var);
  386. end;
  387. end;
  388. else
  389. MessagePos(hp.fileinfo,type_e_illegal_count_var);
  390. end;
  391. end
  392. else
  393. MessagePos(hloopvar.fileinfo,type_e_illegal_count_var);
  394. hfrom:=comp_expr(true,false);
  395. if try_to_consume(_DOWNTO) then
  396. backward:=true
  397. else
  398. begin
  399. consume(_TO);
  400. backward:=false;
  401. end;
  402. hto:=comp_expr(true,false);
  403. consume(_DO);
  404. { Check if the constants fit in the range }
  405. check_range(hfrom,hloopvar.resultdef);
  406. check_range(hto,hloopvar.resultdef);
  407. { first set the varstate for from and to, so
  408. uses of loopvar in those expressions will also
  409. trigger a warning when it is not used yet. This
  410. needs to be done before the instruction block is
  411. parsed to have a valid hloopvar }
  412. typecheckpass(hfrom);
  413. set_varstate(hfrom,vs_read,[vsf_must_be_valid]);
  414. typecheckpass(hto);
  415. set_varstate(hto,vs_read,[vsf_must_be_valid]);
  416. typecheckpass(hloopvar);
  417. { in two steps, because vs_readwritten may turn on vsf_must_be_valid }
  418. { for some subnodes }
  419. set_varstate(hloopvar,vs_written,[]);
  420. set_varstate(hloopvar,vs_read,[vsf_must_be_valid]);
  421. { ... now the instruction block }
  422. hblock:=statement;
  423. { variable is not used for loop counter anymore }
  424. if assigned(loopvarsym) then
  425. exclude(loopvarsym.varoptions,vo_is_loop_counter);
  426. result:=cfornode.create(hloopvar,hfrom,hto,hblock,backward);
  427. end;
  428. function for_in_loop_create(hloopvar: tnode): tnode;
  429. var
  430. expr: tnode;
  431. begin
  432. expr:=comp_expr(true,false);
  433. consume(_DO);
  434. set_varstate(hloopvar,vs_written,[]);
  435. set_varstate(hloopvar,vs_read,[vsf_must_be_valid]);
  436. result:=create_for_in_loop(hloopvar,statement,expr);
  437. expr.free;
  438. end;
  439. var
  440. hloopvar: tnode;
  441. begin
  442. { parse loop header }
  443. consume(_FOR);
  444. hloopvar:=factor(false,false);
  445. valid_for_loopvar(hloopvar,true);
  446. if try_to_consume(_ASSIGNMENT) then
  447. result:=for_loop_create(hloopvar)
  448. else if try_to_consume(_IN) then
  449. result:=for_in_loop_create(hloopvar)
  450. else
  451. consume(_ASSIGNMENT); // fail
  452. end;
  453. function _with_statement : tnode;
  454. var
  455. p : tnode;
  456. i : longint;
  457. st : TSymtable;
  458. newblock : tblocknode;
  459. newstatement : tstatementnode;
  460. calltempnode,
  461. tempnode : ttempcreatenode;
  462. valuenode,
  463. hp,
  464. refnode : tnode;
  465. hdef : tdef;
  466. helperdef : tobjectdef;
  467. hasimplicitderef : boolean;
  468. withsymtablelist : TFPObjectList;
  469. procedure pushobjchild(withdef,obj:tobjectdef);
  470. var
  471. parenthelperdef : tobjectdef;
  472. begin
  473. if not assigned(obj) then
  474. exit;
  475. pushobjchild(withdef,obj.childof);
  476. { we need to look for helpers that were defined for the parent
  477. class as well }
  478. search_last_objectpascal_helper(obj,current_structdef,parenthelperdef);
  479. { push the symtables of the helper's parents in reverse order }
  480. if assigned(parenthelperdef) then
  481. pushobjchild(withdef,parenthelperdef.childof);
  482. { keep the original tobjectdef as owner, because that is used for
  483. visibility of the symtable }
  484. st:=twithsymtable.create(withdef,obj.symtable.SymList,refnode.getcopy);
  485. symtablestack.push(st);
  486. withsymtablelist.add(st);
  487. { push the symtable of the helper }
  488. if assigned(parenthelperdef) then
  489. begin
  490. st:=twithsymtable.create(withdef,parenthelperdef.symtable.SymList,refnode.getcopy);
  491. symtablestack.push(st);
  492. withsymtablelist.add(st);
  493. end;
  494. end;
  495. begin
  496. p:=comp_expr(true,false);
  497. do_typecheckpass(p);
  498. if (p.nodetype=vecn) and
  499. (nf_memseg in p.flags) then
  500. CGMessage(parser_e_no_with_for_variable_in_other_segments);
  501. { "with procvar" can never mean anything, so always try
  502. to call it in case it returns a record/object/... }
  503. maybe_call_procvar(p,false);
  504. if (p.resultdef.typ in [objectdef,recorddef,classrefdef]) or
  505. ((p.resultdef.typ=undefineddef) and (df_generic in current_procinfo.procdef.defoptions)) then
  506. begin
  507. newblock:=nil;
  508. valuenode:=nil;
  509. tempnode:=nil;
  510. { ignore nodes that don't add instructions in the tree }
  511. hp:=p;
  512. while { equal type conversions }
  513. (
  514. (hp.nodetype=typeconvn) and
  515. (ttypeconvnode(hp).convtype=tc_equal)
  516. ) or
  517. { constant array index }
  518. (
  519. (hp.nodetype=vecn) and
  520. (tvecnode(hp).right.nodetype=ordconstn)
  521. ) do
  522. hp:=tunarynode(hp).left;
  523. if (hp.nodetype=loadn) and
  524. (
  525. (tloadnode(hp).symtable=current_procinfo.procdef.localst) or
  526. (tloadnode(hp).symtable=current_procinfo.procdef.parast) or
  527. (tloadnode(hp).symtable.symtabletype in [staticsymtable,globalsymtable])
  528. ) and
  529. { MacPas objects are mapped to classes, and the MacPas compilers
  530. interpret with-statements with MacPas objects the same way
  531. as records (the object referenced by the with-statement
  532. must remain constant)
  533. }
  534. not(is_class(hp.resultdef) and
  535. (m_mac in current_settings.modeswitches)) then
  536. begin
  537. { simple load, we can reference direct }
  538. refnode:=p;
  539. end
  540. else
  541. begin
  542. calltempnode:=nil;
  543. { complex load, load in temp first }
  544. newblock:=internalstatements(newstatement);
  545. { when we can't take the address of p, load it in a temp }
  546. { since we may need its address later on }
  547. if not valid_for_addr(p,false) then
  548. begin
  549. calltempnode:=ctempcreatenode.create(p.resultdef,p.resultdef.size,tt_persistent,true);
  550. addstatement(newstatement,calltempnode);
  551. addstatement(newstatement,cassignmentnode.create(
  552. ctemprefnode.create(calltempnode),
  553. p));
  554. p:=ctemprefnode.create(calltempnode);
  555. typecheckpass(p);
  556. end;
  557. { several object types have implicit dereferencing }
  558. { is_implicit_pointer_object_type() returns true for records
  559. on the JVM target because they are implemented as classes
  560. there, but we definitely have to take their address here
  561. since otherwise a deep copy is made and changes are made to
  562. this copy rather than to the original one }
  563. hasimplicitderef:=
  564. (is_implicit_pointer_object_type(p.resultdef) or
  565. (p.resultdef.typ=classrefdef)) and
  566. not((target_info.system in systems_jvm) and
  567. ((p.resultdef.typ=recorddef) or
  568. is_object(p.resultdef)));
  569. if hasimplicitderef then
  570. hdef:=p.resultdef
  571. else
  572. hdef:=tpointerdef.create(p.resultdef);
  573. { load address of the value in a temp }
  574. tempnode:=ctempcreatenode.create_withnode(hdef,sizeof(pint),tt_persistent,true,p);
  575. typecheckpass(tnode(tempnode));
  576. valuenode:=p;
  577. refnode:=ctemprefnode.create(tempnode);
  578. fillchar(refnode.fileinfo,sizeof(tfileposinfo),0);
  579. { add address call for valuenode and deref for refnode if this
  580. is not done implicitly }
  581. if not hasimplicitderef then
  582. begin
  583. valuenode:=caddrnode.create_internal_nomark(valuenode);
  584. include(valuenode.flags,nf_typedaddr);
  585. refnode:=cderefnode.create(refnode);
  586. fillchar(refnode.fileinfo,sizeof(tfileposinfo),0);
  587. end;
  588. addstatement(newstatement,tempnode);
  589. addstatement(newstatement,cassignmentnode.create(
  590. ctemprefnode.create(tempnode),
  591. valuenode));
  592. typecheckpass(refnode);
  593. end;
  594. { Note: the symtable of the helper is pushed after the following
  595. "case", the symtables of the helper's parents are passed in
  596. the "case" branches }
  597. withsymtablelist:=TFPObjectList.create(true);
  598. case p.resultdef.typ of
  599. objectdef :
  600. begin
  601. { do we have a helper for this type? }
  602. search_last_objectpascal_helper(tabstractrecorddef(p.resultdef),current_structdef,helperdef);
  603. { push symtables of all parents in reverse order }
  604. pushobjchild(tobjectdef(p.resultdef),tobjectdef(p.resultdef).childof);
  605. { push symtables of all parents of the helper in reverse order }
  606. if assigned(helperdef) then
  607. pushobjchild(helperdef,helperdef.childof);
  608. { push object symtable }
  609. st:=twithsymtable.Create(tobjectdef(p.resultdef),tobjectdef(p.resultdef).symtable.SymList,refnode);
  610. symtablestack.push(st);
  611. withsymtablelist.add(st);
  612. end;
  613. classrefdef :
  614. begin
  615. { do we have a helper for this type? }
  616. search_last_objectpascal_helper(tobjectdef(tclassrefdef(p.resultdef).pointeddef),current_structdef,helperdef);
  617. { push symtables of all parents in reverse order }
  618. pushobjchild(tobjectdef(tclassrefdef(p.resultdef).pointeddef),tobjectdef(tclassrefdef(p.resultdef).pointeddef).childof);
  619. { push symtables of all parents of the helper in reverse order }
  620. if assigned(helperdef) then
  621. pushobjchild(helperdef,helperdef.childof);
  622. { push object symtable }
  623. st:=twithsymtable.Create(tobjectdef(tclassrefdef(p.resultdef).pointeddef),tobjectdef(tclassrefdef(p.resultdef).pointeddef).symtable.SymList,refnode);
  624. symtablestack.push(st);
  625. withsymtablelist.add(st);
  626. end;
  627. recorddef :
  628. begin
  629. { do we have a helper for this type? }
  630. search_last_objectpascal_helper(tabstractrecorddef(p.resultdef),current_structdef,helperdef);
  631. { push symtables of all parents of the helper in reverse order }
  632. if assigned(helperdef) then
  633. pushobjchild(helperdef,helperdef.childof);
  634. { push record symtable }
  635. st:=twithsymtable.create(trecorddef(p.resultdef),trecorddef(p.resultdef).symtable.SymList,refnode);
  636. symtablestack.push(st);
  637. withsymtablelist.add(st);
  638. end;
  639. undefineddef :
  640. begin
  641. if not(df_generic in current_procinfo.procdef.defoptions) then
  642. internalerror(2012122802);
  643. helperdef:=nil;
  644. { push record symtable }
  645. st:=twithsymtable.create(p.resultdef,nil,refnode);
  646. symtablestack.push(st);
  647. withsymtablelist.add(st);
  648. end;
  649. else
  650. internalerror(200601271);
  651. end;
  652. { push helper symtable }
  653. if assigned(helperdef) then
  654. begin
  655. st:=twithsymtable.Create(helperdef,helperdef.symtable.SymList,refnode.getcopy);
  656. symtablestack.push(st);
  657. withsymtablelist.add(st);
  658. end;
  659. if try_to_consume(_COMMA) then
  660. p:=_with_statement()
  661. else
  662. begin
  663. consume(_DO);
  664. if token<>_SEMICOLON then
  665. p:=statement
  666. else
  667. p:=cnothingnode.create;
  668. end;
  669. { remove symtables in reverse order from the stack }
  670. for i:=withsymtablelist.count-1 downto 0 do
  671. symtablestack.pop(TSymtable(withsymtablelist[i]));
  672. withsymtablelist.free;
  673. // p:=cwithnode.create(right,twithsymtable(withsymtable),levelcount,refnode);
  674. { Finalize complex withnode with destroy of temp }
  675. if assigned(newblock) then
  676. begin
  677. addstatement(newstatement,p);
  678. if assigned(tempnode) then
  679. addstatement(newstatement,ctempdeletenode.create(tempnode));
  680. if assigned(calltempnode) then
  681. addstatement(newstatement,ctempdeletenode.create(calltempnode));
  682. p:=newblock;
  683. end;
  684. result:=p;
  685. end
  686. else
  687. begin
  688. p.free;
  689. Message1(parser_e_false_with_expr,p.resultdef.GetTypeName);
  690. { try to recover from error }
  691. if try_to_consume(_COMMA) then
  692. begin
  693. hp:=_with_statement();
  694. if (hp=nil) then; { remove warning about unused }
  695. end
  696. else
  697. begin
  698. consume(_DO);
  699. { ignore all }
  700. if token<>_SEMICOLON then
  701. statement;
  702. end;
  703. result:=nil;
  704. end;
  705. end;
  706. function with_statement : tnode;
  707. begin
  708. consume(_WITH);
  709. with_statement:=_with_statement();
  710. end;
  711. function raise_statement : tnode;
  712. var
  713. p,pobj,paddr,pframe : tnode;
  714. begin
  715. pobj:=nil;
  716. paddr:=nil;
  717. pframe:=nil;
  718. consume(_RAISE);
  719. if not(token in endtokens) then
  720. begin
  721. { object }
  722. pobj:=comp_expr(true,false);
  723. if try_to_consume(_AT) then
  724. begin
  725. paddr:=comp_expr(true,false);
  726. if try_to_consume(_COMMA) then
  727. pframe:=comp_expr(true,false);
  728. end;
  729. end
  730. else
  731. begin
  732. if (block_type<>bt_except) then
  733. Message(parser_e_no_reraise_possible);
  734. end;
  735. p:=craisenode.create(pobj,paddr,pframe);
  736. raise_statement:=p;
  737. end;
  738. function try_statement : tnode;
  739. procedure check_type_valid(var def: tdef);
  740. begin
  741. if not (is_class(def) or is_javaclass(def) or
  742. { skip showing error message the second time }
  743. (def.typ=errordef)) then
  744. begin
  745. Message1(type_e_class_type_expected,def.typename);
  746. def:=generrordef;
  747. end;
  748. end;
  749. var
  750. p_try_block,p_finally_block,first,last,
  751. p_default,p_specific,hp : tnode;
  752. ot : tDef;
  753. sym : tlocalvarsym;
  754. old_block_type : tblock_type;
  755. excepTSymtable : TSymtable;
  756. objname,objrealname : TIDString;
  757. srsym : tsym;
  758. srsymtable : TSymtable;
  759. t:ttoken;
  760. unit_found:boolean;
  761. oldcurrent_exceptblock: integer;
  762. begin
  763. p_default:=nil;
  764. p_specific:=nil;
  765. { read statements to try }
  766. consume(_TRY);
  767. first:=nil;
  768. inc(exceptblockcounter);
  769. oldcurrent_exceptblock := current_exceptblock;
  770. current_exceptblock := exceptblockcounter;
  771. old_block_type := block_type;
  772. block_type := bt_body;
  773. while (token<>_FINALLY) and (token<>_EXCEPT) do
  774. begin
  775. if first=nil then
  776. begin
  777. last:=cstatementnode.create(statement,nil);
  778. first:=last;
  779. end
  780. else
  781. begin
  782. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  783. last:=tstatementnode(last).right;
  784. end;
  785. if not try_to_consume(_SEMICOLON) then
  786. break;
  787. consume_emptystats;
  788. end;
  789. p_try_block:=cblocknode.create(first);
  790. if try_to_consume(_FINALLY) then
  791. begin
  792. inc(exceptblockcounter);
  793. current_exceptblock := exceptblockcounter;
  794. p_finally_block:=statements_til_end;
  795. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  796. end
  797. else
  798. begin
  799. consume(_EXCEPT);
  800. block_type:=bt_except;
  801. inc(exceptblockcounter);
  802. current_exceptblock := exceptblockcounter;
  803. ot:=generrordef;
  804. p_specific:=nil;
  805. if (idtoken=_ON) then
  806. { catch specific exceptions }
  807. begin
  808. repeat
  809. consume(_ON);
  810. if token=_ID then
  811. begin
  812. objname:=pattern;
  813. objrealname:=orgpattern;
  814. { can't use consume_sym here, because we need already
  815. to check for the colon }
  816. searchsym(objname,srsym,srsymtable);
  817. consume(_ID);
  818. { is a explicit name for the exception given ? }
  819. if try_to_consume(_COLON) then
  820. begin
  821. single_type(ot,[]);
  822. check_type_valid(ot);
  823. sym:=tlocalvarsym.create(objrealname,vs_value,ot,[]);
  824. end
  825. else
  826. begin
  827. { check if type is valid, must be done here because
  828. with "e: Exception" the e is not necessary }
  829. { support unit.identifier }
  830. unit_found:=try_consume_unitsym(srsym,srsymtable,t,false);
  831. if srsym=nil then
  832. begin
  833. identifier_not_found(orgpattern);
  834. srsym:=generrorsym;
  835. end;
  836. if unit_found then
  837. consume(t);
  838. { check if type is valid, must be done here because
  839. with "e: Exception" the e is not necessary }
  840. if (srsym.typ=typesym) then
  841. begin
  842. ot:=ttypesym(srsym).typedef;
  843. parse_nested_types(ot,false,nil);
  844. check_type_valid(ot);
  845. end
  846. else
  847. begin
  848. Message(type_e_type_id_expected);
  849. ot:=generrordef;
  850. end;
  851. { create dummy symbol so we don't need a special
  852. case in ncgflw, and so that we always know the
  853. type }
  854. sym:=tlocalvarsym.create('$exceptsym',vs_value,ot,[]);
  855. end;
  856. excepTSymtable:=tstt_excepTSymtable.create;
  857. excepTSymtable.insert(sym);
  858. symtablestack.push(excepTSymtable);
  859. end
  860. else
  861. consume(_ID);
  862. consume(_DO);
  863. hp:=connode.create(nil,statement);
  864. if ot.typ=errordef then
  865. begin
  866. hp.free;
  867. hp:=cerrornode.create;
  868. end;
  869. if p_specific=nil then
  870. begin
  871. last:=hp;
  872. p_specific:=last;
  873. end
  874. else
  875. begin
  876. tonnode(last).left:=hp;
  877. last:=tonnode(last).left;
  878. end;
  879. { set the informations }
  880. { only if the creation of the onnode was succesful, it's possible }
  881. { that last and hp are errornodes (JM) }
  882. if last.nodetype = onn then
  883. begin
  884. tonnode(last).excepttype:=tobjectdef(ot);
  885. tonnode(last).excepTSymtable:=excepTSymtable;
  886. end;
  887. { remove exception symtable }
  888. if assigned(excepTSymtable) then
  889. begin
  890. symtablestack.pop(excepTSymtable);
  891. if last.nodetype <> onn then
  892. excepTSymtable.free;
  893. end;
  894. if not try_to_consume(_SEMICOLON) then
  895. break;
  896. consume_emptystats;
  897. until (token in [_END,_ELSE]);
  898. if try_to_consume(_ELSE) then
  899. begin
  900. { catch the other exceptions }
  901. p_default:=statements_til_end;
  902. end
  903. else
  904. consume(_END);
  905. end
  906. else
  907. begin
  908. { catch all exceptions }
  909. p_default:=statements_til_end;
  910. end;
  911. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  912. end;
  913. block_type:=old_block_type;
  914. current_exceptblock := oldcurrent_exceptblock;
  915. end;
  916. function _asm_statement : tnode;
  917. var
  918. asmstat : tasmnode;
  919. Marker : tai;
  920. reg : tregister;
  921. asmreader : tbaseasmreader;
  922. entrypos : tfileposinfo;
  923. begin
  924. Inside_asm_statement:=true;
  925. if assigned(asmmodeinfos[current_settings.asmmode]) then
  926. begin
  927. asmreader:=asmmodeinfos[current_settings.asmmode]^.casmreader.create;
  928. entrypos:=current_filepos;
  929. asmstat:=casmnode.create(asmreader.assemble as TAsmList);
  930. asmstat.fileinfo:=entrypos;
  931. asmreader.free;
  932. end
  933. else
  934. Message(parser_f_assembler_reader_not_supported);
  935. { Mark procedure that it has assembler blocks }
  936. include(current_procinfo.flags,pi_has_assembler_block);
  937. { Read first the _ASM statement }
  938. consume(_ASM);
  939. { END is read, got a list of changed registers? }
  940. if try_to_consume(_LECKKLAMMER) then
  941. begin
  942. {$ifdef cpunofpu}
  943. asmstat.used_regs_fpu:=[0..first_int_imreg-1];
  944. {$else cpunofpu}
  945. asmstat.used_regs_fpu:=[0..first_fpu_imreg-1];
  946. {$endif cpunofpu}
  947. if token<>_RECKKLAMMER then
  948. begin
  949. if po_assembler in current_procinfo.procdef.procoptions then
  950. Message(parser_w_register_list_ignored);
  951. repeat
  952. { it's possible to specify the modified registers }
  953. reg:=std_regnum_search(lower(cstringpattern));
  954. if reg<>NR_NO then
  955. begin
  956. if (getregtype(reg)=R_INTREGISTER) and not(po_assembler in current_procinfo.procdef.procoptions) then
  957. include(asmstat.used_regs_int,getsupreg(reg));
  958. end
  959. else
  960. Message(asmr_e_invalid_register);
  961. consume(_CSTRING);
  962. if not try_to_consume(_COMMA) then
  963. break;
  964. until false;
  965. end;
  966. consume(_RECKKLAMMER);
  967. end
  968. else
  969. begin
  970. asmstat.used_regs_int:=paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  971. asmstat.used_regs_fpu:=paramanager.get_volatile_registers_fpu(current_procinfo.procdef.proccalloption);
  972. end;
  973. { mark the start and the end of the assembler block
  974. this is needed for the optimizer }
  975. If Assigned(AsmStat.p_asm) Then
  976. Begin
  977. Marker := Tai_Marker.Create(mark_AsmBlockStart);
  978. AsmStat.p_asm.Insert(Marker);
  979. Marker := Tai_Marker.Create(mark_AsmBlockEnd);
  980. AsmStat.p_asm.Concat(Marker);
  981. End;
  982. Inside_asm_statement:=false;
  983. _asm_statement:=asmstat;
  984. end;
  985. function statement : tnode;
  986. var
  987. p,
  988. code : tnode;
  989. filepos : tfileposinfo;
  990. srsym : tsym;
  991. srsymtable : TSymtable;
  992. s : TIDString;
  993. begin
  994. filepos:=current_tokenpos;
  995. case token of
  996. _GOTO :
  997. begin
  998. if not(cs_support_goto in current_settings.moduleswitches) then
  999. Message(sym_e_goto_and_label_not_supported);
  1000. consume(_GOTO);
  1001. if (token<>_INTCONST) and (token<>_ID) then
  1002. begin
  1003. Message(sym_e_label_not_found);
  1004. code:=cerrornode.create;
  1005. end
  1006. else
  1007. begin
  1008. if token=_ID then
  1009. consume_sym(srsym,srsymtable)
  1010. else
  1011. begin
  1012. if token<>_INTCONST then
  1013. internalerror(201008021);
  1014. { strip leading 0's in iso mode }
  1015. if m_iso in current_settings.modeswitches then
  1016. while pattern[1]='0' do
  1017. delete(pattern,1,1);
  1018. searchsym(pattern,srsym,srsymtable);
  1019. if srsym=nil then
  1020. begin
  1021. identifier_not_found(pattern);
  1022. srsym:=generrorsym;
  1023. srsymtable:=nil;
  1024. end;
  1025. consume(token);
  1026. end;
  1027. if srsym.typ<>labelsym then
  1028. begin
  1029. Message(sym_e_id_is_no_label_id);
  1030. code:=cerrornode.create;
  1031. end
  1032. else
  1033. begin
  1034. { goto outside the current scope? }
  1035. if srsym.owner<>current_procinfo.procdef.localst then
  1036. begin
  1037. { allowed? }
  1038. if not(m_non_local_goto in current_settings.modeswitches) then
  1039. Message(parser_e_goto_outside_proc);
  1040. include(current_procinfo.flags,pi_has_global_goto);
  1041. end;
  1042. code:=cgotonode.create(tlabelsym(srsym));
  1043. tgotonode(code).labelsym:=tlabelsym(srsym);
  1044. { set flag that this label is used }
  1045. tlabelsym(srsym).used:=true;
  1046. end;
  1047. end;
  1048. end;
  1049. _BEGIN :
  1050. code:=statement_block(_BEGIN);
  1051. _IF :
  1052. code:=if_statement;
  1053. _CASE :
  1054. code:=case_statement;
  1055. _REPEAT :
  1056. code:=repeat_statement;
  1057. _WHILE :
  1058. code:=while_statement;
  1059. _FOR :
  1060. code:=for_statement;
  1061. _WITH :
  1062. code:=with_statement;
  1063. _TRY :
  1064. code:=try_statement;
  1065. _RAISE :
  1066. code:=raise_statement;
  1067. { semicolons,else until and end are ignored }
  1068. _SEMICOLON,
  1069. _ELSE,
  1070. _UNTIL,
  1071. _END:
  1072. code:=cnothingnode.create;
  1073. _FAIL :
  1074. begin
  1075. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  1076. Message(parser_e_fail_only_in_constructor);
  1077. consume(_FAIL);
  1078. code:=cnodeutils.call_fail_node;
  1079. end;
  1080. _ASM :
  1081. begin
  1082. if parse_generic then
  1083. Message(parser_e_no_assembler_in_generic);
  1084. code:=_asm_statement;
  1085. end;
  1086. _EOF :
  1087. Message(scan_f_end_of_file);
  1088. else
  1089. begin
  1090. { don't typecheck yet, because that will also simplify, which may
  1091. result in not detecting certain kinds of syntax errors --
  1092. see mantis #15594 }
  1093. p:=expr(false);
  1094. { save the pattern here for latter usage, the label could be "000",
  1095. even if we read an expression, the pattern is still valid if it's really
  1096. a label (FK)
  1097. if you want to mess here, take care of
  1098. tests/webtbs/tw3546.pp
  1099. }
  1100. s:=pattern;
  1101. { When a colon follows a intconst then transform it into a label }
  1102. if (p.nodetype=ordconstn) and
  1103. try_to_consume(_COLON) then
  1104. begin
  1105. { in iso mode, 0003: is equal to 3: }
  1106. if m_iso in current_settings.modeswitches then
  1107. searchsym(tostr(tordconstnode(p).value),srsym,srsymtable)
  1108. else
  1109. searchsym(s,srsym,srsymtable);
  1110. p.free;
  1111. if assigned(srsym) and
  1112. (srsym.typ=labelsym) then
  1113. begin
  1114. if tlabelsym(srsym).defined then
  1115. Message(sym_e_label_already_defined);
  1116. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1117. begin
  1118. tlabelsym(srsym).nonlocal:=true;
  1119. exclude(current_procinfo.procdef.procoptions,po_inline);
  1120. end;
  1121. if tlabelsym(srsym).nonlocal and
  1122. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1123. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  1124. tlabelsym(srsym).defined:=true;
  1125. p:=clabelnode.create(nil,tlabelsym(srsym));
  1126. tlabelsym(srsym).code:=p;
  1127. end
  1128. else
  1129. begin
  1130. Message1(sym_e_label_used_and_not_defined,s);
  1131. p:=cnothingnode.create;
  1132. end;
  1133. end;
  1134. if p.nodetype=labeln then
  1135. begin
  1136. { the pointer to the following instruction }
  1137. { isn't a very clean way }
  1138. if token in endtokens then
  1139. tlabelnode(p).left:=cnothingnode.create
  1140. else
  1141. tlabelnode(p).left:=statement();
  1142. { be sure to have left also typecheckpass }
  1143. typecheckpass(tlabelnode(p).left);
  1144. end
  1145. else
  1146. { change a load of a procvar to a call. this is also
  1147. supported in fpc mode }
  1148. if p.nodetype in [vecn,derefn,typeconvn,subscriptn,loadn] then
  1149. maybe_call_procvar(p,false);
  1150. { blockn support because a read/write is changed into a blocknode
  1151. with a separate statement for each read/write operation (JM)
  1152. the same is true for val() if the third parameter is not 32 bit
  1153. goto nodes are created by the compiler for non local exit statements, so
  1154. include them as well
  1155. }
  1156. if not(p.nodetype in [nothingn,errorn,calln,ifn,assignn,breakn,inlinen,
  1157. continuen,labeln,blockn,exitn,goton]) or
  1158. ((p.nodetype=inlinen) and
  1159. not is_void(p.resultdef)) or
  1160. ((p.nodetype=calln) and
  1161. (assigned(tcallnode(p).procdefinition)) and
  1162. (tcallnode(p).procdefinition.proctypeoption=potype_operator)) then
  1163. Message(parser_e_illegal_expression);
  1164. if not assigned(p.resultdef) then
  1165. do_typecheckpass(p);
  1166. { Specify that we don't use the value returned by the call.
  1167. This is used for :
  1168. - dispose of temp stack space
  1169. - dispose on FPU stack
  1170. - extended syntax checking }
  1171. if (p.nodetype=calln) then
  1172. begin
  1173. exclude(tcallnode(p).callnodeflags,cnf_return_value_used);
  1174. { in $x- state, the function result must not be ignored }
  1175. if not(cs_extsyntax in current_settings.moduleswitches) and
  1176. not(is_void(p.resultdef)) and
  1177. { can be nil in case there was an error in the expression }
  1178. assigned(tcallnode(p).procdefinition) and
  1179. { allow constructor calls to drop the result if they are
  1180. called as instance methods instead of class methods }
  1181. not(
  1182. (tcallnode(p).procdefinition.proctypeoption=potype_constructor) and
  1183. is_class_or_object(tprocdef(tcallnode(p).procdefinition).struct) and
  1184. assigned(tcallnode(p).methodpointer) and
  1185. (tnode(tcallnode(p).methodpointer).resultdef.typ=objectdef)
  1186. ) then
  1187. Message(parser_e_illegal_expression);
  1188. end;
  1189. code:=p;
  1190. end;
  1191. end;
  1192. if assigned(code) then
  1193. begin
  1194. typecheckpass(code);
  1195. code.fileinfo:=filepos;
  1196. end;
  1197. statement:=code;
  1198. end;
  1199. function statement_block(starttoken : ttoken) : tnode;
  1200. var
  1201. first,last : tnode;
  1202. filepos : tfileposinfo;
  1203. begin
  1204. first:=nil;
  1205. filepos:=current_tokenpos;
  1206. consume(starttoken);
  1207. while not(token in [_END,_FINALIZATION]) do
  1208. begin
  1209. if first=nil then
  1210. begin
  1211. last:=cstatementnode.create(statement,nil);
  1212. first:=last;
  1213. end
  1214. else
  1215. begin
  1216. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  1217. last:=tstatementnode(last).right;
  1218. end;
  1219. if (token in [_END,_FINALIZATION]) then
  1220. break
  1221. else
  1222. begin
  1223. { if no semicolon, then error and go on }
  1224. if token<>_SEMICOLON then
  1225. begin
  1226. consume(_SEMICOLON);
  1227. consume_all_until(_SEMICOLON);
  1228. end;
  1229. consume(_SEMICOLON);
  1230. end;
  1231. consume_emptystats;
  1232. end;
  1233. { don't consume the finalization token, it is consumed when
  1234. reading the finalization block, but allow it only after
  1235. an initalization ! }
  1236. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1237. consume(_END);
  1238. last:=cblocknode.create(first);
  1239. last.fileinfo:=filepos;
  1240. statement_block:=last;
  1241. end;
  1242. function assembler_block : tnode;
  1243. var
  1244. p : tnode;
  1245. {$ifndef arm}
  1246. locals : longint;
  1247. {$endif arm}
  1248. srsym : tsym;
  1249. begin
  1250. if parse_generic then
  1251. message(parser_e_no_assembler_in_generic);
  1252. { Rename the funcret so that recursive calls are possible }
  1253. if not is_void(current_procinfo.procdef.returndef) then
  1254. begin
  1255. srsym:=TSym(current_procinfo.procdef.localst.Find(current_procinfo.procdef.procsym.name));
  1256. if assigned(srsym) then
  1257. srsym.realname:='$hiddenresult';
  1258. end;
  1259. { delphi uses register calling for assembler methods }
  1260. if (m_delphi in current_settings.modeswitches) and
  1261. (po_assembler in current_procinfo.procdef.procoptions) and
  1262. not(po_hascallingconvention in current_procinfo.procdef.procoptions) then
  1263. current_procinfo.procdef.proccalloption:=pocall_register;
  1264. { force the asm statement }
  1265. if token<>_ASM then
  1266. consume(_ASM);
  1267. include(current_procinfo.flags,pi_is_assembler);
  1268. p:=_asm_statement;
  1269. {$if not(defined(sparc)) and not(defined(arm)) and not(defined(avr)) and not(defined(mips))}
  1270. if (po_assembler in current_procinfo.procdef.procoptions) then
  1271. begin
  1272. { set the framepointer to esp for assembler functions when the
  1273. following conditions are met:
  1274. - if the are no local variables and parameters (except the allocated result)
  1275. - no reference to the result variable (refcount<=1)
  1276. - result is not stored as parameter
  1277. - target processor has optional frame pointer save
  1278. (vm, i386, vm only currently)
  1279. }
  1280. locals:=tabstractlocalsymtable(current_procinfo.procdef.parast).count_locals;
  1281. if (current_procinfo.procdef.localst.symtabletype=localsymtable) then
  1282. inc(locals,tabstractlocalsymtable(current_procinfo.procdef.localst).count_locals);
  1283. if (locals=0) and
  1284. not (current_procinfo.procdef.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  1285. (not assigned(current_procinfo.procdef.funcretsym) or
  1286. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs<=1)) and
  1287. not (df_generic in current_procinfo.procdef.defoptions) and
  1288. not(paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef)) then
  1289. begin
  1290. { Only need to set the framepointer, the locals will
  1291. be inserted with the correct reference in tcgasmnode.pass_generate_code }
  1292. current_procinfo.framepointer:=NR_STACK_POINTER_REG;
  1293. end;
  1294. end;
  1295. {$endif not(defined(sparc)) and not(defined(arm)) and not(defined(avr))}
  1296. { Flag the result as assigned when it is returned in a
  1297. register.
  1298. }
  1299. if assigned(current_procinfo.procdef.funcretsym) and
  1300. not (df_generic in current_procinfo.procdef.defoptions) and
  1301. (not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef)) then
  1302. tabstractvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_initialised;
  1303. { because the END is already read we need to get the
  1304. last_endtoken_filepos here (PFV) }
  1305. last_endtoken_filepos:=current_tokenpos;
  1306. assembler_block:=p;
  1307. end;
  1308. end.