ncgflw.pas 55 KB

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