nflw.pas 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  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. symnot,
  25. symtype,symbase,symdef,symsym,
  26. optloop;
  27. type
  28. { flags used by loop nodes }
  29. tloopflag = (
  30. { set if it is a for ... downto ... do loop }
  31. lnf_backward,
  32. { Do we need to parse childs to set var state? }
  33. lnf_varstate,
  34. { Do a test at the begin of the loop?}
  35. lnf_testatbegin,
  36. { Negate the loop test? }
  37. lnf_checknegate,
  38. { Should the value of the loop variable on exit be correct. }
  39. lnf_dont_mind_loopvar_on_exit,
  40. { Loop simplify flag }
  41. lnf_simplify_processing);
  42. tloopflags = set of tloopflag;
  43. const
  44. { loop flags which must match to consider loop nodes equal regarding the flags }
  45. loopflagsequal = [lnf_backward];
  46. type
  47. tlabelnode = class;
  48. tloopnode = class(tbinarynode)
  49. t1,t2 : tnode;
  50. loopflags : tloopflags;
  51. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  52. destructor destroy;override;
  53. function dogetcopy : tnode;override;
  54. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  55. procedure ppuwrite(ppufile:tcompilerppufile);override;
  56. procedure buildderefimpl;override;
  57. procedure derefimpl;override;
  58. procedure insertintolist(l : tnodelist);override;
  59. procedure printnodetree(var t:text);override;
  60. function docompare(p: tnode): boolean; override;
  61. end;
  62. twhilerepeatnode = class(tloopnode)
  63. constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
  64. function pass_typecheck:tnode;override;
  65. function pass_1 : tnode;override;
  66. {$ifdef state_tracking}
  67. function track_state_pass(exec_known:boolean):boolean;override;
  68. {$endif}
  69. end;
  70. twhilerepeatnodeclass = class of twhilerepeatnode;
  71. tifnode = class(tloopnode)
  72. constructor create(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. procedure loop_var_access(not_type:Tnotification_flag;symbol:Tsym);
  90. function wrap_to_value:tnode;
  91. function pass_typecheck:tnode;override;
  92. function pass_1 : tnode;override;
  93. function simplify(forinline : boolean) : tnode;override;
  94. end;
  95. tfornodeclass = class of tfornode;
  96. texitnode = class(tunarynode)
  97. constructor create(l:tnode);virtual;
  98. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  99. procedure ppuwrite(ppufile:tcompilerppufile);override;
  100. function pass_typecheck:tnode;override;
  101. function pass_1 : tnode;override;
  102. property resultexpr : tnode read left write left;
  103. end;
  104. texitnodeclass = class of texitnode;
  105. tbreaknode = class(tnode)
  106. constructor create;virtual;
  107. function pass_typecheck:tnode;override;
  108. function pass_1 : tnode;override;
  109. end;
  110. tbreaknodeclass = class of tbreaknode;
  111. tcontinuenode = class(tnode)
  112. constructor create;virtual;
  113. function pass_typecheck:tnode;override;
  114. function pass_1 : tnode;override;
  115. end;
  116. tcontinuenodeclass = class of tcontinuenode;
  117. tgotonode = class(tnode)
  118. private
  119. labelnodeidx : longint;
  120. public
  121. labelsym : tlabelsym;
  122. labelnode : tlabelnode;
  123. exceptionblock : integer;
  124. constructor create(p : tlabelsym);virtual;
  125. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  126. procedure ppuwrite(ppufile:tcompilerppufile);override;
  127. procedure buildderefimpl;override;
  128. procedure derefimpl;override;
  129. procedure resolveppuidx;override;
  130. function dogetcopy : tnode;override;
  131. function pass_typecheck:tnode;override;
  132. function pass_1 : tnode;override;
  133. function docompare(p: tnode): boolean; override;
  134. end;
  135. tgotonodeclass = class of tgotonode;
  136. tlabelnode = class(tunarynode)
  137. exceptionblock : integer;
  138. { when copying trees, this points to the newly created copy of a label }
  139. copiedto : tlabelnode;
  140. labsym : tlabelsym;
  141. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  142. destructor destroy;override;
  143. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  144. procedure ppuwrite(ppufile:tcompilerppufile);override;
  145. procedure buildderefimpl;override;
  146. procedure derefimpl;override;
  147. function dogetcopy : tnode;override;
  148. function pass_typecheck:tnode;override;
  149. function pass_1 : tnode;override;
  150. function docompare(p: tnode): boolean; override;
  151. end;
  152. tlabelnodeclass = class of tlabelnode;
  153. traisenode = class(ttertiarynode)
  154. constructor create(l,taddr,tframe:tnode);virtual;
  155. function pass_typecheck:tnode;override;
  156. function pass_1 : tnode;override;
  157. end;
  158. traisenodeclass = class of traisenode;
  159. ttryexceptnode = class(tloopnode)
  160. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  161. function pass_typecheck:tnode;override;
  162. function pass_1 : tnode;override;
  163. end;
  164. ttryexceptnodeclass = class of ttryexceptnode;
  165. ttryfinallynode = class(tloopnode)
  166. implicitframe : boolean;
  167. constructor create(l,r:tnode);virtual;reintroduce;
  168. constructor create_implicit(l,r,_t1:tnode);virtual;
  169. function pass_typecheck:tnode;override;
  170. function pass_1 : tnode;override;
  171. function simplify(forinline:boolean): tnode;override;
  172. protected
  173. function create_finalizer_procdef: tprocdef;
  174. end;
  175. ttryfinallynodeclass = class of ttryfinallynode;
  176. tonnode = class(tbinarynode)
  177. excepTSymtable : TSymtable;
  178. excepttype : tobjectdef;
  179. constructor create(l,r:tnode);virtual;
  180. destructor destroy;override;
  181. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  182. function pass_typecheck:tnode;override;
  183. function pass_1 : tnode;override;
  184. function dogetcopy : tnode;override;
  185. function docompare(p: tnode): boolean; override;
  186. end;
  187. tonnodeclass = class of tonnode;
  188. var
  189. cwhilerepeatnode : twhilerepeatnodeclass=twhilerepeatnode;
  190. cifnode : tifnodeclass = tifnode;
  191. cfornode : tfornodeclass = tfornode;
  192. cexitnode : texitnodeclass = texitnode;
  193. cgotonode : tgotonodeclass = tgotonode;
  194. clabelnode : tlabelnodeclass = tlabelnode;
  195. craisenode : traisenodeclass = traisenode;
  196. ctryexceptnode : ttryexceptnodeclass = ttryexceptnode;
  197. ctryfinallynode : ttryfinallynodeclass = ttryfinallynode;
  198. connode : tonnodeclass = tonnode;
  199. cbreaknode : tbreaknodeclass = tbreaknode;
  200. ccontinuenode : tcontinuenodeclass = tcontinuenode;
  201. // for-in loop helpers
  202. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  203. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  204. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  205. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  206. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  207. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  208. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  209. implementation
  210. uses
  211. globtype,systems,constexp,
  212. cutils,verbose,globals,
  213. symconst,symtable,paramgr,defcmp,defutil,htypechk,pass_1,
  214. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,nutils,ninl,nset,
  215. pdecsub,
  216. {$ifdef state_tracking}
  217. nstate,
  218. {$endif}
  219. {$ifdef i8086}
  220. cpuinfo,
  221. {$endif i8086}
  222. cgbase,procinfo
  223. ;
  224. // for-in loop helpers
  225. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  226. begin
  227. result:=cfornode.create(hloopvar,
  228. cinlinenode.create(in_low_x,false,expr.getcopy),
  229. cinlinenode.create(in_high_x,false,expr.getcopy),
  230. hloopbody,
  231. false);
  232. end;
  233. function create_objc_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  234. var
  235. mainstatement, outerloopbodystatement, innerloopbodystatement, tempstatement: tstatementnode;
  236. state, mutationcheck, currentamount, innerloopcounter, items, expressiontemp: ttempcreatenode;
  237. outerloop, innerloop, hp: tnode;
  238. itemsarraydef: tarraydef;
  239. sym: tsym;
  240. begin
  241. { Objective-C enumerators require Objective-C 2.0 }
  242. if not(m_objectivec2 in current_settings.modeswitches) then
  243. begin
  244. result:=cerrornode.create;
  245. MessagePos(expr.fileinfo,parser_e_objc_enumerator_2_0);
  246. exit;
  247. end;
  248. { Requires the NSFastEnumeration protocol and NSFastEnumerationState
  249. record }
  250. maybeloadcocoatypes;
  251. if not assigned(objc_fastenumeration) or
  252. not assigned(objc_fastenumerationstate) then
  253. begin
  254. result:=cerrornode.create;
  255. MessagePos(expr.fileinfo,parser_e_objc_missing_enumeration_defs);
  256. exit;
  257. end;
  258. (* Original code:
  259. for hloopvar in expression do
  260. <hloopbody>
  261. Pascal code equivalent into which it has to be transformed
  262. (sure would be nice if the compiler had some kind of templates ;) :
  263. var
  264. state: NSFastEnumerationState;
  265. expressiontemp: NSFastEnumerationProtocol;
  266. mutationcheck,
  267. currentamount,
  268. innerloopcounter: culong;
  269. { size can be increased/decreased if desired }
  270. items: array[1..16] of id;
  271. begin
  272. fillchar(state,sizeof(state),0);
  273. expressiontemp:=expression;
  274. repeat
  275. currentamount:=expressiontemp.countByEnumeratingWithState_objects_count(@state,@items,length(items));
  276. if currentamount=0 then
  277. begin
  278. { "The iterating variable is set to nil when the loop ends by
  279. exhausting the source pool of objects" }
  280. hloopvar:=nil;
  281. break;
  282. end;
  283. mutationcheck:=state.mutationsptr^;
  284. innerloopcounter:=culong(-1);
  285. repeat
  286. { at the start so that "continue" in <loopbody> works correctly }
  287. { don't use for-loop, because then the value of the iteration
  288. counter is undefined on exit and we have to check it in the
  289. outer repeat/until condition }
  290. {$push}
  291. {$r-,q-}
  292. inc(innerloopcounter);
  293. {$pop}
  294. if innerloopcounter=currentamount then
  295. break;
  296. if mutationcheck<>state.mutationsptr^ then
  297. { raises Objective-C exception... }
  298. objc_enumerationMutation(expressiontemp);
  299. hloopvar:=state.itemsPtr[innerloopcounter];
  300. { if continue in loopbody -> jumps to start, increases count and checks }
  301. { if break in loopbody: goes to outer repeat/until and innerloopcount
  302. will be < currentamount -> stops }
  303. <hloopbody>
  304. until false;
  305. { if the inner loop terminated early, "break" was used and we have
  306. to stop }
  307. { "If the loop is terminated early, the iterating variable is left
  308. pointing to the last iteration item." }
  309. until innerloopcounter<currentamount;
  310. end;
  311. *)
  312. result:=internalstatements(mainstatement);
  313. { the fast enumeration state }
  314. state:=ctempcreatenode.create(objc_fastenumerationstate,objc_fastenumerationstate.size,tt_persistent,false);
  315. typecheckpass(tnode(state));
  316. addstatement(mainstatement,state);
  317. { the temporary items array }
  318. itemsarraydef:=tarraydef.create(1,16,u32inttype);
  319. itemsarraydef.elementdef:=objc_idtype;
  320. items:=ctempcreatenode.create(itemsarraydef,itemsarraydef.size,tt_persistent,false);
  321. addstatement(mainstatement,items);
  322. typecheckpass(tnode(items));
  323. { temp for the expression/collection through which we iterate }
  324. expressiontemp:=ctempcreatenode.create(objc_fastenumeration,objc_fastenumeration.size,tt_persistent,true);
  325. addstatement(mainstatement,expressiontemp);
  326. { currentamount temp (not really clean: we use ptruint instead of
  327. culong) }
  328. currentamount:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  329. typecheckpass(tnode(currentamount));
  330. addstatement(mainstatement,currentamount);
  331. { mutationcheck temp (idem) }
  332. mutationcheck:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  333. typecheckpass(tnode(mutationcheck));
  334. addstatement(mainstatement,mutationcheck);
  335. { innerloopcounter temp (idem) }
  336. innerloopcounter:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  337. typecheckpass(tnode(innerloopcounter));
  338. addstatement(mainstatement,innerloopcounter);
  339. { initialise the state with 0 }
  340. addstatement(mainstatement,ccallnode.createinternfromunit('SYSTEM','FILLCHAR',
  341. ccallparanode.create(genintconstnode(0),
  342. ccallparanode.create(genintconstnode(objc_fastenumerationstate.size),
  343. ccallparanode.create(ctemprefnode.create(state),nil)
  344. )
  345. )
  346. ));
  347. { this will also check whether the expression (potentially) conforms
  348. to the NSFastEnumeration protocol (use expr.getcopy, because the
  349. caller will free expr) }
  350. addstatement(mainstatement,cassignmentnode.create(ctemprefnode.create(expressiontemp),expr.getcopy));
  351. { we add the "repeat..until" afterwards, now just create the body }
  352. outerloop:=internalstatements(outerloopbodystatement);
  353. { the countByEnumeratingWithState_objects_count call }
  354. hp:=ccallparanode.create(cinlinenode.create(in_length_x,false,ctypenode.create(itemsarraydef)),
  355. ccallparanode.create(caddrnode.create(ctemprefnode.create(items)),
  356. ccallparanode.create(caddrnode.create(ctemprefnode.create(state)),nil)
  357. )
  358. );
  359. sym:=search_struct_member(objc_fastenumeration,'COUNTBYENUMERATINGWITHSTATE_OBJECTS_COUNT');
  360. if not assigned(sym) or
  361. (sym.typ<>procsym) then
  362. internalerror(2010061901);
  363. hp:=ccallnode.create(hp,tprocsym(sym),sym.owner,ctemprefnode.create(expressiontemp),[]);
  364. addstatement(outerloopbodystatement,cassignmentnode.create(
  365. ctemprefnode.create(currentamount),hp));
  366. { if currentamount = 0, bail out (use copy of hloopvar, because we
  367. have to use it again below) }
  368. hp:=internalstatements(tempstatement);
  369. addstatement(tempstatement,cassignmentnode.create(
  370. hloopvar.getcopy,cnilnode.create));
  371. addstatement(tempstatement,cbreaknode.create);
  372. addstatement(outerloopbodystatement,cifnode.create(
  373. caddnode.create(equaln,ctemprefnode.create(currentamount),genintconstnode(0)),
  374. hp,nil));
  375. { initial value of mutationcheck }
  376. hp:=ctemprefnode.create(state);
  377. typecheckpass(hp);
  378. hp:=cderefnode.create(genloadfield(hp,'MUTATIONSPTR'));
  379. addstatement(outerloopbodystatement,cassignmentnode.create(
  380. ctemprefnode.create(mutationcheck),hp));
  381. { initialise innerloopcounter }
  382. addstatement(outerloopbodystatement,cassignmentnode.create(
  383. ctemprefnode.create(innerloopcounter),cordconstnode.create(-1,ptruinttype,false)));
  384. { and now the inner loop, again adding the repeat/until afterwards }
  385. innerloop:=internalstatements(innerloopbodystatement);
  386. { inc(innerloopcounter) without range/overflowchecking (because
  387. we go from culong(-1) to 0 during the first iteration }
  388. hp:=cinlinenode.create(
  389. in_inc_x,false,ccallparanode.create(
  390. ctemprefnode.create(innerloopcounter),nil));
  391. hp.localswitches:=hp.localswitches-[cs_check_range,cs_check_overflow];
  392. addstatement(innerloopbodystatement,hp);
  393. { if innerloopcounter=currentamount then break to the outer loop }
  394. addstatement(innerloopbodystatement,cifnode.create(
  395. caddnode.create(equaln,
  396. ctemprefnode.create(innerloopcounter),
  397. ctemprefnode.create(currentamount)),
  398. cbreaknode.create,
  399. nil));
  400. { verify that the collection didn't change in the mean time }
  401. hp:=ctemprefnode.create(state);
  402. typecheckpass(hp);
  403. addstatement(innerloopbodystatement,cifnode.create(
  404. caddnode.create(unequaln,
  405. ctemprefnode.create(mutationcheck),
  406. cderefnode.create(genloadfield(hp,'MUTATIONSPTR'))
  407. ),
  408. ccallnode.createinternfromunit('OBJC','OBJC_ENUMERATIONMUTATION',
  409. ccallparanode.create(ctemprefnode.create(expressiontemp),nil)),
  410. nil));
  411. { finally: actually get the next element }
  412. hp:=ctemprefnode.create(state);
  413. typecheckpass(hp);
  414. hp:=genloadfield(hp,'ITEMSPTR');
  415. typecheckpass(hp);
  416. { don't simply use a vecn, because indexing a pointer won't work in
  417. non-FPC modes }
  418. if hp.resultdef.typ<>pointerdef then
  419. internalerror(2010061904);
  420. inserttypeconv(hp,
  421. tarraydef.create_from_pointer(tpointerdef(hp.resultdef).pointeddef));
  422. hp:=cvecnode.create(hp,ctemprefnode.create(innerloopcounter));
  423. addstatement(innerloopbodystatement,
  424. cassignmentnode.create(hloopvar,hp));
  425. { the actual loop body! }
  426. addstatement(innerloopbodystatement,hloopbody);
  427. { create the inner repeat/until and add it to the body of the outer
  428. one }
  429. hp:=cwhilerepeatnode.create(
  430. { repeat .. until false }
  431. cordconstnode.create(0,pasbool8type,false),innerloop,false,true);
  432. addstatement(outerloopbodystatement,hp);
  433. { create the outer repeat/until and add it to the the main body }
  434. hp:=cwhilerepeatnode.create(
  435. { repeat .. until innerloopcounter<currentamount }
  436. caddnode.create(ltn,
  437. ctemprefnode.create(innerloopcounter),
  438. ctemprefnode.create(currentamount)),
  439. outerloop,false,true);
  440. addstatement(mainstatement,hp);
  441. { release the temps }
  442. addstatement(mainstatement,ctempdeletenode.create(state));
  443. addstatement(mainstatement,ctempdeletenode.create(mutationcheck));
  444. addstatement(mainstatement,ctempdeletenode.create(currentamount));
  445. addstatement(mainstatement,ctempdeletenode.create(innerloopcounter));
  446. addstatement(mainstatement,ctempdeletenode.create(items));
  447. addstatement(mainstatement,ctempdeletenode.create(expressiontemp));
  448. end;
  449. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  450. var
  451. loopstatement, loopbodystatement: tstatementnode;
  452. loopvar, stringvar: ttempcreatenode;
  453. stringindex, loopbody, forloopnode: tnode;
  454. begin
  455. { result is a block of statements }
  456. result:=internalstatements(loopstatement);
  457. { create a temp variable for expression }
  458. stringvar := ctempcreatenode.create(
  459. expr.resultdef,
  460. expr.resultdef.size,
  461. tt_persistent,true);
  462. addstatement(loopstatement,stringvar);
  463. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(stringvar),expr.getcopy));
  464. { create a loop counter: signed integer with size of string length }
  465. loopvar := ctempcreatenode.create(
  466. sinttype,
  467. sinttype.size,
  468. tt_persistent,true);
  469. addstatement(loopstatement,loopvar);
  470. stringindex:=ctemprefnode.create(loopvar);
  471. loopbody:=internalstatements(loopbodystatement);
  472. // for-in loop variable := string_expression[index]
  473. addstatement(loopbodystatement,
  474. cassignmentnode.create(hloopvar, cvecnode.create(ctemprefnode.create(stringvar),stringindex)));
  475. { add the actual statement to the loop }
  476. addstatement(loopbodystatement,hloopbody);
  477. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  478. genintconstnode(1),
  479. cinlinenode.create(in_length_x,false,ctemprefnode.create(stringvar)),
  480. loopbody,
  481. false);
  482. addstatement(loopstatement,forloopnode);
  483. { free the loop counter }
  484. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  485. { free the temp variable for expression }
  486. addstatement(loopstatement,ctempdeletenode.create(stringvar));
  487. end;
  488. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  489. var
  490. loopstatement, loopbodystatement: tstatementnode;
  491. loopvar, arrayvar: ttempcreatenode;
  492. arrayindex, lowbound, highbound, loopbody, forloopnode, expression: tnode;
  493. is_string: boolean;
  494. tmpdef, convertdef: tdef;
  495. elementcount: aword;
  496. begin
  497. expression := expr;
  498. { result is a block of statements }
  499. result:=internalstatements(loopstatement);
  500. is_string:=ado_IsConstString in tarraydef(expr.resultdef).arrayoptions;
  501. // if array element type <> loovar type then create a conversion if possible
  502. if compare_defs(tarraydef(expression.resultdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible then
  503. begin
  504. tmpdef:=expression.resultdef;
  505. elementcount:=1;
  506. while assigned(tmpdef) and (tmpdef.typ=arraydef) and
  507. (tarraydef(tmpdef).arrayoptions = []) and
  508. (compare_defs(tarraydef(tmpdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible) do
  509. begin
  510. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  511. tmpdef:=tarraydef(tmpdef).elementdef;
  512. end;
  513. if assigned(tmpdef) and (tmpdef.typ=arraydef) and (tarraydef(tmpdef).arrayoptions = []) then
  514. begin
  515. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  516. convertdef:=tarraydef.create(0,elementcount-1,s32inttype);
  517. tarraydef(convertdef).elementdef:=tarraydef(tmpdef).elementdef;
  518. expression:=expr.getcopy;
  519. expression:=ctypeconvnode.create_internal(expression,convertdef);
  520. typecheckpass(expression);
  521. addstatement(loopstatement,expression);
  522. end;
  523. end;
  524. if (node_complexity(expression) > 1) and not is_open_array(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(true);
  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. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  1265. symbol:Tsym);
  1266. begin
  1267. {If there is a read access, the value of the loop counter is important;
  1268. at the end of the loop the loop variable should contain the value it
  1269. had in the last iteration.}
  1270. if not_type=vn_onwrite then
  1271. begin
  1272. writeln('Loopvar does not matter on exit');
  1273. end
  1274. else
  1275. begin
  1276. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  1277. writeln('Loopvar does matter on exit');
  1278. end;
  1279. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  1280. end;
  1281. function tfornode.simplify(forinline : boolean) : tnode;
  1282. begin
  1283. result:=nil;
  1284. { Can we spare the first comparision? }
  1285. if (t1.nodetype=ordconstn) and
  1286. (right.nodetype=ordconstn) and
  1287. (
  1288. (
  1289. (lnf_backward in loopflags) and
  1290. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1291. ) or
  1292. (
  1293. not(lnf_backward in loopflags) and
  1294. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1295. )
  1296. ) then
  1297. exclude(loopflags,lnf_testatbegin);
  1298. if (t1.nodetype=ordconstn) and
  1299. (right.nodetype=ordconstn) and
  1300. (
  1301. (
  1302. (lnf_backward in loopflags) and
  1303. (tordconstnode(right).value<tordconstnode(t1).value)
  1304. ) or
  1305. (
  1306. not(lnf_backward in loopflags) and
  1307. (tordconstnode(right).value>tordconstnode(t1).value)
  1308. )
  1309. ) then
  1310. result:=cnothingnode.create;
  1311. end;
  1312. function tfornode.wrap_to_value:tnode;
  1313. var
  1314. statements: tstatementnode;
  1315. temp: ttempcreatenode;
  1316. begin
  1317. result:=internalstatements(statements);
  1318. temp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1319. addstatement(statements,temp);
  1320. addstatement(statements,cassignmentnode.create(
  1321. ctemprefnode.create(temp),
  1322. t1));
  1323. { create a new for node, it is cheaper than cloning entire loop body }
  1324. addstatement(statements,cfornode.create(
  1325. left,right,ctemprefnode.create(temp),t2,lnf_backward in loopflags));
  1326. addstatement(statements,ctempdeletenode.create(temp));
  1327. { all child nodes are reused }
  1328. left:=nil;
  1329. right:=nil;
  1330. t1:=nil;
  1331. t2:=nil;
  1332. end;
  1333. function tfornode.pass_typecheck:tnode;
  1334. var
  1335. res : tnode;
  1336. begin
  1337. result:=nil;
  1338. resultdef:=voidtype;
  1339. { process the loopvar, from and to, varstates are already set }
  1340. typecheckpass(left);
  1341. typecheckpass(right);
  1342. typecheckpass(t1);
  1343. set_varstate(left,vs_written,[]);
  1344. { loop unrolling }
  1345. if cs_opt_loopunroll in current_settings.optimizerswitches then
  1346. begin
  1347. res:=t2.simplify(false);
  1348. if assigned(res) then
  1349. t2:=res;
  1350. res:=unroll_loop(self);
  1351. if assigned(res) then
  1352. begin
  1353. typecheckpass(res);
  1354. result:=res;
  1355. exit;
  1356. end;
  1357. end;
  1358. { Make sure that the loop var and the
  1359. from and to values are compatible types }
  1360. check_ranges(right.fileinfo,right,left.resultdef);
  1361. inserttypeconv(right,left.resultdef);
  1362. check_ranges(t1.fileinfo,t1,left.resultdef);
  1363. inserttypeconv(t1,left.resultdef);
  1364. if assigned(t2) then
  1365. typecheckpass(t2);
  1366. end;
  1367. function tfornode.pass_1 : tnode;
  1368. begin
  1369. result:=nil;
  1370. expectloc:=LOC_VOID;
  1371. firstpass(left);
  1372. firstpass(right);
  1373. firstpass(t1);
  1374. if assigned(t2) then
  1375. firstpass(t2);
  1376. if codegenerror then
  1377. exit;
  1378. { 'to' value must be evaluated once before loop, so its possible modifications
  1379. inside loop body do not affect the number of iterations (see webtbs/tw8883). }
  1380. if not (t1.nodetype in [ordconstn,temprefn]) then
  1381. result:=wrap_to_value;
  1382. end;
  1383. {*****************************************************************************
  1384. TEXITNODE
  1385. *****************************************************************************}
  1386. constructor texitnode.create(l:tnode);
  1387. begin
  1388. inherited create(exitn,l);
  1389. if assigned(left) then
  1390. begin
  1391. { add assignment to funcretsym }
  1392. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1393. left:=cassignmentnode.create(
  1394. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1395. left);
  1396. end;
  1397. end;
  1398. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1399. begin
  1400. inherited ppuload(t,ppufile);
  1401. end;
  1402. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1403. begin
  1404. inherited ppuwrite(ppufile);
  1405. end;
  1406. function texitnode.pass_typecheck:tnode;
  1407. var
  1408. newstatement : tstatementnode;
  1409. begin
  1410. result:=nil;
  1411. if assigned(left) then
  1412. begin
  1413. result:=internalstatements(newstatement);
  1414. addstatement(newstatement,left);
  1415. left:=nil;
  1416. addstatement(newstatement,self.getcopy);
  1417. end;
  1418. resultdef:=voidtype;
  1419. end;
  1420. function texitnode.pass_1 : tnode;
  1421. begin
  1422. result:=nil;
  1423. expectloc:=LOC_VOID;
  1424. if assigned(left) then
  1425. internalerror(2011052801);
  1426. end;
  1427. {*****************************************************************************
  1428. TBREAKNODE
  1429. *****************************************************************************}
  1430. constructor tbreaknode.create;
  1431. begin
  1432. inherited create(breakn);
  1433. end;
  1434. function tbreaknode.pass_typecheck:tnode;
  1435. begin
  1436. result:=nil;
  1437. resultdef:=voidtype;
  1438. end;
  1439. function tbreaknode.pass_1 : tnode;
  1440. begin
  1441. result:=nil;
  1442. expectloc:=LOC_VOID;
  1443. end;
  1444. {*****************************************************************************
  1445. TCONTINUENODE
  1446. *****************************************************************************}
  1447. constructor tcontinuenode.create;
  1448. begin
  1449. inherited create(continuen);
  1450. end;
  1451. function tcontinuenode.pass_typecheck:tnode;
  1452. begin
  1453. result:=nil;
  1454. resultdef:=voidtype;
  1455. end;
  1456. function tcontinuenode.pass_1 : tnode;
  1457. begin
  1458. result:=nil;
  1459. expectloc:=LOC_VOID;
  1460. end;
  1461. {*****************************************************************************
  1462. TGOTONODE
  1463. *****************************************************************************}
  1464. constructor tgotonode.create(p : tlabelsym);
  1465. begin
  1466. inherited create(goton);
  1467. exceptionblock:=current_exceptblock;
  1468. labelnode:=nil;
  1469. labelsym:=p;
  1470. end;
  1471. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1472. begin
  1473. inherited ppuload(t,ppufile);
  1474. labelnodeidx:=ppufile.getlongint;
  1475. exceptionblock:=ppufile.getbyte;
  1476. end;
  1477. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1478. begin
  1479. inherited ppuwrite(ppufile);
  1480. labelnodeidx:=labelnode.ppuidx;
  1481. ppufile.putlongint(labelnodeidx);
  1482. ppufile.putbyte(exceptionblock);
  1483. end;
  1484. procedure tgotonode.buildderefimpl;
  1485. begin
  1486. inherited buildderefimpl;
  1487. end;
  1488. procedure tgotonode.derefimpl;
  1489. begin
  1490. inherited derefimpl;
  1491. end;
  1492. procedure tgotonode.resolveppuidx;
  1493. begin
  1494. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  1495. if labelnode.nodetype<>labeln then
  1496. internalerror(200809021);
  1497. end;
  1498. function tgotonode.pass_typecheck:tnode;
  1499. begin
  1500. result:=nil;
  1501. resultdef:=voidtype;
  1502. end;
  1503. function tgotonode.pass_1 : tnode;
  1504. var
  1505. p2 : tprocinfo;
  1506. begin
  1507. result:=nil;
  1508. expectloc:=LOC_VOID;
  1509. { The labelnode can already be set when
  1510. this node was copied }
  1511. if not(assigned(labelnode)) then
  1512. begin
  1513. { inner procedure goto? }
  1514. if assigned(labelsym.code) and
  1515. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1516. { generated by the optimizer? }
  1517. not(assigned(labelsym.owner))) then
  1518. labelnode:=tlabelnode(labelsym.code)
  1519. else if ((m_non_local_goto in current_settings.modeswitches) and
  1520. assigned(labelsym.owner)) or
  1521. { nested exits don't need the non local goto switch }
  1522. (labelsym.realname='$nestedexit') then
  1523. begin
  1524. if current_procinfo.procdef.parast.symtablelevel>labelsym.owner.symtablelevel then
  1525. begin
  1526. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  1527. if exceptionblock>0 then
  1528. CGMessage(cg_e_goto_inout_of_exception_block);
  1529. { goto across procedures using exception?
  1530. this is not allowed because we cannot
  1531. easily unwind the exception frame
  1532. stack
  1533. }
  1534. p2:=current_procinfo;
  1535. while true do
  1536. begin
  1537. if (p2.flags*[pi_needs_implicit_finally,pi_uses_exceptions,pi_has_implicit_finally])<>[] then
  1538. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  1539. if labelsym.owner=p2.procdef.localst then
  1540. break;
  1541. p2:=p2.parent
  1542. end;
  1543. if assigned(labelsym.jumpbuf) then
  1544. begin
  1545. labelsym.nonlocal:=true;
  1546. exclude(current_procinfo.procdef.procoptions,po_inline);
  1547. result:=ccallnode.createintern('fpc_longjmp',
  1548. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  1549. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  1550. nil)));
  1551. end
  1552. else
  1553. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1554. end
  1555. else
  1556. CGMessage(cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  1557. end
  1558. else
  1559. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1560. end;
  1561. { check if we don't mess with exception blocks }
  1562. if assigned(labelnode) and
  1563. (exceptionblock<>labelnode.exceptionblock) then
  1564. CGMessage(cg_e_goto_inout_of_exception_block);
  1565. end;
  1566. function tgotonode.dogetcopy : tnode;
  1567. var
  1568. p : tgotonode;
  1569. begin
  1570. p:=tgotonode(inherited dogetcopy);
  1571. p.exceptionblock:=exceptionblock;
  1572. { generate labelnode if not done yet }
  1573. if not(assigned(labelnode)) then
  1574. begin
  1575. if assigned(labelsym) and assigned(labelsym.code) then
  1576. labelnode:=tlabelnode(labelsym.code)
  1577. end;
  1578. p.labelsym:=labelsym;
  1579. if assigned(labelnode) then
  1580. p.labelnode:=tlabelnode(labelnode.dogetcopy)
  1581. else
  1582. begin
  1583. { don't trigger IE when there was already an error, i.e. the
  1584. label is not defined. See tw11763 (PFV) }
  1585. if (errorcount=0) and
  1586. { don't trigger IE if it's a global goto }
  1587. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1588. not(assigned(labelsym.owner))) then
  1589. internalerror(200610291);
  1590. end;
  1591. result:=p;
  1592. end;
  1593. function tgotonode.docompare(p: tnode): boolean;
  1594. begin
  1595. docompare := false;
  1596. end;
  1597. {*****************************************************************************
  1598. TLABELNODE
  1599. *****************************************************************************}
  1600. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  1601. begin
  1602. inherited create(labeln,l);
  1603. exceptionblock:=current_exceptblock;
  1604. labsym:=alabsym;
  1605. { Register labelnode in labelsym }
  1606. labsym.code:=self;
  1607. end;
  1608. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1609. begin
  1610. inherited ppuload(t,ppufile);
  1611. exceptionblock:=ppufile.getbyte;
  1612. end;
  1613. destructor tlabelnode.destroy;
  1614. begin
  1615. { Remove reference in labelsym, this is to prevent
  1616. goto's to this label }
  1617. if assigned(labsym) and (labsym.code=pointer(self)) then
  1618. labsym.code:=nil;
  1619. inherited destroy;
  1620. end;
  1621. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  1622. begin
  1623. inherited ppuwrite(ppufile);
  1624. ppufile.putbyte(exceptionblock);
  1625. end;
  1626. procedure tlabelnode.buildderefimpl;
  1627. begin
  1628. inherited buildderefimpl;
  1629. end;
  1630. procedure tlabelnode.derefimpl;
  1631. begin
  1632. inherited derefimpl;
  1633. end;
  1634. function tlabelnode.pass_typecheck:tnode;
  1635. begin
  1636. result:=nil;
  1637. { left could still be unassigned }
  1638. if assigned(left) then
  1639. typecheckpass(left);
  1640. resultdef:=voidtype;
  1641. end;
  1642. function tlabelnode.pass_1 : tnode;
  1643. begin
  1644. result:=nil;
  1645. expectloc:=LOC_VOID;
  1646. include(current_procinfo.flags,pi_has_label);
  1647. if assigned(labsym) and labsym.nonlocal then
  1648. begin
  1649. include(current_procinfo.flags,pi_has_interproclabel);
  1650. exclude(current_procinfo.procdef.procoptions,po_inline);
  1651. end;
  1652. if assigned(left) then
  1653. firstpass(left);
  1654. if (m_non_local_goto in current_settings.modeswitches) and
  1655. { the owner can be Nil for internal labels }
  1656. assigned(labsym.owner) and
  1657. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  1658. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  1659. end;
  1660. function tlabelnode.dogetcopy : tnode;
  1661. begin
  1662. if not(assigned(copiedto)) then
  1663. copiedto:=tlabelnode(inherited dogetcopy);
  1664. copiedto.exceptionblock:=exceptionblock;
  1665. result:=copiedto;
  1666. end;
  1667. function tlabelnode.docompare(p: tnode): boolean;
  1668. begin
  1669. docompare := false;
  1670. end;
  1671. {*****************************************************************************
  1672. TRAISENODE
  1673. *****************************************************************************}
  1674. constructor traisenode.create(l,taddr,tframe:tnode);
  1675. begin
  1676. inherited create(raisen,l,taddr,tframe);
  1677. end;
  1678. function traisenode.pass_typecheck:tnode;
  1679. begin
  1680. result:=nil;
  1681. resultdef:=voidtype;
  1682. if assigned(left) then
  1683. begin
  1684. { first para must be a _class_ }
  1685. typecheckpass(left);
  1686. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1687. if codegenerror then
  1688. exit;
  1689. if not is_class(left.resultdef) and
  1690. not is_javaclass(left.resultdef) then
  1691. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  1692. { insert needed typeconvs for addr,frame }
  1693. if assigned(right) then
  1694. begin
  1695. { addr }
  1696. typecheckpass(right);
  1697. {$ifdef i8086}
  1698. if current_settings.x86memorymodel in x86_far_code_models then
  1699. inserttypeconv(right,voidfarpointertype)
  1700. else
  1701. inserttypeconv(right,voidnearpointertype);
  1702. {$else i8086}
  1703. inserttypeconv(right,voidpointertype);
  1704. {$endif i8086}
  1705. { frame }
  1706. if assigned(third) then
  1707. begin
  1708. typecheckpass(third);
  1709. inserttypeconv(third,voidpointertype);
  1710. end;
  1711. end;
  1712. end;
  1713. end;
  1714. function traisenode.pass_1 : tnode;
  1715. var
  1716. statements : tstatementnode;
  1717. current_addr : tlabelnode;
  1718. raisenode : tcallnode;
  1719. begin
  1720. result:=internalstatements(statements);
  1721. if assigned(left) then
  1722. begin
  1723. { first para must be a class }
  1724. firstpass(left);
  1725. { insert needed typeconvs for addr,frame }
  1726. if assigned(right) then
  1727. begin
  1728. { addr }
  1729. firstpass(right);
  1730. { frame }
  1731. if assigned(third) then
  1732. firstpass(third)
  1733. else
  1734. third:=cpointerconstnode.Create(0,voidpointertype);
  1735. end
  1736. else
  1737. begin
  1738. third:=cinlinenode.create(in_get_frame,false,nil);
  1739. current_addr:=clabelnode.create(cnothingnode.create,tlabelsym.create('$raiseaddr'));
  1740. addstatement(statements,current_addr);
  1741. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  1742. end;
  1743. raisenode:=ccallnode.createintern('fpc_raiseexception',
  1744. ccallparanode.create(third,
  1745. ccallparanode.create(right,
  1746. ccallparanode.create(left,nil)))
  1747. );
  1748. include(raisenode.callnodeflags,cnf_call_never_returns);
  1749. addstatement(statements,raisenode);
  1750. end
  1751. else
  1752. begin
  1753. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  1754. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  1755. include(raisenode.callnodeflags,cnf_call_never_returns);
  1756. addstatement(statements,raisenode);
  1757. end;
  1758. left:=nil;
  1759. right:=nil;
  1760. third:=nil;
  1761. end;
  1762. {*****************************************************************************
  1763. TTRYEXCEPTNODE
  1764. *****************************************************************************}
  1765. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1766. begin
  1767. inherited create(tryexceptn,l,r,_t1,nil);
  1768. end;
  1769. function ttryexceptnode.pass_typecheck:tnode;
  1770. begin
  1771. result:=nil;
  1772. typecheckpass(left);
  1773. { on statements }
  1774. if assigned(right) then
  1775. typecheckpass(right);
  1776. { else block }
  1777. if assigned(t1) then
  1778. typecheckpass(t1);
  1779. resultdef:=voidtype;
  1780. end;
  1781. function ttryexceptnode.pass_1 : tnode;
  1782. begin
  1783. result:=nil;
  1784. expectloc:=LOC_VOID;
  1785. firstpass(left);
  1786. { on statements }
  1787. if assigned(right) then
  1788. firstpass(right);
  1789. { else block }
  1790. if assigned(t1) then
  1791. firstpass(t1);
  1792. include(current_procinfo.flags,pi_do_call);
  1793. include(current_procinfo.flags,pi_uses_exceptions);
  1794. inc(current_procinfo.estimatedtempsize,get_jumpbuf_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. inc(current_procinfo.estimatedtempsize,get_jumpbuf_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:=tprocdef.create(max(normal_function_level,st.symtablelevel)+1);
  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:=tprocsym.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. {*****************************************************************************
  1899. TONNODE
  1900. *****************************************************************************}
  1901. constructor tonnode.create(l,r:tnode);
  1902. begin
  1903. inherited create(onn,l,r);
  1904. excepTSymtable:=nil;
  1905. excepttype:=nil;
  1906. end;
  1907. destructor tonnode.destroy;
  1908. begin
  1909. { copied nodes don't need to release the symtable }
  1910. if assigned(excepTSymtable) then
  1911. excepTSymtable.free;
  1912. inherited destroy;
  1913. end;
  1914. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1915. begin
  1916. inherited ppuload(t,ppufile);
  1917. excepTSymtable:=nil;
  1918. excepttype:=nil;
  1919. end;
  1920. function tonnode.dogetcopy : tnode;
  1921. var
  1922. n : tonnode;
  1923. begin
  1924. n:=tonnode(inherited dogetcopy);
  1925. if assigned(exceptsymtable) then
  1926. n.exceptsymtable:=exceptsymtable.getcopy
  1927. else
  1928. n.exceptsymtable:=nil;
  1929. n.excepttype:=excepttype;
  1930. result:=n;
  1931. end;
  1932. function tonnode.pass_typecheck:tnode;
  1933. begin
  1934. result:=nil;
  1935. resultdef:=voidtype;
  1936. if not is_class(excepttype) and
  1937. not is_javaclass(excepttype) then
  1938. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1939. if assigned(left) then
  1940. typecheckpass(left);
  1941. if assigned(right) then
  1942. typecheckpass(right);
  1943. end;
  1944. function tonnode.pass_1 : tnode;
  1945. begin
  1946. result:=nil;
  1947. include(current_procinfo.flags,pi_do_call);
  1948. { Loads exception class VMT, therefore may need GOT
  1949. (generic code only; descendants may need to avoid this check) }
  1950. if (cs_create_pic in current_settings.moduleswitches) and
  1951. (tf_pic_uses_got in target_info.flags) then
  1952. include(current_procinfo.flags,pi_needs_got);
  1953. expectloc:=LOC_VOID;
  1954. if assigned(left) then
  1955. firstpass(left);
  1956. if assigned(right) then
  1957. firstpass(right);
  1958. end;
  1959. function tonnode.docompare(p: tnode): boolean;
  1960. begin
  1961. docompare := false;
  1962. end;
  1963. end.