ncgflw.pas 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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,symdef,symsym,aasmbase,aasmtai,aasmcpu,defutil,
  65. procinfo,cgbase,pass_2,parabase,
  66. cpubase,cpuinfo,
  67. nld,ncon,
  68. ncgutil,
  69. tgobj,paramgr,
  70. regvars,
  71. cgutils,cgobj
  72. ;
  73. {*****************************************************************************
  74. Second_While_RepeatN
  75. *****************************************************************************}
  76. procedure tcgwhilerepeatnode.pass_2;
  77. var
  78. lcont,lbreak,lloop,
  79. oldclabel,oldblabel : tasmlabel;
  80. otlabel,oflabel : tasmlabel;
  81. oldflowcontrol : tflowcontrol;
  82. begin
  83. location_reset(location,LOC_VOID,OS_NO);
  84. objectlibrary.getlabel(lloop);
  85. objectlibrary.getlabel(lcont);
  86. objectlibrary.getlabel(lbreak);
  87. { arrange continue and breaklabels: }
  88. oldflowcontrol:=flowcontrol;
  89. oldclabel:=aktcontinuelabel;
  90. oldblabel:=aktbreaklabel;
  91. {$ifdef OLDREGVARS}
  92. load_all_regvars(exprasmlist);
  93. {$endif OLDREGVARS}
  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. if assigned(right) then
  105. secondpass(right);
  106. {$ifdef OLDREGVARS}
  107. load_all_regvars(exprasmlist);
  108. {$endif OLDREGVARS}
  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. (*
  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. *)
  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. (*
  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. *)
  165. maketojumpbool(exprasmlist,left,lr_dont_load_regvars);
  166. (*
  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. *)
  173. if assigned(right) then
  174. begin
  175. cg.a_label(exprasmlist,truelabel);
  176. secondpass(right);
  177. end;
  178. { save current asmlist (previous instructions + then-block) and }
  179. { loaded regvar state and create new clean ones }
  180. if cs_regvars in aktglobalswitches then
  181. begin
  182. { then_regvar_loaded_int := rg.regvar_loaded_int;
  183. then_regvar_loaded_other := rg.regvar_loaded_other;
  184. rg.regvar_loaded_int := org_regvar_loaded_int;
  185. rg.regvar_loaded_other := org_regvar_loaded_other;
  186. then_list := exprasmlist;
  187. exprasmlist := taasmoutput.create;}
  188. end;
  189. if assigned(t1) then
  190. begin
  191. if assigned(right) then
  192. begin
  193. objectlibrary.getlabel(hl);
  194. { do go back to if line !! }
  195. (*
  196. if not(cs_regvars in aktglobalswitches) then
  197. *)
  198. aktfilepos:=exprasmList.getlasttaifilepos^
  199. (*
  200. else
  201. aktfilepos:=then_list.getlasttaifilepos^
  202. *)
  203. ;
  204. cg.a_jmp_always(exprasmlist,hl);
  205. end;
  206. cg.a_label(exprasmlist,falselabel);
  207. secondpass(t1);
  208. (*
  209. { save current asmlist (previous instructions + else-block) }
  210. { and loaded regvar state and create a new clean list }
  211. if cs_regvars in aktglobalswitches then
  212. begin
  213. { else_regvar_loaded_int := rg.regvar_loaded_int;
  214. else_regvar_loaded_other := rg.regvar_loaded_other;}
  215. else_list := exprasmlist;
  216. exprasmlist := taasmoutput.create;
  217. end;
  218. *)
  219. if assigned(right) then
  220. cg.a_label(exprasmlist,hl);
  221. end
  222. else
  223. begin
  224. (*
  225. if cs_regvars in aktglobalswitches then
  226. begin
  227. { else_regvar_loaded_int := rg.regvar_loaded_int;
  228. else_regvar_loaded_other := rg.regvar_loaded_other;}
  229. else_list := exprasmlist;
  230. exprasmlist := taasmoutput.create;
  231. end;
  232. *)
  233. cg.a_label(exprasmlist,falselabel);
  234. end;
  235. if not(assigned(right)) then
  236. begin
  237. cg.a_label(exprasmlist,truelabel);
  238. end;
  239. (*
  240. if cs_regvars in aktglobalswitches then
  241. begin
  242. { add loads of regvars at the end of the then- and else-blocks }
  243. { so that at the end of both blocks the same regvars are loaded }
  244. { no else block? }
  245. if not assigned(t1) then
  246. begin
  247. sync_regvars_int(org_list,then_list,org_regvar_loaded_int,then_regvar_loaded_int);
  248. sync_regvars_other(org_list,then_list,org_regvar_loaded_other,then_regvar_loaded_other);
  249. end
  250. { no then block? }
  251. else if not assigned(right) then
  252. begin
  253. sync_regvars_int(org_list,else_list,org_regvar_loaded_int,else_regvar_loaded_int);
  254. sync_regvars_other(org_list,else_list,org_regvar_loaded_other,else_regvar_loaded_other);
  255. end
  256. { both else and then blocks }
  257. else
  258. begin
  259. sync_regvars_int(then_list,else_list,then_regvar_loaded_int,else_regvar_loaded_int);
  260. sync_regvars_other(then_list,else_list,then_regvar_loaded_other,else_regvar_loaded_other);
  261. end;
  262. { add all lists together }
  263. org_list.concatlist(then_list);
  264. then_list.free;
  265. org_list.concatlist(else_list);
  266. else_list.free;
  267. org_list.concatlist(exprasmlist);
  268. exprasmlist.free;
  269. exprasmlist := org_list;
  270. end;
  271. *)
  272. truelabel:=otlabel;
  273. falselabel:=oflabel;
  274. end;
  275. {*****************************************************************************
  276. SecondFor
  277. *****************************************************************************}
  278. procedure tcgfornode.pass_2;
  279. var
  280. l3,oldclabel,oldblabel : tasmlabel;
  281. temptovalue : boolean;
  282. hs : byte;
  283. temp1 : treference;
  284. hop : topcg;
  285. hcond : topcmp;
  286. opsize : tcgsize;
  287. count_var_is_signed,do_loopvar_at_end : boolean;
  288. cmp_const:Tconstexprint;
  289. oldflowcontrol : tflowcontrol;
  290. begin
  291. location_reset(location,LOC_VOID,OS_NO);
  292. oldflowcontrol:=flowcontrol;
  293. oldclabel:=aktcontinuelabel;
  294. oldblabel:=aktbreaklabel;
  295. objectlibrary.getlabel(aktcontinuelabel);
  296. objectlibrary.getlabel(aktbreaklabel);
  297. objectlibrary.getlabel(l3);
  298. { only calculate reference }
  299. secondpass(t2);
  300. hs := t2.resulttype.def.size;
  301. opsize := def_cgsize(t2.resulttype.def);
  302. { first set the to value
  303. because the count var can be in the expression !! }
  304. do_loopvar_at_end:=lnf_dont_mind_loopvar_on_exit in loopflags;
  305. secondpass(right);
  306. { calculate pointer value and check if changeable and if so }
  307. { load into temporary variable }
  308. if right.nodetype<>ordconstn then
  309. begin
  310. do_loopvar_at_end:=false;
  311. tg.GetTemp(exprasmlist,hs,tt_normal,temp1);
  312. temptovalue:=true;
  313. cg.a_load_loc_ref(exprasmlist,opsize,right.location,temp1);
  314. location_freetemp(exprasmlist,right.location);
  315. end
  316. else
  317. temptovalue:=false;
  318. { produce start assignment }
  319. secondpass(left);
  320. count_var_is_signed:=is_signed(t2.resulttype.def);
  321. if lnf_backward in loopflags then
  322. if count_var_is_signed then
  323. hcond:=OC_LT
  324. else
  325. hcond:=OC_B
  326. else
  327. if count_var_is_signed then
  328. hcond:=OC_GT
  329. else
  330. hcond:=OC_A;
  331. {$ifdef OLDREGVARS}
  332. load_all_regvars(exprasmlist);
  333. {$endif OLDREGVARS}
  334. if temptovalue then
  335. begin
  336. cg.a_cmp_ref_loc_label(exprasmlist,opsize,hcond,
  337. temp1,t2.location,aktbreaklabel);
  338. end
  339. else
  340. begin
  341. if lnf_testatbegin in loopflags then
  342. begin
  343. cg.a_cmp_const_loc_label(exprasmlist,opsize,hcond,
  344. tordconstnode(right).value,
  345. t2.location,aktbreaklabel);
  346. end;
  347. end;
  348. {If the loopvar doesn't mind on exit, we avoid this ugly
  349. dec instruction and do the loopvar inc/dec after the loop
  350. body.}
  351. if not do_loopvar_at_end then
  352. begin
  353. if lnf_backward in loopflags then
  354. hop:=OP_ADD
  355. else
  356. hop:=OP_SUB;
  357. cg.a_op_const_loc(exprasmlist,hop,1,t2.location);
  358. end;
  359. if not(cs_littlesize in aktglobalswitches) then
  360. { align loop target }
  361. exprasmList.concat(Tai_align.Create(aktalignment.loopalign));
  362. cg.a_label(exprasmlist,l3);
  363. {If the loopvar doesn't mind on exit, we avoid the loopvar inc/dec
  364. after the loop body instead of here.}
  365. if not do_loopvar_at_end then
  366. begin
  367. { according to count direction DEC or INC... }
  368. if lnf_backward in loopflags then
  369. hop:=OP_SUB
  370. else
  371. hop:=OP_ADD;
  372. cg.a_op_const_loc(exprasmlist,hop,1,t2.location);
  373. end;
  374. { help register must not be in instruction block }
  375. if assigned(t1) then
  376. begin
  377. secondpass(t1);
  378. {$ifdef OLDREGVARS}
  379. load_all_regvars(exprasmlist);
  380. {$endif OLDREGVARS}
  381. end;
  382. {If the loopvar doesn't mind on exit, we do the loopvar inc/dec
  383. after the loop body instead of here.}
  384. if do_loopvar_at_end then
  385. begin
  386. { according to count direction DEC or INC... }
  387. if lnf_backward in loopflags then
  388. hop:=OP_SUB
  389. else
  390. hop:=OP_ADD;
  391. cg.a_op_const_loc(exprasmlist,hop,1,t2.location);
  392. end;
  393. cg.a_label(exprasmlist,aktcontinuelabel);
  394. if do_loopvar_at_end then
  395. if lnf_backward in loopflags then
  396. if count_var_is_signed then
  397. hcond:=OC_GTE
  398. else
  399. hcond:=OC_AE
  400. else
  401. if count_var_is_signed then
  402. hcond:=OC_LTE
  403. else
  404. hcond:=OC_BE
  405. else
  406. if lnf_backward in loopflags then
  407. if count_var_is_signed then
  408. hcond:=OC_GT
  409. else
  410. hcond:=OC_A
  411. else
  412. if count_var_is_signed then
  413. hcond:=OC_LT
  414. else
  415. hcond:=OC_B;
  416. {$ifdef OLDREGVARS}
  417. load_all_regvars(exprasmlist);
  418. {$endif OLDREGVARS}
  419. { produce comparison and the corresponding }
  420. { jump }
  421. if temptovalue then
  422. begin
  423. cg.a_cmp_ref_loc_label(exprasmlist,opsize,hcond,temp1,
  424. t2.location,l3);
  425. tg.ungetiftemp(exprasmlist,temp1);
  426. end
  427. else
  428. begin
  429. cmp_const:=Tordconstnode(right).value;
  430. if do_loopvar_at_end then
  431. begin
  432. {Watch out for wrap around 255 -> 0.}
  433. {Ugly: This code is way to long... Use tables?}
  434. case opsize of
  435. OS_8:
  436. begin
  437. if lnf_backward in loopflags then
  438. begin
  439. if byte(cmp_const)=low(byte) then
  440. begin
  441. hcond:=OC_NE;
  442. cmp_const:=high(byte);
  443. end
  444. end
  445. else
  446. begin
  447. if byte(cmp_const)=high(byte) then
  448. begin
  449. hcond:=OC_NE;
  450. cmp_const:=low(byte);
  451. end
  452. end
  453. end;
  454. OS_16:
  455. begin
  456. if lnf_backward in loopflags then
  457. begin
  458. if word(cmp_const)=high(word) then
  459. begin
  460. hcond:=OC_NE;
  461. cmp_const:=low(word);
  462. end
  463. end
  464. else
  465. begin
  466. if word(cmp_const)=low(word) then
  467. begin
  468. hcond:=OC_NE;
  469. cmp_const:=high(word);
  470. end
  471. end
  472. end;
  473. OS_32:
  474. begin
  475. if lnf_backward in loopflags then
  476. begin
  477. if cardinal(cmp_const)=high(cardinal) then
  478. begin
  479. hcond:=OC_NE;
  480. cmp_const:=low(cardinal);
  481. end
  482. end
  483. else
  484. begin
  485. if cardinal(cmp_const)=low(cardinal) then
  486. begin
  487. hcond:=OC_NE;
  488. cmp_const:=high(cardinal);
  489. end
  490. end
  491. end;
  492. OS_64:
  493. begin
  494. if lnf_backward in loopflags then
  495. begin
  496. if qword(cmp_const)=high(qword) then
  497. begin
  498. hcond:=OC_NE;
  499. cmp_const:=low(qword);
  500. end
  501. end
  502. else
  503. begin
  504. if qword(cmp_const)=low(qword) then
  505. begin
  506. hcond:=OC_NE;
  507. cmp_const:=high(qword);
  508. end
  509. end
  510. end;
  511. OS_S8:
  512. begin
  513. if lnf_backward in loopflags then
  514. begin
  515. if shortint(cmp_const)=low(shortint) then
  516. begin
  517. hcond:=OC_NE;
  518. cmp_const:=high(shortint);
  519. end
  520. end
  521. else
  522. begin
  523. if shortint(cmp_const)=high(shortint) then
  524. begin
  525. hcond:=OC_NE;
  526. cmp_const:=low(shortint);
  527. end
  528. end
  529. end;
  530. OS_S16:
  531. begin
  532. if lnf_backward in loopflags then
  533. begin
  534. if integer(cmp_const)=high(integer) then
  535. begin
  536. hcond:=OC_NE;
  537. cmp_const:=low(integer);
  538. end
  539. end
  540. else
  541. begin
  542. if integer(cmp_const)=low(integer) then
  543. begin
  544. hcond:=OC_NE;
  545. cmp_const:=high(integer);
  546. end
  547. end
  548. end;
  549. OS_S32:
  550. begin
  551. if lnf_backward in loopflags then
  552. begin
  553. if longint(cmp_const)=high(longint) then
  554. begin
  555. hcond:=OC_NE;
  556. cmp_const:=low(longint);
  557. end
  558. end
  559. else
  560. begin
  561. if longint(cmp_const)=low(longint) then
  562. begin
  563. hcond:=OC_NE;
  564. cmp_const:=high(longint);
  565. end
  566. end
  567. end;
  568. OS_S64:
  569. begin
  570. if lnf_backward in loopflags then
  571. begin
  572. if int64(cmp_const)=high(int64) then
  573. begin
  574. hcond:=OC_NE;
  575. cmp_const:=low(int64);
  576. end
  577. end
  578. else
  579. begin
  580. if int64(cmp_const)=low(int64) then
  581. begin
  582. hcond:=OC_NE;
  583. cmp_const:=high(int64);
  584. end
  585. end
  586. end;
  587. else
  588. internalerror(200201021);
  589. end;
  590. end;
  591. cg.a_cmp_const_loc_label(exprasmlist,opsize,hcond,
  592. cmp_const,t2.location,l3);
  593. end;
  594. { this is the break label: }
  595. cg.a_label(exprasmlist,aktbreaklabel);
  596. aktcontinuelabel:=oldclabel;
  597. aktbreaklabel:=oldblabel;
  598. { a break/continue in a while/repeat block can't be seen outside }
  599. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue]);
  600. end;
  601. {*****************************************************************************
  602. SecondExitN
  603. *****************************************************************************}
  604. procedure tcgexitnode.pass_2;
  605. begin
  606. location_reset(location,LOC_VOID,OS_NO);
  607. include(flowcontrol,fc_exit);
  608. if assigned(left) then
  609. secondpass(left);
  610. cg.a_jmp_always(exprasmlist,current_procinfo.aktexitlabel);
  611. end;
  612. {*****************************************************************************
  613. SecondBreakN
  614. *****************************************************************************}
  615. procedure tcgbreaknode.pass_2;
  616. begin
  617. location_reset(location,LOC_VOID,OS_NO);
  618. include(flowcontrol,fc_break);
  619. if aktbreaklabel<>nil then
  620. begin
  621. {$ifdef OLDREGVARS}
  622. load_all_regvars(exprasmlist);
  623. {$endif OLDREGVARS}
  624. cg.a_jmp_always(exprasmlist,aktbreaklabel)
  625. end
  626. else
  627. CGMessage(cg_e_break_not_allowed);
  628. end;
  629. {*****************************************************************************
  630. SecondContinueN
  631. *****************************************************************************}
  632. procedure tcgcontinuenode.pass_2;
  633. begin
  634. location_reset(location,LOC_VOID,OS_NO);
  635. include(flowcontrol,fc_continue);
  636. if aktcontinuelabel<>nil then
  637. begin
  638. {$ifdef OLDREGVARS}
  639. load_all_regvars(exprasmlist);
  640. {$endif OLDREGVARS}
  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. {$ifdef OLDREGVARS}
  653. load_all_regvars(exprasmlist);
  654. {$endif OLDREGVARS}
  655. cg.a_jmp_always(exprasmlist,labsym.lab)
  656. end;
  657. {*****************************************************************************
  658. SecondLabel
  659. *****************************************************************************}
  660. procedure tcglabelnode.pass_2;
  661. begin
  662. location_reset(location,LOC_VOID,OS_NO);
  663. {$ifdef OLDREGVARS}
  664. load_all_regvars(exprasmlist);
  665. {$endif OLDREGVARS}
  666. cg.a_label(exprasmlist,labelnr);
  667. secondpass(left);
  668. end;
  669. {*****************************************************************************
  670. SecondRaise
  671. *****************************************************************************}
  672. procedure tcgraisenode.pass_2;
  673. var
  674. a : tasmlabel;
  675. href2: treference;
  676. paraloc1,paraloc2,paraloc3 : tcgpara;
  677. begin
  678. paraloc1.init;
  679. paraloc2.init;
  680. paraloc3.init;
  681. paramanager.getintparaloc(pocall_default,1,paraloc1);
  682. paramanager.getintparaloc(pocall_default,2,paraloc2);
  683. paramanager.getintparaloc(pocall_default,3,paraloc3);
  684. location_reset(location,LOC_VOID,OS_NO);
  685. if assigned(left) then
  686. begin
  687. { multiple parameters? }
  688. if assigned(right) then
  689. begin
  690. if assigned(frametree) then
  691. secondpass(frametree);
  692. secondpass(right);
  693. end;
  694. secondpass(left);
  695. if codegenerror then
  696. exit;
  697. { Push parameters }
  698. if assigned(right) then
  699. begin
  700. paramanager.allocparaloc(exprasmlist,paraloc3);
  701. if assigned(frametree) then
  702. cg.a_param_loc(exprasmlist,frametree.location,paraloc3)
  703. else
  704. cg.a_param_const(exprasmlist,OS_INT,0,paraloc3);
  705. { push address }
  706. paramanager.allocparaloc(exprasmlist,paraloc2);
  707. cg.a_param_loc(exprasmlist,right.location,paraloc2);
  708. end
  709. else
  710. begin
  711. { get current address }
  712. objectlibrary.getaddrlabel(a);
  713. cg.a_label(exprasmlist,a);
  714. reference_reset_symbol(href2,a,0);
  715. { push current frame }
  716. paramanager.allocparaloc(exprasmlist,paraloc3);
  717. cg.a_param_reg(exprasmlist,OS_ADDR,NR_FRAME_POINTER_REG,paraloc3);
  718. { push current address }
  719. paramanager.allocparaloc(exprasmlist,paraloc2);
  720. if target_info.system <> system_powerpc_macos then
  721. cg.a_paramaddr_ref(exprasmlist,href2,paraloc2)
  722. else
  723. cg.a_param_const(exprasmlist,OS_INT,0,paraloc2);
  724. end;
  725. paramanager.allocparaloc(exprasmlist,paraloc1);
  726. cg.a_param_loc(exprasmlist,left.location,paraloc1);
  727. paramanager.freeparaloc(exprasmlist,paraloc1);
  728. paramanager.freeparaloc(exprasmlist,paraloc2);
  729. paramanager.freeparaloc(exprasmlist,paraloc3);
  730. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  731. cg.a_call_name(exprasmlist,'FPC_RAISEEXCEPTION');
  732. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  733. end
  734. else
  735. begin
  736. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  737. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  738. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  739. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  740. end;
  741. paraloc1.done;
  742. paraloc2.done;
  743. paraloc3.done;
  744. end;
  745. {*****************************************************************************
  746. SecondTryExcept
  747. *****************************************************************************}
  748. var
  749. endexceptlabel : tasmlabel;
  750. { does the necessary things to clean up the object stack }
  751. { in the except block }
  752. procedure cleanupobjectstack;
  753. var
  754. paraloc1 : tcgpara;
  755. begin
  756. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  757. cg.a_call_name(exprasmlist,'FPC_POPOBJECTSTACK');
  758. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  759. paraloc1.init;
  760. paramanager.getintparaloc(pocall_default,1,paraloc1);
  761. paramanager.allocparaloc(exprasmlist,paraloc1);
  762. cg.a_param_reg(exprasmlist,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  763. paramanager.freeparaloc(exprasmlist,paraloc1);
  764. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  765. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  766. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  767. paraloc1.done;
  768. end;
  769. procedure tcgtryexceptnode.pass_2;
  770. var
  771. exceptlabel,doexceptlabel,oldendexceptlabel,
  772. lastonlabel,
  773. exitexceptlabel,
  774. continueexceptlabel,
  775. breakexceptlabel,
  776. exittrylabel,
  777. continuetrylabel,
  778. breaktrylabel,
  779. doobjectdestroy,
  780. doobjectdestroyandreraise,
  781. oldaktexitlabel,
  782. oldaktcontinuelabel,
  783. oldaktbreaklabel : tasmlabel;
  784. oldflowcontrol,tryflowcontrol,
  785. exceptflowcontrol : tflowcontrol;
  786. destroytemps,
  787. excepttemps : texceptiontemps;
  788. paraloc1 : tcgpara;
  789. label
  790. errorexit;
  791. begin
  792. location_reset(location,LOC_VOID,OS_NO);
  793. oldflowcontrol:=flowcontrol;
  794. flowcontrol:=[];
  795. { this can be called recursivly }
  796. oldaktbreaklabel:=nil;
  797. oldaktcontinuelabel:=nil;
  798. oldendexceptlabel:=endexceptlabel;
  799. { save the old labels for control flow statements }
  800. oldaktexitlabel:=current_procinfo.aktexitlabel;
  801. if assigned(aktbreaklabel) then
  802. begin
  803. oldaktcontinuelabel:=aktcontinuelabel;
  804. oldaktbreaklabel:=aktbreaklabel;
  805. end;
  806. { get new labels for the control flow statements }
  807. objectlibrary.getlabel(exittrylabel);
  808. objectlibrary.getlabel(exitexceptlabel);
  809. if assigned(aktbreaklabel) then
  810. begin
  811. objectlibrary.getlabel(breaktrylabel);
  812. objectlibrary.getlabel(continuetrylabel);
  813. objectlibrary.getlabel(breakexceptlabel);
  814. objectlibrary.getlabel(continueexceptlabel);
  815. end;
  816. objectlibrary.getlabel(exceptlabel);
  817. objectlibrary.getlabel(doexceptlabel);
  818. objectlibrary.getlabel(endexceptlabel);
  819. objectlibrary.getlabel(lastonlabel);
  820. get_exception_temps(exprasmlist,excepttemps);
  821. new_exception(exprasmlist,excepttemps,exceptlabel);
  822. { try block }
  823. { set control flow labels for the try block }
  824. current_procinfo.aktexitlabel:=exittrylabel;
  825. if assigned(oldaktbreaklabel) then
  826. begin
  827. aktcontinuelabel:=continuetrylabel;
  828. aktbreaklabel:=breaktrylabel;
  829. end;
  830. flowcontrol:=[];
  831. secondpass(left);
  832. tryflowcontrol:=flowcontrol;
  833. if codegenerror then
  834. goto errorexit;
  835. cg.a_label(exprasmlist,exceptlabel);
  836. free_exception(exprasmlist, excepttemps, 0, endexceptlabel, false);
  837. cg.a_label(exprasmlist,doexceptlabel);
  838. { set control flow labels for the except block }
  839. { and the on statements }
  840. current_procinfo.aktexitlabel:=exitexceptlabel;
  841. if assigned(oldaktbreaklabel) then
  842. begin
  843. aktcontinuelabel:=continueexceptlabel;
  844. aktbreaklabel:=breakexceptlabel;
  845. end;
  846. flowcontrol:=[];
  847. { on statements }
  848. if assigned(right) then
  849. secondpass(right);
  850. cg.a_label(exprasmlist,lastonlabel);
  851. { default handling except handling }
  852. if assigned(t1) then
  853. begin
  854. { FPC_CATCHES must be called with
  855. 'default handler' flag (=-1)
  856. }
  857. paraloc1.init;
  858. paramanager.getintparaloc(pocall_default,1,paraloc1);
  859. paramanager.allocparaloc(exprasmlist,paraloc1);
  860. cg.a_param_const(exprasmlist,OS_ADDR,-1,paraloc1);
  861. paramanager.freeparaloc(exprasmlist,paraloc1);
  862. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  863. cg.a_call_name(exprasmlist,'FPC_CATCHES');
  864. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  865. paraloc1.done;
  866. { the destruction of the exception object must be also }
  867. { guarded by an exception frame }
  868. objectlibrary.getlabel(doobjectdestroy);
  869. objectlibrary.getlabel(doobjectdestroyandreraise);
  870. get_exception_temps(exprasmlist,destroytemps);
  871. new_exception(exprasmlist,destroytemps,doobjectdestroyandreraise);
  872. { here we don't have to reset flowcontrol }
  873. { the default and on flowcontrols are handled equal }
  874. secondpass(t1);
  875. exceptflowcontrol:=flowcontrol;
  876. cg.a_label(exprasmlist,doobjectdestroyandreraise);
  877. free_exception(exprasmlist,destroytemps,0,doobjectdestroy,false);
  878. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  879. cg.a_call_name(exprasmlist,'FPC_POPSECONDOBJECTSTACK');
  880. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  881. paraloc1.init;
  882. paramanager.getintparaloc(pocall_default,1,paraloc1);
  883. paramanager.allocparaloc(exprasmlist,paraloc1);
  884. cg.a_param_reg(exprasmlist, OS_ADDR, NR_FUNCTION_RESULT_REG, paraloc1);
  885. paramanager.freeparaloc(exprasmlist,paraloc1);
  886. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  887. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  888. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  889. paraloc1.done;
  890. { we don't need to restore esi here because reraise never }
  891. { returns }
  892. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  893. cg.a_label(exprasmlist,doobjectdestroy);
  894. cleanupobjectstack;
  895. unget_exception_temps(exprasmlist,destroytemps);
  896. cg.a_jmp_always(exprasmlist,endexceptlabel);
  897. end
  898. else
  899. begin
  900. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  901. exceptflowcontrol:=flowcontrol;
  902. end;
  903. if fc_exit in exceptflowcontrol then
  904. begin
  905. { do some magic for exit in the try block }
  906. cg.a_label(exprasmlist,exitexceptlabel);
  907. { we must also destroy the address frame which guards }
  908. { exception object }
  909. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  910. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  911. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  912. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  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.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  922. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  923. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  924. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  925. cleanupobjectstack;
  926. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  927. end;
  928. if fc_continue in exceptflowcontrol then
  929. begin
  930. cg.a_label(exprasmlist,continueexceptlabel);
  931. { we must also destroy the address frame which guards }
  932. { exception object }
  933. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  934. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  935. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  936. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  937. cleanupobjectstack;
  938. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  939. end;
  940. if fc_exit in tryflowcontrol then
  941. begin
  942. { do some magic for exit in the try block }
  943. cg.a_label(exprasmlist,exittrylabel);
  944. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  945. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  946. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  947. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  948. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  949. end;
  950. if fc_break in tryflowcontrol then
  951. begin
  952. cg.a_label(exprasmlist,breaktrylabel);
  953. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  954. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  955. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  956. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  957. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  958. end;
  959. if fc_continue in tryflowcontrol then
  960. begin
  961. cg.a_label(exprasmlist,continuetrylabel);
  962. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  963. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  964. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  965. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  966. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  967. end;
  968. unget_exception_temps(exprasmlist,excepttemps);
  969. cg.a_label(exprasmlist,endexceptlabel);
  970. errorexit:
  971. { restore all saved labels }
  972. endexceptlabel:=oldendexceptlabel;
  973. { restore the control flow labels }
  974. current_procinfo.aktexitlabel:=oldaktexitlabel;
  975. if assigned(oldaktbreaklabel) then
  976. begin
  977. aktcontinuelabel:=oldaktcontinuelabel;
  978. aktbreaklabel:=oldaktbreaklabel;
  979. end;
  980. { return all used control flow statements }
  981. flowcontrol:=oldflowcontrol+exceptflowcontrol+
  982. tryflowcontrol;
  983. end;
  984. procedure tcgonnode.pass_2;
  985. var
  986. nextonlabel,
  987. exitonlabel,
  988. continueonlabel,
  989. breakonlabel,
  990. oldaktexitlabel,
  991. oldaktcontinuelabel,
  992. doobjectdestroyandreraise,
  993. doobjectdestroy,
  994. oldaktbreaklabel : tasmlabel;
  995. oldflowcontrol : tflowcontrol;
  996. excepttemps : texceptiontemps;
  997. exceptref,
  998. href2: treference;
  999. paraloc1 : tcgpara;
  1000. begin
  1001. paraloc1.init;
  1002. location_reset(location,LOC_VOID,OS_NO);
  1003. oldflowcontrol:=flowcontrol;
  1004. flowcontrol:=[];
  1005. objectlibrary.getlabel(nextonlabel);
  1006. { send the vmt parameter }
  1007. reference_reset_symbol(href2,objectlibrary.newasmsymbol(excepttype.vmt_mangledname,AB_EXTERNAL,AT_DATA),0);
  1008. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1009. paramanager.allocparaloc(exprasmlist,paraloc1);
  1010. cg.a_paramaddr_ref(exprasmlist,href2,paraloc1);
  1011. paramanager.freeparaloc(exprasmlist,paraloc1);
  1012. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1013. cg.a_call_name(exprasmlist,'FPC_CATCHES');
  1014. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1015. { is it this catch? No. go to next onlabel }
  1016. cg.a_cmp_const_reg_label(exprasmlist,OS_ADDR,OC_EQ,0,NR_FUNCTION_RESULT_REG,nextonlabel);
  1017. { what a hack ! }
  1018. if assigned(exceptsymtable) then
  1019. begin
  1020. tlocalvarsym(exceptsymtable.symindex.first).localloc.loc:=LOC_REFERENCE;
  1021. tlocalvarsym(exceptsymtable.symindex.first).localloc.size:=OS_ADDR;
  1022. tg.GetLocal(exprasmlist,sizeof(aint),voidpointertype.def,
  1023. tlocalvarsym(exceptsymtable.symindex.first).localloc.reference);
  1024. cg.a_load_reg_ref(exprasmlist,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,tlocalvarsym(exceptsymtable.symindex.first).localloc.reference);
  1025. end
  1026. else
  1027. begin
  1028. tg.GetTemp(exprasmlist,sizeof(aint),tt_normal,exceptref);
  1029. cg.a_load_reg_ref(exprasmlist,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,exceptref);
  1030. end;
  1031. { in the case that another exception is risen
  1032. we've to destroy the old one }
  1033. objectlibrary.getlabel(doobjectdestroyandreraise);
  1034. { call setjmp, and jump to finally label on non-zero result }
  1035. get_exception_temps(exprasmlist,excepttemps);
  1036. new_exception(exprasmlist,excepttemps,doobjectdestroyandreraise);
  1037. oldaktbreaklabel:=nil;
  1038. oldaktcontinuelabel:=nil;
  1039. if assigned(right) then
  1040. begin
  1041. oldaktexitlabel:=current_procinfo.aktexitlabel;
  1042. objectlibrary.getlabel(exitonlabel);
  1043. current_procinfo.aktexitlabel:=exitonlabel;
  1044. if assigned(aktbreaklabel) then
  1045. begin
  1046. oldaktcontinuelabel:=aktcontinuelabel;
  1047. oldaktbreaklabel:=aktbreaklabel;
  1048. objectlibrary.getlabel(breakonlabel);
  1049. objectlibrary.getlabel(continueonlabel);
  1050. aktcontinuelabel:=continueonlabel;
  1051. aktbreaklabel:=breakonlabel;
  1052. end;
  1053. secondpass(right);
  1054. end;
  1055. objectlibrary.getlabel(doobjectdestroy);
  1056. cg.a_label(exprasmlist,doobjectdestroyandreraise);
  1057. free_exception(exprasmlist,excepttemps,0,doobjectdestroy,false);
  1058. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1059. cg.a_call_name(exprasmlist,'FPC_POPSECONDOBJECTSTACK');
  1060. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1061. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1062. paramanager.allocparaloc(exprasmlist,paraloc1);
  1063. cg.a_param_reg(exprasmlist, OS_ADDR, NR_FUNCTION_RESULT_REG, paraloc1);
  1064. paramanager.freeparaloc(exprasmlist,paraloc1);
  1065. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1066. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  1067. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1068. { we don't need to restore esi here because reraise never }
  1069. { returns }
  1070. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1071. cg.a_label(exprasmlist,doobjectdestroy);
  1072. cleanupobjectstack;
  1073. { clear some stuff }
  1074. if assigned(exceptsymtable) then
  1075. begin
  1076. tg.UngetLocal(exprasmlist,tlocalvarsym(exceptsymtable.symindex.first).localloc.reference);
  1077. tlocalvarsym(exceptsymtable.symindex.first).localloc.loc:=LOC_INVALID;
  1078. end
  1079. else
  1080. tg.Ungettemp(exprasmlist,exceptref);
  1081. cg.a_jmp_always(exprasmlist,endexceptlabel);
  1082. if assigned(right) then
  1083. begin
  1084. { special handling for control flow instructions }
  1085. if fc_exit in flowcontrol then
  1086. begin
  1087. { the address and object pop does secondtryexcept }
  1088. cg.a_label(exprasmlist,exitonlabel);
  1089. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  1090. end;
  1091. if fc_break in flowcontrol then
  1092. begin
  1093. { the address and object pop does secondtryexcept }
  1094. cg.a_label(exprasmlist,breakonlabel);
  1095. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  1096. end;
  1097. if fc_continue in flowcontrol then
  1098. begin
  1099. { the address and object pop does secondtryexcept }
  1100. cg.a_label(exprasmlist,continueonlabel);
  1101. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  1102. end;
  1103. current_procinfo.aktexitlabel:=oldaktexitlabel;
  1104. if assigned(oldaktbreaklabel) then
  1105. begin
  1106. aktcontinuelabel:=oldaktcontinuelabel;
  1107. aktbreaklabel:=oldaktbreaklabel;
  1108. end;
  1109. end;
  1110. unget_exception_temps(exprasmlist,excepttemps);
  1111. cg.a_label(exprasmlist,nextonlabel);
  1112. flowcontrol:=oldflowcontrol+flowcontrol;
  1113. paraloc1.done;
  1114. { next on node }
  1115. if assigned(left) then
  1116. secondpass(left);
  1117. end;
  1118. {*****************************************************************************
  1119. SecondTryFinally
  1120. *****************************************************************************}
  1121. procedure tcgtryfinallynode.pass_2;
  1122. var
  1123. reraiselabel,
  1124. finallylabel,
  1125. endfinallylabel,
  1126. exitfinallylabel,
  1127. continuefinallylabel,
  1128. breakfinallylabel,
  1129. oldaktexitlabel,
  1130. oldaktcontinuelabel,
  1131. oldaktbreaklabel : tasmlabel;
  1132. oldflowcontrol,tryflowcontrol : tflowcontrol;
  1133. decconst : longint;
  1134. excepttemps : texceptiontemps;
  1135. begin
  1136. location_reset(location,LOC_VOID,OS_NO);
  1137. { check if child nodes do a break/continue/exit }
  1138. oldflowcontrol:=flowcontrol;
  1139. flowcontrol:=[];
  1140. objectlibrary.getlabel(finallylabel);
  1141. objectlibrary.getlabel(endfinallylabel);
  1142. objectlibrary.getlabel(reraiselabel);
  1143. { the finally block must catch break, continue and exit }
  1144. { statements }
  1145. oldaktexitlabel:=current_procinfo.aktexitlabel;
  1146. if implicitframe then
  1147. exitfinallylabel:=finallylabel
  1148. else
  1149. objectlibrary.getlabel(exitfinallylabel);
  1150. current_procinfo.aktexitlabel:=exitfinallylabel;
  1151. if assigned(aktbreaklabel) then
  1152. begin
  1153. oldaktcontinuelabel:=aktcontinuelabel;
  1154. oldaktbreaklabel:=aktbreaklabel;
  1155. if implicitframe then
  1156. begin
  1157. breakfinallylabel:=finallylabel;
  1158. continuefinallylabel:=finallylabel;
  1159. end
  1160. else
  1161. begin
  1162. objectlibrary.getlabel(breakfinallylabel);
  1163. objectlibrary.getlabel(continuefinallylabel);
  1164. end;
  1165. aktcontinuelabel:=continuefinallylabel;
  1166. aktbreaklabel:=breakfinallylabel;
  1167. end;
  1168. { call setjmp, and jump to finally label on non-zero result }
  1169. get_exception_temps(exprasmlist,excepttemps);
  1170. new_exception(exprasmlist,excepttemps,finallylabel);
  1171. { try code }
  1172. if assigned(left) then
  1173. begin
  1174. secondpass(left);
  1175. tryflowcontrol:=flowcontrol;
  1176. if codegenerror then
  1177. exit;
  1178. end;
  1179. cg.a_label(exprasmlist,finallylabel);
  1180. { just free the frame information }
  1181. free_exception(exprasmlist,excepttemps,1,finallylabel,true);
  1182. { finally code }
  1183. flowcontrol:=[];
  1184. secondpass(right);
  1185. if flowcontrol<>[] then
  1186. CGMessage(cg_e_control_flow_outside_finally);
  1187. if codegenerror then
  1188. exit;
  1189. { the value should now be in the exception handler }
  1190. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  1191. if implicitframe then
  1192. begin
  1193. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,endfinallylabel);
  1194. { finally code only needed to be executed on exception }
  1195. flowcontrol:=[];
  1196. secondpass(t1);
  1197. if flowcontrol<>[] then
  1198. CGMessage(cg_e_control_flow_outside_finally);
  1199. if codegenerror then
  1200. exit;
  1201. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1202. end
  1203. else
  1204. begin
  1205. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,endfinallylabel);
  1206. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_INT,1,NR_FUNCTION_RESULT_REG);
  1207. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,reraiselabel);
  1208. if fc_exit in tryflowcontrol then
  1209. begin
  1210. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_INT,1,NR_FUNCTION_RESULT_REG);
  1211. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldaktexitlabel);
  1212. decconst:=1;
  1213. end
  1214. else
  1215. decconst:=2;
  1216. if fc_break in tryflowcontrol then
  1217. begin
  1218. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_INT,decconst,NR_FUNCTION_RESULT_REG);
  1219. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldaktbreaklabel);
  1220. decconst:=1;
  1221. end
  1222. else
  1223. inc(decconst);
  1224. if fc_continue in tryflowcontrol then
  1225. begin
  1226. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_INT,decconst,NR_FUNCTION_RESULT_REG);
  1227. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldaktcontinuelabel);
  1228. end;
  1229. cg.a_label(exprasmlist,reraiselabel);
  1230. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1231. { do some magic for exit,break,continue in the try block }
  1232. if fc_exit in tryflowcontrol then
  1233. begin
  1234. cg.a_label(exprasmlist,exitfinallylabel);
  1235. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  1236. cg.g_exception_reason_save_const(exprasmlist,excepttemps.reasonbuf,2);
  1237. cg.a_jmp_always(exprasmlist,finallylabel);
  1238. end;
  1239. if fc_break in tryflowcontrol then
  1240. begin
  1241. cg.a_label(exprasmlist,breakfinallylabel);
  1242. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  1243. cg.g_exception_reason_save_const(exprasmlist,excepttemps.reasonbuf,3);
  1244. cg.a_jmp_always(exprasmlist,finallylabel);
  1245. end;
  1246. if fc_continue in tryflowcontrol then
  1247. begin
  1248. cg.a_label(exprasmlist,continuefinallylabel);
  1249. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  1250. cg.g_exception_reason_save_const(exprasmlist,excepttemps.reasonbuf,4);
  1251. cg.a_jmp_always(exprasmlist,finallylabel);
  1252. end;
  1253. end;
  1254. unget_exception_temps(exprasmlist,excepttemps);
  1255. cg.a_label(exprasmlist,endfinallylabel);
  1256. current_procinfo.aktexitlabel:=oldaktexitlabel;
  1257. if assigned(aktbreaklabel) then
  1258. begin
  1259. aktcontinuelabel:=oldaktcontinuelabel;
  1260. aktbreaklabel:=oldaktbreaklabel;
  1261. end;
  1262. flowcontrol:=oldflowcontrol+tryflowcontrol;
  1263. end;
  1264. begin
  1265. cwhilerepeatnode:=tcgwhilerepeatnode;
  1266. cifnode:=tcgifnode;
  1267. cfornode:=tcgfornode;
  1268. cexitnode:=tcgexitnode;
  1269. cbreaknode:=tcgbreaknode;
  1270. ccontinuenode:=tcgcontinuenode;
  1271. cgotonode:=tcggotonode;
  1272. clabelnode:=tcglabelnode;
  1273. craisenode:=tcgraisenode;
  1274. ctryexceptnode:=tcgtryexceptnode;
  1275. ctryfinallynode:=tcgtryfinallynode;
  1276. connode:=tcgonnode;
  1277. end.
  1278. {
  1279. $Log$
  1280. Revision 1.102 2004-11-08 22:09:59 peter
  1281. * tvarsym splitted
  1282. Revision 1.101 2004/10/24 11:44:28 peter
  1283. * small regvar fixes
  1284. * loadref parameter removed from concatcopy,incrrefcount,etc
  1285. Revision 1.100 2004/09/26 17:45:30 peter
  1286. * simple regvar support, not yet finished
  1287. Revision 1.99 2004/09/25 14:23:54 peter
  1288. * ungetregister is now only used for cpuregisters, renamed to
  1289. ungetcpuregister
  1290. * renamed (get|unget)explicitregister(s) to ..cpuregister
  1291. * removed location-release/reference_release
  1292. Revision 1.98 2004/09/21 17:25:12 peter
  1293. * paraloc branch merged
  1294. Revision 1.97.4.2 2004/09/12 18:30:48 peter
  1295. * uninitialized vars fixed
  1296. Revision 1.97.4.1 2004/08/31 20:43:06 peter
  1297. * paraloc patch
  1298. Revision 1.97 2004/06/20 08:55:29 florian
  1299. * logs truncated
  1300. Revision 1.96 2004/06/16 20:07:08 florian
  1301. * dwarf branch merged
  1302. Revision 1.95.2.3 2004/05/01 16:02:09 peter
  1303. * POINTER_SIZE replaced with sizeof(aint)
  1304. * aint,aword,tconst*int moved to globtype
  1305. Revision 1.95.2.2 2004/04/27 18:18:25 peter
  1306. * aword -> aint
  1307. Revision 1.95.2.1 2004/04/24 20:13:24 florian
  1308. * fixed x86-64 exception handling
  1309. Revision 1.95 2004/03/29 14:43:47 peter
  1310. * cleaner temp get/unget for exceptions
  1311. }