nflw.pas 79 KB

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