nflw.pas 79 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. symnot,
  25. symtype,symbase,symdef,symsym,
  26. optloop;
  27. type
  28. { flags used by loop nodes }
  29. tloopflag = (
  30. { set if it is a for ... downto ... do loop }
  31. lnf_backward,
  32. { Do we need to parse childs to set var state? }
  33. lnf_varstate,
  34. { Do a test at the begin of the loop?}
  35. lnf_testatbegin,
  36. { Negate the loop test? }
  37. lnf_checknegate,
  38. { Should the value of the loop variable on exit be correct. }
  39. lnf_dont_mind_loopvar_on_exit,
  40. { Loop simplify flag }
  41. lnf_simplify_processing);
  42. tloopflags = set of tloopflag;
  43. const
  44. { loop flags which must match to consider loop nodes equal regarding the flags }
  45. loopflagsequal = [lnf_backward];
  46. type
  47. tlabelnode = class;
  48. tloopnode = class(tbinarynode)
  49. t1,t2 : tnode;
  50. loopflags : tloopflags;
  51. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  52. destructor destroy;override;
  53. function dogetcopy : tnode;override;
  54. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  55. procedure ppuwrite(ppufile:tcompilerppufile);override;
  56. procedure buildderefimpl;override;
  57. procedure derefimpl;override;
  58. procedure insertintolist(l : tnodelist);override;
  59. procedure printnodetree(var t:text);override;
  60. function docompare(p: tnode): boolean; override;
  61. end;
  62. twhilerepeatnode = class(tloopnode)
  63. constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
  64. function pass_typecheck:tnode;override;
  65. function pass_1 : tnode;override;
  66. {$ifdef state_tracking}
  67. function track_state_pass(exec_known:boolean):boolean;override;
  68. {$endif}
  69. end;
  70. twhilerepeatnodeclass = class of twhilerepeatnode;
  71. tifnode = class(tloopnode)
  72. constructor create(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. procedure loop_var_access(not_type:Tnotification_flag;symbol:Tsym);
  90. function wrap_to_value:tnode;
  91. function pass_typecheck:tnode;override;
  92. function pass_1 : tnode;override;
  93. function simplify(forinline : boolean) : tnode;override;
  94. end;
  95. tfornodeclass = class of tfornode;
  96. texitnode = class(tunarynode)
  97. constructor create(l:tnode);virtual;
  98. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  99. procedure ppuwrite(ppufile:tcompilerppufile);override;
  100. function pass_typecheck:tnode;override;
  101. function pass_1 : tnode;override;
  102. property resultexpr : tnode read left write left;
  103. end;
  104. texitnodeclass = class of texitnode;
  105. tbreaknode = class(tnode)
  106. constructor create;virtual;
  107. function pass_typecheck:tnode;override;
  108. function pass_1 : tnode;override;
  109. end;
  110. tbreaknodeclass = class of tbreaknode;
  111. tcontinuenode = class(tnode)
  112. constructor create;virtual;
  113. function pass_typecheck:tnode;override;
  114. function pass_1 : tnode;override;
  115. end;
  116. tcontinuenodeclass = class of tcontinuenode;
  117. tgotonode = class(tnode)
  118. private
  119. labelnodeidx : longint;
  120. public
  121. labelsym : tlabelsym;
  122. labelnode : tlabelnode;
  123. exceptionblock : integer;
  124. constructor create(p : tlabelsym);virtual;
  125. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  126. procedure ppuwrite(ppufile:tcompilerppufile);override;
  127. procedure buildderefimpl;override;
  128. procedure derefimpl;override;
  129. procedure resolveppuidx;override;
  130. function dogetcopy : tnode;override;
  131. function pass_typecheck:tnode;override;
  132. function pass_1 : tnode;override;
  133. function docompare(p: tnode): boolean; override;
  134. end;
  135. tgotonodeclass = class of tgotonode;
  136. tlabelnode = class(tunarynode)
  137. exceptionblock : integer;
  138. { when copying trees, this points to the newly created copy of a label }
  139. copiedto : tlabelnode;
  140. labsym : tlabelsym;
  141. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  142. destructor destroy;override;
  143. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  144. procedure ppuwrite(ppufile:tcompilerppufile);override;
  145. procedure buildderefimpl;override;
  146. procedure derefimpl;override;
  147. function dogetcopy : tnode;override;
  148. function pass_typecheck:tnode;override;
  149. function pass_1 : tnode;override;
  150. function docompare(p: tnode): boolean; override;
  151. end;
  152. tlabelnodeclass = class of tlabelnode;
  153. traisenode = class(ttertiarynode)
  154. constructor create(l,taddr,tframe:tnode);virtual;
  155. function pass_typecheck:tnode;override;
  156. function pass_1 : tnode;override;
  157. end;
  158. traisenodeclass = class of traisenode;
  159. ttryexceptnode = class(tloopnode)
  160. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  161. function pass_typecheck:tnode;override;
  162. function pass_1 : 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:=tarraydef.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. tarraydef.create_from_pointer(tpointerdef(hp.resultdef).pointeddef));
  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:=tarraydef.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 not is_open_array(expression.resultdef) then
  525. begin
  526. { create a temp variable for expression }
  527. arrayvar := ctempcreatenode.create(
  528. expression.resultdef,
  529. expression.resultdef.size,
  530. tt_persistent,true);
  531. if is_string then
  532. begin
  533. lowbound:=genintconstnode(1);
  534. highbound:=cinlinenode.create(in_length_x,false,ctemprefnode.create(arrayvar))
  535. end
  536. else
  537. begin
  538. lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
  539. highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
  540. end;
  541. addstatement(loopstatement,arrayvar);
  542. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(arrayvar),expression.getcopy));
  543. end
  544. else
  545. begin
  546. arrayvar:=nil;
  547. if is_string then
  548. begin
  549. lowbound:=genintconstnode(1);
  550. highbound:=cinlinenode.create(in_length_x,false,expression.getcopy);
  551. end
  552. else
  553. begin
  554. lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
  555. highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
  556. end;
  557. end;
  558. { create a loop counter }
  559. loopvar := ctempcreatenode.create(
  560. tarraydef(expression.resultdef).rangedef,
  561. tarraydef(expression.resultdef).rangedef.size,
  562. tt_persistent,true);
  563. addstatement(loopstatement,loopvar);
  564. arrayindex:=ctemprefnode.create(loopvar);
  565. loopbody:=internalstatements(loopbodystatement);
  566. // for-in loop variable := array_expression[index]
  567. if assigned(arrayvar) then
  568. addstatement(loopbodystatement,
  569. cassignmentnode.create(hloopvar,cvecnode.create(ctemprefnode.create(arrayvar),arrayindex)))
  570. else
  571. addstatement(loopbodystatement,
  572. cassignmentnode.create(hloopvar,cvecnode.create(expression.getcopy,arrayindex)));
  573. { add the actual statement to the loop }
  574. addstatement(loopbodystatement,hloopbody);
  575. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  576. lowbound,
  577. highbound,
  578. loopbody,
  579. false);
  580. addstatement(loopstatement,forloopnode);
  581. { free the loop counter }
  582. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  583. { free the temp variable for expression if needed }
  584. if arrayvar<>nil then
  585. addstatement(loopstatement,ctempdeletenode.create(arrayvar));
  586. end;
  587. function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  588. var
  589. loopstatement, loopbodystatement: tstatementnode;
  590. loopvar, setvar: ttempcreatenode;
  591. loopbody, forloopnode: tnode;
  592. begin
  593. // first check is set is empty and if it so then skip other processing
  594. if not Assigned(tsetdef(expr.resultdef).elementdef) then
  595. begin
  596. result:=cnothingnode.create;
  597. // free unused nodes
  598. hloopvar.free;
  599. hloopbody.free;
  600. exit;
  601. end;
  602. { result is a block of statements }
  603. result:=internalstatements(loopstatement);
  604. { create a temp variable for expression }
  605. setvar := ctempcreatenode.create(
  606. expr.resultdef,
  607. expr.resultdef.size,
  608. tt_persistent,true);
  609. addstatement(loopstatement,setvar);
  610. addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(setvar),expr.getcopy));
  611. { create a loop counter }
  612. loopvar := ctempcreatenode.create(
  613. tsetdef(expr.resultdef).elementdef,
  614. tsetdef(expr.resultdef).elementdef.size,
  615. tt_persistent,true);
  616. addstatement(loopstatement,loopvar);
  617. // if loopvar in set then
  618. // begin
  619. // hloopvar := loopvar
  620. // for-in loop body
  621. // end
  622. loopbody:=cifnode.create(
  623. cinnode.create(ctemprefnode.create(loopvar),ctemprefnode.create(setvar)),
  624. internalstatements(loopbodystatement),
  625. nil);
  626. addstatement(loopbodystatement,cassignmentnode.create(hloopvar,ctemprefnode.create(loopvar)));
  627. { add the actual statement to the loop }
  628. addstatement(loopbodystatement,hloopbody);
  629. forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
  630. cinlinenode.create(in_low_x,false,ctemprefnode.create(setvar)),
  631. cinlinenode.create(in_high_x,false,ctemprefnode.create(setvar)),
  632. loopbody,
  633. false);
  634. addstatement(loopstatement,forloopnode);
  635. { free the loop counter }
  636. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  637. { free the temp variable for expression }
  638. addstatement(loopstatement,ctempdeletenode.create(setvar));
  639. end;
  640. function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
  641. enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
  642. var
  643. loopstatement, loopbodystatement: tstatementnode;
  644. enumvar: ttempcreatenode;
  645. loopbody, whileloopnode,
  646. enum_get, enum_move, enum_current, enum_get_params: tnode;
  647. propaccesslist: tpropaccesslist;
  648. enumerator_is_class: boolean;
  649. enumerator_destructor: tprocdef;
  650. begin
  651. { result is a block of statements }
  652. result:=internalstatements(loopstatement);
  653. enumerator_is_class := is_class(enumerator_get.returndef);
  654. { create a temp variable for enumerator }
  655. enumvar := ctempcreatenode.create(
  656. enumerator_get.returndef,
  657. enumerator_get.returndef.size,
  658. tt_persistent,true);
  659. addstatement(loopstatement,enumvar);
  660. if enumerator_get.proctypeoption=potype_operator then
  661. begin
  662. enum_get_params:=ccallparanode.create(expr.getcopy,nil);
  663. enum_get:=ccallnode.create(enum_get_params, tprocsym(enumerator_get.procsym), nil, nil, []);
  664. tcallnode(enum_get).procdefinition:=enumerator_get;
  665. addsymref(enumerator_get.procsym);
  666. end
  667. else
  668. enum_get:=ccallnode.create(nil, tprocsym(enumerator_get.procsym), enumerator_get.owner, expr.getcopy, []);
  669. addstatement(loopstatement,
  670. cassignmentnode.create(
  671. ctemprefnode.create(enumvar),
  672. enum_get
  673. ));
  674. loopbody:=internalstatements(loopbodystatement);
  675. { for-in loop variable := enumerator.current }
  676. if enumerator_current.getpropaccesslist(palt_read,propaccesslist) then
  677. begin
  678. case propaccesslist.firstsym^.sym.typ of
  679. fieldvarsym :
  680. begin
  681. { generate access code }
  682. enum_current:=ctemprefnode.create(enumvar);
  683. propaccesslist_to_node(enum_current,enumerator_current.owner,propaccesslist);
  684. include(enum_current.flags,nf_isproperty);
  685. end;
  686. procsym :
  687. begin
  688. { generate the method call }
  689. enum_current:=ccallnode.create(nil,tprocsym(propaccesslist.firstsym^.sym),enumerator_current.owner,ctemprefnode.create(enumvar),[]);
  690. include(enum_current.flags,nf_isproperty);
  691. end
  692. else
  693. begin
  694. enum_current:=cerrornode.create;
  695. Message(type_e_mismatch);
  696. end;
  697. end;
  698. end
  699. else
  700. enum_current:=cerrornode.create;
  701. addstatement(loopbodystatement,
  702. cassignmentnode.create(hloopvar, enum_current));
  703. { add the actual statement to the loop }
  704. addstatement(loopbodystatement,hloopbody);
  705. enum_move:=ccallnode.create(nil, tprocsym(enumerator_move.procsym), enumerator_move.owner, ctemprefnode.create(enumvar), []);
  706. whileloopnode:=cwhilerepeatnode.create(enum_move,loopbody,true,false);
  707. if enumerator_is_class then
  708. begin
  709. { insert a try-finally and call the destructor for the enumerator in the finally section }
  710. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  711. if assigned(enumerator_destructor) then
  712. begin
  713. whileloopnode:=ctryfinallynode.create(
  714. whileloopnode, // try node
  715. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym), // finally node
  716. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[]));
  717. end;
  718. { if getenumerator <> nil then do the loop }
  719. whileloopnode:=cifnode.create(
  720. caddnode.create(unequaln, ctemprefnode.create(enumvar), cnilnode.create),
  721. whileloopnode,
  722. nil);
  723. end;
  724. addstatement(loopstatement, whileloopnode);
  725. if is_object(enumerator_get.returndef) then
  726. begin
  727. // call the object destructor too
  728. enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
  729. if assigned(enumerator_destructor) then
  730. begin
  731. addstatement(loopstatement,
  732. ccallnode.create(nil,tprocsym(enumerator_destructor.procsym),
  733. enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[]));
  734. end;
  735. end;
  736. { free the temp variable for enumerator }
  737. addstatement(loopstatement,ctempdeletenode.create(enumvar));
  738. end;
  739. function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
  740. var
  741. pd, movenext: tprocdef;
  742. helperdef: tobjectdef;
  743. current: tpropertysym;
  744. storefilepos: tfileposinfo;
  745. begin
  746. storefilepos:=current_filepos;
  747. current_filepos:=hloopvar.fileinfo;
  748. if expr.nodetype=typen then
  749. begin
  750. if (expr.resultdef.typ=enumdef) and tenumdef(expr.resultdef).has_jumps then
  751. begin
  752. result:=cerrornode.create;
  753. hloopvar.free;
  754. hloopbody.free;
  755. MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
  756. end
  757. else
  758. result:=create_type_for_in_loop(hloopvar, hloopbody, expr);
  759. end
  760. else
  761. begin
  762. { loop is made for an expression }
  763. // Objective-C uses different conventions (and it's only supported for Objective-C 2.0)
  764. if is_objc_class_or_protocol(hloopvar.resultdef) or
  765. is_objc_class_or_protocol(expr.resultdef) then
  766. begin
  767. result:=create_objc_for_in_loop(hloopvar,hloopbody,expr);
  768. if result.nodetype=errorn then
  769. begin
  770. hloopvar.free;
  771. hloopbody.free;
  772. end;
  773. end
  774. else
  775. begin
  776. // search for operator first
  777. pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
  778. // if there is no operator then search for class/object enumerator method
  779. if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
  780. begin
  781. { first search using the helper hierarchy }
  782. if search_last_objectpascal_helper(tabstractrecorddef(expr.resultdef),nil,helperdef) then
  783. repeat
  784. pd:=helperdef.search_enumerator_get;
  785. helperdef:=helperdef.childof;
  786. until (pd<>nil) or (helperdef=nil);
  787. { we didn't find an enumerator in a helper, so search in the
  788. class/record/object itself }
  789. if pd=nil then
  790. pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
  791. end;
  792. if pd<>nil then
  793. begin
  794. // seach movenext and current symbols
  795. movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
  796. if movenext = nil then
  797. begin
  798. result:=cerrornode.create;
  799. hloopvar.free;
  800. hloopbody.free;
  801. MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
  802. end
  803. else
  804. begin
  805. current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  806. if current = nil then
  807. begin
  808. result:=cerrornode.create;
  809. hloopvar.free;
  810. hloopbody.free;
  811. MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
  812. end
  813. else
  814. result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
  815. end;
  816. end
  817. else
  818. begin
  819. case expr.resultdef.typ of
  820. stringdef: result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
  821. arraydef: result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
  822. setdef: result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
  823. else
  824. begin
  825. result:=cerrornode.create;
  826. hloopvar.free;
  827. hloopbody.free;
  828. MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
  829. end;
  830. end;
  831. end;
  832. end;
  833. end;
  834. current_filepos:=storefilepos;
  835. end;
  836. {****************************************************************************
  837. TLOOPNODE
  838. *****************************************************************************}
  839. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  840. begin
  841. inherited create(tt,l,r);
  842. t1:=_t1;
  843. t2:=_t2;
  844. fileinfo:=l.fileinfo;
  845. end;
  846. destructor tloopnode.destroy;
  847. begin
  848. t1.free;
  849. t2.free;
  850. inherited destroy;
  851. end;
  852. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  853. begin
  854. inherited ppuload(t,ppufile);
  855. t1:=ppuloadnode(ppufile);
  856. t2:=ppuloadnode(ppufile);
  857. ppufile.getsmallset(loopflags);
  858. end;
  859. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  860. begin
  861. inherited ppuwrite(ppufile);
  862. ppuwritenode(ppufile,t1);
  863. ppuwritenode(ppufile,t2);
  864. ppufile.putsmallset(loopflags);
  865. end;
  866. procedure tloopnode.buildderefimpl;
  867. begin
  868. inherited buildderefimpl;
  869. if assigned(t1) then
  870. t1.buildderefimpl;
  871. if assigned(t2) then
  872. t2.buildderefimpl;
  873. end;
  874. procedure tloopnode.derefimpl;
  875. begin
  876. inherited derefimpl;
  877. if assigned(t1) then
  878. t1.derefimpl;
  879. if assigned(t2) then
  880. t2.derefimpl;
  881. end;
  882. function tloopnode.dogetcopy : tnode;
  883. var
  884. p : tloopnode;
  885. begin
  886. p:=tloopnode(inherited dogetcopy);
  887. if assigned(t1) then
  888. p.t1:=t1.dogetcopy
  889. else
  890. p.t1:=nil;
  891. if assigned(t2) then
  892. p.t2:=t2.dogetcopy
  893. else
  894. p.t2:=nil;
  895. p.loopflags:=loopflags;
  896. dogetcopy:=p;
  897. end;
  898. procedure tloopnode.insertintolist(l : tnodelist);
  899. begin
  900. end;
  901. procedure tloopnode.printnodetree(var t:text);
  902. begin
  903. write(t,printnodeindention,'(');
  904. printnodeindent;
  905. printnodeinfo(t);
  906. writeln(t);
  907. printnode(t,left);
  908. printnode(t,right);
  909. printnode(t,t1);
  910. printnode(t,t2);
  911. printnodeunindent;
  912. writeln(t,printnodeindention,')');
  913. end;
  914. function tloopnode.docompare(p: tnode): boolean;
  915. begin
  916. docompare :=
  917. inherited docompare(p) and
  918. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  919. t1.isequal(tloopnode(p).t1) and
  920. t2.isequal(tloopnode(p).t2);
  921. end;
  922. {****************************************************************************
  923. TWHILEREPEATNODE
  924. *****************************************************************************}
  925. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  926. begin
  927. inherited create(whilerepeatn,l,r,nil,nil);
  928. if tab then
  929. include(loopflags, lnf_testatbegin);
  930. if cn then
  931. include(loopflags,lnf_checknegate);
  932. end;
  933. function twhilerepeatnode.pass_typecheck:tnode;
  934. var
  935. t:Tunarynode;
  936. begin
  937. result:=nil;
  938. resultdef:=voidtype;
  939. typecheckpass(left);
  940. { tp procvar support }
  941. maybe_call_procvar(left,true);
  942. {A not node can be removed.}
  943. if left.nodetype=notn then
  944. begin
  945. t:=Tunarynode(left);
  946. left:=Tunarynode(left).left;
  947. t.left:=nil;
  948. t.destroy;
  949. {Symdif operator, in case you are wondering:}
  950. loopflags:=loopflags >< [lnf_checknegate];
  951. end;
  952. { loop instruction }
  953. if assigned(right) then
  954. typecheckpass(right);
  955. set_varstate(left,vs_read,[vsf_must_be_valid]);
  956. if codegenerror then
  957. exit;
  958. if not(is_boolean(left.resultdef)) and
  959. not(is_typeparam(left.resultdef)) then
  960. inserttypeconv(left,pasbool8type);
  961. { Give warnings for code that will never be executed for
  962. while false do }
  963. if (lnf_testatbegin in loopflags) and
  964. (left.nodetype=ordconstn) and
  965. (tordconstnode(left).value.uvalue=0) and
  966. assigned(right) then
  967. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  968. end;
  969. {$ifdef prefetchnext}
  970. type
  971. passignmentquery = ^tassignmentquery;
  972. tassignmentquery = record
  973. towhat: tnode;
  974. source: tassignmentnode;
  975. statementcount: cardinal;
  976. end;
  977. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  978. var
  979. query: passignmentquery absolute arg;
  980. temp, prederef: tnode;
  981. begin
  982. result := fen_norecurse_false;
  983. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  984. inc(query^.statementcount);
  985. { make sure there's something else in the loop besides going to the }
  986. { next item }
  987. if (query^.statementcount > 1) and
  988. (n.nodetype = assignn) then
  989. begin
  990. { skip type conversions of assignment target }
  991. temp := tassignmentnode(n).left;
  992. while (temp.nodetype = typeconvn) do
  993. temp := ttypeconvnode(temp).left;
  994. { assignment to x of the while assigned(x) check? }
  995. if not(temp.isequal(query^.towhat)) then
  996. exit;
  997. { right hand side of assignment dereferenced field of }
  998. { x? (no derefn in case of class) }
  999. temp := tassignmentnode(n).right;
  1000. while (temp.nodetype = typeconvn) do
  1001. temp := ttypeconvnode(temp).left;
  1002. if (temp.nodetype <> subscriptn) then
  1003. exit;
  1004. prederef := tsubscriptnode(temp).left;
  1005. temp := prederef;
  1006. while (temp.nodetype = typeconvn) do
  1007. temp := ttypeconvnode(temp).left;
  1008. { see tests/test/prefetch1.pp }
  1009. if (temp.nodetype = derefn) then
  1010. temp := tderefnode(temp).left
  1011. else
  1012. temp := prederef;
  1013. if temp.isequal(query^.towhat) then
  1014. begin
  1015. query^.source := tassignmentnode(n);
  1016. result := fen_norecurse_true;
  1017. end
  1018. end
  1019. { don't check nodes which can't contain an assignment or whose }
  1020. { final assignment can vary a lot }
  1021. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  1022. result := fen_false;
  1023. end;
  1024. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  1025. var
  1026. query: tassignmentquery;
  1027. begin
  1028. query.towhat := towhat;
  1029. query.source := nil;
  1030. query.statementcount := 0;
  1031. if foreachnodestatic(where,@checkassignment,@query) then
  1032. result := query.source
  1033. else
  1034. result := nil;
  1035. end;
  1036. {$endif prefetchnext}
  1037. function twhilerepeatnode.pass_1 : tnode;
  1038. {$ifdef prefetchnext}
  1039. var
  1040. runnernode, prefetchcode: tnode;
  1041. assignmentnode: tassignmentnode;
  1042. prefetchstatements: tstatementnode;
  1043. {$endif prefetchnext}
  1044. begin
  1045. result:=nil;
  1046. expectloc:=LOC_VOID;
  1047. firstpass(left);
  1048. if codegenerror then
  1049. exit;
  1050. { loop instruction }
  1051. if assigned(right) then
  1052. begin
  1053. firstpass(right);
  1054. if codegenerror then
  1055. exit;
  1056. end;
  1057. {$ifdef prefetchnext}
  1058. { do at the end so all complex typeconversions are already }
  1059. { converted to calln's }
  1060. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1061. (lnf_testatbegin in loopflags) then
  1062. begin
  1063. { get first component of the while check }
  1064. runnernode := left;
  1065. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  1066. runnernode := tunarynode(runnernode).left;
  1067. { is it an assigned(x) check? }
  1068. if ((runnernode.nodetype = inlinen) and
  1069. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  1070. ((runnernode.nodetype = unequaln) and
  1071. (taddnode(runnernode).right.nodetype = niln)) then
  1072. begin
  1073. runnernode := tunarynode(runnernode).left;
  1074. { in case of in_assigned_x, there's a callparan in between }
  1075. if (runnernode.nodetype = callparan) then
  1076. runnernode := tcallparanode(runnernode).left;
  1077. while (runnernode.nodetype = typeconvn) do
  1078. runnernode := ttypeconvnode(runnernode).left;
  1079. { is there an "x := x(^).somefield"? }
  1080. assignmentnode := findassignment(right,runnernode);
  1081. if assigned(assignmentnode) then
  1082. begin
  1083. prefetchcode := internalstatements(prefetchstatements);
  1084. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  1085. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  1086. addstatement(prefetchstatements,right);
  1087. right := prefetchcode;
  1088. typecheckpass(right);
  1089. end;
  1090. end;
  1091. end;
  1092. {$endif prefetchnext}
  1093. end;
  1094. {$ifdef state_tracking}
  1095. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  1096. var condition:Tnode;
  1097. code:Tnode;
  1098. done:boolean;
  1099. value:boolean;
  1100. change:boolean;
  1101. firsttest:boolean;
  1102. factval:Tnode;
  1103. begin
  1104. track_state_pass:=false;
  1105. done:=false;
  1106. firsttest:=true;
  1107. {For repeat until statements, first do a pass through the code.}
  1108. if not(lnf_testatbegin in flags) then
  1109. begin
  1110. code:=right.getcopy;
  1111. if code.track_state_pass(exec_known) then
  1112. track_state_pass:=true;
  1113. code.destroy;
  1114. end;
  1115. repeat
  1116. condition:=left.getcopy;
  1117. code:=right.getcopy;
  1118. change:=condition.track_state_pass(exec_known);
  1119. factval:=aktstate.find_fact(left);
  1120. if factval<>nil then
  1121. begin
  1122. condition.destroy;
  1123. condition:=factval.getcopy;
  1124. change:=true;
  1125. end;
  1126. if change then
  1127. begin
  1128. track_state_pass:=true;
  1129. {Force new resultdef pass.}
  1130. condition.resultdef:=nil;
  1131. do_typecheckpass(condition);
  1132. end;
  1133. if is_constboolnode(condition) then
  1134. begin
  1135. {Try to turn a while loop into a repeat loop.}
  1136. if firsttest then
  1137. exclude(flags,testatbegin);
  1138. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  1139. if value then
  1140. begin
  1141. if code.track_state_pass(exec_known) then
  1142. track_state_pass:=true;
  1143. end
  1144. else
  1145. done:=true;
  1146. end
  1147. else
  1148. begin
  1149. {Remove any modified variables from the state.}
  1150. code.track_state_pass(false);
  1151. done:=true;
  1152. end;
  1153. code.destroy;
  1154. condition.destroy;
  1155. firsttest:=false;
  1156. until done;
  1157. {The loop condition is also known, for example:
  1158. while i<10 do
  1159. begin
  1160. ...
  1161. end;
  1162. When the loop is done, we do know that i<10 = false.
  1163. }
  1164. condition:=left.getcopy;
  1165. if condition.track_state_pass(exec_known) then
  1166. begin
  1167. track_state_pass:=true;
  1168. {Force new resultdef pass.}
  1169. condition.resultdef:=nil;
  1170. do_typecheckpass(condition);
  1171. end;
  1172. if not is_constboolnode(condition) then
  1173. aktstate.store_fact(condition,
  1174. cordconstnode.create(byte(checknegate),pasbool8type,true))
  1175. else
  1176. condition.destroy;
  1177. end;
  1178. {$endif}
  1179. {*****************************************************************************
  1180. TIFNODE
  1181. *****************************************************************************}
  1182. constructor tifnode.create(l,r,_t1 : tnode);
  1183. begin
  1184. inherited create(ifn,l,r,_t1,nil);
  1185. end;
  1186. function tifnode.internalsimplify(warn: boolean) : tnode;
  1187. begin
  1188. result:=nil;
  1189. { optimize constant expressions }
  1190. if (left.nodetype=ordconstn) then
  1191. begin
  1192. if tordconstnode(left).value.uvalue<>0 then
  1193. begin
  1194. if assigned(right) then
  1195. result:=right
  1196. else
  1197. result:=cnothingnode.create;
  1198. right:=nil;
  1199. if warn and assigned(t1) then
  1200. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  1201. end
  1202. else
  1203. begin
  1204. if assigned(t1) then
  1205. result:=t1
  1206. else
  1207. result:=cnothingnode.create;
  1208. t1:=nil;
  1209. if warn and assigned(right) then
  1210. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  1211. end;
  1212. end;
  1213. end;
  1214. function tifnode.simplify(forinline : boolean) : tnode;
  1215. begin
  1216. result:=internalsimplify(false);
  1217. end;
  1218. function tifnode.pass_typecheck:tnode;
  1219. begin
  1220. result:=nil;
  1221. resultdef:=voidtype;
  1222. typecheckpass(left);
  1223. { tp procvar support }
  1224. maybe_call_procvar(left,true);
  1225. { if path }
  1226. if assigned(right) then
  1227. typecheckpass(right);
  1228. { else path }
  1229. if assigned(t1) then
  1230. typecheckpass(t1);
  1231. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1232. if codegenerror then
  1233. exit;
  1234. if not(is_boolean(left.resultdef)) and
  1235. not(is_typeparam(left.resultdef)) then
  1236. inserttypeconv(left,pasbool8type);
  1237. result:=internalsimplify(true);
  1238. end;
  1239. function tifnode.pass_1 : tnode;
  1240. begin
  1241. result:=nil;
  1242. expectloc:=LOC_VOID;
  1243. firstpass(left);
  1244. { if path }
  1245. if assigned(right) then
  1246. firstpass(right);
  1247. { else path }
  1248. if assigned(t1) then
  1249. firstpass(t1);
  1250. { leave if we've got an error in one of the paths }
  1251. if codegenerror then
  1252. exit;
  1253. end;
  1254. {*****************************************************************************
  1255. TFORNODE
  1256. *****************************************************************************}
  1257. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  1258. begin
  1259. inherited create(forn,l,r,_t1,_t2);
  1260. if back then
  1261. include(loopflags,lnf_backward);
  1262. include(loopflags,lnf_testatbegin);
  1263. end;
  1264. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  1265. symbol:Tsym);
  1266. begin
  1267. {If there is a read access, the value of the loop counter is important;
  1268. at the end of the loop the loop variable should contain the value it
  1269. had in the last iteration.}
  1270. if not_type=vn_onwrite then
  1271. begin
  1272. writeln('Loopvar does not matter on exit');
  1273. end
  1274. else
  1275. begin
  1276. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  1277. writeln('Loopvar does matter on exit');
  1278. end;
  1279. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  1280. end;
  1281. function tfornode.simplify(forinline : boolean) : tnode;
  1282. begin
  1283. result:=nil;
  1284. if (t1.nodetype=ordconstn) and
  1285. (right.nodetype=ordconstn) and
  1286. (
  1287. (
  1288. (lnf_backward in loopflags) and
  1289. (tordconstnode(right).value<tordconstnode(t1).value)
  1290. ) or
  1291. (
  1292. not(lnf_backward in loopflags) and
  1293. (tordconstnode(right).value>tordconstnode(t1).value)
  1294. )
  1295. ) then
  1296. result:=cnothingnode.create;
  1297. end;
  1298. function tfornode.wrap_to_value:tnode;
  1299. var
  1300. statements: tstatementnode;
  1301. temp: ttempcreatenode;
  1302. begin
  1303. result:=internalstatements(statements);
  1304. temp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
  1305. addstatement(statements,temp);
  1306. addstatement(statements,cassignmentnode.create(
  1307. ctemprefnode.create(temp),
  1308. t1));
  1309. { create a new for node, it is cheaper than cloning entire loop body }
  1310. addstatement(statements,cfornode.create(
  1311. left,right,ctemprefnode.create(temp),t2,lnf_backward in loopflags));
  1312. addstatement(statements,ctempdeletenode.create(temp));
  1313. { all child nodes are reused }
  1314. left:=nil;
  1315. right:=nil;
  1316. t1:=nil;
  1317. t2:=nil;
  1318. end;
  1319. function tfornode.pass_typecheck:tnode;
  1320. var
  1321. res : tnode;
  1322. begin
  1323. result:=nil;
  1324. resultdef:=voidtype;
  1325. { process the loopvar, from and to, varstates are already set }
  1326. typecheckpass(left);
  1327. typecheckpass(right);
  1328. typecheckpass(t1);
  1329. set_varstate(left,vs_written,[]);
  1330. { loop unrolling }
  1331. if cs_opt_loopunroll in current_settings.optimizerswitches then
  1332. begin
  1333. res:=t2.simplify(false);
  1334. if assigned(res) then
  1335. t2:=res;
  1336. res:=unroll_loop(self);
  1337. if assigned(res) then
  1338. begin
  1339. typecheckpass(res);
  1340. result:=res;
  1341. exit;
  1342. end;
  1343. end;
  1344. { Can we spare the first comparision? }
  1345. if (t1.nodetype=ordconstn) and
  1346. (right.nodetype=ordconstn) and
  1347. (
  1348. (
  1349. (lnf_backward in loopflags) and
  1350. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  1351. ) or
  1352. (
  1353. not(lnf_backward in loopflags) and
  1354. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  1355. )
  1356. ) then
  1357. exclude(loopflags,lnf_testatbegin);
  1358. { Make sure that the loop var and the
  1359. from and to values are compatible types }
  1360. check_ranges(right.fileinfo,right,left.resultdef);
  1361. inserttypeconv(right,left.resultdef);
  1362. check_ranges(t1.fileinfo,t1,left.resultdef);
  1363. inserttypeconv(t1,left.resultdef);
  1364. if assigned(t2) then
  1365. typecheckpass(t2);
  1366. end;
  1367. function tfornode.pass_1 : tnode;
  1368. begin
  1369. result:=nil;
  1370. expectloc:=LOC_VOID;
  1371. firstpass(left);
  1372. firstpass(right);
  1373. firstpass(t1);
  1374. if assigned(t2) then
  1375. firstpass(t2);
  1376. if codegenerror then
  1377. exit;
  1378. { 'to' value must be evaluated once before loop, so its possible modifications
  1379. inside loop body do not affect the number of iterations (see webtbs/tw8883). }
  1380. if not (t1.nodetype in [ordconstn,temprefn]) then
  1381. result:=wrap_to_value;
  1382. end;
  1383. {*****************************************************************************
  1384. TEXITNODE
  1385. *****************************************************************************}
  1386. constructor texitnode.create(l:tnode);
  1387. begin
  1388. inherited create(exitn,l);
  1389. if assigned(left) then
  1390. begin
  1391. { add assignment to funcretsym }
  1392. left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
  1393. left:=cassignmentnode.create(
  1394. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  1395. left);
  1396. end;
  1397. end;
  1398. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1399. begin
  1400. inherited ppuload(t,ppufile);
  1401. end;
  1402. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  1403. begin
  1404. inherited ppuwrite(ppufile);
  1405. end;
  1406. function texitnode.pass_typecheck:tnode;
  1407. var
  1408. newstatement : tstatementnode;
  1409. begin
  1410. result:=nil;
  1411. if assigned(left) then
  1412. begin
  1413. result:=internalstatements(newstatement);
  1414. addstatement(newstatement,left);
  1415. left:=nil;
  1416. addstatement(newstatement,self.getcopy);
  1417. end;
  1418. resultdef:=voidtype;
  1419. end;
  1420. function texitnode.pass_1 : tnode;
  1421. begin
  1422. result:=nil;
  1423. expectloc:=LOC_VOID;
  1424. if assigned(left) then
  1425. internalerror(2011052801);
  1426. end;
  1427. {*****************************************************************************
  1428. TBREAKNODE
  1429. *****************************************************************************}
  1430. constructor tbreaknode.create;
  1431. begin
  1432. inherited create(breakn);
  1433. end;
  1434. function tbreaknode.pass_typecheck:tnode;
  1435. begin
  1436. result:=nil;
  1437. resultdef:=voidtype;
  1438. end;
  1439. function tbreaknode.pass_1 : tnode;
  1440. begin
  1441. result:=nil;
  1442. expectloc:=LOC_VOID;
  1443. end;
  1444. {*****************************************************************************
  1445. TCONTINUENODE
  1446. *****************************************************************************}
  1447. constructor tcontinuenode.create;
  1448. begin
  1449. inherited create(continuen);
  1450. end;
  1451. function tcontinuenode.pass_typecheck:tnode;
  1452. begin
  1453. result:=nil;
  1454. resultdef:=voidtype;
  1455. end;
  1456. function tcontinuenode.pass_1 : tnode;
  1457. begin
  1458. result:=nil;
  1459. expectloc:=LOC_VOID;
  1460. end;
  1461. {*****************************************************************************
  1462. TGOTONODE
  1463. *****************************************************************************}
  1464. constructor tgotonode.create(p : tlabelsym);
  1465. begin
  1466. inherited create(goton);
  1467. exceptionblock:=current_exceptblock;
  1468. labelnode:=nil;
  1469. labelsym:=p;
  1470. end;
  1471. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1472. begin
  1473. inherited ppuload(t,ppufile);
  1474. labelnodeidx:=ppufile.getlongint;
  1475. exceptionblock:=ppufile.getbyte;
  1476. end;
  1477. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  1478. begin
  1479. inherited ppuwrite(ppufile);
  1480. labelnodeidx:=labelnode.ppuidx;
  1481. ppufile.putlongint(labelnodeidx);
  1482. ppufile.putbyte(exceptionblock);
  1483. end;
  1484. procedure tgotonode.buildderefimpl;
  1485. begin
  1486. inherited buildderefimpl;
  1487. end;
  1488. procedure tgotonode.derefimpl;
  1489. begin
  1490. inherited derefimpl;
  1491. end;
  1492. procedure tgotonode.resolveppuidx;
  1493. begin
  1494. labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
  1495. if labelnode.nodetype<>labeln then
  1496. internalerror(200809021);
  1497. end;
  1498. function tgotonode.pass_typecheck:tnode;
  1499. begin
  1500. result:=nil;
  1501. resultdef:=voidtype;
  1502. end;
  1503. function tgotonode.pass_1 : tnode;
  1504. var
  1505. p2 : tprocinfo;
  1506. begin
  1507. result:=nil;
  1508. expectloc:=LOC_VOID;
  1509. { The labelnode can already be set when
  1510. this node was copied }
  1511. if not(assigned(labelnode)) then
  1512. begin
  1513. { inner procedure goto? }
  1514. if assigned(labelsym.code) and
  1515. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1516. { generated by the optimizer? }
  1517. not(assigned(labelsym.owner))) then
  1518. labelnode:=tlabelnode(labelsym.code)
  1519. else if ((m_non_local_goto in current_settings.modeswitches) and
  1520. assigned(labelsym.owner)) or
  1521. { nested exits don't need the non local goto switch }
  1522. (labelsym.realname='$nestedexit') then
  1523. begin
  1524. if current_procinfo.procdef.parast.symtablelevel>labelsym.owner.symtablelevel then
  1525. begin
  1526. { don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
  1527. if exceptionblock>0 then
  1528. CGMessage(cg_e_goto_inout_of_exception_block);
  1529. { goto across procedures using exception?
  1530. this is not allowed because we cannot
  1531. easily unwind the exception frame
  1532. stack
  1533. }
  1534. p2:=current_procinfo;
  1535. while true do
  1536. begin
  1537. if (p2.flags*[pi_needs_implicit_finally,pi_uses_exceptions,pi_has_implicit_finally])<>[] then
  1538. Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
  1539. if labelsym.owner=p2.procdef.localst then
  1540. break;
  1541. p2:=p2.parent
  1542. end;
  1543. if assigned(labelsym.jumpbuf) then
  1544. begin
  1545. labelsym.nonlocal:=true;
  1546. exclude(current_procinfo.procdef.procoptions,po_inline);
  1547. result:=ccallnode.createintern('fpc_longjmp',
  1548. ccallparanode.create(cordconstnode.create(1,sinttype,true),
  1549. ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
  1550. nil)));
  1551. end
  1552. else
  1553. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1554. end
  1555. else
  1556. CGMessage(cg_e_interprocedural_goto_only_to_outer_scope_allowed);
  1557. end
  1558. else
  1559. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  1560. end;
  1561. { check if we don't mess with exception blocks }
  1562. if assigned(labelnode) and
  1563. (exceptionblock<>labelnode.exceptionblock) then
  1564. CGMessage(cg_e_goto_inout_of_exception_block);
  1565. end;
  1566. function tgotonode.dogetcopy : tnode;
  1567. var
  1568. p : tgotonode;
  1569. begin
  1570. p:=tgotonode(inherited dogetcopy);
  1571. p.exceptionblock:=exceptionblock;
  1572. { generate labelnode if not done yet }
  1573. if not(assigned(labelnode)) then
  1574. begin
  1575. if assigned(labelsym) and assigned(labelsym.code) then
  1576. labelnode:=tlabelnode(labelsym.code)
  1577. end;
  1578. p.labelsym:=labelsym;
  1579. if assigned(labelnode) then
  1580. p.labelnode:=tlabelnode(labelnode.dogetcopy)
  1581. else
  1582. begin
  1583. { don't trigger IE when there was already an error, i.e. the
  1584. label is not defined. See tw11763 (PFV) }
  1585. if (errorcount=0) and
  1586. { don't trigger IE if it's a global goto }
  1587. ((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
  1588. not(assigned(labelsym.owner))) then
  1589. internalerror(200610291);
  1590. end;
  1591. result:=p;
  1592. end;
  1593. function tgotonode.docompare(p: tnode): boolean;
  1594. begin
  1595. docompare := false;
  1596. end;
  1597. {*****************************************************************************
  1598. TLABELNODE
  1599. *****************************************************************************}
  1600. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  1601. begin
  1602. inherited create(labeln,l);
  1603. exceptionblock:=current_exceptblock;
  1604. labsym:=alabsym;
  1605. { Register labelnode in labelsym }
  1606. labsym.code:=self;
  1607. end;
  1608. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1609. begin
  1610. inherited ppuload(t,ppufile);
  1611. exceptionblock:=ppufile.getbyte;
  1612. end;
  1613. destructor tlabelnode.destroy;
  1614. begin
  1615. { Remove reference in labelsym, this is to prevent
  1616. goto's to this label }
  1617. if assigned(labsym) and (labsym.code=pointer(self)) then
  1618. labsym.code:=nil;
  1619. inherited destroy;
  1620. end;
  1621. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  1622. begin
  1623. inherited ppuwrite(ppufile);
  1624. ppufile.putbyte(exceptionblock);
  1625. end;
  1626. procedure tlabelnode.buildderefimpl;
  1627. begin
  1628. inherited buildderefimpl;
  1629. end;
  1630. procedure tlabelnode.derefimpl;
  1631. begin
  1632. inherited derefimpl;
  1633. end;
  1634. function tlabelnode.pass_typecheck:tnode;
  1635. begin
  1636. result:=nil;
  1637. { left could still be unassigned }
  1638. if assigned(left) then
  1639. typecheckpass(left);
  1640. resultdef:=voidtype;
  1641. end;
  1642. function tlabelnode.pass_1 : tnode;
  1643. begin
  1644. result:=nil;
  1645. expectloc:=LOC_VOID;
  1646. include(current_procinfo.flags,pi_has_label);
  1647. if assigned(labsym) and labsym.nonlocal then
  1648. begin
  1649. include(current_procinfo.flags,pi_has_interproclabel);
  1650. exclude(current_procinfo.procdef.procoptions,po_inline);
  1651. end;
  1652. if assigned(left) then
  1653. firstpass(left);
  1654. if (m_non_local_goto in current_settings.modeswitches) and
  1655. { the owner can be Nil for internal labels }
  1656. assigned(labsym.owner) and
  1657. (current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
  1658. CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
  1659. end;
  1660. function tlabelnode.dogetcopy : tnode;
  1661. begin
  1662. if not(assigned(copiedto)) then
  1663. copiedto:=tlabelnode(inherited dogetcopy);
  1664. copiedto.exceptionblock:=exceptionblock;
  1665. result:=copiedto;
  1666. end;
  1667. function tlabelnode.docompare(p: tnode): boolean;
  1668. begin
  1669. docompare := false;
  1670. end;
  1671. {*****************************************************************************
  1672. TRAISENODE
  1673. *****************************************************************************}
  1674. constructor traisenode.create(l,taddr,tframe:tnode);
  1675. begin
  1676. inherited create(raisen,l,taddr,tframe);
  1677. end;
  1678. function traisenode.pass_typecheck:tnode;
  1679. begin
  1680. result:=nil;
  1681. resultdef:=voidtype;
  1682. if assigned(left) then
  1683. begin
  1684. { first para must be a _class_ }
  1685. typecheckpass(left);
  1686. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1687. if codegenerror then
  1688. exit;
  1689. if not is_class(left.resultdef) and
  1690. not is_javaclass(left.resultdef) then
  1691. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  1692. { insert needed typeconvs for addr,frame }
  1693. if assigned(right) then
  1694. begin
  1695. { addr }
  1696. typecheckpass(right);
  1697. {$ifdef i8086}
  1698. if current_settings.x86memorymodel in x86_far_code_models then
  1699. inserttypeconv(right,voidfarpointertype)
  1700. else
  1701. inserttypeconv(right,voidnearpointertype);
  1702. {$else i8086}
  1703. inserttypeconv(right,voidpointertype);
  1704. {$endif i8086}
  1705. { frame }
  1706. if assigned(third) then
  1707. begin
  1708. typecheckpass(third);
  1709. inserttypeconv(third,voidpointertype);
  1710. end;
  1711. end;
  1712. end;
  1713. end;
  1714. function traisenode.pass_1 : tnode;
  1715. var
  1716. statements : tstatementnode;
  1717. current_addr : tlabelnode;
  1718. raisenode : tcallnode;
  1719. begin
  1720. result:=internalstatements(statements);
  1721. if assigned(left) then
  1722. begin
  1723. { first para must be a class }
  1724. firstpass(left);
  1725. { insert needed typeconvs for addr,frame }
  1726. if assigned(right) then
  1727. begin
  1728. { addr }
  1729. firstpass(right);
  1730. { frame }
  1731. if assigned(third) then
  1732. firstpass(third)
  1733. else
  1734. third:=cpointerconstnode.Create(0,voidpointertype);
  1735. end
  1736. else
  1737. begin
  1738. third:=cinlinenode.create(in_get_frame,false,nil);
  1739. current_addr:=clabelnode.create(cnothingnode.create,tlabelsym.create('$raiseaddr'));
  1740. addstatement(statements,current_addr);
  1741. right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
  1742. end;
  1743. raisenode:=ccallnode.createintern('fpc_raiseexception',
  1744. ccallparanode.create(third,
  1745. ccallparanode.create(right,
  1746. ccallparanode.create(left,nil)))
  1747. );
  1748. include(raisenode.callnodeflags,cnf_call_never_returns);
  1749. addstatement(statements,raisenode);
  1750. end
  1751. else
  1752. begin
  1753. addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
  1754. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  1755. include(raisenode.callnodeflags,cnf_call_never_returns);
  1756. addstatement(statements,raisenode);
  1757. end;
  1758. left:=nil;
  1759. right:=nil;
  1760. third:=nil;
  1761. end;
  1762. {*****************************************************************************
  1763. TTRYEXCEPTNODE
  1764. *****************************************************************************}
  1765. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1766. begin
  1767. inherited create(tryexceptn,l,r,_t1,nil);
  1768. end;
  1769. function ttryexceptnode.pass_typecheck:tnode;
  1770. begin
  1771. result:=nil;
  1772. typecheckpass(left);
  1773. { on statements }
  1774. if assigned(right) then
  1775. typecheckpass(right);
  1776. { else block }
  1777. if assigned(t1) then
  1778. typecheckpass(t1);
  1779. resultdef:=voidtype;
  1780. end;
  1781. function ttryexceptnode.pass_1 : tnode;
  1782. begin
  1783. result:=nil;
  1784. include(current_procinfo.flags,pi_do_call);
  1785. include(current_procinfo.flags,pi_uses_exceptions);
  1786. expectloc:=LOC_VOID;
  1787. firstpass(left);
  1788. { on statements }
  1789. if assigned(right) then
  1790. firstpass(right);
  1791. { else block }
  1792. if assigned(t1) then
  1793. firstpass(t1);
  1794. inc(current_procinfo.estimatedtempsize,get_jumpbuf_size*2);
  1795. end;
  1796. {*****************************************************************************
  1797. TTRYFINALLYNODE
  1798. *****************************************************************************}
  1799. constructor ttryfinallynode.create(l,r:tnode);
  1800. begin
  1801. inherited create(tryfinallyn,l,r,nil,nil);
  1802. include(current_procinfo.flags,pi_uses_exceptions);
  1803. implicitframe:=false;
  1804. end;
  1805. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1806. begin
  1807. inherited create(tryfinallyn,l,r,_t1,nil);
  1808. implicitframe:=true;
  1809. end;
  1810. function ttryfinallynode.pass_typecheck:tnode;
  1811. begin
  1812. result:=nil;
  1813. include(current_procinfo.flags,pi_do_call);
  1814. resultdef:=voidtype;
  1815. typecheckpass(left);
  1816. // "try block" is "used"? (JM)
  1817. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  1818. typecheckpass(right);
  1819. // "except block" is "used"? (JM)
  1820. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  1821. { special finally block only executed when there was an exception }
  1822. if assigned(t1) then
  1823. begin
  1824. typecheckpass(t1);
  1825. // "finally block" is "used"? (JM)
  1826. set_varstate(t1,vs_readwritten,[vsf_must_be_valid]);
  1827. end;
  1828. end;
  1829. function ttryfinallynode.pass_1 : tnode;
  1830. begin
  1831. result:=nil;
  1832. expectloc:=LOC_VOID;
  1833. firstpass(left);
  1834. firstpass(right);
  1835. if assigned(t1) then
  1836. firstpass(t1);
  1837. inc(current_procinfo.estimatedtempsize,get_jumpbuf_size);
  1838. end;
  1839. function ttryfinallynode.simplify(forinline : boolean): tnode;
  1840. begin
  1841. result:=nil;
  1842. { if the try contains no code, we can kill
  1843. the try and except and return only the
  1844. finally part }
  1845. if has_no_code(left) then
  1846. begin
  1847. result:=right;
  1848. right:=nil;
  1849. end;
  1850. end;
  1851. var
  1852. seq: longint=0;
  1853. function ttryfinallynode.create_finalizer_procdef: tprocdef;
  1854. var
  1855. st:TSymTable;
  1856. checkstack: psymtablestackitem;
  1857. oldsymtablestack: tsymtablestack;
  1858. sym:tprocsym;
  1859. begin
  1860. { get actual procedure symtable (skip withsymtables, etc.) }
  1861. st:=nil;
  1862. checkstack:=symtablestack.stack;
  1863. while assigned(checkstack) do
  1864. begin
  1865. st:=checkstack^.symtable;
  1866. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  1867. break;
  1868. checkstack:=checkstack^.next;
  1869. end;
  1870. { Create a nested procedure, even from main_program_level.
  1871. Furthermore, force procdef and procsym into the same symtable
  1872. (by default, defs are registered with symtablestack.top which may be
  1873. something temporary like exceptsymtable - in that case, procdef can be
  1874. destroyed before procsym, leaving invalid pointers). }
  1875. oldsymtablestack:=symtablestack;
  1876. symtablestack:=nil;
  1877. result:=tprocdef.create(max(normal_function_level,st.symtablelevel)+1);
  1878. symtablestack:=oldsymtablestack;
  1879. st.insertdef(result);
  1880. result.struct:=current_procinfo.procdef.struct;
  1881. { tabstractprocdef constructor sets po_delphi_nested_cc whenever
  1882. nested procvars modeswitch is active. We must be independent of this switch. }
  1883. exclude(result.procoptions,po_delphi_nested_cc);
  1884. result.proctypeoption:=potype_exceptfilter;
  1885. handle_calling_convention(result);
  1886. sym:=tprocsym.create('$fin$'+tostr(seq));
  1887. st.insert(sym);
  1888. inc(seq);
  1889. result.procsym:=sym;
  1890. proc_add_definition(result);
  1891. result.forwarddef:=false;
  1892. result.aliasnames.insert(result.mangledname);
  1893. end;
  1894. {*****************************************************************************
  1895. TONNODE
  1896. *****************************************************************************}
  1897. constructor tonnode.create(l,r:tnode);
  1898. begin
  1899. inherited create(onn,l,r);
  1900. excepTSymtable:=nil;
  1901. excepttype:=nil;
  1902. end;
  1903. destructor tonnode.destroy;
  1904. begin
  1905. { copied nodes don't need to release the symtable }
  1906. if assigned(excepTSymtable) then
  1907. excepTSymtable.free;
  1908. inherited destroy;
  1909. end;
  1910. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1911. begin
  1912. inherited ppuload(t,ppufile);
  1913. excepTSymtable:=nil;
  1914. excepttype:=nil;
  1915. end;
  1916. function tonnode.dogetcopy : tnode;
  1917. var
  1918. n : tonnode;
  1919. begin
  1920. n:=tonnode(inherited dogetcopy);
  1921. if assigned(exceptsymtable) then
  1922. n.exceptsymtable:=exceptsymtable.getcopy
  1923. else
  1924. n.exceptsymtable:=nil;
  1925. n.excepttype:=excepttype;
  1926. result:=n;
  1927. end;
  1928. function tonnode.pass_typecheck:tnode;
  1929. begin
  1930. result:=nil;
  1931. resultdef:=voidtype;
  1932. if not is_class(excepttype) and
  1933. not is_javaclass(excepttype) then
  1934. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1935. if assigned(left) then
  1936. typecheckpass(left);
  1937. if assigned(right) then
  1938. typecheckpass(right);
  1939. end;
  1940. function tonnode.pass_1 : tnode;
  1941. begin
  1942. result:=nil;
  1943. include(current_procinfo.flags,pi_do_call);
  1944. { Loads exception class VMT, therefore may need GOT
  1945. (generic code only; descendants may need to avoid this check) }
  1946. if (cs_create_pic in current_settings.moduleswitches) and
  1947. (tf_pic_uses_got in target_info.flags) then
  1948. include(current_procinfo.flags,pi_needs_got);
  1949. expectloc:=LOC_VOID;
  1950. if assigned(left) then
  1951. firstpass(left);
  1952. if assigned(right) then
  1953. firstpass(right);
  1954. end;
  1955. function tonnode.docompare(p: tnode): boolean;
  1956. begin
  1957. docompare := false;
  1958. end;
  1959. end.