ncgflw.pas 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate assembler for nodes that influence the flow which are
  5. the same for all (most?) processors
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit ncgflw;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. node,nflw;
  24. type
  25. tcgwhilerepeatnode = class(twhilerepeatnode)
  26. procedure pass_2;override;
  27. end;
  28. tcgifnode = class(tifnode)
  29. procedure pass_2;override;
  30. end;
  31. tcgfornode = class(tfornode)
  32. procedure pass_2;override;
  33. end;
  34. tcgexitnode = class(texitnode)
  35. procedure pass_2;override;
  36. end;
  37. tcgbreaknode = class(tbreaknode)
  38. procedure pass_2;override;
  39. end;
  40. tcgcontinuenode = class(tcontinuenode)
  41. procedure pass_2;override;
  42. end;
  43. tcggotonode = class(tgotonode)
  44. procedure pass_2;override;
  45. end;
  46. tcglabelnode = class(tlabelnode)
  47. procedure pass_2;override;
  48. end;
  49. tcgfailnode = class(tfailnode)
  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,symsym,aasmbase,aasmtai,aasmcpu,defutil,
  68. cginfo,cgbase,pass_2,
  69. cpubase,cpuinfo,
  70. nld,ncon,
  71. ncgutil,
  72. tgobj,rgobj,paramgr,
  73. regvars,cgobj,cgcpu
  74. {$ifndef cpu64bit}
  75. ,cg64f32
  76. {$endif cpu64bit}
  77. ;
  78. const
  79. EXCEPT_BUF_SIZE = 12;
  80. {*****************************************************************************
  81. Second_While_RepeatN
  82. *****************************************************************************}
  83. procedure tcgwhilerepeatnode.pass_2;
  84. var
  85. lcont,lbreak,lloop,
  86. oldclabel,oldblabel : tasmlabel;
  87. otlabel,oflabel : tasmlabel;
  88. begin
  89. objectlibrary.getlabel(lloop);
  90. objectlibrary.getlabel(lcont);
  91. objectlibrary.getlabel(lbreak);
  92. { arrange continue and breaklabels: }
  93. oldclabel:=aktcontinuelabel;
  94. oldblabel:=aktbreaklabel;
  95. load_all_regvars(exprasmlist);
  96. { handling code at the end as it is much more efficient, and makes
  97. while equal to repeat loop, only the end true/false is swapped (PFV) }
  98. if lnf_testatbegin in loopflags then
  99. cg.a_jmp_always(exprasmlist,lcont);
  100. if not(cs_littlesize in aktglobalswitches) then
  101. { align loop target }
  102. exprasmList.concat(Tai_align.Create(aktalignment.loopalign));
  103. cg.a_label(exprasmlist,lloop);
  104. aktcontinuelabel:=lcont;
  105. aktbreaklabel:=lbreak;
  106. rg.cleartempgen;
  107. if assigned(right) then
  108. secondpass(right);
  109. load_all_regvars(exprasmlist);
  110. cg.a_label(exprasmlist,lcont);
  111. otlabel:=truelabel;
  112. oflabel:=falselabel;
  113. if lnf_checknegate in loopflags then
  114. begin
  115. truelabel:=lbreak;
  116. falselabel:=lloop;
  117. end
  118. else
  119. begin
  120. truelabel:=lloop;
  121. falselabel:=lbreak;
  122. end;
  123. rg.cleartempgen;
  124. secondpass(left);
  125. maketojumpbool(exprasmlist,left,lr_load_regvars);
  126. cg.a_label(exprasmlist,lbreak);
  127. truelabel:=otlabel;
  128. falselabel:=oflabel;
  129. aktcontinuelabel:=oldclabel;
  130. aktbreaklabel:=oldblabel;
  131. { a break/continue in a while/repeat block can't be seen outside }
  132. flowcontrol:=flowcontrol-[fc_break,fc_continue];
  133. end;
  134. {*****************************************************************************
  135. tcgIFNODE
  136. *****************************************************************************}
  137. procedure tcgifnode.pass_2;
  138. var
  139. hl,otlabel,oflabel : tasmlabel;
  140. org_regvar_loaded,
  141. then_regvar_loaded,
  142. else_regvar_loaded : regvar_booleanarray;
  143. org_list,
  144. then_list,
  145. else_list : taasmoutput;
  146. begin
  147. otlabel:=truelabel;
  148. oflabel:=falselabel;
  149. objectlibrary.getlabel(truelabel);
  150. objectlibrary.getlabel(falselabel);
  151. rg.cleartempgen;
  152. secondpass(left);
  153. { save regvars loaded in the beginning so that we can restore them }
  154. { when processing the else-block }
  155. if cs_regalloc in aktglobalswitches then
  156. begin
  157. org_list := exprasmlist;
  158. exprasmlist := taasmoutput.create;
  159. end;
  160. maketojumpbool(exprasmlist,left,lr_dont_load_regvars);
  161. if cs_regalloc in aktglobalswitches then
  162. org_regvar_loaded := rg.regvar_loaded;
  163. if assigned(right) then
  164. begin
  165. cg.a_label(exprasmlist,truelabel);
  166. rg.cleartempgen;
  167. secondpass(right);
  168. end;
  169. { save current asmlist (previous instructions + then-block) and }
  170. { loaded regvar state and create new clean ones }
  171. if cs_regalloc in aktglobalswitches then
  172. begin
  173. then_regvar_loaded := rg.regvar_loaded;
  174. rg.regvar_loaded := org_regvar_loaded;
  175. then_list := exprasmlist;
  176. exprasmlist := taasmoutput.create;
  177. end;
  178. if assigned(t1) then
  179. begin
  180. if assigned(right) then
  181. begin
  182. objectlibrary.getlabel(hl);
  183. { do go back to if line !! }
  184. if not(cs_regalloc in aktglobalswitches) then
  185. aktfilepos:=exprasmList.getlasttaifilepos^
  186. else
  187. aktfilepos:=then_list.getlasttaifilepos^;
  188. cg.a_jmp_always(exprasmlist,hl);
  189. end;
  190. cg.a_label(exprasmlist,falselabel);
  191. rg.cleartempgen;
  192. secondpass(t1);
  193. { save current asmlist (previous instructions + else-block) }
  194. { and loaded regvar state and create a new clean list }
  195. if cs_regalloc in aktglobalswitches then
  196. begin
  197. else_regvar_loaded := rg.regvar_loaded;
  198. else_list := exprasmlist;
  199. exprasmlist := taasmoutput.create;
  200. end;
  201. if assigned(right) then
  202. cg.a_label(exprasmlist,hl);
  203. end
  204. else
  205. begin
  206. if cs_regalloc in aktglobalswitches then
  207. begin
  208. else_regvar_loaded := rg.regvar_loaded;
  209. else_list := exprasmlist;
  210. exprasmlist := taasmoutput.create;
  211. end;
  212. cg.a_label(exprasmlist,falselabel);
  213. end;
  214. if not(assigned(right)) then
  215. begin
  216. cg.a_label(exprasmlist,truelabel);
  217. end;
  218. if cs_regalloc in aktglobalswitches then
  219. begin
  220. { add loads of regvars at the end of the then- and else-blocks }
  221. { so that at the end of both blocks the same regvars are loaded }
  222. { no else block? }
  223. if not assigned(t1) then
  224. sync_regvars(org_list,then_list,org_regvar_loaded,
  225. then_regvar_loaded)
  226. { no then block? }
  227. else if not assigned(right) then
  228. sync_regvars(org_list,else_list,org_regvar_loaded,
  229. else_regvar_loaded)
  230. { both else and then blocks }
  231. else
  232. sync_regvars(then_list,else_list,then_regvar_loaded,
  233. else_regvar_loaded);
  234. { add all lists together }
  235. org_list.concatlist(then_list);
  236. then_list.free;
  237. org_list.concatlist(else_list);
  238. else_list.free;
  239. org_list.concatlist(exprasmlist);
  240. exprasmlist.free;
  241. exprasmlist := org_list;
  242. end;
  243. truelabel:=otlabel;
  244. falselabel:=oflabel;
  245. end;
  246. {*****************************************************************************
  247. SecondFor
  248. *****************************************************************************}
  249. procedure tcgfornode.pass_2;
  250. var
  251. l3,oldclabel,oldblabel : tasmlabel;
  252. temptovalue : boolean;
  253. hs : byte;
  254. temp1 : treference;
  255. hop : topcg;
  256. hcond : topcmp;
  257. opsize : tcgsize;
  258. count_var_is_signed,do_loopvar_at_end : boolean;
  259. cmp_const:Tconstexprint;
  260. begin
  261. oldclabel:=aktcontinuelabel;
  262. oldblabel:=aktbreaklabel;
  263. objectlibrary.getlabel(aktcontinuelabel);
  264. objectlibrary.getlabel(aktbreaklabel);
  265. objectlibrary.getlabel(l3);
  266. { only calculate reference }
  267. rg.cleartempgen;
  268. secondpass(t2);
  269. hs := t2.resulttype.def.size;
  270. opsize := def_cgsize(t2.resulttype.def);
  271. { first set the to value
  272. because the count var can be in the expression !! }
  273. rg.cleartempgen;
  274. do_loopvar_at_end:=lnf_dont_mind_loopvar_on_exit in loopflags;
  275. secondpass(right);
  276. { calculate pointer value and check if changeable and if so }
  277. { load into temporary variable }
  278. if right.nodetype<>ordconstn then
  279. begin
  280. do_loopvar_at_end:=false;
  281. tg.GetTemp(exprasmlist,hs,tt_normal,temp1);
  282. temptovalue:=true;
  283. if (right.location.loc=LOC_REGISTER) or
  284. (right.location.loc=LOC_CREGISTER) then
  285. begin
  286. cg.a_load_reg_ref(exprasmlist,opsize,
  287. right.location.register,temp1);
  288. rg.ungetregister(exprasmlist,right.location.register);
  289. end
  290. else
  291. cg.g_concatcopy(exprasmlist,right.location.reference,temp1,
  292. hs,true,false);
  293. end
  294. else
  295. temptovalue:=false;
  296. { produce start assignment }
  297. rg.cleartempgen;
  298. secondpass(left);
  299. count_var_is_signed:=is_signed(t2.resulttype.def);
  300. if lnf_backward in loopflags then
  301. if count_var_is_signed then
  302. hcond:=OC_LT
  303. else
  304. hcond:=OC_B
  305. else
  306. if count_var_is_signed then
  307. hcond:=OC_GT
  308. else
  309. hcond:=OC_A;
  310. load_all_regvars(exprasmlist);
  311. if temptovalue then
  312. begin
  313. cg.a_cmp_ref_loc_label(exprasmlist,opsize,hcond,
  314. temp1,t2.location,aktbreaklabel);
  315. end
  316. else
  317. begin
  318. if lnf_testatbegin in loopflags then
  319. begin
  320. cg.a_cmp_const_loc_label(exprasmlist,opsize,hcond,
  321. aword(tordconstnode(right).value),
  322. t2.location,aktbreaklabel);
  323. end;
  324. end;
  325. {If the loopvar doesn't mind on exit, we avoid this ugly
  326. dec instruction and do the loopvar inc/dec after the loop
  327. body.}
  328. if not do_loopvar_at_end then
  329. begin
  330. if lnf_backward in loopflags then
  331. hop:=OP_ADD
  332. else
  333. hop:=OP_SUB;
  334. cg.a_op_const_loc(exprasmlist,hop,1,t2.location);
  335. end;
  336. if not(cs_littlesize in aktglobalswitches) then
  337. { align loop target }
  338. exprasmList.concat(Tai_align.Create(aktalignment.loopalign));
  339. cg.a_label(exprasmlist,l3);
  340. {If the loopvar doesn't mind on exit, we avoid the loopvar inc/dec
  341. after the loop body instead of here.}
  342. if not do_loopvar_at_end then
  343. begin
  344. { according to count direction DEC or INC... }
  345. if lnf_backward in loopflags then
  346. hop:=OP_SUB
  347. else
  348. hop:=OP_ADD;
  349. cg.a_op_const_loc(exprasmlist,hop,1,t2.location);
  350. end;
  351. { help register must not be in instruction block }
  352. rg.cleartempgen;
  353. if assigned(t1) then
  354. begin
  355. secondpass(t1);
  356. load_all_regvars(exprasmlist);
  357. end;
  358. {If the loopvar doesn't mind on exit, we do the loopvar inc/dec
  359. after the loop body instead of here.}
  360. if do_loopvar_at_end then
  361. begin
  362. { according to count direction DEC or INC... }
  363. if lnf_backward in loopflags then
  364. hop:=OP_SUB
  365. else
  366. hop:=OP_ADD;
  367. cg.a_op_const_loc(exprasmlist,hop,1,t2.location);
  368. end;
  369. cg.a_label(exprasmlist,aktcontinuelabel);
  370. { makes no problems there }
  371. rg.cleartempgen;
  372. if do_loopvar_at_end then
  373. if lnf_backward in loopflags then
  374. if count_var_is_signed then
  375. hcond:=OC_GTE
  376. else
  377. hcond:=OC_AE
  378. else
  379. if count_var_is_signed then
  380. hcond:=OC_LTE
  381. else
  382. hcond:=OC_BE
  383. else
  384. if lnf_backward in loopflags then
  385. if count_var_is_signed then
  386. hcond:=OC_GT
  387. else
  388. hcond:=OC_A
  389. else
  390. if count_var_is_signed then
  391. hcond:=OC_LT
  392. else
  393. hcond:=OC_B;
  394. load_all_regvars(exprasmlist);
  395. cmp_const:=aword(Tordconstnode(right).value);
  396. if do_loopvar_at_end then
  397. begin
  398. {Watch out for wrap around 255 -> 0.}
  399. {Ugly: This code is way to long... Use tables?}
  400. case opsize of
  401. OS_8:
  402. begin
  403. if lnf_backward in loopflags then
  404. begin
  405. if byte(cmp_const)=low(byte) then
  406. begin
  407. hcond:=OC_NE;
  408. cmp_const:=high(byte);
  409. end
  410. end
  411. else
  412. begin
  413. if byte(cmp_const)=high(byte) then
  414. begin
  415. hcond:=OC_NE;
  416. cmp_const:=low(byte);
  417. end
  418. end
  419. end;
  420. OS_16:
  421. begin
  422. if lnf_backward in loopflags then
  423. begin
  424. if word(cmp_const)=high(word) then
  425. begin
  426. hcond:=OC_NE;
  427. cmp_const:=low(word);
  428. end
  429. end
  430. else
  431. begin
  432. if word(cmp_const)=low(word) then
  433. begin
  434. hcond:=OC_NE;
  435. cmp_const:=high(word);
  436. end
  437. end
  438. end;
  439. OS_32:
  440. begin
  441. if lnf_backward in loopflags then
  442. begin
  443. if cardinal(cmp_const)=high(cardinal) then
  444. begin
  445. hcond:=OC_NE;
  446. cmp_const:=low(cardinal);
  447. end
  448. end
  449. else
  450. begin
  451. if cardinal(cmp_const)=low(cardinal) then
  452. begin
  453. hcond:=OC_NE;
  454. cmp_const:=high(cardinal);
  455. end
  456. end
  457. end;
  458. OS_64:
  459. begin
  460. if lnf_backward in loopflags then
  461. begin
  462. if qword(cmp_const)=high(qword) then
  463. begin
  464. hcond:=OC_NE;
  465. cmp_const:=low(qword);
  466. end
  467. end
  468. else
  469. begin
  470. if qword(cmp_const)=low(qword) then
  471. begin
  472. hcond:=OC_NE;
  473. cmp_const:=high(qword);
  474. end
  475. end
  476. end;
  477. OS_S8:
  478. begin
  479. if lnf_backward in loopflags then
  480. begin
  481. if shortint(cmp_const)=low(shortint) then
  482. begin
  483. hcond:=OC_NE;
  484. cmp_const:=high(shortint);
  485. end
  486. end
  487. else
  488. begin
  489. if shortint(cmp_const)=high(shortint) then
  490. begin
  491. hcond:=OC_NE;
  492. cmp_const:=low(shortint);
  493. end
  494. end
  495. end;
  496. OS_S16:
  497. begin
  498. if lnf_backward in loopflags then
  499. begin
  500. if integer(cmp_const)=high(integer) then
  501. begin
  502. hcond:=OC_NE;
  503. cmp_const:=low(integer);
  504. end
  505. end
  506. else
  507. begin
  508. if integer(cmp_const)=low(integer) then
  509. begin
  510. hcond:=OC_NE;
  511. cmp_const:=high(integer);
  512. end
  513. end
  514. end;
  515. OS_S32:
  516. begin
  517. if lnf_backward in loopflags then
  518. begin
  519. if longint(cmp_const)=high(longint) then
  520. begin
  521. hcond:=OC_NE;
  522. cmp_const:=low(longint);
  523. end
  524. end
  525. else
  526. begin
  527. if longint(cmp_const)=low(longint) then
  528. begin
  529. hcond:=OC_NE;
  530. cmp_const:=high(longint);
  531. end
  532. end
  533. end;
  534. OS_S64:
  535. begin
  536. if lnf_backward in loopflags then
  537. begin
  538. if int64(cmp_const)=high(int64) then
  539. begin
  540. hcond:=OC_NE;
  541. cmp_const:=low(int64);
  542. end
  543. end
  544. else
  545. begin
  546. if int64(cmp_const)=low(int64) then
  547. begin
  548. hcond:=OC_NE;
  549. cmp_const:=high(int64);
  550. end
  551. end
  552. end;
  553. else
  554. internalerror(200201021);
  555. end;
  556. end;
  557. { produce comparison and the corresponding }
  558. { jump }
  559. if temptovalue then
  560. begin
  561. cg.a_cmp_ref_loc_label(exprasmlist,opsize,hcond,temp1,
  562. t2.location,l3);
  563. end
  564. else
  565. begin
  566. cg.a_cmp_const_loc_label(exprasmlist,opsize,hcond,
  567. cmp_const,t2.location,l3);
  568. end;
  569. if temptovalue then
  570. tg.ungetiftemp(exprasmlist,temp1);
  571. { this is the break label: }
  572. cg.a_label(exprasmlist,aktbreaklabel);
  573. aktcontinuelabel:=oldclabel;
  574. aktbreaklabel:=oldblabel;
  575. { a break/continue in a for block can't be seen outside }
  576. flowcontrol:=flowcontrol-[fc_break,fc_continue];
  577. end;
  578. {*****************************************************************************
  579. SecondExitN
  580. *****************************************************************************}
  581. procedure tcgexitnode.pass_2;
  582. var
  583. {op : tasmop;
  584. s : topsize;}
  585. otlabel,oflabel : tasmlabel;
  586. cgsize : tcgsize;
  587. r,hreg : tregister;
  588. allocated_acc,
  589. allocated_acchigh: boolean;
  590. label
  591. do_jmp;
  592. begin
  593. include(flowcontrol,fc_exit);
  594. if assigned(left) then
  595. begin
  596. if onlyassign then
  597. begin
  598. { just do a normal assignment followed by exit }
  599. secondpass(left);
  600. cg.a_jmp_always(exprasmlist,aktexitlabel);
  601. end
  602. else
  603. begin
  604. allocated_acc := false;
  605. allocated_acchigh := false;
  606. otlabel:=truelabel;
  607. oflabel:=falselabel;
  608. objectlibrary.getlabel(truelabel);
  609. objectlibrary.getlabel(falselabel);
  610. secondpass(left);
  611. { increment reference counter, this is
  612. useless for string constants }
  613. if (left.resulttype.def.needs_inittable) and
  614. (left.nodetype<>stringconstn) then
  615. cg.g_incrrefcount(exprasmlist,left.resulttype.def,left.location.reference);
  616. { the result of left is not needed anymore after this
  617. node }
  618. location_freetemp(exprasmlist,left.location);
  619. location_release(exprasmlist,left.location);
  620. case left.location.loc of
  621. LOC_FPUREGISTER :
  622. goto do_jmp;
  623. {$ifdef cpuflags}
  624. LOC_FLAGS :
  625. begin
  626. r.enum:=accumulator;
  627. cg.a_reg_alloc(exprasmlist,r);
  628. allocated_acc := true;
  629. cg.g_flags2reg(exprasmlist,OS_INT,left.location.resflags,r);
  630. goto do_jmp;
  631. end;
  632. {$endif cpuflags}
  633. LOC_JUMP :
  634. begin
  635. r.enum:=accumulator;
  636. cg.a_reg_alloc(exprasmlist,r);
  637. { get an 8-bit register }
  638. hreg:=rg.makeregsize(r,OS_8);
  639. allocated_acc := true;
  640. cg.a_label(exprasmlist,truelabel);
  641. cg.a_load_const_reg(exprasmlist,OS_8,1,hreg);
  642. cg.a_jmp_always(exprasmlist,aktexit2label);
  643. cg.a_label(exprasmlist,falselabel);
  644. cg.a_load_const_reg(exprasmlist,OS_8,0,hreg);
  645. goto do_jmp;
  646. end;
  647. end;
  648. case aktprocdef.rettype.def.deftype of
  649. pointerdef,
  650. procvardef :
  651. begin
  652. r.enum:=accumulator;
  653. cg.a_reg_alloc(exprasmlist,r);
  654. allocated_acc := true;
  655. cg.a_load_loc_reg(exprasmlist,left.location,r);
  656. end;
  657. floatdef :
  658. begin
  659. r.enum:=fpu_result_reg;
  660. {$ifndef i386}
  661. cg.a_reg_alloc(exprasmlist,r);
  662. {$endif not i386}
  663. cg.a_loadfpu_loc_reg(exprasmlist,left.location,r);
  664. end;
  665. else
  666. begin
  667. cgsize:=def_cgsize(aktprocdef.rettype.def);
  668. r.enum:=accumulator;
  669. cg.a_reg_alloc(exprasmlist,r);
  670. allocated_acc := true;
  671. {$ifndef cpu64bit}
  672. if cgsize in [OS_64,OS_S64] then
  673. begin
  674. hreg.enum:=accumulatorhigh;
  675. cg.a_reg_alloc(exprasmlist,hreg);
  676. allocated_acchigh := true;
  677. cg64.a_load64_loc_reg(exprasmlist,left.location,
  678. joinreg64(r,hreg));
  679. end
  680. else
  681. {$endif cpu64bit}
  682. begin
  683. hreg:=rg.makeregsize(r,cgsize);
  684. cg.a_load_loc_reg(exprasmlist,left.location,hreg);
  685. end;
  686. end;
  687. end;
  688. do_jmp:
  689. truelabel:=otlabel;
  690. falselabel:=oflabel;
  691. cg.a_jmp_always(exprasmlist,aktexit2label);
  692. r.enum:=accumulator;
  693. hreg.enum:=accumulatorhigh;
  694. if allocated_acc then
  695. cg.a_reg_dealloc(exprasmlist,r);
  696. {$ifndef cpu64bit}
  697. if allocated_acchigh then
  698. cg.a_reg_dealloc(exprasmlist,hreg);
  699. {$endif cpu64bit}
  700. {$ifndef i386}
  701. r.enum:=fpu_result_reg;
  702. if (aktprocdef.rettype.def.deftype = floatdef) then
  703. cg.a_reg_dealloc(exprasmlist,r);
  704. {$endif not i386}
  705. end;
  706. end
  707. else
  708. cg.a_jmp_always(exprasmlist,aktexitlabel);
  709. end;
  710. {*****************************************************************************
  711. SecondBreakN
  712. *****************************************************************************}
  713. procedure tcgbreaknode.pass_2;
  714. begin
  715. include(flowcontrol,fc_break);
  716. if aktbreaklabel<>nil then
  717. begin
  718. load_all_regvars(exprasmlist);
  719. cg.a_jmp_always(exprasmlist,aktbreaklabel)
  720. end
  721. else
  722. CGMessage(cg_e_break_not_allowed);
  723. end;
  724. {*****************************************************************************
  725. SecondContinueN
  726. *****************************************************************************}
  727. procedure tcgcontinuenode.pass_2;
  728. begin
  729. include(flowcontrol,fc_continue);
  730. if aktcontinuelabel<>nil then
  731. begin
  732. load_all_regvars(exprasmlist);
  733. cg.a_jmp_always(exprasmlist,aktcontinuelabel)
  734. end
  735. else
  736. CGMessage(cg_e_continue_not_allowed);
  737. end;
  738. {*****************************************************************************
  739. SecondGoto
  740. *****************************************************************************}
  741. procedure tcggotonode.pass_2;
  742. begin
  743. load_all_regvars(exprasmlist);
  744. cg.a_jmp_always(exprasmlist,labsym.lab)
  745. end;
  746. {*****************************************************************************
  747. SecondLabel
  748. *****************************************************************************}
  749. procedure tcglabelnode.pass_2;
  750. begin
  751. load_all_regvars(exprasmlist);
  752. cg.a_label(exprasmlist,labelnr);
  753. rg.cleartempgen;
  754. secondpass(left);
  755. end;
  756. {*****************************************************************************
  757. SecondFail
  758. *****************************************************************************}
  759. procedure tcgfailnode.pass_2;
  760. begin
  761. cg.a_jmp_always(exprasmlist,faillabel);
  762. end;
  763. {*****************************************************************************
  764. SecondRaise
  765. *****************************************************************************}
  766. procedure tcgraisenode.pass_2;
  767. var
  768. a : tasmlabel;
  769. href : treference;
  770. href2: treference;
  771. r:Tregister;
  772. begin
  773. if assigned(left) then
  774. begin
  775. { multiple parameters? }
  776. if assigned(right) then
  777. begin
  778. { push frame }
  779. if assigned(frametree) then
  780. begin
  781. secondpass(frametree);
  782. if codegenerror then
  783. exit;
  784. cg.a_param_loc(exprasmlist,frametree.location,paramanager.getintparaloc(2));
  785. end
  786. else
  787. cg.a_param_const(exprasmlist,OS_INT,0,paramanager.getintparaloc(2));
  788. { push address }
  789. secondpass(right);
  790. if codegenerror then
  791. exit;
  792. cg.a_param_loc(exprasmlist,right.location,paramanager.getintparaloc(1));
  793. end
  794. else
  795. begin
  796. { get current address }
  797. objectlibrary.getaddrlabel(a);
  798. cg.a_label(exprasmlist,a);
  799. reference_reset_symbol(href2,a,0);
  800. { push current frame }
  801. r.enum:=frame_pointer_reg;
  802. cg.a_param_reg(exprasmlist,OS_ADDR,r,paramanager.getintparaloc(2));
  803. { push current address }
  804. cg.a_paramaddr_ref(exprasmlist,href2,paramanager.getintparaloc(1));
  805. end;
  806. { push object }
  807. secondpass(left);
  808. if codegenerror then
  809. exit;
  810. cg.a_param_loc(exprasmlist,left.location,paramanager.getintparaloc(1));
  811. cg.a_call_name(exprasmlist,'FPC_RAISEEXCEPTION');
  812. end
  813. else
  814. begin
  815. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  816. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  817. end;
  818. end;
  819. {*****************************************************************************
  820. SecondTryExcept
  821. *****************************************************************************}
  822. var
  823. endexceptlabel : tasmlabel;
  824. procedure try_new_exception(list : taasmoutput;var jmpbuf,envbuf, href : treference;
  825. a : aword; exceptlabel : tasmlabel);
  826. begin
  827. tg.GetTemp(list,EXCEPT_BUF_SIZE,tt_persistant,envbuf);
  828. tg.GetTemp(list,JMP_BUF_SIZE,tt_persistant,jmpbuf);
  829. tg.GetTemp(list,sizeof(aword),tt_persistant,href);
  830. new_exception(list, jmpbuf,envbuf, href, a, exceptlabel);
  831. end;
  832. procedure try_free_exception(list : taasmoutput;var jmpbuf, envbuf, href : treference;
  833. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  834. begin
  835. free_exception(list, jmpbuf, envbuf, href, a, endexceptlabel, onlyfree);
  836. tg.ungettemp(list,href);
  837. tg.Ungettemp(list,jmpbuf);
  838. tg.ungettemp(list,envbuf);
  839. end;
  840. { does the necessary things to clean up the object stack }
  841. { in the except block }
  842. procedure cleanupobjectstack;
  843. var r:Tregister;
  844. begin
  845. cg.a_call_name(exprasmlist,'FPC_POPOBJECTSTACK');
  846. r.enum:=accumulator;
  847. cg.a_param_reg(exprasmlist,OS_ADDR,r,paramanager.getintparaloc(1));
  848. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  849. cg.g_maybe_loadself(exprasmlist);
  850. end;
  851. procedure tcgtryexceptnode.pass_2;
  852. var
  853. exceptlabel,doexceptlabel,oldendexceptlabel,
  854. lastonlabel,
  855. exitexceptlabel,
  856. continueexceptlabel,
  857. breakexceptlabel,
  858. exittrylabel,
  859. continuetrylabel,
  860. breaktrylabel,
  861. doobjectdestroy,
  862. doobjectdestroyandreraise,
  863. oldaktexitlabel,
  864. oldaktexit2label,
  865. oldaktcontinuelabel,
  866. oldaktbreaklabel : tasmlabel;
  867. oldflowcontrol,tryflowcontrol,
  868. exceptflowcontrol : tflowcontrol;
  869. tempbuf,tempaddr : treference;
  870. href : treference;
  871. r:Tregister;
  872. label
  873. errorexit;
  874. begin
  875. oldflowcontrol:=flowcontrol;
  876. flowcontrol:=[];
  877. { this can be called recursivly }
  878. oldendexceptlabel:=endexceptlabel;
  879. { save the old labels for control flow statements }
  880. oldaktexitlabel:=aktexitlabel;
  881. oldaktexit2label:=aktexit2label;
  882. if assigned(aktbreaklabel) then
  883. begin
  884. oldaktcontinuelabel:=aktcontinuelabel;
  885. oldaktbreaklabel:=aktbreaklabel;
  886. end;
  887. { get new labels for the control flow statements }
  888. objectlibrary.getlabel(exittrylabel);
  889. objectlibrary.getlabel(exitexceptlabel);
  890. if assigned(aktbreaklabel) then
  891. begin
  892. objectlibrary.getlabel(breaktrylabel);
  893. objectlibrary.getlabel(continuetrylabel);
  894. objectlibrary.getlabel(breakexceptlabel);
  895. objectlibrary.getlabel(continueexceptlabel);
  896. end;
  897. objectlibrary.getlabel(exceptlabel);
  898. objectlibrary.getlabel(doexceptlabel);
  899. objectlibrary.getlabel(endexceptlabel);
  900. objectlibrary.getlabel(lastonlabel);
  901. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,exceptlabel);
  902. { try block }
  903. { set control flow labels for the try block }
  904. aktexitlabel:=exittrylabel;
  905. aktexit2label:=exittrylabel;
  906. if assigned(oldaktbreaklabel) then
  907. begin
  908. aktcontinuelabel:=continuetrylabel;
  909. aktbreaklabel:=breaktrylabel;
  910. end;
  911. flowcontrol:=[];
  912. secondpass(left);
  913. tryflowcontrol:=flowcontrol;
  914. if codegenerror then
  915. goto errorexit;
  916. cg.a_label(exprasmlist,exceptlabel);
  917. try_free_exception(exprasmlist,tempbuf,tempaddr,href,0,endexceptlabel,false);
  918. cg.a_label(exprasmlist,doexceptlabel);
  919. { set control flow labels for the except block }
  920. { and the on statements }
  921. aktexitlabel:=exitexceptlabel;
  922. aktexit2label:=exitexceptlabel;
  923. if assigned(oldaktbreaklabel) then
  924. begin
  925. aktcontinuelabel:=continueexceptlabel;
  926. aktbreaklabel:=breakexceptlabel;
  927. end;
  928. flowcontrol:=[];
  929. { on statements }
  930. if assigned(right) then
  931. secondpass(right);
  932. cg.a_label(exprasmlist,lastonlabel);
  933. { default handling except handling }
  934. if assigned(t1) then
  935. begin
  936. { FPC_CATCHES must be called with
  937. 'default handler' flag (=-1)
  938. }
  939. cg.a_param_const(exprasmlist,OS_ADDR,aword(-1),paramanager.getintparaloc(1));
  940. cg.a_call_name(exprasmlist,'FPC_CATCHES');
  941. cg.g_maybe_loadself(exprasmlist);
  942. { the destruction of the exception object must be also }
  943. { guarded by an exception frame }
  944. objectlibrary.getlabel(doobjectdestroy);
  945. objectlibrary.getlabel(doobjectdestroyandreraise);
  946. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,doobjectdestroyandreraise);
  947. { here we don't have to reset flowcontrol }
  948. { the default and on flowcontrols are handled equal }
  949. secondpass(t1);
  950. exceptflowcontrol:=flowcontrol;
  951. cg.a_label(exprasmlist,doobjectdestroyandreraise);
  952. try_free_exception(exprasmlist,tempbuf,tempaddr,href,0,doobjectdestroy,false);
  953. cg.a_call_name(exprasmlist,'FPC_POPSECONDOBJECTSTACK');
  954. r.enum:=accumulator;
  955. cg.a_param_reg(exprasmlist, OS_ADDR, r, paramanager.getintparaloc(1));
  956. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  957. { we don't need to restore esi here because reraise never }
  958. { returns }
  959. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  960. cg.a_label(exprasmlist,doobjectdestroy);
  961. cleanupobjectstack;
  962. cg.a_jmp_always(exprasmlist,endexceptlabel);
  963. end
  964. else
  965. begin
  966. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  967. exceptflowcontrol:=flowcontrol;
  968. end;
  969. if fc_exit in exceptflowcontrol then
  970. begin
  971. { do some magic for exit in the try block }
  972. cg.a_label(exprasmlist,exitexceptlabel);
  973. { we must also destroy the address frame which guards }
  974. { exception object }
  975. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  976. cg.g_exception_reason_load(exprasmlist,href);
  977. cleanupobjectstack;
  978. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  979. end;
  980. if fc_break in exceptflowcontrol then
  981. begin
  982. cg.a_label(exprasmlist,breakexceptlabel);
  983. { we must also destroy the address frame which guards }
  984. { exception object }
  985. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  986. cg.g_exception_reason_load(exprasmlist,href);
  987. cleanupobjectstack;
  988. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  989. end;
  990. if fc_continue in exceptflowcontrol then
  991. begin
  992. cg.a_label(exprasmlist,continueexceptlabel);
  993. { we must also destroy the address frame which guards }
  994. { exception object }
  995. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  996. cg.g_exception_reason_load(exprasmlist,href);
  997. cleanupobjectstack;
  998. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  999. end;
  1000. if fc_exit in tryflowcontrol then
  1001. begin
  1002. { do some magic for exit in the try block }
  1003. cg.a_label(exprasmlist,exittrylabel);
  1004. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1005. cg.g_exception_reason_load(exprasmlist,href);
  1006. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  1007. end;
  1008. if fc_break in tryflowcontrol then
  1009. begin
  1010. cg.a_label(exprasmlist,breaktrylabel);
  1011. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1012. cg.g_exception_reason_load(exprasmlist,href);
  1013. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  1014. end;
  1015. if fc_continue in tryflowcontrol then
  1016. begin
  1017. cg.a_label(exprasmlist,continuetrylabel);
  1018. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1019. cg.g_exception_reason_load(exprasmlist,href);
  1020. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  1021. end;
  1022. cg.a_label(exprasmlist,endexceptlabel);
  1023. errorexit:
  1024. { restore all saved labels }
  1025. endexceptlabel:=oldendexceptlabel;
  1026. { restore the control flow labels }
  1027. aktexitlabel:=oldaktexitlabel;
  1028. aktexit2label:=oldaktexit2label;
  1029. if assigned(oldaktbreaklabel) then
  1030. begin
  1031. aktcontinuelabel:=oldaktcontinuelabel;
  1032. aktbreaklabel:=oldaktbreaklabel;
  1033. end;
  1034. { return all used control flow statements }
  1035. flowcontrol:=oldflowcontrol+exceptflowcontrol+
  1036. tryflowcontrol;
  1037. end;
  1038. procedure tcgonnode.pass_2;
  1039. var
  1040. nextonlabel,
  1041. exitonlabel,
  1042. continueonlabel,
  1043. breakonlabel,
  1044. oldaktexitlabel,
  1045. oldaktexit2label,
  1046. oldaktcontinuelabel,
  1047. doobjectdestroyandreraise,
  1048. doobjectdestroy,
  1049. oldaktbreaklabel : tasmlabel;
  1050. ref : treference;
  1051. oldflowcontrol : tflowcontrol;
  1052. tempbuf,tempaddr : treference;
  1053. href : treference;
  1054. href2: treference;
  1055. r:Tregister;
  1056. begin
  1057. r.enum:=accumulator;
  1058. oldflowcontrol:=flowcontrol;
  1059. flowcontrol:=[];
  1060. objectlibrary.getlabel(nextonlabel);
  1061. { send the vmt parameter }
  1062. reference_reset_symbol(href2,objectlibrary.newasmsymbol(excepttype.vmt_mangledname),0);
  1063. cg.a_paramaddr_ref(exprasmlist,href2,paramanager.getintparaloc(1));
  1064. cg.a_call_name(exprasmlist,'FPC_CATCHES');
  1065. { is it this catch? No. go to next onlabel }
  1066. cg.a_cmp_const_reg_label(exprasmlist,OS_ADDR,OC_EQ,0,r,nextonlabel);
  1067. ref.symbol:=nil;
  1068. tg.GetTemp(exprasmlist,pointer_size,tt_normal,ref);
  1069. { what a hack ! }
  1070. if assigned(exceptsymtable) then
  1071. tvarsym(exceptsymtable.symindex.first).address:=ref.offset;
  1072. cg.a_load_reg_ref(exprasmlist, OS_ADDR, r, ref);
  1073. { in the case that another exception is risen }
  1074. { we've to destroy the old one }
  1075. objectlibrary.getlabel(doobjectdestroyandreraise);
  1076. { call setjmp, and jump to finally label on non-zero result }
  1077. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,doobjectdestroyandreraise);
  1078. if assigned(right) then
  1079. begin
  1080. oldaktexitlabel:=aktexitlabel;
  1081. oldaktexit2label:=aktexit2label;
  1082. objectlibrary.getlabel(exitonlabel);
  1083. aktexitlabel:=exitonlabel;
  1084. aktexit2label:=exitonlabel;
  1085. if assigned(aktbreaklabel) then
  1086. begin
  1087. oldaktcontinuelabel:=aktcontinuelabel;
  1088. oldaktbreaklabel:=aktbreaklabel;
  1089. objectlibrary.getlabel(breakonlabel);
  1090. objectlibrary.getlabel(continueonlabel);
  1091. aktcontinuelabel:=continueonlabel;
  1092. aktbreaklabel:=breakonlabel;
  1093. end;
  1094. { esi is destroyed by FPC_CATCHES }
  1095. cg.g_maybe_loadself(exprasmlist);
  1096. secondpass(right);
  1097. end;
  1098. objectlibrary.getlabel(doobjectdestroy);
  1099. cg.a_label(exprasmlist,doobjectdestroyandreraise);
  1100. try_free_exception(exprasmlist,tempbuf,tempaddr,href,0,doobjectdestroy,false);
  1101. cg.a_call_name(exprasmlist,'FPC_POPSECONDOBJECTSTACK');
  1102. cg.a_param_reg(exprasmlist, OS_ADDR, r, paramanager.getintparaloc(1));
  1103. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  1104. { we don't need to restore esi here because reraise never }
  1105. { returns }
  1106. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1107. cg.a_label(exprasmlist,doobjectdestroy);
  1108. cleanupobjectstack;
  1109. { clear some stuff }
  1110. tg.ungetiftemp(exprasmlist,ref);
  1111. cg.a_jmp_always(exprasmlist,endexceptlabel);
  1112. if assigned(right) then
  1113. begin
  1114. { special handling for control flow instructions }
  1115. if fc_exit in flowcontrol then
  1116. begin
  1117. { the address and object pop does secondtryexcept }
  1118. cg.a_label(exprasmlist,exitonlabel);
  1119. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  1120. end;
  1121. if fc_break in flowcontrol then
  1122. begin
  1123. { the address and object pop does secondtryexcept }
  1124. cg.a_label(exprasmlist,breakonlabel);
  1125. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  1126. end;
  1127. if fc_continue in flowcontrol then
  1128. begin
  1129. { the address and object pop does secondtryexcept }
  1130. cg.a_label(exprasmlist,continueonlabel);
  1131. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  1132. end;
  1133. aktexitlabel:=oldaktexitlabel;
  1134. aktexit2label:=oldaktexit2label;
  1135. if assigned(oldaktbreaklabel) then
  1136. begin
  1137. aktcontinuelabel:=oldaktcontinuelabel;
  1138. aktbreaklabel:=oldaktbreaklabel;
  1139. end;
  1140. end;
  1141. cg.a_label(exprasmlist,nextonlabel);
  1142. flowcontrol:=oldflowcontrol+flowcontrol;
  1143. { next on node }
  1144. if assigned(left) then
  1145. begin
  1146. rg.cleartempgen;
  1147. secondpass(left);
  1148. end;
  1149. end;
  1150. {*****************************************************************************
  1151. SecondTryFinally
  1152. *****************************************************************************}
  1153. procedure tcgtryfinallynode.pass_2;
  1154. var
  1155. reraiselabel,
  1156. finallylabel,
  1157. endfinallylabel,
  1158. exitfinallylabel,
  1159. continuefinallylabel,
  1160. breakfinallylabel,
  1161. oldaktexitlabel,
  1162. oldaktexit2label,
  1163. oldaktcontinuelabel,
  1164. oldaktbreaklabel : tasmlabel;
  1165. oldflowcontrol,tryflowcontrol : tflowcontrol;
  1166. decconst : longint;
  1167. tempbuf,tempaddr : treference;
  1168. href : treference;
  1169. r:Tregister;
  1170. begin
  1171. { check if child nodes do a break/continue/exit }
  1172. oldflowcontrol:=flowcontrol;
  1173. flowcontrol:=[];
  1174. objectlibrary.getlabel(finallylabel);
  1175. objectlibrary.getlabel(endfinallylabel);
  1176. objectlibrary.getlabel(reraiselabel);
  1177. { the finally block must catch break, continue and exit }
  1178. { statements }
  1179. oldaktexitlabel:=aktexitlabel;
  1180. oldaktexit2label:=aktexit2label;
  1181. objectlibrary.getlabel(exitfinallylabel);
  1182. aktexitlabel:=exitfinallylabel;
  1183. aktexit2label:=exitfinallylabel;
  1184. if assigned(aktbreaklabel) then
  1185. begin
  1186. oldaktcontinuelabel:=aktcontinuelabel;
  1187. oldaktbreaklabel:=aktbreaklabel;
  1188. objectlibrary.getlabel(breakfinallylabel);
  1189. objectlibrary.getlabel(continuefinallylabel);
  1190. aktcontinuelabel:=continuefinallylabel;
  1191. aktbreaklabel:=breakfinallylabel;
  1192. end;
  1193. { call setjmp, and jump to finally label on non-zero result }
  1194. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,finallylabel);
  1195. { try code }
  1196. if assigned(left) then
  1197. begin
  1198. secondpass(left);
  1199. tryflowcontrol:=flowcontrol;
  1200. if codegenerror then
  1201. exit;
  1202. end;
  1203. cg.a_label(exprasmlist,finallylabel);
  1204. { just free the frame information }
  1205. try_free_exception(exprasmlist,tempbuf,tempaddr,href,1,finallylabel,true);
  1206. { finally code }
  1207. flowcontrol:=[];
  1208. secondpass(right);
  1209. if flowcontrol<>[] then
  1210. CGMessage(cg_e_control_flow_outside_finally);
  1211. if codegenerror then
  1212. exit;
  1213. { the value should now be in the exception handler }
  1214. cg.g_exception_reason_load(exprasmlist,href);
  1215. r.enum:=accumulator;
  1216. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,endfinallylabel);
  1217. cg.a_op_const_reg(exprasmlist,OP_SUB,1,r);
  1218. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,reraiselabel);
  1219. if fc_exit in tryflowcontrol then
  1220. begin
  1221. cg.a_op_const_reg(exprasmlist,OP_SUB,1,r);
  1222. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,oldaktexitlabel);
  1223. decconst:=1;
  1224. end
  1225. else
  1226. decconst:=2;
  1227. if fc_break in tryflowcontrol then
  1228. begin
  1229. cg.a_op_const_reg(exprasmlist,OP_SUB,decconst,r);
  1230. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,oldaktbreaklabel);
  1231. decconst:=1;
  1232. end
  1233. else
  1234. inc(decconst);
  1235. if fc_continue in tryflowcontrol then
  1236. begin
  1237. cg.a_op_const_reg(exprasmlist,OP_SUB,decconst,r);
  1238. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,oldaktcontinuelabel);
  1239. end;
  1240. cg.a_label(exprasmlist,reraiselabel);
  1241. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1242. { do some magic for exit,break,continue in the try block }
  1243. if fc_exit in tryflowcontrol then
  1244. begin
  1245. cg.a_label(exprasmlist,exitfinallylabel);
  1246. cg.g_exception_reason_load(exprasmlist,href);
  1247. cg.g_exception_reason_save_const(exprasmlist,href,2);
  1248. cg.a_jmp_always(exprasmlist,finallylabel);
  1249. end;
  1250. if fc_break in tryflowcontrol then
  1251. begin
  1252. cg.a_label(exprasmlist,breakfinallylabel);
  1253. cg.g_exception_reason_load(exprasmlist,href);
  1254. cg.g_exception_reason_save_const(exprasmlist,href,3);
  1255. cg.a_jmp_always(exprasmlist,finallylabel);
  1256. end;
  1257. if fc_continue in tryflowcontrol then
  1258. begin
  1259. cg.a_label(exprasmlist,continuefinallylabel);
  1260. cg.g_exception_reason_load(exprasmlist,href);
  1261. cg.g_exception_reason_save_const(exprasmlist,href,4);
  1262. cg.a_jmp_always(exprasmlist,finallylabel);
  1263. end;
  1264. cg.a_label(exprasmlist,endfinallylabel);
  1265. aktexitlabel:=oldaktexitlabel;
  1266. aktexit2label:=oldaktexit2label;
  1267. if assigned(aktbreaklabel) then
  1268. begin
  1269. aktcontinuelabel:=oldaktcontinuelabel;
  1270. aktbreaklabel:=oldaktbreaklabel;
  1271. end;
  1272. flowcontrol:=oldflowcontrol+tryflowcontrol;
  1273. end;
  1274. begin
  1275. cwhilerepeatnode:=tcgwhilerepeatnode;
  1276. cifnode:=tcgifnode;
  1277. cfornode:=tcgfornode;
  1278. cexitnode:=tcgexitnode;
  1279. cbreaknode:=tcgbreaknode;
  1280. ccontinuenode:=tcgcontinuenode;
  1281. cgotonode:=tcggotonode;
  1282. clabelnode:=tcglabelnode;
  1283. cfailnode:=tcgfailnode;
  1284. craisenode:=tcgraisenode;
  1285. ctryexceptnode:=tcgtryexceptnode;
  1286. ctryfinallynode:=tcgtryfinallynode;
  1287. connode:=tcgonnode;
  1288. end.
  1289. {
  1290. $Log$
  1291. Revision 1.49 2003-01-08 18:43:56 daniel
  1292. * Tregister changed into a record
  1293. Revision 1.48 2003/01/03 09:51:58 daniel
  1294. * Compiler now cycles with var_notification
  1295. Revision 1.47 2003/01/02 15:29:25 daniel
  1296. * Some debugging on for loop optimization
  1297. Revision 1.46 2002/12/31 09:55:58 daniel
  1298. + Notification implementation complete
  1299. + Add for loop code optimization using notifications
  1300. results in 1.5-1.9% speed improvement in nestloop benchmark
  1301. Optimization incomplete, compiler does not cycle yet with
  1302. notifications enabled.
  1303. Revision 1.45 2002/11/28 11:17:01 florian
  1304. * loop node flags from node flags splitted
  1305. Revision 1.44 2002/11/25 17:43:17 peter
  1306. * splitted defbase in defutil,symutil,defcmp
  1307. * merged isconvertable and is_equal into compare_defs(_ext)
  1308. * made operator search faster by walking the list only once
  1309. Revision 1.43 2002/09/30 07:00:45 florian
  1310. * fixes to common code to get the alpha compiler compiled applied
  1311. Revision 1.42 2002/09/07 15:25:02 peter
  1312. * old logs removed and tabs fixed
  1313. Revision 1.41 2002/09/01 18:47:00 peter
  1314. * assignn check in exitnode changed to use a separate boolean as the
  1315. assignn can be changed to a calln
  1316. Revision 1.40 2002/09/01 14:41:47 peter
  1317. * increase refcount in exit(arg) for arg
  1318. Revision 1.39 2002/08/24 18:41:52 peter
  1319. * fixed wrong label in jump of except block (was also in n386flw wrong)
  1320. * fixed wrong pushing of raise parameters
  1321. * fixed wrong compare in finally
  1322. Revision 1.38 2002/08/23 16:14:48 peter
  1323. * tempgen cleanup
  1324. * tt_noreuse temp type added that will be used in genentrycode
  1325. Revision 1.37 2002/08/19 19:36:43 peter
  1326. * More fixes for cross unit inlining, all tnodes are now implemented
  1327. * Moved pocall_internconst to po_internconst because it is not a
  1328. calling type at all and it conflicted when inlining of these small
  1329. functions was requested
  1330. Revision 1.36 2002/08/15 15:15:55 carl
  1331. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  1332. * more generic nodes for maths
  1333. * several fixes for better m68k support
  1334. Revision 1.35 2002/08/13 18:01:52 carl
  1335. * rename swatoperands to swapoperands
  1336. + m68k first compilable version (still needs a lot of testing):
  1337. assembler generator, system information , inline
  1338. assembler reader.
  1339. Revision 1.34 2002/08/11 14:32:26 peter
  1340. * renamed current_library to objectlibrary
  1341. Revision 1.33 2002/08/11 13:24:11 peter
  1342. * saving of asmsymbols in ppu supported
  1343. * asmsymbollist global is removed and moved into a new class
  1344. tasmlibrarydata that will hold the info of a .a file which
  1345. corresponds with a single module. Added librarydata to tmodule
  1346. to keep the library info stored for the module. In the future the
  1347. objectfiles will also be stored to the tasmlibrarydata class
  1348. * all getlabel/newasmsymbol and friends are moved to the new class
  1349. Revision 1.32 2002/08/09 19:10:59 carl
  1350. * fixed generic exception management
  1351. Revision 1.31 2002/08/04 19:06:41 carl
  1352. + added generic exception support (still does not work!)
  1353. + more documentation
  1354. Revision 1.30 2002/07/27 19:53:51 jonas
  1355. + generic implementation of tcg.g_flags2ref()
  1356. * tcg.flags2xxx() now also needs a size parameter
  1357. Revision 1.29 2002/07/25 17:56:29 carl
  1358. + FPURESULTREG -> FPU_RESULT_REG
  1359. Revision 1.28 2002/07/21 06:58:49 daniel
  1360. * Changed booleans into flags
  1361. Revision 1.27 2002/07/20 12:54:53 daniel
  1362. * Optimized the code generated for for nodes. The shootout/nestloop benchmark
  1363. now runs 5% faster on my computer.
  1364. Revision 1.26 2002/07/20 11:57:54 florian
  1365. * types.pas renamed to defbase.pas because D6 contains a types
  1366. unit so this would conflicts if D6 programms are compiled
  1367. + Willamette/SSE2 instructions to assembler added
  1368. Revision 1.25 2002/07/20 11:15:51 daniel
  1369. * The for node does a check if the first comparision can be skipped. I moved
  1370. the check from the second pass to the resulttype pass. The advantage is
  1371. that the state tracker can now decide to skip the first comparision too.
  1372. Revision 1.24 2002/07/20 08:14:24 daniel
  1373. * Loops should not be aligned when optimizing for size
  1374. Revision 1.23 2002/07/19 11:41:35 daniel
  1375. * State tracker work
  1376. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1377. allows the state tracker to change while nodes automatically into
  1378. repeat nodes.
  1379. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1380. 'not(a>b)' is optimized into 'a<=b'.
  1381. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1382. by removing the notn and later switchting the true and falselabels. The
  1383. same is done with 'repeat until not a'.
  1384. Revision 1.22 2002/07/04 20:43:01 florian
  1385. * first x86-64 patches
  1386. }