2
0

nflw.pas 107 KB

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