nflw.pas 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852
  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. symconst,symtype,symbase,symdef,symsym,
  25. optloop;
  26. type
  27. { flags used by loop nodes }
  28. tloopflag = (
  29. { set if it is a for ... downto ... do loop }
  30. lnf_backward,
  31. { Do we need to parse childs to set var state? }
  32. lnf_varstate,
  33. { Do a test at the begin of the loop?}
  34. lnf_testatbegin,
  35. { Negate the loop test? }
  36. lnf_checknegate,
  37. { Should the value of the loop variable on exit be correct. }
  38. lnf_dont_mind_loopvar_on_exit,
  39. { Loop simplify flag }
  40. lnf_simplify_processing,
  41. { set if in a for loop the counter is not used, so an easier exit check
  42. can be carried out }
  43. lnf_counter_not_used);
  44. tloopflags = set of tloopflag;
  45. const
  46. { loop flags which must match to consider loop nodes equal regarding the flags }
  47. loopflagsequal = [lnf_backward];
  48. type
  49. tlabelnode = class;
  50. tloopnode = class(tbinarynode)
  51. t1,t2 : tnode;
  52. loopflags : tloopflags;
  53. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  54. destructor destroy;override;
  55. function dogetcopy : tnode;override;
  56. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  57. procedure ppuwrite(ppufile:tcompilerppufile);override;
  58. procedure buildderefimpl;override;
  59. procedure derefimpl;override;
  60. procedure insertintolist(l : tnodelist);override;
  61. procedure printnodetree(var t:text);override;
  62. {$ifdef DEBUG_NODE_XML}
  63. procedure XMLPrintNodeInfo(var T: Text); override;
  64. procedure XMLPrintNodeTree(var T: Text); override;
  65. {$endif DEBUG_NODE_XML}
  66. function docompare(p: tnode): boolean; override;
  67. end;
  68. twhilerepeatnode = class(tloopnode)
  69. { l: condition; r: body; tab: test at begin; cn: negate condition
  70. x,y,true,false: while loop
  71. x,y,false,true: repeat until loop }
  72. constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
  73. function pass_typecheck:tnode;override;
  74. function pass_1 : tnode;override;
  75. {$ifdef state_tracking}
  76. function track_state_pass(exec_known:boolean):boolean;override;
  77. {$endif}
  78. end;
  79. twhilerepeatnodeclass = class of twhilerepeatnode;
  80. tifnode = class(tloopnode)
  81. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  82. constructor create_internal(l,r,_t1 : tnode);virtual;reintroduce;
  83. function pass_typecheck:tnode;override;
  84. function pass_1 : tnode;override;
  85. function simplify(forinline : boolean) : tnode;override;
  86. private
  87. function internalsimplify(warn: boolean) : tnode;
  88. end;
  89. tifnodeclass = class of tifnode;
  90. tfornode = class(tloopnode)
  91. { if count isn divisable by unrolls then
  92. the for loop must jump to this label to get the correct
  93. number of executions }
  94. entrylabel,
  95. { this is a dummy node used by the dfa to store life information for the loop iteration }
  96. loopiteration : tnode;
  97. loopvar_notid:cardinal;
  98. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;reintroduce;
  99. destructor destroy;override;
  100. function pass_typecheck:tnode;override;
  101. function pass_1 : tnode;override;
  102. function makewhileloop : tnode;
  103. function simplify(forinline : boolean) : tnode;override;
  104. end;
  105. tfornodeclass = class of tfornode;
  106. texitnode = class(tunarynode)
  107. constructor create(l:tnode);virtual;
  108. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  109. procedure ppuwrite(ppufile:tcompilerppufile);override;
  110. function pass_typecheck:tnode;override;
  111. function pass_1 : tnode;override;
  112. property resultexpr : tnode read left write left;
  113. end;
  114. texitnodeclass = class of texitnode;
  115. tbreaknode = class(tnode)
  116. constructor create;virtual;
  117. function pass_typecheck:tnode;override;
  118. function pass_1 : tnode;override;
  119. end;
  120. tbreaknodeclass = class of tbreaknode;
  121. tcontinuenode = class(tnode)
  122. constructor create;virtual;
  123. function pass_typecheck:tnode;override;
  124. function pass_1 : tnode;override;
  125. end;
  126. tcontinuenodeclass = class of tcontinuenode;
  127. tgotonode = class(tnode)
  128. private
  129. labelnodeidx : longint;
  130. public
  131. { * Set when creating the gotonode (since that's all we know at that
  132. point).
  133. * Used in pass_typecheck to find the corresponding labelnode (when a
  134. labelnode is created for a tlabelsym, the label assigns itself to
  135. the "code" field of the labelsym), which is then assigned to the
  136. labelnode field
  137. * After this, the labelsym is (and must) never be used anymore, and
  138. instead the labelnode must always be used. The reason is that the
  139. labelsym may not be owned by anything, and will be freed by the
  140. label node when it gets freed
  141. * The above is the reason why the labelsym field does not get copied
  142. by tgotonode.dogetcopy, but instead the copy of the labelnode gets
  143. tracked (both the labelnode and its goto nodes must always all be
  144. copied).
  145. The labelnode itself will not copy the labelsym either in dogetcopy.
  146. Instead, since the link between the gotos and the labels gets
  147. tracked via node tree references, the label node will generate a new
  148. asmlabel on the fly and the goto node will get it from there (if the
  149. goto node gets processed before the label node has been processed,
  150. it will ask the label node to generate the asmsymbol at that point).
  151. The original tlabelsym will get emitted only for the original
  152. label node. It is only actually used if there is a reference to it
  153. from
  154. * an inline assembly block. Since inline assembly blocks cannot be
  155. inlined at this point, it doesn't matter that this would break
  156. in case the node gets copied
  157. * global goto/label. Inlining is not supported for these, so no
  158. problem here either for now.
  159. * a load node (its symtableentry field). Since the symtableentry
  160. of loadnodes is always expected to be valid, we cannot do like
  161. with the goto nodes. Instead, we will create a new labelsym
  162. when performing a dogetcopy of such a load node and assign this
  163. labelsym to the copied labelnode (and vice versa)
  164. }
  165. labelsym : tlabelsym;
  166. labelnode : tlabelnode;
  167. exceptionblock : integer;
  168. constructor create(p : tlabelsym);virtual;
  169. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  170. procedure ppuwrite(ppufile:tcompilerppufile);override;
  171. procedure buildderefimpl;override;
  172. procedure derefimpl;override;
  173. procedure resolveppuidx;override;
  174. function dogetcopy : tnode;override;
  175. function pass_typecheck:tnode;override;
  176. function pass_1 : tnode;override;
  177. function docompare(p: tnode): boolean; override;
  178. end;
  179. tgotonodeclass = class of tgotonode;
  180. tlabelnode = class(tunarynode)
  181. exceptionblock : integer;
  182. { when copying trees, this points to the newly created copy of a label }
  183. copiedto : tlabelnode;
  184. labsym : tlabelsym;
  185. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  186. destructor destroy;override;
  187. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  188. procedure ppuwrite(ppufile:tcompilerppufile);override;
  189. procedure buildderefimpl;override;
  190. procedure derefimpl;override;
  191. function dogetcopy : tnode;override;
  192. function pass_typecheck:tnode;override;
  193. function pass_1 : tnode;override;
  194. function docompare(p: tnode): boolean; override;
  195. end;
  196. tlabelnodeclass = class of tlabelnode;
  197. traisenode = class(ttertiarynode)
  198. constructor create(l,taddr,tframe:tnode);virtual;
  199. function pass_typecheck:tnode;override;
  200. function pass_1 : tnode;override;
  201. end;
  202. traisenodeclass = class of traisenode;
  203. ttryexceptnode = class(tloopnode)
  204. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  205. function pass_typecheck:tnode;override;
  206. function pass_1 : tnode;override;
  207. function simplify(forinline: boolean): tnode; override;
  208. protected
  209. procedure adjust_estimated_stack_size; virtual;
  210. end;
  211. ttryexceptnodeclass = class of ttryexceptnode;
  212. { the third node is to store a copy of the finally code for llvm:
  213. it needs one copy to execute in case an exception occurs, and
  214. one in case no exception occurs }
  215. ttryfinallynode = class(ttertiarynode)
  216. implicitframe : boolean;
  217. constructor create(l,r:tnode);virtual;reintroduce;
  218. constructor create_implicit(l,r:tnode);virtual;
  219. function pass_typecheck:tnode;override;
  220. function pass_1 : tnode;override;
  221. function simplify(forinline:boolean): tnode;override;
  222. protected
  223. procedure adjust_estimated_stack_size; virtual;
  224. public
  225. function dogetcopy: tnode;override;
  226. end;
  227. ttryfinallynodeclass = class of ttryfinallynode;
  228. tonnode = class(tbinarynode)
  229. excepTSymtable : TSymtable;
  230. excepttype : tobjectdef;
  231. constructor create(l,r:tnode);virtual;
  232. destructor destroy;override;
  233. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  234. function pass_typecheck:tnode;override;
  235. function pass_1 : tnode;override;
  236. function dogetcopy : tnode;override;
  237. function docompare(p: tnode): boolean; override;
  238. end;
  239. tonnodeclass = class of tonnode;
  240. var
  241. cwhilerepeatnode : twhilerepeatnodeclass=twhilerepeatnode;
  242. cifnode : tifnodeclass = tifnode;
  243. cfornode : tfornodeclass = tfornode;
  244. cexitnode : texitnodeclass = texitnode;
  245. cgotonode : tgotonodeclass = tgotonode;
  246. clabelnode : tlabelnodeclass = tlabelnode;
  247. craisenode : traisenodeclass = traisenode;
  248. ctryexceptnode : ttryexceptnodeclass = ttryexceptnode;
  249. ctryfinallynode : ttryfinallynodeclass = ttryfinallynode;
  250. connode : tonnodeclass = tonnode;
  251. cbreaknode : tbreaknodeclass = tbreaknode;
  252. ccontinuenode : tcontinuenodeclass = tcontinuenode;
  253. // for-in loop helpers
  254. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  255. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  256. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  257. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  258. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  259. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  260. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  261. { converts all for nodes in the tree into while nodes,
  262. returns true if something was converted }
  263. function ConvertForLoops(var n : tnode) : Boolean;
  264. implementation
  265. uses
  266. globtype,systems,constexp,compinnr,
  267. cutils,verbose,globals,ppu,
  268. symtable,paramgr,defcmp,defutil,htypechk,pass_1,
  269. ncal,nadd,ncon,nmem,nld,ncnv,nbas,nutils,ninl,nset,ngenutil,
  270. {$ifdef state_tracking}
  271. nstate,
  272. {$endif}
  273. {$ifdef i8086}
  274. cpuinfo,
  275. {$endif i8086}
  276. {$if defined(xtensa) or defined(i386)}
  277. cpuinfo,
  278. {$endif defined(xtensa) or defined(i386)}
  279. cgbase,procinfo
  280. ;
  281. // for-in loop helpers
  282. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  283. begin
  284. result:=cfornode.create(hloopvar,
  285. cinlinenode.create(in_low_x,false,expr.getcopy),
  286. cinlinenode.create(in_high_x,false,expr.getcopy),
  287. hloopbody,
  288. false);
  289. end;
  290. function create_objc_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  291. var
  292. mainstatement, outerloopbodystatement, innerloopbodystatement, tempstatement: tstatementnode;
  293. state, mutationcheck, currentamount, innerloopcounter, items, expressiontemp: ttempcreatenode;
  294. outerloop, innerloop, hp: tnode;
  295. itemsarraydef: tarraydef;
  296. sym: tsym;
  297. begin
  298. { Objective-C enumerators require Objective-C 2.0 }
  299. if not(m_objectivec2 in current_settings.modeswitches) then
  300. begin
  301. result:=cerrornode.create;
  302. MessagePos(expr.fileinfo,parser_e_objc_enumerator_2_0);
  303. exit;
  304. end;
  305. { Requires the NSFastEnumeration protocol and NSFastEnumerationState
  306. record }
  307. maybeloadcocoatypes;
  308. if not assigned(objc_fastenumeration) or
  309. not assigned(objc_fastenumerationstate) then
  310. begin
  311. result:=cerrornode.create;
  312. MessagePos(expr.fileinfo,parser_e_objc_missing_enumeration_defs);
  313. exit;
  314. end;
  315. (* Original code:
  316. for hloopvar in expression do
  317. <hloopbody>
  318. Pascal code equivalent into which it has to be transformed
  319. (sure would be nice if the compiler had some kind of templates ;) :
  320. var
  321. state: NSFastEnumerationState;
  322. expressiontemp: NSFastEnumerationProtocol;
  323. mutationcheck,
  324. currentamount,
  325. innerloopcounter: culong;
  326. { size can be increased/decreased if desired }
  327. items: array[1..16] of id;
  328. begin
  329. fillchar(state,sizeof(state),0);
  330. expressiontemp:=expression;
  331. repeat
  332. currentamount:=expressiontemp.countByEnumeratingWithState_objects_count(@state,@items,length(items));
  333. if currentamount=0 then
  334. begin
  335. { "The iterating variable is set to nil when the loop ends by
  336. exhausting the source pool of objects" }
  337. hloopvar:=nil;
  338. break;
  339. end;
  340. mutationcheck:=state.mutationsptr^;
  341. innerloopcounter:=culong(-1);
  342. repeat
  343. { at the start so that "continue" in <loopbody> works correctly }
  344. { don't use for-loop, because then the value of the iteration
  345. counter is undefined on exit and we have to check it in the
  346. outer repeat/until condition }
  347. {$push}
  348. {$r-,q-}
  349. inc(innerloopcounter);
  350. {$pop}
  351. if innerloopcounter=currentamount then
  352. break;
  353. if mutationcheck<>state.mutationsptr^ then
  354. { raises Objective-C exception... }
  355. objc_enumerationMutation(expressiontemp);
  356. hloopvar:=state.itemsPtr[innerloopcounter];
  357. { if continue in loopbody -> jumps to start, increases count and checks }
  358. { if break in loopbody: goes to outer repeat/until and innerloopcount
  359. will be < currentamount -> stops }
  360. <hloopbody>
  361. until false;
  362. { if the inner loop terminated early, "break" was used and we have
  363. to stop }
  364. { "If the loop is terminated early, the iterating variable is left
  365. pointing to the last iteration item." }
  366. until innerloopcounter<currentamount;
  367. end;
  368. *)
  369. result:=internalstatements(mainstatement);
  370. { the fast enumeration state }
  371. state:=ctempcreatenode.create(objc_fastenumerationstate,objc_fastenumerationstate.size,tt_persistent,false);
  372. typecheckpass(tnode(state));
  373. addstatement(mainstatement,state);
  374. { the temporary items array }
  375. itemsarraydef:=carraydef.create(1,16,u32inttype);
  376. itemsarraydef.elementdef:=objc_idtype;
  377. items:=ctempcreatenode.create(itemsarraydef,itemsarraydef.size,tt_persistent,false);
  378. addstatement(mainstatement,items);
  379. typecheckpass(tnode(items));
  380. { temp for the expression/collection through which we iterate }
  381. expressiontemp:=ctempcreatenode.create(objc_fastenumeration,objc_fastenumeration.size,tt_persistent,true);
  382. addstatement(mainstatement,expressiontemp);
  383. { currentamount temp (not really clean: we use ptruint instead of
  384. culong) }
  385. currentamount:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  386. typecheckpass(tnode(currentamount));
  387. addstatement(mainstatement,currentamount);
  388. { mutationcheck temp (idem) }
  389. mutationcheck:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  390. typecheckpass(tnode(mutationcheck));
  391. addstatement(mainstatement,mutationcheck);
  392. { innerloopcounter temp (idem) }
  393. innerloopcounter:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  394. typecheckpass(tnode(innerloopcounter));
  395. addstatement(mainstatement,innerloopcounter);
  396. { initialise the state with 0 }
  397. addstatement(mainstatement,ccallnode.createinternfromunit('SYSTEM','FILLCHAR',
  398. ccallparanode.create(genintconstnode(0),
  399. ccallparanode.create(genintconstnode(objc_fastenumerationstate.size),
  400. ccallparanode.create(ctemprefnode.create(state),nil)
  401. )
  402. )
  403. ));
  404. { this will also check whether the expression (potentially) conforms
  405. to the NSFastEnumeration protocol (use expr.getcopy, because the
  406. caller will free expr) }
  407. addstatement(mainstatement,cassignmentnode.create(ctemprefnode.create(expressiontemp),expr.getcopy));
  408. { we add the "repeat..until" afterwards, now just create the body }
  409. outerloop:=internalstatements(outerloopbodystatement);
  410. { the countByEnumeratingWithState_objects_count call }
  411. hp:=ccallparanode.create(cinlinenode.create(in_length_x,false,ctypenode.create(itemsarraydef)),
  412. ccallparanode.create(caddrnode.create(ctemprefnode.create(items)),
  413. ccallparanode.create(caddrnode.create(ctemprefnode.create(state)),nil)
  414. )
  415. );
  416. sym:=search_struct_member(objc_fastenumeration,'COUNTBYENUMERATINGWITHSTATE_OBJECTS_COUNT');
  417. if not assigned(sym) or
  418. (sym.typ<>procsym) then
  419. internalerror(2010061901);
  420. hp:=ccallnode.create(hp,tprocsym(sym),sym.owner,ctemprefnode.create(expressiontemp),[],nil);
  421. addstatement(outerloopbodystatement,cassignmentnode.create(
  422. ctemprefnode.create(currentamount),hp));
  423. { if currentamount = 0, bail out (use copy of hloopvar, because we
  424. have to use it again below) }
  425. hp:=internalstatements(tempstatement);
  426. addstatement(tempstatement,cassignmentnode.create(
  427. hloopvar.getcopy,cnilnode.create));
  428. addstatement(tempstatement,cbreaknode.create);
  429. addstatement(outerloopbodystatement,cifnode.create(
  430. caddnode.create(equaln,ctemprefnode.create(currentamount),genintconstnode(0)),
  431. hp,nil));
  432. { initial value of mutationcheck }
  433. hp:=ctemprefnode.create(state);
  434. typecheckpass(hp);
  435. hp:=cderefnode.create(genloadfield(hp,'MUTATIONSPTR'));
  436. addstatement(outerloopbodystatement,cassignmentnode.create(
  437. ctemprefnode.create(mutationcheck),hp));
  438. { initialise innerloopcounter }
  439. addstatement(outerloopbodystatement,cassignmentnode.create(
  440. ctemprefnode.create(innerloopcounter),cordconstnode.create(-1,ptruinttype,false)));
  441. { and now the inner loop, again adding the repeat/until afterwards }
  442. innerloop:=internalstatements(innerloopbodystatement);
  443. { inc(innerloopcounter) without range/overflowchecking (because
  444. we go from culong(-1) to 0 during the first iteration }
  445. hp:=cinlinenode.create(
  446. in_inc_x,false,ccallparanode.create(
  447. ctemprefnode.create(innerloopcounter),nil));
  448. hp.localswitches:=hp.localswitches-[cs_check_range,cs_check_overflow];
  449. addstatement(innerloopbodystatement,hp);
  450. { if innerloopcounter=currentamount then break to the outer loop }
  451. addstatement(innerloopbodystatement,cifnode.create(
  452. caddnode.create(equaln,
  453. ctemprefnode.create(innerloopcounter),
  454. ctemprefnode.create(currentamount)),
  455. cbreaknode.create,
  456. nil));
  457. { verify that the collection didn't change in the mean time }
  458. hp:=ctemprefnode.create(state);
  459. typecheckpass(hp);
  460. addstatement(innerloopbodystatement,cifnode.create(
  461. caddnode.create(unequaln,
  462. ctemprefnode.create(mutationcheck),
  463. cderefnode.create(genloadfield(hp,'MUTATIONSPTR'))
  464. ),
  465. ccallnode.createinternfromunit('OBJC','OBJC_ENUMERATIONMUTATION',
  466. ccallparanode.create(ctemprefnode.create(expressiontemp),nil)),
  467. nil));
  468. { finally: actually get the next element }
  469. hp:=ctemprefnode.create(state);
  470. typecheckpass(hp);
  471. hp:=genloadfield(hp,'ITEMSPTR');
  472. typecheckpass(hp);
  473. { don't simply use a vecn, because indexing a pointer won't work in
  474. non-FPC modes }
  475. if hp.resultdef.typ<>pointerdef then
  476. internalerror(2010061904);
  477. inserttypeconv(hp,
  478. carraydef.create_from_pointer(tpointerdef(hp.resultdef)));
  479. hp:=cvecnode.create(hp,ctemprefnode.create(innerloopcounter));
  480. addstatement(innerloopbodystatement,
  481. cassignmentnode.create(hloopvar,hp));
  482. { the actual loop body! }
  483. addstatement(innerloopbodystatement,hloopbody);
  484. { create the inner repeat/until and add it to the body of the outer
  485. one }
  486. hp:=cwhilerepeatnode.create(
  487. { repeat .. until false }
  488. cordconstnode.create(0,pasbool1type,false),innerloop,false,true);
  489. addstatement(outerloopbodystatement,hp);
  490. { create the outer repeat/until and add it to the the main body }
  491. hp:=cwhilerepeatnode.create(
  492. { repeat .. until innerloopcounter<currentamount }
  493. caddnode.create(ltn,
  494. ctemprefnode.create(innerloopcounter),
  495. ctemprefnode.create(currentamount)),
  496. outerloop,false,true);
  497. addstatement(mainstatement,hp);
  498. { release the temps }
  499. addstatement(mainstatement,ctempdeletenode.create(state));
  500. addstatement(mainstatement,ctempdeletenode.create(mutationcheck));
  501. addstatement(mainstatement,ctempdeletenode.create(currentamount));
  502. addstatement(mainstatement,ctempdeletenode.create(innerloopcounter));
  503. addstatement(mainstatement,ctempdeletenode.create(items));
  504. addstatement(mainstatement,ctempdeletenode.create(expressiontemp));
  505. end;
  506. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  507. var
  508. loopstatement, loopbodystatement: tstatementnode;
  509. loopvar, stringvar: ttempcreatenode;
  510. stringindex, loopbody, forloopnode: tnode;
  511. begin
  512. { result is a block of statements }
  513. result:=internalstatements(loopstatement);
  514. { create a temp variable for expression }
  515. stringvar := ctempcreatenode.create(
  516. expr.resultdef,
  517. expr.resultdef.size,
  518. tt_persistent,true);
  519. addstatement(loopstatement,stringvar);
  520. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(stringvar),expr.getcopy));
  521. { create a loop counter: signed integer with size of string length }
  522. loopvar := ctempcreatenode.create(
  523. sinttype,
  524. sinttype.size,
  525. tt_persistent,true);
  526. addstatement(loopstatement,loopvar);
  527. stringindex:=ctemprefnode.create(loopvar);
  528. loopbody:=internalstatements(loopbodystatement);
  529. // for-in loop variable := string_expression[index]
  530. addstatement(loopbodystatement,
  531. cassignmentnode.create(hloopvar, cvecnode.create(ctemprefnode.create(stringvar),stringindex)));
  532. { add the actual statement to the loop }
  533. addstatement(loopbodystatement,hloopbody);
  534. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  535. genintconstnode(1),
  536. cinlinenode.create(in_length_x,false,ctemprefnode.create(stringvar)),
  537. loopbody,
  538. false);
  539. addstatement(loopstatement,forloopnode);
  540. { free the loop counter }
  541. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  542. { free the temp variable for expression }
  543. addstatement(loopstatement,ctempdeletenode.create(stringvar));
  544. end;
  545. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  546. var
  547. loopstatement, loopbodystatement: tstatementnode;
  548. loopvar, arrayvar: ttempcreatenode;
  549. arrayindex, lowbound, highbound, loopbody, forloopnode, expression: tnode;
  550. is_string: boolean;
  551. tmpdef, convertdef: tdef;
  552. elementcount: aword;
  553. begin
  554. expression := expr;
  555. { result is a block of statements }
  556. result:=internalstatements(loopstatement);
  557. is_string:=ado_IsConstString in tarraydef(expr.resultdef).arrayoptions;
  558. // if array element type <> loovar type then create a conversion if possible
  559. if compare_defs(tarraydef(expression.resultdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible then
  560. begin
  561. tmpdef:=expression.resultdef;
  562. elementcount:=1;
  563. while assigned(tmpdef) and (tmpdef.typ=arraydef) and
  564. (tarraydef(tmpdef).arrayoptions = []) and
  565. (compare_defs(tarraydef(tmpdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible) do
  566. begin
  567. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  568. tmpdef:=tarraydef(tmpdef).elementdef;
  569. end;
  570. if assigned(tmpdef) and (tmpdef.typ=arraydef) and (tarraydef(tmpdef).arrayoptions = []) then
  571. begin
  572. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  573. convertdef:=carraydef.create(0,elementcount-1,s32inttype);
  574. tarraydef(convertdef).elementdef:=tarraydef(tmpdef).elementdef;
  575. expression:=expr.getcopy;
  576. expression:=ctypeconvnode.create_internal(expression,convertdef);
  577. typecheckpass(expression);
  578. addstatement(loopstatement,expression);
  579. end;
  580. end;
  581. if (node_complexity(expression) > 1) and
  582. not(is_open_array(expression.resultdef)) and not(is_array_of_const(expression.resultdef)) then
  583. begin
  584. { create a temp variable for expression }
  585. arrayvar := ctempcreatenode.create(
  586. expression.resultdef,
  587. expression.resultdef.size,
  588. tt_persistent,true);
  589. if is_string then
  590. begin
  591. lowbound:=genintconstnode(1);
  592. highbound:=cinlinenode.create(in_length_x,false,ctemprefnode.create(arrayvar))
  593. end
  594. else
  595. begin
  596. lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
  597. highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
  598. end;
  599. addstatement(loopstatement,arrayvar);
  600. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(arrayvar),expression.getcopy));
  601. end
  602. else
  603. begin
  604. arrayvar:=nil;
  605. if is_string then
  606. begin
  607. lowbound:=genintconstnode(1);
  608. highbound:=cinlinenode.create(in_length_x,false,expression.getcopy);
  609. end
  610. else
  611. begin
  612. lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
  613. highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
  614. end;
  615. end;
  616. { create a loop counter }
  617. loopvar := ctempcreatenode.create(
  618. tarraydef(expression.resultdef).rangedef,
  619. tarraydef(expression.resultdef).rangedef.size,
  620. tt_persistent,true);
  621. addstatement(loopstatement,loopvar);
  622. arrayindex:=ctemprefnode.create(loopvar);
  623. loopbody:=internalstatements(loopbodystatement);
  624. // for-in loop variable := array_expression[index]
  625. if assigned(arrayvar) then
  626. addstatement(loopbodystatement,
  627. cassignmentnode.create(hloopvar,cvecnode.create(ctemprefnode.create(arrayvar),arrayindex)))
  628. else
  629. addstatement(loopbodystatement,
  630. cassignmentnode.create(hloopvar,cvecnode.create(expression.getcopy,arrayindex)));
  631. { add the actual statement to the loop }
  632. addstatement(loopbodystatement,hloopbody);
  633. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  634. lowbound,
  635. highbound,
  636. loopbody,
  637. false);
  638. addstatement(loopstatement,forloopnode);
  639. { free the loop counter }
  640. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  641. { free the temp variable for expression if needed }
  642. if arrayvar<>nil then
  643. addstatement(loopstatement,ctempdeletenode.create(arrayvar));
  644. end;
  645. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  646. var
  647. loopstatement, loopbodystatement: tstatementnode;
  648. loopvar, setvar: ttempcreatenode;
  649. loopbody, forloopnode: tnode;
  650. begin
  651. // first check is set is empty and if it so then skip other processing
  652. if not Assigned(tsetdef(expr.resultdef).elementdef) then
  653. begin
  654. result:=cnothingnode.create;
  655. // free unused nodes
  656. hloopvar.free;
  657. hloopbody.free;
  658. exit;
  659. end;
  660. { result is a block of statements }
  661. result:=internalstatements(loopstatement);
  662. { create a temp variable for expression }
  663. setvar := ctempcreatenode.create(
  664. expr.resultdef,
  665. expr.resultdef.size,
  666. tt_persistent,true);
  667. addstatement(loopstatement,setvar);
  668. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(setvar),expr.getcopy));
  669. { create a loop counter }
  670. loopvar := ctempcreatenode.create(
  671. tsetdef(expr.resultdef).elementdef,
  672. tsetdef(expr.resultdef).elementdef.size,
  673. tt_persistent,true);
  674. addstatement(loopstatement,loopvar);
  675. // if loopvar in set then
  676. // begin
  677. // hloopvar := loopvar
  678. // for-in loop body
  679. // end
  680. loopbody:=cifnode.create(
  681. cinnode.create(ctemprefnode.create(loopvar),ctemprefnode.create(setvar)),
  682. internalstatements(loopbodystatement),
  683. nil);
  684. addstatement(loopbodystatement,cassignmentnode.create(hloopvar,ctemprefnode.create(loopvar)));
  685. { add the actual statement to the loop }
  686. addstatement(loopbodystatement,hloopbody);
  687. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  688. cinlinenode.create(in_low_x,false,ctemprefnode.create(setvar)),
  689. cinlinenode.create(in_high_x,false,ctemprefnode.create(setvar)),
  690. loopbody,
  691. false);
  692. addstatement(loopstatement,forloopnode);
  693. { free the loop counter }
  694. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  695. { free the temp variable for expression }
  696. addstatement(loopstatement,ctempdeletenode.create(setvar));
  697. end;
  698. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  699. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  700. var
  701. loopstatement, loopbodystatement: tstatementnode;
  702. enumvar: ttempcreatenode;
  703. loopbody, whileloopnode,
  704. enum_get, enum_move, enum_current, enum_get_params: tnode;
  705. propaccesslist: tpropaccesslist;
  706. enumerator_is_class: boolean;
  707. enumerator_destructor: tprocdef;
  708. begin
  709. { result is a block of statements }
  710. result:=internalstatements(loopstatement);
  711. enumerator_is_class := is_class(enumerator_get.returndef);
  712. { create a temp variable for enumerator }
  713. enumvar := ctempcreatenode.create(
  714. enumerator_get.returndef,
  715. enumerator_get.returndef.size,
  716. tt_persistent,true);
  717. addstatement(loopstatement,enumvar);
  718. if enumerator_get.proctypeoption=potype_operator then
  719. begin
  720. enum_get_params:=ccallparanode.create(expr.getcopy,nil);
  721. enum_get:=ccallnode.create(enum_get_params, tprocsym(enumerator_get.procsym), nil, nil, [],nil);
  722. tcallnode(enum_get).procdefinition:=enumerator_get;
  723. addsymref(enumerator_get.procsym,enumerator_get);
  724. end
  725. else
  726. enum_get:=ccallnode.create(nil, tprocsym(enumerator_get.procsym), enumerator_get.owner, expr.getcopy, [],nil);
  727. addstatement(loopstatement,
  728. cassignmentnode.create(
  729. ctemprefnode.create(enumvar),
  730. enum_get
  731. ));
  732. loopbody:=internalstatements(loopbodystatement);
  733. { for-in loop variable := enumerator.current }
  734. if enumerator_current.getpropaccesslist(palt_read,propaccesslist) then
  735. begin
  736. case propaccesslist.firstsym^.sym.typ of
  737. fieldvarsym :
  738. begin
  739. { generate access code }
  740. enum_current:=ctemprefnode.create(enumvar);
  741. propaccesslist_to_node(enum_current,enumerator_current.owner,propaccesslist);
  742. include(enum_current.flags,nf_isproperty);
  743. end;
  744. procsym :
  745. begin
  746. { generate the method call }
  747. enum_current:=ccallnode.create(nil,tprocsym(propaccesslist.firstsym^.sym),enumerator_current.owner,ctemprefnode.create(enumvar),[],nil);
  748. include(enum_current.flags,nf_isproperty);
  749. end
  750. else
  751. begin
  752. enum_current:=cerrornode.create;
  753. Message(type_e_mismatch);
  754. end;
  755. end;
  756. end
  757. else
  758. enum_current:=cerrornode.create;
  759. addstatement(loopbodystatement,
  760. cassignmentnode.create(hloopvar, enum_current));
  761. { add the actual statement to the loop }
  762. addstatement(loopbodystatement,hloopbody);
  763. enum_move:=ccallnode.create(nil, tprocsym(enumerator_move.procsym), enumerator_move.owner, ctemprefnode.create(enumvar), [],nil);
  764. whileloopnode:=cwhilerepeatnode.create(enum_move,loopbody,true,false);
  765. if enumerator_is_class then
  766. begin
  767. { insert a try-finally and call the destructor for the enumerator in the finally section }
  768. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  769. if assigned(enumerator_destructor) then
  770. begin
  771. whileloopnode:=ctryfinallynode.create(
  772. whileloopnode, // try node
  773. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym), // finally node
  774. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
  775. end;
  776. { if getenumerator <> nil then do the loop }
  777. whileloopnode:=cifnode.create(
  778. caddnode.create(unequaln, ctemprefnode.create(enumvar), cnilnode.create),
  779. whileloopnode,
  780. nil);
  781. end;
  782. addstatement(loopstatement, whileloopnode);
  783. if is_object(enumerator_get.returndef) then
  784. begin
  785. // call the object destructor too
  786. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  787. if assigned(enumerator_destructor) then
  788. begin
  789. addstatement(loopstatement,
  790. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym),
  791. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
  792. end;
  793. end;
  794. { free the temp variable for enumerator }
  795. addstatement(loopstatement,ctempdeletenode.create(enumvar));
  796. end;
  797. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  798. var
  799. pd, movenext: tprocdef;
  800. helperdef: tobjectdef;
  801. current: tpropertysym;
  802. storefilepos: tfileposinfo;
  803. begin
  804. storefilepos:=current_filepos;
  805. current_filepos:=hloopvar.fileinfo;
  806. if expr.nodetype=typen then
  807. begin
  808. if (expr.resultdef.typ=enumdef) and tenumdef(expr.resultdef).has_jumps then
  809. begin
  810. result:=cerrornode.create;
  811. hloopvar.free;
  812. hloopbody.free;
  813. MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
  814. end
  815. else
  816. result:=create_type_for_in_loop(hloopvar, hloopbody, expr);
  817. end
  818. else
  819. begin
  820. { loop is made for an expression }
  821. // Objective-C uses different conventions (and it's only supported for Objective-C 2.0)
  822. if is_objc_class_or_protocol(hloopvar.resultdef) or
  823. is_objc_class_or_protocol(expr.resultdef) then
  824. begin
  825. result:=create_objc_for_in_loop(hloopvar,hloopbody,expr);
  826. if result.nodetype=errorn then
  827. begin
  828. hloopvar.free;
  829. hloopbody.free;
  830. end;
  831. end
  832. { "for x in [] do ..." always results in a never executed loop body }
  833. else if (is_array_constructor(expr.resultdef) and
  834. (tarraydef(expr.resultdef).elementdef=voidtype)) then
  835. begin
  836. if assigned(hloopbody) then
  837. MessagePos(hloopbody.fileinfo,cg_w_unreachable_code);
  838. result:=cnothingnode.create;
  839. end
  840. else
  841. begin
  842. if is_array_constructor(expr.resultdef) then
  843. tarrayconstructornode(expr).force_type(hloopvar.resultdef);
  844. // search for operator first
  845. pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
  846. // if there is no operator then search for class/object enumerator method
  847. if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
  848. begin
  849. { first search using the helper hierarchy }
  850. if search_last_objectpascal_helper(tabstractrecorddef(expr.resultdef),nil,helperdef) then
  851. repeat
  852. pd:=helperdef.search_enumerator_get;
  853. helperdef:=helperdef.childof;
  854. until (pd<>nil) or (helperdef=nil);
  855. { we didn't find an enumerator in a helper, so search in the
  856. class/record/object itself }
  857. if pd=nil then
  858. pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
  859. end;
  860. if pd<>nil then
  861. begin
  862. // seach movenext and current symbols
  863. movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
  864. if movenext = nil then
  865. begin
  866. result:=cerrornode.create;
  867. hloopvar.free;
  868. hloopbody.free;
  869. MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
  870. end
  871. else
  872. begin
  873. current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  874. if current = nil then
  875. begin
  876. result:=cerrornode.create;
  877. hloopvar.free;
  878. hloopbody.free;
  879. MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
  880. end
  881. else
  882. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  883. end;
  884. end
  885. else
  886. begin
  887. { prefer set if loop var could be a set var and the loop
  888. expression can indeed be a set }
  889. if (expr.nodetype=arrayconstructorn) and
  890. (hloopvar.resultdef.typ in [enumdef,orddef]) and
  891. arrayconstructor_can_be_set(expr) then
  892. begin
  893. expr:=arrayconstructor_to_set(expr,false);
  894. typecheckpass(expr);
  895. end;
  896. case expr.resultdef.typ of
  897. stringdef:
  898. result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  899. arraydef:
  900. result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  901. setdef:
  902. result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  903. undefineddef:
  904. result:=cnothingnode.create;
  905. else
  906. begin
  907. result:=cerrornode.create;
  908. hloopvar.free;
  909. hloopbody.free;
  910. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  911. end;
  912. end;
  913. end;
  914. end;
  915. end;
  916. current_filepos:=storefilepos;
  917. end;
  918. function _ConvertForLoops(var n: tnode; arg: pointer): foreachnoderesult;
  919. var
  920. hp : tnode;
  921. begin
  922. Result:=fen_false;
  923. if n.nodetype=forn then
  924. begin
  925. Result:=fen_true;
  926. hp:=n;
  927. n:=tfornode(n).makewhileloop;
  928. do_firstpass(n);
  929. hp.Free;
  930. end;
  931. end;
  932. function ConvertForLoops(var n : tnode) : boolean;
  933. begin
  934. result:=foreachnodestatic(pm_postprocess,n,@_ConvertForLoops,nil);
  935. end;
  936. {****************************************************************************
  937. TLOOPNODE
  938. *****************************************************************************}
  939. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  940. begin
  941. inherited create(tt,l,r);
  942. t1:=_t1;
  943. t2:=_t2;
  944. fileinfo:=l.fileinfo;
  945. end;
  946. destructor tloopnode.destroy;
  947. begin
  948. t1.free;
  949. t2.free;
  950. inherited destroy;
  951. end;
  952. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  953. begin
  954. inherited ppuload(t,ppufile);
  955. t1:=ppuloadnode(ppufile);
  956. t2:=ppuloadnode(ppufile);
  957. ppufile.getset(tppuset1(loopflags));
  958. end;
  959. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  960. begin
  961. inherited ppuwrite(ppufile);
  962. ppuwritenode(ppufile,t1);
  963. ppuwritenode(ppufile,t2);
  964. ppufile.putset(tppuset1(loopflags));
  965. end;
  966. procedure tloopnode.buildderefimpl;
  967. begin
  968. inherited buildderefimpl;
  969. if assigned(t1) then
  970. t1.buildderefimpl;
  971. if assigned(t2) then
  972. t2.buildderefimpl;
  973. end;
  974. procedure tloopnode.derefimpl;
  975. begin
  976. inherited derefimpl;
  977. if assigned(t1) then
  978. t1.derefimpl;
  979. if assigned(t2) then
  980. t2.derefimpl;
  981. end;
  982. function tloopnode.dogetcopy : tnode;
  983. var
  984. p : tloopnode;
  985. begin
  986. p:=tloopnode(inherited dogetcopy);
  987. if assigned(t1) then
  988. p.t1:=t1.dogetcopy
  989. else
  990. p.t1:=nil;
  991. if assigned(t2) then
  992. p.t2:=t2.dogetcopy
  993. else
  994. p.t2:=nil;
  995. p.loopflags:=loopflags;
  996. dogetcopy:=p;
  997. end;
  998. procedure tloopnode.insertintolist(l : tnodelist);
  999. begin
  1000. end;
  1001. procedure tloopnode.printnodetree(var t:text);
  1002. begin
  1003. write(t,printnodeindention,'(');
  1004. printnodeindent;
  1005. printnodeinfo(t);
  1006. writeln(t);
  1007. printnode(t,left);
  1008. printnode(t,right);
  1009. printnode(t,t1);
  1010. printnode(t,t2);
  1011. printnodeunindent;
  1012. writeln(t,printnodeindention,')');
  1013. end;
  1014. {$ifdef DEBUG_NODE_XML}
  1015. procedure TLoopNode.XMLPrintNodeInfo(var T: Text);
  1016. var
  1017. i: TLoopFlag;
  1018. First: Boolean;
  1019. begin
  1020. inherited XMLPrintNodeInfo(T);
  1021. First := True;
  1022. for i := Low(TLoopFlag) to High(TLoopFlag) do
  1023. if i in loopflags then
  1024. begin
  1025. if First then
  1026. begin
  1027. Write(T, ' loopflags="', i);
  1028. First := False;
  1029. end
  1030. else
  1031. Write(T, ',', i)
  1032. end;
  1033. if not First then
  1034. Write(T, '"');
  1035. end;
  1036. procedure TLoopNode.XMLPrintNodeTree(var T: Text);
  1037. begin
  1038. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  1039. XMLPrintNodeInfo(T);
  1040. WriteLn(T, '>');
  1041. PrintNodeIndent;
  1042. if Assigned(Left) then
  1043. begin
  1044. if nodetype = forn then
  1045. WriteLn(T, PrintNodeIndention, '<counter>')
  1046. else
  1047. WriteLn(T, PrintNodeIndention, '<condition>');
  1048. PrintNodeIndent;
  1049. XMLPrintNode(T, Left);
  1050. PrintNodeUnindent;
  1051. if nodetype = forn then
  1052. WriteLn(T, PrintNodeIndention, '</counter>')
  1053. else
  1054. WriteLn(T, PrintNodeIndention, '</condition>');
  1055. end;
  1056. if Assigned(Right) then
  1057. begin
  1058. case nodetype of
  1059. ifn:
  1060. WriteLn(T, PrintNodeIndention, '<then>');
  1061. forn:
  1062. WriteLn(T, PrintNodeIndention, '<first>');
  1063. else
  1064. WriteLn(T, PrintNodeIndention, '<right>');
  1065. end;
  1066. PrintNodeIndent;
  1067. XMLPrintNode(T, Right);
  1068. PrintNodeUnindent;
  1069. case nodetype of
  1070. ifn:
  1071. WriteLn(T, PrintNodeIndention, '</then>');
  1072. forn:
  1073. WriteLn(T, PrintNodeIndention, '</first>');
  1074. else
  1075. WriteLn(T, PrintNodeIndention, '</right>');
  1076. end;
  1077. end;
  1078. if Assigned(t1) then
  1079. begin
  1080. case nodetype of
  1081. ifn:
  1082. WriteLn(T, PrintNodeIndention, '<else>');
  1083. forn:
  1084. WriteLn(T, PrintNodeIndention, '<last>');
  1085. else
  1086. WriteLn(T, PrintNodeIndention, '<t1>');
  1087. end;
  1088. PrintNodeIndent;
  1089. XMLPrintNode(T, t1);
  1090. PrintNodeUnindent;
  1091. case nodetype of
  1092. ifn:
  1093. WriteLn(T, PrintNodeIndention, '</else>');
  1094. forn:
  1095. WriteLn(T, PrintNodeIndention, '</last>');
  1096. else
  1097. WriteLn(T, PrintNodeIndention, '</t1>');
  1098. end;
  1099. end;
  1100. if Assigned(t2) then
  1101. begin
  1102. if nodetype <> forn then
  1103. begin
  1104. WriteLn(T, PrintNodeIndention, '<loop>');
  1105. PrintNodeIndent;
  1106. end;
  1107. XMLPrintNode(T, t2);
  1108. if nodetype <> forn then
  1109. begin
  1110. PrintNodeUnindent;
  1111. WriteLn(T, PrintNodeIndention, '</loop>');
  1112. end;
  1113. end;
  1114. PrintNodeUnindent;
  1115. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  1116. end;
  1117. {$endif DEBUG_NODE_XML}
  1118. function tloopnode.docompare(p: tnode): boolean;
  1119. begin
  1120. docompare :=
  1121. inherited docompare(p) and
  1122. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  1123. t1.isequal(tloopnode(p).t1) and
  1124. t2.isequal(tloopnode(p).t2);
  1125. end;
  1126. {****************************************************************************
  1127. TWHILEREPEATNODE
  1128. *****************************************************************************}
  1129. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  1130. begin
  1131. inherited create(whilerepeatn,l,r,nil,nil);
  1132. if tab then
  1133. include(loopflags, lnf_testatbegin);
  1134. if cn then
  1135. include(loopflags,lnf_checknegate);
  1136. end;
  1137. function twhilerepeatnode.pass_typecheck:tnode;
  1138. var
  1139. t:Tunarynode;
  1140. begin
  1141. result:=nil;
  1142. resultdef:=voidtype;
  1143. typecheckpass(left);
  1144. { tp procvar support }
  1145. maybe_call_procvar(left,true);
  1146. {A not node can be removed.}
  1147. if left.nodetype=notn then
  1148. begin
  1149. t:=Tunarynode(left);
  1150. left:=Tunarynode(left).left;
  1151. t.left:=nil;
  1152. t.destroy;
  1153. {Symdif operator, in case you are wondering:}
  1154. loopflags:=loopflags >< [lnf_checknegate];
  1155. end;
  1156. { loop instruction }
  1157. if assigned(right) then
  1158. typecheckpass(right);
  1159. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1160. if codegenerror then
  1161. exit;
  1162. if not(is_boolean(left.resultdef)) and
  1163. not(is_typeparam(left.resultdef)) then
  1164. inserttypeconv(left,pasbool1type);
  1165. { Give warnings for code that will never be executed for
  1166. while false do }
  1167. if (lnf_testatbegin in loopflags) and
  1168. (left.nodetype=ordconstn) and
  1169. (tordconstnode(left).value.uvalue=0) and
  1170. not(nf_internal in left.flags) and
  1171. assigned(right) then
  1172. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1173. end;
  1174. {$ifdef prefetchnext}
  1175. type
  1176. passignmentquery = ^tassignmentquery;
  1177. tassignmentquery = record
  1178. towhat: tnode;
  1179. source: tassignmentnode;
  1180. statementcount: cardinal;
  1181. end;
  1182. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  1183. var
  1184. query: passignmentquery absolute arg;
  1185. temp, prederef: tnode;
  1186. begin
  1187. result := fen_norecurse_false;
  1188. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  1189. inc(query^.statementcount);
  1190. { make sure there's something else in the loop besides going to the }
  1191. { next item }
  1192. if (query^.statementcount > 1) and
  1193. (n.nodetype = assignn) then
  1194. begin
  1195. { skip type conversions of assignment target }
  1196. temp := tassignmentnode(n).left;
  1197. while (temp.nodetype = typeconvn) do
  1198. temp := ttypeconvnode(temp).left;
  1199. { assignment to x of the while assigned(x) check? }
  1200. if not(temp.isequal(query^.towhat)) then
  1201. exit;
  1202. { right hand side of assignment dereferenced field of }
  1203. { x? (no derefn in case of class) }
  1204. temp := tassignmentnode(n).right;
  1205. while (temp.nodetype = typeconvn) do
  1206. temp := ttypeconvnode(temp).left;
  1207. if (temp.nodetype <> subscriptn) then
  1208. exit;
  1209. prederef := tsubscriptnode(temp).left;
  1210. temp := prederef;
  1211. while (temp.nodetype = typeconvn) do
  1212. temp := ttypeconvnode(temp).left;
  1213. { see tests/test/prefetch1.pp }
  1214. if (temp.nodetype = derefn) then
  1215. temp := tderefnode(temp).left
  1216. else
  1217. temp := prederef;
  1218. if temp.isequal(query^.towhat) then
  1219. begin
  1220. query^.source := tassignmentnode(n);
  1221. result := fen_norecurse_true;
  1222. end
  1223. end
  1224. { don't check nodes which can't contain an assignment or whose }
  1225. { final assignment can vary a lot }
  1226. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1227. result := fen_false;
  1228. end;
  1229. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1230. var
  1231. query: tassignmentquery;
  1232. begin
  1233. query.towhat := towhat;
  1234. query.source := nil;
  1235. query.statementcount := 0;
  1236. if foreachnodestatic(where,@checkassignment,@query) then
  1237. result := query.source
  1238. else
  1239. result := nil;
  1240. end;
  1241. {$endif prefetchnext}
  1242. function twhilerepeatnode.pass_1 : tnode;
  1243. {$ifdef prefetchnext}
  1244. var
  1245. runnernode, prefetchcode: tnode;
  1246. assignmentnode: tassignmentnode;
  1247. prefetchstatements: tstatementnode;
  1248. {$endif prefetchnext}
  1249. begin
  1250. result:=nil;
  1251. expectloc:=LOC_VOID;
  1252. firstpass(left);
  1253. if codegenerror then
  1254. exit;
  1255. { loop instruction }
  1256. if assigned(right) then
  1257. begin
  1258. firstpass(right);
  1259. if codegenerror then
  1260. exit;
  1261. end;
  1262. {$ifdef prefetchnext}
  1263. { do at the end so all complex typeconversions are already }
  1264. { converted to calln's }
  1265. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1266. (lnf_testatbegin in loopflags) then
  1267. begin
  1268. { get first component of the while check }
  1269. runnernode := left;
  1270. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1271. runnernode := tunarynode(runnernode).left;
  1272. { is it an assigned(x) check? }
  1273. if ((runnernode.nodetype = inlinen) and
  1274. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1275. ((runnernode.nodetype = unequaln) and
  1276. (taddnode(runnernode).right.nodetype = niln)) then
  1277. begin
  1278. runnernode := tunarynode(runnernode).left;
  1279. { in case of in_assigned_x, there's a callparan in between }
  1280. if (runnernode.nodetype = callparan) then
  1281. runnernode := tcallparanode(runnernode).left;
  1282. while (runnernode.nodetype = typeconvn) do
  1283. runnernode := ttypeconvnode(runnernode).left;
  1284. { is there an "x := x(^).somefield"? }
  1285. assignmentnode := findassignment(right,runnernode);
  1286. if assigned(assignmentnode) then
  1287. begin
  1288. prefetchcode := internalstatements(prefetchstatements);
  1289. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1290. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1291. addstatement(prefetchstatements,right);
  1292. right := prefetchcode;
  1293. typecheckpass(right);
  1294. end;
  1295. end;
  1296. end;
  1297. {$endif prefetchnext}
  1298. end;
  1299. {$ifdef state_tracking}
  1300. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1301. var condition:Tnode;
  1302. code:Tnode;
  1303. done:boolean;
  1304. value:boolean;
  1305. change:boolean;
  1306. firsttest:boolean;
  1307. factval:Tnode;
  1308. begin
  1309. track_state_pass:=false;
  1310. done:=false;
  1311. firsttest:=true;
  1312. {For repeat until statements, first do a pass through the code.}
  1313. if not(lnf_testatbegin in flags) then
  1314. begin
  1315. code:=right.getcopy;
  1316. if code.track_state_pass(exec_known) then
  1317. track_state_pass:=true;
  1318. code.destroy;
  1319. end;
  1320. repeat
  1321. condition:=left.getcopy;
  1322. code:=right.getcopy;
  1323. change:=condition.track_state_pass(exec_known);
  1324. factval:=aktstate.find_fact(left);
  1325. if factval<>nil then
  1326. begin
  1327. condition.destroy;
  1328. condition:=factval.getcopy;
  1329. change:=true;
  1330. end;
  1331. if change then
  1332. begin
  1333. track_state_pass:=true;
  1334. {Force new resultdef pass.}
  1335. condition.resultdef:=nil;
  1336. do_typecheckpass(condition);
  1337. end;
  1338. if is_constboolnode(condition) then
  1339. begin
  1340. {Try to turn a while loop into a repeat loop.}
  1341. if firsttest then
  1342. exclude(flags,testatbegin);
  1343. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1344. if value then
  1345. begin
  1346. if code.track_state_pass(exec_known) then
  1347. track_state_pass:=true;
  1348. end
  1349. else
  1350. done:=true;
  1351. end
  1352. else
  1353. begin
  1354. {Remove any modified variables from the state.}
  1355. code.track_state_pass(false);
  1356. done:=true;
  1357. end;
  1358. code.destroy;
  1359. condition.destroy;
  1360. firsttest:=false;
  1361. until done;
  1362. {The loop condition is also known, for example:
  1363. while i<10 do
  1364. begin
  1365. ...
  1366. end;
  1367. When the loop is done, we do know that i<10 = false.
  1368. }
  1369. condition:=left.getcopy;
  1370. if condition.track_state_pass(exec_known) then
  1371. begin
  1372. track_state_pass:=true;
  1373. {Force new resultdef pass.}
  1374. condition.resultdef:=nil;
  1375. do_typecheckpass(condition);
  1376. end;
  1377. if not is_constboolnode(condition) then
  1378. aktstate.store_fact(condition,
  1379. cordconstnode.create(byte(checknegate),pasbool1type,true))
  1380. else
  1381. condition.destroy;
  1382. end;
  1383. {$endif}
  1384. {*****************************************************************************
  1385. TIFNODE
  1386. *****************************************************************************}
  1387. constructor tifnode.create(l,r,_t1 : tnode);
  1388. begin
  1389. inherited create(ifn,l,r,_t1,nil);
  1390. end;
  1391. constructor tifnode.create_internal(l,r,_t1 : tnode);
  1392. begin
  1393. create(l,r,_t1);
  1394. include(flags,nf_internal);
  1395. end;
  1396. function tifnode.internalsimplify(warn: boolean) : tnode;
  1397. {$if defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1398. var
  1399. thenstmnt, elsestmnt: tnode;
  1400. in_nr: tinlinenumber;
  1401. paratype: tdef;
  1402. {$endif defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1403. begin
  1404. result:=nil;
  1405. { optimize constant expressions }
  1406. if (left.nodetype=ordconstn) then
  1407. begin
  1408. if tordconstnode(left).value.uvalue<>0 then
  1409. begin
  1410. if assigned(right) then
  1411. result:=right
  1412. else
  1413. result:=cnothingnode.create;
  1414. right:=nil;
  1415. if warn and assigned(t1) and not(nf_internal in left.flags) then
  1416. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1417. end
  1418. else
  1419. begin
  1420. if assigned(t1) then
  1421. result:=t1
  1422. else
  1423. result:=cnothingnode.create;
  1424. t1:=nil;
  1425. if warn and assigned(right) and not(nf_internal in left.flags) then
  1426. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1427. end;
  1428. end;
  1429. {$ifndef llvm}
  1430. {$if defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1431. { use min/max intrinsic?
  1432. convert (with <op> being <, >, >=, <=
  1433. if a <op> b then
  1434. x:=a
  1435. else
  1436. x:=b;
  1437. and
  1438. if a <op> b then
  1439. x:=a;
  1440. into appropriate min/max intrinsics
  1441. }
  1442. elsestmnt:=nil;
  1443. in_nr:=Default(tinlinenumber);
  1444. if (cs_opt_level2 in current_settings.optimizerswitches) and
  1445. (left.nodetype in [gtn,gten,ltn,lten]) and IsSingleStatement(right,thenstmnt) and
  1446. ((t1=nil) or IsSingleStatement(t1,elsestmnt)) and
  1447. (thenstmnt.nodetype=assignn) and ((t1=nil) or (elsestmnt.nodetype=assignn)) and
  1448. not(might_have_sideeffects(left)) and
  1449. ((t1=nil) or tassignmentnode(thenstmnt).left.isequal(tassignmentnode(elsestmnt).left)) and
  1450. {$if defined(i386) or defined(x86_64)}
  1451. {$ifdef i386}
  1452. (((current_settings.fputype>=fpu_sse) and is_single(tassignmentnode(thenstmnt).left.resultdef)) or
  1453. ((current_settings.fputype>=fpu_sse2) and is_double(tassignmentnode(thenstmnt).left.resultdef))
  1454. ) and
  1455. {$else i386}
  1456. (is_single(tassignmentnode(thenstmnt).left.resultdef) or is_double(tassignmentnode(thenstmnt).left.resultdef)) and
  1457. {$endif i386}
  1458. {$endif defined(i386) or defined(x86_64)}
  1459. {$if defined(xtensa)}
  1460. (CPUXTENSA_HAS_MINMAX in cpu_capabilities[current_settings.cputype]) and is_32bitint(tassignmentnode(thenstmnt).right.resultdef) and
  1461. {$endif defined(xtensa)}
  1462. {$if defined(aarch64)}
  1463. (is_single(tassignmentnode(thenstmnt).left.resultdef) or is_double(tassignmentnode(thenstmnt).left.resultdef) or
  1464. is_32bitint(tassignmentnode(thenstmnt).right.resultdef)) and
  1465. {$endif defined(aarch64)}
  1466. (
  1467. { the right size of the assignment in the then clause must either }
  1468. { equal to the left ... }
  1469. (tassignmentnode(thenstmnt).right.isequal(taddnode(left).left) and
  1470. { ... and the else clause must be either not exist }
  1471. { and the left side of the assignment in the then clause must be }
  1472. { equal to the right operand of the comparison operator }
  1473. (
  1474. ((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).right))) or
  1475. { or the else clause exists and the right side of the assignment in the else clause }
  1476. { must be equal to the right side of the comparison operator }
  1477. (assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).right)))
  1478. ) or
  1479. { ... or right operand of the comparison operator }
  1480. (tassignmentnode(thenstmnt).right.isequal(taddnode(left).right) and
  1481. { ... and the else clause must be either not exist }
  1482. { and the left side of the assignment in the then clause must be }
  1483. { equal to the left operand of the comparison operator }
  1484. (
  1485. ((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).left))) or
  1486. { or the else clause exists and the right side of the assignment in the else clause }
  1487. { must be equal to the left side of the comparison operator }
  1488. (assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).left))
  1489. )
  1490. )
  1491. ) then
  1492. begin
  1493. paratype:=tassignmentnode(thenstmnt).left.resultdef;
  1494. if ((left.nodetype in [gtn,gten]) and
  1495. tassignmentnode(thenstmnt).right.isequal(taddnode(left).left)) or
  1496. ((left.nodetype in [ltn,lten]) and
  1497. tassignmentnode(thenstmnt).right.isequal(taddnode(left).right)) then
  1498. begin
  1499. if is_double(paratype) then
  1500. in_nr:=in_max_double
  1501. else if is_single(paratype) then
  1502. in_nr:=in_max_single
  1503. else if is_u32bitint(paratype) then
  1504. in_nr:=in_max_dword
  1505. else if is_s32bitint(paratype) then
  1506. in_nr:=in_max_longint;
  1507. end
  1508. else
  1509. begin
  1510. if is_double(paratype) then
  1511. in_nr:=in_min_double
  1512. else if is_single(paratype) then
  1513. in_nr:=in_min_single
  1514. else if is_u32bitint(paratype) then
  1515. in_nr:=in_min_dword
  1516. else if is_s32bitint(paratype) then
  1517. in_nr:=in_min_longint;
  1518. end;
  1519. { for inline nodes, the first parameter is the last one in the linked list
  1520. Due to the defined behaviour for the min/max intrinsics that in case of a NaN
  1521. the second parameter is taken, we have to put the else part into the second parameter
  1522. thus pass it to the first callparanode call }
  1523. if t1=nil then
  1524. Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
  1525. cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(thenstmnt).left.getcopy,
  1526. ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
  1527. )
  1528. else
  1529. Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
  1530. cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(elsestmnt).right.getcopy,
  1531. ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
  1532. );
  1533. end;
  1534. {$endif defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64)}
  1535. {$endif llvm}
  1536. end;
  1537. function tifnode.simplify(forinline : boolean) : tnode;
  1538. begin
  1539. result:=internalsimplify(false);
  1540. end;
  1541. function tifnode.pass_typecheck:tnode;
  1542. begin
  1543. result:=nil;
  1544. resultdef:=voidtype;
  1545. typecheckpass(left);
  1546. { tp procvar support }
  1547. maybe_call_procvar(left,true);
  1548. { if path }
  1549. if assigned(right) then
  1550. typecheckpass(right);
  1551. { else path }
  1552. if assigned(t1) then
  1553. typecheckpass(t1);
  1554. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1555. if codegenerror then
  1556. exit;
  1557. if not(is_boolean(left.resultdef)) and
  1558. not(is_typeparam(left.resultdef)) then
  1559. inserttypeconv(left,pasbool1type);
  1560. result:=internalsimplify(not(nf_internal in flags));
  1561. end;
  1562. function tifnode.pass_1 : tnode;
  1563. begin
  1564. result:=nil;
  1565. expectloc:=LOC_VOID;
  1566. firstpass(left);
  1567. { if path }
  1568. if assigned(right) then
  1569. firstpass(right);
  1570. { else path }
  1571. if assigned(t1) then
  1572. firstpass(t1);
  1573. { leave if we've got an error in one of the paths }
  1574. if codegenerror then
  1575. exit;
  1576. end;
  1577. {*****************************************************************************
  1578. TFORNODE
  1579. *****************************************************************************}
  1580. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1581. begin
  1582. inherited create(forn,l,r,_t1,_t2);
  1583. if back then
  1584. include(loopflags,lnf_backward);
  1585. include(loopflags,lnf_testatbegin);
  1586. end;
  1587. destructor tfornode.destroy;
  1588. begin
  1589. if assigned(loopiteration) then
  1590. loopiteration.destroy;
  1591. inherited destroy;
  1592. end;
  1593. function tfornode.simplify(forinline : boolean) : tnode;
  1594. begin
  1595. result:=nil;
  1596. { Can we spare the first comparision? }
  1597. if (t1.nodetype=ordconstn) and
  1598. (right.nodetype=ordconstn) and
  1599. (
  1600. (
  1601. (lnf_backward in loopflags) and
  1602. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1603. ) or
  1604. (
  1605. not(lnf_backward in loopflags) and
  1606. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1607. )
  1608. ) then
  1609. exclude(loopflags,lnf_testatbegin);
  1610. if (t1.nodetype=ordconstn) and
  1611. (right.nodetype=ordconstn) and
  1612. (
  1613. (
  1614. (lnf_backward in loopflags) and
  1615. (tordconstnode(right).value<tordconstnode(t1).value)
  1616. ) or
  1617. (
  1618. not(lnf_backward in loopflags) and
  1619. (tordconstnode(right).value>tordconstnode(t1).value)
  1620. )
  1621. ) then
  1622. result:=cnothingnode.create;
  1623. end;
  1624. function tfornode.pass_typecheck:tnode;
  1625. var
  1626. res : tnode;
  1627. rangedef: tdef;
  1628. begin
  1629. result:=nil;
  1630. resultdef:=voidtype;
  1631. { process the loopvar, from and to, varstates are already set }
  1632. typecheckpass(left);
  1633. typecheckpass(right);
  1634. typecheckpass(t1);
  1635. set_varstate(left,vs_written,[]);
  1636. { Make sure that the loop var and the
  1637. from and to values are compatible types }
  1638. if not(m_iso in current_settings.modeswitches) then
  1639. rangedef:=left.resultdef
  1640. else
  1641. rangedef:=get_iso_range_type(left.resultdef);
  1642. check_ranges(right.fileinfo,right,rangedef);
  1643. inserttypeconv(right,rangedef);
  1644. check_ranges(t1.fileinfo,t1,rangedef);
  1645. inserttypeconv(t1,rangedef);
  1646. if assigned(t2) then
  1647. typecheckpass(t2);
  1648. result:=simplify(false);
  1649. { loop unrolling }
  1650. if not(assigned(result)) and
  1651. (cs_opt_loopunroll in current_settings.optimizerswitches) and
  1652. assigned(t2) and
  1653. { statements must be error free }
  1654. not(nf_error in t2.flags) then
  1655. begin
  1656. typecheckpass(t2);
  1657. res:=t2.simplify(false);
  1658. if assigned(res) then
  1659. t2:=res;
  1660. res:=unroll_loop(self);
  1661. if assigned(res) then
  1662. begin
  1663. typecheckpass(res);
  1664. result:=res;
  1665. exit;
  1666. end;
  1667. end;
  1668. end;
  1669. function tfornode.pass_1 : tnode;
  1670. begin
  1671. result:=nil;
  1672. expectloc:=LOC_VOID;
  1673. firstpass(left);
  1674. firstpass(right);
  1675. firstpass(t1);
  1676. if assigned(t2) then
  1677. firstpass(t2);
  1678. end;
  1679. function checkcontinue(var n:tnode; arg: pointer): foreachnoderesult;
  1680. begin
  1681. if n.nodetype=continuen then
  1682. result:=fen_norecurse_true
  1683. else
  1684. result:=fen_false;
  1685. end;
  1686. function tfornode.makewhileloop : tnode;
  1687. var
  1688. ifblock,loopblock : tblocknode;
  1689. ifstatements,statements,loopstatements : tstatementnode;
  1690. fromtemp,totemp : ttempcreatenode;
  1691. do_loopvar_at_end : Boolean;
  1692. { if the lower and/or upper bound are variable, we need a surrounding if }
  1693. needsifblock : Boolean;
  1694. cond : tnodetype;
  1695. fromexpr : tnode;
  1696. toexpr, leftcopy: tnode;
  1697. { if the upper bound is not constant, it must be store in a temp initially }
  1698. usetotemp : boolean;
  1699. { if the lower bound is not constant, it must be store in a temp before calculating the upper bound }
  1700. usefromtemp : boolean;
  1701. storefilepos: tfileposinfo;
  1702. countermin, countermax: Tconstexprint;
  1703. procedure iterate_counter(var s : tstatementnode;fw : boolean);
  1704. var
  1705. leftcopy: tnode;
  1706. begin
  1707. { get rid of nf_write etc. as the left node is now only read }
  1708. leftcopy:=left.getcopy;
  1709. node_reset_flags(leftcopy,[nf_pass1_done,nf_modify,nf_write]);
  1710. if fw then
  1711. addstatement(s,
  1712. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_succ_x,false,leftcopy)))
  1713. else
  1714. addstatement(s,
  1715. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_pred_x,false,leftcopy)));
  1716. end;
  1717. function iterate_counter_func(arg : tnode;fw : boolean) : tnode;
  1718. begin
  1719. if fw then
  1720. result:=cinlinenode.createintern(in_succ_x,false,arg)
  1721. else
  1722. result:=cinlinenode.createintern(in_pred_x,false,arg);
  1723. end;
  1724. begin
  1725. result:=nil;
  1726. totemp:=nil;
  1727. fromtemp:=nil;
  1728. storefilepos:=current_filepos;
  1729. current_filepos:=fileinfo;
  1730. case left.resultdef.typ of
  1731. enumdef:
  1732. begin
  1733. countermin:=tenumdef(left.resultdef).min;
  1734. countermax:=tenumdef(left.resultdef).max;
  1735. end;
  1736. orddef:
  1737. begin
  1738. countermin:=torddef(left.resultdef).low;
  1739. countermax:=torddef(left.resultdef).high;
  1740. end;
  1741. else
  1742. Internalerror(2020012601);
  1743. end;
  1744. { check if we can pred/succ the loop var at the end }
  1745. do_loopvar_at_end:=(lnf_dont_mind_loopvar_on_exit in loopflags) and
  1746. is_constnode(right) and is_constnode(t1) and
  1747. { we cannot test at the end after the pred/succ if the to value is equal to the max./min. value of the counter variable
  1748. because we either get an overflow/underflow or the compiler removes the check as it never can be true }
  1749. { checking just the min./max. value depending on the pure size of the counter does not work as the check might
  1750. get optimized away
  1751. not(not(lnf_backward in loopflags) and not(is_signed(left.resultdef)) and (get_ordinal_value(t1)=((1 shl (left.resultdef.size*8))-1))) and
  1752. not(not(lnf_backward in loopflags) and is_signed(left.resultdef) and (get_ordinal_value(t1)=((1 shl (left.resultdef.size*8-1))-1))) and
  1753. not((lnf_backward in loopflags) and not(is_signed(left.resultdef)) and (get_ordinal_value(t1)=0)) and
  1754. not((lnf_backward in loopflags) and is_signed(left.resultdef) and (get_ordinal_value(t1)=(-Tconstexprint(1 shl (left.resultdef.size*8-1))))) and
  1755. }
  1756. not(not(lnf_backward in loopflags) and (get_ordinal_value(t1)=countermax)) and
  1757. not((lnf_backward in loopflags) and (get_ordinal_value(t1)=countermin)) and
  1758. { neither might the for loop contain a continue statement as continue in a while loop would skip the increment at the end
  1759. of the loop, this could be overcome by replacing the continue statement with an pred/succ; continue sequence }
  1760. not(foreachnodestatic(t2,@checkcontinue,nil)) and
  1761. { if the loop is unrolled and there is a jump into the loop,
  1762. then we can't do the trick with incrementing the loop var only at the
  1763. end
  1764. }
  1765. not(assigned(entrylabel));
  1766. needsifblock:=not(is_constnode(right)) or not(is_constnode(t1));
  1767. { convert the for loop into a while loop }
  1768. result:=internalstatements(statements);
  1769. ifblock:=internalstatements(ifstatements);
  1770. loopblock:=internalstatements(loopstatements);
  1771. usefromtemp:=(might_have_sideeffects(t1) and not(is_const(right))) or (node_complexity(right)>1);
  1772. usetotemp:=not(is_const(t1));
  1773. if needsifblock then
  1774. begin
  1775. { do not generate a temp. for the from node, if it is a const, it can be copied directly since
  1776. no side effect might change it }
  1777. if usefromtemp then
  1778. begin
  1779. fromtemp:=ctempcreatenode.create(right.resultdef,right.resultdef.size,tt_persistent,true);
  1780. { the if block might be optimized out, so we put the deletetempnode after the if-block, however,
  1781. this causes a long life time of the fromtemp. If the final regsync is left away, the reg. allocator
  1782. figures out the needed life time. As their are no loops involved between the uses of the fromtemp,
  1783. this does no hurt }
  1784. fromtemp.includetempflag(ti_no_final_regsync);
  1785. addstatement(statements,fromtemp);
  1786. { while it would be beneficial to fold the initial reverse succ/pred into this assignment, this is
  1787. not possible because it might wrap around and the if check later on goes wrong }
  1788. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(fromtemp),right.getcopy));
  1789. end;
  1790. if usetotemp then
  1791. begin
  1792. totemp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1793. addstatement(statements,totemp);
  1794. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(totemp),t1.getcopy));
  1795. end;
  1796. if usefromtemp then
  1797. begin
  1798. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,ctemprefnode.create(fromtemp)));
  1799. if not(do_loopvar_at_end) then
  1800. iterate_counter(ifstatements,lnf_backward in loopflags);
  1801. end
  1802. else
  1803. begin
  1804. if not(do_loopvar_at_end) then
  1805. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1806. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1807. else
  1808. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1809. end;
  1810. end
  1811. else
  1812. begin
  1813. if not(do_loopvar_at_end) then
  1814. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1815. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1816. else
  1817. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1818. end;
  1819. if assigned(entrylabel) then
  1820. addstatement(ifstatements,cgotonode.create(tlabelnode(entrylabel).labsym));
  1821. if not(do_loopvar_at_end) then
  1822. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1823. { avoid copying t2, it is used only once and it might be big }
  1824. addstatement(loopstatements,t2);
  1825. t2:=nil;
  1826. if do_loopvar_at_end then
  1827. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1828. if do_loopvar_at_end then
  1829. begin
  1830. if lnf_backward in loopflags then
  1831. cond:=ltn
  1832. else
  1833. cond:=gtn;
  1834. end
  1835. else
  1836. begin
  1837. if lnf_backward in loopflags then
  1838. cond:=lten
  1839. else
  1840. cond:=gten;
  1841. end;
  1842. { get rid of nf_write etc. as the left node is now only read }
  1843. leftcopy:=left.getcopy;
  1844. node_reset_flags(leftcopy,[nf_pass1_done,nf_modify,nf_write]);
  1845. if needsifblock then
  1846. begin
  1847. if usetotemp then
  1848. toexpr:=ctemprefnode.create(totemp)
  1849. else
  1850. toexpr:=t1.getcopy;
  1851. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,toexpr),loopblock,false,true));
  1852. if usefromtemp then
  1853. fromexpr:=ctemprefnode.create(fromtemp)
  1854. else
  1855. fromexpr:=right.getcopy;
  1856. if usetotemp then
  1857. toexpr:=ctemprefnode.create(totemp)
  1858. else
  1859. toexpr:=t1.getcopy;
  1860. if lnf_backward in loopflags then
  1861. addstatement(statements,cifnode.create(caddnode.create_internal(gten,
  1862. fromexpr,toexpr),ifblock,nil))
  1863. else
  1864. addstatement(statements,cifnode.create(caddnode.create_internal(lten,
  1865. fromexpr,toexpr),ifblock,nil));
  1866. if usetotemp then
  1867. addstatement(statements,ctempdeletenode.create(totemp));
  1868. if usefromtemp then
  1869. addstatement(statements,ctempdeletenode.create(fromtemp));
  1870. end
  1871. else
  1872. begin
  1873. { is a simple comparision for equality sufficient? }
  1874. if do_loopvar_at_end and (lnf_backward in loopflags) and (lnf_counter_not_used in loopflags) then
  1875. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(equaln,leftcopy,
  1876. caddnode.create_internal(subn,t1.getcopy,cordconstnode.create(1,t1.resultdef,false))),loopblock,false,true))
  1877. else
  1878. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,t1.getcopy),loopblock,false,true));
  1879. addstatement(statements,ifblock);
  1880. end;
  1881. current_filepos:=storefilepos;
  1882. end;
  1883. {*****************************************************************************
  1884. TEXITNODE
  1885. *****************************************************************************}
  1886. constructor texitnode.create(l:tnode);
  1887. begin
  1888. inherited create(exitn,l);
  1889. if assigned(left) then
  1890. begin
  1891. { add assignment to funcretsym }
  1892. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1893. left:=cassignmentnode.create(
  1894. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1895. left);
  1896. end;
  1897. end;
  1898. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1899. begin
  1900. inherited ppuload(t,ppufile);
  1901. end;
  1902. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1903. begin
  1904. inherited ppuwrite(ppufile);
  1905. end;
  1906. function texitnode.pass_typecheck:tnode;
  1907. var
  1908. newstatement : tstatementnode;
  1909. ressym: tsym;
  1910. resdef: tdef;
  1911. begin
  1912. result:=nil;
  1913. newstatement:=nil;
  1914. if assigned(left) then
  1915. begin
  1916. result:=internalstatements(newstatement);
  1917. addstatement(newstatement,left);
  1918. left:=nil;
  1919. end;
  1920. { if the function result has been migrated to the parentfpstruct,
  1921. we have to load it back to the original location (from which the
  1922. code generator will load it into the function result location),
  1923. because the code to this that we add in tnodeutils.wrap_proc_body()
  1924. gets inserted before the exit label to which this node will jump }
  1925. if (target_info.system in systems_fpnestedstruct) and
  1926. not(nf_internal in flags) and
  1927. current_procinfo.procdef.get_funcretsym_info(ressym,resdef) and
  1928. (tabstractnormalvarsym(ressym).inparentfpstruct) then
  1929. begin
  1930. if not assigned(result) then
  1931. result:=internalstatements(newstatement);
  1932. cnodeutils.load_parentfpstruct_nested_funcret(ressym,newstatement);
  1933. end;
  1934. if assigned(result) then
  1935. begin
  1936. addstatement(newstatement,self.getcopy);
  1937. { ensure we don't insert the function result loading code again for
  1938. this node }
  1939. include(newstatement.left.flags,nf_internal);
  1940. end;
  1941. resultdef:=voidtype;
  1942. end;
  1943. function texitnode.pass_1 : tnode;
  1944. begin
  1945. result:=nil;
  1946. expectloc:=LOC_VOID;
  1947. if assigned(left) then
  1948. internalerror(2011052801);
  1949. end;
  1950. {*****************************************************************************
  1951. TBREAKNODE
  1952. *****************************************************************************}
  1953. constructor tbreaknode.create;
  1954. begin
  1955. inherited create(breakn);
  1956. end;
  1957. function tbreaknode.pass_typecheck:tnode;
  1958. begin
  1959. result:=nil;
  1960. resultdef:=voidtype;
  1961. end;
  1962. function tbreaknode.pass_1 : tnode;
  1963. begin
  1964. result:=nil;
  1965. expectloc:=LOC_VOID;
  1966. end;
  1967. {*****************************************************************************
  1968. TCONTINUENODE
  1969. *****************************************************************************}
  1970. constructor tcontinuenode.create;
  1971. begin
  1972. inherited create(continuen);
  1973. end;
  1974. function tcontinuenode.pass_typecheck:tnode;
  1975. begin
  1976. result:=nil;
  1977. resultdef:=voidtype;
  1978. end;
  1979. function tcontinuenode.pass_1 : tnode;
  1980. begin
  1981. result:=nil;
  1982. expectloc:=LOC_VOID;
  1983. end;
  1984. {*****************************************************************************
  1985. TGOTONODE
  1986. *****************************************************************************}
  1987. constructor tgotonode.create(p : tlabelsym);
  1988. begin
  1989. inherited create(goton);
  1990. exceptionblock:=current_exceptblock;
  1991. labelnode:=nil;
  1992. labelsym:=p;
  1993. end;
  1994. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1995. begin
  1996. inherited ppuload(t,ppufile);
  1997. labelnodeidx:=ppufile.getlongint;
  1998. exceptionblock:=ppufile.getbyte;
  1999. end;
  2000. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  2001. begin
  2002. inherited ppuwrite(ppufile);
  2003. labelnodeidx:=labelnode.ppuidx;
  2004. ppufile.putlongint(labelnodeidx);
  2005. ppufile.putbyte(exceptionblock);
  2006. end;
  2007. procedure tgotonode.buildderefimpl;
  2008. begin
  2009. inherited buildderefimpl;
  2010. end;
  2011. procedure tgotonode.derefimpl;
  2012. begin
  2013. inherited derefimpl;
  2014. end;
  2015. procedure tgotonode.resolveppuidx;
  2016. begin
  2017. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  2018. if labelnode.nodetype<>labeln then
  2019. internalerror(200809021);
  2020. end;
  2021. function tgotonode.pass_typecheck:tnode;
  2022. begin
  2023. result:=nil;
  2024. resultdef:=voidtype;
  2025. end;
  2026. function tgotonode.pass_1 : tnode;
  2027. var
  2028. p2 : tprocinfo;
  2029. begin
  2030. result:=nil;
  2031. expectloc:=LOC_VOID;
  2032. { The labelnode can already be set when
  2033. this node was copied }
  2034. if not(assigned(labelnode)) then
  2035. begin
  2036. { inner procedure goto? }
  2037. if assigned(labelsym.code) and
  2038. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  2039. { generated by the optimizer? }
  2040. not(assigned(labelsym.owner))) then
  2041. labelnode:=tlabelnode(labelsym.code)
  2042. else if ((m_non_local_goto in current_settings.modeswitches) and
  2043. assigned(labelsym.owner)) or
  2044. { nested exits don't need the non local goto switch }
  2045. (labelsym.realname='$nestedexit') then
  2046. begin
  2047. if current_procinfo.procdef.parast.symtablelevel>=labelsym.owner.symtablelevel then
  2048. begin
  2049. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  2050. if exceptionblock>0 then
  2051. CGMessage(cg_e_goto_inout_of_exception_block);
  2052. { goto across procedures using exception?
  2053. this is not allowed because we cannot
  2054. easily unwind the exception frame
  2055. stack
  2056. }
  2057. p2:=current_procinfo;
  2058. while true do
  2059. begin
  2060. if ((cs_implicit_exceptions in current_settings.moduleswitches) and ((p2.flags*[pi_needs_implicit_finally,pi_has_implicit_finally])<>[])) or
  2061. ((p2.flags*[pi_uses_exceptions])<>[]) then
  2062. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  2063. if labelsym.owner=p2.procdef.localst then
  2064. break;
  2065. p2:=p2.parent
  2066. end;
  2067. if assigned(labelsym.jumpbuf) then
  2068. begin
  2069. result:=ccallnode.createintern('fpc_longjmp',
  2070. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  2071. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  2072. nil)));
  2073. end
  2074. else
  2075. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  2076. end
  2077. else
  2078. CGMessagePos(self.fileinfo,cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  2079. end
  2080. else
  2081. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  2082. end;
  2083. { check if we don't mess with exception blocks }
  2084. if assigned(labelnode) and
  2085. (exceptionblock<>labelnode.exceptionblock) then
  2086. CGMessage(cg_e_goto_inout_of_exception_block);
  2087. end;
  2088. function tgotonode.dogetcopy : tnode;
  2089. var
  2090. p : tgotonode;
  2091. begin
  2092. p:=tgotonode(inherited dogetcopy);
  2093. p.exceptionblock:=exceptionblock;
  2094. { generate labelnode if not done yet }
  2095. if not(assigned(labelnode)) then
  2096. begin
  2097. if assigned(labelsym) and assigned(labelsym.code) then
  2098. labelnode:=tlabelnode(labelsym.code)
  2099. end;
  2100. p.labelsym:=labelsym;
  2101. { do not copy the label node here as we do not know if the label node is part of the tree or not,
  2102. this will be fixed after the copying in node.setuplabelnode: if the labelnode has copiedto set,
  2103. labelnode of the goto node is update }
  2104. if assigned(labelnode) then
  2105. p.labelnode:=labelnode
  2106. else
  2107. begin
  2108. { don't trigger IE when there was already an error, i.e. the
  2109. label is not defined. See tw11763 (PFV) }
  2110. if (errorcount=0) and
  2111. { don't trigger IE if it's a global goto }
  2112. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  2113. not(assigned(labelsym.owner))) then
  2114. internalerror(200610291);
  2115. end;
  2116. result:=p;
  2117. end;
  2118. function tgotonode.docompare(p: tnode): boolean;
  2119. begin
  2120. docompare := false;
  2121. end;
  2122. {*****************************************************************************
  2123. TLABELNODE
  2124. *****************************************************************************}
  2125. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  2126. begin
  2127. inherited create(labeln,l);
  2128. exceptionblock:=current_exceptblock;
  2129. labsym:=alabsym;
  2130. { Register labelnode in labelsym }
  2131. labsym.code:=self;
  2132. end;
  2133. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2134. begin
  2135. inherited ppuload(t,ppufile);
  2136. exceptionblock:=ppufile.getbyte;
  2137. end;
  2138. destructor tlabelnode.destroy;
  2139. begin
  2140. if assigned(labsym) then
  2141. begin
  2142. if not assigned(labsym.Owner) then
  2143. labsym.Free // Free labelsym if it has no owner
  2144. else
  2145. if labsym.code=pointer(self) then
  2146. begin
  2147. { Remove reference in labelsym, this is to prevent
  2148. goto's to this label }
  2149. labsym.code:=nil;
  2150. end;
  2151. end;
  2152. inherited destroy;
  2153. end;
  2154. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  2155. begin
  2156. inherited ppuwrite(ppufile);
  2157. ppufile.putbyte(exceptionblock);
  2158. end;
  2159. procedure tlabelnode.buildderefimpl;
  2160. begin
  2161. inherited buildderefimpl;
  2162. end;
  2163. procedure tlabelnode.derefimpl;
  2164. begin
  2165. inherited derefimpl;
  2166. end;
  2167. function tlabelnode.pass_typecheck:tnode;
  2168. begin
  2169. result:=nil;
  2170. { left could still be unassigned }
  2171. if assigned(left) then
  2172. typecheckpass(left);
  2173. resultdef:=voidtype;
  2174. end;
  2175. function tlabelnode.pass_1 : tnode;
  2176. begin
  2177. result:=nil;
  2178. expectloc:=LOC_VOID;
  2179. if not (nf_internal in flags) then
  2180. include(current_procinfo.flags,pi_has_label);
  2181. if assigned(left) then
  2182. firstpass(left);
  2183. if (m_non_local_goto in current_settings.modeswitches) and
  2184. { the owner can be Nil for internal labels }
  2185. assigned(labsym.owner) and
  2186. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  2187. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  2188. end;
  2189. function tlabelnode.dogetcopy : tnode;
  2190. begin
  2191. if not(assigned(copiedto)) then
  2192. copiedto:=tlabelnode(inherited dogetcopy);
  2193. copiedto.exceptionblock:=exceptionblock;
  2194. result:=copiedto;
  2195. end;
  2196. function tlabelnode.docompare(p: tnode): boolean;
  2197. begin
  2198. docompare := false;
  2199. end;
  2200. {*****************************************************************************
  2201. TRAISENODE
  2202. *****************************************************************************}
  2203. constructor traisenode.create(l,taddr,tframe:tnode);
  2204. begin
  2205. inherited create(raisen,l,taddr,tframe);
  2206. end;
  2207. function traisenode.pass_typecheck:tnode;
  2208. begin
  2209. result:=nil;
  2210. resultdef:=voidtype;
  2211. if assigned(left) then
  2212. begin
  2213. { first para must be a _class_ }
  2214. typecheckpass(left);
  2215. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2216. if codegenerror then
  2217. exit;
  2218. if not is_class(left.resultdef) and
  2219. not is_javaclass(left.resultdef) then
  2220. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2221. { insert needed typeconvs for addr,frame }
  2222. if assigned(right) then
  2223. begin
  2224. { addr }
  2225. typecheckpass(right);
  2226. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2227. inserttypeconv(right,voidcodepointertype);
  2228. { frame }
  2229. if assigned(third) then
  2230. begin
  2231. typecheckpass(third);
  2232. set_varstate(third,vs_read,[vsf_must_be_valid]);
  2233. inserttypeconv(third,voidpointertype);
  2234. end;
  2235. end;
  2236. end;
  2237. end;
  2238. function traisenode.pass_1 : tnode;
  2239. var
  2240. statements : tstatementnode;
  2241. current_addr : tlabelnode;
  2242. raisenode : tcallnode;
  2243. begin
  2244. result:=internalstatements(statements);
  2245. if assigned(left) then
  2246. begin
  2247. { first para must be a class }
  2248. firstpass(left);
  2249. { insert needed typeconvs for addr,frame }
  2250. if assigned(right) then
  2251. begin
  2252. { addr }
  2253. firstpass(right);
  2254. { frame }
  2255. if assigned(third) then
  2256. firstpass(third)
  2257. else
  2258. third:=cpointerconstnode.Create(0,voidpointertype);
  2259. end
  2260. else
  2261. begin
  2262. third:=cinlinenode.create(in_get_frame,false,nil);
  2263. current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  2264. include(current_addr.flags,nf_internal);
  2265. addstatement(statements,current_addr);
  2266. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  2267. { raise address off by one so we are for sure inside the action area for the raise }
  2268. if tf_use_psabieh in target_info.flags then
  2269. right:=caddnode.create_internal(addn,right,cordconstnode.create(1,sizesinttype,false));
  2270. end;
  2271. raisenode:=ccallnode.createintern('fpc_raiseexception',
  2272. ccallparanode.create(third,
  2273. ccallparanode.create(right,
  2274. ccallparanode.create(left,nil)))
  2275. );
  2276. include(raisenode.callnodeflags,cnf_call_never_returns);
  2277. addstatement(statements,raisenode);
  2278. end
  2279. else
  2280. begin
  2281. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  2282. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  2283. include(raisenode.callnodeflags,cnf_call_never_returns);
  2284. addstatement(statements,raisenode);
  2285. end;
  2286. left:=nil;
  2287. right:=nil;
  2288. third:=nil;
  2289. end;
  2290. {*****************************************************************************
  2291. TTRYEXCEPTNODE
  2292. *****************************************************************************}
  2293. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  2294. begin
  2295. inherited create(tryexceptn,l,r,_t1,nil);
  2296. end;
  2297. function ttryexceptnode.pass_typecheck:tnode;
  2298. begin
  2299. result:=nil;
  2300. typecheckpass(left);
  2301. { on statements }
  2302. if assigned(right) then
  2303. typecheckpass(right);
  2304. { else block }
  2305. if assigned(t1) then
  2306. typecheckpass(t1);
  2307. resultdef:=voidtype;
  2308. end;
  2309. function ttryexceptnode.pass_1 : tnode;
  2310. begin
  2311. result:=nil;
  2312. expectloc:=LOC_VOID;
  2313. firstpass(left);
  2314. { on statements }
  2315. if assigned(right) then
  2316. firstpass(right);
  2317. { else block }
  2318. if assigned(t1) then
  2319. firstpass(t1);
  2320. include(current_procinfo.flags,pi_do_call);
  2321. include(current_procinfo.flags,pi_uses_exceptions);
  2322. adjust_estimated_stack_size;
  2323. end;
  2324. function ttryexceptnode.simplify(forinline: boolean): tnode;
  2325. begin
  2326. result:=nil;
  2327. { empty try -> can never raise exception -> do nothing }
  2328. if has_no_code(left) then
  2329. result:=cnothingnode.create;
  2330. end;
  2331. procedure ttryexceptnode.adjust_estimated_stack_size;
  2332. begin
  2333. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size*2);
  2334. end;
  2335. {*****************************************************************************
  2336. TTRYFINALLYNODE
  2337. *****************************************************************************}
  2338. constructor ttryfinallynode.create(l,r:tnode);
  2339. begin
  2340. inherited create(tryfinallyn,l,r,nil);
  2341. third:=nil;
  2342. implicitframe:=false;
  2343. end;
  2344. constructor ttryfinallynode.create_implicit(l,r:tnode);
  2345. begin
  2346. inherited create(tryfinallyn,l,r,nil);
  2347. third:=nil;
  2348. implicitframe:=true;
  2349. end;
  2350. function ttryfinallynode.pass_typecheck:tnode;
  2351. begin
  2352. result:=nil;
  2353. resultdef:=voidtype;
  2354. typecheckpass(left);
  2355. // "try block" is "used"? (JM)
  2356. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  2357. typecheckpass(right);
  2358. // "except block" is "used"? (JM)
  2359. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  2360. if assigned(third) then
  2361. begin
  2362. typecheckpass(third);
  2363. set_varstate(third,vs_readwritten,[vsf_must_be_valid]);
  2364. end;
  2365. end;
  2366. function ttryfinallynode.pass_1 : tnode;
  2367. begin
  2368. result:=nil;
  2369. expectloc:=LOC_VOID;
  2370. firstpass(left);
  2371. firstpass(right);
  2372. if assigned(third) then
  2373. firstpass(third);
  2374. include(current_procinfo.flags,pi_do_call);
  2375. { pi_uses_exceptions is an information for the optimizer and it
  2376. is only interested in exceptions if they appear inside the body,
  2377. so ignore implicit frames when setting the flag }
  2378. if not(implicitframe) then
  2379. include(current_procinfo.flags,pi_uses_exceptions);
  2380. adjust_estimated_stack_size;
  2381. end;
  2382. function ttryfinallynode.simplify(forinline : boolean): tnode;
  2383. begin
  2384. result:=nil;
  2385. { if the try contains no code, we can kill
  2386. the try and except and return only the
  2387. finally part }
  2388. if has_no_code(left) then
  2389. begin
  2390. result:=right;
  2391. right:=nil;
  2392. end
  2393. { if the finally block contains no code, we can kill
  2394. it and just return the try part }
  2395. else if has_no_code(right) and not(assigned(third)) and not(implicitframe) then
  2396. begin
  2397. result:=left;
  2398. left:=nil;
  2399. end;
  2400. end;
  2401. function ttryfinallynode.dogetcopy: tnode;
  2402. begin
  2403. result:=inherited dogetcopy;
  2404. ttryfinallynode(result).implicitframe:=implicitframe;
  2405. end;
  2406. procedure ttryfinallynode.adjust_estimated_stack_size;
  2407. begin
  2408. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size);
  2409. end;
  2410. {*****************************************************************************
  2411. TONNODE
  2412. *****************************************************************************}
  2413. constructor tonnode.create(l,r:tnode);
  2414. begin
  2415. inherited create(onn,l,r);
  2416. excepTSymtable:=nil;
  2417. excepttype:=nil;
  2418. end;
  2419. destructor tonnode.destroy;
  2420. begin
  2421. { copied nodes don't need to release the symtable }
  2422. if assigned(excepTSymtable) then
  2423. excepTSymtable.free;
  2424. inherited destroy;
  2425. end;
  2426. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2427. begin
  2428. inherited ppuload(t,ppufile);
  2429. excepTSymtable:=nil;
  2430. excepttype:=nil;
  2431. end;
  2432. function tonnode.dogetcopy : tnode;
  2433. var
  2434. n : tonnode;
  2435. begin
  2436. n:=tonnode(inherited dogetcopy);
  2437. if assigned(exceptsymtable) then
  2438. n.exceptsymtable:=exceptsymtable.getcopy
  2439. else
  2440. n.exceptsymtable:=nil;
  2441. n.excepttype:=excepttype;
  2442. result:=n;
  2443. end;
  2444. function tonnode.pass_typecheck:tnode;
  2445. begin
  2446. result:=nil;
  2447. resultdef:=voidtype;
  2448. if not is_class(excepttype) and
  2449. not is_javaclass(excepttype) then
  2450. CGMessage1(type_e_class_type_expected,excepttype.typename);
  2451. if assigned(left) then
  2452. typecheckpass(left);
  2453. if assigned(right) then
  2454. typecheckpass(right);
  2455. end;
  2456. function tonnode.pass_1 : tnode;
  2457. begin
  2458. result:=nil;
  2459. include(current_procinfo.flags,pi_do_call);
  2460. expectloc:=LOC_VOID;
  2461. if assigned(left) then
  2462. firstpass(left);
  2463. if assigned(right) then
  2464. firstpass(right);
  2465. end;
  2466. function tonnode.docompare(p: tnode): boolean;
  2467. begin
  2468. docompare := false;
  2469. end;
  2470. end.