nflw.pas 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453
  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:
  842. result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  843. arraydef:
  844. result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  845. setdef:
  846. result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  847. undefineddef:
  848. result:=cnothingnode.create;
  849. else
  850. begin
  851. result:=cerrornode.create;
  852. hloopvar.free;
  853. hloopbody.free;
  854. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  855. end;
  856. end;
  857. end;
  858. end;
  859. end;
  860. current_filepos:=storefilepos;
  861. end;
  862. {****************************************************************************
  863. TLOOPNODE
  864. *****************************************************************************}
  865. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  866. begin
  867. inherited create(tt,l,r);
  868. t1:=_t1;
  869. t2:=_t2;
  870. fileinfo:=l.fileinfo;
  871. end;
  872. destructor tloopnode.destroy;
  873. begin
  874. t1.free;
  875. t2.free;
  876. inherited destroy;
  877. end;
  878. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  879. begin
  880. inherited ppuload(t,ppufile);
  881. t1:=ppuloadnode(ppufile);
  882. t2:=ppuloadnode(ppufile);
  883. ppufile.getsmallset(loopflags);
  884. end;
  885. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  886. begin
  887. inherited ppuwrite(ppufile);
  888. ppuwritenode(ppufile,t1);
  889. ppuwritenode(ppufile,t2);
  890. ppufile.putsmallset(loopflags);
  891. end;
  892. procedure tloopnode.buildderefimpl;
  893. begin
  894. inherited buildderefimpl;
  895. if assigned(t1) then
  896. t1.buildderefimpl;
  897. if assigned(t2) then
  898. t2.buildderefimpl;
  899. end;
  900. procedure tloopnode.derefimpl;
  901. begin
  902. inherited derefimpl;
  903. if assigned(t1) then
  904. t1.derefimpl;
  905. if assigned(t2) then
  906. t2.derefimpl;
  907. end;
  908. function tloopnode.dogetcopy : tnode;
  909. var
  910. p : tloopnode;
  911. begin
  912. p:=tloopnode(inherited dogetcopy);
  913. if assigned(t1) then
  914. p.t1:=t1.dogetcopy
  915. else
  916. p.t1:=nil;
  917. if assigned(t2) then
  918. p.t2:=t2.dogetcopy
  919. else
  920. p.t2:=nil;
  921. p.loopflags:=loopflags;
  922. dogetcopy:=p;
  923. end;
  924. procedure tloopnode.insertintolist(l : tnodelist);
  925. begin
  926. end;
  927. procedure tloopnode.printnodetree(var t:text);
  928. begin
  929. write(t,printnodeindention,'(');
  930. printnodeindent;
  931. printnodeinfo(t);
  932. writeln(t);
  933. printnode(t,left);
  934. printnode(t,right);
  935. printnode(t,t1);
  936. printnode(t,t2);
  937. printnodeunindent;
  938. writeln(t,printnodeindention,')');
  939. end;
  940. function tloopnode.docompare(p: tnode): boolean;
  941. begin
  942. docompare :=
  943. inherited docompare(p) and
  944. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  945. t1.isequal(tloopnode(p).t1) and
  946. t2.isequal(tloopnode(p).t2);
  947. end;
  948. {****************************************************************************
  949. TWHILEREPEATNODE
  950. *****************************************************************************}
  951. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  952. begin
  953. inherited create(whilerepeatn,l,r,nil,nil);
  954. if tab then
  955. include(loopflags, lnf_testatbegin);
  956. if cn then
  957. include(loopflags,lnf_checknegate);
  958. end;
  959. function twhilerepeatnode.pass_typecheck:tnode;
  960. var
  961. t:Tunarynode;
  962. begin
  963. result:=nil;
  964. resultdef:=voidtype;
  965. typecheckpass(left);
  966. { tp procvar support }
  967. maybe_call_procvar(left,true);
  968. {A not node can be removed.}
  969. if left.nodetype=notn then
  970. begin
  971. t:=Tunarynode(left);
  972. left:=Tunarynode(left).left;
  973. t.left:=nil;
  974. t.destroy;
  975. {Symdif operator, in case you are wondering:}
  976. loopflags:=loopflags >< [lnf_checknegate];
  977. end;
  978. { loop instruction }
  979. if assigned(right) then
  980. typecheckpass(right);
  981. set_varstate(left,vs_read,[vsf_must_be_valid]);
  982. if codegenerror then
  983. exit;
  984. if not(is_boolean(left.resultdef)) and
  985. not(is_typeparam(left.resultdef)) then
  986. inserttypeconv(left,pasbool1type);
  987. { Give warnings for code that will never be executed for
  988. while false do }
  989. if (lnf_testatbegin in loopflags) and
  990. (left.nodetype=ordconstn) and
  991. (tordconstnode(left).value.uvalue=0) and
  992. assigned(right) then
  993. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  994. end;
  995. {$ifdef prefetchnext}
  996. type
  997. passignmentquery = ^tassignmentquery;
  998. tassignmentquery = record
  999. towhat: tnode;
  1000. source: tassignmentnode;
  1001. statementcount: cardinal;
  1002. end;
  1003. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  1004. var
  1005. query: passignmentquery absolute arg;
  1006. temp, prederef: tnode;
  1007. begin
  1008. result := fen_norecurse_false;
  1009. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  1010. inc(query^.statementcount);
  1011. { make sure there's something else in the loop besides going to the }
  1012. { next item }
  1013. if (query^.statementcount > 1) and
  1014. (n.nodetype = assignn) then
  1015. begin
  1016. { skip type conversions of assignment target }
  1017. temp := tassignmentnode(n).left;
  1018. while (temp.nodetype = typeconvn) do
  1019. temp := ttypeconvnode(temp).left;
  1020. { assignment to x of the while assigned(x) check? }
  1021. if not(temp.isequal(query^.towhat)) then
  1022. exit;
  1023. { right hand side of assignment dereferenced field of }
  1024. { x? (no derefn in case of class) }
  1025. temp := tassignmentnode(n).right;
  1026. while (temp.nodetype = typeconvn) do
  1027. temp := ttypeconvnode(temp).left;
  1028. if (temp.nodetype <> subscriptn) then
  1029. exit;
  1030. prederef := tsubscriptnode(temp).left;
  1031. temp := prederef;
  1032. while (temp.nodetype = typeconvn) do
  1033. temp := ttypeconvnode(temp).left;
  1034. { see tests/test/prefetch1.pp }
  1035. if (temp.nodetype = derefn) then
  1036. temp := tderefnode(temp).left
  1037. else
  1038. temp := prederef;
  1039. if temp.isequal(query^.towhat) then
  1040. begin
  1041. query^.source := tassignmentnode(n);
  1042. result := fen_norecurse_true;
  1043. end
  1044. end
  1045. { don't check nodes which can't contain an assignment or whose }
  1046. { final assignment can vary a lot }
  1047. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1048. result := fen_false;
  1049. end;
  1050. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1051. var
  1052. query: tassignmentquery;
  1053. begin
  1054. query.towhat := towhat;
  1055. query.source := nil;
  1056. query.statementcount := 0;
  1057. if foreachnodestatic(where,@checkassignment,@query) then
  1058. result := query.source
  1059. else
  1060. result := nil;
  1061. end;
  1062. {$endif prefetchnext}
  1063. function twhilerepeatnode.pass_1 : tnode;
  1064. {$ifdef prefetchnext}
  1065. var
  1066. runnernode, prefetchcode: tnode;
  1067. assignmentnode: tassignmentnode;
  1068. prefetchstatements: tstatementnode;
  1069. {$endif prefetchnext}
  1070. begin
  1071. result:=nil;
  1072. expectloc:=LOC_VOID;
  1073. firstpass(left);
  1074. if codegenerror then
  1075. exit;
  1076. { loop instruction }
  1077. if assigned(right) then
  1078. begin
  1079. firstpass(right);
  1080. if codegenerror then
  1081. exit;
  1082. end;
  1083. {$ifdef prefetchnext}
  1084. { do at the end so all complex typeconversions are already }
  1085. { converted to calln's }
  1086. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1087. (lnf_testatbegin in loopflags) then
  1088. begin
  1089. { get first component of the while check }
  1090. runnernode := left;
  1091. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1092. runnernode := tunarynode(runnernode).left;
  1093. { is it an assigned(x) check? }
  1094. if ((runnernode.nodetype = inlinen) and
  1095. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1096. ((runnernode.nodetype = unequaln) and
  1097. (taddnode(runnernode).right.nodetype = niln)) then
  1098. begin
  1099. runnernode := tunarynode(runnernode).left;
  1100. { in case of in_assigned_x, there's a callparan in between }
  1101. if (runnernode.nodetype = callparan) then
  1102. runnernode := tcallparanode(runnernode).left;
  1103. while (runnernode.nodetype = typeconvn) do
  1104. runnernode := ttypeconvnode(runnernode).left;
  1105. { is there an "x := x(^).somefield"? }
  1106. assignmentnode := findassignment(right,runnernode);
  1107. if assigned(assignmentnode) then
  1108. begin
  1109. prefetchcode := internalstatements(prefetchstatements);
  1110. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1111. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1112. addstatement(prefetchstatements,right);
  1113. right := prefetchcode;
  1114. typecheckpass(right);
  1115. end;
  1116. end;
  1117. end;
  1118. {$endif prefetchnext}
  1119. end;
  1120. {$ifdef state_tracking}
  1121. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1122. var condition:Tnode;
  1123. code:Tnode;
  1124. done:boolean;
  1125. value:boolean;
  1126. change:boolean;
  1127. firsttest:boolean;
  1128. factval:Tnode;
  1129. begin
  1130. track_state_pass:=false;
  1131. done:=false;
  1132. firsttest:=true;
  1133. {For repeat until statements, first do a pass through the code.}
  1134. if not(lnf_testatbegin in flags) then
  1135. begin
  1136. code:=right.getcopy;
  1137. if code.track_state_pass(exec_known) then
  1138. track_state_pass:=true;
  1139. code.destroy;
  1140. end;
  1141. repeat
  1142. condition:=left.getcopy;
  1143. code:=right.getcopy;
  1144. change:=condition.track_state_pass(exec_known);
  1145. factval:=aktstate.find_fact(left);
  1146. if factval<>nil then
  1147. begin
  1148. condition.destroy;
  1149. condition:=factval.getcopy;
  1150. change:=true;
  1151. end;
  1152. if change then
  1153. begin
  1154. track_state_pass:=true;
  1155. {Force new resultdef pass.}
  1156. condition.resultdef:=nil;
  1157. do_typecheckpass(condition);
  1158. end;
  1159. if is_constboolnode(condition) then
  1160. begin
  1161. {Try to turn a while loop into a repeat loop.}
  1162. if firsttest then
  1163. exclude(flags,testatbegin);
  1164. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1165. if value then
  1166. begin
  1167. if code.track_state_pass(exec_known) then
  1168. track_state_pass:=true;
  1169. end
  1170. else
  1171. done:=true;
  1172. end
  1173. else
  1174. begin
  1175. {Remove any modified variables from the state.}
  1176. code.track_state_pass(false);
  1177. done:=true;
  1178. end;
  1179. code.destroy;
  1180. condition.destroy;
  1181. firsttest:=false;
  1182. until done;
  1183. {The loop condition is also known, for example:
  1184. while i<10 do
  1185. begin
  1186. ...
  1187. end;
  1188. When the loop is done, we do know that i<10 = false.
  1189. }
  1190. condition:=left.getcopy;
  1191. if condition.track_state_pass(exec_known) then
  1192. begin
  1193. track_state_pass:=true;
  1194. {Force new resultdef pass.}
  1195. condition.resultdef:=nil;
  1196. do_typecheckpass(condition);
  1197. end;
  1198. if not is_constboolnode(condition) then
  1199. aktstate.store_fact(condition,
  1200. cordconstnode.create(byte(checknegate),pasbool1type,true))
  1201. else
  1202. condition.destroy;
  1203. end;
  1204. {$endif}
  1205. {*****************************************************************************
  1206. TIFNODE
  1207. *****************************************************************************}
  1208. constructor tifnode.create(l,r,_t1 : tnode);
  1209. begin
  1210. inherited create(ifn,l,r,_t1,nil);
  1211. end;
  1212. constructor tifnode.create_internal(l,r,_t1 : tnode);
  1213. begin
  1214. create(l,r,_t1);
  1215. include(flags,nf_internal);
  1216. end;
  1217. function tifnode.internalsimplify(warn: boolean) : tnode;
  1218. begin
  1219. result:=nil;
  1220. { optimize constant expressions }
  1221. if (left.nodetype=ordconstn) then
  1222. begin
  1223. if tordconstnode(left).value.uvalue<>0 then
  1224. begin
  1225. if assigned(right) then
  1226. result:=right
  1227. else
  1228. result:=cnothingnode.create;
  1229. right:=nil;
  1230. if warn and assigned(t1) then
  1231. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1232. end
  1233. else
  1234. begin
  1235. if assigned(t1) then
  1236. result:=t1
  1237. else
  1238. result:=cnothingnode.create;
  1239. t1:=nil;
  1240. if warn and assigned(right) then
  1241. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1242. end;
  1243. end;
  1244. end;
  1245. function tifnode.simplify(forinline : boolean) : tnode;
  1246. begin
  1247. result:=internalsimplify(false);
  1248. end;
  1249. function tifnode.pass_typecheck:tnode;
  1250. begin
  1251. result:=nil;
  1252. resultdef:=voidtype;
  1253. typecheckpass(left);
  1254. { tp procvar support }
  1255. maybe_call_procvar(left,true);
  1256. { if path }
  1257. if assigned(right) then
  1258. typecheckpass(right);
  1259. { else path }
  1260. if assigned(t1) then
  1261. typecheckpass(t1);
  1262. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1263. if codegenerror then
  1264. exit;
  1265. if not(is_boolean(left.resultdef)) and
  1266. not(is_typeparam(left.resultdef)) then
  1267. inserttypeconv(left,pasbool1type);
  1268. result:=internalsimplify(not(nf_internal in flags));
  1269. end;
  1270. function tifnode.pass_1 : tnode;
  1271. begin
  1272. result:=nil;
  1273. expectloc:=LOC_VOID;
  1274. firstpass(left);
  1275. { if path }
  1276. if assigned(right) then
  1277. firstpass(right);
  1278. { else path }
  1279. if assigned(t1) then
  1280. firstpass(t1);
  1281. { leave if we've got an error in one of the paths }
  1282. if codegenerror then
  1283. exit;
  1284. end;
  1285. {*****************************************************************************
  1286. TFORNODE
  1287. *****************************************************************************}
  1288. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1289. begin
  1290. inherited create(forn,l,r,_t1,_t2);
  1291. if back then
  1292. include(loopflags,lnf_backward);
  1293. include(loopflags,lnf_testatbegin);
  1294. end;
  1295. function tfornode.simplify(forinline : boolean) : tnode;
  1296. begin
  1297. result:=nil;
  1298. { Can we spare the first comparision? }
  1299. if (t1.nodetype=ordconstn) and
  1300. (right.nodetype=ordconstn) and
  1301. (
  1302. (
  1303. (lnf_backward in loopflags) and
  1304. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1305. ) or
  1306. (
  1307. not(lnf_backward in loopflags) and
  1308. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1309. )
  1310. ) then
  1311. exclude(loopflags,lnf_testatbegin);
  1312. if (t1.nodetype=ordconstn) and
  1313. (right.nodetype=ordconstn) and
  1314. (
  1315. (
  1316. (lnf_backward in loopflags) and
  1317. (tordconstnode(right).value<tordconstnode(t1).value)
  1318. ) or
  1319. (
  1320. not(lnf_backward in loopflags) and
  1321. (tordconstnode(right).value>tordconstnode(t1).value)
  1322. )
  1323. ) then
  1324. result:=cnothingnode.create;
  1325. end;
  1326. function tfornode.pass_typecheck:tnode;
  1327. var
  1328. res : tnode;
  1329. rangedef: tdef;
  1330. begin
  1331. result:=nil;
  1332. resultdef:=voidtype;
  1333. { process the loopvar, from and to, varstates are already set }
  1334. typecheckpass(left);
  1335. typecheckpass(right);
  1336. typecheckpass(t1);
  1337. set_varstate(left,vs_written,[]);
  1338. { loop unrolling }
  1339. if (cs_opt_loopunroll in current_settings.optimizerswitches) and
  1340. assigned(t2) and
  1341. { statements must be error free }
  1342. not(nf_error in t2.flags) then
  1343. begin
  1344. typecheckpass(t2);
  1345. res:=t2.simplify(false);
  1346. if assigned(res) then
  1347. t2:=res;
  1348. res:=unroll_loop(self);
  1349. if assigned(res) then
  1350. begin
  1351. typecheckpass(res);
  1352. result:=res;
  1353. exit;
  1354. end;
  1355. end;
  1356. { Make sure that the loop var and the
  1357. from and to values are compatible types }
  1358. if not(m_iso in current_settings.modeswitches) then
  1359. rangedef:=left.resultdef
  1360. else
  1361. rangedef:=get_iso_range_type(left.resultdef);
  1362. check_ranges(right.fileinfo,right,rangedef);
  1363. inserttypeconv(right,rangedef);
  1364. check_ranges(t1.fileinfo,t1,rangedef);
  1365. inserttypeconv(t1,rangedef);
  1366. if assigned(t2) then
  1367. typecheckpass(t2);
  1368. result:=simplify(false);
  1369. end;
  1370. function tfornode.pass_1 : tnode;
  1371. var
  1372. ifblock,loopblock : tblocknode;
  1373. ifstatements,statements,loopstatements : tstatementnode;
  1374. fromtemp,totemp : ttempcreatenode;
  1375. do_loopvar_at_end : Boolean;
  1376. { if the lower and/or upper bound are variable, we need a surrounding if }
  1377. needsifblock : Boolean;
  1378. cond : tnodetype;
  1379. fromexpr : tnode;
  1380. toexpr : tnode;
  1381. { if the upper bound is not constant, it must be store in a temp initially }
  1382. usetotemp : boolean;
  1383. { if the lower bound is not constant, it must be store in a temp before calculating the upper bound }
  1384. usefromtemp : boolean;
  1385. procedure iterate_counter(var s : tstatementnode;fw : boolean);
  1386. begin
  1387. if fw then
  1388. addstatement(s,
  1389. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_succ_x,false,left.getcopy)))
  1390. else
  1391. addstatement(s,
  1392. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_pred_x,false,left.getcopy)));
  1393. end;
  1394. function iterate_counter_func(arg : tnode;fw : boolean) : tnode;
  1395. begin
  1396. if fw then
  1397. result:=cinlinenode.createintern(in_succ_x,false,arg)
  1398. else
  1399. result:=cinlinenode.createintern(in_pred_x,false,arg);
  1400. end;
  1401. begin
  1402. result:=nil;
  1403. expectloc:=LOC_VOID;
  1404. fromtemp:=nil;
  1405. totemp:=nil;
  1406. firstpass(left);
  1407. firstpass(right);
  1408. firstpass(t1);
  1409. if assigned(t2) then
  1410. begin
  1411. firstpass(t2);
  1412. if codegenerror then
  1413. exit;
  1414. end;
  1415. { first set the to value
  1416. because the count var can be in the expression ! }
  1417. do_loopvar_at_end:=(lnf_dont_mind_loopvar_on_exit in loopflags)
  1418. { if the loop is unrolled and there is a jump into the loop,
  1419. then we can't do the trick with incrementing the loop var only at the
  1420. end
  1421. }
  1422. and not(assigned(entrylabel));
  1423. { calculate pointer value and check if changeable and if so
  1424. load into temporary variable }
  1425. if (right.nodetype<>ordconstn) or (t1.nodetype<>ordconstn) then
  1426. begin
  1427. do_loopvar_at_end:=false;
  1428. needsifblock:=true;
  1429. end
  1430. else
  1431. needsifblock:=false;
  1432. { convert the for loop into a while loop }
  1433. result:=internalstatements(statements);
  1434. ifblock:=internalstatements(ifstatements);
  1435. loopblock:=internalstatements(loopstatements);
  1436. usefromtemp:=(might_have_sideeffects(t1) and not(is_const(right))) or (node_complexity(right)>1);
  1437. usetotemp:=not(is_const(t1));
  1438. if needsifblock then
  1439. begin
  1440. { do not generate a temp. for the from node, if it is a const, it can be copied directly since
  1441. no side effect might change it }
  1442. if usefromtemp then
  1443. begin
  1444. fromtemp:=ctempcreatenode.create(right.resultdef,right.resultdef.size,tt_persistent,true);
  1445. { the if block might be optimized out, so we put the deletetempnode after the if-block, however,
  1446. this causes a long life time of the fromtemp. If the final regsync is left away, the reg. allocator
  1447. figures out the needed life time. As their are no loops involved between the uses of the fromtemp,
  1448. this does no hurt }
  1449. fromtemp.includetempflag(ti_no_final_regsync);
  1450. addstatement(statements,fromtemp);
  1451. { while it would be beneficial to fold the initial reverse succ/pred into this assignment, this is
  1452. not possible because it might wrap around and the if check later on goes wrong }
  1453. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(fromtemp),right.getcopy));
  1454. end;
  1455. if usetotemp then
  1456. begin
  1457. totemp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1458. addstatement(statements,totemp);
  1459. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(totemp),t1.getcopy));
  1460. end;
  1461. if usefromtemp then
  1462. begin
  1463. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,ctemprefnode.create(fromtemp)));
  1464. if not(do_loopvar_at_end) then
  1465. iterate_counter(ifstatements,lnf_backward in loopflags);
  1466. end
  1467. else
  1468. begin
  1469. if not(do_loopvar_at_end) then
  1470. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1471. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1472. else
  1473. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1474. end;
  1475. end
  1476. else
  1477. begin
  1478. if not(do_loopvar_at_end) then
  1479. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1480. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1481. else
  1482. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1483. end;
  1484. if assigned(entrylabel) then
  1485. addstatement(ifstatements,cgotonode.create(tlabelnode(entrylabel).labsym));
  1486. if not(do_loopvar_at_end) then
  1487. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1488. { avoid copying t2, it is used only once and it might be big }
  1489. addstatement(loopstatements,t2);
  1490. t2:=nil;
  1491. if do_loopvar_at_end then
  1492. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1493. if do_loopvar_at_end then
  1494. begin
  1495. if lnf_backward in loopflags then
  1496. cond:=ltn
  1497. else
  1498. cond:=gtn;
  1499. end
  1500. else
  1501. begin
  1502. if lnf_backward in loopflags then
  1503. cond:=lten
  1504. else
  1505. cond:=gten;
  1506. end;
  1507. if needsifblock then
  1508. begin
  1509. if usetotemp then
  1510. toexpr:=ctemprefnode.create(totemp)
  1511. else
  1512. toexpr:=t1.getcopy;
  1513. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,left.getcopy,toexpr),loopblock,false,true));
  1514. if usefromtemp then
  1515. fromexpr:=ctemprefnode.create(fromtemp)
  1516. else
  1517. fromexpr:=right.getcopy;
  1518. if usetotemp then
  1519. toexpr:=ctemprefnode.create(totemp)
  1520. else
  1521. toexpr:=t1.getcopy;
  1522. if lnf_backward in loopflags then
  1523. addstatement(statements,cifnode.create(caddnode.create_internal(gten,
  1524. fromexpr,toexpr),ifblock,nil))
  1525. else
  1526. addstatement(statements,cifnode.create(caddnode.create_internal(lten,
  1527. fromexpr,toexpr),ifblock,nil));
  1528. if usetotemp then
  1529. addstatement(statements,ctempdeletenode.create(totemp));
  1530. if usefromtemp then
  1531. addstatement(statements,ctempdeletenode.create(fromtemp));
  1532. end
  1533. else
  1534. begin
  1535. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,left.getcopy,t1.getcopy),loopblock,false,true));
  1536. addstatement(statements,ifblock);
  1537. end;
  1538. end;
  1539. {*****************************************************************************
  1540. TEXITNODE
  1541. *****************************************************************************}
  1542. constructor texitnode.create(l:tnode);
  1543. begin
  1544. inherited create(exitn,l);
  1545. if assigned(left) then
  1546. begin
  1547. { add assignment to funcretsym }
  1548. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1549. left:=cassignmentnode.create(
  1550. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1551. left);
  1552. end;
  1553. end;
  1554. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1555. begin
  1556. inherited ppuload(t,ppufile);
  1557. end;
  1558. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1559. begin
  1560. inherited ppuwrite(ppufile);
  1561. end;
  1562. function texitnode.pass_typecheck:tnode;
  1563. var
  1564. pd: tprocdef;
  1565. newstatement : tstatementnode;
  1566. begin
  1567. result:=nil;
  1568. newstatement:=nil;
  1569. if assigned(left) then
  1570. begin
  1571. result:=internalstatements(newstatement);
  1572. addstatement(newstatement,left);
  1573. left:=nil;
  1574. end;
  1575. { if the function result has been migrated to the parentfpstruct,
  1576. we have to load it back to the original location (from which the
  1577. code generator will load it into the function result location),
  1578. because the code to this that we add in tnodeutils.wrap_proc_body()
  1579. gets inserted before the exit label to which this node will jump }
  1580. if (target_info.system in systems_fpnestedstruct) and
  1581. not(nf_internal in flags) then
  1582. begin
  1583. pd:=current_procinfo.procdef;
  1584. if assigned(pd.funcretsym) and
  1585. tabstractnormalvarsym(pd.funcretsym).inparentfpstruct then
  1586. begin
  1587. if not assigned(result) then
  1588. result:=internalstatements(newstatement);
  1589. cnodeutils.load_parentfpstruct_nested_funcret(current_procinfo.procdef,newstatement);
  1590. end;
  1591. end;
  1592. if assigned(result) then
  1593. begin
  1594. addstatement(newstatement,self.getcopy);
  1595. { ensure we don't insert the function result loading code again for
  1596. this node }
  1597. include(newstatement.left.flags,nf_internal);
  1598. end;
  1599. resultdef:=voidtype;
  1600. end;
  1601. function texitnode.pass_1 : tnode;
  1602. begin
  1603. result:=nil;
  1604. expectloc:=LOC_VOID;
  1605. if assigned(left) then
  1606. internalerror(2011052801);
  1607. end;
  1608. {*****************************************************************************
  1609. TBREAKNODE
  1610. *****************************************************************************}
  1611. constructor tbreaknode.create;
  1612. begin
  1613. inherited create(breakn);
  1614. end;
  1615. function tbreaknode.pass_typecheck:tnode;
  1616. begin
  1617. result:=nil;
  1618. resultdef:=voidtype;
  1619. end;
  1620. function tbreaknode.pass_1 : tnode;
  1621. begin
  1622. result:=nil;
  1623. expectloc:=LOC_VOID;
  1624. end;
  1625. {*****************************************************************************
  1626. TCONTINUENODE
  1627. *****************************************************************************}
  1628. constructor tcontinuenode.create;
  1629. begin
  1630. inherited create(continuen);
  1631. end;
  1632. function tcontinuenode.pass_typecheck:tnode;
  1633. begin
  1634. result:=nil;
  1635. resultdef:=voidtype;
  1636. end;
  1637. function tcontinuenode.pass_1 : tnode;
  1638. begin
  1639. result:=nil;
  1640. expectloc:=LOC_VOID;
  1641. end;
  1642. {*****************************************************************************
  1643. TGOTONODE
  1644. *****************************************************************************}
  1645. constructor tgotonode.create(p : tlabelsym);
  1646. begin
  1647. inherited create(goton);
  1648. exceptionblock:=current_exceptblock;
  1649. labelnode:=nil;
  1650. labelsym:=p;
  1651. end;
  1652. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1653. begin
  1654. inherited ppuload(t,ppufile);
  1655. labelnodeidx:=ppufile.getlongint;
  1656. exceptionblock:=ppufile.getbyte;
  1657. end;
  1658. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1659. begin
  1660. inherited ppuwrite(ppufile);
  1661. labelnodeidx:=labelnode.ppuidx;
  1662. ppufile.putlongint(labelnodeidx);
  1663. ppufile.putbyte(exceptionblock);
  1664. end;
  1665. procedure tgotonode.buildderefimpl;
  1666. begin
  1667. inherited buildderefimpl;
  1668. end;
  1669. procedure tgotonode.derefimpl;
  1670. begin
  1671. inherited derefimpl;
  1672. end;
  1673. procedure tgotonode.resolveppuidx;
  1674. begin
  1675. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  1676. if labelnode.nodetype<>labeln then
  1677. internalerror(200809021);
  1678. end;
  1679. function tgotonode.pass_typecheck:tnode;
  1680. begin
  1681. result:=nil;
  1682. resultdef:=voidtype;
  1683. end;
  1684. function tgotonode.pass_1 : tnode;
  1685. var
  1686. p2 : tprocinfo;
  1687. begin
  1688. result:=nil;
  1689. expectloc:=LOC_VOID;
  1690. { The labelnode can already be set when
  1691. this node was copied }
  1692. if not(assigned(labelnode)) then
  1693. begin
  1694. { inner procedure goto? }
  1695. if assigned(labelsym.code) and
  1696. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1697. { generated by the optimizer? }
  1698. not(assigned(labelsym.owner))) then
  1699. labelnode:=tlabelnode(labelsym.code)
  1700. else if ((m_non_local_goto in current_settings.modeswitches) and
  1701. assigned(labelsym.owner)) or
  1702. { nested exits don't need the non local goto switch }
  1703. (labelsym.realname='$nestedexit') then
  1704. begin
  1705. if current_procinfo.procdef.parast.symtablelevel>labelsym.owner.symtablelevel then
  1706. begin
  1707. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  1708. if exceptionblock>0 then
  1709. CGMessage(cg_e_goto_inout_of_exception_block);
  1710. { goto across procedures using exception?
  1711. this is not allowed because we cannot
  1712. easily unwind the exception frame
  1713. stack
  1714. }
  1715. p2:=current_procinfo;
  1716. while true do
  1717. begin
  1718. if ((cs_implicit_exceptions in current_settings.moduleswitches) and ((p2.flags*[pi_needs_implicit_finally,pi_has_implicit_finally])<>[])) or
  1719. ((p2.flags*[pi_uses_exceptions])<>[]) then
  1720. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  1721. if labelsym.owner=p2.procdef.localst then
  1722. break;
  1723. p2:=p2.parent
  1724. end;
  1725. if assigned(labelsym.jumpbuf) then
  1726. begin
  1727. labelsym.nonlocal:=true;
  1728. exclude(current_procinfo.procdef.procoptions,po_inline);
  1729. result:=ccallnode.createintern('fpc_longjmp',
  1730. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  1731. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  1732. nil)));
  1733. end
  1734. else
  1735. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1736. end
  1737. else
  1738. CGMessage(cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  1739. end
  1740. else
  1741. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1742. end;
  1743. { check if we don't mess with exception blocks }
  1744. if assigned(labelnode) and
  1745. (exceptionblock<>labelnode.exceptionblock) then
  1746. CGMessage(cg_e_goto_inout_of_exception_block);
  1747. end;
  1748. function tgotonode.dogetcopy : tnode;
  1749. var
  1750. p : tgotonode;
  1751. begin
  1752. p:=tgotonode(inherited dogetcopy);
  1753. p.exceptionblock:=exceptionblock;
  1754. { generate labelnode if not done yet }
  1755. if not(assigned(labelnode)) then
  1756. begin
  1757. if assigned(labelsym) and assigned(labelsym.code) then
  1758. labelnode:=tlabelnode(labelsym.code)
  1759. end;
  1760. p.labelsym:=labelsym;
  1761. if assigned(labelnode) then
  1762. p.labelnode:=tlabelnode(labelnode.dogetcopy)
  1763. else
  1764. begin
  1765. { don't trigger IE when there was already an error, i.e. the
  1766. label is not defined. See tw11763 (PFV) }
  1767. if (errorcount=0) and
  1768. { don't trigger IE if it's a global goto }
  1769. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1770. not(assigned(labelsym.owner))) then
  1771. internalerror(200610291);
  1772. end;
  1773. result:=p;
  1774. end;
  1775. function tgotonode.docompare(p: tnode): boolean;
  1776. begin
  1777. docompare := false;
  1778. end;
  1779. {*****************************************************************************
  1780. TLABELNODE
  1781. *****************************************************************************}
  1782. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  1783. begin
  1784. inherited create(labeln,l);
  1785. exceptionblock:=current_exceptblock;
  1786. labsym:=alabsym;
  1787. { Register labelnode in labelsym }
  1788. labsym.code:=self;
  1789. end;
  1790. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1791. begin
  1792. inherited ppuload(t,ppufile);
  1793. exceptionblock:=ppufile.getbyte;
  1794. end;
  1795. destructor tlabelnode.destroy;
  1796. begin
  1797. if assigned(labsym) then
  1798. begin
  1799. if not assigned(labsym.Owner) then
  1800. labsym.Free // Free labelsym if it has no owner
  1801. else
  1802. if labsym.code=pointer(self) then
  1803. begin
  1804. { Remove reference in labelsym, this is to prevent
  1805. goto's to this label }
  1806. labsym.code:=nil;
  1807. end;
  1808. end;
  1809. inherited destroy;
  1810. end;
  1811. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  1812. begin
  1813. inherited ppuwrite(ppufile);
  1814. ppufile.putbyte(exceptionblock);
  1815. end;
  1816. procedure tlabelnode.buildderefimpl;
  1817. begin
  1818. inherited buildderefimpl;
  1819. end;
  1820. procedure tlabelnode.derefimpl;
  1821. begin
  1822. inherited derefimpl;
  1823. end;
  1824. function tlabelnode.pass_typecheck:tnode;
  1825. begin
  1826. result:=nil;
  1827. { left could still be unassigned }
  1828. if assigned(left) then
  1829. typecheckpass(left);
  1830. resultdef:=voidtype;
  1831. end;
  1832. function tlabelnode.pass_1 : tnode;
  1833. begin
  1834. result:=nil;
  1835. expectloc:=LOC_VOID;
  1836. include(current_procinfo.flags,pi_has_label);
  1837. if assigned(labsym) and labsym.nonlocal then
  1838. begin
  1839. include(current_procinfo.flags,pi_has_interproclabel);
  1840. exclude(current_procinfo.procdef.procoptions,po_inline);
  1841. end;
  1842. if assigned(left) then
  1843. firstpass(left);
  1844. if (m_non_local_goto in current_settings.modeswitches) and
  1845. { the owner can be Nil for internal labels }
  1846. assigned(labsym.owner) and
  1847. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  1848. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  1849. end;
  1850. function tlabelnode.dogetcopy : tnode;
  1851. begin
  1852. if not(assigned(copiedto)) then
  1853. copiedto:=tlabelnode(inherited dogetcopy);
  1854. copiedto.exceptionblock:=exceptionblock;
  1855. result:=copiedto;
  1856. end;
  1857. function tlabelnode.docompare(p: tnode): boolean;
  1858. begin
  1859. docompare := false;
  1860. end;
  1861. {*****************************************************************************
  1862. TRAISENODE
  1863. *****************************************************************************}
  1864. constructor traisenode.create(l,taddr,tframe:tnode);
  1865. begin
  1866. inherited create(raisen,l,taddr,tframe);
  1867. end;
  1868. function traisenode.pass_typecheck:tnode;
  1869. begin
  1870. result:=nil;
  1871. resultdef:=voidtype;
  1872. if assigned(left) then
  1873. begin
  1874. { first para must be a _class_ }
  1875. typecheckpass(left);
  1876. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1877. if codegenerror then
  1878. exit;
  1879. if not is_class(left.resultdef) and
  1880. not is_javaclass(left.resultdef) then
  1881. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  1882. { insert needed typeconvs for addr,frame }
  1883. if assigned(right) then
  1884. begin
  1885. { addr }
  1886. typecheckpass(right);
  1887. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1888. inserttypeconv(right,voidcodepointertype);
  1889. { frame }
  1890. if assigned(third) then
  1891. begin
  1892. typecheckpass(third);
  1893. set_varstate(third,vs_read,[vsf_must_be_valid]);
  1894. inserttypeconv(third,voidpointertype);
  1895. end;
  1896. end;
  1897. end;
  1898. end;
  1899. function traisenode.pass_1 : tnode;
  1900. var
  1901. statements : tstatementnode;
  1902. current_addr : tlabelnode;
  1903. raisenode : tcallnode;
  1904. begin
  1905. result:=internalstatements(statements);
  1906. if assigned(left) then
  1907. begin
  1908. { first para must be a class }
  1909. firstpass(left);
  1910. { insert needed typeconvs for addr,frame }
  1911. if assigned(right) then
  1912. begin
  1913. { addr }
  1914. firstpass(right);
  1915. { frame }
  1916. if assigned(third) then
  1917. firstpass(third)
  1918. else
  1919. third:=cpointerconstnode.Create(0,voidpointertype);
  1920. end
  1921. else
  1922. begin
  1923. third:=cinlinenode.create(in_get_frame,false,nil);
  1924. current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  1925. addstatement(statements,current_addr);
  1926. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  1927. end;
  1928. raisenode:=ccallnode.createintern('fpc_raiseexception',
  1929. ccallparanode.create(third,
  1930. ccallparanode.create(right,
  1931. ccallparanode.create(left,nil)))
  1932. );
  1933. include(raisenode.callnodeflags,cnf_call_never_returns);
  1934. addstatement(statements,raisenode);
  1935. end
  1936. else
  1937. begin
  1938. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  1939. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  1940. include(raisenode.callnodeflags,cnf_call_never_returns);
  1941. addstatement(statements,raisenode);
  1942. end;
  1943. left:=nil;
  1944. right:=nil;
  1945. third:=nil;
  1946. end;
  1947. {*****************************************************************************
  1948. TTRYEXCEPTNODE
  1949. *****************************************************************************}
  1950. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1951. begin
  1952. inherited create(tryexceptn,l,r,_t1,nil);
  1953. end;
  1954. function ttryexceptnode.pass_typecheck:tnode;
  1955. begin
  1956. result:=nil;
  1957. typecheckpass(left);
  1958. { on statements }
  1959. if assigned(right) then
  1960. typecheckpass(right);
  1961. { else block }
  1962. if assigned(t1) then
  1963. typecheckpass(t1);
  1964. resultdef:=voidtype;
  1965. end;
  1966. function ttryexceptnode.pass_1 : tnode;
  1967. begin
  1968. result:=nil;
  1969. expectloc:=LOC_VOID;
  1970. firstpass(left);
  1971. { on statements }
  1972. if assigned(right) then
  1973. firstpass(right);
  1974. { else block }
  1975. if assigned(t1) then
  1976. firstpass(t1);
  1977. include(current_procinfo.flags,pi_do_call);
  1978. include(current_procinfo.flags,pi_uses_exceptions);
  1979. adjust_estimated_stack_size;
  1980. end;
  1981. function ttryexceptnode.simplify(forinline: boolean): tnode;
  1982. begin
  1983. result:=nil;
  1984. { empty try -> can never raise exception -> do nothing }
  1985. if has_no_code(left) then
  1986. result:=cnothingnode.create;
  1987. end;
  1988. procedure ttryexceptnode.adjust_estimated_stack_size;
  1989. begin
  1990. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size*2);
  1991. end;
  1992. {*****************************************************************************
  1993. TTRYFINALLYNODE
  1994. *****************************************************************************}
  1995. constructor ttryfinallynode.create(l,r:tnode);
  1996. begin
  1997. inherited create(tryfinallyn,l,r);
  1998. implicitframe:=false;
  1999. end;
  2000. constructor ttryfinallynode.create_implicit(l,r:tnode);
  2001. begin
  2002. inherited create(tryfinallyn,l,r);
  2003. implicitframe:=true;
  2004. end;
  2005. function ttryfinallynode.pass_typecheck:tnode;
  2006. begin
  2007. result:=nil;
  2008. resultdef:=voidtype;
  2009. typecheckpass(left);
  2010. // "try block" is "used"? (JM)
  2011. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  2012. typecheckpass(right);
  2013. // "except block" is "used"? (JM)
  2014. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  2015. end;
  2016. function ttryfinallynode.pass_1 : tnode;
  2017. begin
  2018. result:=nil;
  2019. expectloc:=LOC_VOID;
  2020. firstpass(left);
  2021. firstpass(right);
  2022. include(current_procinfo.flags,pi_do_call);
  2023. { pi_uses_exceptions is an information for the optimizer and it
  2024. is only interested in exceptions if they appear inside the body,
  2025. so ignore implicit frames when setting the flag }
  2026. if not(implicitframe) then
  2027. include(current_procinfo.flags,pi_uses_exceptions);
  2028. adjust_estimated_stack_size;
  2029. end;
  2030. function ttryfinallynode.simplify(forinline : boolean): tnode;
  2031. begin
  2032. result:=nil;
  2033. { if the try contains no code, we can kill
  2034. the try and except and return only the
  2035. finally part }
  2036. if has_no_code(left) then
  2037. begin
  2038. result:=right;
  2039. right:=nil;
  2040. end;
  2041. end;
  2042. procedure ttryfinallynode.adjust_estimated_stack_size;
  2043. begin
  2044. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size);
  2045. end;
  2046. {*****************************************************************************
  2047. TONNODE
  2048. *****************************************************************************}
  2049. constructor tonnode.create(l,r:tnode);
  2050. begin
  2051. inherited create(onn,l,r);
  2052. excepTSymtable:=nil;
  2053. excepttype:=nil;
  2054. end;
  2055. destructor tonnode.destroy;
  2056. begin
  2057. { copied nodes don't need to release the symtable }
  2058. if assigned(excepTSymtable) then
  2059. excepTSymtable.free;
  2060. inherited destroy;
  2061. end;
  2062. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2063. begin
  2064. inherited ppuload(t,ppufile);
  2065. excepTSymtable:=nil;
  2066. excepttype:=nil;
  2067. end;
  2068. function tonnode.dogetcopy : tnode;
  2069. var
  2070. n : tonnode;
  2071. begin
  2072. n:=tonnode(inherited dogetcopy);
  2073. if assigned(exceptsymtable) then
  2074. n.exceptsymtable:=exceptsymtable.getcopy
  2075. else
  2076. n.exceptsymtable:=nil;
  2077. n.excepttype:=excepttype;
  2078. result:=n;
  2079. end;
  2080. function tonnode.pass_typecheck:tnode;
  2081. begin
  2082. result:=nil;
  2083. resultdef:=voidtype;
  2084. if not is_class(excepttype) and
  2085. not is_javaclass(excepttype) then
  2086. CGMessage1(type_e_class_type_expected,excepttype.typename);
  2087. if assigned(left) then
  2088. typecheckpass(left);
  2089. if assigned(right) then
  2090. typecheckpass(right);
  2091. end;
  2092. function tonnode.pass_1 : tnode;
  2093. begin
  2094. result:=nil;
  2095. include(current_procinfo.flags,pi_do_call);
  2096. { Loads exception class VMT, therefore may need GOT
  2097. (generic code only; descendants may need to avoid this check) }
  2098. if (cs_create_pic in current_settings.moduleswitches) and
  2099. (tf_pic_uses_got in target_info.flags) then
  2100. include(current_procinfo.flags,pi_needs_got);
  2101. expectloc:=LOC_VOID;
  2102. if assigned(left) then
  2103. firstpass(left);
  2104. if assigned(right) then
  2105. firstpass(right);
  2106. end;
  2107. function tonnode.docompare(p: tnode): boolean;
  2108. begin
  2109. docompare := false;
  2110. end;
  2111. end.