ncgflw.pas 54 KB

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