nflw.pas 80 KB

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