ncgflw.pas 58 KB

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