ncgflw.pas 54 KB

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