nflw.pas 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  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. constructor create_internal(l,r,_t1 : tnode);virtual;reintroduce;
  73. function pass_typecheck:tnode;override;
  74. function pass_1 : tnode;override;
  75. function simplify(forinline : boolean) : tnode;override;
  76. private
  77. function internalsimplify(warn: boolean) : tnode;
  78. end;
  79. tifnodeclass = class of tifnode;
  80. tfornode = class(tloopnode)
  81. { if count isn divisable by unrolls then
  82. the for loop must jump to this label to get the correct
  83. number of executions }
  84. entrylabel,
  85. { this is a dummy node used by the dfa to store life information for the loop iteration }
  86. loopiteration : tnode;
  87. loopvar_notid:cardinal;
  88. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;reintroduce;
  89. function wrap_to_value:tnode;
  90. function pass_typecheck:tnode;override;
  91. function pass_1 : tnode;override;
  92. function simplify(forinline : boolean) : tnode;override;
  93. end;
  94. tfornodeclass = class of tfornode;
  95. texitnode = class(tunarynode)
  96. constructor create(l:tnode);virtual;
  97. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  98. procedure ppuwrite(ppufile:tcompilerppufile);override;
  99. function pass_typecheck:tnode;override;
  100. function pass_1 : tnode;override;
  101. property resultexpr : tnode read left write left;
  102. end;
  103. texitnodeclass = class of texitnode;
  104. tbreaknode = class(tnode)
  105. constructor create;virtual;
  106. function pass_typecheck:tnode;override;
  107. function pass_1 : tnode;override;
  108. end;
  109. tbreaknodeclass = class of tbreaknode;
  110. tcontinuenode = class(tnode)
  111. constructor create;virtual;
  112. function pass_typecheck:tnode;override;
  113. function pass_1 : tnode;override;
  114. end;
  115. tcontinuenodeclass = class of tcontinuenode;
  116. tgotonode = class(tnode)
  117. private
  118. labelnodeidx : longint;
  119. public
  120. labelsym : tlabelsym;
  121. labelnode : tlabelnode;
  122. exceptionblock : integer;
  123. constructor create(p : tlabelsym);virtual;
  124. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  125. procedure ppuwrite(ppufile:tcompilerppufile);override;
  126. procedure buildderefimpl;override;
  127. procedure derefimpl;override;
  128. procedure resolveppuidx;override;
  129. function dogetcopy : tnode;override;
  130. function pass_typecheck:tnode;override;
  131. function pass_1 : tnode;override;
  132. function docompare(p: tnode): boolean; override;
  133. end;
  134. tgotonodeclass = class of tgotonode;
  135. tlabelnode = class(tunarynode)
  136. exceptionblock : integer;
  137. { when copying trees, this points to the newly created copy of a label }
  138. copiedto : tlabelnode;
  139. labsym : tlabelsym;
  140. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  141. destructor destroy;override;
  142. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  143. procedure ppuwrite(ppufile:tcompilerppufile);override;
  144. procedure buildderefimpl;override;
  145. procedure derefimpl;override;
  146. function dogetcopy : tnode;override;
  147. function pass_typecheck:tnode;override;
  148. function pass_1 : tnode;override;
  149. function docompare(p: tnode): boolean; override;
  150. end;
  151. tlabelnodeclass = class of tlabelnode;
  152. traisenode = class(ttertiarynode)
  153. constructor create(l,taddr,tframe:tnode);virtual;
  154. function pass_typecheck:tnode;override;
  155. function pass_1 : tnode;override;
  156. end;
  157. traisenodeclass = class of traisenode;
  158. ttryexceptnode = class(tloopnode)
  159. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  160. function pass_typecheck:tnode;override;
  161. function pass_1 : tnode;override;
  162. function simplify(forinline: boolean): tnode; override;
  163. protected
  164. procedure adjust_estimated_stack_size; virtual;
  165. end;
  166. ttryexceptnodeclass = class of ttryexceptnode;
  167. ttryfinallynode = class(tloopnode)
  168. implicitframe : boolean;
  169. constructor create(l,r:tnode);virtual;reintroduce;
  170. constructor create_implicit(l,r,_t1:tnode);virtual;
  171. function pass_typecheck:tnode;override;
  172. function pass_1 : tnode;override;
  173. function simplify(forinline:boolean): tnode;override;
  174. protected
  175. function create_finalizer_procdef: tprocdef;
  176. procedure adjust_estimated_stack_size; virtual;
  177. end;
  178. ttryfinallynodeclass = class of ttryfinallynode;
  179. tonnode = class(tbinarynode)
  180. excepTSymtable : TSymtable;
  181. excepttype : tobjectdef;
  182. constructor create(l,r:tnode);virtual;
  183. destructor destroy;override;
  184. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  185. function pass_typecheck:tnode;override;
  186. function pass_1 : tnode;override;
  187. function dogetcopy : tnode;override;
  188. function docompare(p: tnode): boolean; override;
  189. end;
  190. tonnodeclass = class of tonnode;
  191. var
  192. cwhilerepeatnode : twhilerepeatnodeclass=twhilerepeatnode;
  193. cifnode : tifnodeclass = tifnode;
  194. cfornode : tfornodeclass = tfornode;
  195. cexitnode : texitnodeclass = texitnode;
  196. cgotonode : tgotonodeclass = tgotonode;
  197. clabelnode : tlabelnodeclass = tlabelnode;
  198. craisenode : traisenodeclass = traisenode;
  199. ctryexceptnode : ttryexceptnodeclass = ttryexceptnode;
  200. ctryfinallynode : ttryfinallynodeclass = ttryfinallynode;
  201. connode : tonnodeclass = tonnode;
  202. cbreaknode : tbreaknodeclass = tbreaknode;
  203. ccontinuenode : tcontinuenodeclass = tcontinuenode;
  204. // for-in loop helpers
  205. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  206. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  207. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  208. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  209. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  210. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  211. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  212. implementation
  213. uses
  214. globtype,systems,constexp,
  215. cutils,verbose,globals,
  216. symconst,symtable,paramgr,defcmp,defutil,htypechk,pass_1,
  217. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,nutils,ninl,nset,
  218. pdecsub,
  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,pasbool8type,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. else
  779. begin
  780. // search for operator first
  781. pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
  782. // if there is no operator then search for class/object enumerator method
  783. if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
  784. begin
  785. { first search using the helper hierarchy }
  786. if search_last_objectpascal_helper(tabstractrecorddef(expr.resultdef),nil,helperdef) then
  787. repeat
  788. pd:=helperdef.search_enumerator_get;
  789. helperdef:=helperdef.childof;
  790. until (pd<>nil) or (helperdef=nil);
  791. { we didn't find an enumerator in a helper, so search in the
  792. class/record/object itself }
  793. if pd=nil then
  794. pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
  795. end;
  796. if pd<>nil then
  797. begin
  798. // seach movenext and current symbols
  799. movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
  800. if movenext = nil then
  801. begin
  802. result:=cerrornode.create;
  803. hloopvar.free;
  804. hloopbody.free;
  805. MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
  806. end
  807. else
  808. begin
  809. current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  810. if current = nil then
  811. begin
  812. result:=cerrornode.create;
  813. hloopvar.free;
  814. hloopbody.free;
  815. MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
  816. end
  817. else
  818. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  819. end;
  820. end
  821. else
  822. begin
  823. case expr.resultdef.typ of
  824. stringdef: result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  825. arraydef: result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  826. setdef: result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  827. else
  828. begin
  829. result:=cerrornode.create;
  830. hloopvar.free;
  831. hloopbody.free;
  832. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  833. end;
  834. end;
  835. end;
  836. end;
  837. end;
  838. current_filepos:=storefilepos;
  839. end;
  840. {****************************************************************************
  841. TLOOPNODE
  842. *****************************************************************************}
  843. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  844. begin
  845. inherited create(tt,l,r);
  846. t1:=_t1;
  847. t2:=_t2;
  848. fileinfo:=l.fileinfo;
  849. end;
  850. destructor tloopnode.destroy;
  851. begin
  852. t1.free;
  853. t2.free;
  854. inherited destroy;
  855. end;
  856. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  857. begin
  858. inherited ppuload(t,ppufile);
  859. t1:=ppuloadnode(ppufile);
  860. t2:=ppuloadnode(ppufile);
  861. ppufile.getsmallset(loopflags);
  862. end;
  863. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  864. begin
  865. inherited ppuwrite(ppufile);
  866. ppuwritenode(ppufile,t1);
  867. ppuwritenode(ppufile,t2);
  868. ppufile.putsmallset(loopflags);
  869. end;
  870. procedure tloopnode.buildderefimpl;
  871. begin
  872. inherited buildderefimpl;
  873. if assigned(t1) then
  874. t1.buildderefimpl;
  875. if assigned(t2) then
  876. t2.buildderefimpl;
  877. end;
  878. procedure tloopnode.derefimpl;
  879. begin
  880. inherited derefimpl;
  881. if assigned(t1) then
  882. t1.derefimpl;
  883. if assigned(t2) then
  884. t2.derefimpl;
  885. end;
  886. function tloopnode.dogetcopy : tnode;
  887. var
  888. p : tloopnode;
  889. begin
  890. p:=tloopnode(inherited dogetcopy);
  891. if assigned(t1) then
  892. p.t1:=t1.dogetcopy
  893. else
  894. p.t1:=nil;
  895. if assigned(t2) then
  896. p.t2:=t2.dogetcopy
  897. else
  898. p.t2:=nil;
  899. p.loopflags:=loopflags;
  900. dogetcopy:=p;
  901. end;
  902. procedure tloopnode.insertintolist(l : tnodelist);
  903. begin
  904. end;
  905. procedure tloopnode.printnodetree(var t:text);
  906. begin
  907. write(t,printnodeindention,'(');
  908. printnodeindent;
  909. printnodeinfo(t);
  910. writeln(t);
  911. printnode(t,left);
  912. printnode(t,right);
  913. printnode(t,t1);
  914. printnode(t,t2);
  915. printnodeunindent;
  916. writeln(t,printnodeindention,')');
  917. end;
  918. function tloopnode.docompare(p: tnode): boolean;
  919. begin
  920. docompare :=
  921. inherited docompare(p) and
  922. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  923. t1.isequal(tloopnode(p).t1) and
  924. t2.isequal(tloopnode(p).t2);
  925. end;
  926. {****************************************************************************
  927. TWHILEREPEATNODE
  928. *****************************************************************************}
  929. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  930. begin
  931. inherited create(whilerepeatn,l,r,nil,nil);
  932. if tab then
  933. include(loopflags, lnf_testatbegin);
  934. if cn then
  935. include(loopflags,lnf_checknegate);
  936. end;
  937. function twhilerepeatnode.pass_typecheck:tnode;
  938. var
  939. t:Tunarynode;
  940. begin
  941. result:=nil;
  942. resultdef:=voidtype;
  943. typecheckpass(left);
  944. { tp procvar support }
  945. maybe_call_procvar(left,true);
  946. {A not node can be removed.}
  947. if left.nodetype=notn then
  948. begin
  949. t:=Tunarynode(left);
  950. left:=Tunarynode(left).left;
  951. t.left:=nil;
  952. t.destroy;
  953. {Symdif operator, in case you are wondering:}
  954. loopflags:=loopflags >< [lnf_checknegate];
  955. end;
  956. { loop instruction }
  957. if assigned(right) then
  958. typecheckpass(right);
  959. set_varstate(left,vs_read,[vsf_must_be_valid]);
  960. if codegenerror then
  961. exit;
  962. if not(is_boolean(left.resultdef)) and
  963. not(is_typeparam(left.resultdef)) then
  964. inserttypeconv(left,pasbool8type);
  965. { Give warnings for code that will never be executed for
  966. while false do }
  967. if (lnf_testatbegin in loopflags) and
  968. (left.nodetype=ordconstn) and
  969. (tordconstnode(left).value.uvalue=0) and
  970. assigned(right) then
  971. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  972. end;
  973. {$ifdef prefetchnext}
  974. type
  975. passignmentquery = ^tassignmentquery;
  976. tassignmentquery = record
  977. towhat: tnode;
  978. source: tassignmentnode;
  979. statementcount: cardinal;
  980. end;
  981. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  982. var
  983. query: passignmentquery absolute arg;
  984. temp, prederef: tnode;
  985. begin
  986. result := fen_norecurse_false;
  987. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  988. inc(query^.statementcount);
  989. { make sure there's something else in the loop besides going to the }
  990. { next item }
  991. if (query^.statementcount > 1) and
  992. (n.nodetype = assignn) then
  993. begin
  994. { skip type conversions of assignment target }
  995. temp := tassignmentnode(n).left;
  996. while (temp.nodetype = typeconvn) do
  997. temp := ttypeconvnode(temp).left;
  998. { assignment to x of the while assigned(x) check? }
  999. if not(temp.isequal(query^.towhat)) then
  1000. exit;
  1001. { right hand side of assignment dereferenced field of }
  1002. { x? (no derefn in case of class) }
  1003. temp := tassignmentnode(n).right;
  1004. while (temp.nodetype = typeconvn) do
  1005. temp := ttypeconvnode(temp).left;
  1006. if (temp.nodetype <> subscriptn) then
  1007. exit;
  1008. prederef := tsubscriptnode(temp).left;
  1009. temp := prederef;
  1010. while (temp.nodetype = typeconvn) do
  1011. temp := ttypeconvnode(temp).left;
  1012. { see tests/test/prefetch1.pp }
  1013. if (temp.nodetype = derefn) then
  1014. temp := tderefnode(temp).left
  1015. else
  1016. temp := prederef;
  1017. if temp.isequal(query^.towhat) then
  1018. begin
  1019. query^.source := tassignmentnode(n);
  1020. result := fen_norecurse_true;
  1021. end
  1022. end
  1023. { don't check nodes which can't contain an assignment or whose }
  1024. { final assignment can vary a lot }
  1025. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1026. result := fen_false;
  1027. end;
  1028. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1029. var
  1030. query: tassignmentquery;
  1031. begin
  1032. query.towhat := towhat;
  1033. query.source := nil;
  1034. query.statementcount := 0;
  1035. if foreachnodestatic(where,@checkassignment,@query) then
  1036. result := query.source
  1037. else
  1038. result := nil;
  1039. end;
  1040. {$endif prefetchnext}
  1041. function twhilerepeatnode.pass_1 : tnode;
  1042. {$ifdef prefetchnext}
  1043. var
  1044. runnernode, prefetchcode: tnode;
  1045. assignmentnode: tassignmentnode;
  1046. prefetchstatements: tstatementnode;
  1047. {$endif prefetchnext}
  1048. begin
  1049. result:=nil;
  1050. expectloc:=LOC_VOID;
  1051. firstpass(left);
  1052. if codegenerror then
  1053. exit;
  1054. { loop instruction }
  1055. if assigned(right) then
  1056. begin
  1057. firstpass(right);
  1058. if codegenerror then
  1059. exit;
  1060. end;
  1061. {$ifdef prefetchnext}
  1062. { do at the end so all complex typeconversions are already }
  1063. { converted to calln's }
  1064. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1065. (lnf_testatbegin in loopflags) then
  1066. begin
  1067. { get first component of the while check }
  1068. runnernode := left;
  1069. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1070. runnernode := tunarynode(runnernode).left;
  1071. { is it an assigned(x) check? }
  1072. if ((runnernode.nodetype = inlinen) and
  1073. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1074. ((runnernode.nodetype = unequaln) and
  1075. (taddnode(runnernode).right.nodetype = niln)) then
  1076. begin
  1077. runnernode := tunarynode(runnernode).left;
  1078. { in case of in_assigned_x, there's a callparan in between }
  1079. if (runnernode.nodetype = callparan) then
  1080. runnernode := tcallparanode(runnernode).left;
  1081. while (runnernode.nodetype = typeconvn) do
  1082. runnernode := ttypeconvnode(runnernode).left;
  1083. { is there an "x := x(^).somefield"? }
  1084. assignmentnode := findassignment(right,runnernode);
  1085. if assigned(assignmentnode) then
  1086. begin
  1087. prefetchcode := internalstatements(prefetchstatements);
  1088. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1089. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1090. addstatement(prefetchstatements,right);
  1091. right := prefetchcode;
  1092. typecheckpass(right);
  1093. end;
  1094. end;
  1095. end;
  1096. {$endif prefetchnext}
  1097. end;
  1098. {$ifdef state_tracking}
  1099. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1100. var condition:Tnode;
  1101. code:Tnode;
  1102. done:boolean;
  1103. value:boolean;
  1104. change:boolean;
  1105. firsttest:boolean;
  1106. factval:Tnode;
  1107. begin
  1108. track_state_pass:=false;
  1109. done:=false;
  1110. firsttest:=true;
  1111. {For repeat until statements, first do a pass through the code.}
  1112. if not(lnf_testatbegin in flags) then
  1113. begin
  1114. code:=right.getcopy;
  1115. if code.track_state_pass(exec_known) then
  1116. track_state_pass:=true;
  1117. code.destroy;
  1118. end;
  1119. repeat
  1120. condition:=left.getcopy;
  1121. code:=right.getcopy;
  1122. change:=condition.track_state_pass(exec_known);
  1123. factval:=aktstate.find_fact(left);
  1124. if factval<>nil then
  1125. begin
  1126. condition.destroy;
  1127. condition:=factval.getcopy;
  1128. change:=true;
  1129. end;
  1130. if change then
  1131. begin
  1132. track_state_pass:=true;
  1133. {Force new resultdef pass.}
  1134. condition.resultdef:=nil;
  1135. do_typecheckpass(condition);
  1136. end;
  1137. if is_constboolnode(condition) then
  1138. begin
  1139. {Try to turn a while loop into a repeat loop.}
  1140. if firsttest then
  1141. exclude(flags,testatbegin);
  1142. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1143. if value then
  1144. begin
  1145. if code.track_state_pass(exec_known) then
  1146. track_state_pass:=true;
  1147. end
  1148. else
  1149. done:=true;
  1150. end
  1151. else
  1152. begin
  1153. {Remove any modified variables from the state.}
  1154. code.track_state_pass(false);
  1155. done:=true;
  1156. end;
  1157. code.destroy;
  1158. condition.destroy;
  1159. firsttest:=false;
  1160. until done;
  1161. {The loop condition is also known, for example:
  1162. while i<10 do
  1163. begin
  1164. ...
  1165. end;
  1166. When the loop is done, we do know that i<10 = false.
  1167. }
  1168. condition:=left.getcopy;
  1169. if condition.track_state_pass(exec_known) then
  1170. begin
  1171. track_state_pass:=true;
  1172. {Force new resultdef pass.}
  1173. condition.resultdef:=nil;
  1174. do_typecheckpass(condition);
  1175. end;
  1176. if not is_constboolnode(condition) then
  1177. aktstate.store_fact(condition,
  1178. cordconstnode.create(byte(checknegate),pasbool8type,true))
  1179. else
  1180. condition.destroy;
  1181. end;
  1182. {$endif}
  1183. {*****************************************************************************
  1184. TIFNODE
  1185. *****************************************************************************}
  1186. constructor tifnode.create(l,r,_t1 : tnode);
  1187. begin
  1188. inherited create(ifn,l,r,_t1,nil);
  1189. end;
  1190. constructor tifnode.create_internal(l,r,_t1 : tnode);
  1191. begin
  1192. create(l,r,_t1);
  1193. include(flags,nf_internal);
  1194. end;
  1195. function tifnode.internalsimplify(warn: boolean) : tnode;
  1196. begin
  1197. result:=nil;
  1198. { optimize constant expressions }
  1199. if (left.nodetype=ordconstn) then
  1200. begin
  1201. if tordconstnode(left).value.uvalue<>0 then
  1202. begin
  1203. if assigned(right) then
  1204. result:=right
  1205. else
  1206. result:=cnothingnode.create;
  1207. right:=nil;
  1208. if warn and assigned(t1) then
  1209. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1210. end
  1211. else
  1212. begin
  1213. if assigned(t1) then
  1214. result:=t1
  1215. else
  1216. result:=cnothingnode.create;
  1217. t1:=nil;
  1218. if warn and assigned(right) then
  1219. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1220. end;
  1221. end;
  1222. end;
  1223. function tifnode.simplify(forinline : boolean) : tnode;
  1224. begin
  1225. result:=internalsimplify(false);
  1226. end;
  1227. function tifnode.pass_typecheck:tnode;
  1228. begin
  1229. result:=nil;
  1230. resultdef:=voidtype;
  1231. typecheckpass(left);
  1232. { tp procvar support }
  1233. maybe_call_procvar(left,true);
  1234. { if path }
  1235. if assigned(right) then
  1236. typecheckpass(right);
  1237. { else path }
  1238. if assigned(t1) then
  1239. typecheckpass(t1);
  1240. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1241. if codegenerror then
  1242. exit;
  1243. if not(is_boolean(left.resultdef)) and
  1244. not(is_typeparam(left.resultdef)) then
  1245. inserttypeconv(left,pasbool8type);
  1246. result:=internalsimplify(not(nf_internal in flags));
  1247. end;
  1248. function tifnode.pass_1 : tnode;
  1249. begin
  1250. result:=nil;
  1251. expectloc:=LOC_VOID;
  1252. firstpass(left);
  1253. { if path }
  1254. if assigned(right) then
  1255. firstpass(right);
  1256. { else path }
  1257. if assigned(t1) then
  1258. firstpass(t1);
  1259. { leave if we've got an error in one of the paths }
  1260. if codegenerror then
  1261. exit;
  1262. end;
  1263. {*****************************************************************************
  1264. TFORNODE
  1265. *****************************************************************************}
  1266. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1267. begin
  1268. inherited create(forn,l,r,_t1,_t2);
  1269. if back then
  1270. include(loopflags,lnf_backward);
  1271. include(loopflags,lnf_testatbegin);
  1272. end;
  1273. function tfornode.simplify(forinline : boolean) : tnode;
  1274. begin
  1275. result:=nil;
  1276. { Can we spare the first comparision? }
  1277. if (t1.nodetype=ordconstn) and
  1278. (right.nodetype=ordconstn) and
  1279. (
  1280. (
  1281. (lnf_backward in loopflags) and
  1282. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1283. ) or
  1284. (
  1285. not(lnf_backward in loopflags) and
  1286. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1287. )
  1288. ) then
  1289. exclude(loopflags,lnf_testatbegin);
  1290. if (t1.nodetype=ordconstn) and
  1291. (right.nodetype=ordconstn) and
  1292. (
  1293. (
  1294. (lnf_backward in loopflags) and
  1295. (tordconstnode(right).value<tordconstnode(t1).value)
  1296. ) or
  1297. (
  1298. not(lnf_backward in loopflags) and
  1299. (tordconstnode(right).value>tordconstnode(t1).value)
  1300. )
  1301. ) then
  1302. result:=cnothingnode.create;
  1303. end;
  1304. function tfornode.wrap_to_value:tnode;
  1305. var
  1306. statements: tstatementnode;
  1307. temp: ttempcreatenode;
  1308. begin
  1309. result:=internalstatements(statements);
  1310. temp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1311. addstatement(statements,temp);
  1312. addstatement(statements,cassignmentnode.create(
  1313. ctemprefnode.create(temp),
  1314. t1));
  1315. { create a new for node, it is cheaper than cloning entire loop body }
  1316. addstatement(statements,cfornode.create(
  1317. left,right,ctemprefnode.create(temp),t2,lnf_backward in loopflags));
  1318. addstatement(statements,ctempdeletenode.create(temp));
  1319. { all child nodes are reused }
  1320. left:=nil;
  1321. right:=nil;
  1322. t1:=nil;
  1323. t2:=nil;
  1324. end;
  1325. function tfornode.pass_typecheck:tnode;
  1326. var
  1327. res : tnode;
  1328. begin
  1329. result:=nil;
  1330. resultdef:=voidtype;
  1331. { process the loopvar, from and to, varstates are already set }
  1332. typecheckpass(left);
  1333. typecheckpass(right);
  1334. typecheckpass(t1);
  1335. set_varstate(left,vs_written,[]);
  1336. { loop unrolling }
  1337. if (cs_opt_loopunroll in current_settings.optimizerswitches) and
  1338. assigned(t2) and
  1339. { statements must be error free }
  1340. not(nf_error in t2.flags) then
  1341. begin
  1342. typecheckpass(t2);
  1343. res:=t2.simplify(false);
  1344. if assigned(res) then
  1345. t2:=res;
  1346. res:=unroll_loop(self);
  1347. if assigned(res) then
  1348. begin
  1349. typecheckpass(res);
  1350. result:=res;
  1351. exit;
  1352. end;
  1353. end;
  1354. { Make sure that the loop var and the
  1355. from and to values are compatible types }
  1356. check_ranges(right.fileinfo,right,left.resultdef);
  1357. inserttypeconv(right,left.resultdef);
  1358. check_ranges(t1.fileinfo,t1,left.resultdef);
  1359. inserttypeconv(t1,left.resultdef);
  1360. if assigned(t2) then
  1361. typecheckpass(t2);
  1362. end;
  1363. function tfornode.pass_1 : tnode;
  1364. begin
  1365. result:=nil;
  1366. expectloc:=LOC_VOID;
  1367. firstpass(left);
  1368. firstpass(right);
  1369. firstpass(t1);
  1370. if assigned(t2) then
  1371. firstpass(t2);
  1372. if codegenerror then
  1373. exit;
  1374. { 'to' value must be evaluated once before loop, so its possible modifications
  1375. inside loop body do not affect the number of iterations (see webtbs/tw8883). }
  1376. if not (t1.nodetype in [ordconstn,temprefn]) then
  1377. result:=wrap_to_value;
  1378. end;
  1379. {*****************************************************************************
  1380. TEXITNODE
  1381. *****************************************************************************}
  1382. constructor texitnode.create(l:tnode);
  1383. begin
  1384. inherited create(exitn,l);
  1385. if assigned(left) then
  1386. begin
  1387. { add assignment to funcretsym }
  1388. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1389. left:=cassignmentnode.create(
  1390. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1391. left);
  1392. end;
  1393. end;
  1394. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1395. begin
  1396. inherited ppuload(t,ppufile);
  1397. end;
  1398. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1399. begin
  1400. inherited ppuwrite(ppufile);
  1401. end;
  1402. function texitnode.pass_typecheck:tnode;
  1403. var
  1404. newstatement : tstatementnode;
  1405. begin
  1406. result:=nil;
  1407. if assigned(left) then
  1408. begin
  1409. result:=internalstatements(newstatement);
  1410. addstatement(newstatement,left);
  1411. left:=nil;
  1412. addstatement(newstatement,self.getcopy);
  1413. end;
  1414. resultdef:=voidtype;
  1415. end;
  1416. function texitnode.pass_1 : tnode;
  1417. begin
  1418. result:=nil;
  1419. expectloc:=LOC_VOID;
  1420. if assigned(left) then
  1421. internalerror(2011052801);
  1422. end;
  1423. {*****************************************************************************
  1424. TBREAKNODE
  1425. *****************************************************************************}
  1426. constructor tbreaknode.create;
  1427. begin
  1428. inherited create(breakn);
  1429. end;
  1430. function tbreaknode.pass_typecheck:tnode;
  1431. begin
  1432. result:=nil;
  1433. resultdef:=voidtype;
  1434. end;
  1435. function tbreaknode.pass_1 : tnode;
  1436. begin
  1437. result:=nil;
  1438. expectloc:=LOC_VOID;
  1439. end;
  1440. {*****************************************************************************
  1441. TCONTINUENODE
  1442. *****************************************************************************}
  1443. constructor tcontinuenode.create;
  1444. begin
  1445. inherited create(continuen);
  1446. end;
  1447. function tcontinuenode.pass_typecheck:tnode;
  1448. begin
  1449. result:=nil;
  1450. resultdef:=voidtype;
  1451. end;
  1452. function tcontinuenode.pass_1 : tnode;
  1453. begin
  1454. result:=nil;
  1455. expectloc:=LOC_VOID;
  1456. end;
  1457. {*****************************************************************************
  1458. TGOTONODE
  1459. *****************************************************************************}
  1460. constructor tgotonode.create(p : tlabelsym);
  1461. begin
  1462. inherited create(goton);
  1463. exceptionblock:=current_exceptblock;
  1464. labelnode:=nil;
  1465. labelsym:=p;
  1466. end;
  1467. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1468. begin
  1469. inherited ppuload(t,ppufile);
  1470. labelnodeidx:=ppufile.getlongint;
  1471. exceptionblock:=ppufile.getbyte;
  1472. end;
  1473. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1474. begin
  1475. inherited ppuwrite(ppufile);
  1476. labelnodeidx:=labelnode.ppuidx;
  1477. ppufile.putlongint(labelnodeidx);
  1478. ppufile.putbyte(exceptionblock);
  1479. end;
  1480. procedure tgotonode.buildderefimpl;
  1481. begin
  1482. inherited buildderefimpl;
  1483. end;
  1484. procedure tgotonode.derefimpl;
  1485. begin
  1486. inherited derefimpl;
  1487. end;
  1488. procedure tgotonode.resolveppuidx;
  1489. begin
  1490. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  1491. if labelnode.nodetype<>labeln then
  1492. internalerror(200809021);
  1493. end;
  1494. function tgotonode.pass_typecheck:tnode;
  1495. begin
  1496. result:=nil;
  1497. resultdef:=voidtype;
  1498. end;
  1499. function tgotonode.pass_1 : tnode;
  1500. var
  1501. p2 : tprocinfo;
  1502. begin
  1503. result:=nil;
  1504. expectloc:=LOC_VOID;
  1505. { The labelnode can already be set when
  1506. this node was copied }
  1507. if not(assigned(labelnode)) then
  1508. begin
  1509. { inner procedure goto? }
  1510. if assigned(labelsym.code) and
  1511. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1512. { generated by the optimizer? }
  1513. not(assigned(labelsym.owner))) then
  1514. labelnode:=tlabelnode(labelsym.code)
  1515. else if ((m_non_local_goto in current_settings.modeswitches) and
  1516. assigned(labelsym.owner)) or
  1517. { nested exits don't need the non local goto switch }
  1518. (labelsym.realname='$nestedexit') then
  1519. begin
  1520. if current_procinfo.procdef.parast.symtablelevel>labelsym.owner.symtablelevel then
  1521. begin
  1522. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  1523. if exceptionblock>0 then
  1524. CGMessage(cg_e_goto_inout_of_exception_block);
  1525. { goto across procedures using exception?
  1526. this is not allowed because we cannot
  1527. easily unwind the exception frame
  1528. stack
  1529. }
  1530. p2:=current_procinfo;
  1531. while true do
  1532. begin
  1533. if (p2.flags*[pi_needs_implicit_finally,pi_uses_exceptions,pi_has_implicit_finally])<>[] then
  1534. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  1535. if labelsym.owner=p2.procdef.localst then
  1536. break;
  1537. p2:=p2.parent
  1538. end;
  1539. if assigned(labelsym.jumpbuf) then
  1540. begin
  1541. labelsym.nonlocal:=true;
  1542. exclude(current_procinfo.procdef.procoptions,po_inline);
  1543. result:=ccallnode.createintern('fpc_longjmp',
  1544. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  1545. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  1546. nil)));
  1547. end
  1548. else
  1549. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1550. end
  1551. else
  1552. CGMessage(cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  1553. end
  1554. else
  1555. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1556. end;
  1557. { check if we don't mess with exception blocks }
  1558. if assigned(labelnode) and
  1559. (exceptionblock<>labelnode.exceptionblock) then
  1560. CGMessage(cg_e_goto_inout_of_exception_block);
  1561. end;
  1562. function tgotonode.dogetcopy : tnode;
  1563. var
  1564. p : tgotonode;
  1565. begin
  1566. p:=tgotonode(inherited dogetcopy);
  1567. p.exceptionblock:=exceptionblock;
  1568. { generate labelnode if not done yet }
  1569. if not(assigned(labelnode)) then
  1570. begin
  1571. if assigned(labelsym) and assigned(labelsym.code) then
  1572. labelnode:=tlabelnode(labelsym.code)
  1573. end;
  1574. p.labelsym:=labelsym;
  1575. if assigned(labelnode) then
  1576. p.labelnode:=tlabelnode(labelnode.dogetcopy)
  1577. else
  1578. begin
  1579. { don't trigger IE when there was already an error, i.e. the
  1580. label is not defined. See tw11763 (PFV) }
  1581. if (errorcount=0) and
  1582. { don't trigger IE if it's a global goto }
  1583. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1584. not(assigned(labelsym.owner))) then
  1585. internalerror(200610291);
  1586. end;
  1587. result:=p;
  1588. end;
  1589. function tgotonode.docompare(p: tnode): boolean;
  1590. begin
  1591. docompare := false;
  1592. end;
  1593. {*****************************************************************************
  1594. TLABELNODE
  1595. *****************************************************************************}
  1596. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  1597. begin
  1598. inherited create(labeln,l);
  1599. exceptionblock:=current_exceptblock;
  1600. labsym:=alabsym;
  1601. { Register labelnode in labelsym }
  1602. labsym.code:=self;
  1603. end;
  1604. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1605. begin
  1606. inherited ppuload(t,ppufile);
  1607. exceptionblock:=ppufile.getbyte;
  1608. end;
  1609. destructor tlabelnode.destroy;
  1610. begin
  1611. { Remove reference in labelsym, this is to prevent
  1612. goto's to this label }
  1613. if assigned(labsym) and (labsym.code=pointer(self)) then
  1614. labsym.code:=nil;
  1615. inherited destroy;
  1616. end;
  1617. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  1618. begin
  1619. inherited ppuwrite(ppufile);
  1620. ppufile.putbyte(exceptionblock);
  1621. end;
  1622. procedure tlabelnode.buildderefimpl;
  1623. begin
  1624. inherited buildderefimpl;
  1625. end;
  1626. procedure tlabelnode.derefimpl;
  1627. begin
  1628. inherited derefimpl;
  1629. end;
  1630. function tlabelnode.pass_typecheck:tnode;
  1631. begin
  1632. result:=nil;
  1633. { left could still be unassigned }
  1634. if assigned(left) then
  1635. typecheckpass(left);
  1636. resultdef:=voidtype;
  1637. end;
  1638. function tlabelnode.pass_1 : tnode;
  1639. begin
  1640. result:=nil;
  1641. expectloc:=LOC_VOID;
  1642. include(current_procinfo.flags,pi_has_label);
  1643. if assigned(labsym) and labsym.nonlocal then
  1644. begin
  1645. include(current_procinfo.flags,pi_has_interproclabel);
  1646. exclude(current_procinfo.procdef.procoptions,po_inline);
  1647. end;
  1648. if assigned(left) then
  1649. firstpass(left);
  1650. if (m_non_local_goto in current_settings.modeswitches) and
  1651. { the owner can be Nil for internal labels }
  1652. assigned(labsym.owner) and
  1653. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  1654. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  1655. end;
  1656. function tlabelnode.dogetcopy : tnode;
  1657. begin
  1658. if not(assigned(copiedto)) then
  1659. copiedto:=tlabelnode(inherited dogetcopy);
  1660. copiedto.exceptionblock:=exceptionblock;
  1661. result:=copiedto;
  1662. end;
  1663. function tlabelnode.docompare(p: tnode): boolean;
  1664. begin
  1665. docompare := false;
  1666. end;
  1667. {*****************************************************************************
  1668. TRAISENODE
  1669. *****************************************************************************}
  1670. constructor traisenode.create(l,taddr,tframe:tnode);
  1671. begin
  1672. inherited create(raisen,l,taddr,tframe);
  1673. end;
  1674. function traisenode.pass_typecheck:tnode;
  1675. begin
  1676. result:=nil;
  1677. resultdef:=voidtype;
  1678. if assigned(left) then
  1679. begin
  1680. { first para must be a _class_ }
  1681. typecheckpass(left);
  1682. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1683. if codegenerror then
  1684. exit;
  1685. if not is_class(left.resultdef) and
  1686. not is_javaclass(left.resultdef) then
  1687. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  1688. { insert needed typeconvs for addr,frame }
  1689. if assigned(right) then
  1690. begin
  1691. { addr }
  1692. typecheckpass(right);
  1693. inserttypeconv(right,voidcodepointertype);
  1694. { frame }
  1695. if assigned(third) then
  1696. begin
  1697. typecheckpass(third);
  1698. inserttypeconv(third,voidpointertype);
  1699. end;
  1700. end;
  1701. end;
  1702. end;
  1703. function traisenode.pass_1 : tnode;
  1704. var
  1705. statements : tstatementnode;
  1706. current_addr : tlabelnode;
  1707. raisenode : tcallnode;
  1708. begin
  1709. result:=internalstatements(statements);
  1710. if assigned(left) then
  1711. begin
  1712. { first para must be a class }
  1713. firstpass(left);
  1714. { insert needed typeconvs for addr,frame }
  1715. if assigned(right) then
  1716. begin
  1717. { addr }
  1718. firstpass(right);
  1719. { frame }
  1720. if assigned(third) then
  1721. firstpass(third)
  1722. else
  1723. third:=cpointerconstnode.Create(0,voidpointertype);
  1724. end
  1725. else
  1726. begin
  1727. third:=cinlinenode.create(in_get_frame,false,nil);
  1728. current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  1729. addstatement(statements,current_addr);
  1730. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  1731. end;
  1732. raisenode:=ccallnode.createintern('fpc_raiseexception',
  1733. ccallparanode.create(third,
  1734. ccallparanode.create(right,
  1735. ccallparanode.create(left,nil)))
  1736. );
  1737. include(raisenode.callnodeflags,cnf_call_never_returns);
  1738. addstatement(statements,raisenode);
  1739. end
  1740. else
  1741. begin
  1742. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  1743. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  1744. include(raisenode.callnodeflags,cnf_call_never_returns);
  1745. addstatement(statements,raisenode);
  1746. end;
  1747. left:=nil;
  1748. right:=nil;
  1749. third:=nil;
  1750. end;
  1751. {*****************************************************************************
  1752. TTRYEXCEPTNODE
  1753. *****************************************************************************}
  1754. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1755. begin
  1756. inherited create(tryexceptn,l,r,_t1,nil);
  1757. end;
  1758. function ttryexceptnode.pass_typecheck:tnode;
  1759. begin
  1760. result:=nil;
  1761. typecheckpass(left);
  1762. { on statements }
  1763. if assigned(right) then
  1764. typecheckpass(right);
  1765. { else block }
  1766. if assigned(t1) then
  1767. typecheckpass(t1);
  1768. resultdef:=voidtype;
  1769. end;
  1770. function ttryexceptnode.pass_1 : tnode;
  1771. begin
  1772. result:=nil;
  1773. expectloc:=LOC_VOID;
  1774. firstpass(left);
  1775. { on statements }
  1776. if assigned(right) then
  1777. firstpass(right);
  1778. { else block }
  1779. if assigned(t1) then
  1780. firstpass(t1);
  1781. include(current_procinfo.flags,pi_do_call);
  1782. include(current_procinfo.flags,pi_uses_exceptions);
  1783. adjust_estimated_stack_size;
  1784. end;
  1785. function ttryexceptnode.simplify(forinline: boolean): tnode;
  1786. begin
  1787. result:=nil;
  1788. { empty try -> can never raise exception -> do nothing }
  1789. if has_no_code(left) then
  1790. result:=cnothingnode.create;
  1791. end;
  1792. procedure ttryexceptnode.adjust_estimated_stack_size;
  1793. begin
  1794. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size*2);
  1795. end;
  1796. {*****************************************************************************
  1797. TTRYFINALLYNODE
  1798. *****************************************************************************}
  1799. constructor ttryfinallynode.create(l,r:tnode);
  1800. begin
  1801. inherited create(tryfinallyn,l,r,nil,nil);
  1802. implicitframe:=false;
  1803. end;
  1804. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1805. begin
  1806. inherited create(tryfinallyn,l,r,_t1,nil);
  1807. implicitframe:=true;
  1808. end;
  1809. function ttryfinallynode.pass_typecheck:tnode;
  1810. begin
  1811. result:=nil;
  1812. resultdef:=voidtype;
  1813. typecheckpass(left);
  1814. // "try block" is "used"? (JM)
  1815. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  1816. typecheckpass(right);
  1817. // "except block" is "used"? (JM)
  1818. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  1819. { special finally block only executed when there was an exception }
  1820. if assigned(t1) then
  1821. begin
  1822. typecheckpass(t1);
  1823. // "finally block" is "used"? (JM)
  1824. set_varstate(t1,vs_readwritten,[vsf_must_be_valid]);
  1825. end;
  1826. end;
  1827. function ttryfinallynode.pass_1 : tnode;
  1828. begin
  1829. result:=nil;
  1830. expectloc:=LOC_VOID;
  1831. firstpass(left);
  1832. firstpass(right);
  1833. if assigned(t1) then
  1834. firstpass(t1);
  1835. include(current_procinfo.flags,pi_do_call);
  1836. { pi_uses_exceptions is an information for the optimizer and it
  1837. is only interested in exceptions if they appear inside the body,
  1838. so ignore implicit frames when setting the flag }
  1839. if not(implicitframe) then
  1840. include(current_procinfo.flags,pi_uses_exceptions);
  1841. adjust_estimated_stack_size;
  1842. end;
  1843. function ttryfinallynode.simplify(forinline : boolean): tnode;
  1844. begin
  1845. result:=nil;
  1846. { if the try contains no code, we can kill
  1847. the try and except and return only the
  1848. finally part }
  1849. if has_no_code(left) then
  1850. begin
  1851. result:=right;
  1852. right:=nil;
  1853. end;
  1854. end;
  1855. var
  1856. seq: longint=0;
  1857. function ttryfinallynode.create_finalizer_procdef: tprocdef;
  1858. var
  1859. st:TSymTable;
  1860. checkstack: psymtablestackitem;
  1861. oldsymtablestack: tsymtablestack;
  1862. sym:tprocsym;
  1863. begin
  1864. { get actual procedure symtable (skip withsymtables, etc.) }
  1865. st:=nil;
  1866. checkstack:=symtablestack.stack;
  1867. while assigned(checkstack) do
  1868. begin
  1869. st:=checkstack^.symtable;
  1870. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  1871. break;
  1872. checkstack:=checkstack^.next;
  1873. end;
  1874. { Create a nested procedure, even from main_program_level.
  1875. Furthermore, force procdef and procsym into the same symtable
  1876. (by default, defs are registered with symtablestack.top which may be
  1877. something temporary like exceptsymtable - in that case, procdef can be
  1878. destroyed before procsym, leaving invalid pointers). }
  1879. oldsymtablestack:=symtablestack;
  1880. symtablestack:=nil;
  1881. result:=cprocdef.create(max(normal_function_level,st.symtablelevel)+1,true);
  1882. symtablestack:=oldsymtablestack;
  1883. st.insertdef(result);
  1884. result.struct:=current_procinfo.procdef.struct;
  1885. { tabstractprocdef constructor sets po_delphi_nested_cc whenever
  1886. nested procvars modeswitch is active. We must be independent of this switch. }
  1887. exclude(result.procoptions,po_delphi_nested_cc);
  1888. result.proctypeoption:=potype_exceptfilter;
  1889. handle_calling_convention(result);
  1890. sym:=cprocsym.create('$fin$'+tostr(seq));
  1891. st.insert(sym);
  1892. inc(seq);
  1893. result.procsym:=sym;
  1894. proc_add_definition(result);
  1895. result.forwarddef:=false;
  1896. result.aliasnames.insert(result.mangledname);
  1897. end;
  1898. procedure ttryfinallynode.adjust_estimated_stack_size;
  1899. begin
  1900. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size);
  1901. end;
  1902. {*****************************************************************************
  1903. TONNODE
  1904. *****************************************************************************}
  1905. constructor tonnode.create(l,r:tnode);
  1906. begin
  1907. inherited create(onn,l,r);
  1908. excepTSymtable:=nil;
  1909. excepttype:=nil;
  1910. end;
  1911. destructor tonnode.destroy;
  1912. begin
  1913. { copied nodes don't need to release the symtable }
  1914. if assigned(excepTSymtable) then
  1915. excepTSymtable.free;
  1916. inherited destroy;
  1917. end;
  1918. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1919. begin
  1920. inherited ppuload(t,ppufile);
  1921. excepTSymtable:=nil;
  1922. excepttype:=nil;
  1923. end;
  1924. function tonnode.dogetcopy : tnode;
  1925. var
  1926. n : tonnode;
  1927. begin
  1928. n:=tonnode(inherited dogetcopy);
  1929. if assigned(exceptsymtable) then
  1930. n.exceptsymtable:=exceptsymtable.getcopy
  1931. else
  1932. n.exceptsymtable:=nil;
  1933. n.excepttype:=excepttype;
  1934. result:=n;
  1935. end;
  1936. function tonnode.pass_typecheck:tnode;
  1937. begin
  1938. result:=nil;
  1939. resultdef:=voidtype;
  1940. if not is_class(excepttype) and
  1941. not is_javaclass(excepttype) then
  1942. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1943. if assigned(left) then
  1944. typecheckpass(left);
  1945. if assigned(right) then
  1946. typecheckpass(right);
  1947. end;
  1948. function tonnode.pass_1 : tnode;
  1949. begin
  1950. result:=nil;
  1951. include(current_procinfo.flags,pi_do_call);
  1952. { Loads exception class VMT, therefore may need GOT
  1953. (generic code only; descendants may need to avoid this check) }
  1954. if (cs_create_pic in current_settings.moduleswitches) and
  1955. (tf_pic_uses_got in target_info.flags) then
  1956. include(current_procinfo.flags,pi_needs_got);
  1957. expectloc:=LOC_VOID;
  1958. if assigned(left) then
  1959. firstpass(left);
  1960. if assigned(right) then
  1961. firstpass(right);
  1962. end;
  1963. function tonnode.docompare(p: tnode): boolean;
  1964. begin
  1965. docompare := false;
  1966. end;
  1967. end.