nflw.pas 85 KB

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