ncgflw.pas 54 KB

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