ncgflw.pas 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate assembler for nodes that influence the flow which are
  4. the same for all (most?) processors
  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 ncgflw;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. aasmbase,node,nflw,ncgutil;
  23. type
  24. tcgwhilerepeatnode = class(twhilerepeatnode)
  25. procedure pass_generate_code;override;
  26. procedure sync_regvars(checkusedregvars: boolean);
  27. usedregvars: tusedregvars;
  28. end;
  29. tcgifnode = class(tifnode)
  30. procedure pass_generate_code;override;
  31. end;
  32. tcgfornode = class(tfornode)
  33. procedure pass_generate_code;override;
  34. procedure sync_regvars(checkusedregvars: boolean);
  35. usedregvars: tusedregvars;
  36. end;
  37. tcgexitnode = class(texitnode)
  38. procedure pass_generate_code;override;
  39. end;
  40. tcgbreaknode = class(tbreaknode)
  41. procedure pass_generate_code;override;
  42. end;
  43. tcgcontinuenode = class(tcontinuenode)
  44. procedure pass_generate_code;override;
  45. end;
  46. tcggotonode = class(tgotonode)
  47. procedure pass_generate_code;override;
  48. end;
  49. tcglabelnode = class(tlabelnode)
  50. private
  51. asmlabel : tasmlabel;
  52. public
  53. function getasmlabel : tasmlabel;
  54. procedure pass_generate_code;override;
  55. end;
  56. tcgraisenode = class(traisenode)
  57. procedure pass_generate_code;override;
  58. end;
  59. tcgtryexceptnode = class(ttryexceptnode)
  60. procedure pass_generate_code;override;
  61. end;
  62. tcgtryfinallynode = class(ttryfinallynode)
  63. procedure pass_generate_code;override;
  64. end;
  65. tcgonnode = class(tonnode)
  66. procedure pass_generate_code;override;
  67. end;
  68. implementation
  69. uses
  70. verbose,globals,systems,globtype,constexp,
  71. symconst,symdef,symsym,aasmtai,aasmdata,aasmcpu,defutil,
  72. procinfo,cgbase,pass_2,parabase,
  73. cpubase,cpuinfo,
  74. nld,ncon,
  75. tgobj,paramgr,
  76. regvars,
  77. cgutils,cgobj
  78. ;
  79. {*****************************************************************************
  80. Second_While_RepeatN
  81. *****************************************************************************}
  82. procedure tcgwhilerepeatnode.sync_regvars(checkusedregvars: boolean);
  83. begin
  84. if (cs_opt_regvar in current_settings.optimizerswitches) and
  85. not(pi_has_goto in current_procinfo.flags) then
  86. begin
  87. if checkusedregvars then
  88. begin
  89. usedregvars.intregvars.init;
  90. usedregvars.fpuregvars.init;
  91. usedregvars.mmregvars.init;
  92. { we have to synchronise both the regvars used in the loop }
  93. { and the ones in the while/until condition }
  94. get_used_regvars(self,usedregvars);
  95. gen_sync_regvars(current_asmdata.CurrAsmList,usedregvars);
  96. end
  97. else
  98. begin
  99. gen_sync_regvars(current_asmdata.CurrAsmList,usedregvars);
  100. usedregvars.intregvars.done;
  101. usedregvars.fpuregvars.done;
  102. usedregvars.mmregvars.done;
  103. end;
  104. end;
  105. end;
  106. procedure tcgwhilerepeatnode.pass_generate_code;
  107. var
  108. lcont,lbreak,lloop,
  109. oldclabel,oldblabel : tasmlabel;
  110. otlabel,oflabel : tasmlabel;
  111. oldflowcontrol : tflowcontrol;
  112. oldexecutionweight : longint;
  113. begin
  114. location_reset(location,LOC_VOID,OS_NO);
  115. current_asmdata.getjumplabel(lloop);
  116. current_asmdata.getjumplabel(lcont);
  117. current_asmdata.getjumplabel(lbreak);
  118. { arrange continue and breaklabels: }
  119. oldflowcontrol:=flowcontrol;
  120. oldclabel:=current_procinfo.CurrContinueLabel;
  121. oldblabel:=current_procinfo.CurrBreakLabel;
  122. include(flowcontrol,fc_inflowcontrol);
  123. sync_regvars(true);
  124. {$ifdef OLDREGVARS}
  125. load_all_regvars(current_asmdata.CurrAsmList);
  126. {$endif OLDREGVARS}
  127. { handling code at the end as it is much more efficient, and makes
  128. while equal to repeat loop, only the end true/false is swapped (PFV) }
  129. if lnf_testatbegin in loopflags then
  130. cg.a_jmp_always(current_asmdata.CurrAsmList,lcont);
  131. if not(cs_opt_size in current_settings.optimizerswitches) then
  132. { align loop target }
  133. current_asmdata.CurrAsmList.concat(Tai_align.Create(current_settings.alignment.loopalign));
  134. cg.a_label(current_asmdata.CurrAsmList,lloop);
  135. current_procinfo.CurrContinueLabel:=lcont;
  136. current_procinfo.CurrBreakLabel:=lbreak;
  137. if assigned(right) then
  138. begin
  139. { calc register weight }
  140. oldexecutionweight:=cg.executionweight;
  141. cg.executionweight:=cg.executionweight*8;
  142. secondpass(right);
  143. cg.executionweight:=oldexecutionweight;
  144. end;
  145. {$ifdef OLDREGVARS}
  146. load_all_regvars(current_asmdata.CurrAsmList);
  147. {$endif OLDREGVARS}
  148. cg.a_label(current_asmdata.CurrAsmList,lcont);
  149. otlabel:=current_procinfo.CurrTrueLabel;
  150. oflabel:=current_procinfo.CurrFalseLabel;
  151. if lnf_checknegate in loopflags then
  152. begin
  153. current_procinfo.CurrTrueLabel:=lbreak;
  154. current_procinfo.CurrFalseLabel:=lloop;
  155. end
  156. else
  157. begin
  158. current_procinfo.CurrTrueLabel:=lloop;
  159. current_procinfo.CurrFalseLabel:=lbreak;
  160. end;
  161. secondpass(left);
  162. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  163. cg.a_label(current_asmdata.CurrAsmList,lbreak);
  164. sync_regvars(false);
  165. current_procinfo.CurrTrueLabel:=otlabel;
  166. current_procinfo.CurrFalseLabel:=oflabel;
  167. current_procinfo.CurrContinueLabel:=oldclabel;
  168. current_procinfo.CurrBreakLabel:=oldblabel;
  169. { a break/continue in a while/repeat block can't be seen outside }
  170. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);
  171. end;
  172. {*****************************************************************************
  173. tcgIFNODE
  174. *****************************************************************************}
  175. procedure tcgifnode.pass_generate_code;
  176. var
  177. hl,otlabel,oflabel : tasmlabel;
  178. oldflowcontrol: tflowcontrol;
  179. oldexecutionweight : longint;
  180. (*
  181. org_regvar_loaded_other,
  182. then_regvar_loaded_other,
  183. else_regvar_loaded_other : regvarother_booleanarray;
  184. org_regvar_loaded_int,
  185. then_regvar_loaded_int,
  186. else_regvar_loaded_int : Tsuperregisterset;
  187. org_list,
  188. then_list,
  189. else_list : TAsmList;
  190. *)
  191. begin
  192. location_reset(location,LOC_VOID,OS_NO);
  193. oldflowcontrol := flowcontrol;
  194. include(flowcontrol,fc_inflowcontrol);
  195. otlabel:=current_procinfo.CurrTrueLabel;
  196. oflabel:=current_procinfo.CurrFalseLabel;
  197. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  198. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  199. secondpass(left);
  200. (*
  201. { save regvars loaded in the beginning so that we can restore them }
  202. { when processing the else-block }
  203. if cs_opt_regvar in current_settings.optimizerswitches then
  204. begin
  205. org_list := current_asmdata.CurrAsmList;
  206. current_asmdata.CurrAsmList := TAsmList.create;
  207. end;
  208. *)
  209. maketojumpbool(current_asmdata.CurrAsmList,left,lr_dont_load_regvars);
  210. (*
  211. if cs_opt_regvar in current_settings.optimizerswitches then
  212. begin
  213. org_regvar_loaded_int := rg.regvar_loaded_int;
  214. org_regvar_loaded_other := rg.regvar_loaded_other;
  215. end;
  216. *)
  217. { determines registers weigths }
  218. oldexecutionweight:=cg.executionweight;
  219. cg.executionweight:=cg.executionweight div 2;
  220. if cg.executionweight<1 then
  221. cg.executionweight:=1;
  222. if assigned(right) then
  223. begin
  224. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  225. secondpass(right);
  226. end;
  227. { save current asmlist (previous instructions + then-block) and }
  228. { loaded regvar state and create new clean ones }
  229. {
  230. if cs_opt_regvar in current_settings.optimizerswitches then
  231. begin
  232. then_regvar_loaded_int := rg.regvar_loaded_int;
  233. then_regvar_loaded_other := rg.regvar_loaded_other;
  234. rg.regvar_loaded_int := org_regvar_loaded_int;
  235. rg.regvar_loaded_other := org_regvar_loaded_other;
  236. then_list := current_asmdata.CurrAsmList;
  237. current_asmdata.CurrAsmList := TAsmList.create;
  238. end;
  239. }
  240. if assigned(t1) then
  241. begin
  242. if assigned(right) then
  243. begin
  244. current_asmdata.getjumplabel(hl);
  245. { do go back to if line !! }
  246. (*
  247. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  248. *)
  249. current_filepos:=current_asmdata.CurrAsmList.getlasttaifilepos^
  250. (*
  251. else
  252. current_filepos:=then_list.getlasttaifilepos^
  253. *)
  254. ;
  255. cg.a_jmp_always(current_asmdata.CurrAsmList,hl);
  256. end;
  257. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  258. secondpass(t1);
  259. (*
  260. { save current asmlist (previous instructions + else-block) }
  261. { and loaded regvar state and create a new clean list }
  262. if cs_opt_regvar in current_settings.optimizerswitches then
  263. begin
  264. { else_regvar_loaded_int := rg.regvar_loaded_int;
  265. else_regvar_loaded_other := rg.regvar_loaded_other;}
  266. else_list := current_asmdata.CurrAsmList;
  267. current_asmdata.CurrAsmList := TAsmList.create;
  268. end;
  269. *)
  270. if assigned(right) then
  271. cg.a_label(current_asmdata.CurrAsmList,hl);
  272. end
  273. else
  274. begin
  275. (*
  276. if cs_opt_regvar in current_settings.optimizerswitches then
  277. begin
  278. { else_regvar_loaded_int := rg.regvar_loaded_int;
  279. else_regvar_loaded_other := rg.regvar_loaded_other;}
  280. else_list := current_asmdata.CurrAsmList;
  281. current_asmdata.CurrAsmList := TAsmList.create;
  282. end;
  283. *)
  284. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  285. end;
  286. if not(assigned(right)) then
  287. begin
  288. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  289. end;
  290. (*
  291. if cs_opt_regvar in current_settings.optimizerswitches then
  292. begin
  293. { add loads of regvars at the end of the then- and else-blocks }
  294. { so that at the end of both blocks the same regvars are loaded }
  295. { no else block? }
  296. if not assigned(t1) then
  297. begin
  298. sync_regvars_int(org_list,then_list,org_regvar_loaded_int,then_regvar_loaded_int);
  299. sync_regvars_other(org_list,then_list,org_regvar_loaded_other,then_regvar_loaded_other);
  300. end
  301. { no then block? }
  302. else if not assigned(right) then
  303. begin
  304. sync_regvars_int(org_list,else_list,org_regvar_loaded_int,else_regvar_loaded_int);
  305. sync_regvars_other(org_list,else_list,org_regvar_loaded_other,else_regvar_loaded_other);
  306. end
  307. { both else and then blocks }
  308. else
  309. begin
  310. sync_regvars_int(then_list,else_list,then_regvar_loaded_int,else_regvar_loaded_int);
  311. sync_regvars_other(then_list,else_list,then_regvar_loaded_other,else_regvar_loaded_other);
  312. end;
  313. { add all lists together }
  314. org_list.concatlist(then_list);
  315. then_list.free;
  316. org_list.concatlist(else_list);
  317. else_list.free;
  318. org_list.concatlist(current_asmdata.CurrAsmList);
  319. current_asmdata.CurrAsmList.free;
  320. current_asmdata.CurrAsmList := org_list;
  321. end;
  322. *)
  323. cg.executionweight:=oldexecutionweight;
  324. current_procinfo.CurrTrueLabel:=otlabel;
  325. current_procinfo.CurrFalseLabel:=oflabel;
  326. flowcontrol := oldflowcontrol + (flowcontrol - [fc_inflowcontrol]);
  327. end;
  328. {*****************************************************************************
  329. SecondFor
  330. *****************************************************************************}
  331. procedure tcgfornode.sync_regvars(checkusedregvars: boolean);
  332. begin
  333. if (cs_opt_regvar in current_settings.optimizerswitches) and
  334. not(pi_has_goto in current_procinfo.flags) then
  335. begin
  336. if checkusedregvars then
  337. begin
  338. usedregvars.intregvars.init;
  339. usedregvars.fpuregvars.init;
  340. usedregvars.mmregvars.init;
  341. { We have to synchronise the loop variable and loop body. }
  342. { The loop end is not necessary, unless it's a register }
  343. { variable. The start value also doesn't matter. }
  344. { loop var }
  345. get_used_regvars(right,usedregvars);
  346. { loop body }
  347. get_used_regvars(t2,usedregvars);
  348. { end value (t1) is not necessary (it cannot be a regvar, }
  349. { see webtbs/tw8883) }
  350. gen_sync_regvars(current_asmdata.CurrAsmList,usedregvars);
  351. end
  352. else
  353. begin
  354. gen_sync_regvars(current_asmdata.CurrAsmList,usedregvars);
  355. usedregvars.intregvars.done;
  356. usedregvars.fpuregvars.done;
  357. usedregvars.mmregvars.done;
  358. end;
  359. end;
  360. end;
  361. procedure tcgfornode.pass_generate_code;
  362. var
  363. l3,oldclabel,oldblabel : tasmlabel;
  364. temptovalue : boolean;
  365. hop : topcg;
  366. hcond : topcmp;
  367. opsize : tcgsize;
  368. count_var_is_signed,do_loopvar_at_end : boolean;
  369. cmp_const:Tconstexprint;
  370. oldflowcontrol : tflowcontrol;
  371. oldexecutionweight : longint;
  372. begin
  373. location_reset(location,LOC_VOID,OS_NO);
  374. oldclabel:=current_procinfo.CurrContinueLabel;
  375. oldblabel:=current_procinfo.CurrBreakLabel;
  376. current_asmdata.getjumplabel(current_procinfo.CurrContinueLabel);
  377. current_asmdata.getjumplabel(current_procinfo.CurrBreakLabel);
  378. current_asmdata.getjumplabel(l3);
  379. { only calculate reference }
  380. opsize := def_cgsize(left.resultdef);
  381. count_var_is_signed:=is_signed(left.resultdef);
  382. { first set the to value
  383. because the count var can be in the expression ! }
  384. do_loopvar_at_end:=(lnf_dont_mind_loopvar_on_exit in loopflags)
  385. { if the loop is unrolled and there is a jump into the loop,
  386. then we can't do the trick with incrementing the loop var only at the
  387. end
  388. }
  389. and not(assigned(entrylabel));
  390. secondpass(t1);
  391. { calculate pointer value and check if changeable and if so }
  392. { load into temporary variable }
  393. if t1.nodetype<>ordconstn then
  394. begin
  395. do_loopvar_at_end:=false;
  396. location_force_reg(current_asmdata.CurrAsmList,t1.location,t1.location.size,false);
  397. temptovalue:=true;
  398. end
  399. else
  400. temptovalue:=false;
  401. { load loopvar, prefer loopvar being a register variable }
  402. oldexecutionweight:=cg.executionweight;
  403. inc(cg.executionweight,8);
  404. secondpass(left);
  405. cg.executionweight:=oldexecutionweight;
  406. { load from value }
  407. secondpass(right);
  408. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,false);
  409. oldflowcontrol:=flowcontrol;
  410. include(flowcontrol,fc_inflowcontrol);
  411. { produce start assignment }
  412. case left.location.loc of
  413. LOC_REFERENCE,
  414. LOC_CREFERENCE :
  415. cg.a_load_loc_ref(current_asmdata.CurrAsmList,left.location.size,right.location,left.location.reference);
  416. LOC_REGISTER,
  417. LOC_CREGISTER:
  418. cg.a_load_loc_reg(current_asmdata.CurrAsmList,left.location.size,right.location,left.location.register);
  419. LOC_SUBSETREG,
  420. LOC_CSUBSETREG :
  421. cg.a_load_loc_subsetreg(current_asmdata.CurrAsmList,left.location.size,right.location,left.location.sreg);
  422. else
  423. internalerror(200501311);
  424. end;
  425. if lnf_backward in loopflags then
  426. if count_var_is_signed then
  427. hcond:=OC_LT
  428. else
  429. hcond:=OC_B
  430. else
  431. if count_var_is_signed then
  432. hcond:=OC_GT
  433. else
  434. hcond:=OC_A;
  435. sync_regvars(true);
  436. {$ifdef OLDREGVARS}
  437. load_all_regvars(current_asmdata.CurrAsmList);
  438. {$endif OLDREGVARS}
  439. if temptovalue then
  440. begin
  441. cg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,opsize,hcond,
  442. t1.location.register,left.location,current_procinfo.CurrBreakLabel);
  443. end
  444. else
  445. begin
  446. if lnf_testatbegin in loopflags then
  447. begin
  448. cg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,opsize,hcond,
  449. tordconstnode(t1).value.svalue,
  450. left.location,current_procinfo.CurrBreakLabel);
  451. end;
  452. end;
  453. {If the loopvar doesn't mind on exit, we avoid this ugly
  454. dec instruction and do the loopvar inc/dec after the loop
  455. body.}
  456. if not do_loopvar_at_end then
  457. begin
  458. if lnf_backward in loopflags then
  459. hop:=OP_ADD
  460. else
  461. hop:=OP_SUB;
  462. cg.a_op_const_loc(current_asmdata.CurrAsmList,hop,1,left.location);
  463. end;
  464. if assigned(entrylabel) then
  465. cg.a_jmp_always(current_asmdata.CurrAsmList,tcglabelnode(entrylabel).getasmlabel);
  466. { align loop target }
  467. if not(cs_opt_size in current_settings.optimizerswitches) then
  468. current_asmdata.CurrAsmList.concat(Tai_align.Create(current_settings.alignment.loopalign));
  469. cg.a_label(current_asmdata.CurrAsmList,l3);
  470. {If the loopvar doesn't mind on exit, we avoid the loopvar inc/dec
  471. after the loop body instead of here.}
  472. if not do_loopvar_at_end then
  473. begin
  474. { according to count direction DEC or INC... }
  475. if lnf_backward in loopflags then
  476. hop:=OP_SUB
  477. else
  478. hop:=OP_ADD;
  479. cg.a_op_const_loc(current_asmdata.CurrAsmList,hop,1,left.location);
  480. end;
  481. if assigned(t2) then
  482. begin
  483. { Calc register weight }
  484. oldexecutionweight:=cg.executionweight;
  485. cg.executionweight:=cg.executionweight*8;
  486. secondpass(t2);
  487. cg.executionweight:=oldexecutionweight;
  488. {$ifdef OLDREGVARS}
  489. load_all_regvars(current_asmdata.CurrAsmList);
  490. {$endif OLDREGVARS}
  491. end;
  492. {If the loopvar doesn't mind on exit, we do the loopvar inc/dec
  493. after the loop body instead of here.}
  494. if do_loopvar_at_end then
  495. begin
  496. { according to count direction DEC or INC... }
  497. if lnf_backward in loopflags then
  498. hop:=OP_SUB
  499. else
  500. hop:=OP_ADD;
  501. cg.a_op_const_loc(current_asmdata.CurrAsmList,hop,1,left.location);
  502. end;
  503. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrContinueLabel);
  504. if do_loopvar_at_end then
  505. if lnf_backward in loopflags then
  506. if count_var_is_signed then
  507. hcond:=OC_GTE
  508. else
  509. hcond:=OC_AE
  510. else
  511. if count_var_is_signed then
  512. hcond:=OC_LTE
  513. else
  514. hcond:=OC_BE
  515. else
  516. if lnf_backward in loopflags then
  517. if count_var_is_signed then
  518. hcond:=OC_GT
  519. else
  520. hcond:=OC_A
  521. else
  522. if count_var_is_signed then
  523. hcond:=OC_LT
  524. else
  525. hcond:=OC_B;
  526. {$ifdef OLDREGVARS}
  527. load_all_regvars(current_asmdata.CurrAsmList);
  528. {$endif OLDREGVARS}
  529. { produce comparison and the corresponding }
  530. { jump }
  531. if temptovalue then
  532. begin
  533. cg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,opsize,hcond,t1.location.register,
  534. left.location,l3);
  535. end
  536. else
  537. begin
  538. cmp_const:=Tordconstnode(t1).value;
  539. if do_loopvar_at_end then
  540. begin
  541. {Watch out for wrap around 255 -> 0.}
  542. {Ugly: This code is way to long... Use tables?}
  543. case opsize of
  544. OS_8:
  545. begin
  546. if lnf_backward in loopflags then
  547. begin
  548. if byte(cmp_const.svalue)=low(byte) then
  549. begin
  550. hcond:=OC_NE;
  551. cmp_const:=high(byte);
  552. end
  553. end
  554. else
  555. begin
  556. if byte(cmp_const.svalue)=high(byte) then
  557. begin
  558. hcond:=OC_NE;
  559. cmp_const:=low(byte);
  560. end
  561. end
  562. end;
  563. OS_16:
  564. begin
  565. if lnf_backward in loopflags then
  566. begin
  567. if word(cmp_const.svalue)=high(word) then
  568. begin
  569. hcond:=OC_NE;
  570. cmp_const:=low(word);
  571. end
  572. end
  573. else
  574. begin
  575. if word(cmp_const.svalue)=low(word) then
  576. begin
  577. hcond:=OC_NE;
  578. cmp_const:=high(word);
  579. end
  580. end
  581. end;
  582. OS_32:
  583. begin
  584. if lnf_backward in loopflags then
  585. begin
  586. if cardinal(cmp_const.svalue)=high(cardinal) then
  587. begin
  588. hcond:=OC_NE;
  589. cmp_const:=low(cardinal);
  590. end
  591. end
  592. else
  593. begin
  594. if cardinal(cmp_const.svalue)=low(cardinal) then
  595. begin
  596. hcond:=OC_NE;
  597. cmp_const:=high(cardinal);
  598. end
  599. end
  600. end;
  601. OS_64:
  602. begin
  603. if lnf_backward in loopflags then
  604. begin
  605. if qword(cmp_const.uvalue)=high(qword) then
  606. begin
  607. hcond:=OC_NE;
  608. cmp_const:=low(qword);
  609. end
  610. end
  611. else
  612. begin
  613. if qword(cmp_const.uvalue)=low(qword) then
  614. begin
  615. hcond:=OC_NE;
  616. cmp_const:=high(qword);
  617. end
  618. end
  619. end;
  620. OS_S8:
  621. begin
  622. if lnf_backward in loopflags then
  623. begin
  624. if shortint(cmp_const.svalue)=low(shortint) then
  625. begin
  626. hcond:=OC_NE;
  627. cmp_const:=high(shortint);
  628. end
  629. end
  630. else
  631. begin
  632. if shortint(cmp_const.svalue)=high(shortint) then
  633. begin
  634. hcond:=OC_NE;
  635. cmp_const:=int64(low(shortint));
  636. end
  637. end
  638. end;
  639. OS_S16:
  640. begin
  641. if lnf_backward in loopflags then
  642. begin
  643. if integer(cmp_const.svalue)=high(smallint) then
  644. begin
  645. hcond:=OC_NE;
  646. cmp_const:=int64(low(smallint));
  647. end
  648. end
  649. else
  650. begin
  651. if integer(cmp_const.svalue)=low(smallint) then
  652. begin
  653. hcond:=OC_NE;
  654. cmp_const:=int64(high(smallint));
  655. end
  656. end
  657. end;
  658. OS_S32:
  659. begin
  660. if lnf_backward in loopflags then
  661. begin
  662. if longint(cmp_const.svalue)=high(longint) then
  663. begin
  664. hcond:=OC_NE;
  665. cmp_const:=int64(low(longint));
  666. end
  667. end
  668. else
  669. begin
  670. if longint(cmp_const.svalue)=low(longint) then
  671. begin
  672. hcond:=OC_NE;
  673. cmp_const:=int64(high(longint));
  674. end
  675. end
  676. end;
  677. OS_S64:
  678. begin
  679. if lnf_backward in loopflags then
  680. begin
  681. if int64(cmp_const.svalue)=high(int64) then
  682. begin
  683. hcond:=OC_NE;
  684. cmp_const:=low(int64);
  685. end
  686. end
  687. else
  688. begin
  689. if int64(cmp_const.svalue)=low(int64) then
  690. begin
  691. hcond:=OC_NE;
  692. cmp_const:=high(int64);
  693. end
  694. end
  695. end;
  696. else
  697. internalerror(200201021);
  698. end;
  699. end;
  700. cg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,opsize,hcond,
  701. aint(cmp_const.svalue),left.location,l3);
  702. end;
  703. { this is the break label: }
  704. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrBreakLabel);
  705. sync_regvars(false);
  706. current_procinfo.CurrContinueLabel:=oldclabel;
  707. current_procinfo.CurrBreakLabel:=oldblabel;
  708. { a break/continue in a while/repeat block can't be seen outside }
  709. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);
  710. end;
  711. {*****************************************************************************
  712. SecondExitN
  713. *****************************************************************************}
  714. procedure tcgexitnode.pass_generate_code;
  715. begin
  716. location_reset(location,LOC_VOID,OS_NO);
  717. include(flowcontrol,fc_exit);
  718. if assigned(left) then
  719. secondpass(left);
  720. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrExitLabel);
  721. end;
  722. {*****************************************************************************
  723. SecondBreakN
  724. *****************************************************************************}
  725. procedure tcgbreaknode.pass_generate_code;
  726. begin
  727. location_reset(location,LOC_VOID,OS_NO);
  728. include(flowcontrol,fc_break);
  729. if current_procinfo.CurrBreakLabel<>nil then
  730. begin
  731. {$ifdef OLDREGVARS}
  732. load_all_regvars(current_asmdata.CurrAsmList);
  733. {$endif OLDREGVARS}
  734. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrBreakLabel)
  735. end
  736. else
  737. CGMessage(cg_e_break_not_allowed);
  738. end;
  739. {*****************************************************************************
  740. SecondContinueN
  741. *****************************************************************************}
  742. procedure tcgcontinuenode.pass_generate_code;
  743. begin
  744. location_reset(location,LOC_VOID,OS_NO);
  745. include(flowcontrol,fc_continue);
  746. if current_procinfo.CurrContinueLabel<>nil then
  747. begin
  748. {$ifdef OLDREGVARS}
  749. load_all_regvars(current_asmdata.CurrAsmList);
  750. {$endif OLDREGVARS}
  751. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrContinueLabel)
  752. end
  753. else
  754. CGMessage(cg_e_continue_not_allowed);
  755. end;
  756. {*****************************************************************************
  757. SecondGoto
  758. *****************************************************************************}
  759. procedure tcggotonode.pass_generate_code;
  760. begin
  761. location_reset(location,LOC_VOID,OS_NO);
  762. include(flowcontrol,fc_gotolabel);
  763. {$ifdef OLDREGVARS}
  764. load_all_regvars(current_asmdata.CurrAsmList);
  765. {$endif OLDREGVARS}
  766. cg.a_jmp_always(current_asmdata.CurrAsmList,tcglabelnode(labelnode).getasmlabel)
  767. end;
  768. {*****************************************************************************
  769. SecondLabel
  770. *****************************************************************************}
  771. function tcglabelnode.getasmlabel : tasmlabel;
  772. begin
  773. if not(assigned(asmlabel)) then
  774. { labsym is not set in inlined procedures, but since assembler }
  775. { routines can't be inlined, that shouldn't matter }
  776. if assigned(labsym) and
  777. labsym.nonlocal then
  778. current_asmdata.getglobaljumplabel(asmlabel)
  779. else
  780. current_asmdata.getjumplabel(asmlabel);
  781. result:=asmlabel
  782. end;
  783. procedure tcglabelnode.pass_generate_code;
  784. begin
  785. location_reset(location,LOC_VOID,OS_NO);
  786. include(flowcontrol,fc_gotolabel);
  787. {$ifdef OLDREGVARS}
  788. load_all_regvars(current_asmdata.CurrAsmList);
  789. {$endif OLDREGVARS}
  790. cg.a_label(current_asmdata.CurrAsmList,getasmlabel);
  791. { Write also extra label if this label was referenced from
  792. assembler block }
  793. if assigned(labsym) and
  794. assigned(labsym.asmblocklabel) then
  795. cg.a_label(current_asmdata.CurrAsmList,labsym.asmblocklabel);
  796. secondpass(left);
  797. end;
  798. {*****************************************************************************
  799. SecondRaise
  800. *****************************************************************************}
  801. procedure tcgraisenode.pass_generate_code;
  802. var
  803. a : tasmlabel;
  804. href2: treference;
  805. paraloc1,paraloc2,paraloc3 : tcgpara;
  806. begin
  807. paraloc1.init;
  808. paraloc2.init;
  809. paraloc3.init;
  810. paramanager.getintparaloc(pocall_default,1,paraloc1);
  811. paramanager.getintparaloc(pocall_default,2,paraloc2);
  812. paramanager.getintparaloc(pocall_default,3,paraloc3);
  813. location_reset(location,LOC_VOID,OS_NO);
  814. if assigned(left) then
  815. begin
  816. { multiple parameters? }
  817. if assigned(right) then
  818. begin
  819. { frame tree }
  820. if assigned(third) then
  821. secondpass(third);
  822. secondpass(right);
  823. end;
  824. secondpass(left);
  825. if codegenerror then
  826. exit;
  827. { Push parameters }
  828. if assigned(right) then
  829. begin
  830. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc3);
  831. { frame tree }
  832. if assigned(third) then
  833. cg.a_param_loc(current_asmdata.CurrAsmList,third.location,paraloc3)
  834. else
  835. cg.a_param_const(current_asmdata.CurrAsmList,OS_INT,0,paraloc3);
  836. { push address }
  837. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  838. cg.a_param_loc(current_asmdata.CurrAsmList,right.location,paraloc2);
  839. end
  840. else
  841. begin
  842. { get current address }
  843. current_asmdata.getaddrlabel(a);
  844. cg.a_label(current_asmdata.CurrAsmList,a);
  845. reference_reset_symbol(href2,a,0,1);
  846. { push current frame }
  847. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc3);
  848. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,NR_FRAME_POINTER_REG,paraloc3);
  849. { push current address }
  850. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  851. if target_info.system <> system_powerpc_macos then
  852. cg.a_paramaddr_ref(current_asmdata.CurrAsmList,href2,paraloc2)
  853. else
  854. cg.a_param_const(current_asmdata.CurrAsmList,OS_INT,0,paraloc2);
  855. end;
  856. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  857. cg.a_param_loc(current_asmdata.CurrAsmList,left.location,paraloc1);
  858. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  859. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  860. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc3);
  861. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  862. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RAISEEXCEPTION',false);
  863. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  864. end
  865. else
  866. begin
  867. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  868. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK',false);
  869. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE',false);
  870. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  871. end;
  872. paraloc1.done;
  873. paraloc2.done;
  874. paraloc3.done;
  875. end;
  876. {*****************************************************************************
  877. SecondTryExcept
  878. *****************************************************************************}
  879. var
  880. endexceptlabel : tasmlabel;
  881. { does the necessary things to clean up the object stack }
  882. { in the except block }
  883. procedure cleanupobjectstack;
  884. var
  885. paraloc1 : tcgpara;
  886. begin
  887. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  888. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPOBJECTSTACK',false);
  889. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  890. paraloc1.init;
  891. paramanager.getintparaloc(pocall_default,1,paraloc1);
  892. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  893. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  894. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  895. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  896. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DESTROYEXCEPTION',false);
  897. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  898. paraloc1.done;
  899. end;
  900. procedure tcgtryexceptnode.pass_generate_code;
  901. var
  902. exceptlabel,doexceptlabel,oldendexceptlabel,
  903. lastonlabel,
  904. exitexceptlabel,
  905. continueexceptlabel,
  906. breakexceptlabel,
  907. exittrylabel,
  908. continuetrylabel,
  909. breaktrylabel,
  910. doobjectdestroy,
  911. doobjectdestroyandreraise,
  912. oldCurrExitLabel,
  913. oldContinueLabel,
  914. oldBreakLabel : tasmlabel;
  915. oldflowcontrol,tryflowcontrol,
  916. exceptflowcontrol : tflowcontrol;
  917. destroytemps,
  918. excepttemps : texceptiontemps;
  919. paraloc1 : tcgpara;
  920. label
  921. errorexit;
  922. begin
  923. location_reset(location,LOC_VOID,OS_NO);
  924. oldflowcontrol:=flowcontrol;
  925. flowcontrol:=[fc_inflowcontrol];
  926. { this can be called recursivly }
  927. oldBreakLabel:=nil;
  928. oldContinueLabel:=nil;
  929. oldendexceptlabel:=endexceptlabel;
  930. { save the old labels for control flow statements }
  931. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  932. if assigned(current_procinfo.CurrBreakLabel) then
  933. begin
  934. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  935. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  936. end;
  937. { get new labels for the control flow statements }
  938. current_asmdata.getjumplabel(exittrylabel);
  939. current_asmdata.getjumplabel(exitexceptlabel);
  940. if assigned(current_procinfo.CurrBreakLabel) then
  941. begin
  942. current_asmdata.getjumplabel(breaktrylabel);
  943. current_asmdata.getjumplabel(continuetrylabel);
  944. current_asmdata.getjumplabel(breakexceptlabel);
  945. current_asmdata.getjumplabel(continueexceptlabel);
  946. end;
  947. current_asmdata.getjumplabel(exceptlabel);
  948. current_asmdata.getjumplabel(doexceptlabel);
  949. current_asmdata.getjumplabel(endexceptlabel);
  950. current_asmdata.getjumplabel(lastonlabel);
  951. get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  952. new_exception(current_asmdata.CurrAsmList,excepttemps,exceptlabel);
  953. { try block }
  954. { set control flow labels for the try block }
  955. current_procinfo.CurrExitLabel:=exittrylabel;
  956. if assigned(oldBreakLabel) then
  957. begin
  958. current_procinfo.CurrContinueLabel:=continuetrylabel;
  959. current_procinfo.CurrBreakLabel:=breaktrylabel;
  960. end;
  961. flowcontrol:=[fc_inflowcontrol];
  962. secondpass(left);
  963. tryflowcontrol:=flowcontrol;
  964. if codegenerror then
  965. goto errorexit;
  966. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  967. free_exception(current_asmdata.CurrAsmList, excepttemps, 0, endexceptlabel, false);
  968. cg.a_label(current_asmdata.CurrAsmList,doexceptlabel);
  969. { set control flow labels for the except block }
  970. { and the on statements }
  971. current_procinfo.CurrExitLabel:=exitexceptlabel;
  972. if assigned(oldBreakLabel) then
  973. begin
  974. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  975. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  976. end;
  977. flowcontrol:=[fc_inflowcontrol];
  978. { on statements }
  979. if assigned(right) then
  980. secondpass(right);
  981. cg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  982. { default handling except handling }
  983. if assigned(t1) then
  984. begin
  985. { FPC_CATCHES must be called with
  986. 'default handler' flag (=-1)
  987. }
  988. paraloc1.init;
  989. paramanager.getintparaloc(pocall_default,1,paraloc1);
  990. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  991. cg.a_param_const(current_asmdata.CurrAsmList,OS_ADDR,-1,paraloc1);
  992. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  993. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  994. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CATCHES',false);
  995. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  996. paraloc1.done;
  997. { the destruction of the exception object must be also }
  998. { guarded by an exception frame }
  999. current_asmdata.getjumplabel(doobjectdestroy);
  1000. current_asmdata.getjumplabel(doobjectdestroyandreraise);
  1001. get_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  1002. new_exception(current_asmdata.CurrAsmList,destroytemps,doobjectdestroyandreraise);
  1003. { here we don't have to reset flowcontrol }
  1004. { the default and on flowcontrols are handled equal }
  1005. secondpass(t1);
  1006. exceptflowcontrol:=flowcontrol;
  1007. cg.a_label(current_asmdata.CurrAsmList,doobjectdestroyandreraise);
  1008. free_exception(current_asmdata.CurrAsmList,destroytemps,0,doobjectdestroy,false);
  1009. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1010. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPSECONDOBJECTSTACK',false);
  1011. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1012. paraloc1.init;
  1013. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1014. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  1015. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR, NR_FUNCTION_RESULT_REG, paraloc1);
  1016. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  1017. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1018. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DESTROYEXCEPTION',false);
  1019. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1020. paraloc1.done;
  1021. { we don't need to restore esi here because reraise never }
  1022. { returns }
  1023. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE',false);
  1024. cg.a_label(current_asmdata.CurrAsmList,doobjectdestroy);
  1025. cleanupobjectstack;
  1026. unget_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  1027. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  1028. end
  1029. else
  1030. begin
  1031. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE',false);
  1032. exceptflowcontrol:=flowcontrol;
  1033. end;
  1034. if fc_exit in exceptflowcontrol then
  1035. begin
  1036. { do some magic for exit in the try block }
  1037. cg.a_label(current_asmdata.CurrAsmList,exitexceptlabel);
  1038. { we must also destroy the address frame which guards }
  1039. { exception object }
  1040. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1041. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK',false);
  1042. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1043. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1044. cleanupobjectstack;
  1045. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  1046. end;
  1047. if fc_break in exceptflowcontrol then
  1048. begin
  1049. cg.a_label(current_asmdata.CurrAsmList,breakexceptlabel);
  1050. { we must also destroy the address frame which guards }
  1051. { exception object }
  1052. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1053. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK',false);
  1054. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1055. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1056. cleanupobjectstack;
  1057. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  1058. end;
  1059. if fc_continue in exceptflowcontrol then
  1060. begin
  1061. cg.a_label(current_asmdata.CurrAsmList,continueexceptlabel);
  1062. { we must also destroy the address frame which guards }
  1063. { exception object }
  1064. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1065. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK',false);
  1066. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1067. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1068. cleanupobjectstack;
  1069. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  1070. end;
  1071. if fc_exit in tryflowcontrol then
  1072. begin
  1073. { do some magic for exit in the try block }
  1074. cg.a_label(current_asmdata.CurrAsmList,exittrylabel);
  1075. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1076. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK',false);
  1077. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1078. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1079. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  1080. end;
  1081. if fc_break in tryflowcontrol then
  1082. begin
  1083. cg.a_label(current_asmdata.CurrAsmList,breaktrylabel);
  1084. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1085. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK',false);
  1086. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1087. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1088. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  1089. end;
  1090. if fc_continue in tryflowcontrol then
  1091. begin
  1092. cg.a_label(current_asmdata.CurrAsmList,continuetrylabel);
  1093. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1094. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK',false);
  1095. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1096. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1097. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  1098. end;
  1099. unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1100. cg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  1101. errorexit:
  1102. { restore all saved labels }
  1103. endexceptlabel:=oldendexceptlabel;
  1104. { restore the control flow labels }
  1105. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  1106. if assigned(oldBreakLabel) then
  1107. begin
  1108. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  1109. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  1110. end;
  1111. { return all used control flow statements }
  1112. flowcontrol:=oldflowcontrol+(exceptflowcontrol +
  1113. tryflowcontrol - [fc_inflowcontrol]);
  1114. end;
  1115. procedure tcgonnode.pass_generate_code;
  1116. var
  1117. nextonlabel,
  1118. exitonlabel,
  1119. continueonlabel,
  1120. breakonlabel,
  1121. oldCurrExitLabel,
  1122. oldContinueLabel,
  1123. doobjectdestroyandreraise,
  1124. doobjectdestroy,
  1125. oldBreakLabel : tasmlabel;
  1126. oldflowcontrol : tflowcontrol;
  1127. excepttemps : texceptiontemps;
  1128. exceptref,
  1129. href2: treference;
  1130. paraloc1 : tcgpara;
  1131. exceptvarsym : tlocalvarsym;
  1132. begin
  1133. paraloc1.init;
  1134. location_reset(location,LOC_VOID,OS_NO);
  1135. oldflowcontrol:=flowcontrol;
  1136. flowcontrol:=[fc_inflowcontrol];
  1137. current_asmdata.getjumplabel(nextonlabel);
  1138. { send the vmt parameter }
  1139. reference_reset_symbol(href2,current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname),0,sizeof(pint));
  1140. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1141. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  1142. cg.a_paramaddr_ref(current_asmdata.CurrAsmList,href2,paraloc1);
  1143. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  1144. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1145. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CATCHES',false);
  1146. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1147. { is it this catch? No. go to next onlabel }
  1148. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_EQ,0,NR_FUNCTION_RESULT_REG,nextonlabel);
  1149. { Retrieve exception variable }
  1150. if assigned(excepTSymtable) then
  1151. exceptvarsym:=tlocalvarsym(excepTSymtable.SymList[0])
  1152. else
  1153. exceptvarsym:=nil;
  1154. if assigned(exceptvarsym) then
  1155. begin
  1156. exceptvarsym.localloc.loc:=LOC_REFERENCE;
  1157. exceptvarsym.localloc.size:=OS_ADDR;
  1158. tg.GetLocal(current_asmdata.CurrAsmList,sizeof(pint),voidpointertype,exceptvarsym.localloc.reference);
  1159. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,exceptvarsym.localloc.reference);
  1160. end
  1161. else
  1162. begin
  1163. tg.GetTemp(current_asmdata.CurrAsmList,sizeof(pint),sizeof(pint),tt_normal,exceptref);
  1164. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,exceptref);
  1165. end;
  1166. { in the case that another exception is risen
  1167. we've to destroy the old one }
  1168. current_asmdata.getjumplabel(doobjectdestroyandreraise);
  1169. { call setjmp, and jump to finally label on non-zero result }
  1170. get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1171. new_exception(current_asmdata.CurrAsmList,excepttemps,doobjectdestroyandreraise);
  1172. oldBreakLabel:=nil;
  1173. oldContinueLabel:=nil;
  1174. if assigned(right) then
  1175. begin
  1176. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  1177. current_asmdata.getjumplabel(exitonlabel);
  1178. current_procinfo.CurrExitLabel:=exitonlabel;
  1179. if assigned(current_procinfo.CurrBreakLabel) then
  1180. begin
  1181. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  1182. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  1183. current_asmdata.getjumplabel(breakonlabel);
  1184. current_asmdata.getjumplabel(continueonlabel);
  1185. current_procinfo.CurrContinueLabel:=continueonlabel;
  1186. current_procinfo.CurrBreakLabel:=breakonlabel;
  1187. end;
  1188. secondpass(right);
  1189. end;
  1190. current_asmdata.getjumplabel(doobjectdestroy);
  1191. cg.a_label(current_asmdata.CurrAsmList,doobjectdestroyandreraise);
  1192. free_exception(current_asmdata.CurrAsmList,excepttemps,0,doobjectdestroy,false);
  1193. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1194. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPSECONDOBJECTSTACK',false);
  1195. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1196. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1197. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  1198. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR, NR_FUNCTION_RESULT_REG, paraloc1);
  1199. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  1200. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1201. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DESTROYEXCEPTION',false);
  1202. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1203. { we don't need to store/restore registers here because reraise never
  1204. returns }
  1205. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE',false);
  1206. cg.a_label(current_asmdata.CurrAsmList,doobjectdestroy);
  1207. cleanupobjectstack;
  1208. { clear some stuff }
  1209. if assigned(exceptvarsym) then
  1210. begin
  1211. tg.UngetLocal(current_asmdata.CurrAsmList,exceptvarsym.localloc.reference);
  1212. exceptvarsym.localloc.loc:=LOC_INVALID;
  1213. end
  1214. else
  1215. tg.Ungettemp(current_asmdata.CurrAsmList,exceptref);
  1216. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  1217. if assigned(right) then
  1218. begin
  1219. { special handling for control flow instructions }
  1220. if fc_exit in flowcontrol then
  1221. begin
  1222. { the address and object pop does secondtryexcept }
  1223. cg.a_label(current_asmdata.CurrAsmList,exitonlabel);
  1224. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  1225. end;
  1226. if fc_break in flowcontrol then
  1227. begin
  1228. { the address and object pop does secondtryexcept }
  1229. cg.a_label(current_asmdata.CurrAsmList,breakonlabel);
  1230. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  1231. end;
  1232. if fc_continue in flowcontrol then
  1233. begin
  1234. { the address and object pop does secondtryexcept }
  1235. cg.a_label(current_asmdata.CurrAsmList,continueonlabel);
  1236. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  1237. end;
  1238. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  1239. if assigned(oldBreakLabel) then
  1240. begin
  1241. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  1242. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  1243. end;
  1244. end;
  1245. unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1246. cg.a_label(current_asmdata.CurrAsmList,nextonlabel);
  1247. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_inflowcontrol]);
  1248. paraloc1.done;
  1249. { next on node }
  1250. if assigned(left) then
  1251. secondpass(left);
  1252. end;
  1253. {*****************************************************************************
  1254. SecondTryFinally
  1255. *****************************************************************************}
  1256. procedure tcgtryfinallynode.pass_generate_code;
  1257. var
  1258. reraiselabel,
  1259. finallylabel,
  1260. endfinallylabel,
  1261. exitfinallylabel,
  1262. continuefinallylabel,
  1263. breakfinallylabel,
  1264. oldCurrExitLabel,
  1265. oldContinueLabel,
  1266. oldBreakLabel : tasmlabel;
  1267. oldflowcontrol,tryflowcontrol : tflowcontrol;
  1268. decconst : longint;
  1269. excepttemps : texceptiontemps;
  1270. begin
  1271. location_reset(location,LOC_VOID,OS_NO);
  1272. { check if child nodes do a break/continue/exit }
  1273. oldflowcontrol:=flowcontrol;
  1274. flowcontrol:=[fc_inflowcontrol];
  1275. current_asmdata.getjumplabel(finallylabel);
  1276. current_asmdata.getjumplabel(endfinallylabel);
  1277. current_asmdata.getjumplabel(reraiselabel);
  1278. { the finally block must catch break, continue and exit }
  1279. { statements }
  1280. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  1281. if implicitframe then
  1282. exitfinallylabel:=finallylabel
  1283. else
  1284. current_asmdata.getjumplabel(exitfinallylabel);
  1285. current_procinfo.CurrExitLabel:=exitfinallylabel;
  1286. if assigned(current_procinfo.CurrBreakLabel) then
  1287. begin
  1288. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  1289. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  1290. if implicitframe then
  1291. begin
  1292. breakfinallylabel:=finallylabel;
  1293. continuefinallylabel:=finallylabel;
  1294. end
  1295. else
  1296. begin
  1297. current_asmdata.getjumplabel(breakfinallylabel);
  1298. current_asmdata.getjumplabel(continuefinallylabel);
  1299. end;
  1300. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  1301. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  1302. end;
  1303. { call setjmp, and jump to finally label on non-zero result }
  1304. get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1305. new_exception(current_asmdata.CurrAsmList,excepttemps,finallylabel);
  1306. { try code }
  1307. if assigned(left) then
  1308. begin
  1309. secondpass(left);
  1310. tryflowcontrol:=flowcontrol;
  1311. if codegenerror then
  1312. exit;
  1313. end;
  1314. cg.a_label(current_asmdata.CurrAsmList,finallylabel);
  1315. { just free the frame information }
  1316. free_exception(current_asmdata.CurrAsmList,excepttemps,1,finallylabel,true);
  1317. { finally code }
  1318. flowcontrol:=[fc_inflowcontrol];
  1319. secondpass(right);
  1320. { goto is allowed if it stays inside the finally block,
  1321. this is checked using the exception block number }
  1322. if (flowcontrol-[fc_gotolabel])<>[fc_inflowcontrol] then
  1323. CGMessage(cg_e_control_flow_outside_finally);
  1324. if codegenerror then
  1325. exit;
  1326. { the value should now be in the exception handler }
  1327. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1328. if implicitframe then
  1329. begin
  1330. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,endfinallylabel);
  1331. { finally code only needed to be executed on exception }
  1332. flowcontrol:=[fc_inflowcontrol];
  1333. secondpass(t1);
  1334. if flowcontrol<>[fc_inflowcontrol] then
  1335. CGMessage(cg_e_control_flow_outside_finally);
  1336. if codegenerror then
  1337. exit;
  1338. {$if defined(x86) or defined(arm)}
  1339. if (target_info.system in system_all_windows) and
  1340. (current_procinfo.procdef.proccalloption=pocall_safecall) then
  1341. begin
  1342. { Remove and destroy the last exception object }
  1343. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPOBJECTSTACK',false);
  1344. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DESTROYEXCEPTION',false);
  1345. { Set return value of safecall procedure to indicate exception. }
  1346. { Exception will be raised after procedure exit based on return value }
  1347. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_ADDR,aint($8000FFFF),NR_FUNCTION_RETURN_REG);
  1348. end
  1349. else
  1350. {$endif}
  1351. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE',false);
  1352. end
  1353. else
  1354. begin
  1355. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,endfinallylabel);
  1356. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,1,NR_FUNCTION_RESULT_REG);
  1357. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,reraiselabel);
  1358. if fc_exit in tryflowcontrol then
  1359. begin
  1360. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,1,NR_FUNCTION_RESULT_REG);
  1361. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldCurrExitLabel);
  1362. decconst:=1;
  1363. end
  1364. else
  1365. decconst:=2;
  1366. if fc_break in tryflowcontrol then
  1367. begin
  1368. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,decconst,NR_FUNCTION_RESULT_REG);
  1369. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldBreakLabel);
  1370. decconst:=1;
  1371. end
  1372. else
  1373. inc(decconst);
  1374. if fc_continue in tryflowcontrol then
  1375. begin
  1376. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,decconst,NR_FUNCTION_RESULT_REG);
  1377. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldContinueLabel);
  1378. end;
  1379. cg.a_label(current_asmdata.CurrAsmList,reraiselabel);
  1380. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE',false);
  1381. { do some magic for exit,break,continue in the try block }
  1382. if fc_exit in tryflowcontrol then
  1383. begin
  1384. cg.a_label(current_asmdata.CurrAsmList,exitfinallylabel);
  1385. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1386. cg.g_exception_reason_save_const(current_asmdata.CurrAsmList,excepttemps.reasonbuf,2);
  1387. cg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  1388. end;
  1389. if fc_break in tryflowcontrol then
  1390. begin
  1391. cg.a_label(current_asmdata.CurrAsmList,breakfinallylabel);
  1392. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1393. cg.g_exception_reason_save_const(current_asmdata.CurrAsmList,excepttemps.reasonbuf,3);
  1394. cg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  1395. end;
  1396. if fc_continue in tryflowcontrol then
  1397. begin
  1398. cg.a_label(current_asmdata.CurrAsmList,continuefinallylabel);
  1399. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1400. cg.g_exception_reason_save_const(current_asmdata.CurrAsmList,excepttemps.reasonbuf,4);
  1401. cg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  1402. end;
  1403. end;
  1404. unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1405. cg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  1406. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  1407. if assigned(current_procinfo.CurrBreakLabel) then
  1408. begin
  1409. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  1410. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  1411. end;
  1412. flowcontrol:=oldflowcontrol+(tryflowcontrol-[fc_inflowcontrol]);
  1413. end;
  1414. begin
  1415. cwhilerepeatnode:=tcgwhilerepeatnode;
  1416. cifnode:=tcgifnode;
  1417. cfornode:=tcgfornode;
  1418. cexitnode:=tcgexitnode;
  1419. cbreaknode:=tcgbreaknode;
  1420. ccontinuenode:=tcgcontinuenode;
  1421. cgotonode:=tcggotonode;
  1422. clabelnode:=tcglabelnode;
  1423. craisenode:=tcgraisenode;
  1424. ctryexceptnode:=tcgtryexceptnode;
  1425. ctryfinallynode:=tcgtryfinallynode;
  1426. connode:=tcgonnode;
  1427. end.