nflw.pas 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948
  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) then
  1215. begin
  1216. if ((left.nodetype=gtn) and (taddnode(left).left.nodetype=loadn) and is_constintnode(taddnode(left).right) and
  1217. (tordconstnode(taddnode(left).right).value=0)) then
  1218. begin
  1219. p:=GetLastStatement(right);
  1220. if assigned(p) and (p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=in_dec_x) and
  1221. taddnode(left).left.isequal(tcallparanode(tinlinenode(p).left).left) and
  1222. not(assigned(tcallparanode(tinlinenode(p).left).right)) then
  1223. begin
  1224. result:=cifnode.create_internal(left.getcopy,cwhilerepeatnode.create(left,right,false,true),nil);
  1225. left:=nil;
  1226. right:=nil;
  1227. twhilerepeatnode(tifnode(result).right).left.nodetype:=equaln;
  1228. end;
  1229. end
  1230. else if not(cs_opt_size in current_settings.optimizerswitches) and
  1231. (node_complexity(left)<=3) then
  1232. begin
  1233. result:=cifnode.create_internal(left.getcopy,cwhilerepeatnode.create(left,right,false,false),nil);
  1234. left:=nil;
  1235. right:=nil;
  1236. end;
  1237. end;
  1238. end;
  1239. function twhilerepeatnode.simplify(forinline : boolean) : tnode;
  1240. begin
  1241. result:=internalsimplify(false);
  1242. end;
  1243. {$ifdef prefetchnext}
  1244. type
  1245. passignmentquery = ^tassignmentquery;
  1246. tassignmentquery = record
  1247. towhat: tnode;
  1248. source: tassignmentnode;
  1249. statementcount: cardinal;
  1250. end;
  1251. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  1252. var
  1253. query: passignmentquery absolute arg;
  1254. temp, prederef: tnode;
  1255. begin
  1256. result := fen_norecurse_false;
  1257. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  1258. inc(query^.statementcount);
  1259. { make sure there's something else in the loop besides going to the }
  1260. { next item }
  1261. if (query^.statementcount > 1) and
  1262. (n.nodetype = assignn) then
  1263. begin
  1264. { skip type conversions of assignment target }
  1265. temp := tassignmentnode(n).left;
  1266. while (temp.nodetype = typeconvn) do
  1267. temp := ttypeconvnode(temp).left;
  1268. { assignment to x of the while assigned(x) check? }
  1269. if not(temp.isequal(query^.towhat)) then
  1270. exit;
  1271. { right hand side of assignment dereferenced field of }
  1272. { x? (no derefn in case of class) }
  1273. temp := tassignmentnode(n).right;
  1274. while (temp.nodetype = typeconvn) do
  1275. temp := ttypeconvnode(temp).left;
  1276. if (temp.nodetype <> subscriptn) then
  1277. exit;
  1278. prederef := tsubscriptnode(temp).left;
  1279. temp := prederef;
  1280. while (temp.nodetype = typeconvn) do
  1281. temp := ttypeconvnode(temp).left;
  1282. { see tests/test/prefetch1.pp }
  1283. if (temp.nodetype = derefn) then
  1284. temp := tderefnode(temp).left
  1285. else
  1286. temp := prederef;
  1287. if temp.isequal(query^.towhat) then
  1288. begin
  1289. query^.source := tassignmentnode(n);
  1290. result := fen_norecurse_true;
  1291. end
  1292. end
  1293. { don't check nodes which can't contain an assignment or whose }
  1294. { final assignment can vary a lot }
  1295. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1296. result := fen_false;
  1297. end;
  1298. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1299. var
  1300. query: tassignmentquery;
  1301. begin
  1302. query.towhat := towhat;
  1303. query.source := nil;
  1304. query.statementcount := 0;
  1305. if foreachnodestatic(where,@checkassignment,@query) then
  1306. result := query.source
  1307. else
  1308. result := nil;
  1309. end;
  1310. {$endif prefetchnext}
  1311. function twhilerepeatnode.pass_1 : tnode;
  1312. {$ifdef prefetchnext}
  1313. var
  1314. runnernode, prefetchcode: tnode;
  1315. assignmentnode: tassignmentnode;
  1316. prefetchstatements: tstatementnode;
  1317. {$endif prefetchnext}
  1318. begin
  1319. result:=nil;
  1320. expectloc:=LOC_VOID;
  1321. firstpass(left);
  1322. if codegenerror then
  1323. exit;
  1324. { loop instruction }
  1325. if assigned(right) then
  1326. begin
  1327. firstpass(right);
  1328. if codegenerror then
  1329. exit;
  1330. end;
  1331. {$ifdef prefetchnext}
  1332. { do at the end so all complex typeconversions are already }
  1333. { converted to calln's }
  1334. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1335. (lnf_testatbegin in loopflags) then
  1336. begin
  1337. { get first component of the while check }
  1338. runnernode := left;
  1339. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1340. runnernode := tunarynode(runnernode).left;
  1341. { is it an assigned(x) check? }
  1342. if ((runnernode.nodetype = inlinen) and
  1343. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1344. ((runnernode.nodetype = unequaln) and
  1345. (taddnode(runnernode).right.nodetype = niln)) then
  1346. begin
  1347. runnernode := tunarynode(runnernode).left;
  1348. { in case of in_assigned_x, there's a callparan in between }
  1349. if (runnernode.nodetype = callparan) then
  1350. runnernode := tcallparanode(runnernode).left;
  1351. while (runnernode.nodetype = typeconvn) do
  1352. runnernode := ttypeconvnode(runnernode).left;
  1353. { is there an "x := x(^).somefield"? }
  1354. assignmentnode := findassignment(right,runnernode);
  1355. if assigned(assignmentnode) then
  1356. begin
  1357. prefetchcode := internalstatements(prefetchstatements);
  1358. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1359. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1360. addstatement(prefetchstatements,right);
  1361. right := prefetchcode;
  1362. typecheckpass(right);
  1363. end;
  1364. end;
  1365. end;
  1366. {$endif prefetchnext}
  1367. end;
  1368. {$ifdef state_tracking}
  1369. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1370. var condition:Tnode;
  1371. code:Tnode;
  1372. done:boolean;
  1373. value:boolean;
  1374. change:boolean;
  1375. firsttest:boolean;
  1376. factval:Tnode;
  1377. begin
  1378. track_state_pass:=false;
  1379. done:=false;
  1380. firsttest:=true;
  1381. {For repeat until statements, first do a pass through the code.}
  1382. if not(lnf_testatbegin in flags) then
  1383. begin
  1384. code:=right.getcopy;
  1385. if code.track_state_pass(exec_known) then
  1386. track_state_pass:=true;
  1387. code.free;
  1388. end;
  1389. repeat
  1390. condition:=left.getcopy;
  1391. code:=right.getcopy;
  1392. change:=condition.track_state_pass(exec_known);
  1393. factval:=aktstate.find_fact(left);
  1394. if factval<>nil then
  1395. begin
  1396. condition.free;
  1397. condition:=factval.getcopy;
  1398. change:=true;
  1399. end;
  1400. if change then
  1401. begin
  1402. track_state_pass:=true;
  1403. {Force new resultdef pass.}
  1404. condition.resultdef:=nil;
  1405. do_typecheckpass(condition);
  1406. end;
  1407. if is_constboolnode(condition) then
  1408. begin
  1409. {Try to turn a while loop into a repeat loop.}
  1410. if firsttest then
  1411. exclude(flags,testatbegin);
  1412. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1413. if value then
  1414. begin
  1415. if code.track_state_pass(exec_known) then
  1416. track_state_pass:=true;
  1417. end
  1418. else
  1419. done:=true;
  1420. end
  1421. else
  1422. begin
  1423. {Remove any modified variables from the state.}
  1424. code.track_state_pass(false);
  1425. done:=true;
  1426. end;
  1427. code.free;
  1428. condition.free;
  1429. firsttest:=false;
  1430. until done;
  1431. {The loop condition is also known, for example:
  1432. while i<10 do
  1433. begin
  1434. ...
  1435. end;
  1436. When the loop is done, we do know that i<10 = false.
  1437. }
  1438. condition:=left.getcopy;
  1439. if condition.track_state_pass(exec_known) then
  1440. begin
  1441. track_state_pass:=true;
  1442. {Force new resultdef pass.}
  1443. condition.resultdef:=nil;
  1444. do_typecheckpass(condition);
  1445. end;
  1446. if not is_constboolnode(condition) then
  1447. aktstate.store_fact(condition,
  1448. cordconstnode.create(byte(checknegate),pasbool1type,true))
  1449. else
  1450. condition.free;
  1451. end;
  1452. {$endif}
  1453. {*****************************************************************************
  1454. TIFNODE
  1455. *****************************************************************************}
  1456. constructor tifnode.create(l,r,_t1 : tnode);
  1457. begin
  1458. inherited create(ifn,l,r,_t1,nil);
  1459. end;
  1460. constructor tifnode.create_internal(l,r,_t1 : tnode);
  1461. begin
  1462. create(l,r,_t1);
  1463. include(flags,nf_internal);
  1464. end;
  1465. {$ifndef llvm}
  1466. {$if defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64) or defined(riscv)}
  1467. {$define HAS_MINMAX_INTRINSICS}
  1468. {$endif defined(i386) or defined(x86_64) or defined(xtensa) or defined(aarch64) or defined(riscv)}
  1469. {$endif llvm}
  1470. function tifnode.internalsimplify(warn: boolean) : tnode;
  1471. {$if defined(HAS_MINMAX_INTRINSICS)}
  1472. var
  1473. thenstmnt, elsestmnt: tnode;
  1474. in_nr: tinlinenumber;
  1475. paratype: tdef;
  1476. {$endif defined(HAS_MINMAX_INTRINSICS)}
  1477. begin
  1478. result:=nil;
  1479. { optimize constant expressions }
  1480. if (left.nodetype=ordconstn) then
  1481. begin
  1482. if tordconstnode(left).value.uvalue<>0 then
  1483. begin
  1484. if assigned(right) then
  1485. result:=right
  1486. else
  1487. result:=cnothingnode.create;
  1488. right:=nil;
  1489. if warn and assigned(t1) and not(nf_internal in left.flags) then
  1490. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1491. end
  1492. else
  1493. begin
  1494. if assigned(t1) then
  1495. result:=t1
  1496. else
  1497. result:=cnothingnode.create;
  1498. t1:=nil;
  1499. if warn and assigned(right) and not(nf_internal in left.flags) then
  1500. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1501. end;
  1502. end;
  1503. {$if defined(HAS_MINMAX_INTRINSICS)}
  1504. { use min/max intrinsic?
  1505. convert (with <op> being <, >, >=, <=
  1506. if a <op> b then
  1507. x:=a
  1508. else
  1509. x:=b;
  1510. and
  1511. if a <op> b then
  1512. x:=a;
  1513. into appropriate min/max intrinsics
  1514. }
  1515. elsestmnt:=nil;
  1516. in_nr:=Default(tinlinenumber);
  1517. if (cs_opt_level2 in current_settings.optimizerswitches) and
  1518. (left.nodetype in [gtn,gten,ltn,lten]) and IsSingleStatement(right,thenstmnt) and
  1519. ((t1=nil) or IsSingleStatement(t1,elsestmnt)) and
  1520. (thenstmnt.nodetype=assignn) and ((t1=nil) or (elsestmnt.nodetype=assignn)) and
  1521. not(might_have_sideeffects(left)) and
  1522. ((t1=nil) or tassignmentnode(thenstmnt).left.isequal(tassignmentnode(elsestmnt).left)) and
  1523. {$if defined(i386) or defined(x86_64)}
  1524. {$ifdef i386}
  1525. (((current_settings.fputype>=fpu_sse) and is_single(tassignmentnode(thenstmnt).left.resultdef)) or
  1526. ((current_settings.fputype>=fpu_sse2) and is_double(tassignmentnode(thenstmnt).left.resultdef)) or
  1527. ((CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) and is_32bitint(tassignmentnode(thenstmnt).left.resultdef))
  1528. ) and
  1529. {$else i386}
  1530. (is_single(tassignmentnode(thenstmnt).left.resultdef) or
  1531. is_double(tassignmentnode(thenstmnt).left.resultdef) or
  1532. is_32bitint(tassignmentnode(thenstmnt).left.resultdef) or
  1533. is_64bitint(tassignmentnode(thenstmnt).left.resultdef)
  1534. ) and
  1535. {$endif i386}
  1536. {$endif defined(i386) or defined(x86_64)}
  1537. {$if defined(xtensa)}
  1538. (CPUXTENSA_HAS_MINMAX in cpu_capabilities[current_settings.cputype]) and is_32bitint(tassignmentnode(thenstmnt).right.resultdef) and
  1539. {$endif defined(xtensa)}
  1540. {$if defined(aarch64)}
  1541. (is_single(tassignmentnode(thenstmnt).left.resultdef) or is_double(tassignmentnode(thenstmnt).left.resultdef) or
  1542. is_32bitint(tassignmentnode(thenstmnt).left.resultdef) or is_64bitint(tassignmentnode(thenstmnt).left.resultdef)) and
  1543. {$endif defined(aarch64)}
  1544. {$if defined(riscv)}
  1545. { RiscV fmin/fmax/fminm/fmaxm uses the IEEE semantics (2008 or 201x) of min/max regarding NaN (using either
  1546. always the NaN or non-NaN operand instead of the second one in case on is NaN), so
  1547. we can use them only when fast math is on }
  1548. ((cs_opt_fastmath in current_settings.optimizerswitches) and
  1549. ((is_single(tassignmentnode(thenstmnt).left.resultdef) and (CPURV_HAS_F in cpu_capabilities[current_settings.cputype])) or
  1550. (is_double(tassignmentnode(thenstmnt).left.resultdef) and (CPURV_HAS_D in cpu_capabilities[current_settings.cputype])) or
  1551. (is_quad(tassignmentnode(thenstmnt).left.resultdef) and (CPURV_HAS_Q in cpu_capabilities[current_settings.cputype])))) and
  1552. {$endif defined(riscv)}
  1553. (
  1554. { the right size of the assignment in the then clause must either }
  1555. { equal to the left ... }
  1556. (tassignmentnode(thenstmnt).right.isequal(taddnode(left).left) and
  1557. { ... and the else clause must be either not exist }
  1558. { and the left side of the assignment in the then clause must be }
  1559. { equal to the right operand of the comparison operator }
  1560. (
  1561. ((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).right))) or
  1562. { or the else clause exists and the right side of the assignment in the else clause }
  1563. { must be equal to the right side of the comparison operator }
  1564. (assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).right)))
  1565. ) or
  1566. { ... or right operand of the comparison operator }
  1567. (tassignmentnode(thenstmnt).right.isequal(taddnode(left).right) and
  1568. { ... and the else clause must be either not exist }
  1569. { and the left side of the assignment in the then clause must be }
  1570. { equal to the left operand of the comparison operator }
  1571. (
  1572. ((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).left))) or
  1573. { or the else clause exists and the right side of the assignment in the else clause }
  1574. { must be equal to the left side of the comparison operator }
  1575. (assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).left))
  1576. )
  1577. )
  1578. ) then
  1579. begin
  1580. paratype:=tassignmentnode(thenstmnt).left.resultdef;
  1581. if ((left.nodetype in [gtn,gten]) and
  1582. tassignmentnode(thenstmnt).right.isequal(taddnode(left).left)) or
  1583. ((left.nodetype in [ltn,lten]) and
  1584. tassignmentnode(thenstmnt).right.isequal(taddnode(left).right)) then
  1585. begin
  1586. if is_double(paratype) then
  1587. in_nr:=in_max_double
  1588. else if is_single(paratype) then
  1589. in_nr:=in_max_single
  1590. else if is_u32bitint(paratype) then
  1591. in_nr:=in_max_dword
  1592. else if is_s32bitint(paratype) then
  1593. in_nr:=in_max_longint
  1594. else if is_u64bitint(paratype) then
  1595. in_nr:=in_max_qword
  1596. else if is_s64bitint(paratype) then
  1597. in_nr:=in_max_int64;
  1598. end
  1599. else
  1600. begin
  1601. if is_double(paratype) then
  1602. in_nr:=in_min_double
  1603. else if is_single(paratype) then
  1604. in_nr:=in_min_single
  1605. else if is_u32bitint(paratype) then
  1606. in_nr:=in_min_dword
  1607. else if is_s32bitint(paratype) then
  1608. in_nr:=in_min_longint
  1609. else if is_u64bitint(paratype) then
  1610. in_nr:=in_min_qword
  1611. else if is_s64bitint(paratype) then
  1612. in_nr:=in_min_int64;
  1613. end;
  1614. { for inline nodes, the first parameter is the last one in the linked list
  1615. Due to the defined behaviour for the min/max intrinsics that in case of a NaN
  1616. the second parameter is taken, we have to put the else part into the second parameter
  1617. thus pass it to the first callparanode call }
  1618. if t1=nil then
  1619. Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
  1620. cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(thenstmnt).left.getcopy,
  1621. ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
  1622. )
  1623. else
  1624. Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
  1625. cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(elsestmnt).right.getcopy,
  1626. ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
  1627. );
  1628. node_reset_pass1_write(Result);
  1629. end;
  1630. {$endif defined(HAS_MINMAX_INTRINSICS)}
  1631. end;
  1632. function tifnode.simplify(forinline : boolean) : tnode;
  1633. begin
  1634. result:=internalsimplify(false);
  1635. end;
  1636. function tifnode.pass_typecheck:tnode;
  1637. begin
  1638. result:=nil;
  1639. resultdef:=voidtype;
  1640. typecheckpass(left);
  1641. { tp procvar support }
  1642. maybe_call_procvar(left,true);
  1643. { if path }
  1644. if assigned(right) then
  1645. typecheckpass(right);
  1646. { else path }
  1647. if assigned(t1) then
  1648. typecheckpass(t1);
  1649. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1650. if codegenerror then
  1651. exit;
  1652. if not(is_boolean(left.resultdef)) and
  1653. not(is_typeparam(left.resultdef)) then
  1654. inserttypeconv(left,pasbool1type);
  1655. result:=internalsimplify(not(nf_internal in flags));
  1656. end;
  1657. function tifnode.pass_1 : tnode;
  1658. begin
  1659. result:=nil;
  1660. expectloc:=LOC_VOID;
  1661. firstpass(left);
  1662. { if path }
  1663. if assigned(right) then
  1664. firstpass(right);
  1665. { else path }
  1666. if assigned(t1) then
  1667. firstpass(t1);
  1668. { leave if we've got an error in one of the paths }
  1669. if codegenerror then
  1670. exit;
  1671. end;
  1672. {*****************************************************************************
  1673. TFORNODE
  1674. *****************************************************************************}
  1675. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1676. begin
  1677. inherited create(forn,l,r,_t1,_t2);
  1678. if back then
  1679. include(loopflags,lnf_backward);
  1680. include(loopflags,lnf_testatbegin);
  1681. end;
  1682. destructor tfornode.destroy;
  1683. begin
  1684. loopiteration.free;
  1685. inherited destroy;
  1686. end;
  1687. function tfornode.simplify(forinline : boolean) : tnode;
  1688. begin
  1689. result:=nil;
  1690. { Can we spare the first comparision? }
  1691. if (t1.nodetype=ordconstn) and
  1692. (right.nodetype=ordconstn) and
  1693. (
  1694. (
  1695. (lnf_backward in loopflags) and
  1696. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1697. ) or
  1698. (
  1699. not(lnf_backward in loopflags) and
  1700. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1701. )
  1702. ) then
  1703. exclude(loopflags,lnf_testatbegin);
  1704. if (t1.nodetype=ordconstn) and
  1705. (right.nodetype=ordconstn) and
  1706. (
  1707. (
  1708. (lnf_backward in loopflags) and
  1709. (tordconstnode(right).value<tordconstnode(t1).value)
  1710. ) or
  1711. (
  1712. not(lnf_backward in loopflags) and
  1713. (tordconstnode(right).value>tordconstnode(t1).value)
  1714. )
  1715. ) then
  1716. result:=cnothingnode.create;
  1717. end;
  1718. function tfornode.pass_typecheck:tnode;
  1719. var
  1720. res : tnode;
  1721. rangedef: tdef;
  1722. begin
  1723. result:=nil;
  1724. resultdef:=voidtype;
  1725. { process the loopvar, from and to, varstates are already set }
  1726. typecheckpass(left);
  1727. typecheckpass(right);
  1728. typecheckpass(t1);
  1729. set_varstate(left,vs_written,[]);
  1730. { Make sure that the loop var and the
  1731. from and to values are compatible types }
  1732. if not(m_iso in current_settings.modeswitches) then
  1733. rangedef:=left.resultdef
  1734. else
  1735. rangedef:=get_iso_range_type(left.resultdef);
  1736. check_ranges(right.fileinfo,right,rangedef);
  1737. inserttypeconv(right,rangedef);
  1738. check_ranges(t1.fileinfo,t1,rangedef);
  1739. inserttypeconv(t1,rangedef);
  1740. if assigned(t2) then
  1741. typecheckpass(t2);
  1742. result:=simplify(false);
  1743. { loop unrolling }
  1744. if not(assigned(result)) and
  1745. (cs_opt_loopunroll in current_settings.optimizerswitches) and
  1746. assigned(t2) and
  1747. { statements must be error free }
  1748. not(tnf_error in t2.transientflags) then
  1749. begin
  1750. typecheckpass(t2);
  1751. res:=t2.simplify(false);
  1752. if assigned(res) then
  1753. t2:=res;
  1754. res:=unroll_loop(self);
  1755. if assigned(res) then
  1756. begin
  1757. typecheckpass(res);
  1758. result:=res;
  1759. exit;
  1760. end;
  1761. end;
  1762. end;
  1763. function tfornode.pass_1 : tnode;
  1764. begin
  1765. result:=nil;
  1766. expectloc:=LOC_VOID;
  1767. firstpass(left);
  1768. firstpass(right);
  1769. firstpass(t1);
  1770. if assigned(t2) then
  1771. firstpass(t2);
  1772. end;
  1773. function tfornode.makewhileloop : tnode;
  1774. var
  1775. ifblock,loopblock : tblocknode;
  1776. ifstatements,statements,loopstatements : tstatementnode;
  1777. fromtemp,totemp : ttempcreatenode;
  1778. do_loopvar_at_end : Boolean;
  1779. { if the lower and/or upper bound are variable, we need a surrounding if }
  1780. needsifblock : Boolean;
  1781. cond : tnodetype;
  1782. fromexpr : tnode;
  1783. toexpr, leftcopy: tnode;
  1784. { if the upper bound is not constant, it must be store in a temp initially }
  1785. usetotemp : boolean;
  1786. { if the lower bound is not constant, it must be store in a temp before calculating the upper bound }
  1787. usefromtemp : boolean;
  1788. storefilepos: tfileposinfo;
  1789. countermin, countermax: Tconstexprint;
  1790. procedure iterate_counter(var s : tstatementnode;fw : boolean);
  1791. var
  1792. leftcopy: tnode;
  1793. begin
  1794. { get rid of nf_write etc. as the left node is now only read }
  1795. leftcopy:=left.getcopy;
  1796. node_reset_flags(leftcopy,[nf_modify,nf_write],[tnf_pass1_done]);
  1797. if fw then
  1798. addstatement(s,
  1799. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_succ_x,false,leftcopy)))
  1800. else
  1801. addstatement(s,
  1802. cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_pred_x,false,leftcopy)));
  1803. end;
  1804. function iterate_counter_func(arg : tnode;fw : boolean) : tnode;
  1805. begin
  1806. if fw then
  1807. result:=cinlinenode.createintern(in_succ_x,false,arg)
  1808. else
  1809. result:=cinlinenode.createintern(in_pred_x,false,arg);
  1810. end;
  1811. begin
  1812. result:=nil;
  1813. totemp:=nil;
  1814. fromtemp:=nil;
  1815. storefilepos:=current_filepos;
  1816. current_filepos:=fileinfo;
  1817. case left.resultdef.typ of
  1818. enumdef:
  1819. begin
  1820. countermin:=tenumdef(left.resultdef).min;
  1821. countermax:=tenumdef(left.resultdef).max;
  1822. end;
  1823. orddef:
  1824. begin
  1825. countermin:=torddef(left.resultdef).low;
  1826. countermax:=torddef(left.resultdef).high;
  1827. end;
  1828. else
  1829. Internalerror(2020012601);
  1830. end;
  1831. { check if we can pred/succ the loop var at the end }
  1832. do_loopvar_at_end:=(lnf_dont_mind_loopvar_on_exit in loopflags) and
  1833. is_constnode(t1) and
  1834. { 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
  1835. because we either get an overflow/underflow or the compiler removes the check as it never can be true }
  1836. { checking just the min./max. value depending on the pure size of the counter does not work as the check might
  1837. get optimized away
  1838. 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
  1839. 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
  1840. not((lnf_backward in loopflags) and not(is_signed(left.resultdef)) and (get_ordinal_value(t1)=0)) and
  1841. not((lnf_backward in loopflags) and is_signed(left.resultdef) and (get_ordinal_value(t1)=(-Tconstexprint(1 shl (left.resultdef.size*8-1))))) and
  1842. }
  1843. not(not(lnf_backward in loopflags) and (get_ordinal_value(t1)=countermax)) and
  1844. not((lnf_backward in loopflags) and (get_ordinal_value(t1)=countermin)) and
  1845. { neither might the for loop contain a continue statement as continue in a while loop would skip the increment at the end
  1846. of the loop, this could be overcome by replacing the continue statement with an pred/succ; continue sequence }
  1847. not(has_node_of_type(t2,[continuen])) and
  1848. { if the loop is unrolled and there is a jump into the loop,
  1849. then we can't do the trick with incrementing the loop var only at the
  1850. end
  1851. }
  1852. not(assigned(entrylabel));
  1853. needsifblock:=not(is_constnode(right)) or not(is_constnode(t1));
  1854. { convert the for loop into a while loop }
  1855. result:=internalstatements(statements);
  1856. ifblock:=internalstatements(ifstatements);
  1857. loopblock:=internalstatements(loopstatements);
  1858. usefromtemp:=(might_have_sideeffects(t1) and not(is_const(right))) or (node_complexity(right)>1);
  1859. usetotemp:=not(is_const(t1));
  1860. if needsifblock then
  1861. begin
  1862. { do not generate a temp. for the from node, if it is a const, it can be copied directly since
  1863. no side effect might change it }
  1864. if usefromtemp then
  1865. begin
  1866. fromtemp:=ctempcreatenode.create(right.resultdef,right.resultdef.size,tt_persistent,true);
  1867. { the if block might be optimized out, so we put the deletetempnode after the if-block, however,
  1868. this causes a long life time of the fromtemp. If the final regsync is left away, the reg. allocator
  1869. figures out the needed life time. As their are no loops involved between the uses of the fromtemp,
  1870. this does no hurt }
  1871. fromtemp.includetempflag(ti_no_final_regsync);
  1872. addstatement(statements,fromtemp);
  1873. { while it would be beneficial to fold the initial reverse succ/pred into this assignment, this is
  1874. not possible because it might wrap around and the if check later on goes wrong }
  1875. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(fromtemp),right.getcopy));
  1876. end;
  1877. if usetotemp then
  1878. begin
  1879. totemp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1880. addstatement(statements,totemp);
  1881. addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(totemp),t1.getcopy));
  1882. end;
  1883. if usefromtemp then
  1884. begin
  1885. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,ctemprefnode.create(fromtemp)));
  1886. if not(do_loopvar_at_end) then
  1887. iterate_counter(ifstatements,lnf_backward in loopflags);
  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. end
  1898. else
  1899. begin
  1900. if not(do_loopvar_at_end) then
  1901. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
  1902. iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
  1903. else
  1904. addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
  1905. end;
  1906. if assigned(entrylabel) then
  1907. addstatement(ifstatements,cgotonode.create(tlabelnode(entrylabel).labsym));
  1908. if not(do_loopvar_at_end) then
  1909. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1910. { avoid copying t2, it is used only once and it might be big }
  1911. addstatement(loopstatements,t2);
  1912. t2:=nil;
  1913. if do_loopvar_at_end then
  1914. iterate_counter(loopstatements,not(lnf_backward in loopflags));
  1915. if do_loopvar_at_end then
  1916. begin
  1917. if lnf_backward in loopflags then
  1918. cond:=ltn
  1919. else
  1920. cond:=gtn;
  1921. end
  1922. else
  1923. begin
  1924. if lnf_backward in loopflags then
  1925. cond:=lten
  1926. else
  1927. cond:=gten;
  1928. end;
  1929. { get rid of nf_write etc. as the left node is now only read }
  1930. leftcopy:=left.getcopy;
  1931. node_reset_flags(leftcopy,[nf_modify,nf_write],[tnf_pass1_done]);
  1932. if needsifblock then
  1933. begin
  1934. if usetotemp then
  1935. toexpr:=ctemprefnode.create(totemp)
  1936. else
  1937. toexpr:=t1.getcopy;
  1938. { checking against zero might improve the generated assembler,
  1939. doing this transformation for other values is normally not beneficial }
  1940. if do_loopvar_at_end and (lnf_backward in loopflags) and is_constintnode(toexpr) and (tordconstnode(toexpr).value=1) and
  1941. (countermin<tordconstnode(toexpr).value) then
  1942. begin
  1943. tordconstnode(toexpr).value:=tordconstnode(toexpr).value-1;
  1944. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(equaln,leftcopy,toexpr),loopblock,false,true))
  1945. end
  1946. else
  1947. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,toexpr),loopblock,false,true));
  1948. if usefromtemp then
  1949. fromexpr:=ctemprefnode.create(fromtemp)
  1950. else
  1951. fromexpr:=right.getcopy;
  1952. if usetotemp then
  1953. toexpr:=ctemprefnode.create(totemp)
  1954. else
  1955. toexpr:=t1.getcopy;
  1956. if lnf_backward in loopflags then
  1957. addstatement(statements,cifnode.create(caddnode.create_internal(gten,
  1958. fromexpr,toexpr),ifblock,nil))
  1959. else
  1960. addstatement(statements,cifnode.create(caddnode.create_internal(lten,
  1961. fromexpr,toexpr),ifblock,nil));
  1962. if usetotemp then
  1963. addstatement(statements,ctempdeletenode.create(totemp));
  1964. if usefromtemp then
  1965. addstatement(statements,ctempdeletenode.create(fromtemp));
  1966. end
  1967. else
  1968. begin
  1969. { is a simple comparison for equality sufficient? }
  1970. if do_loopvar_at_end and (lnf_backward in loopflags) and (lnf_counter_not_used in loopflags) then
  1971. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(equaln,leftcopy,
  1972. caddnode.create_internal(subn,t1.getcopy,cordconstnode.create(1,t1.resultdef,false))),loopblock,false,true))
  1973. else
  1974. addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,t1.getcopy),loopblock,false,true));
  1975. addstatement(statements,ifblock);
  1976. end;
  1977. current_filepos:=storefilepos;
  1978. end;
  1979. {*****************************************************************************
  1980. TEXITNODE
  1981. *****************************************************************************}
  1982. constructor texitnode.create(l:tnode);
  1983. begin
  1984. inherited create(exitn,l);
  1985. if assigned(left) then
  1986. begin
  1987. { add assignment to funcretsym }
  1988. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1989. left:=cassignmentnode.create(
  1990. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1991. left);
  1992. end;
  1993. end;
  1994. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1995. begin
  1996. inherited ppuload(t,ppufile);
  1997. end;
  1998. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1999. begin
  2000. inherited ppuwrite(ppufile);
  2001. end;
  2002. function texitnode.pass_typecheck:tnode;
  2003. var
  2004. newstatement : tstatementnode;
  2005. ressym: tsym;
  2006. resdef: tdef;
  2007. begin
  2008. result:=nil;
  2009. newstatement:=nil;
  2010. if assigned(left) then
  2011. begin
  2012. result:=internalstatements(newstatement);
  2013. addstatement(newstatement,left);
  2014. left:=nil;
  2015. end;
  2016. { if the function result has been migrated to the parentfpstruct,
  2017. we have to load it back to the original location (from which the
  2018. code generator will load it into the function result location),
  2019. because the code to this that we add in tnodeutils.wrap_proc_body()
  2020. gets inserted before the exit label to which this node will jump }
  2021. if (target_info.system in systems_fpnestedstruct) and
  2022. not(nf_internal in flags) and
  2023. current_procinfo.procdef.get_funcretsym_info(ressym,resdef) and
  2024. (tabstractnormalvarsym(ressym).inparentfpstruct) then
  2025. begin
  2026. if not assigned(result) then
  2027. result:=internalstatements(newstatement);
  2028. cnodeutils.load_parentfpstruct_nested_funcret(ressym,newstatement);
  2029. end;
  2030. if assigned(result) then
  2031. begin
  2032. addstatement(newstatement,self.getcopy);
  2033. { ensure we don't insert the function result loading code again for
  2034. this node }
  2035. include(newstatement.left.flags,nf_internal);
  2036. end;
  2037. resultdef:=voidtype;
  2038. end;
  2039. function texitnode.pass_1 : tnode;
  2040. begin
  2041. result:=nil;
  2042. expectloc:=LOC_VOID;
  2043. if assigned(left) then
  2044. internalerror(2011052801);
  2045. end;
  2046. {*****************************************************************************
  2047. TBREAKNODE
  2048. *****************************************************************************}
  2049. constructor tbreaknode.create;
  2050. begin
  2051. inherited create(breakn);
  2052. end;
  2053. function tbreaknode.pass_typecheck:tnode;
  2054. begin
  2055. result:=nil;
  2056. resultdef:=voidtype;
  2057. end;
  2058. function tbreaknode.pass_1 : tnode;
  2059. begin
  2060. result:=nil;
  2061. expectloc:=LOC_VOID;
  2062. end;
  2063. {*****************************************************************************
  2064. TCONTINUENODE
  2065. *****************************************************************************}
  2066. constructor tcontinuenode.create;
  2067. begin
  2068. inherited create(continuen);
  2069. end;
  2070. function tcontinuenode.pass_typecheck:tnode;
  2071. begin
  2072. result:=nil;
  2073. resultdef:=voidtype;
  2074. end;
  2075. function tcontinuenode.pass_1 : tnode;
  2076. begin
  2077. result:=nil;
  2078. expectloc:=LOC_VOID;
  2079. end;
  2080. {*****************************************************************************
  2081. TGOTONODE
  2082. *****************************************************************************}
  2083. constructor tgotonode.create(p : tlabelsym);
  2084. begin
  2085. inherited create(goton);
  2086. exceptionblock:=current_exceptblock;
  2087. labelnode:=nil;
  2088. labelsym:=p;
  2089. end;
  2090. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2091. begin
  2092. inherited ppuload(t,ppufile);
  2093. labelnodeidx:=ppufile.getlongint;
  2094. exceptionblock:=ppufile.getbyte;
  2095. end;
  2096. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  2097. begin
  2098. inherited ppuwrite(ppufile);
  2099. labelnodeidx:=labelnode.ppuidx;
  2100. ppufile.putlongint(labelnodeidx);
  2101. ppufile.putbyte(exceptionblock);
  2102. end;
  2103. procedure tgotonode.buildderefimpl;
  2104. begin
  2105. inherited buildderefimpl;
  2106. end;
  2107. procedure tgotonode.derefimpl;
  2108. begin
  2109. inherited derefimpl;
  2110. end;
  2111. procedure tgotonode.resolveppuidx;
  2112. begin
  2113. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  2114. if labelnode.nodetype<>labeln then
  2115. internalerror(200809021);
  2116. end;
  2117. function tgotonode.pass_typecheck:tnode;
  2118. begin
  2119. result:=nil;
  2120. resultdef:=voidtype;
  2121. end;
  2122. function tgotonode.pass_1 : tnode;
  2123. var
  2124. p2 : tprocinfo;
  2125. begin
  2126. result:=nil;
  2127. expectloc:=LOC_VOID;
  2128. { The labelnode can already be set when
  2129. this node was copied }
  2130. if not(assigned(labelnode)) then
  2131. begin
  2132. { inner procedure goto? }
  2133. if assigned(labelsym.code) and
  2134. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  2135. { generated by the optimizer? }
  2136. not(assigned(labelsym.owner))) then
  2137. labelnode:=tlabelnode(labelsym.code)
  2138. else if ((m_non_local_goto in current_settings.modeswitches) and
  2139. assigned(labelsym.owner)) or
  2140. { nested exits don't need the non local goto switch }
  2141. (labelsym.realname='$nestedexit') then
  2142. begin
  2143. if current_procinfo.procdef.parast.symtablelevel>=labelsym.owner.symtablelevel then
  2144. begin
  2145. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  2146. if exceptionblock>0 then
  2147. CGMessage(cg_e_goto_inout_of_exception_block);
  2148. { goto across procedures using exception?
  2149. this is not allowed because we cannot
  2150. easily unwind the exception frame
  2151. stack
  2152. }
  2153. p2:=current_procinfo;
  2154. while true do
  2155. begin
  2156. if ((cs_implicit_exceptions in current_settings.moduleswitches) and ((p2.flags*[pi_needs_implicit_finally,pi_has_implicit_finally])<>[])) or
  2157. ((p2.flags*[pi_uses_exceptions])<>[]) then
  2158. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  2159. if labelsym.owner=p2.procdef.localst then
  2160. break;
  2161. p2:=p2.parent
  2162. end;
  2163. if assigned(labelsym.jumpbuf) then
  2164. begin
  2165. result:=ccallnode.createintern('fpc_longjmp',
  2166. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  2167. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  2168. nil)));
  2169. end
  2170. else
  2171. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  2172. end
  2173. else
  2174. CGMessagePos(self.fileinfo,cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  2175. end
  2176. else
  2177. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  2178. end;
  2179. { check if we don't mess with exception blocks }
  2180. if assigned(labelnode) and
  2181. (exceptionblock<>labelnode.exceptionblock) then
  2182. CGMessage(cg_e_goto_inout_of_exception_block);
  2183. end;
  2184. function tgotonode.dogetcopy : tnode;
  2185. var
  2186. p : tgotonode;
  2187. begin
  2188. p:=tgotonode(inherited dogetcopy);
  2189. p.exceptionblock:=exceptionblock;
  2190. { generate labelnode if not done yet }
  2191. if not(assigned(labelnode)) then
  2192. begin
  2193. if assigned(labelsym) and assigned(labelsym.code) then
  2194. labelnode:=tlabelnode(labelsym.code)
  2195. end;
  2196. p.labelsym:=labelsym;
  2197. { do not copy the label node here as we do not know if the label node is part of the tree or not,
  2198. this will be fixed after the copying in node.setuplabelnode: if the labelnode has copiedto set,
  2199. labelnode of the goto node is update }
  2200. if assigned(labelnode) then
  2201. p.labelnode:=labelnode
  2202. else
  2203. begin
  2204. { don't trigger IE when there was already an error, i.e. the
  2205. label is not defined. See tw11763 (PFV) }
  2206. if (errorcount=0) and
  2207. { don't trigger IE if it's a global goto }
  2208. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  2209. not(assigned(labelsym.owner))) then
  2210. internalerror(200610291);
  2211. end;
  2212. result:=p;
  2213. end;
  2214. function tgotonode.docompare(p: tnode): boolean;
  2215. begin
  2216. docompare := false;
  2217. end;
  2218. {*****************************************************************************
  2219. TLABELNODE
  2220. *****************************************************************************}
  2221. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  2222. begin
  2223. inherited create(labeln);
  2224. exceptionblock:=current_exceptblock;
  2225. labsym:=alabsym;
  2226. { Register labelnode in labelsym }
  2227. labsym.code:=self;
  2228. end;
  2229. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2230. begin
  2231. inherited ppuload(t,ppufile);
  2232. exceptionblock:=ppufile.getbyte;
  2233. end;
  2234. destructor tlabelnode.destroy;
  2235. begin
  2236. if assigned(labsym) then
  2237. begin
  2238. if not assigned(labsym.Owner) then
  2239. labsym.Free // Free labelsym if it has no owner
  2240. else
  2241. if labsym.code=pointer(self) then
  2242. begin
  2243. { Remove reference in labelsym, this is to prevent
  2244. goto's to this label }
  2245. labsym.code:=nil;
  2246. end;
  2247. end;
  2248. inherited destroy;
  2249. end;
  2250. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  2251. begin
  2252. inherited ppuwrite(ppufile);
  2253. ppufile.putbyte(exceptionblock);
  2254. end;
  2255. procedure tlabelnode.buildderefimpl;
  2256. begin
  2257. inherited buildderefimpl;
  2258. end;
  2259. procedure tlabelnode.derefimpl;
  2260. begin
  2261. inherited derefimpl;
  2262. end;
  2263. function tlabelnode.pass_typecheck:tnode;
  2264. begin
  2265. result:=nil;
  2266. resultdef:=voidtype;
  2267. end;
  2268. function tlabelnode.pass_1 : tnode;
  2269. begin
  2270. result:=nil;
  2271. expectloc:=LOC_VOID;
  2272. if not (nf_internal in flags) then
  2273. include(current_procinfo.flags,pi_has_label);
  2274. if (m_non_local_goto in current_settings.modeswitches) and
  2275. { the owner can be Nil for internal labels }
  2276. assigned(labsym.owner) and
  2277. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  2278. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  2279. end;
  2280. function tlabelnode.dogetcopy : tnode;
  2281. begin
  2282. if not(assigned(copiedto)) then
  2283. copiedto:=tlabelnode(inherited dogetcopy);
  2284. copiedto.exceptionblock:=exceptionblock;
  2285. result:=copiedto;
  2286. end;
  2287. function tlabelnode.docompare(p: tnode): boolean;
  2288. begin
  2289. docompare := false;
  2290. end;
  2291. {*****************************************************************************
  2292. TRAISENODE
  2293. *****************************************************************************}
  2294. constructor traisenode.create(l,taddr,tframe:tnode);
  2295. begin
  2296. inherited create(raisen,l,taddr,tframe);
  2297. end;
  2298. function traisenode.pass_typecheck:tnode;
  2299. begin
  2300. result:=nil;
  2301. resultdef:=voidtype;
  2302. if assigned(left) then
  2303. begin
  2304. { first para must be a _class_ }
  2305. typecheckpass(left);
  2306. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2307. if codegenerror then
  2308. exit;
  2309. if not is_class(left.resultdef) and
  2310. not is_javaclass(left.resultdef) then
  2311. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2312. { insert needed typeconvs for addr,frame }
  2313. if assigned(right) then
  2314. begin
  2315. { addr }
  2316. typecheckpass(right);
  2317. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2318. inserttypeconv(right,voidcodepointertype);
  2319. { frame }
  2320. if assigned(third) then
  2321. begin
  2322. typecheckpass(third);
  2323. set_varstate(third,vs_read,[vsf_must_be_valid]);
  2324. inserttypeconv(third,voidpointertype);
  2325. end;
  2326. end;
  2327. end;
  2328. end;
  2329. function traisenode.pass_1 : tnode;
  2330. var
  2331. statements : tstatementnode;
  2332. current_addr : tlabelnode;
  2333. raisenode : tcallnode;
  2334. begin
  2335. result:=internalstatements(statements);
  2336. if assigned(left) then
  2337. begin
  2338. { first para must be a class }
  2339. firstpass(left);
  2340. { insert needed typeconvs for addr,frame }
  2341. if assigned(right) then
  2342. begin
  2343. { addr }
  2344. firstpass(right);
  2345. { frame }
  2346. if assigned(third) then
  2347. firstpass(third)
  2348. else
  2349. third:=cpointerconstnode.Create(0,voidpointertype);
  2350. end
  2351. else
  2352. begin
  2353. third:=cinlinenode.create(in_get_frame,false,nil);
  2354. current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  2355. include(current_addr.flags,nf_internal);
  2356. addstatement(statements,current_addr);
  2357. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  2358. { raise address off by one so we are for sure inside the action area for the raise }
  2359. if tf_use_psabieh in target_info.flags then
  2360. right:=caddnode.create_internal(addn,right,cordconstnode.create(1,sizesinttype,false));
  2361. end;
  2362. raisenode:=ccallnode.createintern('fpc_raiseexception',
  2363. ccallparanode.create(third,
  2364. ccallparanode.create(right,
  2365. ccallparanode.create(left,nil)))
  2366. );
  2367. include(raisenode.callnodeflags,cnf_call_never_returns);
  2368. addstatement(statements,raisenode);
  2369. end
  2370. else
  2371. begin
  2372. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  2373. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  2374. include(raisenode.callnodeflags,cnf_call_never_returns);
  2375. addstatement(statements,raisenode);
  2376. end;
  2377. left:=nil;
  2378. right:=nil;
  2379. third:=nil;
  2380. end;
  2381. {*****************************************************************************
  2382. TTRYEXCEPTNODE
  2383. *****************************************************************************}
  2384. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  2385. begin
  2386. inherited create(tryexceptn,l,r,_t1,nil);
  2387. end;
  2388. function ttryexceptnode.pass_typecheck:tnode;
  2389. begin
  2390. result:=nil;
  2391. typecheckpass(left);
  2392. { on statements }
  2393. if assigned(right) then
  2394. typecheckpass(right);
  2395. { else block }
  2396. if assigned(t1) then
  2397. typecheckpass(t1);
  2398. resultdef:=voidtype;
  2399. end;
  2400. function ttryexceptnode.pass_1 : tnode;
  2401. begin
  2402. result:=nil;
  2403. expectloc:=LOC_VOID;
  2404. firstpass(left);
  2405. { on statements }
  2406. if assigned(right) then
  2407. firstpass(right);
  2408. { else block }
  2409. if assigned(t1) then
  2410. firstpass(t1);
  2411. include(current_procinfo.flags,pi_do_call);
  2412. include(current_procinfo.flags,pi_uses_exceptions);
  2413. adjust_estimated_stack_size;
  2414. end;
  2415. function ttryexceptnode.simplify(forinline: boolean): tnode;
  2416. begin
  2417. result:=nil;
  2418. { empty try -> can never raise exception -> do nothing }
  2419. if has_no_code(left) then
  2420. result:=cnothingnode.create;
  2421. end;
  2422. procedure ttryexceptnode.adjust_estimated_stack_size;
  2423. begin
  2424. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size*2);
  2425. end;
  2426. {*****************************************************************************
  2427. TTRYFINALLYNODE
  2428. *****************************************************************************}
  2429. constructor ttryfinallynode.create(l,r:tnode);
  2430. begin
  2431. inherited create(tryfinallyn,l,r,nil);
  2432. third:=nil;
  2433. implicitframe:=false;
  2434. end;
  2435. constructor ttryfinallynode.create_implicit(l,r:tnode);
  2436. begin
  2437. inherited create(tryfinallyn,l,r,nil);
  2438. third:=nil;
  2439. implicitframe:=true;
  2440. end;
  2441. function ttryfinallynode.pass_typecheck:tnode;
  2442. begin
  2443. result:=nil;
  2444. resultdef:=voidtype;
  2445. typecheckpass(left);
  2446. // "try block" is "used"? (JM)
  2447. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  2448. typecheckpass(right);
  2449. // "except block" is "used"? (JM)
  2450. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  2451. if assigned(third) then
  2452. begin
  2453. typecheckpass(third);
  2454. set_varstate(third,vs_readwritten,[vsf_must_be_valid]);
  2455. end;
  2456. end;
  2457. function ttryfinallynode.pass_1 : tnode;
  2458. begin
  2459. result:=nil;
  2460. expectloc:=LOC_VOID;
  2461. firstpass(left);
  2462. firstpass(right);
  2463. if assigned(third) then
  2464. firstpass(third);
  2465. include(current_procinfo.flags,pi_do_call);
  2466. { pi_uses_exceptions is an information for the optimizer and it
  2467. is only interested in exceptions if they appear inside the body,
  2468. so ignore implicit frames when setting the flag }
  2469. if not(implicitframe) then
  2470. include(current_procinfo.flags,pi_uses_exceptions);
  2471. adjust_estimated_stack_size;
  2472. end;
  2473. function ttryfinallynode.simplify(forinline : boolean): tnode;
  2474. begin
  2475. result:=nil;
  2476. { if the try contains no code, we can kill
  2477. the try and except and return only the
  2478. finally part }
  2479. if has_no_code(left) then
  2480. begin
  2481. result:=right;
  2482. right:=nil;
  2483. end
  2484. { if the finally block contains no code, we can kill
  2485. it and just return the try part }
  2486. else if has_no_code(right) and not(assigned(third)) and not(implicitframe) then
  2487. begin
  2488. result:=left;
  2489. left:=nil;
  2490. end;
  2491. end;
  2492. function ttryfinallynode.dogetcopy: tnode;
  2493. begin
  2494. result:=inherited dogetcopy;
  2495. ttryfinallynode(result).implicitframe:=implicitframe;
  2496. end;
  2497. procedure ttryfinallynode.adjust_estimated_stack_size;
  2498. begin
  2499. inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size);
  2500. end;
  2501. {*****************************************************************************
  2502. TONNODE
  2503. *****************************************************************************}
  2504. constructor tonnode.create(l,r:tnode);
  2505. begin
  2506. inherited create(onn,l,r);
  2507. excepTSymtable:=nil;
  2508. excepttype:=nil;
  2509. end;
  2510. destructor tonnode.destroy;
  2511. begin
  2512. { copied nodes don't need to release the symtable }
  2513. if assigned(excepTSymtable) then
  2514. excepTSymtable.free;
  2515. inherited destroy;
  2516. end;
  2517. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2518. begin
  2519. inherited ppuload(t,ppufile);
  2520. excepTSymtable:=nil;
  2521. excepttype:=nil;
  2522. end;
  2523. function tonnode.dogetcopy : tnode;
  2524. var
  2525. n : tonnode;
  2526. begin
  2527. n:=tonnode(inherited dogetcopy);
  2528. if assigned(exceptsymtable) then
  2529. n.exceptsymtable:=exceptsymtable.getcopy
  2530. else
  2531. n.exceptsymtable:=nil;
  2532. n.excepttype:=excepttype;
  2533. result:=n;
  2534. end;
  2535. function tonnode.pass_typecheck:tnode;
  2536. begin
  2537. result:=nil;
  2538. resultdef:=voidtype;
  2539. if not is_class(excepttype) and
  2540. not is_javaclass(excepttype) then
  2541. CGMessage1(type_e_class_type_expected,excepttype.typename);
  2542. if assigned(left) then
  2543. typecheckpass(left);
  2544. if assigned(right) then
  2545. typecheckpass(right);
  2546. end;
  2547. function tonnode.pass_1 : tnode;
  2548. begin
  2549. result:=nil;
  2550. include(current_procinfo.flags,pi_do_call);
  2551. expectloc:=LOC_VOID;
  2552. if assigned(left) then
  2553. firstpass(left);
  2554. if assigned(right) then
  2555. firstpass(right);
  2556. end;
  2557. function tonnode.docompare(p: tnode): boolean;
  2558. begin
  2559. docompare := false;
  2560. end;
  2561. end.