ncgflw.pas 55 KB

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