nflw.pas 79 KB

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