nflw.pas 103 KB

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