nflw.pas 99 KB

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