ncgflw.pas 52 KB

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