pstatmnt.pas 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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,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. extendeddef : tabstractrecorddef;
  467. helperdef : tobjectdef;
  468. hasimplicitderef : boolean;
  469. withsymtablelist : TFPObjectList;
  470. procedure pushobjchild(withdef,obj:tobjectdef);
  471. var
  472. parenthelperdef : tobjectdef;
  473. begin
  474. if not assigned(obj) then
  475. exit;
  476. pushobjchild(withdef,obj.childof);
  477. { we need to look for helpers that were defined for the parent
  478. class as well }
  479. search_last_objectpascal_helper(obj,current_structdef,parenthelperdef);
  480. { push the symtables of the helper's parents in reverse order }
  481. if assigned(parenthelperdef) then
  482. pushobjchild(withdef,parenthelperdef.childof);
  483. { keep the original tobjectdef as owner, because that is used for
  484. visibility of the symtable }
  485. st:=twithsymtable.create(withdef,obj.symtable.SymList,refnode.getcopy);
  486. symtablestack.push(st);
  487. withsymtablelist.add(st);
  488. { push the symtable of the helper }
  489. if assigned(parenthelperdef) then
  490. begin
  491. st:=twithsymtable.create(withdef,parenthelperdef.symtable.SymList,refnode.getcopy);
  492. symtablestack.push(st);
  493. withsymtablelist.add(st);
  494. end;
  495. end;
  496. begin
  497. p:=comp_expr(true,false);
  498. do_typecheckpass(p);
  499. if (p.nodetype=vecn) and
  500. (nf_memseg in p.flags) then
  501. CGMessage(parser_e_no_with_for_variable_in_other_segments);
  502. { "with procvar" can never mean anything, so always try
  503. to call it in case it returns a record/object/... }
  504. maybe_call_procvar(p,false);
  505. if (p.resultdef.typ in [objectdef,recorddef,classrefdef]) 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. hasimplicitderef:=is_implicit_pointer_object_type(p.resultdef) or
  559. (p.resultdef.typ = classrefdef);
  560. if hasimplicitderef then
  561. hdef:=p.resultdef
  562. else
  563. hdef:=tpointerdef.create(p.resultdef);
  564. { load address of the value in a temp }
  565. tempnode:=ctempcreatenode.create_withnode(hdef,sizeof(pint),tt_persistent,true,p);
  566. typecheckpass(tnode(tempnode));
  567. valuenode:=p;
  568. refnode:=ctemprefnode.create(tempnode);
  569. fillchar(refnode.fileinfo,sizeof(tfileposinfo),0);
  570. { add address call for valuenode and deref for refnode if this
  571. is not done implicitly }
  572. if not hasimplicitderef then
  573. begin
  574. valuenode:=caddrnode.create_internal_nomark(valuenode);
  575. include(valuenode.flags,nf_typedaddr);
  576. refnode:=cderefnode.create(refnode);
  577. fillchar(refnode.fileinfo,sizeof(tfileposinfo),0);
  578. end;
  579. addstatement(newstatement,tempnode);
  580. addstatement(newstatement,cassignmentnode.create(
  581. ctemprefnode.create(tempnode),
  582. valuenode));
  583. typecheckpass(refnode);
  584. end;
  585. { do we have a helper for this type? }
  586. if p.resultdef.typ=classrefdef then
  587. extendeddef:=tobjectdef(tclassrefdef(p.resultdef).pointeddef)
  588. else
  589. extendeddef:=tabstractrecorddef(p.resultdef);
  590. search_last_objectpascal_helper(extendeddef,current_structdef,helperdef);
  591. { Note: the symtable of the helper is pushed after the following
  592. "case", the symtables of the helper's parents are passed in
  593. the "case" branches }
  594. withsymtablelist:=TFPObjectList.create(true);
  595. case p.resultdef.typ of
  596. objectdef :
  597. begin
  598. { push symtables of all parents in reverse order }
  599. pushobjchild(tobjectdef(p.resultdef),tobjectdef(p.resultdef).childof);
  600. { push symtables of all parents of the helper in reverse order }
  601. if assigned(helperdef) then
  602. pushobjchild(helperdef,helperdef.childof);
  603. { push object symtable }
  604. st:=twithsymtable.Create(tobjectdef(p.resultdef),tobjectdef(p.resultdef).symtable.SymList,refnode);
  605. symtablestack.push(st);
  606. withsymtablelist.add(st);
  607. end;
  608. classrefdef :
  609. begin
  610. { push symtables of all parents in reverse order }
  611. pushobjchild(tobjectdef(tclassrefdef(p.resultdef).pointeddef),tobjectdef(tclassrefdef(p.resultdef).pointeddef).childof);
  612. { push symtables of all parents of the helper in reverse order }
  613. if assigned(helperdef) then
  614. pushobjchild(helperdef,helperdef.childof);
  615. { push object symtable }
  616. st:=twithsymtable.Create(tobjectdef(tclassrefdef(p.resultdef).pointeddef),tobjectdef(tclassrefdef(p.resultdef).pointeddef).symtable.SymList,refnode);
  617. symtablestack.push(st);
  618. withsymtablelist.add(st);
  619. end;
  620. recorddef :
  621. begin
  622. { push symtables of all parents of the helper in reverse order }
  623. if assigned(helperdef) then
  624. pushobjchild(helperdef,helperdef.childof);
  625. { push record symtable }
  626. st:=twithsymtable.create(trecorddef(p.resultdef),trecorddef(p.resultdef).symtable.SymList,refnode);
  627. symtablestack.push(st);
  628. withsymtablelist.add(st);
  629. end;
  630. else
  631. internalerror(200601271);
  632. end;
  633. { push helper symtable }
  634. if assigned(helperdef) then
  635. begin
  636. st:=twithsymtable.Create(helperdef,helperdef.symtable.SymList,refnode.getcopy);
  637. symtablestack.push(st);
  638. withsymtablelist.add(st);
  639. end;
  640. if try_to_consume(_COMMA) then
  641. p:=_with_statement()
  642. else
  643. begin
  644. consume(_DO);
  645. if token<>_SEMICOLON then
  646. p:=statement
  647. else
  648. p:=cnothingnode.create;
  649. end;
  650. { remove symtables in reverse order from the stack }
  651. for i:=withsymtablelist.count-1 downto 0 do
  652. symtablestack.pop(TSymtable(withsymtablelist[i]));
  653. withsymtablelist.free;
  654. // p:=cwithnode.create(right,twithsymtable(withsymtable),levelcount,refnode);
  655. { Finalize complex withnode with destroy of temp }
  656. if assigned(newblock) then
  657. begin
  658. addstatement(newstatement,p);
  659. if assigned(tempnode) then
  660. addstatement(newstatement,ctempdeletenode.create(tempnode));
  661. if assigned(calltempnode) then
  662. addstatement(newstatement,ctempdeletenode.create(calltempnode));
  663. p:=newblock;
  664. end;
  665. result:=p;
  666. end
  667. else
  668. begin
  669. p.free;
  670. Message(parser_e_false_with_expr);
  671. { try to recover from error }
  672. if try_to_consume(_COMMA) then
  673. begin
  674. hp:=_with_statement();
  675. if (hp=nil) then; { remove warning about unused }
  676. end
  677. else
  678. begin
  679. consume(_DO);
  680. { ignore all }
  681. if token<>_SEMICOLON then
  682. statement;
  683. end;
  684. result:=nil;
  685. end;
  686. end;
  687. function with_statement : tnode;
  688. begin
  689. consume(_WITH);
  690. with_statement:=_with_statement();
  691. end;
  692. function raise_statement : tnode;
  693. var
  694. p,pobj,paddr,pframe : tnode;
  695. begin
  696. pobj:=nil;
  697. paddr:=nil;
  698. pframe:=nil;
  699. consume(_RAISE);
  700. if not(token in endtokens) then
  701. begin
  702. { object }
  703. pobj:=comp_expr(true,false);
  704. if try_to_consume(_AT) then
  705. begin
  706. paddr:=comp_expr(true,false);
  707. if try_to_consume(_COMMA) then
  708. pframe:=comp_expr(true,false);
  709. end;
  710. end
  711. else
  712. begin
  713. if (block_type<>bt_except) then
  714. Message(parser_e_no_reraise_possible);
  715. end;
  716. p:=craisenode.create(pobj,paddr,pframe);
  717. raise_statement:=p;
  718. end;
  719. function try_statement : tnode;
  720. var
  721. p_try_block,p_finally_block,first,last,
  722. p_default,p_specific,hp : tnode;
  723. ot : tDef;
  724. sym : tlocalvarsym;
  725. old_block_type : tblock_type;
  726. excepTSymtable : TSymtable;
  727. objname,objrealname : TIDString;
  728. srsym : tsym;
  729. srsymtable : TSymtable;
  730. t:ttoken;
  731. unit_found:boolean;
  732. oldcurrent_exceptblock: integer;
  733. begin
  734. p_default:=nil;
  735. p_specific:=nil;
  736. { read statements to try }
  737. consume(_TRY);
  738. first:=nil;
  739. inc(exceptblockcounter);
  740. oldcurrent_exceptblock := current_exceptblock;
  741. current_exceptblock := exceptblockcounter;
  742. old_block_type := block_type;
  743. block_type := bt_body;
  744. while (token<>_FINALLY) and (token<>_EXCEPT) do
  745. begin
  746. if first=nil then
  747. begin
  748. last:=cstatementnode.create(statement,nil);
  749. first:=last;
  750. end
  751. else
  752. begin
  753. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  754. last:=tstatementnode(last).right;
  755. end;
  756. if not try_to_consume(_SEMICOLON) then
  757. break;
  758. consume_emptystats;
  759. end;
  760. p_try_block:=cblocknode.create(first);
  761. if try_to_consume(_FINALLY) then
  762. begin
  763. inc(exceptblockcounter);
  764. current_exceptblock := exceptblockcounter;
  765. p_finally_block:=statements_til_end;
  766. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  767. end
  768. else
  769. begin
  770. consume(_EXCEPT);
  771. block_type:=bt_except;
  772. inc(exceptblockcounter);
  773. current_exceptblock := exceptblockcounter;
  774. ot:=generrordef;
  775. p_specific:=nil;
  776. if (idtoken=_ON) then
  777. { catch specific exceptions }
  778. begin
  779. repeat
  780. consume(_ON);
  781. if token=_ID then
  782. begin
  783. objname:=pattern;
  784. objrealname:=orgpattern;
  785. { can't use consume_sym here, because we need already
  786. to check for the colon }
  787. searchsym(objname,srsym,srsymtable);
  788. consume(_ID);
  789. { is a explicit name for the exception given ? }
  790. if try_to_consume(_COLON) then
  791. begin
  792. consume_sym(srsym,srsymtable);
  793. if (srsym.typ=typesym) and
  794. (is_class(ttypesym(srsym).typedef) or
  795. is_javaclass(ttypesym(srsym).typedef)) then
  796. begin
  797. ot:=ttypesym(srsym).typedef;
  798. sym:=tlocalvarsym.create(objrealname,vs_value,ot,[]);
  799. end
  800. else
  801. begin
  802. sym:=tlocalvarsym.create(objrealname,vs_value,generrordef,[]);
  803. if (srsym.typ=typesym) then
  804. Message1(type_e_class_type_expected,ttypesym(srsym).typedef.typename)
  805. else
  806. Message1(type_e_class_type_expected,ot.typename);
  807. end;
  808. end
  809. else
  810. begin
  811. { check if type is valid, must be done here because
  812. with "e: Exception" the e is not necessary }
  813. { support unit.identifier }
  814. unit_found:=try_consume_unitsym(srsym,srsymtable,t,false);
  815. if srsym=nil then
  816. begin
  817. identifier_not_found(orgpattern);
  818. srsym:=generrorsym;
  819. end;
  820. if unit_found then
  821. consume(t);
  822. { check if type is valid, must be done here because
  823. with "e: Exception" the e is not necessary }
  824. if (srsym.typ=typesym) and
  825. (is_class(ttypesym(srsym).typedef) or
  826. is_javaclass(ttypesym(srsym).typedef)) then
  827. ot:=ttypesym(srsym).typedef
  828. else
  829. begin
  830. ot:=generrordef;
  831. if (srsym.typ=typesym) then
  832. Message1(type_e_class_type_expected,ttypesym(srsym).typedef.typename)
  833. else
  834. Message1(type_e_class_type_expected,ot.typename);
  835. end;
  836. { create dummy symbol so we don't need a special
  837. case in ncgflw, and so that we always know the
  838. type }
  839. sym:=tlocalvarsym.create('$exceptsym',vs_value,ot,[]);
  840. end;
  841. excepTSymtable:=tstt_excepTSymtable.create;
  842. excepTSymtable.insert(sym);
  843. symtablestack.push(excepTSymtable);
  844. end
  845. else
  846. consume(_ID);
  847. consume(_DO);
  848. hp:=connode.create(nil,statement);
  849. if ot.typ=errordef then
  850. begin
  851. hp.free;
  852. hp:=cerrornode.create;
  853. end;
  854. if p_specific=nil then
  855. begin
  856. last:=hp;
  857. p_specific:=last;
  858. end
  859. else
  860. begin
  861. tonnode(last).left:=hp;
  862. last:=tonnode(last).left;
  863. end;
  864. { set the informations }
  865. { only if the creation of the onnode was succesful, it's possible }
  866. { that last and hp are errornodes (JM) }
  867. if last.nodetype = onn then
  868. begin
  869. tonnode(last).excepttype:=tobjectdef(ot);
  870. tonnode(last).excepTSymtable:=excepTSymtable;
  871. end;
  872. { remove exception symtable }
  873. if assigned(excepTSymtable) then
  874. begin
  875. symtablestack.pop(excepTSymtable);
  876. if last.nodetype <> onn then
  877. excepTSymtable.free;
  878. end;
  879. if not try_to_consume(_SEMICOLON) then
  880. break;
  881. consume_emptystats;
  882. until (token in [_END,_ELSE]);
  883. if try_to_consume(_ELSE) then
  884. begin
  885. { catch the other exceptions }
  886. p_default:=statements_til_end;
  887. end
  888. else
  889. consume(_END);
  890. end
  891. else
  892. begin
  893. { catch all exceptions }
  894. p_default:=statements_til_end;
  895. end;
  896. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  897. end;
  898. block_type:=old_block_type;
  899. current_exceptblock := oldcurrent_exceptblock;
  900. end;
  901. function _asm_statement : tnode;
  902. var
  903. asmstat : tasmnode;
  904. Marker : tai;
  905. reg : tregister;
  906. asmreader : tbaseasmreader;
  907. entrypos : tfileposinfo;
  908. begin
  909. Inside_asm_statement:=true;
  910. if assigned(asmmodeinfos[current_settings.asmmode]) then
  911. begin
  912. asmreader:=asmmodeinfos[current_settings.asmmode]^.casmreader.create;
  913. entrypos:=current_filepos;
  914. asmstat:=casmnode.create(asmreader.assemble as TAsmList);
  915. asmstat.fileinfo:=entrypos;
  916. asmreader.free;
  917. end
  918. else
  919. Message(parser_f_assembler_reader_not_supported);
  920. { Mark procedure that it has assembler blocks }
  921. include(current_procinfo.flags,pi_has_assembler_block);
  922. { Read first the _ASM statement }
  923. consume(_ASM);
  924. { END is read, got a list of changed registers? }
  925. if try_to_consume(_LECKKLAMMER) then
  926. begin
  927. {$ifdef cpunofpu}
  928. asmstat.used_regs_fpu:=[0..first_int_imreg-1];
  929. {$else cpunofpu}
  930. asmstat.used_regs_fpu:=[0..first_fpu_imreg-1];
  931. {$endif cpunofpu}
  932. if token<>_RECKKLAMMER then
  933. begin
  934. if po_assembler in current_procinfo.procdef.procoptions then
  935. Message(parser_w_register_list_ignored);
  936. repeat
  937. { it's possible to specify the modified registers }
  938. reg:=std_regnum_search(lower(cstringpattern));
  939. if reg<>NR_NO then
  940. begin
  941. if (getregtype(reg)=R_INTREGISTER) and not(po_assembler in current_procinfo.procdef.procoptions) then
  942. include(asmstat.used_regs_int,getsupreg(reg));
  943. end
  944. else
  945. Message(asmr_e_invalid_register);
  946. consume(_CSTRING);
  947. if not try_to_consume(_COMMA) then
  948. break;
  949. until false;
  950. end;
  951. consume(_RECKKLAMMER);
  952. end
  953. else
  954. begin
  955. asmstat.used_regs_int:=paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  956. asmstat.used_regs_fpu:=paramanager.get_volatile_registers_fpu(current_procinfo.procdef.proccalloption);
  957. end;
  958. { mark the start and the end of the assembler block
  959. this is needed for the optimizer }
  960. If Assigned(AsmStat.p_asm) Then
  961. Begin
  962. Marker := Tai_Marker.Create(mark_AsmBlockStart);
  963. AsmStat.p_asm.Insert(Marker);
  964. Marker := Tai_Marker.Create(mark_AsmBlockEnd);
  965. AsmStat.p_asm.Concat(Marker);
  966. End;
  967. Inside_asm_statement:=false;
  968. _asm_statement:=asmstat;
  969. end;
  970. function statement : tnode;
  971. var
  972. p,
  973. code : tnode;
  974. filepos : tfileposinfo;
  975. srsym : tsym;
  976. srsymtable : TSymtable;
  977. s : TIDString;
  978. begin
  979. filepos:=current_tokenpos;
  980. case token of
  981. _GOTO :
  982. begin
  983. if not(cs_support_goto in current_settings.moduleswitches) then
  984. Message(sym_e_goto_and_label_not_supported);
  985. consume(_GOTO);
  986. if (token<>_INTCONST) and (token<>_ID) then
  987. begin
  988. Message(sym_e_label_not_found);
  989. code:=cerrornode.create;
  990. end
  991. else
  992. begin
  993. if token=_ID then
  994. consume_sym(srsym,srsymtable)
  995. else
  996. begin
  997. if token<>_INTCONST then
  998. internalerror(201008021);
  999. { strip leading 0's in iso mode }
  1000. if m_iso in current_settings.modeswitches then
  1001. while pattern[1]='0' do
  1002. delete(pattern,1,1);
  1003. searchsym(pattern,srsym,srsymtable);
  1004. if srsym=nil then
  1005. begin
  1006. identifier_not_found(pattern);
  1007. srsym:=generrorsym;
  1008. srsymtable:=nil;
  1009. end;
  1010. consume(token);
  1011. end;
  1012. if srsym.typ<>labelsym then
  1013. begin
  1014. Message(sym_e_id_is_no_label_id);
  1015. code:=cerrornode.create;
  1016. end
  1017. else
  1018. begin
  1019. { goto outside the current scope? }
  1020. if srsym.owner<>current_procinfo.procdef.localst then
  1021. begin
  1022. { allowed? }
  1023. if not(m_non_local_goto in current_settings.modeswitches) then
  1024. Message(parser_e_goto_outside_proc);
  1025. include(current_procinfo.flags,pi_has_global_goto);
  1026. end;
  1027. code:=cgotonode.create(tlabelsym(srsym));
  1028. tgotonode(code).labelsym:=tlabelsym(srsym);
  1029. { set flag that this label is used }
  1030. tlabelsym(srsym).used:=true;
  1031. end;
  1032. end;
  1033. end;
  1034. _BEGIN :
  1035. code:=statement_block(_BEGIN);
  1036. _IF :
  1037. code:=if_statement;
  1038. _CASE :
  1039. code:=case_statement;
  1040. _REPEAT :
  1041. code:=repeat_statement;
  1042. _WHILE :
  1043. code:=while_statement;
  1044. _FOR :
  1045. code:=for_statement;
  1046. _WITH :
  1047. code:=with_statement;
  1048. _TRY :
  1049. code:=try_statement;
  1050. _RAISE :
  1051. code:=raise_statement;
  1052. { semicolons,else until and end are ignored }
  1053. _SEMICOLON,
  1054. _ELSE,
  1055. _UNTIL,
  1056. _END:
  1057. code:=cnothingnode.create;
  1058. _FAIL :
  1059. begin
  1060. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  1061. Message(parser_e_fail_only_in_constructor);
  1062. consume(_FAIL);
  1063. code:=cnodeutils.call_fail_node;
  1064. end;
  1065. _ASM :
  1066. code:=_asm_statement;
  1067. _EOF :
  1068. Message(scan_f_end_of_file);
  1069. else
  1070. begin
  1071. { don't typecheck yet, because that will also simplify, which may
  1072. result in not detecting certain kinds of syntax errors --
  1073. see mantis #15594 }
  1074. p:=expr(false);
  1075. { save the pattern here for latter usage, the label could be "000",
  1076. even if we read an expression, the pattern is still valid if it's really
  1077. a label (FK)
  1078. if you want to mess here, take care of
  1079. tests/webtbs/tw3546.pp
  1080. }
  1081. s:=pattern;
  1082. { When a colon follows a intconst then transform it into a label }
  1083. if (p.nodetype=ordconstn) and
  1084. try_to_consume(_COLON) then
  1085. begin
  1086. { in iso mode, 0003: is equal to 3: }
  1087. if m_iso in current_settings.modeswitches then
  1088. searchsym(tostr(tordconstnode(p).value),srsym,srsymtable)
  1089. else
  1090. searchsym(s,srsym,srsymtable);
  1091. p.free;
  1092. if assigned(srsym) and
  1093. (srsym.typ=labelsym) then
  1094. begin
  1095. if tlabelsym(srsym).defined then
  1096. Message(sym_e_label_already_defined);
  1097. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1098. begin
  1099. tlabelsym(srsym).nonlocal:=true;
  1100. exclude(current_procinfo.procdef.procoptions,po_inline);
  1101. end;
  1102. if tlabelsym(srsym).nonlocal and
  1103. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1104. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  1105. tlabelsym(srsym).defined:=true;
  1106. p:=clabelnode.create(nil,tlabelsym(srsym));
  1107. tlabelsym(srsym).code:=p;
  1108. end
  1109. else
  1110. begin
  1111. Message1(sym_e_label_used_and_not_defined,s);
  1112. p:=cnothingnode.create;
  1113. end;
  1114. end;
  1115. if p.nodetype=labeln then
  1116. begin
  1117. { the pointer to the following instruction }
  1118. { isn't a very clean way }
  1119. if token in endtokens then
  1120. tlabelnode(p).left:=cnothingnode.create
  1121. else
  1122. tlabelnode(p).left:=statement();
  1123. { be sure to have left also typecheckpass }
  1124. typecheckpass(tlabelnode(p).left);
  1125. end
  1126. else
  1127. { change a load of a procvar to a call. this is also
  1128. supported in fpc mode }
  1129. if p.nodetype in [vecn,derefn,typeconvn,subscriptn,loadn] then
  1130. maybe_call_procvar(p,false);
  1131. { blockn support because a read/write is changed into a blocknode }
  1132. { with a separate statement for each read/write operation (JM) }
  1133. { the same is true for val() if the third parameter is not 32 bit }
  1134. if not(p.nodetype in [nothingn,errorn,calln,ifn,assignn,breakn,inlinen,
  1135. continuen,labeln,blockn,exitn]) or
  1136. ((p.nodetype=inlinen) and
  1137. not is_void(p.resultdef)) or
  1138. ((p.nodetype=calln) and
  1139. (assigned(tcallnode(p).procdefinition)) and
  1140. (tcallnode(p).procdefinition.proctypeoption=potype_operator)) then
  1141. Message(parser_e_illegal_expression);
  1142. if not assigned(p.resultdef) then
  1143. do_typecheckpass(p);
  1144. { Specify that we don't use the value returned by the call.
  1145. This is used for :
  1146. - dispose of temp stack space
  1147. - dispose on FPU stack
  1148. - extended syntax checking }
  1149. if (p.nodetype=calln) then
  1150. begin
  1151. exclude(tcallnode(p).callnodeflags,cnf_return_value_used);
  1152. { in $x- state, the function result must not be ignored }
  1153. if not(cs_extsyntax in current_settings.moduleswitches) and
  1154. not(is_void(p.resultdef)) and
  1155. { can be nil in case there was an error in the expression }
  1156. assigned(tcallnode(p).procdefinition) and
  1157. not((tcallnode(p).procdefinition.proctypeoption=potype_constructor) and
  1158. is_object(tprocdef(tcallnode(p).procdefinition).struct)) then
  1159. Message(parser_e_illegal_expression);
  1160. end;
  1161. code:=p;
  1162. end;
  1163. end;
  1164. if assigned(code) then
  1165. begin
  1166. typecheckpass(code);
  1167. code.fileinfo:=filepos;
  1168. end;
  1169. statement:=code;
  1170. end;
  1171. function statement_block(starttoken : ttoken) : tnode;
  1172. var
  1173. first,last : tnode;
  1174. filepos : tfileposinfo;
  1175. begin
  1176. first:=nil;
  1177. filepos:=current_tokenpos;
  1178. consume(starttoken);
  1179. while not(token in [_END,_FINALIZATION]) do
  1180. begin
  1181. if first=nil then
  1182. begin
  1183. last:=cstatementnode.create(statement,nil);
  1184. first:=last;
  1185. end
  1186. else
  1187. begin
  1188. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  1189. last:=tstatementnode(last).right;
  1190. end;
  1191. if (token in [_END,_FINALIZATION]) then
  1192. break
  1193. else
  1194. begin
  1195. { if no semicolon, then error and go on }
  1196. if token<>_SEMICOLON then
  1197. begin
  1198. consume(_SEMICOLON);
  1199. consume_all_until(_SEMICOLON);
  1200. end;
  1201. consume(_SEMICOLON);
  1202. end;
  1203. consume_emptystats;
  1204. end;
  1205. { don't consume the finalization token, it is consumed when
  1206. reading the finalization block, but allow it only after
  1207. an initalization ! }
  1208. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1209. consume(_END);
  1210. last:=cblocknode.create(first);
  1211. last.fileinfo:=filepos;
  1212. statement_block:=last;
  1213. end;
  1214. function assembler_block : tnode;
  1215. var
  1216. p : tnode;
  1217. {$ifndef arm}
  1218. locals : longint;
  1219. {$endif arm}
  1220. srsym : tsym;
  1221. begin
  1222. { Rename the funcret so that recursive calls are possible }
  1223. if not is_void(current_procinfo.procdef.returndef) then
  1224. begin
  1225. srsym:=TSym(current_procinfo.procdef.localst.Find(current_procinfo.procdef.procsym.name));
  1226. if assigned(srsym) then
  1227. srsym.realname:='$hiddenresult';
  1228. end;
  1229. { delphi uses register calling for assembler methods }
  1230. if (m_delphi in current_settings.modeswitches) and
  1231. (po_assembler in current_procinfo.procdef.procoptions) and
  1232. not(po_hascallingconvention in current_procinfo.procdef.procoptions) then
  1233. current_procinfo.procdef.proccalloption:=pocall_register;
  1234. { force the asm statement }
  1235. if token<>_ASM then
  1236. consume(_ASM);
  1237. include(current_procinfo.flags,pi_is_assembler);
  1238. p:=_asm_statement;
  1239. {$if not(defined(sparc)) and not(defined(arm)) and not(defined(avr))}
  1240. if (po_assembler in current_procinfo.procdef.procoptions) then
  1241. begin
  1242. { set the framepointer to esp for assembler functions when the
  1243. following conditions are met:
  1244. - if the are no local variables and parameters (except the allocated result)
  1245. - no reference to the result variable (refcount<=1)
  1246. - result is not stored as parameter
  1247. - target processor has optional frame pointer save
  1248. (vm, i386, vm only currently)
  1249. }
  1250. locals:=tabstractlocalsymtable(current_procinfo.procdef.parast).count_locals;
  1251. if (current_procinfo.procdef.localst.symtabletype=localsymtable) then
  1252. inc(locals,tabstractlocalsymtable(current_procinfo.procdef.localst).count_locals);
  1253. if (locals=0) and
  1254. not (current_procinfo.procdef.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  1255. (not assigned(current_procinfo.procdef.funcretsym) or
  1256. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs<=1)) and
  1257. not(paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption)) then
  1258. begin
  1259. { Only need to set the framepointer, the locals will
  1260. be inserted with the correct reference in tcgasmnode.pass_generate_code }
  1261. current_procinfo.framepointer:=NR_STACK_POINTER_REG;
  1262. end;
  1263. end;
  1264. {$endif not(defined(sparc)) and not(defined(arm)) and not(defined(avr))}
  1265. { Flag the result as assigned when it is returned in a
  1266. register.
  1267. }
  1268. if assigned(current_procinfo.procdef.funcretsym) and
  1269. (not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption)) then
  1270. tabstractvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_initialised;
  1271. { because the END is already read we need to get the
  1272. last_endtoken_filepos here (PFV) }
  1273. last_endtoken_filepos:=current_tokenpos;
  1274. assembler_block:=p;
  1275. end;
  1276. end.