nflw.pas 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  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. 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. tloopflags = set of tloopflag;
  42. const
  43. { loop flags which must match to consider loop nodes equal regarding the flags }
  44. loopflagsequal = [lnf_backward];
  45. type
  46. tlabelnode = class;
  47. tloopnode = class(tbinarynode)
  48. t1,t2 : tnode;
  49. loopflags : tloopflags;
  50. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  51. destructor destroy;override;
  52. function dogetcopy : tnode;override;
  53. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  54. procedure ppuwrite(ppufile:tcompilerppufile);override;
  55. procedure buildderefimpl;override;
  56. procedure derefimpl;override;
  57. procedure insertintolist(l : tnodelist);override;
  58. procedure printnodetree(var t:text);override;
  59. function docompare(p: tnode): boolean; override;
  60. end;
  61. twhilerepeatnode = class(tloopnode)
  62. constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
  63. function pass_typecheck:tnode;override;
  64. function pass_1 : tnode;override;
  65. {$ifdef state_tracking}
  66. function track_state_pass(exec_known:boolean):boolean;override;
  67. {$endif}
  68. end;
  69. twhilerepeatnodeclass = class of twhilerepeatnode;
  70. tifnode = class(tloopnode)
  71. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  72. constructor create_internal(l,r,_t1 : tnode);virtual;reintroduce;
  73. function pass_typecheck:tnode;override;
  74. function pass_1 : tnode;override;
  75. function simplify(forinline : boolean) : tnode;override;
  76. private
  77. function internalsimplify(warn: boolean) : tnode;
  78. end;
  79. tifnodeclass = class of tifnode;
  80. tfornode = class(tloopnode)
  81. { if count isn divisable by unrolls then
  82. the for loop must jump to this label to get the correct
  83. number of executions }
  84. entrylabel,
  85. { this is a dummy node used by the dfa to store life information for the loop iteration }
  86. loopiteration : tnode;
  87. loopvar_notid:cardinal;
  88. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;reintroduce;
  89. function wrap_to_value:tnode;
  90. function pass_typecheck:tnode;override;
  91. function pass_1 : tnode;override;
  92. function simplify(forinline : boolean) : tnode;override;
  93. end;
  94. tfornodeclass = class of tfornode;
  95. texitnode = class(tunarynode)
  96. constructor create(l:tnode);virtual;
  97. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  98. procedure ppuwrite(ppufile:tcompilerppufile);override;
  99. function pass_typecheck:tnode;override;
  100. function pass_1 : tnode;override;
  101. property resultexpr : tnode read left write left;
  102. end;
  103. texitnodeclass = class of texitnode;
  104. tbreaknode = class(tnode)
  105. constructor create;virtual;
  106. function pass_typecheck:tnode;override;
  107. function pass_1 : tnode;override;
  108. end;
  109. tbreaknodeclass = class of tbreaknode;
  110. tcontinuenode = class(tnode)
  111. constructor create;virtual;
  112. function pass_typecheck:tnode;override;
  113. function pass_1 : tnode;override;
  114. end;
  115. tcontinuenodeclass = class of tcontinuenode;
  116. tgotonode = class(tnode)
  117. private
  118. labelnodeidx : longint;
  119. public
  120. labelsym : tlabelsym;
  121. labelnode : tlabelnode;
  122. exceptionblock : integer;
  123. constructor create(p : tlabelsym);virtual;
  124. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  125. procedure ppuwrite(ppufile:tcompilerppufile);override;
  126. procedure buildderefimpl;override;
  127. procedure derefimpl;override;
  128. procedure resolveppuidx;override;
  129. function dogetcopy : tnode;override;
  130. function pass_typecheck:tnode;override;
  131. function pass_1 : tnode;override;
  132. function docompare(p: tnode): boolean; override;
  133. end;
  134. tgotonodeclass = class of tgotonode;
  135. tlabelnode = class(tunarynode)
  136. exceptionblock : integer;
  137. { when copying trees, this points to the newly created copy of a label }
  138. copiedto : tlabelnode;
  139. labsym : tlabelsym;
  140. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  141. destructor destroy;override;
  142. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  143. procedure ppuwrite(ppufile:tcompilerppufile);override;
  144. procedure buildderefimpl;override;
  145. procedure derefimpl;override;
  146. function dogetcopy : tnode;override;
  147. function pass_typecheck:tnode;override;
  148. function pass_1 : tnode;override;
  149. function docompare(p: tnode): boolean; override;
  150. end;
  151. tlabelnodeclass = class of tlabelnode;
  152. traisenode = class(ttertiarynode)
  153. constructor create(l,taddr,tframe:tnode);virtual;
  154. function pass_typecheck:tnode;override;
  155. function pass_1 : tnode;override;
  156. end;
  157. traisenodeclass = class of traisenode;
  158. ttryexceptnode = class(tloopnode)
  159. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  160. function pass_typecheck:tnode;override;
  161. function pass_1 : tnode;override;
  162. function simplify(forinline: boolean): tnode; override;
  163. end;
  164. ttryexceptnodeclass = class of ttryexceptnode;
  165. ttryfinallynode = class(tloopnode)
  166. implicitframe : boolean;
  167. constructor create(l,r:tnode);virtual;reintroduce;
  168. constructor create_implicit(l,r,_t1:tnode);virtual;
  169. function pass_typecheck:tnode;override;
  170. function pass_1 : tnode;override;
  171. function simplify(forinline:boolean): tnode;override;
  172. protected
  173. function create_finalizer_procdef: tprocdef;
  174. end;
  175. ttryfinallynodeclass = class of ttryfinallynode;
  176. tonnode = class(tbinarynode)
  177. excepTSymtable : TSymtable;
  178. excepttype : tobjectdef;
  179. constructor create(l,r:tnode);virtual;
  180. destructor destroy;override;
  181. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  182. function pass_typecheck:tnode;override;
  183. function pass_1 : tnode;override;
  184. function dogetcopy : tnode;override;
  185. function docompare(p: tnode): boolean; override;
  186. end;
  187. tonnodeclass = class of tonnode;
  188. var
  189. cwhilerepeatnode : twhilerepeatnodeclass=twhilerepeatnode;
  190. cifnode : tifnodeclass = tifnode;
  191. cfornode : tfornodeclass = tfornode;
  192. cexitnode : texitnodeclass = texitnode;
  193. cgotonode : tgotonodeclass = tgotonode;
  194. clabelnode : tlabelnodeclass = tlabelnode;
  195. craisenode : traisenodeclass = traisenode;
  196. ctryexceptnode : ttryexceptnodeclass = ttryexceptnode;
  197. ctryfinallynode : ttryfinallynodeclass = ttryfinallynode;
  198. connode : tonnodeclass = tonnode;
  199. cbreaknode : tbreaknodeclass = tbreaknode;
  200. ccontinuenode : tcontinuenodeclass = tcontinuenode;
  201. // for-in loop helpers
  202. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  203. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  204. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  205. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  206. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  207. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  208. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  209. implementation
  210. uses
  211. globtype,systems,constexp,
  212. cutils,verbose,globals,
  213. symconst,symtable,paramgr,defcmp,defutil,htypechk,pass_1,
  214. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,nutils,ninl,nset,
  215. pdecsub,
  216. {$ifdef state_tracking}
  217. nstate,
  218. {$endif}
  219. {$ifdef i8086}
  220. cpuinfo,
  221. {$endif i8086}
  222. cgbase,procinfo
  223. ;
  224. // for-in loop helpers
  225. function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  226. begin
  227. result:=cfornode.create(hloopvar,
  228. cinlinenode.create(in_low_x,false,expr.getcopy),
  229. cinlinenode.create(in_high_x,false,expr.getcopy),
  230. hloopbody,
  231. false);
  232. end;
  233. function create_objc_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  234. var
  235. mainstatement, outerloopbodystatement, innerloopbodystatement, tempstatement: tstatementnode;
  236. state, mutationcheck, currentamount, innerloopcounter, items, expressiontemp: ttempcreatenode;
  237. outerloop, innerloop, hp: tnode;
  238. itemsarraydef: tarraydef;
  239. sym: tsym;
  240. begin
  241. { Objective-C enumerators require Objective-C 2.0 }
  242. if not(m_objectivec2 in current_settings.modeswitches) then
  243. begin
  244. result:=cerrornode.create;
  245. MessagePos(expr.fileinfo,parser_e_objc_enumerator_2_0);
  246. exit;
  247. end;
  248. { Requires the NSFastEnumeration protocol and NSFastEnumerationState
  249. record }
  250. maybeloadcocoatypes;
  251. if not assigned(objc_fastenumeration) or
  252. not assigned(objc_fastenumerationstate) then
  253. begin
  254. result:=cerrornode.create;
  255. MessagePos(expr.fileinfo,parser_e_objc_missing_enumeration_defs);
  256. exit;
  257. end;
  258. (* Original code:
  259. for hloopvar in expression do
  260. <hloopbody>
  261. Pascal code equivalent into which it has to be transformed
  262. (sure would be nice if the compiler had some kind of templates ;) :
  263. var
  264. state: NSFastEnumerationState;
  265. expressiontemp: NSFastEnumerationProtocol;
  266. mutationcheck,
  267. currentamount,
  268. innerloopcounter: culong;
  269. { size can be increased/decreased if desired }
  270. items: array[1..16] of id;
  271. begin
  272. fillchar(state,sizeof(state),0);
  273. expressiontemp:=expression;
  274. repeat
  275. currentamount:=expressiontemp.countByEnumeratingWithState_objects_count(@state,@items,length(items));
  276. if currentamount=0 then
  277. begin
  278. { "The iterating variable is set to nil when the loop ends by
  279. exhausting the source pool of objects" }
  280. hloopvar:=nil;
  281. break;
  282. end;
  283. mutationcheck:=state.mutationsptr^;
  284. innerloopcounter:=culong(-1);
  285. repeat
  286. { at the start so that "continue" in <loopbody> works correctly }
  287. { don't use for-loop, because then the value of the iteration
  288. counter is undefined on exit and we have to check it in the
  289. outer repeat/until condition }
  290. {$push}
  291. {$r-,q-}
  292. inc(innerloopcounter);
  293. {$pop}
  294. if innerloopcounter=currentamount then
  295. break;
  296. if mutationcheck<>state.mutationsptr^ then
  297. { raises Objective-C exception... }
  298. objc_enumerationMutation(expressiontemp);
  299. hloopvar:=state.itemsPtr[innerloopcounter];
  300. { if continue in loopbody -> jumps to start, increases count and checks }
  301. { if break in loopbody: goes to outer repeat/until and innerloopcount
  302. will be < currentamount -> stops }
  303. <hloopbody>
  304. until false;
  305. { if the inner loop terminated early, "break" was used and we have
  306. to stop }
  307. { "If the loop is terminated early, the iterating variable is left
  308. pointing to the last iteration item." }
  309. until innerloopcounter<currentamount;
  310. end;
  311. *)
  312. result:=internalstatements(mainstatement);
  313. { the fast enumeration state }
  314. state:=ctempcreatenode.create(objc_fastenumerationstate,objc_fastenumerationstate.size,tt_persistent,false);
  315. typecheckpass(tnode(state));
  316. addstatement(mainstatement,state);
  317. { the temporary items array }
  318. itemsarraydef:=carraydef.create(1,16,u32inttype);
  319. itemsarraydef.elementdef:=objc_idtype;
  320. items:=ctempcreatenode.create(itemsarraydef,itemsarraydef.size,tt_persistent,false);
  321. addstatement(mainstatement,items);
  322. typecheckpass(tnode(items));
  323. { temp for the expression/collection through which we iterate }
  324. expressiontemp:=ctempcreatenode.create(objc_fastenumeration,objc_fastenumeration.size,tt_persistent,true);
  325. addstatement(mainstatement,expressiontemp);
  326. { currentamount temp (not really clean: we use ptruint instead of
  327. culong) }
  328. currentamount:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  329. typecheckpass(tnode(currentamount));
  330. addstatement(mainstatement,currentamount);
  331. { mutationcheck temp (idem) }
  332. mutationcheck:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  333. typecheckpass(tnode(mutationcheck));
  334. addstatement(mainstatement,mutationcheck);
  335. { innerloopcounter temp (idem) }
  336. innerloopcounter:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
  337. typecheckpass(tnode(innerloopcounter));
  338. addstatement(mainstatement,innerloopcounter);
  339. { initialise the state with 0 }
  340. addstatement(mainstatement,ccallnode.createinternfromunit('SYSTEM','FILLCHAR',
  341. ccallparanode.create(genintconstnode(0),
  342. ccallparanode.create(genintconstnode(objc_fastenumerationstate.size),
  343. ccallparanode.create(ctemprefnode.create(state),nil)
  344. )
  345. )
  346. ));
  347. { this will also check whether the expression (potentially) conforms
  348. to the NSFastEnumeration protocol (use expr.getcopy, because the
  349. caller will free expr) }
  350. addstatement(mainstatement,cassignmentnode.create(ctemprefnode.create(expressiontemp),expr.getcopy));
  351. { we add the "repeat..until" afterwards, now just create the body }
  352. outerloop:=internalstatements(outerloopbodystatement);
  353. { the countByEnumeratingWithState_objects_count call }
  354. hp:=ccallparanode.create(cinlinenode.create(in_length_x,false,ctypenode.create(itemsarraydef)),
  355. ccallparanode.create(caddrnode.create(ctemprefnode.create(items)),
  356. ccallparanode.create(caddrnode.create(ctemprefnode.create(state)),nil)
  357. )
  358. );
  359. sym:=search_struct_member(objc_fastenumeration,'COUNTBYENUMERATINGWITHSTATE_OBJECTS_COUNT');
  360. if not assigned(sym) or
  361. (sym.typ<>procsym) then
  362. internalerror(2010061901);
  363. hp:=ccallnode.create(hp,tprocsym(sym),sym.owner,ctemprefnode.create(expressiontemp),[]);
  364. addstatement(outerloopbodystatement,cassignmentnode.create(
  365. ctemprefnode.create(currentamount),hp));
  366. { if currentamount = 0, bail out (use copy of hloopvar, because we
  367. have to use it again below) }
  368. hp:=internalstatements(tempstatement);
  369. addstatement(tempstatement,cassignmentnode.create(
  370. hloopvar.getcopy,cnilnode.create));
  371. addstatement(tempstatement,cbreaknode.create);
  372. addstatement(outerloopbodystatement,cifnode.create(
  373. caddnode.create(equaln,ctemprefnode.create(currentamount),genintconstnode(0)),
  374. hp,nil));
  375. { initial value of mutationcheck }
  376. hp:=ctemprefnode.create(state);
  377. typecheckpass(hp);
  378. hp:=cderefnode.create(genloadfield(hp,'MUTATIONSPTR'));
  379. addstatement(outerloopbodystatement,cassignmentnode.create(
  380. ctemprefnode.create(mutationcheck),hp));
  381. { initialise innerloopcounter }
  382. addstatement(outerloopbodystatement,cassignmentnode.create(
  383. ctemprefnode.create(innerloopcounter),cordconstnode.create(-1,ptruinttype,false)));
  384. { and now the inner loop, again adding the repeat/until afterwards }
  385. innerloop:=internalstatements(innerloopbodystatement);
  386. { inc(innerloopcounter) without range/overflowchecking (because
  387. we go from culong(-1) to 0 during the first iteration }
  388. hp:=cinlinenode.create(
  389. in_inc_x,false,ccallparanode.create(
  390. ctemprefnode.create(innerloopcounter),nil));
  391. hp.localswitches:=hp.localswitches-[cs_check_range,cs_check_overflow];
  392. addstatement(innerloopbodystatement,hp);
  393. { if innerloopcounter=currentamount then break to the outer loop }
  394. addstatement(innerloopbodystatement,cifnode.create(
  395. caddnode.create(equaln,
  396. ctemprefnode.create(innerloopcounter),
  397. ctemprefnode.create(currentamount)),
  398. cbreaknode.create,
  399. nil));
  400. { verify that the collection didn't change in the mean time }
  401. hp:=ctemprefnode.create(state);
  402. typecheckpass(hp);
  403. addstatement(innerloopbodystatement,cifnode.create(
  404. caddnode.create(unequaln,
  405. ctemprefnode.create(mutationcheck),
  406. cderefnode.create(genloadfield(hp,'MUTATIONSPTR'))
  407. ),
  408. ccallnode.createinternfromunit('OBJC','OBJC_ENUMERATIONMUTATION',
  409. ccallparanode.create(ctemprefnode.create(expressiontemp),nil)),
  410. nil));
  411. { finally: actually get the next element }
  412. hp:=ctemprefnode.create(state);
  413. typecheckpass(hp);
  414. hp:=genloadfield(hp,'ITEMSPTR');
  415. typecheckpass(hp);
  416. { don't simply use a vecn, because indexing a pointer won't work in
  417. non-FPC modes }
  418. if hp.resultdef.typ<>pointerdef then
  419. internalerror(2010061904);
  420. inserttypeconv(hp,
  421. carraydef.create_from_pointer(tpointerdef(hp.resultdef)));
  422. hp:=cvecnode.create(hp,ctemprefnode.create(innerloopcounter));
  423. addstatement(innerloopbodystatement,
  424. cassignmentnode.create(hloopvar,hp));
  425. { the actual loop body! }
  426. addstatement(innerloopbodystatement,hloopbody);
  427. { create the inner repeat/until and add it to the body of the outer
  428. one }
  429. hp:=cwhilerepeatnode.create(
  430. { repeat .. until false }
  431. cordconstnode.create(0,pasbool8type,false),innerloop,false,true);
  432. addstatement(outerloopbodystatement,hp);
  433. { create the outer repeat/until and add it to the the main body }
  434. hp:=cwhilerepeatnode.create(
  435. { repeat .. until innerloopcounter<currentamount }
  436. caddnode.create(ltn,
  437. ctemprefnode.create(innerloopcounter),
  438. ctemprefnode.create(currentamount)),
  439. outerloop,false,true);
  440. addstatement(mainstatement,hp);
  441. { release the temps }
  442. addstatement(mainstatement,ctempdeletenode.create(state));
  443. addstatement(mainstatement,ctempdeletenode.create(mutationcheck));
  444. addstatement(mainstatement,ctempdeletenode.create(currentamount));
  445. addstatement(mainstatement,ctempdeletenode.create(innerloopcounter));
  446. addstatement(mainstatement,ctempdeletenode.create(items));
  447. addstatement(mainstatement,ctempdeletenode.create(expressiontemp));
  448. end;
  449. function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  450. var
  451. loopstatement, loopbodystatement: tstatementnode;
  452. loopvar, stringvar: ttempcreatenode;
  453. stringindex, loopbody, forloopnode: tnode;
  454. begin
  455. { result is a block of statements }
  456. result:=internalstatements(loopstatement);
  457. { create a temp variable for expression }
  458. stringvar := ctempcreatenode.create(
  459. expr.resultdef,
  460. expr.resultdef.size,
  461. tt_persistent,true);
  462. addstatement(loopstatement,stringvar);
  463. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(stringvar),expr.getcopy));
  464. { create a loop counter: signed integer with size of string length }
  465. loopvar := ctempcreatenode.create(
  466. sinttype,
  467. sinttype.size,
  468. tt_persistent,true);
  469. addstatement(loopstatement,loopvar);
  470. stringindex:=ctemprefnode.create(loopvar);
  471. loopbody:=internalstatements(loopbodystatement);
  472. // for-in loop variable := string_expression[index]
  473. addstatement(loopbodystatement,
  474. cassignmentnode.create(hloopvar, cvecnode.create(ctemprefnode.create(stringvar),stringindex)));
  475. { add the actual statement to the loop }
  476. addstatement(loopbodystatement,hloopbody);
  477. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  478. genintconstnode(1),
  479. cinlinenode.create(in_length_x,false,ctemprefnode.create(stringvar)),
  480. loopbody,
  481. false);
  482. addstatement(loopstatement,forloopnode);
  483. { free the loop counter }
  484. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  485. { free the temp variable for expression }
  486. addstatement(loopstatement,ctempdeletenode.create(stringvar));
  487. end;
  488. function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  489. var
  490. loopstatement, loopbodystatement: tstatementnode;
  491. loopvar, arrayvar: ttempcreatenode;
  492. arrayindex, lowbound, highbound, loopbody, forloopnode, expression: tnode;
  493. is_string: boolean;
  494. tmpdef, convertdef: tdef;
  495. elementcount: aword;
  496. begin
  497. expression := expr;
  498. { result is a block of statements }
  499. result:=internalstatements(loopstatement);
  500. is_string:=ado_IsConstString in tarraydef(expr.resultdef).arrayoptions;
  501. // if array element type <> loovar type then create a conversion if possible
  502. if compare_defs(tarraydef(expression.resultdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible then
  503. begin
  504. tmpdef:=expression.resultdef;
  505. elementcount:=1;
  506. while assigned(tmpdef) and (tmpdef.typ=arraydef) and
  507. (tarraydef(tmpdef).arrayoptions = []) and
  508. (compare_defs(tarraydef(tmpdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible) do
  509. begin
  510. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  511. tmpdef:=tarraydef(tmpdef).elementdef;
  512. end;
  513. if assigned(tmpdef) and (tmpdef.typ=arraydef) and (tarraydef(tmpdef).arrayoptions = []) then
  514. begin
  515. elementcount:=elementcount*tarraydef(tmpdef).elecount;
  516. convertdef:=carraydef.create(0,elementcount-1,s32inttype);
  517. tarraydef(convertdef).elementdef:=tarraydef(tmpdef).elementdef;
  518. expression:=expr.getcopy;
  519. expression:=ctypeconvnode.create_internal(expression,convertdef);
  520. typecheckpass(expression);
  521. addstatement(loopstatement,expression);
  522. end;
  523. end;
  524. if (node_complexity(expression) > 1) and
  525. not(is_open_array(expression.resultdef)) and not(is_array_of_const(expression.resultdef)) then
  526. begin
  527. { create a temp variable for expression }
  528. arrayvar := ctempcreatenode.create(
  529. expression.resultdef,
  530. expression.resultdef.size,
  531. tt_persistent,true);
  532. if is_string then
  533. begin
  534. lowbound:=genintconstnode(1);
  535. highbound:=cinlinenode.create(in_length_x,false,ctemprefnode.create(arrayvar))
  536. end
  537. else
  538. begin
  539. lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
  540. highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
  541. end;
  542. addstatement(loopstatement,arrayvar);
  543. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(arrayvar),expression.getcopy));
  544. end
  545. else
  546. begin
  547. arrayvar:=nil;
  548. if is_string then
  549. begin
  550. lowbound:=genintconstnode(1);
  551. highbound:=cinlinenode.create(in_length_x,false,expression.getcopy);
  552. end
  553. else
  554. begin
  555. lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
  556. highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
  557. end;
  558. end;
  559. { create a loop counter }
  560. loopvar := ctempcreatenode.create(
  561. tarraydef(expression.resultdef).rangedef,
  562. tarraydef(expression.resultdef).rangedef.size,
  563. tt_persistent,true);
  564. addstatement(loopstatement,loopvar);
  565. arrayindex:=ctemprefnode.create(loopvar);
  566. loopbody:=internalstatements(loopbodystatement);
  567. // for-in loop variable := array_expression[index]
  568. if assigned(arrayvar) then
  569. addstatement(loopbodystatement,
  570. cassignmentnode.create(hloopvar,cvecnode.create(ctemprefnode.create(arrayvar),arrayindex)))
  571. else
  572. addstatement(loopbodystatement,
  573. cassignmentnode.create(hloopvar,cvecnode.create(expression.getcopy,arrayindex)));
  574. { add the actual statement to the loop }
  575. addstatement(loopbodystatement,hloopbody);
  576. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  577. lowbound,
  578. highbound,
  579. loopbody,
  580. false);
  581. addstatement(loopstatement,forloopnode);
  582. { free the loop counter }
  583. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  584. { free the temp variable for expression if needed }
  585. if arrayvar<>nil then
  586. addstatement(loopstatement,ctempdeletenode.create(arrayvar));
  587. end;
  588. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  589. var
  590. loopstatement, loopbodystatement: tstatementnode;
  591. loopvar, setvar: ttempcreatenode;
  592. loopbody, forloopnode: tnode;
  593. begin
  594. // first check is set is empty and if it so then skip other processing
  595. if not Assigned(tsetdef(expr.resultdef).elementdef) then
  596. begin
  597. result:=cnothingnode.create;
  598. // free unused nodes
  599. hloopvar.free;
  600. hloopbody.free;
  601. exit;
  602. end;
  603. { result is a block of statements }
  604. result:=internalstatements(loopstatement);
  605. { create a temp variable for expression }
  606. setvar := ctempcreatenode.create(
  607. expr.resultdef,
  608. expr.resultdef.size,
  609. tt_persistent,true);
  610. addstatement(loopstatement,setvar);
  611. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(setvar),expr.getcopy));
  612. { create a loop counter }
  613. loopvar := ctempcreatenode.create(
  614. tsetdef(expr.resultdef).elementdef,
  615. tsetdef(expr.resultdef).elementdef.size,
  616. tt_persistent,true);
  617. addstatement(loopstatement,loopvar);
  618. // if loopvar in set then
  619. // begin
  620. // hloopvar := loopvar
  621. // for-in loop body
  622. // end
  623. loopbody:=cifnode.create(
  624. cinnode.create(ctemprefnode.create(loopvar),ctemprefnode.create(setvar)),
  625. internalstatements(loopbodystatement),
  626. nil);
  627. addstatement(loopbodystatement,cassignmentnode.create(hloopvar,ctemprefnode.create(loopvar)));
  628. { add the actual statement to the loop }
  629. addstatement(loopbodystatement,hloopbody);
  630. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  631. cinlinenode.create(in_low_x,false,ctemprefnode.create(setvar)),
  632. cinlinenode.create(in_high_x,false,ctemprefnode.create(setvar)),
  633. loopbody,
  634. false);
  635. addstatement(loopstatement,forloopnode);
  636. { free the loop counter }
  637. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  638. { free the temp variable for expression }
  639. addstatement(loopstatement,ctempdeletenode.create(setvar));
  640. end;
  641. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  642. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  643. var
  644. loopstatement, loopbodystatement: tstatementnode;
  645. enumvar: ttempcreatenode;
  646. loopbody, whileloopnode,
  647. enum_get, enum_move, enum_current, enum_get_params: tnode;
  648. propaccesslist: tpropaccesslist;
  649. enumerator_is_class: boolean;
  650. enumerator_destructor: tprocdef;
  651. begin
  652. { result is a block of statements }
  653. result:=internalstatements(loopstatement);
  654. enumerator_is_class := is_class(enumerator_get.returndef);
  655. { create a temp variable for enumerator }
  656. enumvar := ctempcreatenode.create(
  657. enumerator_get.returndef,
  658. enumerator_get.returndef.size,
  659. tt_persistent,true);
  660. addstatement(loopstatement,enumvar);
  661. if enumerator_get.proctypeoption=potype_operator then
  662. begin
  663. enum_get_params:=ccallparanode.create(expr.getcopy,nil);
  664. enum_get:=ccallnode.create(enum_get_params, tprocsym(enumerator_get.procsym), nil, nil, []);
  665. tcallnode(enum_get).procdefinition:=enumerator_get;
  666. addsymref(enumerator_get.procsym);
  667. end
  668. else
  669. enum_get:=ccallnode.create(nil, tprocsym(enumerator_get.procsym), enumerator_get.owner, expr.getcopy, []);
  670. addstatement(loopstatement,
  671. cassignmentnode.create(
  672. ctemprefnode.create(enumvar),
  673. enum_get
  674. ));
  675. loopbody:=internalstatements(loopbodystatement);
  676. { for-in loop variable := enumerator.current }
  677. if enumerator_current.getpropaccesslist(palt_read,propaccesslist) then
  678. begin
  679. case propaccesslist.firstsym^.sym.typ of
  680. fieldvarsym :
  681. begin
  682. { generate access code }
  683. enum_current:=ctemprefnode.create(enumvar);
  684. propaccesslist_to_node(enum_current,enumerator_current.owner,propaccesslist);
  685. include(enum_current.flags,nf_isproperty);
  686. end;
  687. procsym :
  688. begin
  689. { generate the method call }
  690. enum_current:=ccallnode.create(nil,tprocsym(propaccesslist.firstsym^.sym),enumerator_current.owner,ctemprefnode.create(enumvar),[]);
  691. include(enum_current.flags,nf_isproperty);
  692. end
  693. else
  694. begin
  695. enum_current:=cerrornode.create;
  696. Message(type_e_mismatch);
  697. end;
  698. end;
  699. end
  700. else
  701. enum_current:=cerrornode.create;
  702. addstatement(loopbodystatement,
  703. cassignmentnode.create(hloopvar, enum_current));
  704. { add the actual statement to the loop }
  705. addstatement(loopbodystatement,hloopbody);
  706. enum_move:=ccallnode.create(nil, tprocsym(enumerator_move.procsym), enumerator_move.owner, ctemprefnode.create(enumvar), []);
  707. whileloopnode:=cwhilerepeatnode.create(enum_move,loopbody,true,false);
  708. if enumerator_is_class then
  709. begin
  710. { insert a try-finally and call the destructor for the enumerator in the finally section }
  711. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  712. if assigned(enumerator_destructor) then
  713. begin
  714. whileloopnode:=ctryfinallynode.create(
  715. whileloopnode, // try node
  716. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym), // finally node
  717. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[]));
  718. end;
  719. { if getenumerator <> nil then do the loop }
  720. whileloopnode:=cifnode.create(
  721. caddnode.create(unequaln, ctemprefnode.create(enumvar), cnilnode.create),
  722. whileloopnode,
  723. nil);
  724. end;
  725. addstatement(loopstatement, whileloopnode);
  726. if is_object(enumerator_get.returndef) then
  727. begin
  728. // call the object destructor too
  729. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  730. if assigned(enumerator_destructor) then
  731. begin
  732. addstatement(loopstatement,
  733. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym),
  734. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[]));
  735. end;
  736. end;
  737. { free the temp variable for enumerator }
  738. addstatement(loopstatement,ctempdeletenode.create(enumvar));
  739. end;
  740. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  741. var
  742. pd, movenext: tprocdef;
  743. helperdef: tobjectdef;
  744. current: tpropertysym;
  745. storefilepos: tfileposinfo;
  746. begin
  747. storefilepos:=current_filepos;
  748. current_filepos:=hloopvar.fileinfo;
  749. if expr.nodetype=typen then
  750. begin
  751. if (expr.resultdef.typ=enumdef) and tenumdef(expr.resultdef).has_jumps then
  752. begin
  753. result:=cerrornode.create;
  754. hloopvar.free;
  755. hloopbody.free;
  756. MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
  757. end
  758. else
  759. result:=create_type_for_in_loop(hloopvar, hloopbody, expr);
  760. end
  761. else
  762. begin
  763. { loop is made for an expression }
  764. // Objective-C uses different conventions (and it's only supported for Objective-C 2.0)
  765. if is_objc_class_or_protocol(hloopvar.resultdef) or
  766. is_objc_class_or_protocol(expr.resultdef) then
  767. begin
  768. result:=create_objc_for_in_loop(hloopvar,hloopbody,expr);
  769. if result.nodetype=errorn then
  770. begin
  771. hloopvar.free;
  772. hloopbody.free;
  773. end;
  774. end
  775. else
  776. begin
  777. // search for operator first
  778. pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
  779. // if there is no operator then search for class/object enumerator method
  780. if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
  781. begin
  782. { first search using the helper hierarchy }
  783. if search_last_objectpascal_helper(tabstractrecorddef(expr.resultdef),nil,helperdef) then
  784. repeat
  785. pd:=helperdef.search_enumerator_get;
  786. helperdef:=helperdef.childof;
  787. until (pd<>nil) or (helperdef=nil);
  788. { we didn't find an enumerator in a helper, so search in the
  789. class/record/object itself }
  790. if pd=nil then
  791. pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
  792. end;
  793. if pd<>nil then
  794. begin
  795. // seach movenext and current symbols
  796. movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
  797. if movenext = nil then
  798. begin
  799. result:=cerrornode.create;
  800. hloopvar.free;
  801. hloopbody.free;
  802. MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
  803. end
  804. else
  805. begin
  806. current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  807. if current = nil then
  808. begin
  809. result:=cerrornode.create;
  810. hloopvar.free;
  811. hloopbody.free;
  812. MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
  813. end
  814. else
  815. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  816. end;
  817. end
  818. else
  819. begin
  820. case expr.resultdef.typ of
  821. stringdef: result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  822. arraydef: result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  823. setdef: result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  824. else
  825. begin
  826. result:=cerrornode.create;
  827. hloopvar.free;
  828. hloopbody.free;
  829. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  830. end;
  831. end;
  832. end;
  833. end;
  834. end;
  835. current_filepos:=storefilepos;
  836. end;
  837. {****************************************************************************
  838. TLOOPNODE
  839. *****************************************************************************}
  840. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  841. begin
  842. inherited create(tt,l,r);
  843. t1:=_t1;
  844. t2:=_t2;
  845. fileinfo:=l.fileinfo;
  846. end;
  847. destructor tloopnode.destroy;
  848. begin
  849. t1.free;
  850. t2.free;
  851. inherited destroy;
  852. end;
  853. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  854. begin
  855. inherited ppuload(t,ppufile);
  856. t1:=ppuloadnode(ppufile);
  857. t2:=ppuloadnode(ppufile);
  858. ppufile.getsmallset(loopflags);
  859. end;
  860. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  861. begin
  862. inherited ppuwrite(ppufile);
  863. ppuwritenode(ppufile,t1);
  864. ppuwritenode(ppufile,t2);
  865. ppufile.putsmallset(loopflags);
  866. end;
  867. procedure tloopnode.buildderefimpl;
  868. begin
  869. inherited buildderefimpl;
  870. if assigned(t1) then
  871. t1.buildderefimpl;
  872. if assigned(t2) then
  873. t2.buildderefimpl;
  874. end;
  875. procedure tloopnode.derefimpl;
  876. begin
  877. inherited derefimpl;
  878. if assigned(t1) then
  879. t1.derefimpl;
  880. if assigned(t2) then
  881. t2.derefimpl;
  882. end;
  883. function tloopnode.dogetcopy : tnode;
  884. var
  885. p : tloopnode;
  886. begin
  887. p:=tloopnode(inherited dogetcopy);
  888. if assigned(t1) then
  889. p.t1:=t1.dogetcopy
  890. else
  891. p.t1:=nil;
  892. if assigned(t2) then
  893. p.t2:=t2.dogetcopy
  894. else
  895. p.t2:=nil;
  896. p.loopflags:=loopflags;
  897. dogetcopy:=p;
  898. end;
  899. procedure tloopnode.insertintolist(l : tnodelist);
  900. begin
  901. end;
  902. procedure tloopnode.printnodetree(var t:text);
  903. begin
  904. write(t,printnodeindention,'(');
  905. printnodeindent;
  906. printnodeinfo(t);
  907. writeln(t);
  908. printnode(t,left);
  909. printnode(t,right);
  910. printnode(t,t1);
  911. printnode(t,t2);
  912. printnodeunindent;
  913. writeln(t,printnodeindention,')');
  914. end;
  915. function tloopnode.docompare(p: tnode): boolean;
  916. begin
  917. docompare :=
  918. inherited docompare(p) and
  919. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  920. t1.isequal(tloopnode(p).t1) and
  921. t2.isequal(tloopnode(p).t2);
  922. end;
  923. {****************************************************************************
  924. TWHILEREPEATNODE
  925. *****************************************************************************}
  926. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  927. begin
  928. inherited create(whilerepeatn,l,r,nil,nil);
  929. if tab then
  930. include(loopflags, lnf_testatbegin);
  931. if cn then
  932. include(loopflags,lnf_checknegate);
  933. end;
  934. function twhilerepeatnode.pass_typecheck:tnode;
  935. var
  936. t:Tunarynode;
  937. begin
  938. result:=nil;
  939. resultdef:=voidtype;
  940. typecheckpass(left);
  941. { tp procvar support }
  942. maybe_call_procvar(left,true);
  943. {A not node can be removed.}
  944. if left.nodetype=notn then
  945. begin
  946. t:=Tunarynode(left);
  947. left:=Tunarynode(left).left;
  948. t.left:=nil;
  949. t.destroy;
  950. {Symdif operator, in case you are wondering:}
  951. loopflags:=loopflags >< [lnf_checknegate];
  952. end;
  953. { loop instruction }
  954. if assigned(right) then
  955. typecheckpass(right);
  956. set_varstate(left,vs_read,[vsf_must_be_valid]);
  957. if codegenerror then
  958. exit;
  959. if not(is_boolean(left.resultdef)) and
  960. not(is_typeparam(left.resultdef)) then
  961. inserttypeconv(left,pasbool8type);
  962. { Give warnings for code that will never be executed for
  963. while false do }
  964. if (lnf_testatbegin in loopflags) and
  965. (left.nodetype=ordconstn) and
  966. (tordconstnode(left).value.uvalue=0) and
  967. assigned(right) then
  968. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  969. end;
  970. {$ifdef prefetchnext}
  971. type
  972. passignmentquery = ^tassignmentquery;
  973. tassignmentquery = record
  974. towhat: tnode;
  975. source: tassignmentnode;
  976. statementcount: cardinal;
  977. end;
  978. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  979. var
  980. query: passignmentquery absolute arg;
  981. temp, prederef: tnode;
  982. begin
  983. result := fen_norecurse_false;
  984. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  985. inc(query^.statementcount);
  986. { make sure there's something else in the loop besides going to the }
  987. { next item }
  988. if (query^.statementcount > 1) and
  989. (n.nodetype = assignn) then
  990. begin
  991. { skip type conversions of assignment target }
  992. temp := tassignmentnode(n).left;
  993. while (temp.nodetype = typeconvn) do
  994. temp := ttypeconvnode(temp).left;
  995. { assignment to x of the while assigned(x) check? }
  996. if not(temp.isequal(query^.towhat)) then
  997. exit;
  998. { right hand side of assignment dereferenced field of }
  999. { x? (no derefn in case of class) }
  1000. temp := tassignmentnode(n).right;
  1001. while (temp.nodetype = typeconvn) do
  1002. temp := ttypeconvnode(temp).left;
  1003. if (temp.nodetype <> subscriptn) then
  1004. exit;
  1005. prederef := tsubscriptnode(temp).left;
  1006. temp := prederef;
  1007. while (temp.nodetype = typeconvn) do
  1008. temp := ttypeconvnode(temp).left;
  1009. { see tests/test/prefetch1.pp }
  1010. if (temp.nodetype = derefn) then
  1011. temp := tderefnode(temp).left
  1012. else
  1013. temp := prederef;
  1014. if temp.isequal(query^.towhat) then
  1015. begin
  1016. query^.source := tassignmentnode(n);
  1017. result := fen_norecurse_true;
  1018. end
  1019. end
  1020. { don't check nodes which can't contain an assignment or whose }
  1021. { final assignment can vary a lot }
  1022. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1023. result := fen_false;
  1024. end;
  1025. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1026. var
  1027. query: tassignmentquery;
  1028. begin
  1029. query.towhat := towhat;
  1030. query.source := nil;
  1031. query.statementcount := 0;
  1032. if foreachnodestatic(where,@checkassignment,@query) then
  1033. result := query.source
  1034. else
  1035. result := nil;
  1036. end;
  1037. {$endif prefetchnext}
  1038. function twhilerepeatnode.pass_1 : tnode;
  1039. {$ifdef prefetchnext}
  1040. var
  1041. runnernode, prefetchcode: tnode;
  1042. assignmentnode: tassignmentnode;
  1043. prefetchstatements: tstatementnode;
  1044. {$endif prefetchnext}
  1045. begin
  1046. result:=nil;
  1047. expectloc:=LOC_VOID;
  1048. firstpass(left);
  1049. if codegenerror then
  1050. exit;
  1051. { loop instruction }
  1052. if assigned(right) then
  1053. begin
  1054. firstpass(right);
  1055. if codegenerror then
  1056. exit;
  1057. end;
  1058. {$ifdef prefetchnext}
  1059. { do at the end so all complex typeconversions are already }
  1060. { converted to calln's }
  1061. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1062. (lnf_testatbegin in loopflags) then
  1063. begin
  1064. { get first component of the while check }
  1065. runnernode := left;
  1066. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1067. runnernode := tunarynode(runnernode).left;
  1068. { is it an assigned(x) check? }
  1069. if ((runnernode.nodetype = inlinen) and
  1070. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1071. ((runnernode.nodetype = unequaln) and
  1072. (taddnode(runnernode).right.nodetype = niln)) then
  1073. begin
  1074. runnernode := tunarynode(runnernode).left;
  1075. { in case of in_assigned_x, there's a callparan in between }
  1076. if (runnernode.nodetype = callparan) then
  1077. runnernode := tcallparanode(runnernode).left;
  1078. while (runnernode.nodetype = typeconvn) do
  1079. runnernode := ttypeconvnode(runnernode).left;
  1080. { is there an "x := x(^).somefield"? }
  1081. assignmentnode := findassignment(right,runnernode);
  1082. if assigned(assignmentnode) then
  1083. begin
  1084. prefetchcode := internalstatements(prefetchstatements);
  1085. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1086. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1087. addstatement(prefetchstatements,right);
  1088. right := prefetchcode;
  1089. typecheckpass(right);
  1090. end;
  1091. end;
  1092. end;
  1093. {$endif prefetchnext}
  1094. end;
  1095. {$ifdef state_tracking}
  1096. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1097. var condition:Tnode;
  1098. code:Tnode;
  1099. done:boolean;
  1100. value:boolean;
  1101. change:boolean;
  1102. firsttest:boolean;
  1103. factval:Tnode;
  1104. begin
  1105. track_state_pass:=false;
  1106. done:=false;
  1107. firsttest:=true;
  1108. {For repeat until statements, first do a pass through the code.}
  1109. if not(lnf_testatbegin in flags) then
  1110. begin
  1111. code:=right.getcopy;
  1112. if code.track_state_pass(exec_known) then
  1113. track_state_pass:=true;
  1114. code.destroy;
  1115. end;
  1116. repeat
  1117. condition:=left.getcopy;
  1118. code:=right.getcopy;
  1119. change:=condition.track_state_pass(exec_known);
  1120. factval:=aktstate.find_fact(left);
  1121. if factval<>nil then
  1122. begin
  1123. condition.destroy;
  1124. condition:=factval.getcopy;
  1125. change:=true;
  1126. end;
  1127. if change then
  1128. begin
  1129. track_state_pass:=true;
  1130. {Force new resultdef pass.}
  1131. condition.resultdef:=nil;
  1132. do_typecheckpass(condition);
  1133. end;
  1134. if is_constboolnode(condition) then
  1135. begin
  1136. {Try to turn a while loop into a repeat loop.}
  1137. if firsttest then
  1138. exclude(flags,testatbegin);
  1139. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1140. if value then
  1141. begin
  1142. if code.track_state_pass(exec_known) then
  1143. track_state_pass:=true;
  1144. end
  1145. else
  1146. done:=true;
  1147. end
  1148. else
  1149. begin
  1150. {Remove any modified variables from the state.}
  1151. code.track_state_pass(false);
  1152. done:=true;
  1153. end;
  1154. code.destroy;
  1155. condition.destroy;
  1156. firsttest:=false;
  1157. until done;
  1158. {The loop condition is also known, for example:
  1159. while i<10 do
  1160. begin
  1161. ...
  1162. end;
  1163. When the loop is done, we do know that i<10 = false.
  1164. }
  1165. condition:=left.getcopy;
  1166. if condition.track_state_pass(exec_known) then
  1167. begin
  1168. track_state_pass:=true;
  1169. {Force new resultdef pass.}
  1170. condition.resultdef:=nil;
  1171. do_typecheckpass(condition);
  1172. end;
  1173. if not is_constboolnode(condition) then
  1174. aktstate.store_fact(condition,
  1175. cordconstnode.create(byte(checknegate),pasbool8type,true))
  1176. else
  1177. condition.destroy;
  1178. end;
  1179. {$endif}
  1180. {*****************************************************************************
  1181. TIFNODE
  1182. *****************************************************************************}
  1183. constructor tifnode.create(l,r,_t1 : tnode);
  1184. begin
  1185. inherited create(ifn,l,r,_t1,nil);
  1186. end;
  1187. constructor tifnode.create_internal(l,r,_t1 : tnode);
  1188. begin
  1189. create(l,r,_t1);
  1190. include(flags,nf_internal);
  1191. end;
  1192. function tifnode.internalsimplify(warn: boolean) : tnode;
  1193. begin
  1194. result:=nil;
  1195. { optimize constant expressions }
  1196. if (left.nodetype=ordconstn) then
  1197. begin
  1198. if tordconstnode(left).value.uvalue<>0 then
  1199. begin
  1200. if assigned(right) then
  1201. result:=right
  1202. else
  1203. result:=cnothingnode.create;
  1204. right:=nil;
  1205. if warn and assigned(t1) then
  1206. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1207. end
  1208. else
  1209. begin
  1210. if assigned(t1) then
  1211. result:=t1
  1212. else
  1213. result:=cnothingnode.create;
  1214. t1:=nil;
  1215. if warn and assigned(right) then
  1216. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1217. end;
  1218. end;
  1219. end;
  1220. function tifnode.simplify(forinline : boolean) : tnode;
  1221. begin
  1222. result:=internalsimplify(false);
  1223. end;
  1224. function tifnode.pass_typecheck:tnode;
  1225. begin
  1226. result:=nil;
  1227. resultdef:=voidtype;
  1228. typecheckpass(left);
  1229. { tp procvar support }
  1230. maybe_call_procvar(left,true);
  1231. { if path }
  1232. if assigned(right) then
  1233. typecheckpass(right);
  1234. { else path }
  1235. if assigned(t1) then
  1236. typecheckpass(t1);
  1237. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1238. if codegenerror then
  1239. exit;
  1240. if not(is_boolean(left.resultdef)) and
  1241. not(is_typeparam(left.resultdef)) then
  1242. inserttypeconv(left,pasbool8type);
  1243. result:=internalsimplify(not(nf_internal in flags));
  1244. end;
  1245. function tifnode.pass_1 : tnode;
  1246. begin
  1247. result:=nil;
  1248. expectloc:=LOC_VOID;
  1249. firstpass(left);
  1250. { if path }
  1251. if assigned(right) then
  1252. firstpass(right);
  1253. { else path }
  1254. if assigned(t1) then
  1255. firstpass(t1);
  1256. { leave if we've got an error in one of the paths }
  1257. if codegenerror then
  1258. exit;
  1259. end;
  1260. {*****************************************************************************
  1261. TFORNODE
  1262. *****************************************************************************}
  1263. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1264. begin
  1265. inherited create(forn,l,r,_t1,_t2);
  1266. if back then
  1267. include(loopflags,lnf_backward);
  1268. include(loopflags,lnf_testatbegin);
  1269. end;
  1270. function tfornode.simplify(forinline : boolean) : tnode;
  1271. begin
  1272. result:=nil;
  1273. { Can we spare the first comparision? }
  1274. if (t1.nodetype=ordconstn) and
  1275. (right.nodetype=ordconstn) and
  1276. (
  1277. (
  1278. (lnf_backward in loopflags) and
  1279. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1280. ) or
  1281. (
  1282. not(lnf_backward in loopflags) and
  1283. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1284. )
  1285. ) then
  1286. exclude(loopflags,lnf_testatbegin);
  1287. if (t1.nodetype=ordconstn) and
  1288. (right.nodetype=ordconstn) and
  1289. (
  1290. (
  1291. (lnf_backward in loopflags) and
  1292. (tordconstnode(right).value<tordconstnode(t1).value)
  1293. ) or
  1294. (
  1295. not(lnf_backward in loopflags) and
  1296. (tordconstnode(right).value>tordconstnode(t1).value)
  1297. )
  1298. ) then
  1299. result:=cnothingnode.create;
  1300. end;
  1301. function tfornode.wrap_to_value:tnode;
  1302. var
  1303. statements: tstatementnode;
  1304. temp: ttempcreatenode;
  1305. begin
  1306. result:=internalstatements(statements);
  1307. temp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1308. addstatement(statements,temp);
  1309. addstatement(statements,cassignmentnode.create(
  1310. ctemprefnode.create(temp),
  1311. t1));
  1312. { create a new for node, it is cheaper than cloning entire loop body }
  1313. addstatement(statements,cfornode.create(
  1314. left,right,ctemprefnode.create(temp),t2,lnf_backward in loopflags));
  1315. addstatement(statements,ctempdeletenode.create(temp));
  1316. { all child nodes are reused }
  1317. left:=nil;
  1318. right:=nil;
  1319. t1:=nil;
  1320. t2:=nil;
  1321. end;
  1322. function tfornode.pass_typecheck:tnode;
  1323. var
  1324. res : tnode;
  1325. begin
  1326. result:=nil;
  1327. resultdef:=voidtype;
  1328. { process the loopvar, from and to, varstates are already set }
  1329. typecheckpass(left);
  1330. typecheckpass(right);
  1331. typecheckpass(t1);
  1332. set_varstate(left,vs_written,[]);
  1333. { loop unrolling }
  1334. if (cs_opt_loopunroll in current_settings.optimizerswitches) and
  1335. { statements must be error free }
  1336. not(nf_error in t2.flags) then
  1337. begin
  1338. typecheckpass(t2);
  1339. res:=t2.simplify(false);
  1340. if assigned(res) then
  1341. t2:=res;
  1342. res:=unroll_loop(self);
  1343. if assigned(res) then
  1344. begin
  1345. typecheckpass(res);
  1346. result:=res;
  1347. exit;
  1348. end;
  1349. end;
  1350. { Make sure that the loop var and the
  1351. from and to values are compatible types }
  1352. check_ranges(right.fileinfo,right,left.resultdef);
  1353. inserttypeconv(right,left.resultdef);
  1354. check_ranges(t1.fileinfo,t1,left.resultdef);
  1355. inserttypeconv(t1,left.resultdef);
  1356. if assigned(t2) then
  1357. typecheckpass(t2);
  1358. end;
  1359. function tfornode.pass_1 : tnode;
  1360. begin
  1361. result:=nil;
  1362. expectloc:=LOC_VOID;
  1363. firstpass(left);
  1364. firstpass(right);
  1365. firstpass(t1);
  1366. if assigned(t2) then
  1367. firstpass(t2);
  1368. if codegenerror then
  1369. exit;
  1370. { 'to' value must be evaluated once before loop, so its possible modifications
  1371. inside loop body do not affect the number of iterations (see webtbs/tw8883). }
  1372. if not (t1.nodetype in [ordconstn,temprefn]) then
  1373. result:=wrap_to_value;
  1374. end;
  1375. {*****************************************************************************
  1376. TEXITNODE
  1377. *****************************************************************************}
  1378. constructor texitnode.create(l:tnode);
  1379. begin
  1380. inherited create(exitn,l);
  1381. if assigned(left) then
  1382. begin
  1383. { add assignment to funcretsym }
  1384. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1385. left:=cassignmentnode.create(
  1386. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1387. left);
  1388. end;
  1389. end;
  1390. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1391. begin
  1392. inherited ppuload(t,ppufile);
  1393. end;
  1394. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1395. begin
  1396. inherited ppuwrite(ppufile);
  1397. end;
  1398. function texitnode.pass_typecheck:tnode;
  1399. var
  1400. newstatement : tstatementnode;
  1401. begin
  1402. result:=nil;
  1403. if assigned(left) then
  1404. begin
  1405. result:=internalstatements(newstatement);
  1406. addstatement(newstatement,left);
  1407. left:=nil;
  1408. addstatement(newstatement,self.getcopy);
  1409. end;
  1410. resultdef:=voidtype;
  1411. end;
  1412. function texitnode.pass_1 : tnode;
  1413. begin
  1414. result:=nil;
  1415. expectloc:=LOC_VOID;
  1416. if assigned(left) then
  1417. internalerror(2011052801);
  1418. end;
  1419. {*****************************************************************************
  1420. TBREAKNODE
  1421. *****************************************************************************}
  1422. constructor tbreaknode.create;
  1423. begin
  1424. inherited create(breakn);
  1425. end;
  1426. function tbreaknode.pass_typecheck:tnode;
  1427. begin
  1428. result:=nil;
  1429. resultdef:=voidtype;
  1430. end;
  1431. function tbreaknode.pass_1 : tnode;
  1432. begin
  1433. result:=nil;
  1434. expectloc:=LOC_VOID;
  1435. end;
  1436. {*****************************************************************************
  1437. TCONTINUENODE
  1438. *****************************************************************************}
  1439. constructor tcontinuenode.create;
  1440. begin
  1441. inherited create(continuen);
  1442. end;
  1443. function tcontinuenode.pass_typecheck:tnode;
  1444. begin
  1445. result:=nil;
  1446. resultdef:=voidtype;
  1447. end;
  1448. function tcontinuenode.pass_1 : tnode;
  1449. begin
  1450. result:=nil;
  1451. expectloc:=LOC_VOID;
  1452. end;
  1453. {*****************************************************************************
  1454. TGOTONODE
  1455. *****************************************************************************}
  1456. constructor tgotonode.create(p : tlabelsym);
  1457. begin
  1458. inherited create(goton);
  1459. exceptionblock:=current_exceptblock;
  1460. labelnode:=nil;
  1461. labelsym:=p;
  1462. end;
  1463. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1464. begin
  1465. inherited ppuload(t,ppufile);
  1466. labelnodeidx:=ppufile.getlongint;
  1467. exceptionblock:=ppufile.getbyte;
  1468. end;
  1469. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1470. begin
  1471. inherited ppuwrite(ppufile);
  1472. labelnodeidx:=labelnode.ppuidx;
  1473. ppufile.putlongint(labelnodeidx);
  1474. ppufile.putbyte(exceptionblock);
  1475. end;
  1476. procedure tgotonode.buildderefimpl;
  1477. begin
  1478. inherited buildderefimpl;
  1479. end;
  1480. procedure tgotonode.derefimpl;
  1481. begin
  1482. inherited derefimpl;
  1483. end;
  1484. procedure tgotonode.resolveppuidx;
  1485. begin
  1486. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  1487. if labelnode.nodetype<>labeln then
  1488. internalerror(200809021);
  1489. end;
  1490. function tgotonode.pass_typecheck:tnode;
  1491. begin
  1492. result:=nil;
  1493. resultdef:=voidtype;
  1494. end;
  1495. function tgotonode.pass_1 : tnode;
  1496. var
  1497. p2 : tprocinfo;
  1498. begin
  1499. result:=nil;
  1500. expectloc:=LOC_VOID;
  1501. { The labelnode can already be set when
  1502. this node was copied }
  1503. if not(assigned(labelnode)) then
  1504. begin
  1505. { inner procedure goto? }
  1506. if assigned(labelsym.code) and
  1507. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1508. { generated by the optimizer? }
  1509. not(assigned(labelsym.owner))) then
  1510. labelnode:=tlabelnode(labelsym.code)
  1511. else if ((m_non_local_goto in current_settings.modeswitches) and
  1512. assigned(labelsym.owner)) or
  1513. { nested exits don't need the non local goto switch }
  1514. (labelsym.realname='$nestedexit') then
  1515. begin
  1516. if current_procinfo.procdef.parast.symtablelevel>labelsym.owner.symtablelevel then
  1517. begin
  1518. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  1519. if exceptionblock>0 then
  1520. CGMessage(cg_e_goto_inout_of_exception_block);
  1521. { goto across procedures using exception?
  1522. this is not allowed because we cannot
  1523. easily unwind the exception frame
  1524. stack
  1525. }
  1526. p2:=current_procinfo;
  1527. while true do
  1528. begin
  1529. if (p2.flags*[pi_needs_implicit_finally,pi_uses_exceptions,pi_has_implicit_finally])<>[] then
  1530. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  1531. if labelsym.owner=p2.procdef.localst then
  1532. break;
  1533. p2:=p2.parent
  1534. end;
  1535. if assigned(labelsym.jumpbuf) then
  1536. begin
  1537. labelsym.nonlocal:=true;
  1538. exclude(current_procinfo.procdef.procoptions,po_inline);
  1539. result:=ccallnode.createintern('fpc_longjmp',
  1540. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  1541. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  1542. nil)));
  1543. end
  1544. else
  1545. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1546. end
  1547. else
  1548. CGMessage(cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  1549. end
  1550. else
  1551. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1552. end;
  1553. { check if we don't mess with exception blocks }
  1554. if assigned(labelnode) and
  1555. (exceptionblock<>labelnode.exceptionblock) then
  1556. CGMessage(cg_e_goto_inout_of_exception_block);
  1557. end;
  1558. function tgotonode.dogetcopy : tnode;
  1559. var
  1560. p : tgotonode;
  1561. begin
  1562. p:=tgotonode(inherited dogetcopy);
  1563. p.exceptionblock:=exceptionblock;
  1564. { generate labelnode if not done yet }
  1565. if not(assigned(labelnode)) then
  1566. begin
  1567. if assigned(labelsym) and assigned(labelsym.code) then
  1568. labelnode:=tlabelnode(labelsym.code)
  1569. end;
  1570. p.labelsym:=labelsym;
  1571. if assigned(labelnode) then
  1572. p.labelnode:=tlabelnode(labelnode.dogetcopy)
  1573. else
  1574. begin
  1575. { don't trigger IE when there was already an error, i.e. the
  1576. label is not defined. See tw11763 (PFV) }
  1577. if (errorcount=0) and
  1578. { don't trigger IE if it's a global goto }
  1579. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1580. not(assigned(labelsym.owner))) then
  1581. internalerror(200610291);
  1582. end;
  1583. result:=p;
  1584. end;
  1585. function tgotonode.docompare(p: tnode): boolean;
  1586. begin
  1587. docompare := false;
  1588. end;
  1589. {*****************************************************************************
  1590. TLABELNODE
  1591. *****************************************************************************}
  1592. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  1593. begin
  1594. inherited create(labeln,l);
  1595. exceptionblock:=current_exceptblock;
  1596. labsym:=alabsym;
  1597. { Register labelnode in labelsym }
  1598. labsym.code:=self;
  1599. end;
  1600. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1601. begin
  1602. inherited ppuload(t,ppufile);
  1603. exceptionblock:=ppufile.getbyte;
  1604. end;
  1605. destructor tlabelnode.destroy;
  1606. begin
  1607. { Remove reference in labelsym, this is to prevent
  1608. goto's to this label }
  1609. if assigned(labsym) and (labsym.code=pointer(self)) then
  1610. labsym.code:=nil;
  1611. inherited destroy;
  1612. end;
  1613. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  1614. begin
  1615. inherited ppuwrite(ppufile);
  1616. ppufile.putbyte(exceptionblock);
  1617. end;
  1618. procedure tlabelnode.buildderefimpl;
  1619. begin
  1620. inherited buildderefimpl;
  1621. end;
  1622. procedure tlabelnode.derefimpl;
  1623. begin
  1624. inherited derefimpl;
  1625. end;
  1626. function tlabelnode.pass_typecheck:tnode;
  1627. begin
  1628. result:=nil;
  1629. { left could still be unassigned }
  1630. if assigned(left) then
  1631. typecheckpass(left);
  1632. resultdef:=voidtype;
  1633. end;
  1634. function tlabelnode.pass_1 : tnode;
  1635. begin
  1636. result:=nil;
  1637. expectloc:=LOC_VOID;
  1638. include(current_procinfo.flags,pi_has_label);
  1639. if assigned(labsym) and labsym.nonlocal then
  1640. begin
  1641. include(current_procinfo.flags,pi_has_interproclabel);
  1642. exclude(current_procinfo.procdef.procoptions,po_inline);
  1643. end;
  1644. if assigned(left) then
  1645. firstpass(left);
  1646. if (m_non_local_goto in current_settings.modeswitches) and
  1647. { the owner can be Nil for internal labels }
  1648. assigned(labsym.owner) and
  1649. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  1650. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  1651. end;
  1652. function tlabelnode.dogetcopy : tnode;
  1653. begin
  1654. if not(assigned(copiedto)) then
  1655. copiedto:=tlabelnode(inherited dogetcopy);
  1656. copiedto.exceptionblock:=exceptionblock;
  1657. result:=copiedto;
  1658. end;
  1659. function tlabelnode.docompare(p: tnode): boolean;
  1660. begin
  1661. docompare := false;
  1662. end;
  1663. {*****************************************************************************
  1664. TRAISENODE
  1665. *****************************************************************************}
  1666. constructor traisenode.create(l,taddr,tframe:tnode);
  1667. begin
  1668. inherited create(raisen,l,taddr,tframe);
  1669. end;
  1670. function traisenode.pass_typecheck:tnode;
  1671. begin
  1672. result:=nil;
  1673. resultdef:=voidtype;
  1674. if assigned(left) then
  1675. begin
  1676. { first para must be a _class_ }
  1677. typecheckpass(left);
  1678. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1679. if codegenerror then
  1680. exit;
  1681. if not is_class(left.resultdef) and
  1682. not is_javaclass(left.resultdef) then
  1683. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  1684. { insert needed typeconvs for addr,frame }
  1685. if assigned(right) then
  1686. begin
  1687. { addr }
  1688. typecheckpass(right);
  1689. inserttypeconv(right,voidcodepointertype);
  1690. { frame }
  1691. if assigned(third) then
  1692. begin
  1693. typecheckpass(third);
  1694. inserttypeconv(third,voidpointertype);
  1695. end;
  1696. end;
  1697. end;
  1698. end;
  1699. function traisenode.pass_1 : tnode;
  1700. var
  1701. statements : tstatementnode;
  1702. current_addr : tlabelnode;
  1703. raisenode : tcallnode;
  1704. begin
  1705. result:=internalstatements(statements);
  1706. if assigned(left) then
  1707. begin
  1708. { first para must be a class }
  1709. firstpass(left);
  1710. { insert needed typeconvs for addr,frame }
  1711. if assigned(right) then
  1712. begin
  1713. { addr }
  1714. firstpass(right);
  1715. { frame }
  1716. if assigned(third) then
  1717. firstpass(third)
  1718. else
  1719. third:=cpointerconstnode.Create(0,voidpointertype);
  1720. end
  1721. else
  1722. begin
  1723. third:=cinlinenode.create(in_get_frame,false,nil);
  1724. current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
  1725. addstatement(statements,current_addr);
  1726. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  1727. end;
  1728. raisenode:=ccallnode.createintern('fpc_raiseexception',
  1729. ccallparanode.create(third,
  1730. ccallparanode.create(right,
  1731. ccallparanode.create(left,nil)))
  1732. );
  1733. include(raisenode.callnodeflags,cnf_call_never_returns);
  1734. addstatement(statements,raisenode);
  1735. end
  1736. else
  1737. begin
  1738. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  1739. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  1740. include(raisenode.callnodeflags,cnf_call_never_returns);
  1741. addstatement(statements,raisenode);
  1742. end;
  1743. left:=nil;
  1744. right:=nil;
  1745. third:=nil;
  1746. end;
  1747. {*****************************************************************************
  1748. TTRYEXCEPTNODE
  1749. *****************************************************************************}
  1750. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1751. begin
  1752. inherited create(tryexceptn,l,r,_t1,nil);
  1753. end;
  1754. function ttryexceptnode.pass_typecheck:tnode;
  1755. begin
  1756. result:=nil;
  1757. typecheckpass(left);
  1758. { on statements }
  1759. if assigned(right) then
  1760. typecheckpass(right);
  1761. { else block }
  1762. if assigned(t1) then
  1763. typecheckpass(t1);
  1764. resultdef:=voidtype;
  1765. end;
  1766. function ttryexceptnode.pass_1 : tnode;
  1767. begin
  1768. result:=nil;
  1769. expectloc:=LOC_VOID;
  1770. firstpass(left);
  1771. { on statements }
  1772. if assigned(right) then
  1773. firstpass(right);
  1774. { else block }
  1775. if assigned(t1) then
  1776. firstpass(t1);
  1777. include(current_procinfo.flags,pi_do_call);
  1778. include(current_procinfo.flags,pi_uses_exceptions);
  1779. inc(current_procinfo.estimatedtempsize,get_jumpbuf_size*2);
  1780. end;
  1781. function ttryexceptnode.simplify(forinline: boolean): tnode;
  1782. begin
  1783. result:=nil;
  1784. { empty try -> can never raise exception -> do nothing }
  1785. if has_no_code(left) then
  1786. result:=cnothingnode.create;
  1787. end;
  1788. {*****************************************************************************
  1789. TTRYFINALLYNODE
  1790. *****************************************************************************}
  1791. constructor ttryfinallynode.create(l,r:tnode);
  1792. begin
  1793. inherited create(tryfinallyn,l,r,nil,nil);
  1794. implicitframe:=false;
  1795. end;
  1796. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1797. begin
  1798. inherited create(tryfinallyn,l,r,_t1,nil);
  1799. implicitframe:=true;
  1800. end;
  1801. function ttryfinallynode.pass_typecheck:tnode;
  1802. begin
  1803. result:=nil;
  1804. resultdef:=voidtype;
  1805. typecheckpass(left);
  1806. // "try block" is "used"? (JM)
  1807. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  1808. typecheckpass(right);
  1809. // "except block" is "used"? (JM)
  1810. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  1811. { special finally block only executed when there was an exception }
  1812. if assigned(t1) then
  1813. begin
  1814. typecheckpass(t1);
  1815. // "finally block" is "used"? (JM)
  1816. set_varstate(t1,vs_readwritten,[vsf_must_be_valid]);
  1817. end;
  1818. end;
  1819. function ttryfinallynode.pass_1 : tnode;
  1820. begin
  1821. result:=nil;
  1822. expectloc:=LOC_VOID;
  1823. firstpass(left);
  1824. firstpass(right);
  1825. if assigned(t1) then
  1826. firstpass(t1);
  1827. include(current_procinfo.flags,pi_do_call);
  1828. { pi_uses_exceptions is an information for the optimizer and it
  1829. is only interested in exceptions if they appear inside the body,
  1830. so ignore implicit frames when setting the flag }
  1831. if not(implicitframe) then
  1832. include(current_procinfo.flags,pi_uses_exceptions);
  1833. inc(current_procinfo.estimatedtempsize,get_jumpbuf_size);
  1834. end;
  1835. function ttryfinallynode.simplify(forinline : boolean): tnode;
  1836. begin
  1837. result:=nil;
  1838. { if the try contains no code, we can kill
  1839. the try and except and return only the
  1840. finally part }
  1841. if has_no_code(left) then
  1842. begin
  1843. result:=right;
  1844. right:=nil;
  1845. end;
  1846. end;
  1847. var
  1848. seq: longint=0;
  1849. function ttryfinallynode.create_finalizer_procdef: tprocdef;
  1850. var
  1851. st:TSymTable;
  1852. checkstack: psymtablestackitem;
  1853. oldsymtablestack: tsymtablestack;
  1854. sym:tprocsym;
  1855. begin
  1856. { get actual procedure symtable (skip withsymtables, etc.) }
  1857. st:=nil;
  1858. checkstack:=symtablestack.stack;
  1859. while assigned(checkstack) do
  1860. begin
  1861. st:=checkstack^.symtable;
  1862. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  1863. break;
  1864. checkstack:=checkstack^.next;
  1865. end;
  1866. { Create a nested procedure, even from main_program_level.
  1867. Furthermore, force procdef and procsym into the same symtable
  1868. (by default, defs are registered with symtablestack.top which may be
  1869. something temporary like exceptsymtable - in that case, procdef can be
  1870. destroyed before procsym, leaving invalid pointers). }
  1871. oldsymtablestack:=symtablestack;
  1872. symtablestack:=nil;
  1873. result:=cprocdef.create(max(normal_function_level,st.symtablelevel)+1);
  1874. symtablestack:=oldsymtablestack;
  1875. st.insertdef(result);
  1876. result.struct:=current_procinfo.procdef.struct;
  1877. { tabstractprocdef constructor sets po_delphi_nested_cc whenever
  1878. nested procvars modeswitch is active. We must be independent of this switch. }
  1879. exclude(result.procoptions,po_delphi_nested_cc);
  1880. result.proctypeoption:=potype_exceptfilter;
  1881. handle_calling_convention(result);
  1882. sym:=cprocsym.create('$fin$'+tostr(seq));
  1883. st.insert(sym);
  1884. inc(seq);
  1885. result.procsym:=sym;
  1886. proc_add_definition(result);
  1887. result.forwarddef:=false;
  1888. result.aliasnames.insert(result.mangledname);
  1889. end;
  1890. {*****************************************************************************
  1891. TONNODE
  1892. *****************************************************************************}
  1893. constructor tonnode.create(l,r:tnode);
  1894. begin
  1895. inherited create(onn,l,r);
  1896. excepTSymtable:=nil;
  1897. excepttype:=nil;
  1898. end;
  1899. destructor tonnode.destroy;
  1900. begin
  1901. { copied nodes don't need to release the symtable }
  1902. if assigned(excepTSymtable) then
  1903. excepTSymtable.free;
  1904. inherited destroy;
  1905. end;
  1906. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1907. begin
  1908. inherited ppuload(t,ppufile);
  1909. excepTSymtable:=nil;
  1910. excepttype:=nil;
  1911. end;
  1912. function tonnode.dogetcopy : tnode;
  1913. var
  1914. n : tonnode;
  1915. begin
  1916. n:=tonnode(inherited dogetcopy);
  1917. if assigned(exceptsymtable) then
  1918. n.exceptsymtable:=exceptsymtable.getcopy
  1919. else
  1920. n.exceptsymtable:=nil;
  1921. n.excepttype:=excepttype;
  1922. result:=n;
  1923. end;
  1924. function tonnode.pass_typecheck:tnode;
  1925. begin
  1926. result:=nil;
  1927. resultdef:=voidtype;
  1928. if not is_class(excepttype) and
  1929. not is_javaclass(excepttype) then
  1930. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1931. if assigned(left) then
  1932. typecheckpass(left);
  1933. if assigned(right) then
  1934. typecheckpass(right);
  1935. end;
  1936. function tonnode.pass_1 : tnode;
  1937. begin
  1938. result:=nil;
  1939. include(current_procinfo.flags,pi_do_call);
  1940. { Loads exception class VMT, therefore may need GOT
  1941. (generic code only; descendants may need to avoid this check) }
  1942. if (cs_create_pic in current_settings.moduleswitches) and
  1943. (tf_pic_uses_got in target_info.flags) then
  1944. include(current_procinfo.flags,pi_needs_got);
  1945. expectloc:=LOC_VOID;
  1946. if assigned(left) then
  1947. firstpass(left);
  1948. if assigned(right) then
  1949. firstpass(right);
  1950. end;
  1951. function tonnode.docompare(p: tnode): boolean;
  1952. begin
  1953. docompare := false;
  1954. end;
  1955. end.