nflw.pas 75 KB

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