nflw.pas 78 KB

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