nflw.pas 76 KB

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