ncgflw.pas 56 KB

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