nflw.pas 103 KB

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