nflw.pas 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and register allocation for nodes that influence
  4. the flow
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nflw;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. node,cpubase,
  24. symtype,symbase,symdef,symsym,
  25. optloop;
  26. type
  27. { flags used by loop nodes }
  28. tloopflag = (
  29. { set if it is a for ... downto ... do loop }
  30. lnf_backward,
  31. { Do we need to parse childs to set var state? }
  32. lnf_varstate,
  33. { Do a test at the begin of the loop?}
  34. lnf_testatbegin,
  35. { Negate the loop test? }
  36. lnf_checknegate,
  37. { Should the value of the loop variable on exit be correct. }
  38. lnf_dont_mind_loopvar_on_exit,
  39. { Loop simplify flag }
  40. lnf_simplify_processing);
  41. tloopflags = set of tloopflag;
  42. const
  43. { loop flags which must match to consider loop nodes equal regarding the flags }
  44. loopflagsequal = [lnf_backward];
  45. type
  46. tlabelnode = class;
  47. tloopnode = class(tbinarynode)
  48. t1,t2 : tnode;
  49. loopflags : tloopflags;
  50. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  51. destructor destroy;override;
  52. function dogetcopy : tnode;override;
  53. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  54. procedure ppuwrite(ppufile:tcompilerppufile);override;
  55. procedure buildderefimpl;override;
  56. procedure derefimpl;override;
  57. procedure insertintolist(l : tnodelist);override;
  58. procedure printnodetree(var t:text);override;
  59. function docompare(p: tnode): boolean; override;
  60. end;
  61. twhilerepeatnode = class(tloopnode)
  62. { l: condition; r: body; tab: test at begin; cn: negate condition
  63. x,y,true,false: while loop
  64. x,y,false,true: repeat until loop }
  65. constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
  66. function pass_typecheck:tnode;override;
  67. function pass_1 : tnode;override;
  68. {$ifdef state_tracking}
  69. function track_state_pass(exec_known:boolean):boolean;override;
  70. {$endif}
  71. end;
  72. twhilerepeatnodeclass = class of twhilerepeatnode;
  73. tifnode = class(tloopnode)
  74. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  75. constructor create_internal(l,r,_t1 : tnode);virtual;reintroduce;
  76. function pass_typecheck:tnode;override;
  77. function pass_1 : tnode;override;
  78. function simplify(forinline : boolean) : tnode;override;
  79. private
  80. function internalsimplify(warn: boolean) : tnode;
  81. end;
  82. tifnodeclass = class of tifnode;
  83. tfornode = class(tloopnode)
  84. { if count isn divisable by unrolls then
  85. the for loop must jump to this label to get the correct
  86. number of executions }
  87. entrylabel,
  88. { this is a dummy node used by the dfa to store life information for the loop iteration }
  89. loopiteration : tnode;
  90. loopvar_notid:cardinal;
  91. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;reintroduce;
  92. function pass_typecheck:tnode;override;
  93. function pass_1 : tnode;override;
  94. function simplify(forinline : boolean) : tnode;override;
  95. end;
  96. tfornodeclass = class of tfornode;
  97. texitnode = class(tunarynode)
  98. constructor create(l:tnode);virtual;
  99. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  100. procedure ppuwrite(ppufile:tcompilerppufile);override;
  101. function pass_typecheck:tnode;override;
  102. function pass_1 : tnode;override;
  103. property resultexpr : tnode read left write left;
  104. end;
  105. texitnodeclass = class of texitnode;
  106. tbreaknode = class(tnode)
  107. constructor create;virtual;
  108. function pass_typecheck:tnode;override;
  109. function pass_1 : tnode;override;
  110. end;
  111. tbreaknodeclass = class of tbreaknode;
  112. tcontinuenode = class(tnode)
  113. constructor create;virtual;
  114. function pass_typecheck:tnode;override;
  115. function pass_1 : tnode;override;
  116. end;
  117. tcontinuenodeclass = class of tcontinuenode;
  118. tgotonode = class(tnode)
  119. private
  120. labelnodeidx : longint;
  121. public
  122. labelsym : tlabelsym;
  123. labelnode : tlabelnode;
  124. exceptionblock : integer;
  125. constructor create(p : tlabelsym);virtual;
  126. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  127. procedure ppuwrite(ppufile:tcompilerppufile);override;
  128. procedure buildderefimpl;override;
  129. procedure derefimpl;override;
  130. procedure resolveppuidx;override;
  131. function dogetcopy : tnode;override;
  132. function pass_typecheck:tnode;override;
  133. function pass_1 : tnode;override;
  134. function docompare(p: tnode): boolean; override;
  135. end;
  136. tgotonodeclass = class of tgotonode;
  137. tlabelnode = class(tunarynode)
  138. exceptionblock : integer;
  139. { when copying trees, this points to the newly created copy of a label }
  140. copiedto : tlabelnode;
  141. labsym : tlabelsym;
  142. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  143. destructor destroy;override;
  144. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  145. procedure ppuwrite(ppufile:tcompilerppufile);override;
  146. procedure buildderefimpl;override;
  147. procedure derefimpl;override;
  148. function dogetcopy : tnode;override;
  149. function pass_typecheck:tnode;override;
  150. function pass_1 : tnode;override;
  151. function docompare(p: tnode): boolean; override;
  152. end;
  153. tlabelnodeclass = class of tlabelnode;
  154. traisenode = class(ttertiarynode)
  155. constructor create(l,taddr,tframe:tnode);virtual;
  156. function pass_typecheck:tnode;override;
  157. function pass_1 : tnode;override;
  158. end;
  159. traisenodeclass = class of traisenode;
  160. ttryexceptnode = class(tloopnode)
  161. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  162. function pass_typecheck:tnode;override;
  163. function pass_1 : tnode;override;
  164. function simplify(forinline: boolean): tnode; override;
  165. protected
  166. procedure adjust_estimated_stack_size; virtual;
  167. end;
  168. ttryexceptnodeclass = class of ttryexceptnode;
  169. ttryfinallynode = class(tloopnode)
  170. implicitframe : boolean;
  171. constructor create(l,r:tnode);virtual;reintroduce;
  172. constructor create_implicit(l,r,_t1:tnode);virtual;
  173. function pass_typecheck:tnode;override;
  174. function pass_1 : tnode;override;
  175. function simplify(forinline:boolean): tnode;override;
  176. protected
  177. function create_finalizer_procdef: tprocdef;
  178. procedure adjust_estimated_stack_size; virtual;
  179. end;
  180. ttryfinallynodeclass = class of ttryfinallynode;
  181. tonnode = class(tbinarynode)
  182. excepTSymtable : TSymtable;
  183. excepttype : tobjectdef;
  184. constructor create(l,r:tnode);virtual;
  185. destructor destroy;override;
  186. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  187. function pass_typecheck:tnode;override;
  188. function pass_1 : tnode;override;
  189. function dogetcopy : tnode;override;
  190. function docompare(p: tnode): boolean; override;
  191. end;
  192. tonnodeclass = class of tonnode;
  193. var
  194. cwhilerepeatnode : twhilerepeatnodeclass=twhilerepeatnode;
  195. cifnode : tifnodeclass = tifnode;
  196. cfornode : tfornodeclass = tfornode;
  197. cexitnode : texitnodeclass = texitnode;
  198. cgotonode : tgotonodeclass = tgotonode;
  199. clabelnode : tlabelnodeclass = tlabelnode;
  200. craisenode : traisenodeclass = traisenode;
  201. ctryexceptnode : ttryexceptnodeclass = ttryexceptnode;
  202. ctryfinallynode : ttryfinallynodeclass = ttryfinallynode;
  203. connode : tonnodeclass = tonnode;
  204. cbreaknode : tbreaknodeclass = tbreaknode;
  205. ccontinuenode : tcontinuenodeclass = tcontinuenode;
  206. // for-in loop helpers
  207. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  208. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  209. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  210. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  211. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  212. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  213. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  214. implementation
  215. uses
  216. globtype,systems,constexp,
  217. cutils,verbose,globals,
  218. symconst,symtable,paramgr,defcmp,defutil,htypechk,pass_1,
  219. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,nutils,ninl,nset,ngenutil,
  220. pdecsub,
  221. {$ifdef state_tracking}
  222. nstate,
  223. {$endif}
  224. {$ifdef i8086}
  225. cpuinfo,
  226. {$endif i8086}
  227. cgbase,procinfo
  228. ;
  229. // for-in loop helpers
  230. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  231. begin
  232. result:=cfornode.create(hloopvar,
  233. cinlinenode.create(in_low_x,false,expr.getcopy),
  234. cinlinenode.create(in_high_x,false,expr.getcopy),
  235. hloopbody,
  236. false);
  237. end;
  238. function create_objc_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  239. var
  240. mainstatement, outerloopbodystatement, innerloopbodystatement, tempstatement: tstatementnode;
  241. state, mutationcheck, currentamount, innerloopcounter, items, expressiontemp: ttempcreatenode;
  242. outerloop, innerloop, hp: tnode;
  243. itemsarraydef: tarraydef;
  244. sym: tsym;
  245. begin
  246. { Objective-C enumerators require Objective-C 2.0 }
  247. if not(m_objectivec2 in current_settings.modeswitches) then
  248. begin
  249. result:=cerrornode.create;
  250. MessagePos(expr.fileinfo,parser_e_objc_enumerator_2_0);
  251. exit;
  252. end;
  253. { Requires the NSFastEnumeration protocol and NSFastEnumerationState
  254. record }
  255. maybeloadcocoatypes;
  256. if not assigned(objc_fastenumeration) or
  257. not assigned(objc_fastenumerationstate) then
  258. begin
  259. result:=cerrornode.create;
  260. MessagePos(expr.fileinfo,parser_e_objc_missing_enumeration_defs);
  261. exit;
  262. end;
  263. (* Original code:
  264. for hloopvar in expression do
  265. <hloopbody>
  266. Pascal code equivalent into which it has to be transformed
  267. (sure would be nice if the compiler had some kind of templates ;) :
  268. var
  269. state: NSFastEnumerationState;
  270. expressiontemp: NSFastEnumerationProtocol;
  271. mutationcheck,
  272. currentamount,
  273. innerloopcounter: culong;
  274. { size can be increased/decreased if desired }
  275. items: array[1..16] of id;
  276. begin
  277. fillchar(state,sizeof(state),0);
  278. expressiontemp:=expression;
  279. repeat
  280. currentamount:=expressiontemp.countByEnumeratingWithState_objects_count(@state,@items,length(items));
  281. if currentamount=0 then
  282. begin
  283. { "The iterating variable is set to nil when the loop ends by
  284. exhausting the source pool of objects" }
  285. hloopvar:=nil;
  286. break;
  287. end;
  288. mutationcheck:=state.mutationsptr^;
  289. innerloopcounter:=culong(-1);
  290. repeat
  291. { at the start so that "continue" in <loopbody> works correctly }
  292. { don't use for-loop, because then the value of the iteration
  293. counter is undefined on exit and we have to check it in the
  294. outer repeat/until condition }
  295. {$push}
  296. {$r-,q-}
  297. inc(innerloopcounter);
  298. {$pop}
  299. if innerloopcounter=currentamount then
  300. break;
  301. if mutationcheck<>state.mutationsptr^ then
  302. { raises Objective-C exception... }
  303. objc_enumerationMutation(expressiontemp);
  304. hloopvar:=state.itemsPtr[innerloopcounter];
  305. { if continue in loopbody -> jumps to start, increases count and checks }
  306. { if break in loopbody: goes to outer repeat/until and innerloopcount
  307. will be < currentamount -> stops }
  308. <hloopbody>
  309. until false;
  310. { if the inner loop terminated early, "break" was used and we have
  311. to stop }
  312. { "If the loop is terminated early, the iterating variable is left
  313. pointing to the last iteration item." }
  314. until innerloopcounter<currentamount;
  315. end;
  316. *)
  317. result:=internalstatements(mainstatement);
  318. { the fast enumeration state }
  319. state:=ctempcreatenode.create(objc_fastenumerationstate,objc_fastenumerationstate.size,tt_persistent,false);
  320. typecheckpass(tnode(state));
  321. addstatement(mainstatement,state);
  322. { the temporary items array }
  323. itemsarraydef:=carraydef.create(1,16,u32inttype);
  324. itemsarraydef.elementdef:=objc_idtype;
  325. items:=ctempcreatenode.create(itemsarraydef,itemsarraydef.size,tt_persistent,false);
  326. addstatement(mainstatement,items);
  327. typecheckpass(tnode(items));
  328. { temp for the expression/collection through which we iterate }
  329. expressiontemp:=ctempcreatenode.create(objc_fastenumeration,objc_fastenumeration.size,tt_persistent,true);
  330. addstatement(mainstatement,expressiontemp);
  331. { currentamount temp (not really clean: we use ptruint instead of
  332. culong) }
  333. currentamount:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  334. typecheckpass(tnode(currentamount));
  335. addstatement(mainstatement,currentamount);
  336. { mutationcheck temp (idem) }
  337. mutationcheck:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  338. typecheckpass(tnode(mutationcheck));
  339. addstatement(mainstatement,mutationcheck);
  340. { innerloopcounter temp (idem) }
  341. innerloopcounter:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  342. typecheckpass(tnode(innerloopcounter));
  343. addstatement(mainstatement,innerloopcounter);
  344. { initialise the state with 0 }
  345. addstatement(mainstatement,ccallnode.createinternfromunit('SYSTEM','FILLCHAR',
  346. ccallparanode.create(genintconstnode(0),
  347. ccallparanode.create(genintconstnode(objc_fastenumerationstate.size),
  348. ccallparanode.create(ctemprefnode.create(state),nil)
  349. )
  350. )
  351. ));
  352. { this will also check whether the expression (potentially) conforms
  353. to the NSFastEnumeration protocol (use expr.getcopy, because the
  354. caller will free expr) }
  355. addstatement(mainstatement,cassignmentnode.create(ctemprefnode.create(expressiontemp),expr.getcopy));
  356. { we add the "repeat..until" afterwards, now just create the body }
  357. outerloop:=internalstatements(outerloopbodystatement);
  358. { the countByEnumeratingWithState_objects_count call }
  359. hp:=ccallparanode.create(cinlinenode.create(in_length_x,false,ctypenode.create(itemsarraydef)),
  360. ccallparanode.create(caddrnode.create(ctemprefnode.create(items)),
  361. ccallparanode.create(caddrnode.create(ctemprefnode.create(state)),nil)
  362. )
  363. );
  364. sym:=search_struct_member(objc_fastenumeration,'COUNTBYENUMERATINGWITHSTATE_OBJECTS_COUNT');
  365. if not assigned(sym) or
  366. (sym.typ<>procsym) then
  367. internalerror(2010061901);
  368. hp:=ccallnode.create(hp,tprocsym(sym),sym.owner,ctemprefnode.create(expressiontemp),[],nil);
  369. addstatement(outerloopbodystatement,cassignmentnode.create(
  370. ctemprefnode.create(currentamount),hp));
  371. { if currentamount = 0, bail out (use copy of hloopvar, because we
  372. have to use it again below) }
  373. hp:=internalstatements(tempstatement);
  374. addstatement(tempstatement,cassignmentnode.create(
  375. hloopvar.getcopy,cnilnode.create));
  376. addstatement(tempstatement,cbreaknode.create);
  377. addstatement(outerloopbodystatement,cifnode.create(
  378. caddnode.create(equaln,ctemprefnode.create(currentamount),genintconstnode(0)),
  379. hp,nil));
  380. { initial value of mutationcheck }
  381. hp:=ctemprefnode.create(state);
  382. typecheckpass(hp);
  383. hp:=cderefnode.create(genloadfield(hp,'MUTATIONSPTR'));
  384. addstatement(outerloopbodystatement,cassignmentnode.create(
  385. ctemprefnode.create(mutationcheck),hp));
  386. { initialise innerloopcounter }
  387. addstatement(outerloopbodystatement,cassignmentnode.create(
  388. ctemprefnode.create(innerloopcounter),cordconstnode.create(-1,ptruinttype,false)));
  389. { and now the inner loop, again adding the repeat/until afterwards }
  390. innerloop:=internalstatements(innerloopbodystatement);
  391. { inc(innerloopcounter) without range/overflowchecking (because
  392. we go from culong(-1) to 0 during the first iteration }
  393. hp:=cinlinenode.create(
  394. in_inc_x,false,ccallparanode.create(
  395. ctemprefnode.create(innerloopcounter),nil));
  396. hp.localswitches:=hp.localswitches-[cs_check_range,cs_check_overflow];
  397. addstatement(innerloopbodystatement,hp);
  398. { if innerloopcounter=currentamount then break to the outer loop }
  399. addstatement(innerloopbodystatement,cifnode.create(
  400. caddnode.create(equaln,
  401. ctemprefnode.create(innerloopcounter),
  402. ctemprefnode.create(currentamount)),
  403. cbreaknode.create,
  404. nil));
  405. { verify that the collection didn't change in the mean time }
  406. hp:=ctemprefnode.create(state);
  407. typecheckpass(hp);
  408. addstatement(innerloopbodystatement,cifnode.create(
  409. caddnode.create(unequaln,
  410. ctemprefnode.create(mutationcheck),
  411. cderefnode.create(genloadfield(hp,'MUTATIONSPTR'))
  412. ),
  413. ccallnode.createinternfromunit('OBJC','OBJC_ENUMERATIONMUTATION',
  414. ccallparanode.create(ctemprefnode.create(expressiontemp),nil)),
  415. nil));
  416. { finally: actually get the next element }
  417. hp:=ctemprefnode.create(state);
  418. typecheckpass(hp);
  419. hp:=genloadfield(hp,'ITEMSPTR');
  420. typecheckpass(hp);
  421. { don't simply use a vecn, because indexing a pointer won't work in
  422. non-FPC modes }
  423. if hp.resultdef.typ<>pointerdef then
  424. internalerror(2010061904);
  425. inserttypeconv(hp,
  426. carraydef.create_from_pointer(tpointerdef(hp.resultdef)));
  427. hp:=cvecnode.create(hp,ctemprefnode.create(innerloopcounter));
  428. addstatement(innerloopbodystatement,
  429. cassignmentnode.create(hloopvar,hp));
  430. { the actual loop body! }
  431. addstatement(innerloopbodystatement,hloopbody);
  432. { create the inner repeat/until and add it to the body of the outer
  433. one }
  434. hp:=cwhilerepeatnode.create(
  435. { repeat .. until false }
  436. cordconstnode.create(0,pasbool8type,false),innerloop,false,true);
  437. addstatement(outerloopbodystatement,hp);
  438. { create the outer repeat/until and add it to the the main body }
  439. hp:=cwhilerepeatnode.create(
  440. { repeat .. until innerloopcounter<currentamount }
  441. caddnode.create(ltn,
  442. ctemprefnode.create(innerloopcounter),
  443. ctemprefnode.create(currentamount)),
  444. outerloop,false,true);
  445. addstatement(mainstatement,hp);
  446. { release the temps }
  447. addstatement(mainstatement,ctempdeletenode.create(state));
  448. addstatement(mainstatement,ctempdeletenode.create(mutationcheck));
  449. addstatement(mainstatement,ctempdeletenode.create(currentamount));
  450. addstatement(mainstatement,ctempdeletenode.create(innerloopcounter));
  451. addstatement(mainstatement,ctempdeletenode.create(items));
  452. addstatement(mainstatement,ctempdeletenode.create(expressiontemp));
  453. end;
  454. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  455. var
  456. loopstatement, loopbodystatement: tstatementnode;
  457. loopvar, stringvar: ttempcreatenode;
  458. stringindex, loopbody, forloopnode: tnode;
  459. begin
  460. { result is a block of statements }
  461. result:=internalstatements(loopstatement);
  462. { create a temp variable for expression }
  463. stringvar := ctempcreatenode.create(
  464. expr.resultdef,
  465. expr.resultdef.size,
  466. tt_persistent,true);
  467. addstatement(loopstatement,stringvar);
  468. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(stringvar),expr.getcopy));
  469. { create a loop counter: signed integer with size of string length }
  470. loopvar := ctempcreatenode.create(
  471. sinttype,
  472. sinttype.size,
  473. tt_persistent,true);
  474. addstatement(loopstatement,loopvar);
  475. stringindex:=ctemprefnode.create(loopvar);
  476. loopbody:=internalstatements(loopbodystatement);
  477. // for-in loop variable := string_expression[index]
  478. addstatement(loopbodystatement,
  479. cassignmentnode.create(hloopvar, cvecnode.create(ctemprefnode.create(stringvar),stringindex)));
  480. { add the actual statement to the loop }
  481. addstatement(loopbodystatement,hloopbody);
  482. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  483. genintconstnode(1),
  484. cinlinenode.create(in_length_x,false,ctemprefnode.create(stringvar)),
  485. loopbody,
  486. false);
  487. addstatement(loopstatement,forloopnode);
  488. { free the loop counter }
  489. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  490. { free the temp variable for expression }
  491. addstatement(loopstatement,ctempdeletenode.create(stringvar));
  492. end;
  493. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  494. var
  495. loopstatement, loopbodystatement: tstatementnode;
  496. loopvar, arrayvar: ttempcreatenode;
  497. arrayindex, lowbound, highbound, loopbody, forloopnode, expression: tnode;
  498. is_string: boolean;
  499. tmpdef, convertdef: tdef;
  500. elementcount: aword;
  501. begin
  502. expression := expr;
  503. { result is a block of statements }
  504. result:=internalstatements(loopstatement);
  505. is_string:=ado_IsConstString in tarraydef(expr.resultdef).arrayoptions;
  506. // if array element type <> loovar type then create a conversion if possible
  507. if compare_defs(tarraydef(expression.resultdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible then
  508. begin
  509. tmpdef:=expression.resultdef;
  510. elementcount:=1;
  511. while assigned(tmpdef) and (tmpdef.typ=arraydef) and
  512. (tarraydef(tmpdef).arrayoptions = []) and
  513. (compare_defs(tarraydef(tmpdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible) do
  514. begin
  515. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  516. tmpdef:=tarraydef(tmpdef).elementdef;
  517. end;
  518. if assigned(tmpdef) and (tmpdef.typ=arraydef) and (tarraydef(tmpdef).arrayoptions = []) then
  519. begin
  520. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  521. convertdef:=carraydef.create(0,elementcount-1,s32inttype);
  522. tarraydef(convertdef).elementdef:=tarraydef(tmpdef).elementdef;
  523. expression:=expr.getcopy;
  524. expression:=ctypeconvnode.create_internal(expression,convertdef);
  525. typecheckpass(expression);
  526. addstatement(loopstatement,expression);
  527. end;
  528. end;
  529. if (node_complexity(expression) > 1) and
  530. not(is_open_array(expression.resultdef)) and not(is_array_of_const(expression.resultdef)) then
  531. begin
  532. { create a temp variable for expression }
  533. arrayvar := ctempcreatenode.create(
  534. expression.resultdef,
  535. expression.resultdef.size,
  536. tt_persistent,true);
  537. if is_string then
  538. begin
  539. lowbound:=genintconstnode(1);
  540. highbound:=cinlinenode.create(in_length_x,false,ctemprefnode.create(arrayvar))
  541. end
  542. else
  543. begin
  544. lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
  545. highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
  546. end;
  547. addstatement(loopstatement,arrayvar);
  548. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(arrayvar),expression.getcopy));
  549. end
  550. else
  551. begin
  552. arrayvar:=nil;
  553. if is_string then
  554. begin
  555. lowbound:=genintconstnode(1);
  556. highbound:=cinlinenode.create(in_length_x,false,expression.getcopy);
  557. end
  558. else
  559. begin
  560. lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
  561. highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
  562. end;
  563. end;
  564. { create a loop counter }
  565. loopvar := ctempcreatenode.create(
  566. tarraydef(expression.resultdef).rangedef,
  567. tarraydef(expression.resultdef).rangedef.size,
  568. tt_persistent,true);
  569. addstatement(loopstatement,loopvar);
  570. arrayindex:=ctemprefnode.create(loopvar);
  571. loopbody:=internalstatements(loopbodystatement);
  572. // for-in loop variable := array_expression[index]
  573. if assigned(arrayvar) then
  574. addstatement(loopbodystatement,
  575. cassignmentnode.create(hloopvar,cvecnode.create(ctemprefnode.create(arrayvar),arrayindex)))
  576. else
  577. addstatement(loopbodystatement,
  578. cassignmentnode.create(hloopvar,cvecnode.create(expression.getcopy,arrayindex)));
  579. { add the actual statement to the loop }
  580. addstatement(loopbodystatement,hloopbody);
  581. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  582. lowbound,
  583. highbound,
  584. loopbody,
  585. false);
  586. addstatement(loopstatement,forloopnode);
  587. { free the loop counter }
  588. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  589. { free the temp variable for expression if needed }
  590. if arrayvar<>nil then
  591. addstatement(loopstatement,ctempdeletenode.create(arrayvar));
  592. end;
  593. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  594. var
  595. loopstatement, loopbodystatement: tstatementnode;
  596. loopvar, setvar: ttempcreatenode;
  597. loopbody, forloopnode: tnode;
  598. begin
  599. // first check is set is empty and if it so then skip other processing
  600. if not Assigned(tsetdef(expr.resultdef).elementdef) then
  601. begin
  602. result:=cnothingnode.create;
  603. // free unused nodes
  604. hloopvar.free;
  605. hloopbody.free;
  606. exit;
  607. end;
  608. { result is a block of statements }
  609. result:=internalstatements(loopstatement);
  610. { create a temp variable for expression }
  611. setvar := ctempcreatenode.create(
  612. expr.resultdef,
  613. expr.resultdef.size,
  614. tt_persistent,true);
  615. addstatement(loopstatement,setvar);
  616. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(setvar),expr.getcopy));
  617. { create a loop counter }
  618. loopvar := ctempcreatenode.create(
  619. tsetdef(expr.resultdef).elementdef,
  620. tsetdef(expr.resultdef).elementdef.size,
  621. tt_persistent,true);
  622. addstatement(loopstatement,loopvar);
  623. // if loopvar in set then
  624. // begin
  625. // hloopvar := loopvar
  626. // for-in loop body
  627. // end
  628. loopbody:=cifnode.create(
  629. cinnode.create(ctemprefnode.create(loopvar),ctemprefnode.create(setvar)),
  630. internalstatements(loopbodystatement),
  631. nil);
  632. addstatement(loopbodystatement,cassignmentnode.create(hloopvar,ctemprefnode.create(loopvar)));
  633. { add the actual statement to the loop }
  634. addstatement(loopbodystatement,hloopbody);
  635. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  636. cinlinenode.create(in_low_x,false,ctemprefnode.create(setvar)),
  637. cinlinenode.create(in_high_x,false,ctemprefnode.create(setvar)),
  638. loopbody,
  639. false);
  640. addstatement(loopstatement,forloopnode);
  641. { free the loop counter }
  642. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  643. { free the temp variable for expression }
  644. addstatement(loopstatement,ctempdeletenode.create(setvar));
  645. end;
  646. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  647. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  648. var
  649. loopstatement, loopbodystatement: tstatementnode;
  650. enumvar: ttempcreatenode;
  651. loopbody, whileloopnode,
  652. enum_get, enum_move, enum_current, enum_get_params: tnode;
  653. propaccesslist: tpropaccesslist;
  654. enumerator_is_class: boolean;
  655. enumerator_destructor: tprocdef;
  656. begin
  657. { result is a block of statements }
  658. result:=internalstatements(loopstatement);
  659. enumerator_is_class := is_class(enumerator_get.returndef);
  660. { create a temp variable for enumerator }
  661. enumvar := ctempcreatenode.create(
  662. enumerator_get.returndef,
  663. enumerator_get.returndef.size,
  664. tt_persistent,true);
  665. addstatement(loopstatement,enumvar);
  666. if enumerator_get.proctypeoption=potype_operator then
  667. begin
  668. enum_get_params:=ccallparanode.create(expr.getcopy,nil);
  669. enum_get:=ccallnode.create(enum_get_params, tprocsym(enumerator_get.procsym), nil, nil, [],nil);
  670. tcallnode(enum_get).procdefinition:=enumerator_get;
  671. addsymref(enumerator_get.procsym);
  672. end
  673. else
  674. enum_get:=ccallnode.create(nil, tprocsym(enumerator_get.procsym), enumerator_get.owner, expr.getcopy, [],nil);
  675. addstatement(loopstatement,
  676. cassignmentnode.create(
  677. ctemprefnode.create(enumvar),
  678. enum_get
  679. ));
  680. loopbody:=internalstatements(loopbodystatement);
  681. { for-in loop variable := enumerator.current }
  682. if enumerator_current.getpropaccesslist(palt_read,propaccesslist) then
  683. begin
  684. case propaccesslist.firstsym^.sym.typ of
  685. fieldvarsym :
  686. begin
  687. { generate access code }
  688. enum_current:=ctemprefnode.create(enumvar);
  689. propaccesslist_to_node(enum_current,enumerator_current.owner,propaccesslist);
  690. include(enum_current.flags,nf_isproperty);
  691. end;
  692. procsym :
  693. begin
  694. { generate the method call }
  695. enum_current:=ccallnode.create(nil,tprocsym(propaccesslist.firstsym^.sym),enumerator_current.owner,ctemprefnode.create(enumvar),[],nil);
  696. include(enum_current.flags,nf_isproperty);
  697. end
  698. else
  699. begin
  700. enum_current:=cerrornode.create;
  701. Message(type_e_mismatch);
  702. end;
  703. end;
  704. end
  705. else
  706. enum_current:=cerrornode.create;
  707. addstatement(loopbodystatement,
  708. cassignmentnode.create(hloopvar, enum_current));
  709. { add the actual statement to the loop }
  710. addstatement(loopbodystatement,hloopbody);
  711. enum_move:=ccallnode.create(nil, tprocsym(enumerator_move.procsym), enumerator_move.owner, ctemprefnode.create(enumvar), [],nil);
  712. whileloopnode:=cwhilerepeatnode.create(enum_move,loopbody,true,false);
  713. if enumerator_is_class then
  714. begin
  715. { insert a try-finally and call the destructor for the enumerator in the finally section }
  716. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  717. if assigned(enumerator_destructor) then
  718. begin
  719. whileloopnode:=ctryfinallynode.create(
  720. whileloopnode, // try node
  721. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym), // finally node
  722. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
  723. end;
  724. { if getenumerator <> nil then do the loop }
  725. whileloopnode:=cifnode.create(
  726. caddnode.create(unequaln, ctemprefnode.create(enumvar), cnilnode.create),
  727. whileloopnode,
  728. nil);
  729. end;
  730. addstatement(loopstatement, whileloopnode);
  731. if is_object(enumerator_get.returndef) then
  732. begin
  733. // call the object destructor too
  734. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  735. if assigned(enumerator_destructor) then
  736. begin
  737. addstatement(loopstatement,
  738. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym),
  739. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
  740. end;
  741. end;
  742. { free the temp variable for enumerator }
  743. addstatement(loopstatement,ctempdeletenode.create(enumvar));
  744. end;
  745. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  746. var
  747. pd, movenext: tprocdef;
  748. helperdef: tobjectdef;
  749. current: tpropertysym;
  750. storefilepos: tfileposinfo;
  751. begin
  752. storefilepos:=current_filepos;
  753. current_filepos:=hloopvar.fileinfo;
  754. if expr.nodetype=typen then
  755. begin
  756. if (expr.resultdef.typ=enumdef) and tenumdef(expr.resultdef).has_jumps then
  757. begin
  758. result:=cerrornode.create;
  759. hloopvar.free;
  760. hloopbody.free;
  761. MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
  762. end
  763. else
  764. result:=create_type_for_in_loop(hloopvar, hloopbody, expr);
  765. end
  766. else
  767. begin
  768. { loop is made for an expression }
  769. // Objective-C uses different conventions (and it's only supported for Objective-C 2.0)
  770. if is_objc_class_or_protocol(hloopvar.resultdef) or
  771. is_objc_class_or_protocol(expr.resultdef) then
  772. begin
  773. result:=create_objc_for_in_loop(hloopvar,hloopbody,expr);
  774. if result.nodetype=errorn then
  775. begin
  776. hloopvar.free;
  777. hloopbody.free;
  778. end;
  779. end
  780. else
  781. begin
  782. // search for operator first
  783. pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
  784. // if there is no operator then search for class/object enumerator method
  785. if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
  786. begin
  787. { first search using the helper hierarchy }
  788. if search_last_objectpascal_helper(tabstractrecorddef(expr.resultdef),nil,helperdef) then
  789. repeat
  790. pd:=helperdef.search_enumerator_get;
  791. helperdef:=helperdef.childof;
  792. until (pd<>nil) or (helperdef=nil);
  793. { we didn't find an enumerator in a helper, so search in the
  794. class/record/object itself }
  795. if pd=nil then
  796. pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
  797. end;
  798. if pd<>nil then
  799. begin
  800. // seach movenext and current symbols
  801. movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
  802. if movenext = nil then
  803. begin
  804. result:=cerrornode.create;
  805. hloopvar.free;
  806. hloopbody.free;
  807. MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
  808. end
  809. else
  810. begin
  811. current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  812. if current = nil then
  813. begin
  814. result:=cerrornode.create;
  815. hloopvar.free;
  816. hloopbody.free;
  817. MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
  818. end
  819. else
  820. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  821. end;
  822. end
  823. else
  824. begin
  825. case expr.resultdef.typ of
  826. stringdef: result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  827. arraydef: result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  828. setdef: result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  829. else
  830. begin
  831. result:=cerrornode.create;
  832. hloopvar.free;
  833. hloopbody.free;
  834. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  835. end;
  836. end;
  837. end;
  838. end;
  839. end;
  840. current_filepos:=storefilepos;
  841. end;
  842. {****************************************************************************
  843. TLOOPNODE
  844. *****************************************************************************}
  845. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  846. begin
  847. inherited create(tt,l,r);
  848. t1:=_t1;
  849. t2:=_t2;
  850. fileinfo:=l.fileinfo;
  851. end;
  852. destructor tloopnode.destroy;
  853. begin
  854. t1.free;
  855. t2.free;
  856. inherited destroy;
  857. end;
  858. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  859. begin
  860. inherited ppuload(t,ppufile);
  861. t1:=ppuloadnode(ppufile);
  862. t2:=ppuloadnode(ppufile);
  863. ppufile.getsmallset(loopflags);
  864. end;
  865. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  866. begin
  867. inherited ppuwrite(ppufile);
  868. ppuwritenode(ppufile,t1);
  869. ppuwritenode(ppufile,t2);
  870. ppufile.putsmallset(loopflags);
  871. end;
  872. procedure tloopnode.buildderefimpl;
  873. begin
  874. inherited buildderefimpl;
  875. if assigned(t1) then
  876. t1.buildderefimpl;
  877. if assigned(t2) then
  878. t2.buildderefimpl;
  879. end;
  880. procedure tloopnode.derefimpl;
  881. begin
  882. inherited derefimpl;
  883. if assigned(t1) then
  884. t1.derefimpl;
  885. if assigned(t2) then
  886. t2.derefimpl;
  887. end;
  888. function tloopnode.dogetcopy : tnode;
  889. var
  890. p : tloopnode;
  891. begin
  892. p:=tloopnode(inherited dogetcopy);
  893. if assigned(t1) then
  894. p.t1:=t1.dogetcopy
  895. else
  896. p.t1:=nil;
  897. if assigned(t2) then
  898. p.t2:=t2.dogetcopy
  899. else
  900. p.t2:=nil;
  901. p.loopflags:=loopflags;
  902. dogetcopy:=p;
  903. end;
  904. procedure tloopnode.insertintolist(l : tnodelist);
  905. begin
  906. end;
  907. procedure tloopnode.printnodetree(var t:text);
  908. begin
  909. write(t,printnodeindention,'(');
  910. printnodeindent;
  911. printnodeinfo(t);
  912. writeln(t);
  913. printnode(t,left);
  914. printnode(t,right);
  915. printnode(t,t1);
  916. printnode(t,t2);
  917. printnodeunindent;
  918. writeln(t,printnodeindention,')');
  919. end;
  920. function tloopnode.docompare(p: tnode): boolean;
  921. begin
  922. docompare :=
  923. inherited docompare(p) and
  924. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  925. t1.isequal(tloopnode(p).t1) and
  926. t2.isequal(tloopnode(p).t2);
  927. end;
  928. {****************************************************************************
  929. TWHILEREPEATNODE
  930. *****************************************************************************}
  931. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  932. begin
  933. inherited create(whilerepeatn,l,r,nil,nil);
  934. if tab then
  935. include(loopflags, lnf_testatbegin);
  936. if cn then
  937. include(loopflags,lnf_checknegate);
  938. end;
  939. function twhilerepeatnode.pass_typecheck:tnode;
  940. var
  941. t:Tunarynode;
  942. begin
  943. result:=nil;
  944. resultdef:=voidtype;
  945. typecheckpass(left);
  946. { tp procvar support }
  947. maybe_call_procvar(left,true);
  948. {A not node can be removed.}
  949. if left.nodetype=notn then
  950. begin
  951. t:=Tunarynode(left);
  952. left:=Tunarynode(left).left;
  953. t.left:=nil;
  954. t.destroy;
  955. {Symdif operator, in case you are wondering:}
  956. loopflags:=loopflags >< [lnf_checknegate];
  957. end;
  958. { loop instruction }
  959. if assigned(right) then
  960. typecheckpass(right);
  961. set_varstate(left,vs_read,[vsf_must_be_valid]);
  962. if codegenerror then
  963. exit;
  964. if not(is_boolean(left.resultdef)) and
  965. not(is_typeparam(left.resultdef)) then
  966. inserttypeconv(left,pasbool8type);
  967. { Give warnings for code that will never be executed for
  968. while false do }
  969. if (lnf_testatbegin in loopflags) and
  970. (left.nodetype=ordconstn) and
  971. (tordconstnode(left).value.uvalue=0) and
  972. assigned(right) then
  973. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  974. end;
  975. {$ifdef prefetchnext}
  976. type
  977. passignmentquery = ^tassignmentquery;
  978. tassignmentquery = record
  979. towhat: tnode;
  980. source: tassignmentnode;
  981. statementcount: cardinal;
  982. end;
  983. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  984. var
  985. query: passignmentquery absolute arg;
  986. temp, prederef: tnode;
  987. begin
  988. result := fen_norecurse_false;
  989. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  990. inc(query^.statementcount);
  991. { make sure there's something else in the loop besides going to the }
  992. { next item }
  993. if (query^.statementcount > 1) and
  994. (n.nodetype = assignn) then
  995. begin
  996. { skip type conversions of assignment target }
  997. temp := tassignmentnode(n).left;
  998. while (temp.nodetype = typeconvn) do
  999. temp := ttypeconvnode(temp).left;
  1000. { assignment to x of the while assigned(x) check? }
  1001. if not(temp.isequal(query^.towhat)) then
  1002. exit;
  1003. { right hand side of assignment dereferenced field of }
  1004. { x? (no derefn in case of class) }
  1005. temp := tassignmentnode(n).right;
  1006. while (temp.nodetype = typeconvn) do
  1007. temp := ttypeconvnode(temp).left;
  1008. if (temp.nodetype <> subscriptn) then
  1009. exit;
  1010. prederef := tsubscriptnode(temp).left;
  1011. temp := prederef;
  1012. while (temp.nodetype = typeconvn) do
  1013. temp := ttypeconvnode(temp).left;
  1014. { see tests/test/prefetch1.pp }
  1015. if (temp.nodetype = derefn) then
  1016. temp := tderefnode(temp).left
  1017. else
  1018. temp := prederef;
  1019. if temp.isequal(query^.towhat) then
  1020. begin
  1021. query^.source := tassignmentnode(n);
  1022. result := fen_norecurse_true;
  1023. end
  1024. end
  1025. { don't check nodes which can't contain an assignment or whose }
  1026. { final assignment can vary a lot }
  1027. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1028. result := fen_false;
  1029. end;
  1030. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1031. var
  1032. query: tassignmentquery;
  1033. begin
  1034. query.towhat := towhat;
  1035. query.source := nil;
  1036. query.statementcount := 0;
  1037. if foreachnodestatic(where,@checkassignment,@query) then
  1038. result := query.source
  1039. else
  1040. result := nil;
  1041. end;
  1042. {$endif prefetchnext}
  1043. function twhilerepeatnode.pass_1 : tnode;
  1044. {$ifdef prefetchnext}
  1045. var
  1046. runnernode, prefetchcode: tnode;
  1047. assignmentnode: tassignmentnode;
  1048. prefetchstatements: tstatementnode;
  1049. {$endif prefetchnext}
  1050. begin
  1051. result:=nil;
  1052. expectloc:=LOC_VOID;
  1053. firstpass(left);
  1054. if codegenerror then
  1055. exit;
  1056. { loop instruction }
  1057. if assigned(right) then
  1058. begin
  1059. firstpass(right);
  1060. if codegenerror then
  1061. exit;
  1062. end;
  1063. {$ifdef prefetchnext}
  1064. { do at the end so all complex typeconversions are already }
  1065. { converted to calln's }
  1066. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1067. (lnf_testatbegin in loopflags) then
  1068. begin
  1069. { get first component of the while check }
  1070. runnernode := left;
  1071. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1072. runnernode := tunarynode(runnernode).left;
  1073. { is it an assigned(x) check? }
  1074. if ((runnernode.nodetype = inlinen) and
  1075. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1076. ((runnernode.nodetype = unequaln) and
  1077. (taddnode(runnernode).right.nodetype = niln)) then
  1078. begin
  1079. runnernode := tunarynode(runnernode).left;
  1080. { in case of in_assigned_x, there's a callparan in between }
  1081. if (runnernode.nodetype = callparan) then
  1082. runnernode := tcallparanode(runnernode).left;
  1083. while (runnernode.nodetype = typeconvn) do
  1084. runnernode := ttypeconvnode(runnernode).left;
  1085. { is there an "x := x(^).somefield"? }
  1086. assignmentnode := findassignment(right,runnernode);
  1087. if assigned(assignmentnode) then
  1088. begin
  1089. prefetchcode := internalstatements(prefetchstatements);
  1090. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1091. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1092. addstatement(prefetchstatements,right);
  1093. right := prefetchcode;
  1094. typecheckpass(right);
  1095. end;
  1096. end;
  1097. end;
  1098. {$endif prefetchnext}
  1099. end;
  1100. {$ifdef state_tracking}
  1101. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1102. var condition:Tnode;
  1103. code:Tnode;
  1104. done:boolean;
  1105. value:boolean;
  1106. change:boolean;
  1107. firsttest:boolean;
  1108. factval:Tnode;
  1109. begin
  1110. track_state_pass:=false;
  1111. done:=false;
  1112. firsttest:=true;
  1113. {For repeat until statements, first do a pass through the code.}
  1114. if not(lnf_testatbegin in flags) then
  1115. begin
  1116. code:=right.getcopy;
  1117. if code.track_state_pass(exec_known) then
  1118. track_state_pass:=true;
  1119. code.destroy;
  1120. end;
  1121. repeat
  1122. condition:=left.getcopy;
  1123. code:=right.getcopy;
  1124. change:=condition.track_state_pass(exec_known);
  1125. factval:=aktstate.find_fact(left);
  1126. if factval<>nil then
  1127. begin
  1128. condition.destroy;
  1129. condition:=factval.getcopy;
  1130. change:=true;
  1131. end;
  1132. if change then
  1133. begin
  1134. track_state_pass:=true;
  1135. {Force new resultdef pass.}
  1136. condition.resultdef:=nil;
  1137. do_typecheckpass(condition);
  1138. end;
  1139. if is_constboolnode(condition) then
  1140. begin
  1141. {Try to turn a while loop into a repeat loop.}
  1142. if firsttest then
  1143. exclude(flags,testatbegin);
  1144. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1145. if value then
  1146. begin
  1147. if code.track_state_pass(exec_known) then
  1148. track_state_pass:=true;
  1149. end
  1150. else
  1151. done:=true;
  1152. end
  1153. else
  1154. begin
  1155. {Remove any modified variables from the state.}
  1156. code.track_state_pass(false);
  1157. done:=true;
  1158. end;
  1159. code.destroy;
  1160. condition.destroy;
  1161. firsttest:=false;
  1162. until done;
  1163. {The loop condition is also known, for example:
  1164. while i<10 do
  1165. begin
  1166. ...
  1167. end;
  1168. When the loop is done, we do know that i<10 = false.
  1169. }
  1170. condition:=left.getcopy;
  1171. if condition.track_state_pass(exec_known) then
  1172. begin
  1173. track_state_pass:=true;
  1174. {Force new resultdef pass.}
  1175. condition.resultdef:=nil;
  1176. do_typecheckpass(condition);
  1177. end;
  1178. if not is_constboolnode(condition) then
  1179. aktstate.store_fact(condition,
  1180. cordconstnode.create(byte(checknegate),pasbool8type,true))
  1181. else
  1182. condition.destroy;
  1183. end;
  1184. {$endif}
  1185. {*****************************************************************************
  1186. TIFNODE
  1187. *****************************************************************************}
  1188. constructor tifnode.create(l,r,_t1 : tnode);
  1189. begin
  1190. inherited create(ifn,l,r,_t1,nil);
  1191. end;
  1192. constructor tifnode.create_internal(l,r,_t1 : tnode);
  1193. begin
  1194. create(l,r,_t1);
  1195. include(flags,nf_internal);
  1196. end;
  1197. function tifnode.internalsimplify(warn: boolean) : tnode;
  1198. begin
  1199. result:=nil;
  1200. { optimize constant expressions }
  1201. if (left.nodetype=ordconstn) then
  1202. begin
  1203. if tordconstnode(left).value.uvalue<>0 then
  1204. begin
  1205. if assigned(right) then
  1206. result:=right
  1207. else
  1208. result:=cnothingnode.create;
  1209. right:=nil;
  1210. if warn and assigned(t1) then
  1211. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1212. end
  1213. else
  1214. begin
  1215. if assigned(t1) then
  1216. result:=t1
  1217. else
  1218. result:=cnothingnode.create;
  1219. t1:=nil;
  1220. if warn and assigned(right) then
  1221. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1222. end;
  1223. end;
  1224. end;
  1225. function tifnode.simplify(forinline : boolean) : tnode;
  1226. begin
  1227. result:=internalsimplify(false);
  1228. end;
  1229. function tifnode.pass_typecheck:tnode;
  1230. begin
  1231. result:=nil;
  1232. resultdef:=voidtype;
  1233. typecheckpass(left);
  1234. { tp procvar support }
  1235. maybe_call_procvar(left,true);
  1236. { if path }
  1237. if assigned(right) then
  1238. typecheckpass(right);
  1239. { else path }
  1240. if assigned(t1) then
  1241. typecheckpass(t1);
  1242. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1243. if codegenerror then
  1244. exit;
  1245. if not(is_boolean(left.resultdef)) and
  1246. not(is_typeparam(left.resultdef)) then
  1247. inserttypeconv(left,pasbool8type);
  1248. result:=internalsimplify(not(nf_internal in flags));
  1249. end;
  1250. function tifnode.pass_1 : tnode;
  1251. begin
  1252. result:=nil;
  1253. expectloc:=LOC_VOID;
  1254. firstpass(left);
  1255. { if path }
  1256. if assigned(right) then
  1257. firstpass(right);
  1258. { else path }
  1259. if assigned(t1) then
  1260. firstpass(t1);
  1261. { leave if we've got an error in one of the paths }
  1262. if codegenerror then
  1263. exit;
  1264. end;
  1265. {*****************************************************************************
  1266. TFORNODE
  1267. *****************************************************************************}
  1268. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1269. begin
  1270. inherited create(forn,l,r,_t1,_t2);
  1271. if back then
  1272. include(loopflags,lnf_backward);
  1273. include(loopflags,lnf_testatbegin);
  1274. end;
  1275. function tfornode.simplify(forinline : boolean) : tnode;
  1276. begin
  1277. result:=nil;
  1278. { Can we spare the first comparision? }
  1279. if (t1.nodetype=ordconstn) and
  1280. (right.nodetype=ordconstn) and
  1281. (
  1282. (
  1283. (lnf_backward in loopflags) and
  1284. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1285. ) or
  1286. (
  1287. not(lnf_backward in loopflags) and
  1288. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1289. )
  1290. ) then
  1291. exclude(loopflags,lnf_testatbegin);
  1292. if (t1.nodetype=ordconstn) and
  1293. (right.nodetype=ordconstn) and
  1294. (
  1295. (
  1296. (lnf_backward in loopflags) and
  1297. (tordconstnode(right).value<tordconstnode(t1).value)
  1298. ) or
  1299. (
  1300. not(lnf_backward in loopflags) and
  1301. (tordconstnode(right).value>tordconstnode(t1).value)
  1302. )
  1303. ) then
  1304. result:=cnothingnode.create;
  1305. end;
  1306. function tfornode.pass_typecheck:tnode;
  1307. var
  1308. res : tnode;
  1309. begin
  1310. result:=nil;
  1311. resultdef:=voidtype;
  1312. { process the loopvar, from and to, varstates are already set }
  1313. typecheckpass(left);
  1314. typecheckpass(right);
  1315. typecheckpass(t1);
  1316. set_varstate(left,vs_written,[]);
  1317. { loop unrolling }
  1318. if (cs_opt_loopunroll in current_settings.optimizerswitches) and
  1319. assigned(t2) and
  1320. { statements must be error free }
  1321. not(nf_error in t2.flags) then
  1322. begin
  1323. typecheckpass(t2);
  1324. res:=t2.simplify(false);
  1325. if assigned(res) then
  1326. t2:=res;
  1327. res:=unroll_loop(self);
  1328. if assigned(res) then
  1329. begin
  1330. typecheckpass(res);
  1331. result:=res;
  1332. exit;
  1333. end;
  1334. end;
  1335. { Make sure that the loop var and the
  1336. from and to values are compatible types }
  1337. check_ranges(right.fileinfo,right,left.resultdef);
  1338. inserttypeconv(right,left.resultdef);
  1339. check_ranges(t1.fileinfo,t1,left.resultdef);
  1340. inserttypeconv(t1,left.resultdef);
  1341. if assigned(t2) then
  1342. typecheckpass(t2);
  1343. end;
  1344. function tfornode.pass_1 : tnode;
  1345. var
  1346. ifblock,whileblock,loopblock : tblocknode;
  1347. ifstatements,statements,loopstatements : tstatementnode;
  1348. fromtemp,totemp : ttempcreatenode;
  1349. do_loopvar_at_end : Boolean;
  1350. { if the lower and/or upper bound are variable, we need a surrounding if }
  1351. needsifblock : Boolean;
  1352. cond : tnodetype;
  1353. fromexpr : tnode;
  1354. toexpr : tnode;
  1355. { if the upper bound is not constant, it must be store in a temp initially }
  1356. usetotemp : boolean;
  1357. { if the lower bound is not constant, it must be store in a temp before calculating the upper bound }
  1358. usefromtemp : boolean;
  1359. procedure iterate_counter(var s : tstatementnode;fw : boolean);
  1360. begin
  1361. if fw then
  1362. addstatement(s,
  1363. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_succ_x,false,left.getcopy)))
  1364. else
  1365. addstatement(s,
  1366. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_pred_x,false,left.getcopy)));
  1367. end;
  1368. function iterate_counter_func(arg : tnode;fw : boolean) : tnode;
  1369. begin
  1370. if fw then
  1371. result:=cinlinenode.createintern(in_succ_x,false,arg)
  1372. else
  1373. result:=cinlinenode.createintern(in_pred_x,false,arg);
  1374. end;
  1375. begin
  1376. result:=nil;
  1377. expectloc:=LOC_VOID;
  1378. fromtemp:=nil;
  1379. totemp:=nil;
  1380. firstpass(left);
  1381. firstpass(right);
  1382. firstpass(t1);
  1383. if assigned(t2) then
  1384. begin
  1385. firstpass(t2);
  1386. if codegenerror then
  1387. exit;
  1388. end;
  1389. { first set the to value
  1390. because the count var can be in the expression ! }
  1391. do_loopvar_at_end:=(lnf_dont_mind_loopvar_on_exit in loopflags)
  1392. { if the loop is unrolled and there is a jump into the loop,
  1393. then we can't do the trick with incrementing the loop var only at the
  1394. end
  1395. }
  1396. and not(assigned(entrylabel));
  1397. { calculate pointer value and check if changeable and if so
  1398. load into temporary variable }
  1399. if (right.nodetype<>ordconstn) or (t1.nodetype<>ordconstn) then
  1400. begin
  1401. do_loopvar_at_end:=false;
  1402. needsifblock:=true;
  1403. end
  1404. else
  1405. needsifblock:=false;
  1406. { convert the for loop into a while loop }
  1407. result:=internalstatements(statements);
  1408. ifblock:=internalstatements(ifstatements);
  1409. loopblock:=internalstatements(loopstatements);
  1410. usefromtemp:=(might_have_sideeffects(t1) and not(is_const(right))) or (node_complexity(right)>1);
  1411. usetotemp:=not(is_const(t1));
  1412. if needsifblock then
  1413. begin
  1414. { do not generate a temp. for the from node, if it is a const, it can be copied directly since
  1415. no side effect might change it }
  1416. if usefromtemp then
  1417. begin
  1418. fromtemp:=ctempcreatenode.create(right.resultdef,right.resultdef.size,tt_persistent,true);
  1419. { the if block might be optimized out, so we put the deletetempnode after the if-block, however,
  1420. this causes a long life time of the fromtemp. If the final regsync is left away, the reg. allocator
  1421. figures out the needed life time. As their are no loops involved between the uses of the fromtemp,
  1422. this does no hurt }
  1423. fromtemp.includetempflag(ti_no_final_regsync);
  1424. addstatement(statements,fromtemp);
  1425. { while it would be beneficial to fold the initial reverse succ/pred into this assignment, this is
  1426. not possible because it might wrap around and the if check later on goes wrong }
  1427. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(fromtemp),right.getcopy));
  1428. end;
  1429. if usetotemp then
  1430. begin
  1431. totemp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1432. addstatement(statements,totemp);
  1433. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(totemp),t1.getcopy));
  1434. end;
  1435. if usefromtemp then
  1436. begin
  1437. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,ctemprefnode.create(fromtemp)));
  1438. if not(do_loopvar_at_end) then
  1439. iterate_counter(ifstatements,lnf_backward in loopflags);
  1440. end
  1441. else
  1442. begin
  1443. if not(do_loopvar_at_end) then
  1444. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1445. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1446. else
  1447. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1448. end;
  1449. end
  1450. else
  1451. begin
  1452. if not(do_loopvar_at_end) then
  1453. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1454. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1455. else
  1456. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1457. end;
  1458. if assigned(entrylabel) then
  1459. addstatement(ifstatements,cgotonode.create(tlabelnode(entrylabel).labsym));
  1460. if not(do_loopvar_at_end) then
  1461. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1462. { avoid copying t2, it is used only once and it might be big }
  1463. addstatement(loopstatements,t2);
  1464. t2:=nil;
  1465. if do_loopvar_at_end then
  1466. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1467. if do_loopvar_at_end then
  1468. begin
  1469. if lnf_backward in loopflags then
  1470. cond:=ltn
  1471. else
  1472. cond:=gtn;
  1473. end
  1474. else
  1475. begin
  1476. if lnf_backward in loopflags then
  1477. cond:=lten
  1478. else
  1479. cond:=gten;
  1480. end;
  1481. if needsifblock then
  1482. begin
  1483. if usetotemp then
  1484. toexpr:=ctemprefnode.create(totemp)
  1485. else
  1486. toexpr:=t1.getcopy;
  1487. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create(cond,left.getcopy,toexpr),loopblock,false,true));
  1488. if usefromtemp then
  1489. fromexpr:=ctemprefnode.create(fromtemp)
  1490. else
  1491. fromexpr:=right.getcopy;
  1492. if usetotemp then
  1493. toexpr:=ctemprefnode.create(totemp)
  1494. else
  1495. toexpr:=t1.getcopy;
  1496. if lnf_backward in loopflags then
  1497. addstatement(statements,cifnode.create(caddnode.create(gten,
  1498. fromexpr,toexpr),ifblock,nil))
  1499. else
  1500. addstatement(statements,cifnode.create(caddnode.create(lten,
  1501. fromexpr,toexpr),ifblock,nil));
  1502. if usetotemp then
  1503. addstatement(statements,ctempdeletenode.create(totemp));
  1504. if usefromtemp then
  1505. addstatement(statements,ctempdeletenode.create(fromtemp));
  1506. end
  1507. else
  1508. begin
  1509. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create(cond,left.getcopy,t1.getcopy),loopblock,false,true));
  1510. addstatement(statements,ifblock);
  1511. end;
  1512. end;
  1513. {*****************************************************************************
  1514. TEXITNODE
  1515. *****************************************************************************}
  1516. constructor texitnode.create(l:tnode);
  1517. begin
  1518. inherited create(exitn,l);
  1519. if assigned(left) then
  1520. begin
  1521. { add assignment to funcretsym }
  1522. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1523. left:=cassignmentnode.create(
  1524. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1525. left);
  1526. end;
  1527. end;
  1528. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1529. begin
  1530. inherited ppuload(t,ppufile);
  1531. end;
  1532. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1533. begin
  1534. inherited ppuwrite(ppufile);
  1535. end;
  1536. function texitnode.pass_typecheck:tnode;
  1537. var
  1538. pd: tprocdef;
  1539. newstatement : tstatementnode;
  1540. begin
  1541. result:=nil;
  1542. newstatement:=nil;
  1543. if assigned(left) then
  1544. begin
  1545. result:=internalstatements(newstatement);
  1546. addstatement(newstatement,left);
  1547. left:=nil;
  1548. end;
  1549. { if the function result has been migrated to the parentfpstruct,
  1550. we have to load it back to the original location (from which the
  1551. code generator will load it into the function result location),
  1552. because the code to this that we add in tnodeutils.wrap_proc_body()
  1553. gets inserted before the exit label to which this node will jump }
  1554. if (target_info.system in systems_fpnestedstruct) and
  1555. not(nf_internal in flags) then
  1556. begin
  1557. pd:=current_procinfo.procdef;
  1558. if assigned(pd.funcretsym) and
  1559. tabstractnormalvarsym(pd.funcretsym).inparentfpstruct then
  1560. begin
  1561. if not assigned(result) then
  1562. result:=internalstatements(newstatement);
  1563. cnodeutils.load_parentfpstruct_nested_funcret(current_procinfo.procdef,newstatement);
  1564. end;
  1565. end;
  1566. if assigned(result) then
  1567. begin
  1568. addstatement(newstatement,self.getcopy);
  1569. { ensure we don't insert the function result loading code again for
  1570. this node }
  1571. include(newstatement.left.flags,nf_internal);
  1572. end;
  1573. resultdef:=voidtype;
  1574. end;
  1575. function texitnode.pass_1 : tnode;
  1576. begin
  1577. result:=nil;
  1578. expectloc:=LOC_VOID;
  1579. if assigned(left) then
  1580. internalerror(2011052801);
  1581. end;
  1582. {*****************************************************************************
  1583. TBREAKNODE
  1584. *****************************************************************************}
  1585. constructor tbreaknode.create;
  1586. begin
  1587. inherited create(breakn);
  1588. end;
  1589. function tbreaknode.pass_typecheck:tnode;
  1590. begin
  1591. result:=nil;
  1592. resultdef:=voidtype;
  1593. end;
  1594. function tbreaknode.pass_1 : tnode;
  1595. begin
  1596. result:=nil;
  1597. expectloc:=LOC_VOID;
  1598. end;
  1599. {*****************************************************************************
  1600. TCONTINUENODE
  1601. *****************************************************************************}
  1602. constructor tcontinuenode.create;
  1603. begin
  1604. inherited create(continuen);
  1605. end;
  1606. function tcontinuenode.pass_typecheck:tnode;
  1607. begin
  1608. result:=nil;
  1609. resultdef:=voidtype;
  1610. end;
  1611. function tcontinuenode.pass_1 : tnode;
  1612. begin
  1613. result:=nil;
  1614. expectloc:=LOC_VOID;
  1615. end;
  1616. {*****************************************************************************
  1617. TGOTONODE
  1618. *****************************************************************************}
  1619. constructor tgotonode.create(p : tlabelsym);
  1620. begin
  1621. inherited create(goton);
  1622. exceptionblock:=current_exceptblock;
  1623. labelnode:=nil;
  1624. labelsym:=p;
  1625. end;
  1626. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1627. begin
  1628. inherited ppuload(t,ppufile);
  1629. labelnodeidx:=ppufile.getlongint;
  1630. exceptionblock:=ppufile.getbyte;
  1631. end;
  1632. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1633. begin
  1634. inherited ppuwrite(ppufile);
  1635. labelnodeidx:=labelnode.ppuidx;
  1636. ppufile.putlongint(labelnodeidx);
  1637. ppufile.putbyte(exceptionblock);
  1638. end;
  1639. procedure tgotonode.buildderefimpl;
  1640. begin
  1641. inherited buildderefimpl;
  1642. end;
  1643. procedure tgotonode.derefimpl;
  1644. begin
  1645. inherited derefimpl;
  1646. end;
  1647. procedure tgotonode.resolveppuidx;
  1648. begin
  1649. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  1650. if labelnode.nodetype<>labeln then
  1651. internalerror(200809021);
  1652. end;
  1653. function tgotonode.pass_typecheck:tnode;
  1654. begin
  1655. result:=nil;
  1656. resultdef:=voidtype;
  1657. end;
  1658. function tgotonode.pass_1 : tnode;
  1659. var
  1660. p2 : tprocinfo;
  1661. begin
  1662. result:=nil;
  1663. expectloc:=LOC_VOID;
  1664. { The labelnode can already be set when
  1665. this node was copied }
  1666. if not(assigned(labelnode)) then
  1667. begin
  1668. { inner procedure goto? }
  1669. if assigned(labelsym.code) and
  1670. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1671. { generated by the optimizer? }
  1672. not(assigned(labelsym.owner))) then
  1673. labelnode:=tlabelnode(labelsym.code)
  1674. else if ((m_non_local_goto in current_settings.modeswitches) and
  1675. assigned(labelsym.owner)) or
  1676. { nested exits don't need the non local goto switch }
  1677. (labelsym.realname='$nestedexit') then
  1678. begin
  1679. if current_procinfo.procdef.parast.symtablelevel>labelsym.owner.symtablelevel then
  1680. begin
  1681. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  1682. if exceptionblock>0 then
  1683. CGMessage(cg_e_goto_inout_of_exception_block);
  1684. { goto across procedures using exception?
  1685. this is not allowed because we cannot
  1686. easily unwind the exception frame
  1687. stack
  1688. }
  1689. p2:=current_procinfo;
  1690. while true do
  1691. begin
  1692. if (p2.flags*[pi_needs_implicit_finally,pi_uses_exceptions,pi_has_implicit_finally])<>[] then
  1693. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  1694. if labelsym.owner=p2.procdef.localst then
  1695. break;
  1696. p2:=p2.parent
  1697. end;
  1698. if assigned(labelsym.jumpbuf) then
  1699. begin
  1700. labelsym.nonlocal:=true;
  1701. exclude(current_procinfo.procdef.procoptions,po_inline);
  1702. result:=ccallnode.createintern('fpc_longjmp',
  1703. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  1704. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  1705. nil)));
  1706. end
  1707. else
  1708. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1709. end
  1710. else
  1711. CGMessage(cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  1712. end
  1713. else
  1714. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1715. end;
  1716. { check if we don't mess with exception blocks }
  1717. if assigned(labelnode) and
  1718. (exceptionblock<>labelnode.exceptionblock) then
  1719. CGMessage(cg_e_goto_inout_of_exception_block);
  1720. end;
  1721. function tgotonode.dogetcopy : tnode;
  1722. var
  1723. p : tgotonode;
  1724. begin
  1725. p:=tgotonode(inherited dogetcopy);
  1726. p.exceptionblock:=exceptionblock;
  1727. { generate labelnode if not done yet }
  1728. if not(assigned(labelnode)) then
  1729. begin
  1730. if assigned(labelsym) and assigned(labelsym.code) then
  1731. labelnode:=tlabelnode(labelsym.code)
  1732. end;
  1733. p.labelsym:=labelsym;
  1734. if assigned(labelnode) then
  1735. p.labelnode:=tlabelnode(labelnode.dogetcopy)
  1736. else
  1737. begin
  1738. { don't trigger IE when there was already an error, i.e. the
  1739. label is not defined. See tw11763 (PFV) }
  1740. if (errorcount=0) and
  1741. { don't trigger IE if it's a global goto }
  1742. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1743. not(assigned(labelsym.owner))) then
  1744. internalerror(200610291);
  1745. end;
  1746. result:=p;
  1747. end;
  1748. function tgotonode.docompare(p: tnode): boolean;
  1749. begin
  1750. docompare := false;
  1751. end;
  1752. {*****************************************************************************
  1753. TLABELNODE
  1754. *****************************************************************************}
  1755. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  1756. begin
  1757. inherited create(labeln,l);
  1758. exceptionblock:=current_exceptblock;
  1759. labsym:=alabsym;
  1760. { Register labelnode in labelsym }
  1761. labsym.code:=self;
  1762. end;
  1763. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1764. begin
  1765. inherited ppuload(t,ppufile);
  1766. exceptionblock:=ppufile.getbyte;
  1767. end;
  1768. destructor tlabelnode.destroy;
  1769. begin
  1770. { Remove reference in labelsym, this is to prevent
  1771. goto's to this label }
  1772. if assigned(labsym) and (labsym.code=pointer(self)) then
  1773. labsym.code:=nil;
  1774. inherited destroy;
  1775. end;
  1776. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  1777. begin
  1778. inherited ppuwrite(ppufile);
  1779. ppufile.putbyte(exceptionblock);
  1780. end;
  1781. procedure tlabelnode.buildderefimpl;
  1782. begin
  1783. inherited buildderefimpl;
  1784. end;
  1785. procedure tlabelnode.derefimpl;
  1786. begin
  1787. inherited derefimpl;
  1788. end;
  1789. function tlabelnode.pass_typecheck:tnode;
  1790. begin
  1791. result:=nil;
  1792. { left could still be unassigned }
  1793. if assigned(left) then
  1794. typecheckpass(left);
  1795. resultdef:=voidtype;
  1796. end;
  1797. function tlabelnode.pass_1 : tnode;
  1798. begin
  1799. result:=nil;
  1800. expectloc:=LOC_VOID;
  1801. include(current_procinfo.flags,pi_has_label);
  1802. if assigned(labsym) and labsym.nonlocal then
  1803. begin
  1804. include(current_procinfo.flags,pi_has_interproclabel);
  1805. exclude(current_procinfo.procdef.procoptions,po_inline);
  1806. end;
  1807. if assigned(left) then
  1808. firstpass(left);
  1809. if (m_non_local_goto in current_settings.modeswitches) and
  1810. { the owner can be Nil for internal labels }
  1811. assigned(labsym.owner) and
  1812. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  1813. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  1814. end;
  1815. function tlabelnode.dogetcopy : tnode;
  1816. begin
  1817. if not(assigned(copiedto)) then
  1818. copiedto:=tlabelnode(inherited dogetcopy);
  1819. copiedto.exceptionblock:=exceptionblock;
  1820. result:=copiedto;
  1821. end;
  1822. function tlabelnode.docompare(p: tnode): boolean;
  1823. begin
  1824. docompare := false;
  1825. end;
  1826. {*****************************************************************************
  1827. TRAISENODE
  1828. *****************************************************************************}
  1829. constructor traisenode.create(l,taddr,tframe:tnode);
  1830. begin
  1831. inherited create(raisen,l,taddr,tframe);
  1832. end;
  1833. function traisenode.pass_typecheck:tnode;
  1834. begin
  1835. result:=nil;
  1836. resultdef:=voidtype;
  1837. if assigned(left) then
  1838. begin
  1839. { first para must be a _class_ }
  1840. typecheckpass(left);
  1841. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1842. if codegenerror then
  1843. exit;
  1844. if not is_class(left.resultdef) and
  1845. not is_javaclass(left.resultdef) then
  1846. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  1847. { insert needed typeconvs for addr,frame }
  1848. if assigned(right) then
  1849. begin
  1850. { addr }
  1851. typecheckpass(right);
  1852. inserttypeconv(right,voidcodepointertype);
  1853. { frame }
  1854. if assigned(third) then
  1855. begin
  1856. typecheckpass(third);
  1857. inserttypeconv(third,voidpointertype);
  1858. end;
  1859. end;
  1860. end;
  1861. end;
  1862. function traisenode.pass_1 : tnode;
  1863. var
  1864. statements : tstatementnode;
  1865. current_addr : tlabelnode;
  1866. raisenode : tcallnode;
  1867. begin
  1868. result:=internalstatements(statements);
  1869. if assigned(left) then
  1870. begin
  1871. { first para must be a class }
  1872. firstpass(left);
  1873. { insert needed typeconvs for addr,frame }
  1874. if assigned(right) then
  1875. begin
  1876. { addr }
  1877. firstpass(right);
  1878. { frame }
  1879. if assigned(third) then
  1880. firstpass(third)
  1881. else
  1882. third:=cpointerconstnode.Create(0,voidpointertype);
  1883. end
  1884. else
  1885. begin
  1886. third:=cinlinenode.create(in_get_frame,false,nil);
  1887. current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  1888. addstatement(statements,current_addr);
  1889. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  1890. end;
  1891. raisenode:=ccallnode.createintern('fpc_raiseexception',
  1892. ccallparanode.create(third,
  1893. ccallparanode.create(right,
  1894. ccallparanode.create(left,nil)))
  1895. );
  1896. include(raisenode.callnodeflags,cnf_call_never_returns);
  1897. addstatement(statements,raisenode);
  1898. end
  1899. else
  1900. begin
  1901. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  1902. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  1903. include(raisenode.callnodeflags,cnf_call_never_returns);
  1904. addstatement(statements,raisenode);
  1905. end;
  1906. left:=nil;
  1907. right:=nil;
  1908. third:=nil;
  1909. end;
  1910. {*****************************************************************************
  1911. TTRYEXCEPTNODE
  1912. *****************************************************************************}
  1913. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1914. begin
  1915. inherited create(tryexceptn,l,r,_t1,nil);
  1916. end;
  1917. function ttryexceptnode.pass_typecheck:tnode;
  1918. begin
  1919. result:=nil;
  1920. typecheckpass(left);
  1921. { on statements }
  1922. if assigned(right) then
  1923. typecheckpass(right);
  1924. { else block }
  1925. if assigned(t1) then
  1926. typecheckpass(t1);
  1927. resultdef:=voidtype;
  1928. end;
  1929. function ttryexceptnode.pass_1 : tnode;
  1930. begin
  1931. result:=nil;
  1932. expectloc:=LOC_VOID;
  1933. firstpass(left);
  1934. { on statements }
  1935. if assigned(right) then
  1936. firstpass(right);
  1937. { else block }
  1938. if assigned(t1) then
  1939. firstpass(t1);
  1940. include(current_procinfo.flags,pi_do_call);
  1941. include(current_procinfo.flags,pi_uses_exceptions);
  1942. adjust_estimated_stack_size;
  1943. end;
  1944. function ttryexceptnode.simplify(forinline: boolean): tnode;
  1945. begin
  1946. result:=nil;
  1947. { empty try -> can never raise exception -> do nothing }
  1948. if has_no_code(left) then
  1949. result:=cnothingnode.create;
  1950. end;
  1951. procedure ttryexceptnode.adjust_estimated_stack_size;
  1952. begin
  1953. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size*2);
  1954. end;
  1955. {*****************************************************************************
  1956. TTRYFINALLYNODE
  1957. *****************************************************************************}
  1958. constructor ttryfinallynode.create(l,r:tnode);
  1959. begin
  1960. inherited create(tryfinallyn,l,r,nil,nil);
  1961. implicitframe:=false;
  1962. end;
  1963. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1964. begin
  1965. inherited create(tryfinallyn,l,r,_t1,nil);
  1966. implicitframe:=true;
  1967. end;
  1968. function ttryfinallynode.pass_typecheck:tnode;
  1969. begin
  1970. result:=nil;
  1971. resultdef:=voidtype;
  1972. typecheckpass(left);
  1973. // "try block" is "used"? (JM)
  1974. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  1975. typecheckpass(right);
  1976. // "except block" is "used"? (JM)
  1977. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  1978. { special finally block only executed when there was an exception }
  1979. if assigned(t1) then
  1980. begin
  1981. typecheckpass(t1);
  1982. // "finally block" is "used"? (JM)
  1983. set_varstate(t1,vs_readwritten,[vsf_must_be_valid]);
  1984. end;
  1985. end;
  1986. function ttryfinallynode.pass_1 : tnode;
  1987. begin
  1988. result:=nil;
  1989. expectloc:=LOC_VOID;
  1990. firstpass(left);
  1991. firstpass(right);
  1992. if assigned(t1) then
  1993. firstpass(t1);
  1994. include(current_procinfo.flags,pi_do_call);
  1995. { pi_uses_exceptions is an information for the optimizer and it
  1996. is only interested in exceptions if they appear inside the body,
  1997. so ignore implicit frames when setting the flag }
  1998. if not(implicitframe) then
  1999. include(current_procinfo.flags,pi_uses_exceptions);
  2000. adjust_estimated_stack_size;
  2001. end;
  2002. function ttryfinallynode.simplify(forinline : boolean): tnode;
  2003. begin
  2004. result:=nil;
  2005. { if the try contains no code, we can kill
  2006. the try and except and return only the
  2007. finally part }
  2008. if has_no_code(left) then
  2009. begin
  2010. result:=right;
  2011. right:=nil;
  2012. end;
  2013. end;
  2014. var
  2015. seq: longint=0;
  2016. function ttryfinallynode.create_finalizer_procdef: tprocdef;
  2017. var
  2018. st:TSymTable;
  2019. checkstack: psymtablestackitem;
  2020. oldsymtablestack: tsymtablestack;
  2021. sym:tprocsym;
  2022. begin
  2023. { get actual procedure symtable (skip withsymtables, etc.) }
  2024. st:=nil;
  2025. checkstack:=symtablestack.stack;
  2026. while assigned(checkstack) do
  2027. begin
  2028. st:=checkstack^.symtable;
  2029. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  2030. break;
  2031. checkstack:=checkstack^.next;
  2032. end;
  2033. { Create a nested procedure, even from main_program_level.
  2034. Furthermore, force procdef and procsym into the same symtable
  2035. (by default, defs are registered with symtablestack.top which may be
  2036. something temporary like exceptsymtable - in that case, procdef can be
  2037. destroyed before procsym, leaving invalid pointers). }
  2038. oldsymtablestack:=symtablestack;
  2039. symtablestack:=nil;
  2040. result:=cprocdef.create(max(normal_function_level,st.symtablelevel)+1,true);
  2041. symtablestack:=oldsymtablestack;
  2042. st.insertdef(result);
  2043. result.struct:=current_procinfo.procdef.struct;
  2044. { tabstractprocdef constructor sets po_delphi_nested_cc whenever
  2045. nested procvars modeswitch is active. We must be independent of this switch. }
  2046. exclude(result.procoptions,po_delphi_nested_cc);
  2047. result.proctypeoption:=potype_exceptfilter;
  2048. handle_calling_convention(result);
  2049. sym:=cprocsym.create('$fin$'+tostr(seq));
  2050. st.insert(sym);
  2051. inc(seq);
  2052. result.procsym:=sym;
  2053. proc_add_definition(result);
  2054. result.forwarddef:=false;
  2055. result.aliasnames.insert(result.mangledname);
  2056. end;
  2057. procedure ttryfinallynode.adjust_estimated_stack_size;
  2058. begin
  2059. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size);
  2060. end;
  2061. {*****************************************************************************
  2062. TONNODE
  2063. *****************************************************************************}
  2064. constructor tonnode.create(l,r:tnode);
  2065. begin
  2066. inherited create(onn,l,r);
  2067. excepTSymtable:=nil;
  2068. excepttype:=nil;
  2069. end;
  2070. destructor tonnode.destroy;
  2071. begin
  2072. { copied nodes don't need to release the symtable }
  2073. if assigned(excepTSymtable) then
  2074. excepTSymtable.free;
  2075. inherited destroy;
  2076. end;
  2077. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2078. begin
  2079. inherited ppuload(t,ppufile);
  2080. excepTSymtable:=nil;
  2081. excepttype:=nil;
  2082. end;
  2083. function tonnode.dogetcopy : tnode;
  2084. var
  2085. n : tonnode;
  2086. begin
  2087. n:=tonnode(inherited dogetcopy);
  2088. if assigned(exceptsymtable) then
  2089. n.exceptsymtable:=exceptsymtable.getcopy
  2090. else
  2091. n.exceptsymtable:=nil;
  2092. n.excepttype:=excepttype;
  2093. result:=n;
  2094. end;
  2095. function tonnode.pass_typecheck:tnode;
  2096. begin
  2097. result:=nil;
  2098. resultdef:=voidtype;
  2099. if not is_class(excepttype) and
  2100. not is_javaclass(excepttype) then
  2101. CGMessage1(type_e_class_type_expected,excepttype.typename);
  2102. if assigned(left) then
  2103. typecheckpass(left);
  2104. if assigned(right) then
  2105. typecheckpass(right);
  2106. end;
  2107. function tonnode.pass_1 : tnode;
  2108. begin
  2109. result:=nil;
  2110. include(current_procinfo.flags,pi_do_call);
  2111. { Loads exception class VMT, therefore may need GOT
  2112. (generic code only; descendants may need to avoid this check) }
  2113. if (cs_create_pic in current_settings.moduleswitches) and
  2114. (tf_pic_uses_got in target_info.flags) then
  2115. include(current_procinfo.flags,pi_needs_got);
  2116. expectloc:=LOC_VOID;
  2117. if assigned(left) then
  2118. firstpass(left);
  2119. if assigned(right) then
  2120. firstpass(right);
  2121. end;
  2122. function tonnode.docompare(p: tnode): boolean;
  2123. begin
  2124. docompare := false;
  2125. end;
  2126. end.