ncgflw.pas 55 KB

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