nflw.pas 107 KB

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