pstatmnt.pas 52 KB

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