ncgflw.pas 60 KB

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