ncgflw.pas 52 KB

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