ncgflw.pas 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  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. hreg.enum:=R_INTREGISTER;
  728. hreg.number:=NR_ACCUMULATORHIGH;
  729. if allocated_acc then
  730. cg.a_reg_dealloc(exprasmlist,r);
  731. {$ifndef cpu64bit}
  732. if allocated_acchigh then
  733. cg.a_reg_dealloc(exprasmlist,hreg);
  734. {$endif cpu64bit}
  735. {$ifndef i386}
  736. r.enum:=fpu_result_reg;
  737. if (current_procdef.rettype.def.deftype = floatdef) then
  738. cg.a_reg_dealloc(exprasmlist,r);
  739. {$endif not i386}
  740. end;
  741. end
  742. else
  743. cg.a_jmp_always(exprasmlist,aktexitlabel);
  744. end;
  745. {*****************************************************************************
  746. SecondBreakN
  747. *****************************************************************************}
  748. procedure tcgbreaknode.pass_2;
  749. begin
  750. location_reset(location,LOC_VOID,OS_NO);
  751. include(flowcontrol,fc_break);
  752. if aktbreaklabel<>nil then
  753. begin
  754. load_all_regvars(exprasmlist);
  755. cg.a_jmp_always(exprasmlist,aktbreaklabel)
  756. end
  757. else
  758. CGMessage(cg_e_break_not_allowed);
  759. end;
  760. {*****************************************************************************
  761. SecondContinueN
  762. *****************************************************************************}
  763. procedure tcgcontinuenode.pass_2;
  764. begin
  765. location_reset(location,LOC_VOID,OS_NO);
  766. include(flowcontrol,fc_continue);
  767. if aktcontinuelabel<>nil then
  768. begin
  769. load_all_regvars(exprasmlist);
  770. cg.a_jmp_always(exprasmlist,aktcontinuelabel)
  771. end
  772. else
  773. CGMessage(cg_e_continue_not_allowed);
  774. end;
  775. {*****************************************************************************
  776. SecondGoto
  777. *****************************************************************************}
  778. procedure tcggotonode.pass_2;
  779. begin
  780. location_reset(location,LOC_VOID,OS_NO);
  781. load_all_regvars(exprasmlist);
  782. cg.a_jmp_always(exprasmlist,labsym.lab)
  783. end;
  784. {*****************************************************************************
  785. SecondLabel
  786. *****************************************************************************}
  787. procedure tcglabelnode.pass_2;
  788. begin
  789. location_reset(location,LOC_VOID,OS_NO);
  790. load_all_regvars(exprasmlist);
  791. cg.a_label(exprasmlist,labelnr);
  792. {$ifndef newra}
  793. rg.cleartempgen;
  794. {$endif newra}
  795. secondpass(left);
  796. end;
  797. {*****************************************************************************
  798. SecondFail
  799. *****************************************************************************}
  800. procedure tcgfailnode.pass_2;
  801. begin
  802. location_reset(location,LOC_VOID,OS_NO);
  803. cg.a_jmp_always(exprasmlist,faillabel);
  804. end;
  805. {*****************************************************************************
  806. SecondRaise
  807. *****************************************************************************}
  808. procedure tcgraisenode.pass_2;
  809. var
  810. a : tasmlabel;
  811. href2: treference;
  812. r:Tregister;
  813. begin
  814. location_reset(location,LOC_VOID,OS_NO);
  815. if assigned(left) then
  816. begin
  817. { multiple parameters? }
  818. if assigned(right) then
  819. begin
  820. { push frame }
  821. if assigned(frametree) then
  822. begin
  823. secondpass(frametree);
  824. if codegenerror then
  825. exit;
  826. cg.a_param_loc(exprasmlist,frametree.location,paramanager.getintparaloc(2));
  827. end
  828. else
  829. cg.a_param_const(exprasmlist,OS_INT,0,paramanager.getintparaloc(2));
  830. { push address }
  831. secondpass(right);
  832. if codegenerror then
  833. exit;
  834. cg.a_param_loc(exprasmlist,right.location,paramanager.getintparaloc(1));
  835. end
  836. else
  837. begin
  838. { get current address }
  839. objectlibrary.getaddrlabel(a);
  840. cg.a_label(exprasmlist,a);
  841. reference_reset_symbol(href2,a,0);
  842. { push current frame }
  843. r.enum:=R_INTREGISTER;
  844. r.number:=NR_FRAME_POINTER_REG;
  845. cg.a_param_reg(exprasmlist,OS_ADDR,r,paramanager.getintparaloc(2));
  846. { push current address }
  847. cg.a_paramaddr_ref(exprasmlist,href2,paramanager.getintparaloc(1));
  848. end;
  849. { push object }
  850. secondpass(left);
  851. if codegenerror then
  852. exit;
  853. cg.a_param_loc(exprasmlist,left.location,paramanager.getintparaloc(1));
  854. cg.a_call_name(exprasmlist,'FPC_RAISEEXCEPTION');
  855. end
  856. else
  857. begin
  858. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  859. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  860. end;
  861. end;
  862. {*****************************************************************************
  863. SecondTryExcept
  864. *****************************************************************************}
  865. var
  866. endexceptlabel : tasmlabel;
  867. procedure try_new_exception(list : taasmoutput;var jmpbuf,envbuf, href : treference;
  868. a : aword; exceptlabel : tasmlabel);
  869. begin
  870. tg.GetTemp(list,EXCEPT_BUF_SIZE,tt_persistant,envbuf);
  871. tg.GetTemp(list,JMP_BUF_SIZE,tt_persistant,jmpbuf);
  872. tg.GetTemp(list,sizeof(aword),tt_persistant,href);
  873. new_exception(list, jmpbuf,envbuf, href, a, exceptlabel);
  874. end;
  875. procedure try_free_exception(list : taasmoutput;var jmpbuf, envbuf, href : treference;
  876. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  877. begin
  878. free_exception(list, jmpbuf, envbuf, href, a, endexceptlabel, onlyfree);
  879. tg.ungettemp(list,href);
  880. tg.Ungettemp(list,jmpbuf);
  881. tg.ungettemp(list,envbuf);
  882. end;
  883. { does the necessary things to clean up the object stack }
  884. { in the except block }
  885. procedure cleanupobjectstack;
  886. var r:Tregister;
  887. begin
  888. cg.a_call_name(exprasmlist,'FPC_POPOBJECTSTACK');
  889. r.enum:=R_INTREGISTER;
  890. r.number:=NR_ACCUMULATOR;
  891. cg.a_param_reg(exprasmlist,OS_ADDR,r,paramanager.getintparaloc(1));
  892. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  893. end;
  894. procedure tcgtryexceptnode.pass_2;
  895. var
  896. exceptlabel,doexceptlabel,oldendexceptlabel,
  897. lastonlabel,
  898. exitexceptlabel,
  899. continueexceptlabel,
  900. breakexceptlabel,
  901. exittrylabel,
  902. continuetrylabel,
  903. breaktrylabel,
  904. doobjectdestroy,
  905. doobjectdestroyandreraise,
  906. oldaktexitlabel,
  907. oldaktexit2label,
  908. oldaktcontinuelabel,
  909. oldaktbreaklabel : tasmlabel;
  910. oldflowcontrol,tryflowcontrol,
  911. exceptflowcontrol : tflowcontrol;
  912. tempbuf,tempaddr : treference;
  913. href : treference;
  914. r:Tregister;
  915. label
  916. errorexit;
  917. begin
  918. location_reset(location,LOC_VOID,OS_NO);
  919. oldflowcontrol:=flowcontrol;
  920. flowcontrol:=[];
  921. { this can be called recursivly }
  922. oldendexceptlabel:=endexceptlabel;
  923. { save the old labels for control flow statements }
  924. oldaktexitlabel:=aktexitlabel;
  925. oldaktexit2label:=aktexit2label;
  926. if assigned(aktbreaklabel) then
  927. begin
  928. oldaktcontinuelabel:=aktcontinuelabel;
  929. oldaktbreaklabel:=aktbreaklabel;
  930. end;
  931. { get new labels for the control flow statements }
  932. objectlibrary.getlabel(exittrylabel);
  933. objectlibrary.getlabel(exitexceptlabel);
  934. if assigned(aktbreaklabel) then
  935. begin
  936. objectlibrary.getlabel(breaktrylabel);
  937. objectlibrary.getlabel(continuetrylabel);
  938. objectlibrary.getlabel(breakexceptlabel);
  939. objectlibrary.getlabel(continueexceptlabel);
  940. end;
  941. objectlibrary.getlabel(exceptlabel);
  942. objectlibrary.getlabel(doexceptlabel);
  943. objectlibrary.getlabel(endexceptlabel);
  944. objectlibrary.getlabel(lastonlabel);
  945. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,exceptlabel);
  946. { try block }
  947. { set control flow labels for the try block }
  948. aktexitlabel:=exittrylabel;
  949. aktexit2label:=exittrylabel;
  950. if assigned(oldaktbreaklabel) then
  951. begin
  952. aktcontinuelabel:=continuetrylabel;
  953. aktbreaklabel:=breaktrylabel;
  954. end;
  955. flowcontrol:=[];
  956. secondpass(left);
  957. tryflowcontrol:=flowcontrol;
  958. if codegenerror then
  959. goto errorexit;
  960. cg.a_label(exprasmlist,exceptlabel);
  961. try_free_exception(exprasmlist,tempbuf,tempaddr,href,0,endexceptlabel,false);
  962. cg.a_label(exprasmlist,doexceptlabel);
  963. { set control flow labels for the except block }
  964. { and the on statements }
  965. aktexitlabel:=exitexceptlabel;
  966. aktexit2label:=exitexceptlabel;
  967. if assigned(oldaktbreaklabel) then
  968. begin
  969. aktcontinuelabel:=continueexceptlabel;
  970. aktbreaklabel:=breakexceptlabel;
  971. end;
  972. flowcontrol:=[];
  973. { on statements }
  974. if assigned(right) then
  975. secondpass(right);
  976. cg.a_label(exprasmlist,lastonlabel);
  977. { default handling except handling }
  978. if assigned(t1) then
  979. begin
  980. { FPC_CATCHES must be called with
  981. 'default handler' flag (=-1)
  982. }
  983. cg.a_param_const(exprasmlist,OS_ADDR,aword(-1),paramanager.getintparaloc(1));
  984. cg.a_call_name(exprasmlist,'FPC_CATCHES');
  985. { the destruction of the exception object must be also }
  986. { guarded by an exception frame }
  987. objectlibrary.getlabel(doobjectdestroy);
  988. objectlibrary.getlabel(doobjectdestroyandreraise);
  989. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,doobjectdestroyandreraise);
  990. { here we don't have to reset flowcontrol }
  991. { the default and on flowcontrols are handled equal }
  992. secondpass(t1);
  993. exceptflowcontrol:=flowcontrol;
  994. cg.a_label(exprasmlist,doobjectdestroyandreraise);
  995. try_free_exception(exprasmlist,tempbuf,tempaddr,href,0,doobjectdestroy,false);
  996. cg.a_call_name(exprasmlist,'FPC_POPSECONDOBJECTSTACK');
  997. r.enum:=R_INTREGISTER;
  998. r.number:=NR_ACCUMULATOR;
  999. cg.a_param_reg(exprasmlist, OS_ADDR, r, paramanager.getintparaloc(1));
  1000. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  1001. { we don't need to restore esi here because reraise never }
  1002. { returns }
  1003. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1004. cg.a_label(exprasmlist,doobjectdestroy);
  1005. cleanupobjectstack;
  1006. cg.a_jmp_always(exprasmlist,endexceptlabel);
  1007. end
  1008. else
  1009. begin
  1010. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1011. exceptflowcontrol:=flowcontrol;
  1012. end;
  1013. if fc_exit in exceptflowcontrol then
  1014. begin
  1015. { do some magic for exit in the try block }
  1016. cg.a_label(exprasmlist,exitexceptlabel);
  1017. { we must also destroy the address frame which guards }
  1018. { exception object }
  1019. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1020. cg.g_exception_reason_load(exprasmlist,href);
  1021. cleanupobjectstack;
  1022. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  1023. end;
  1024. if fc_break in exceptflowcontrol then
  1025. begin
  1026. cg.a_label(exprasmlist,breakexceptlabel);
  1027. { we must also destroy the address frame which guards }
  1028. { exception object }
  1029. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1030. cg.g_exception_reason_load(exprasmlist,href);
  1031. cleanupobjectstack;
  1032. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  1033. end;
  1034. if fc_continue in exceptflowcontrol then
  1035. begin
  1036. cg.a_label(exprasmlist,continueexceptlabel);
  1037. { we must also destroy the address frame which guards }
  1038. { exception object }
  1039. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1040. cg.g_exception_reason_load(exprasmlist,href);
  1041. cleanupobjectstack;
  1042. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  1043. end;
  1044. if fc_exit in tryflowcontrol then
  1045. begin
  1046. { do some magic for exit in the try block }
  1047. cg.a_label(exprasmlist,exittrylabel);
  1048. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1049. cg.g_exception_reason_load(exprasmlist,href);
  1050. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  1051. end;
  1052. if fc_break in tryflowcontrol then
  1053. begin
  1054. cg.a_label(exprasmlist,breaktrylabel);
  1055. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1056. cg.g_exception_reason_load(exprasmlist,href);
  1057. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  1058. end;
  1059. if fc_continue in tryflowcontrol then
  1060. begin
  1061. cg.a_label(exprasmlist,continuetrylabel);
  1062. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1063. cg.g_exception_reason_load(exprasmlist,href);
  1064. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  1065. end;
  1066. cg.a_label(exprasmlist,endexceptlabel);
  1067. errorexit:
  1068. { restore all saved labels }
  1069. endexceptlabel:=oldendexceptlabel;
  1070. { restore the control flow labels }
  1071. aktexitlabel:=oldaktexitlabel;
  1072. aktexit2label:=oldaktexit2label;
  1073. if assigned(oldaktbreaklabel) then
  1074. begin
  1075. aktcontinuelabel:=oldaktcontinuelabel;
  1076. aktbreaklabel:=oldaktbreaklabel;
  1077. end;
  1078. { return all used control flow statements }
  1079. flowcontrol:=oldflowcontrol+exceptflowcontrol+
  1080. tryflowcontrol;
  1081. end;
  1082. procedure tcgonnode.pass_2;
  1083. var
  1084. nextonlabel,
  1085. exitonlabel,
  1086. continueonlabel,
  1087. breakonlabel,
  1088. oldaktexitlabel,
  1089. oldaktexit2label,
  1090. oldaktcontinuelabel,
  1091. doobjectdestroyandreraise,
  1092. doobjectdestroy,
  1093. oldaktbreaklabel : tasmlabel;
  1094. ref : treference;
  1095. oldflowcontrol : tflowcontrol;
  1096. tempbuf,tempaddr : treference;
  1097. href : treference;
  1098. href2: treference;
  1099. r:Tregister;
  1100. begin
  1101. location_reset(location,LOC_VOID,OS_NO);
  1102. r.enum:=R_INTREGISTER;
  1103. r.number:=NR_ACCUMULATOR;
  1104. oldflowcontrol:=flowcontrol;
  1105. flowcontrol:=[];
  1106. objectlibrary.getlabel(nextonlabel);
  1107. { send the vmt parameter }
  1108. reference_reset_symbol(href2,objectlibrary.newasmsymboldata(excepttype.vmt_mangledname),0);
  1109. cg.a_paramaddr_ref(exprasmlist,href2,paramanager.getintparaloc(1));
  1110. cg.a_call_name(exprasmlist,'FPC_CATCHES');
  1111. { is it this catch? No. go to next onlabel }
  1112. cg.a_cmp_const_reg_label(exprasmlist,OS_ADDR,OC_EQ,0,r,nextonlabel);
  1113. ref.symbol:=nil;
  1114. tg.GetTemp(exprasmlist,pointer_size,tt_normal,ref);
  1115. { what a hack ! }
  1116. if assigned(exceptsymtable) then
  1117. tvarsym(exceptsymtable.symindex.first).address:=ref.offset;
  1118. cg.a_load_reg_ref(exprasmlist, OS_ADDR, r, ref);
  1119. { in the case that another exception is risen }
  1120. { we've to destroy the old one }
  1121. objectlibrary.getlabel(doobjectdestroyandreraise);
  1122. { call setjmp, and jump to finally label on non-zero result }
  1123. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,doobjectdestroyandreraise);
  1124. if assigned(right) then
  1125. begin
  1126. oldaktexitlabel:=aktexitlabel;
  1127. oldaktexit2label:=aktexit2label;
  1128. objectlibrary.getlabel(exitonlabel);
  1129. aktexitlabel:=exitonlabel;
  1130. aktexit2label:=exitonlabel;
  1131. if assigned(aktbreaklabel) then
  1132. begin
  1133. oldaktcontinuelabel:=aktcontinuelabel;
  1134. oldaktbreaklabel:=aktbreaklabel;
  1135. objectlibrary.getlabel(breakonlabel);
  1136. objectlibrary.getlabel(continueonlabel);
  1137. aktcontinuelabel:=continueonlabel;
  1138. aktbreaklabel:=breakonlabel;
  1139. end;
  1140. secondpass(right);
  1141. end;
  1142. objectlibrary.getlabel(doobjectdestroy);
  1143. cg.a_label(exprasmlist,doobjectdestroyandreraise);
  1144. try_free_exception(exprasmlist,tempbuf,tempaddr,href,0,doobjectdestroy,false);
  1145. cg.a_call_name(exprasmlist,'FPC_POPSECONDOBJECTSTACK');
  1146. cg.a_param_reg(exprasmlist, OS_ADDR, r, paramanager.getintparaloc(1));
  1147. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  1148. { we don't need to restore esi here because reraise never }
  1149. { returns }
  1150. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1151. cg.a_label(exprasmlist,doobjectdestroy);
  1152. cleanupobjectstack;
  1153. { clear some stuff }
  1154. tg.ungetiftemp(exprasmlist,ref);
  1155. cg.a_jmp_always(exprasmlist,endexceptlabel);
  1156. if assigned(right) then
  1157. begin
  1158. { special handling for control flow instructions }
  1159. if fc_exit in flowcontrol then
  1160. begin
  1161. { the address and object pop does secondtryexcept }
  1162. cg.a_label(exprasmlist,exitonlabel);
  1163. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  1164. end;
  1165. if fc_break in flowcontrol then
  1166. begin
  1167. { the address and object pop does secondtryexcept }
  1168. cg.a_label(exprasmlist,breakonlabel);
  1169. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  1170. end;
  1171. if fc_continue in flowcontrol then
  1172. begin
  1173. { the address and object pop does secondtryexcept }
  1174. cg.a_label(exprasmlist,continueonlabel);
  1175. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  1176. end;
  1177. aktexitlabel:=oldaktexitlabel;
  1178. aktexit2label:=oldaktexit2label;
  1179. if assigned(oldaktbreaklabel) then
  1180. begin
  1181. aktcontinuelabel:=oldaktcontinuelabel;
  1182. aktbreaklabel:=oldaktbreaklabel;
  1183. end;
  1184. end;
  1185. cg.a_label(exprasmlist,nextonlabel);
  1186. flowcontrol:=oldflowcontrol+flowcontrol;
  1187. { next on node }
  1188. if assigned(left) then
  1189. begin
  1190. {$ifndef newra}
  1191. rg.cleartempgen;
  1192. {$endif newra}
  1193. secondpass(left);
  1194. end;
  1195. end;
  1196. {*****************************************************************************
  1197. SecondTryFinally
  1198. *****************************************************************************}
  1199. procedure tcgtryfinallynode.pass_2;
  1200. var
  1201. reraiselabel,
  1202. finallylabel,
  1203. endfinallylabel,
  1204. exitfinallylabel,
  1205. continuefinallylabel,
  1206. breakfinallylabel,
  1207. oldaktexitlabel,
  1208. oldaktexit2label,
  1209. oldaktcontinuelabel,
  1210. oldaktbreaklabel : tasmlabel;
  1211. oldflowcontrol,tryflowcontrol : tflowcontrol;
  1212. decconst : longint;
  1213. tempbuf,tempaddr : treference;
  1214. href : treference;
  1215. r:Tregister;
  1216. begin
  1217. location_reset(location,LOC_VOID,OS_NO);
  1218. { check if child nodes do a break/continue/exit }
  1219. oldflowcontrol:=flowcontrol;
  1220. flowcontrol:=[];
  1221. objectlibrary.getlabel(finallylabel);
  1222. objectlibrary.getlabel(endfinallylabel);
  1223. objectlibrary.getlabel(reraiselabel);
  1224. { the finally block must catch break, continue and exit }
  1225. { statements }
  1226. oldaktexitlabel:=aktexitlabel;
  1227. oldaktexit2label:=aktexit2label;
  1228. objectlibrary.getlabel(exitfinallylabel);
  1229. aktexitlabel:=exitfinallylabel;
  1230. aktexit2label:=exitfinallylabel;
  1231. if assigned(aktbreaklabel) then
  1232. begin
  1233. oldaktcontinuelabel:=aktcontinuelabel;
  1234. oldaktbreaklabel:=aktbreaklabel;
  1235. objectlibrary.getlabel(breakfinallylabel);
  1236. objectlibrary.getlabel(continuefinallylabel);
  1237. aktcontinuelabel:=continuefinallylabel;
  1238. aktbreaklabel:=breakfinallylabel;
  1239. end;
  1240. { call setjmp, and jump to finally label on non-zero result }
  1241. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,finallylabel);
  1242. { try code }
  1243. if assigned(left) then
  1244. begin
  1245. secondpass(left);
  1246. tryflowcontrol:=flowcontrol;
  1247. if codegenerror then
  1248. exit;
  1249. end;
  1250. cg.a_label(exprasmlist,finallylabel);
  1251. { just free the frame information }
  1252. try_free_exception(exprasmlist,tempbuf,tempaddr,href,1,finallylabel,true);
  1253. { finally code }
  1254. flowcontrol:=[];
  1255. secondpass(right);
  1256. if flowcontrol<>[] then
  1257. CGMessage(cg_e_control_flow_outside_finally);
  1258. if codegenerror then
  1259. exit;
  1260. { the value should now be in the exception handler }
  1261. cg.g_exception_reason_load(exprasmlist,href);
  1262. r.enum:=R_INTREGISTER;
  1263. r.number:=NR_ACCUMULATOR;
  1264. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,endfinallylabel);
  1265. cg.a_op_const_reg(exprasmlist,OP_SUB,1,r);
  1266. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,reraiselabel);
  1267. if fc_exit in tryflowcontrol then
  1268. begin
  1269. cg.a_op_const_reg(exprasmlist,OP_SUB,1,r);
  1270. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,oldaktexitlabel);
  1271. decconst:=1;
  1272. end
  1273. else
  1274. decconst:=2;
  1275. if fc_break in tryflowcontrol then
  1276. begin
  1277. cg.a_op_const_reg(exprasmlist,OP_SUB,decconst,r);
  1278. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,oldaktbreaklabel);
  1279. decconst:=1;
  1280. end
  1281. else
  1282. inc(decconst);
  1283. if fc_continue in tryflowcontrol then
  1284. begin
  1285. cg.a_op_const_reg(exprasmlist,OP_SUB,decconst,r);
  1286. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,oldaktcontinuelabel);
  1287. end;
  1288. cg.a_label(exprasmlist,reraiselabel);
  1289. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1290. { do some magic for exit,break,continue in the try block }
  1291. if fc_exit in tryflowcontrol then
  1292. begin
  1293. cg.a_label(exprasmlist,exitfinallylabel);
  1294. cg.g_exception_reason_load(exprasmlist,href);
  1295. cg.g_exception_reason_save_const(exprasmlist,href,2);
  1296. cg.a_jmp_always(exprasmlist,finallylabel);
  1297. end;
  1298. if fc_break in tryflowcontrol then
  1299. begin
  1300. cg.a_label(exprasmlist,breakfinallylabel);
  1301. cg.g_exception_reason_load(exprasmlist,href);
  1302. cg.g_exception_reason_save_const(exprasmlist,href,3);
  1303. cg.a_jmp_always(exprasmlist,finallylabel);
  1304. end;
  1305. if fc_continue in tryflowcontrol then
  1306. begin
  1307. cg.a_label(exprasmlist,continuefinallylabel);
  1308. cg.g_exception_reason_load(exprasmlist,href);
  1309. cg.g_exception_reason_save_const(exprasmlist,href,4);
  1310. cg.a_jmp_always(exprasmlist,finallylabel);
  1311. end;
  1312. cg.a_label(exprasmlist,endfinallylabel);
  1313. aktexitlabel:=oldaktexitlabel;
  1314. aktexit2label:=oldaktexit2label;
  1315. if assigned(aktbreaklabel) then
  1316. begin
  1317. aktcontinuelabel:=oldaktcontinuelabel;
  1318. aktbreaklabel:=oldaktbreaklabel;
  1319. end;
  1320. flowcontrol:=oldflowcontrol+tryflowcontrol;
  1321. end;
  1322. begin
  1323. cwhilerepeatnode:=tcgwhilerepeatnode;
  1324. cifnode:=tcgifnode;
  1325. cfornode:=tcgfornode;
  1326. cexitnode:=tcgexitnode;
  1327. cbreaknode:=tcgbreaknode;
  1328. ccontinuenode:=tcgcontinuenode;
  1329. cgotonode:=tcggotonode;
  1330. clabelnode:=tcglabelnode;
  1331. cfailnode:=tcgfailnode;
  1332. craisenode:=tcgraisenode;
  1333. ctryexceptnode:=tcgtryexceptnode;
  1334. ctryfinallynode:=tcgtryfinallynode;
  1335. connode:=tcgonnode;
  1336. end.
  1337. {
  1338. $Log$
  1339. Revision 1.57 2003-04-29 07:29:14 michael
  1340. + Patch from peter to fix wrong pushing of ansistring function results in open array
  1341. Revision 1.56 2003/04/27 11:21:33 peter
  1342. * aktprocdef renamed to current_procdef
  1343. * procinfo renamed to current_procinfo
  1344. * procinfo will now be stored in current_module so it can be
  1345. cleaned up properly
  1346. * gen_main_procsym changed to create_main_proc and release_main_proc
  1347. to also generate a tprocinfo structure
  1348. * fixed unit implicit initfinal
  1349. Revision 1.55 2003/04/22 23:50:22 peter
  1350. * firstpass uses expectloc
  1351. * checks if there are differences between the expectloc and
  1352. location.loc from secondpass in EXTDEBUG
  1353. Revision 1.54 2003/04/17 07:50:24 daniel
  1354. * Some work on interference graph construction
  1355. Revision 1.53 2003/04/06 21:11:23 olle
  1356. * changed newasmsymbol to newasmsymboldata for data symbols
  1357. Revision 1.52 2003/03/28 19:16:56 peter
  1358. * generic constructor working for i386
  1359. * remove fixed self register
  1360. * esi added as address register for i386
  1361. Revision 1.51 2003/02/19 22:00:14 daniel
  1362. * Code generator converted to new register notation
  1363. - Horribily outdated todo.txt removed
  1364. Revision 1.50 2003/02/15 22:17:38 carl
  1365. * bugfix of FPU emulation code
  1366. Revision 1.49 2003/01/08 18:43:56 daniel
  1367. * Tregister changed into a record
  1368. Revision 1.48 2003/01/03 09:51:58 daniel
  1369. * Compiler now cycles with var_notification
  1370. Revision 1.47 2003/01/02 15:29:25 daniel
  1371. * Some debugging on for loop optimization
  1372. Revision 1.46 2002/12/31 09:55:58 daniel
  1373. + Notification implementation complete
  1374. + Add for loop code optimization using notifications
  1375. results in 1.5-1.9% speed improvement in nestloop benchmark
  1376. Optimization incomplete, compiler does not cycle yet with
  1377. notifications enabled.
  1378. Revision 1.45 2002/11/28 11:17:01 florian
  1379. * loop node flags from node flags splitted
  1380. Revision 1.44 2002/11/25 17:43:17 peter
  1381. * splitted defbase in defutil,symutil,defcmp
  1382. * merged isconvertable and is_equal into compare_defs(_ext)
  1383. * made operator search faster by walking the list only once
  1384. Revision 1.43 2002/09/30 07:00:45 florian
  1385. * fixes to common code to get the alpha compiler compiled applied
  1386. Revision 1.42 2002/09/07 15:25:02 peter
  1387. * old logs removed and tabs fixed
  1388. Revision 1.41 2002/09/01 18:47:00 peter
  1389. * assignn check in exitnode changed to use a separate boolean as the
  1390. assignn can be changed to a calln
  1391. Revision 1.40 2002/09/01 14:41:47 peter
  1392. * increase refcount in exit(arg) for arg
  1393. Revision 1.39 2002/08/24 18:41:52 peter
  1394. * fixed wrong label in jump of except block (was also in n386flw wrong)
  1395. * fixed wrong pushing of raise parameters
  1396. * fixed wrong compare in finally
  1397. Revision 1.38 2002/08/23 16:14:48 peter
  1398. * tempgen cleanup
  1399. * tt_noreuse temp type added that will be used in genentrycode
  1400. Revision 1.37 2002/08/19 19:36:43 peter
  1401. * More fixes for cross unit inlining, all tnodes are now implemented
  1402. * Moved pocall_internconst to po_internconst because it is not a
  1403. calling type at all and it conflicted when inlining of these small
  1404. functions was requested
  1405. Revision 1.36 2002/08/15 15:15:55 carl
  1406. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  1407. * more generic nodes for maths
  1408. * several fixes for better m68k support
  1409. Revision 1.35 2002/08/13 18:01:52 carl
  1410. * rename swatoperands to swapoperands
  1411. + m68k first compilable version (still needs a lot of testing):
  1412. assembler generator, system information , inline
  1413. assembler reader.
  1414. Revision 1.34 2002/08/11 14:32:26 peter
  1415. * renamed current_library to objectlibrary
  1416. Revision 1.33 2002/08/11 13:24:11 peter
  1417. * saving of asmsymbols in ppu supported
  1418. * asmsymbollist global is removed and moved into a new class
  1419. tasmlibrarydata that will hold the info of a .a file which
  1420. corresponds with a single module. Added librarydata to tmodule
  1421. to keep the library info stored for the module. In the future the
  1422. objectfiles will also be stored to the tasmlibrarydata class
  1423. * all getlabel/newasmsymbol and friends are moved to the new class
  1424. Revision 1.32 2002/08/09 19:10:59 carl
  1425. * fixed generic exception management
  1426. Revision 1.31 2002/08/04 19:06:41 carl
  1427. + added generic exception support (still does not work!)
  1428. + more documentation
  1429. Revision 1.30 2002/07/27 19:53:51 jonas
  1430. + generic implementation of tcg.g_flags2ref()
  1431. * tcg.flags2xxx() now also needs a size parameter
  1432. Revision 1.29 2002/07/25 17:56:29 carl
  1433. + FPURESULTREG -> FPU_RESULT_REG
  1434. Revision 1.28 2002/07/21 06:58:49 daniel
  1435. * Changed booleans into flags
  1436. Revision 1.27 2002/07/20 12:54:53 daniel
  1437. * Optimized the code generated for for nodes. The shootout/nestloop benchmark
  1438. now runs 5% faster on my computer.
  1439. Revision 1.26 2002/07/20 11:57:54 florian
  1440. * types.pas renamed to defbase.pas because D6 contains a types
  1441. unit so this would conflicts if D6 programms are compiled
  1442. + Willamette/SSE2 instructions to assembler added
  1443. Revision 1.25 2002/07/20 11:15:51 daniel
  1444. * The for node does a check if the first comparision can be skipped. I moved
  1445. the check from the second pass to the resulttype pass. The advantage is
  1446. that the state tracker can now decide to skip the first comparision too.
  1447. Revision 1.24 2002/07/20 08:14:24 daniel
  1448. * Loops should not be aligned when optimizing for size
  1449. Revision 1.23 2002/07/19 11:41:35 daniel
  1450. * State tracker work
  1451. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1452. allows the state tracker to change while nodes automatically into
  1453. repeat nodes.
  1454. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1455. 'not(a>b)' is optimized into 'a<=b'.
  1456. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1457. by removing the notn and later switchting the true and falselabels. The
  1458. same is done with 'repeat until not a'.
  1459. Revision 1.22 2002/07/04 20:43:01 florian
  1460. * first x86-64 patches
  1461. }