ncgflw.pas 54 KB

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