nflw.pas 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279
  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:=carraydef.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. carraydef.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:=carraydef.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
  525. not(is_open_array(expression.resultdef)) and not(is_array_of_const(expression.resultdef)) then
  526. begin
  527. { create a temp variable for expression }
  528. arrayvar := ctempcreatenode.create(
  529. expression.resultdef,
  530. expression.resultdef.size,
  531. tt_persistent,true);
  532. if is_string then
  533. begin
  534. lowbound:=genintconstnode(1);
  535. highbound:=cinlinenode.create(in_length_x,false,ctemprefnode.create(arrayvar))
  536. end
  537. else
  538. begin
  539. lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
  540. highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
  541. end;
  542. addstatement(loopstatement,arrayvar);
  543. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(arrayvar),expression.getcopy));
  544. end
  545. else
  546. begin
  547. arrayvar:=nil;
  548. if is_string then
  549. begin
  550. lowbound:=genintconstnode(1);
  551. highbound:=cinlinenode.create(in_length_x,false,expression.getcopy);
  552. end
  553. else
  554. begin
  555. lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
  556. highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
  557. end;
  558. end;
  559. { create a loop counter }
  560. loopvar := ctempcreatenode.create(
  561. tarraydef(expression.resultdef).rangedef,
  562. tarraydef(expression.resultdef).rangedef.size,
  563. tt_persistent,true);
  564. addstatement(loopstatement,loopvar);
  565. arrayindex:=ctemprefnode.create(loopvar);
  566. loopbody:=internalstatements(loopbodystatement);
  567. // for-in loop variable := array_expression[index]
  568. if assigned(arrayvar) then
  569. addstatement(loopbodystatement,
  570. cassignmentnode.create(hloopvar,cvecnode.create(ctemprefnode.create(arrayvar),arrayindex)))
  571. else
  572. addstatement(loopbodystatement,
  573. cassignmentnode.create(hloopvar,cvecnode.create(expression.getcopy,arrayindex)));
  574. { add the actual statement to the loop }
  575. addstatement(loopbodystatement,hloopbody);
  576. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  577. lowbound,
  578. highbound,
  579. loopbody,
  580. false);
  581. addstatement(loopstatement,forloopnode);
  582. { free the loop counter }
  583. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  584. { free the temp variable for expression if needed }
  585. if arrayvar<>nil then
  586. addstatement(loopstatement,ctempdeletenode.create(arrayvar));
  587. end;
  588. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  589. var
  590. loopstatement, loopbodystatement: tstatementnode;
  591. loopvar, setvar: ttempcreatenode;
  592. loopbody, forloopnode: tnode;
  593. begin
  594. // first check is set is empty and if it so then skip other processing
  595. if not Assigned(tsetdef(expr.resultdef).elementdef) then
  596. begin
  597. result:=cnothingnode.create;
  598. // free unused nodes
  599. hloopvar.free;
  600. hloopbody.free;
  601. exit;
  602. end;
  603. { result is a block of statements }
  604. result:=internalstatements(loopstatement);
  605. { create a temp variable for expression }
  606. setvar := ctempcreatenode.create(
  607. expr.resultdef,
  608. expr.resultdef.size,
  609. tt_persistent,true);
  610. addstatement(loopstatement,setvar);
  611. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(setvar),expr.getcopy));
  612. { create a loop counter }
  613. loopvar := ctempcreatenode.create(
  614. tsetdef(expr.resultdef).elementdef,
  615. tsetdef(expr.resultdef).elementdef.size,
  616. tt_persistent,true);
  617. addstatement(loopstatement,loopvar);
  618. // if loopvar in set then
  619. // begin
  620. // hloopvar := loopvar
  621. // for-in loop body
  622. // end
  623. loopbody:=cifnode.create(
  624. cinnode.create(ctemprefnode.create(loopvar),ctemprefnode.create(setvar)),
  625. internalstatements(loopbodystatement),
  626. nil);
  627. addstatement(loopbodystatement,cassignmentnode.create(hloopvar,ctemprefnode.create(loopvar)));
  628. { add the actual statement to the loop }
  629. addstatement(loopbodystatement,hloopbody);
  630. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  631. cinlinenode.create(in_low_x,false,ctemprefnode.create(setvar)),
  632. cinlinenode.create(in_high_x,false,ctemprefnode.create(setvar)),
  633. loopbody,
  634. false);
  635. addstatement(loopstatement,forloopnode);
  636. { free the loop counter }
  637. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  638. { free the temp variable for expression }
  639. addstatement(loopstatement,ctempdeletenode.create(setvar));
  640. end;
  641. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  642. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  643. var
  644. loopstatement, loopbodystatement: tstatementnode;
  645. enumvar: ttempcreatenode;
  646. loopbody, whileloopnode,
  647. enum_get, enum_move, enum_current, enum_get_params: tnode;
  648. propaccesslist: tpropaccesslist;
  649. enumerator_is_class: boolean;
  650. enumerator_destructor: tprocdef;
  651. begin
  652. { result is a block of statements }
  653. result:=internalstatements(loopstatement);
  654. enumerator_is_class := is_class(enumerator_get.returndef);
  655. { create a temp variable for enumerator }
  656. enumvar := ctempcreatenode.create(
  657. enumerator_get.returndef,
  658. enumerator_get.returndef.size,
  659. tt_persistent,true);
  660. addstatement(loopstatement,enumvar);
  661. if enumerator_get.proctypeoption=potype_operator then
  662. begin
  663. enum_get_params:=ccallparanode.create(expr.getcopy,nil);
  664. enum_get:=ccallnode.create(enum_get_params, tprocsym(enumerator_get.procsym), nil, nil, []);
  665. tcallnode(enum_get).procdefinition:=enumerator_get;
  666. addsymref(enumerator_get.procsym);
  667. end
  668. else
  669. enum_get:=ccallnode.create(nil, tprocsym(enumerator_get.procsym), enumerator_get.owner, expr.getcopy, []);
  670. addstatement(loopstatement,
  671. cassignmentnode.create(
  672. ctemprefnode.create(enumvar),
  673. enum_get
  674. ));
  675. loopbody:=internalstatements(loopbodystatement);
  676. { for-in loop variable := enumerator.current }
  677. if enumerator_current.getpropaccesslist(palt_read,propaccesslist) then
  678. begin
  679. case propaccesslist.firstsym^.sym.typ of
  680. fieldvarsym :
  681. begin
  682. { generate access code }
  683. enum_current:=ctemprefnode.create(enumvar);
  684. propaccesslist_to_node(enum_current,enumerator_current.owner,propaccesslist);
  685. include(enum_current.flags,nf_isproperty);
  686. end;
  687. procsym :
  688. begin
  689. { generate the method call }
  690. enum_current:=ccallnode.create(nil,tprocsym(propaccesslist.firstsym^.sym),enumerator_current.owner,ctemprefnode.create(enumvar),[]);
  691. include(enum_current.flags,nf_isproperty);
  692. end
  693. else
  694. begin
  695. enum_current:=cerrornode.create;
  696. Message(type_e_mismatch);
  697. end;
  698. end;
  699. end
  700. else
  701. enum_current:=cerrornode.create;
  702. addstatement(loopbodystatement,
  703. cassignmentnode.create(hloopvar, enum_current));
  704. { add the actual statement to the loop }
  705. addstatement(loopbodystatement,hloopbody);
  706. enum_move:=ccallnode.create(nil, tprocsym(enumerator_move.procsym), enumerator_move.owner, ctemprefnode.create(enumvar), []);
  707. whileloopnode:=cwhilerepeatnode.create(enum_move,loopbody,true,false);
  708. if enumerator_is_class then
  709. begin
  710. { insert a try-finally and call the destructor for the enumerator in the finally section }
  711. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  712. if assigned(enumerator_destructor) then
  713. begin
  714. whileloopnode:=ctryfinallynode.create(
  715. whileloopnode, // try node
  716. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym), // finally node
  717. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[]));
  718. end;
  719. { if getenumerator <> nil then do the loop }
  720. whileloopnode:=cifnode.create(
  721. caddnode.create(unequaln, ctemprefnode.create(enumvar), cnilnode.create),
  722. whileloopnode,
  723. nil);
  724. end;
  725. addstatement(loopstatement, whileloopnode);
  726. if is_object(enumerator_get.returndef) then
  727. begin
  728. // call the object destructor too
  729. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  730. if assigned(enumerator_destructor) then
  731. begin
  732. addstatement(loopstatement,
  733. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym),
  734. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[]));
  735. end;
  736. end;
  737. { free the temp variable for enumerator }
  738. addstatement(loopstatement,ctempdeletenode.create(enumvar));
  739. end;
  740. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  741. var
  742. pd, movenext: tprocdef;
  743. helperdef: tobjectdef;
  744. current: tpropertysym;
  745. storefilepos: tfileposinfo;
  746. begin
  747. storefilepos:=current_filepos;
  748. current_filepos:=hloopvar.fileinfo;
  749. if expr.nodetype=typen then
  750. begin
  751. if (expr.resultdef.typ=enumdef) and tenumdef(expr.resultdef).has_jumps then
  752. begin
  753. result:=cerrornode.create;
  754. hloopvar.free;
  755. hloopbody.free;
  756. MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
  757. end
  758. else
  759. result:=create_type_for_in_loop(hloopvar, hloopbody, expr);
  760. end
  761. else
  762. begin
  763. { loop is made for an expression }
  764. // Objective-C uses different conventions (and it's only supported for Objective-C 2.0)
  765. if is_objc_class_or_protocol(hloopvar.resultdef) or
  766. is_objc_class_or_protocol(expr.resultdef) then
  767. begin
  768. result:=create_objc_for_in_loop(hloopvar,hloopbody,expr);
  769. if result.nodetype=errorn then
  770. begin
  771. hloopvar.free;
  772. hloopbody.free;
  773. end;
  774. end
  775. else
  776. begin
  777. // search for operator first
  778. pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
  779. // if there is no operator then search for class/object enumerator method
  780. if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
  781. begin
  782. { first search using the helper hierarchy }
  783. if search_last_objectpascal_helper(tabstractrecorddef(expr.resultdef),nil,helperdef) then
  784. repeat
  785. pd:=helperdef.search_enumerator_get;
  786. helperdef:=helperdef.childof;
  787. until (pd<>nil) or (helperdef=nil);
  788. { we didn't find an enumerator in a helper, so search in the
  789. class/record/object itself }
  790. if pd=nil then
  791. pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
  792. end;
  793. if pd<>nil then
  794. begin
  795. // seach movenext and current symbols
  796. movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
  797. if movenext = nil then
  798. begin
  799. result:=cerrornode.create;
  800. hloopvar.free;
  801. hloopbody.free;
  802. MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
  803. end
  804. else
  805. begin
  806. current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  807. if current = nil then
  808. begin
  809. result:=cerrornode.create;
  810. hloopvar.free;
  811. hloopbody.free;
  812. MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
  813. end
  814. else
  815. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  816. end;
  817. end
  818. else
  819. begin
  820. case expr.resultdef.typ of
  821. stringdef: result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  822. arraydef: result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  823. setdef: result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  824. else
  825. begin
  826. result:=cerrornode.create;
  827. hloopvar.free;
  828. hloopbody.free;
  829. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  830. end;
  831. end;
  832. end;
  833. end;
  834. end;
  835. current_filepos:=storefilepos;
  836. end;
  837. {****************************************************************************
  838. TLOOPNODE
  839. *****************************************************************************}
  840. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  841. begin
  842. inherited create(tt,l,r);
  843. t1:=_t1;
  844. t2:=_t2;
  845. fileinfo:=l.fileinfo;
  846. end;
  847. destructor tloopnode.destroy;
  848. begin
  849. t1.free;
  850. t2.free;
  851. inherited destroy;
  852. end;
  853. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  854. begin
  855. inherited ppuload(t,ppufile);
  856. t1:=ppuloadnode(ppufile);
  857. t2:=ppuloadnode(ppufile);
  858. ppufile.getsmallset(loopflags);
  859. end;
  860. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  861. begin
  862. inherited ppuwrite(ppufile);
  863. ppuwritenode(ppufile,t1);
  864. ppuwritenode(ppufile,t2);
  865. ppufile.putsmallset(loopflags);
  866. end;
  867. procedure tloopnode.buildderefimpl;
  868. begin
  869. inherited buildderefimpl;
  870. if assigned(t1) then
  871. t1.buildderefimpl;
  872. if assigned(t2) then
  873. t2.buildderefimpl;
  874. end;
  875. procedure tloopnode.derefimpl;
  876. begin
  877. inherited derefimpl;
  878. if assigned(t1) then
  879. t1.derefimpl;
  880. if assigned(t2) then
  881. t2.derefimpl;
  882. end;
  883. function tloopnode.dogetcopy : tnode;
  884. var
  885. p : tloopnode;
  886. begin
  887. p:=tloopnode(inherited dogetcopy);
  888. if assigned(t1) then
  889. p.t1:=t1.dogetcopy
  890. else
  891. p.t1:=nil;
  892. if assigned(t2) then
  893. p.t2:=t2.dogetcopy
  894. else
  895. p.t2:=nil;
  896. p.loopflags:=loopflags;
  897. dogetcopy:=p;
  898. end;
  899. procedure tloopnode.insertintolist(l : tnodelist);
  900. begin
  901. end;
  902. procedure tloopnode.printnodetree(var t:text);
  903. begin
  904. write(t,printnodeindention,'(');
  905. printnodeindent;
  906. printnodeinfo(t);
  907. writeln(t);
  908. printnode(t,left);
  909. printnode(t,right);
  910. printnode(t,t1);
  911. printnode(t,t2);
  912. printnodeunindent;
  913. writeln(t,printnodeindention,')');
  914. end;
  915. function tloopnode.docompare(p: tnode): boolean;
  916. begin
  917. docompare :=
  918. inherited docompare(p) and
  919. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  920. t1.isequal(tloopnode(p).t1) and
  921. t2.isequal(tloopnode(p).t2);
  922. end;
  923. {****************************************************************************
  924. TWHILEREPEATNODE
  925. *****************************************************************************}
  926. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  927. begin
  928. inherited create(whilerepeatn,l,r,nil,nil);
  929. if tab then
  930. include(loopflags, lnf_testatbegin);
  931. if cn then
  932. include(loopflags,lnf_checknegate);
  933. end;
  934. function twhilerepeatnode.pass_typecheck:tnode;
  935. var
  936. t:Tunarynode;
  937. begin
  938. result:=nil;
  939. resultdef:=voidtype;
  940. typecheckpass(left);
  941. { tp procvar support }
  942. maybe_call_procvar(left,true);
  943. {A not node can be removed.}
  944. if left.nodetype=notn then
  945. begin
  946. t:=Tunarynode(left);
  947. left:=Tunarynode(left).left;
  948. t.left:=nil;
  949. t.destroy;
  950. {Symdif operator, in case you are wondering:}
  951. loopflags:=loopflags >< [lnf_checknegate];
  952. end;
  953. { loop instruction }
  954. if assigned(right) then
  955. typecheckpass(right);
  956. set_varstate(left,vs_read,[vsf_must_be_valid]);
  957. if codegenerror then
  958. exit;
  959. if not(is_boolean(left.resultdef)) and
  960. not(is_typeparam(left.resultdef)) then
  961. inserttypeconv(left,pasbool8type);
  962. { Give warnings for code that will never be executed for
  963. while false do }
  964. if (lnf_testatbegin in loopflags) and
  965. (left.nodetype=ordconstn) and
  966. (tordconstnode(left).value.uvalue=0) and
  967. assigned(right) then
  968. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  969. end;
  970. {$ifdef prefetchnext}
  971. type
  972. passignmentquery = ^tassignmentquery;
  973. tassignmentquery = record
  974. towhat: tnode;
  975. source: tassignmentnode;
  976. statementcount: cardinal;
  977. end;
  978. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  979. var
  980. query: passignmentquery absolute arg;
  981. temp, prederef: tnode;
  982. begin
  983. result := fen_norecurse_false;
  984. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  985. inc(query^.statementcount);
  986. { make sure there's something else in the loop besides going to the }
  987. { next item }
  988. if (query^.statementcount > 1) and
  989. (n.nodetype = assignn) then
  990. begin
  991. { skip type conversions of assignment target }
  992. temp := tassignmentnode(n).left;
  993. while (temp.nodetype = typeconvn) do
  994. temp := ttypeconvnode(temp).left;
  995. { assignment to x of the while assigned(x) check? }
  996. if not(temp.isequal(query^.towhat)) then
  997. exit;
  998. { right hand side of assignment dereferenced field of }
  999. { x? (no derefn in case of class) }
  1000. temp := tassignmentnode(n).right;
  1001. while (temp.nodetype = typeconvn) do
  1002. temp := ttypeconvnode(temp).left;
  1003. if (temp.nodetype <> subscriptn) then
  1004. exit;
  1005. prederef := tsubscriptnode(temp).left;
  1006. temp := prederef;
  1007. while (temp.nodetype = typeconvn) do
  1008. temp := ttypeconvnode(temp).left;
  1009. { see tests/test/prefetch1.pp }
  1010. if (temp.nodetype = derefn) then
  1011. temp := tderefnode(temp).left
  1012. else
  1013. temp := prederef;
  1014. if temp.isequal(query^.towhat) then
  1015. begin
  1016. query^.source := tassignmentnode(n);
  1017. result := fen_norecurse_true;
  1018. end
  1019. end
  1020. { don't check nodes which can't contain an assignment or whose }
  1021. { final assignment can vary a lot }
  1022. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1023. result := fen_false;
  1024. end;
  1025. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1026. var
  1027. query: tassignmentquery;
  1028. begin
  1029. query.towhat := towhat;
  1030. query.source := nil;
  1031. query.statementcount := 0;
  1032. if foreachnodestatic(where,@checkassignment,@query) then
  1033. result := query.source
  1034. else
  1035. result := nil;
  1036. end;
  1037. {$endif prefetchnext}
  1038. function twhilerepeatnode.pass_1 : tnode;
  1039. {$ifdef prefetchnext}
  1040. var
  1041. runnernode, prefetchcode: tnode;
  1042. assignmentnode: tassignmentnode;
  1043. prefetchstatements: tstatementnode;
  1044. {$endif prefetchnext}
  1045. begin
  1046. result:=nil;
  1047. expectloc:=LOC_VOID;
  1048. firstpass(left);
  1049. if codegenerror then
  1050. exit;
  1051. { loop instruction }
  1052. if assigned(right) then
  1053. begin
  1054. firstpass(right);
  1055. if codegenerror then
  1056. exit;
  1057. end;
  1058. {$ifdef prefetchnext}
  1059. { do at the end so all complex typeconversions are already }
  1060. { converted to calln's }
  1061. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1062. (lnf_testatbegin in loopflags) then
  1063. begin
  1064. { get first component of the while check }
  1065. runnernode := left;
  1066. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1067. runnernode := tunarynode(runnernode).left;
  1068. { is it an assigned(x) check? }
  1069. if ((runnernode.nodetype = inlinen) and
  1070. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1071. ((runnernode.nodetype = unequaln) and
  1072. (taddnode(runnernode).right.nodetype = niln)) then
  1073. begin
  1074. runnernode := tunarynode(runnernode).left;
  1075. { in case of in_assigned_x, there's a callparan in between }
  1076. if (runnernode.nodetype = callparan) then
  1077. runnernode := tcallparanode(runnernode).left;
  1078. while (runnernode.nodetype = typeconvn) do
  1079. runnernode := ttypeconvnode(runnernode).left;
  1080. { is there an "x := x(^).somefield"? }
  1081. assignmentnode := findassignment(right,runnernode);
  1082. if assigned(assignmentnode) then
  1083. begin
  1084. prefetchcode := internalstatements(prefetchstatements);
  1085. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1086. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1087. addstatement(prefetchstatements,right);
  1088. right := prefetchcode;
  1089. typecheckpass(right);
  1090. end;
  1091. end;
  1092. end;
  1093. {$endif prefetchnext}
  1094. end;
  1095. {$ifdef state_tracking}
  1096. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1097. var condition:Tnode;
  1098. code:Tnode;
  1099. done:boolean;
  1100. value:boolean;
  1101. change:boolean;
  1102. firsttest:boolean;
  1103. factval:Tnode;
  1104. begin
  1105. track_state_pass:=false;
  1106. done:=false;
  1107. firsttest:=true;
  1108. {For repeat until statements, first do a pass through the code.}
  1109. if not(lnf_testatbegin in flags) then
  1110. begin
  1111. code:=right.getcopy;
  1112. if code.track_state_pass(exec_known) then
  1113. track_state_pass:=true;
  1114. code.destroy;
  1115. end;
  1116. repeat
  1117. condition:=left.getcopy;
  1118. code:=right.getcopy;
  1119. change:=condition.track_state_pass(exec_known);
  1120. factval:=aktstate.find_fact(left);
  1121. if factval<>nil then
  1122. begin
  1123. condition.destroy;
  1124. condition:=factval.getcopy;
  1125. change:=true;
  1126. end;
  1127. if change then
  1128. begin
  1129. track_state_pass:=true;
  1130. {Force new resultdef pass.}
  1131. condition.resultdef:=nil;
  1132. do_typecheckpass(condition);
  1133. end;
  1134. if is_constboolnode(condition) then
  1135. begin
  1136. {Try to turn a while loop into a repeat loop.}
  1137. if firsttest then
  1138. exclude(flags,testatbegin);
  1139. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1140. if value then
  1141. begin
  1142. if code.track_state_pass(exec_known) then
  1143. track_state_pass:=true;
  1144. end
  1145. else
  1146. done:=true;
  1147. end
  1148. else
  1149. begin
  1150. {Remove any modified variables from the state.}
  1151. code.track_state_pass(false);
  1152. done:=true;
  1153. end;
  1154. code.destroy;
  1155. condition.destroy;
  1156. firsttest:=false;
  1157. until done;
  1158. {The loop condition is also known, for example:
  1159. while i<10 do
  1160. begin
  1161. ...
  1162. end;
  1163. When the loop is done, we do know that i<10 = false.
  1164. }
  1165. condition:=left.getcopy;
  1166. if condition.track_state_pass(exec_known) then
  1167. begin
  1168. track_state_pass:=true;
  1169. {Force new resultdef pass.}
  1170. condition.resultdef:=nil;
  1171. do_typecheckpass(condition);
  1172. end;
  1173. if not is_constboolnode(condition) then
  1174. aktstate.store_fact(condition,
  1175. cordconstnode.create(byte(checknegate),pasbool8type,true))
  1176. else
  1177. condition.destroy;
  1178. end;
  1179. {$endif}
  1180. {*****************************************************************************
  1181. TIFNODE
  1182. *****************************************************************************}
  1183. constructor tifnode.create(l,r,_t1 : tnode);
  1184. begin
  1185. inherited create(ifn,l,r,_t1,nil);
  1186. end;
  1187. function tifnode.internalsimplify(warn: boolean) : tnode;
  1188. begin
  1189. result:=nil;
  1190. { optimize constant expressions }
  1191. if (left.nodetype=ordconstn) then
  1192. begin
  1193. if tordconstnode(left).value.uvalue<>0 then
  1194. begin
  1195. if assigned(right) then
  1196. result:=right
  1197. else
  1198. result:=cnothingnode.create;
  1199. right:=nil;
  1200. if warn and assigned(t1) then
  1201. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1202. end
  1203. else
  1204. begin
  1205. if assigned(t1) then
  1206. result:=t1
  1207. else
  1208. result:=cnothingnode.create;
  1209. t1:=nil;
  1210. if warn and assigned(right) then
  1211. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1212. end;
  1213. end;
  1214. end;
  1215. function tifnode.simplify(forinline : boolean) : tnode;
  1216. begin
  1217. result:=internalsimplify(false);
  1218. end;
  1219. function tifnode.pass_typecheck:tnode;
  1220. begin
  1221. result:=nil;
  1222. resultdef:=voidtype;
  1223. typecheckpass(left);
  1224. { tp procvar support }
  1225. maybe_call_procvar(left,true);
  1226. { if path }
  1227. if assigned(right) then
  1228. typecheckpass(right);
  1229. { else path }
  1230. if assigned(t1) then
  1231. typecheckpass(t1);
  1232. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1233. if codegenerror then
  1234. exit;
  1235. if not(is_boolean(left.resultdef)) and
  1236. not(is_typeparam(left.resultdef)) then
  1237. inserttypeconv(left,pasbool8type);
  1238. result:=internalsimplify(true);
  1239. end;
  1240. function tifnode.pass_1 : tnode;
  1241. begin
  1242. result:=nil;
  1243. expectloc:=LOC_VOID;
  1244. firstpass(left);
  1245. { if path }
  1246. if assigned(right) then
  1247. firstpass(right);
  1248. { else path }
  1249. if assigned(t1) then
  1250. firstpass(t1);
  1251. { leave if we've got an error in one of the paths }
  1252. if codegenerror then
  1253. exit;
  1254. end;
  1255. {*****************************************************************************
  1256. TFORNODE
  1257. *****************************************************************************}
  1258. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1259. begin
  1260. inherited create(forn,l,r,_t1,_t2);
  1261. if back then
  1262. include(loopflags,lnf_backward);
  1263. include(loopflags,lnf_testatbegin);
  1264. end;
  1265. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  1266. symbol:Tsym);
  1267. begin
  1268. {If there is a read access, the value of the loop counter is important;
  1269. at the end of the loop the loop variable should contain the value it
  1270. had in the last iteration.}
  1271. if not_type=vn_onwrite then
  1272. begin
  1273. writeln('Loopvar does not matter on exit');
  1274. end
  1275. else
  1276. begin
  1277. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  1278. writeln('Loopvar does matter on exit');
  1279. end;
  1280. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  1281. end;
  1282. function tfornode.simplify(forinline : boolean) : tnode;
  1283. begin
  1284. result:=nil;
  1285. { Can we spare the first comparision? }
  1286. if (t1.nodetype=ordconstn) and
  1287. (right.nodetype=ordconstn) and
  1288. (
  1289. (
  1290. (lnf_backward in loopflags) and
  1291. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1292. ) or
  1293. (
  1294. not(lnf_backward in loopflags) and
  1295. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1296. )
  1297. ) then
  1298. exclude(loopflags,lnf_testatbegin);
  1299. if (t1.nodetype=ordconstn) and
  1300. (right.nodetype=ordconstn) and
  1301. (
  1302. (
  1303. (lnf_backward in loopflags) and
  1304. (tordconstnode(right).value<tordconstnode(t1).value)
  1305. ) or
  1306. (
  1307. not(lnf_backward in loopflags) and
  1308. (tordconstnode(right).value>tordconstnode(t1).value)
  1309. )
  1310. ) then
  1311. result:=cnothingnode.create;
  1312. end;
  1313. function tfornode.wrap_to_value:tnode;
  1314. var
  1315. statements: tstatementnode;
  1316. temp: ttempcreatenode;
  1317. begin
  1318. result:=internalstatements(statements);
  1319. temp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1320. addstatement(statements,temp);
  1321. addstatement(statements,cassignmentnode.create(
  1322. ctemprefnode.create(temp),
  1323. t1));
  1324. { create a new for node, it is cheaper than cloning entire loop body }
  1325. addstatement(statements,cfornode.create(
  1326. left,right,ctemprefnode.create(temp),t2,lnf_backward in loopflags));
  1327. addstatement(statements,ctempdeletenode.create(temp));
  1328. { all child nodes are reused }
  1329. left:=nil;
  1330. right:=nil;
  1331. t1:=nil;
  1332. t2:=nil;
  1333. end;
  1334. function tfornode.pass_typecheck:tnode;
  1335. var
  1336. res : tnode;
  1337. begin
  1338. result:=nil;
  1339. resultdef:=voidtype;
  1340. { process the loopvar, from and to, varstates are already set }
  1341. typecheckpass(left);
  1342. typecheckpass(right);
  1343. typecheckpass(t1);
  1344. set_varstate(left,vs_written,[]);
  1345. { loop unrolling }
  1346. if cs_opt_loopunroll in current_settings.optimizerswitches then
  1347. begin
  1348. res:=t2.simplify(false);
  1349. if assigned(res) then
  1350. t2:=res;
  1351. res:=unroll_loop(self);
  1352. if assigned(res) then
  1353. begin
  1354. typecheckpass(res);
  1355. result:=res;
  1356. exit;
  1357. end;
  1358. end;
  1359. { Make sure that the loop var and the
  1360. from and to values are compatible types }
  1361. check_ranges(right.fileinfo,right,left.resultdef);
  1362. inserttypeconv(right,left.resultdef);
  1363. check_ranges(t1.fileinfo,t1,left.resultdef);
  1364. inserttypeconv(t1,left.resultdef);
  1365. if assigned(t2) then
  1366. typecheckpass(t2);
  1367. end;
  1368. function tfornode.pass_1 : tnode;
  1369. begin
  1370. result:=nil;
  1371. expectloc:=LOC_VOID;
  1372. firstpass(left);
  1373. firstpass(right);
  1374. firstpass(t1);
  1375. if assigned(t2) then
  1376. firstpass(t2);
  1377. if codegenerror then
  1378. exit;
  1379. { 'to' value must be evaluated once before loop, so its possible modifications
  1380. inside loop body do not affect the number of iterations (see webtbs/tw8883). }
  1381. if not (t1.nodetype in [ordconstn,temprefn]) then
  1382. result:=wrap_to_value;
  1383. end;
  1384. {*****************************************************************************
  1385. TEXITNODE
  1386. *****************************************************************************}
  1387. constructor texitnode.create(l:tnode);
  1388. begin
  1389. inherited create(exitn,l);
  1390. if assigned(left) then
  1391. begin
  1392. { add assignment to funcretsym }
  1393. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1394. left:=cassignmentnode.create(
  1395. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1396. left);
  1397. end;
  1398. end;
  1399. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1400. begin
  1401. inherited ppuload(t,ppufile);
  1402. end;
  1403. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1404. begin
  1405. inherited ppuwrite(ppufile);
  1406. end;
  1407. function texitnode.pass_typecheck:tnode;
  1408. var
  1409. newstatement : tstatementnode;
  1410. begin
  1411. result:=nil;
  1412. if assigned(left) then
  1413. begin
  1414. result:=internalstatements(newstatement);
  1415. addstatement(newstatement,left);
  1416. left:=nil;
  1417. addstatement(newstatement,self.getcopy);
  1418. end;
  1419. resultdef:=voidtype;
  1420. end;
  1421. function texitnode.pass_1 : tnode;
  1422. begin
  1423. result:=nil;
  1424. expectloc:=LOC_VOID;
  1425. if assigned(left) then
  1426. internalerror(2011052801);
  1427. end;
  1428. {*****************************************************************************
  1429. TBREAKNODE
  1430. *****************************************************************************}
  1431. constructor tbreaknode.create;
  1432. begin
  1433. inherited create(breakn);
  1434. end;
  1435. function tbreaknode.pass_typecheck:tnode;
  1436. begin
  1437. result:=nil;
  1438. resultdef:=voidtype;
  1439. end;
  1440. function tbreaknode.pass_1 : tnode;
  1441. begin
  1442. result:=nil;
  1443. expectloc:=LOC_VOID;
  1444. end;
  1445. {*****************************************************************************
  1446. TCONTINUENODE
  1447. *****************************************************************************}
  1448. constructor tcontinuenode.create;
  1449. begin
  1450. inherited create(continuen);
  1451. end;
  1452. function tcontinuenode.pass_typecheck:tnode;
  1453. begin
  1454. result:=nil;
  1455. resultdef:=voidtype;
  1456. end;
  1457. function tcontinuenode.pass_1 : tnode;
  1458. begin
  1459. result:=nil;
  1460. expectloc:=LOC_VOID;
  1461. end;
  1462. {*****************************************************************************
  1463. TGOTONODE
  1464. *****************************************************************************}
  1465. constructor tgotonode.create(p : tlabelsym);
  1466. begin
  1467. inherited create(goton);
  1468. exceptionblock:=current_exceptblock;
  1469. labelnode:=nil;
  1470. labelsym:=p;
  1471. end;
  1472. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1473. begin
  1474. inherited ppuload(t,ppufile);
  1475. labelnodeidx:=ppufile.getlongint;
  1476. exceptionblock:=ppufile.getbyte;
  1477. end;
  1478. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1479. begin
  1480. inherited ppuwrite(ppufile);
  1481. labelnodeidx:=labelnode.ppuidx;
  1482. ppufile.putlongint(labelnodeidx);
  1483. ppufile.putbyte(exceptionblock);
  1484. end;
  1485. procedure tgotonode.buildderefimpl;
  1486. begin
  1487. inherited buildderefimpl;
  1488. end;
  1489. procedure tgotonode.derefimpl;
  1490. begin
  1491. inherited derefimpl;
  1492. end;
  1493. procedure tgotonode.resolveppuidx;
  1494. begin
  1495. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  1496. if labelnode.nodetype<>labeln then
  1497. internalerror(200809021);
  1498. end;
  1499. function tgotonode.pass_typecheck:tnode;
  1500. begin
  1501. result:=nil;
  1502. resultdef:=voidtype;
  1503. end;
  1504. function tgotonode.pass_1 : tnode;
  1505. var
  1506. p2 : tprocinfo;
  1507. begin
  1508. result:=nil;
  1509. expectloc:=LOC_VOID;
  1510. { The labelnode can already be set when
  1511. this node was copied }
  1512. if not(assigned(labelnode)) then
  1513. begin
  1514. { inner procedure goto? }
  1515. if assigned(labelsym.code) and
  1516. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1517. { generated by the optimizer? }
  1518. not(assigned(labelsym.owner))) then
  1519. labelnode:=tlabelnode(labelsym.code)
  1520. else if ((m_non_local_goto in current_settings.modeswitches) and
  1521. assigned(labelsym.owner)) or
  1522. { nested exits don't need the non local goto switch }
  1523. (labelsym.realname='$nestedexit') then
  1524. begin
  1525. if current_procinfo.procdef.parast.symtablelevel>labelsym.owner.symtablelevel then
  1526. begin
  1527. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  1528. if exceptionblock>0 then
  1529. CGMessage(cg_e_goto_inout_of_exception_block);
  1530. { goto across procedures using exception?
  1531. this is not allowed because we cannot
  1532. easily unwind the exception frame
  1533. stack
  1534. }
  1535. p2:=current_procinfo;
  1536. while true do
  1537. begin
  1538. if (p2.flags*[pi_needs_implicit_finally,pi_uses_exceptions,pi_has_implicit_finally])<>[] then
  1539. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  1540. if labelsym.owner=p2.procdef.localst then
  1541. break;
  1542. p2:=p2.parent
  1543. end;
  1544. if assigned(labelsym.jumpbuf) then
  1545. begin
  1546. labelsym.nonlocal:=true;
  1547. exclude(current_procinfo.procdef.procoptions,po_inline);
  1548. result:=ccallnode.createintern('fpc_longjmp',
  1549. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  1550. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  1551. nil)));
  1552. end
  1553. else
  1554. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1555. end
  1556. else
  1557. CGMessage(cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  1558. end
  1559. else
  1560. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1561. end;
  1562. { check if we don't mess with exception blocks }
  1563. if assigned(labelnode) and
  1564. (exceptionblock<>labelnode.exceptionblock) then
  1565. CGMessage(cg_e_goto_inout_of_exception_block);
  1566. end;
  1567. function tgotonode.dogetcopy : tnode;
  1568. var
  1569. p : tgotonode;
  1570. begin
  1571. p:=tgotonode(inherited dogetcopy);
  1572. p.exceptionblock:=exceptionblock;
  1573. { generate labelnode if not done yet }
  1574. if not(assigned(labelnode)) then
  1575. begin
  1576. if assigned(labelsym) and assigned(labelsym.code) then
  1577. labelnode:=tlabelnode(labelsym.code)
  1578. end;
  1579. p.labelsym:=labelsym;
  1580. if assigned(labelnode) then
  1581. p.labelnode:=tlabelnode(labelnode.dogetcopy)
  1582. else
  1583. begin
  1584. { don't trigger IE when there was already an error, i.e. the
  1585. label is not defined. See tw11763 (PFV) }
  1586. if (errorcount=0) and
  1587. { don't trigger IE if it's a global goto }
  1588. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1589. not(assigned(labelsym.owner))) then
  1590. internalerror(200610291);
  1591. end;
  1592. result:=p;
  1593. end;
  1594. function tgotonode.docompare(p: tnode): boolean;
  1595. begin
  1596. docompare := false;
  1597. end;
  1598. {*****************************************************************************
  1599. TLABELNODE
  1600. *****************************************************************************}
  1601. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  1602. begin
  1603. inherited create(labeln,l);
  1604. exceptionblock:=current_exceptblock;
  1605. labsym:=alabsym;
  1606. { Register labelnode in labelsym }
  1607. labsym.code:=self;
  1608. end;
  1609. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1610. begin
  1611. inherited ppuload(t,ppufile);
  1612. exceptionblock:=ppufile.getbyte;
  1613. end;
  1614. destructor tlabelnode.destroy;
  1615. begin
  1616. { Remove reference in labelsym, this is to prevent
  1617. goto's to this label }
  1618. if assigned(labsym) and (labsym.code=pointer(self)) then
  1619. labsym.code:=nil;
  1620. inherited destroy;
  1621. end;
  1622. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  1623. begin
  1624. inherited ppuwrite(ppufile);
  1625. ppufile.putbyte(exceptionblock);
  1626. end;
  1627. procedure tlabelnode.buildderefimpl;
  1628. begin
  1629. inherited buildderefimpl;
  1630. end;
  1631. procedure tlabelnode.derefimpl;
  1632. begin
  1633. inherited derefimpl;
  1634. end;
  1635. function tlabelnode.pass_typecheck:tnode;
  1636. begin
  1637. result:=nil;
  1638. { left could still be unassigned }
  1639. if assigned(left) then
  1640. typecheckpass(left);
  1641. resultdef:=voidtype;
  1642. end;
  1643. function tlabelnode.pass_1 : tnode;
  1644. begin
  1645. result:=nil;
  1646. expectloc:=LOC_VOID;
  1647. include(current_procinfo.flags,pi_has_label);
  1648. if assigned(labsym) and labsym.nonlocal then
  1649. begin
  1650. include(current_procinfo.flags,pi_has_interproclabel);
  1651. exclude(current_procinfo.procdef.procoptions,po_inline);
  1652. end;
  1653. if assigned(left) then
  1654. firstpass(left);
  1655. if (m_non_local_goto in current_settings.modeswitches) and
  1656. { the owner can be Nil for internal labels }
  1657. assigned(labsym.owner) and
  1658. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  1659. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  1660. end;
  1661. function tlabelnode.dogetcopy : tnode;
  1662. begin
  1663. if not(assigned(copiedto)) then
  1664. copiedto:=tlabelnode(inherited dogetcopy);
  1665. copiedto.exceptionblock:=exceptionblock;
  1666. result:=copiedto;
  1667. end;
  1668. function tlabelnode.docompare(p: tnode): boolean;
  1669. begin
  1670. docompare := false;
  1671. end;
  1672. {*****************************************************************************
  1673. TRAISENODE
  1674. *****************************************************************************}
  1675. constructor traisenode.create(l,taddr,tframe:tnode);
  1676. begin
  1677. inherited create(raisen,l,taddr,tframe);
  1678. end;
  1679. function traisenode.pass_typecheck:tnode;
  1680. begin
  1681. result:=nil;
  1682. resultdef:=voidtype;
  1683. if assigned(left) then
  1684. begin
  1685. { first para must be a _class_ }
  1686. typecheckpass(left);
  1687. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1688. if codegenerror then
  1689. exit;
  1690. if not is_class(left.resultdef) and
  1691. not is_javaclass(left.resultdef) then
  1692. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  1693. { insert needed typeconvs for addr,frame }
  1694. if assigned(right) then
  1695. begin
  1696. { addr }
  1697. typecheckpass(right);
  1698. inserttypeconv(right,voidcodepointertype);
  1699. { frame }
  1700. if assigned(third) then
  1701. begin
  1702. typecheckpass(third);
  1703. inserttypeconv(third,voidpointertype);
  1704. end;
  1705. end;
  1706. end;
  1707. end;
  1708. function traisenode.pass_1 : tnode;
  1709. var
  1710. statements : tstatementnode;
  1711. current_addr : tlabelnode;
  1712. raisenode : tcallnode;
  1713. begin
  1714. result:=internalstatements(statements);
  1715. if assigned(left) then
  1716. begin
  1717. { first para must be a class }
  1718. firstpass(left);
  1719. { insert needed typeconvs for addr,frame }
  1720. if assigned(right) then
  1721. begin
  1722. { addr }
  1723. firstpass(right);
  1724. { frame }
  1725. if assigned(third) then
  1726. firstpass(third)
  1727. else
  1728. third:=cpointerconstnode.Create(0,voidpointertype);
  1729. end
  1730. else
  1731. begin
  1732. third:=cinlinenode.create(in_get_frame,false,nil);
  1733. current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  1734. addstatement(statements,current_addr);
  1735. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  1736. end;
  1737. raisenode:=ccallnode.createintern('fpc_raiseexception',
  1738. ccallparanode.create(third,
  1739. ccallparanode.create(right,
  1740. ccallparanode.create(left,nil)))
  1741. );
  1742. include(raisenode.callnodeflags,cnf_call_never_returns);
  1743. addstatement(statements,raisenode);
  1744. end
  1745. else
  1746. begin
  1747. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  1748. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  1749. include(raisenode.callnodeflags,cnf_call_never_returns);
  1750. addstatement(statements,raisenode);
  1751. end;
  1752. left:=nil;
  1753. right:=nil;
  1754. third:=nil;
  1755. end;
  1756. {*****************************************************************************
  1757. TTRYEXCEPTNODE
  1758. *****************************************************************************}
  1759. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1760. begin
  1761. inherited create(tryexceptn,l,r,_t1,nil);
  1762. end;
  1763. function ttryexceptnode.pass_typecheck:tnode;
  1764. begin
  1765. result:=nil;
  1766. typecheckpass(left);
  1767. { on statements }
  1768. if assigned(right) then
  1769. typecheckpass(right);
  1770. { else block }
  1771. if assigned(t1) then
  1772. typecheckpass(t1);
  1773. resultdef:=voidtype;
  1774. end;
  1775. function ttryexceptnode.pass_1 : tnode;
  1776. begin
  1777. result:=nil;
  1778. expectloc:=LOC_VOID;
  1779. firstpass(left);
  1780. { on statements }
  1781. if assigned(right) then
  1782. firstpass(right);
  1783. { else block }
  1784. if assigned(t1) then
  1785. firstpass(t1);
  1786. include(current_procinfo.flags,pi_do_call);
  1787. include(current_procinfo.flags,pi_uses_exceptions);
  1788. inc(current_procinfo.estimatedtempsize,get_jumpbuf_size*2);
  1789. end;
  1790. {*****************************************************************************
  1791. TTRYFINALLYNODE
  1792. *****************************************************************************}
  1793. constructor ttryfinallynode.create(l,r:tnode);
  1794. begin
  1795. inherited create(tryfinallyn,l,r,nil,nil);
  1796. implicitframe:=false;
  1797. end;
  1798. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1799. begin
  1800. inherited create(tryfinallyn,l,r,_t1,nil);
  1801. implicitframe:=true;
  1802. end;
  1803. function ttryfinallynode.pass_typecheck:tnode;
  1804. begin
  1805. result:=nil;
  1806. resultdef:=voidtype;
  1807. typecheckpass(left);
  1808. // "try block" is "used"? (JM)
  1809. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  1810. typecheckpass(right);
  1811. // "except block" is "used"? (JM)
  1812. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  1813. { special finally block only executed when there was an exception }
  1814. if assigned(t1) then
  1815. begin
  1816. typecheckpass(t1);
  1817. // "finally block" is "used"? (JM)
  1818. set_varstate(t1,vs_readwritten,[vsf_must_be_valid]);
  1819. end;
  1820. end;
  1821. function ttryfinallynode.pass_1 : tnode;
  1822. begin
  1823. result:=nil;
  1824. expectloc:=LOC_VOID;
  1825. firstpass(left);
  1826. firstpass(right);
  1827. if assigned(t1) then
  1828. firstpass(t1);
  1829. include(current_procinfo.flags,pi_do_call);
  1830. { pi_uses_exceptions is an information for the optimizer and it
  1831. is only interested in exceptions if they appear inside the body,
  1832. so ignore implicit frames when setting the flag }
  1833. if not(implicitframe) then
  1834. include(current_procinfo.flags,pi_uses_exceptions);
  1835. inc(current_procinfo.estimatedtempsize,get_jumpbuf_size);
  1836. end;
  1837. function ttryfinallynode.simplify(forinline : boolean): tnode;
  1838. begin
  1839. result:=nil;
  1840. { if the try contains no code, we can kill
  1841. the try and except and return only the
  1842. finally part }
  1843. if has_no_code(left) then
  1844. begin
  1845. result:=right;
  1846. right:=nil;
  1847. end;
  1848. end;
  1849. var
  1850. seq: longint=0;
  1851. function ttryfinallynode.create_finalizer_procdef: tprocdef;
  1852. var
  1853. st:TSymTable;
  1854. checkstack: psymtablestackitem;
  1855. oldsymtablestack: tsymtablestack;
  1856. sym:tprocsym;
  1857. begin
  1858. { get actual procedure symtable (skip withsymtables, etc.) }
  1859. st:=nil;
  1860. checkstack:=symtablestack.stack;
  1861. while assigned(checkstack) do
  1862. begin
  1863. st:=checkstack^.symtable;
  1864. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  1865. break;
  1866. checkstack:=checkstack^.next;
  1867. end;
  1868. { Create a nested procedure, even from main_program_level.
  1869. Furthermore, force procdef and procsym into the same symtable
  1870. (by default, defs are registered with symtablestack.top which may be
  1871. something temporary like exceptsymtable - in that case, procdef can be
  1872. destroyed before procsym, leaving invalid pointers). }
  1873. oldsymtablestack:=symtablestack;
  1874. symtablestack:=nil;
  1875. result:=cprocdef.create(max(normal_function_level,st.symtablelevel)+1);
  1876. symtablestack:=oldsymtablestack;
  1877. st.insertdef(result);
  1878. result.struct:=current_procinfo.procdef.struct;
  1879. { tabstractprocdef constructor sets po_delphi_nested_cc whenever
  1880. nested procvars modeswitch is active. We must be independent of this switch. }
  1881. exclude(result.procoptions,po_delphi_nested_cc);
  1882. result.proctypeoption:=potype_exceptfilter;
  1883. handle_calling_convention(result);
  1884. sym:=cprocsym.create('$fin$'+tostr(seq));
  1885. st.insert(sym);
  1886. inc(seq);
  1887. result.procsym:=sym;
  1888. proc_add_definition(result);
  1889. result.forwarddef:=false;
  1890. result.aliasnames.insert(result.mangledname);
  1891. end;
  1892. {*****************************************************************************
  1893. TONNODE
  1894. *****************************************************************************}
  1895. constructor tonnode.create(l,r:tnode);
  1896. begin
  1897. inherited create(onn,l,r);
  1898. excepTSymtable:=nil;
  1899. excepttype:=nil;
  1900. end;
  1901. destructor tonnode.destroy;
  1902. begin
  1903. { copied nodes don't need to release the symtable }
  1904. if assigned(excepTSymtable) then
  1905. excepTSymtable.free;
  1906. inherited destroy;
  1907. end;
  1908. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1909. begin
  1910. inherited ppuload(t,ppufile);
  1911. excepTSymtable:=nil;
  1912. excepttype:=nil;
  1913. end;
  1914. function tonnode.dogetcopy : tnode;
  1915. var
  1916. n : tonnode;
  1917. begin
  1918. n:=tonnode(inherited dogetcopy);
  1919. if assigned(exceptsymtable) then
  1920. n.exceptsymtable:=exceptsymtable.getcopy
  1921. else
  1922. n.exceptsymtable:=nil;
  1923. n.excepttype:=excepttype;
  1924. result:=n;
  1925. end;
  1926. function tonnode.pass_typecheck:tnode;
  1927. begin
  1928. result:=nil;
  1929. resultdef:=voidtype;
  1930. if not is_class(excepttype) and
  1931. not is_javaclass(excepttype) then
  1932. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1933. if assigned(left) then
  1934. typecheckpass(left);
  1935. if assigned(right) then
  1936. typecheckpass(right);
  1937. end;
  1938. function tonnode.pass_1 : tnode;
  1939. begin
  1940. result:=nil;
  1941. include(current_procinfo.flags,pi_do_call);
  1942. { Loads exception class VMT, therefore may need GOT
  1943. (generic code only; descendants may need to avoid this check) }
  1944. if (cs_create_pic in current_settings.moduleswitches) and
  1945. (tf_pic_uses_got in target_info.flags) then
  1946. include(current_procinfo.flags,pi_needs_got);
  1947. expectloc:=LOC_VOID;
  1948. if assigned(left) then
  1949. firstpass(left);
  1950. if assigned(right) then
  1951. firstpass(right);
  1952. end;
  1953. function tonnode.docompare(p: tnode): boolean;
  1954. begin
  1955. docompare := false;
  1956. end;
  1957. end.