ncgflw.pas 54 KB

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