ncgflw.pas 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate assembler for nodes that influence the flow which are
  4. the same for all (most?) processors
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncgflw;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. aasmbase,node,nflw;
  23. type
  24. tcgwhilerepeatnode = class(twhilerepeatnode)
  25. procedure pass_2;override;
  26. end;
  27. tcgifnode = class(tifnode)
  28. procedure pass_2;override;
  29. end;
  30. tcgfornode = class(tfornode)
  31. procedure pass_2;override;
  32. end;
  33. tcgexitnode = class(texitnode)
  34. procedure pass_2;override;
  35. end;
  36. tcgbreaknode = class(tbreaknode)
  37. procedure pass_2;override;
  38. end;
  39. tcgcontinuenode = class(tcontinuenode)
  40. procedure pass_2;override;
  41. end;
  42. tcggotonode = class(tgotonode)
  43. procedure pass_2;override;
  44. end;
  45. tcglabelnode = class(tlabelnode)
  46. private
  47. asmlabel : tasmlabel;
  48. public
  49. function getasmlabel : tasmlabel;
  50. procedure pass_2;override;
  51. end;
  52. tcgraisenode = class(traisenode)
  53. procedure pass_2;override;
  54. end;
  55. tcgtryexceptnode = class(ttryexceptnode)
  56. procedure pass_2;override;
  57. end;
  58. tcgtryfinallynode = class(ttryfinallynode)
  59. procedure pass_2;override;
  60. end;
  61. tcgonnode = class(tonnode)
  62. procedure pass_2;override;
  63. end;
  64. implementation
  65. uses
  66. verbose,globals,systems,globtype,
  67. symconst,symdef,symsym,aasmtai,aasmcpu,defutil,
  68. procinfo,cgbase,pass_2,parabase,
  69. cpubase,cpuinfo,
  70. nld,ncon,
  71. ncgutil,
  72. tgobj,paramgr,
  73. regvars,
  74. cgutils,cgobj
  75. ;
  76. {*****************************************************************************
  77. Second_While_RepeatN
  78. *****************************************************************************}
  79. procedure tcgwhilerepeatnode.pass_2;
  80. var
  81. lcont,lbreak,lloop,
  82. oldclabel,oldblabel : tasmlabel;
  83. otlabel,oflabel : tasmlabel;
  84. oldflowcontrol : tflowcontrol;
  85. begin
  86. location_reset(location,LOC_VOID,OS_NO);
  87. objectlibrary.getlabel(lloop);
  88. objectlibrary.getlabel(lcont);
  89. objectlibrary.getlabel(lbreak);
  90. { arrange continue and breaklabels: }
  91. oldflowcontrol:=flowcontrol;
  92. oldclabel:=aktcontinuelabel;
  93. oldblabel:=aktbreaklabel;
  94. {$ifdef OLDREGVARS}
  95. load_all_regvars(exprasmlist);
  96. {$endif OLDREGVARS}
  97. { handling code at the end as it is much more efficient, and makes
  98. while equal to repeat loop, only the end true/false is swapped (PFV) }
  99. if lnf_testatbegin in loopflags then
  100. cg.a_jmp_always(exprasmlist,lcont);
  101. if not(cs_littlesize in aktglobalswitches) then
  102. { align loop target }
  103. exprasmList.concat(Tai_align.Create(aktalignment.loopalign));
  104. cg.a_label(exprasmlist,lloop);
  105. aktcontinuelabel:=lcont;
  106. aktbreaklabel:=lbreak;
  107. if assigned(right) then
  108. secondpass(right);
  109. {$ifdef OLDREGVARS}
  110. load_all_regvars(exprasmlist);
  111. {$endif OLDREGVARS}
  112. cg.a_label(exprasmlist,lcont);
  113. otlabel:=truelabel;
  114. oflabel:=falselabel;
  115. if lnf_checknegate in loopflags then
  116. begin
  117. truelabel:=lbreak;
  118. falselabel:=lloop;
  119. end
  120. else
  121. begin
  122. truelabel:=lloop;
  123. falselabel:=lbreak;
  124. end;
  125. secondpass(left);
  126. maketojumpbool(exprasmlist,left,lr_load_regvars);
  127. cg.a_label(exprasmlist,lbreak);
  128. truelabel:=otlabel;
  129. falselabel:=oflabel;
  130. aktcontinuelabel:=oldclabel;
  131. aktbreaklabel:=oldblabel;
  132. { a break/continue in a while/repeat block can't be seen outside }
  133. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue]);
  134. end;
  135. {*****************************************************************************
  136. tcgIFNODE
  137. *****************************************************************************}
  138. procedure tcgifnode.pass_2;
  139. var
  140. hl,otlabel,oflabel : tasmlabel;
  141. (*
  142. org_regvar_loaded_other,
  143. then_regvar_loaded_other,
  144. else_regvar_loaded_other : regvarother_booleanarray;
  145. org_regvar_loaded_int,
  146. then_regvar_loaded_int,
  147. else_regvar_loaded_int : Tsuperregisterset;
  148. org_list,
  149. then_list,
  150. else_list : taasmoutput;
  151. *)
  152. begin
  153. location_reset(location,LOC_VOID,OS_NO);
  154. otlabel:=truelabel;
  155. oflabel:=falselabel;
  156. objectlibrary.getlabel(truelabel);
  157. objectlibrary.getlabel(falselabel);
  158. secondpass(left);
  159. (*
  160. { save regvars loaded in the beginning so that we can restore them }
  161. { when processing the else-block }
  162. if cs_regvars in aktglobalswitches then
  163. begin
  164. org_list := exprasmlist;
  165. exprasmlist := taasmoutput.create;
  166. end;
  167. *)
  168. maketojumpbool(exprasmlist,left,lr_dont_load_regvars);
  169. (*
  170. if cs_regvars in aktglobalswitches then
  171. begin
  172. org_regvar_loaded_int := rg.regvar_loaded_int;
  173. org_regvar_loaded_other := rg.regvar_loaded_other;
  174. end;
  175. *)
  176. if assigned(right) then
  177. begin
  178. cg.a_label(exprasmlist,truelabel);
  179. secondpass(right);
  180. end;
  181. { save current asmlist (previous instructions + then-block) and }
  182. { loaded regvar state and create new clean ones }
  183. if cs_regvars in aktglobalswitches then
  184. begin
  185. { then_regvar_loaded_int := rg.regvar_loaded_int;
  186. then_regvar_loaded_other := rg.regvar_loaded_other;
  187. rg.regvar_loaded_int := org_regvar_loaded_int;
  188. rg.regvar_loaded_other := org_regvar_loaded_other;
  189. then_list := exprasmlist;
  190. exprasmlist := taasmoutput.create;}
  191. end;
  192. if assigned(t1) then
  193. begin
  194. if assigned(right) then
  195. begin
  196. objectlibrary.getlabel(hl);
  197. { do go back to if line !! }
  198. (*
  199. if not(cs_regvars in aktglobalswitches) then
  200. *)
  201. aktfilepos:=exprasmList.getlasttaifilepos^
  202. (*
  203. else
  204. aktfilepos:=then_list.getlasttaifilepos^
  205. *)
  206. ;
  207. cg.a_jmp_always(exprasmlist,hl);
  208. end;
  209. cg.a_label(exprasmlist,falselabel);
  210. secondpass(t1);
  211. (*
  212. { save current asmlist (previous instructions + else-block) }
  213. { and loaded regvar state and create a new clean list }
  214. if cs_regvars in aktglobalswitches then
  215. begin
  216. { else_regvar_loaded_int := rg.regvar_loaded_int;
  217. else_regvar_loaded_other := rg.regvar_loaded_other;}
  218. else_list := exprasmlist;
  219. exprasmlist := taasmoutput.create;
  220. end;
  221. *)
  222. if assigned(right) then
  223. cg.a_label(exprasmlist,hl);
  224. end
  225. else
  226. begin
  227. (*
  228. if cs_regvars in aktglobalswitches then
  229. begin
  230. { else_regvar_loaded_int := rg.regvar_loaded_int;
  231. else_regvar_loaded_other := rg.regvar_loaded_other;}
  232. else_list := exprasmlist;
  233. exprasmlist := taasmoutput.create;
  234. end;
  235. *)
  236. cg.a_label(exprasmlist,falselabel);
  237. end;
  238. if not(assigned(right)) then
  239. begin
  240. cg.a_label(exprasmlist,truelabel);
  241. end;
  242. (*
  243. if cs_regvars in aktglobalswitches then
  244. begin
  245. { add loads of regvars at the end of the then- and else-blocks }
  246. { so that at the end of both blocks the same regvars are loaded }
  247. { no else block? }
  248. if not assigned(t1) then
  249. begin
  250. sync_regvars_int(org_list,then_list,org_regvar_loaded_int,then_regvar_loaded_int);
  251. sync_regvars_other(org_list,then_list,org_regvar_loaded_other,then_regvar_loaded_other);
  252. end
  253. { no then block? }
  254. else if not assigned(right) then
  255. begin
  256. sync_regvars_int(org_list,else_list,org_regvar_loaded_int,else_regvar_loaded_int);
  257. sync_regvars_other(org_list,else_list,org_regvar_loaded_other,else_regvar_loaded_other);
  258. end
  259. { both else and then blocks }
  260. else
  261. begin
  262. sync_regvars_int(then_list,else_list,then_regvar_loaded_int,else_regvar_loaded_int);
  263. sync_regvars_other(then_list,else_list,then_regvar_loaded_other,else_regvar_loaded_other);
  264. end;
  265. { add all lists together }
  266. org_list.concatlist(then_list);
  267. then_list.free;
  268. org_list.concatlist(else_list);
  269. else_list.free;
  270. org_list.concatlist(exprasmlist);
  271. exprasmlist.free;
  272. exprasmlist := org_list;
  273. end;
  274. *)
  275. truelabel:=otlabel;
  276. falselabel:=oflabel;
  277. end;
  278. {*****************************************************************************
  279. SecondFor
  280. *****************************************************************************}
  281. procedure tcgfornode.pass_2;
  282. var
  283. l3,oldclabel,oldblabel : tasmlabel;
  284. temptovalue : boolean;
  285. hop : topcg;
  286. hcond : topcmp;
  287. opsize : tcgsize;
  288. count_var_is_signed,do_loopvar_at_end : boolean;
  289. cmp_const:Tconstexprint;
  290. oldflowcontrol : tflowcontrol;
  291. begin
  292. location_reset(location,LOC_VOID,OS_NO);
  293. oldflowcontrol:=flowcontrol;
  294. oldclabel:=aktcontinuelabel;
  295. oldblabel:=aktbreaklabel;
  296. objectlibrary.getlabel(aktcontinuelabel);
  297. objectlibrary.getlabel(aktbreaklabel);
  298. objectlibrary.getlabel(l3);
  299. { only calculate reference }
  300. opsize := def_cgsize(left.resulttype.def);
  301. count_var_is_signed:=is_signed(left.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(t1);
  306. { calculate pointer value and check if changeable and if so }
  307. { load into temporary variable }
  308. if t1.nodetype<>ordconstn then
  309. begin
  310. do_loopvar_at_end:=false;
  311. location_force_reg(exprasmlist,t1.location,t1.location.size,true);
  312. temptovalue:=true;
  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_reg_loc_label(exprasmlist,opsize,hcond,
  345. t1.location.register,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. if assigned(entrylabel) then
  368. cg.a_jmp_always(exprasmlist,tcglabelnode(entrylabel).getasmlabel);
  369. { align loop target }
  370. if not(cs_littlesize in aktglobalswitches) then
  371. exprasmList.concat(Tai_align.Create(aktalignment.loopalign));
  372. cg.a_label(exprasmlist,l3);
  373. {If the loopvar doesn't mind on exit, we avoid the loopvar inc/dec
  374. after the loop body instead of here.}
  375. if not do_loopvar_at_end then
  376. begin
  377. { according to count direction DEC or INC... }
  378. if lnf_backward in loopflags then
  379. hop:=OP_SUB
  380. else
  381. hop:=OP_ADD;
  382. cg.a_op_const_loc(exprasmlist,hop,1,left.location);
  383. end;
  384. if assigned(t2) then
  385. begin
  386. secondpass(t2);
  387. {$ifdef OLDREGVARS}
  388. load_all_regvars(exprasmlist);
  389. {$endif OLDREGVARS}
  390. end;
  391. {If the loopvar doesn't mind on exit, we do the loopvar inc/dec
  392. after the loop body instead of here.}
  393. if do_loopvar_at_end then
  394. begin
  395. { according to count direction DEC or INC... }
  396. if lnf_backward in loopflags then
  397. hop:=OP_SUB
  398. else
  399. hop:=OP_ADD;
  400. cg.a_op_const_loc(exprasmlist,hop,1,left.location);
  401. end;
  402. cg.a_label(exprasmlist,aktcontinuelabel);
  403. if do_loopvar_at_end then
  404. if lnf_backward in loopflags then
  405. if count_var_is_signed then
  406. hcond:=OC_GTE
  407. else
  408. hcond:=OC_AE
  409. else
  410. if count_var_is_signed then
  411. hcond:=OC_LTE
  412. else
  413. hcond:=OC_BE
  414. else
  415. if lnf_backward in loopflags then
  416. if count_var_is_signed then
  417. hcond:=OC_GT
  418. else
  419. hcond:=OC_A
  420. else
  421. if count_var_is_signed then
  422. hcond:=OC_LT
  423. else
  424. hcond:=OC_B;
  425. {$ifdef OLDREGVARS}
  426. load_all_regvars(exprasmlist);
  427. {$endif OLDREGVARS}
  428. { produce comparison and the corresponding }
  429. { jump }
  430. if temptovalue then
  431. begin
  432. cg.a_cmp_reg_loc_label(exprasmlist,opsize,hcond,t1.location.register,
  433. left.location,l3);
  434. end
  435. else
  436. begin
  437. cmp_const:=Tordconstnode(t1).value;
  438. if do_loopvar_at_end then
  439. begin
  440. {Watch out for wrap around 255 -> 0.}
  441. {Ugly: This code is way to long... Use tables?}
  442. case opsize of
  443. OS_8:
  444. begin
  445. if lnf_backward in loopflags then
  446. begin
  447. if byte(cmp_const)=low(byte) then
  448. begin
  449. hcond:=OC_NE;
  450. cmp_const:=high(byte);
  451. end
  452. end
  453. else
  454. begin
  455. if byte(cmp_const)=high(byte) then
  456. begin
  457. hcond:=OC_NE;
  458. cmp_const:=low(byte);
  459. end
  460. end
  461. end;
  462. OS_16:
  463. begin
  464. if lnf_backward in loopflags then
  465. begin
  466. if word(cmp_const)=high(word) then
  467. begin
  468. hcond:=OC_NE;
  469. cmp_const:=low(word);
  470. end
  471. end
  472. else
  473. begin
  474. if word(cmp_const)=low(word) then
  475. begin
  476. hcond:=OC_NE;
  477. cmp_const:=high(word);
  478. end
  479. end
  480. end;
  481. OS_32:
  482. begin
  483. if lnf_backward in loopflags then
  484. begin
  485. if cardinal(cmp_const)=high(cardinal) then
  486. begin
  487. hcond:=OC_NE;
  488. cmp_const:=low(cardinal);
  489. end
  490. end
  491. else
  492. begin
  493. if cardinal(cmp_const)=low(cardinal) then
  494. begin
  495. hcond:=OC_NE;
  496. cmp_const:=high(cardinal);
  497. end
  498. end
  499. end;
  500. OS_64:
  501. begin
  502. if lnf_backward in loopflags then
  503. begin
  504. if qword(cmp_const)=high(qword) then
  505. begin
  506. hcond:=OC_NE;
  507. cmp_const:=low(qword);
  508. end
  509. end
  510. else
  511. begin
  512. if qword(cmp_const)=low(qword) then
  513. begin
  514. hcond:=OC_NE;
  515. cmp_const:=high(qword);
  516. end
  517. end
  518. end;
  519. OS_S8:
  520. begin
  521. if lnf_backward in loopflags then
  522. begin
  523. if shortint(cmp_const)=low(shortint) then
  524. begin
  525. hcond:=OC_NE;
  526. cmp_const:=high(shortint);
  527. end
  528. end
  529. else
  530. begin
  531. if shortint(cmp_const)=high(shortint) then
  532. begin
  533. hcond:=OC_NE;
  534. cmp_const:=low(shortint);
  535. end
  536. end
  537. end;
  538. OS_S16:
  539. begin
  540. if lnf_backward in loopflags then
  541. begin
  542. if integer(cmp_const)=high(smallint) then
  543. begin
  544. hcond:=OC_NE;
  545. cmp_const:=low(smallint);
  546. end
  547. end
  548. else
  549. begin
  550. if integer(cmp_const)=low(smallint) then
  551. begin
  552. hcond:=OC_NE;
  553. cmp_const:=high(smallint);
  554. end
  555. end
  556. end;
  557. OS_S32:
  558. begin
  559. if lnf_backward in loopflags then
  560. begin
  561. if longint(cmp_const)=high(longint) then
  562. begin
  563. hcond:=OC_NE;
  564. cmp_const:=low(longint);
  565. end
  566. end
  567. else
  568. begin
  569. if longint(cmp_const)=low(longint) then
  570. begin
  571. hcond:=OC_NE;
  572. cmp_const:=high(longint);
  573. end
  574. end
  575. end;
  576. OS_S64:
  577. begin
  578. if lnf_backward in loopflags then
  579. begin
  580. if int64(cmp_const)=high(int64) then
  581. begin
  582. hcond:=OC_NE;
  583. cmp_const:=low(int64);
  584. end
  585. end
  586. else
  587. begin
  588. if int64(cmp_const)=low(int64) then
  589. begin
  590. hcond:=OC_NE;
  591. cmp_const:=high(int64);
  592. end
  593. end
  594. end;
  595. else
  596. internalerror(200201021);
  597. end;
  598. end;
  599. cg.a_cmp_const_loc_label(exprasmlist,opsize,hcond,
  600. cmp_const,left.location,l3);
  601. end;
  602. { this is the break label: }
  603. cg.a_label(exprasmlist,aktbreaklabel);
  604. aktcontinuelabel:=oldclabel;
  605. aktbreaklabel:=oldblabel;
  606. { a break/continue in a while/repeat block can't be seen outside }
  607. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue]);
  608. end;
  609. {*****************************************************************************
  610. SecondExitN
  611. *****************************************************************************}
  612. procedure tcgexitnode.pass_2;
  613. begin
  614. location_reset(location,LOC_VOID,OS_NO);
  615. include(flowcontrol,fc_exit);
  616. if assigned(left) then
  617. secondpass(left);
  618. cg.a_jmp_always(exprasmlist,current_procinfo.aktexitlabel);
  619. end;
  620. {*****************************************************************************
  621. SecondBreakN
  622. *****************************************************************************}
  623. procedure tcgbreaknode.pass_2;
  624. begin
  625. location_reset(location,LOC_VOID,OS_NO);
  626. include(flowcontrol,fc_break);
  627. if aktbreaklabel<>nil then
  628. begin
  629. {$ifdef OLDREGVARS}
  630. load_all_regvars(exprasmlist);
  631. {$endif OLDREGVARS}
  632. cg.a_jmp_always(exprasmlist,aktbreaklabel)
  633. end
  634. else
  635. CGMessage(cg_e_break_not_allowed);
  636. end;
  637. {*****************************************************************************
  638. SecondContinueN
  639. *****************************************************************************}
  640. procedure tcgcontinuenode.pass_2;
  641. begin
  642. location_reset(location,LOC_VOID,OS_NO);
  643. include(flowcontrol,fc_continue);
  644. if aktcontinuelabel<>nil then
  645. begin
  646. {$ifdef OLDREGVARS}
  647. load_all_regvars(exprasmlist);
  648. {$endif OLDREGVARS}
  649. cg.a_jmp_always(exprasmlist,aktcontinuelabel)
  650. end
  651. else
  652. CGMessage(cg_e_continue_not_allowed);
  653. end;
  654. {*****************************************************************************
  655. SecondGoto
  656. *****************************************************************************}
  657. procedure tcggotonode.pass_2;
  658. begin
  659. location_reset(location,LOC_VOID,OS_NO);
  660. {$ifdef OLDREGVARS}
  661. load_all_regvars(exprasmlist);
  662. {$endif OLDREGVARS}
  663. cg.a_jmp_always(exprasmlist,tcglabelnode(labelnode).getasmlabel)
  664. end;
  665. {*****************************************************************************
  666. SecondLabel
  667. *****************************************************************************}
  668. function tcglabelnode.getasmlabel : tasmlabel;
  669. begin
  670. if not(assigned(asmlabel)) then
  671. objectlibrary.getlabel(asmlabel);
  672. result:=asmlabel
  673. end;
  674. procedure tcglabelnode.pass_2;
  675. begin
  676. location_reset(location,LOC_VOID,OS_NO);
  677. {$ifdef OLDREGVARS}
  678. load_all_regvars(exprasmlist);
  679. {$endif OLDREGVARS}
  680. cg.a_label(exprasmlist,getasmlabel);
  681. secondpass(left);
  682. end;
  683. {*****************************************************************************
  684. SecondRaise
  685. *****************************************************************************}
  686. procedure tcgraisenode.pass_2;
  687. var
  688. a : tasmlabel;
  689. href2: treference;
  690. paraloc1,paraloc2,paraloc3 : tcgpara;
  691. begin
  692. paraloc1.init;
  693. paraloc2.init;
  694. paraloc3.init;
  695. paramanager.getintparaloc(pocall_default,1,paraloc1);
  696. paramanager.getintparaloc(pocall_default,2,paraloc2);
  697. paramanager.getintparaloc(pocall_default,3,paraloc3);
  698. location_reset(location,LOC_VOID,OS_NO);
  699. if assigned(left) then
  700. begin
  701. { multiple parameters? }
  702. if assigned(right) then
  703. begin
  704. if assigned(frametree) then
  705. secondpass(frametree);
  706. secondpass(right);
  707. end;
  708. secondpass(left);
  709. if codegenerror then
  710. exit;
  711. { Push parameters }
  712. if assigned(right) then
  713. begin
  714. paramanager.allocparaloc(exprasmlist,paraloc3);
  715. if assigned(frametree) then
  716. cg.a_param_loc(exprasmlist,frametree.location,paraloc3)
  717. else
  718. cg.a_param_const(exprasmlist,OS_INT,0,paraloc3);
  719. { push address }
  720. paramanager.allocparaloc(exprasmlist,paraloc2);
  721. cg.a_param_loc(exprasmlist,right.location,paraloc2);
  722. end
  723. else
  724. begin
  725. { get current address }
  726. objectlibrary.getaddrlabel(a);
  727. cg.a_label(exprasmlist,a);
  728. reference_reset_symbol(href2,a,0);
  729. { push current frame }
  730. paramanager.allocparaloc(exprasmlist,paraloc3);
  731. cg.a_param_reg(exprasmlist,OS_ADDR,NR_FRAME_POINTER_REG,paraloc3);
  732. { push current address }
  733. paramanager.allocparaloc(exprasmlist,paraloc2);
  734. if target_info.system <> system_powerpc_macos then
  735. cg.a_paramaddr_ref(exprasmlist,href2,paraloc2)
  736. else
  737. cg.a_param_const(exprasmlist,OS_INT,0,paraloc2);
  738. end;
  739. paramanager.allocparaloc(exprasmlist,paraloc1);
  740. cg.a_param_loc(exprasmlist,left.location,paraloc1);
  741. paramanager.freeparaloc(exprasmlist,paraloc1);
  742. paramanager.freeparaloc(exprasmlist,paraloc2);
  743. paramanager.freeparaloc(exprasmlist,paraloc3);
  744. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  745. cg.a_call_name(exprasmlist,'FPC_RAISEEXCEPTION');
  746. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  747. end
  748. else
  749. begin
  750. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  751. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  752. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  753. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  754. end;
  755. paraloc1.done;
  756. paraloc2.done;
  757. paraloc3.done;
  758. end;
  759. {*****************************************************************************
  760. SecondTryExcept
  761. *****************************************************************************}
  762. var
  763. endexceptlabel : tasmlabel;
  764. { does the necessary things to clean up the object stack }
  765. { in the except block }
  766. procedure cleanupobjectstack;
  767. var
  768. paraloc1 : tcgpara;
  769. begin
  770. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  771. cg.a_call_name(exprasmlist,'FPC_POPOBJECTSTACK');
  772. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  773. paraloc1.init;
  774. paramanager.getintparaloc(pocall_default,1,paraloc1);
  775. paramanager.allocparaloc(exprasmlist,paraloc1);
  776. cg.a_param_reg(exprasmlist,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  777. paramanager.freeparaloc(exprasmlist,paraloc1);
  778. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  779. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  780. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  781. paraloc1.done;
  782. end;
  783. procedure tcgtryexceptnode.pass_2;
  784. var
  785. exceptlabel,doexceptlabel,oldendexceptlabel,
  786. lastonlabel,
  787. exitexceptlabel,
  788. continueexceptlabel,
  789. breakexceptlabel,
  790. exittrylabel,
  791. continuetrylabel,
  792. breaktrylabel,
  793. doobjectdestroy,
  794. doobjectdestroyandreraise,
  795. oldaktexitlabel,
  796. oldaktcontinuelabel,
  797. oldaktbreaklabel : tasmlabel;
  798. oldflowcontrol,tryflowcontrol,
  799. exceptflowcontrol : tflowcontrol;
  800. destroytemps,
  801. excepttemps : texceptiontemps;
  802. paraloc1 : tcgpara;
  803. label
  804. errorexit;
  805. begin
  806. location_reset(location,LOC_VOID,OS_NO);
  807. oldflowcontrol:=flowcontrol;
  808. flowcontrol:=[];
  809. { this can be called recursivly }
  810. oldaktbreaklabel:=nil;
  811. oldaktcontinuelabel:=nil;
  812. oldendexceptlabel:=endexceptlabel;
  813. { save the old labels for control flow statements }
  814. oldaktexitlabel:=current_procinfo.aktexitlabel;
  815. if assigned(aktbreaklabel) then
  816. begin
  817. oldaktcontinuelabel:=aktcontinuelabel;
  818. oldaktbreaklabel:=aktbreaklabel;
  819. end;
  820. { get new labels for the control flow statements }
  821. objectlibrary.getlabel(exittrylabel);
  822. objectlibrary.getlabel(exitexceptlabel);
  823. if assigned(aktbreaklabel) then
  824. begin
  825. objectlibrary.getlabel(breaktrylabel);
  826. objectlibrary.getlabel(continuetrylabel);
  827. objectlibrary.getlabel(breakexceptlabel);
  828. objectlibrary.getlabel(continueexceptlabel);
  829. end;
  830. objectlibrary.getlabel(exceptlabel);
  831. objectlibrary.getlabel(doexceptlabel);
  832. objectlibrary.getlabel(endexceptlabel);
  833. objectlibrary.getlabel(lastonlabel);
  834. get_exception_temps(exprasmlist,excepttemps);
  835. new_exception(exprasmlist,excepttemps,exceptlabel);
  836. { try block }
  837. { set control flow labels for the try block }
  838. current_procinfo.aktexitlabel:=exittrylabel;
  839. if assigned(oldaktbreaklabel) then
  840. begin
  841. aktcontinuelabel:=continuetrylabel;
  842. aktbreaklabel:=breaktrylabel;
  843. end;
  844. flowcontrol:=[];
  845. secondpass(left);
  846. tryflowcontrol:=flowcontrol;
  847. if codegenerror then
  848. goto errorexit;
  849. cg.a_label(exprasmlist,exceptlabel);
  850. free_exception(exprasmlist, excepttemps, 0, endexceptlabel, false);
  851. cg.a_label(exprasmlist,doexceptlabel);
  852. { set control flow labels for the except block }
  853. { and the on statements }
  854. current_procinfo.aktexitlabel:=exitexceptlabel;
  855. if assigned(oldaktbreaklabel) then
  856. begin
  857. aktcontinuelabel:=continueexceptlabel;
  858. aktbreaklabel:=breakexceptlabel;
  859. end;
  860. flowcontrol:=[];
  861. { on statements }
  862. if assigned(right) then
  863. secondpass(right);
  864. cg.a_label(exprasmlist,lastonlabel);
  865. { default handling except handling }
  866. if assigned(t1) then
  867. begin
  868. { FPC_CATCHES must be called with
  869. 'default handler' flag (=-1)
  870. }
  871. paraloc1.init;
  872. paramanager.getintparaloc(pocall_default,1,paraloc1);
  873. paramanager.allocparaloc(exprasmlist,paraloc1);
  874. cg.a_param_const(exprasmlist,OS_ADDR,-1,paraloc1);
  875. paramanager.freeparaloc(exprasmlist,paraloc1);
  876. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  877. cg.a_call_name(exprasmlist,'FPC_CATCHES');
  878. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  879. paraloc1.done;
  880. { the destruction of the exception object must be also }
  881. { guarded by an exception frame }
  882. objectlibrary.getlabel(doobjectdestroy);
  883. objectlibrary.getlabel(doobjectdestroyandreraise);
  884. get_exception_temps(exprasmlist,destroytemps);
  885. new_exception(exprasmlist,destroytemps,doobjectdestroyandreraise);
  886. { here we don't have to reset flowcontrol }
  887. { the default and on flowcontrols are handled equal }
  888. secondpass(t1);
  889. exceptflowcontrol:=flowcontrol;
  890. cg.a_label(exprasmlist,doobjectdestroyandreraise);
  891. free_exception(exprasmlist,destroytemps,0,doobjectdestroy,false);
  892. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  893. cg.a_call_name(exprasmlist,'FPC_POPSECONDOBJECTSTACK');
  894. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  895. paraloc1.init;
  896. paramanager.getintparaloc(pocall_default,1,paraloc1);
  897. paramanager.allocparaloc(exprasmlist,paraloc1);
  898. cg.a_param_reg(exprasmlist, OS_ADDR, NR_FUNCTION_RESULT_REG, paraloc1);
  899. paramanager.freeparaloc(exprasmlist,paraloc1);
  900. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  901. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  902. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  903. paraloc1.done;
  904. { we don't need to restore esi here because reraise never }
  905. { returns }
  906. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  907. cg.a_label(exprasmlist,doobjectdestroy);
  908. cleanupobjectstack;
  909. unget_exception_temps(exprasmlist,destroytemps);
  910. cg.a_jmp_always(exprasmlist,endexceptlabel);
  911. end
  912. else
  913. begin
  914. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  915. exceptflowcontrol:=flowcontrol;
  916. end;
  917. if fc_exit in exceptflowcontrol then
  918. begin
  919. { do some magic for exit in the try block }
  920. cg.a_label(exprasmlist,exitexceptlabel);
  921. { we must also destroy the address frame which guards }
  922. { exception object }
  923. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  924. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  925. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  926. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  927. cleanupobjectstack;
  928. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  929. end;
  930. if fc_break in exceptflowcontrol then
  931. begin
  932. cg.a_label(exprasmlist,breakexceptlabel);
  933. { we must also destroy the address frame which guards }
  934. { exception object }
  935. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  936. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  937. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  938. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  939. cleanupobjectstack;
  940. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  941. end;
  942. if fc_continue in exceptflowcontrol then
  943. begin
  944. cg.a_label(exprasmlist,continueexceptlabel);
  945. { we must also destroy the address frame which guards }
  946. { exception object }
  947. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  948. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  949. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  950. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  951. cleanupobjectstack;
  952. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  953. end;
  954. if fc_exit in tryflowcontrol then
  955. begin
  956. { do some magic for exit in the try block }
  957. cg.a_label(exprasmlist,exittrylabel);
  958. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  959. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  960. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  961. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  962. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  963. end;
  964. if fc_break in tryflowcontrol then
  965. begin
  966. cg.a_label(exprasmlist,breaktrylabel);
  967. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  968. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  969. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  970. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  971. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  972. end;
  973. if fc_continue in tryflowcontrol then
  974. begin
  975. cg.a_label(exprasmlist,continuetrylabel);
  976. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  977. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  978. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  979. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  980. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  981. end;
  982. unget_exception_temps(exprasmlist,excepttemps);
  983. cg.a_label(exprasmlist,endexceptlabel);
  984. errorexit:
  985. { restore all saved labels }
  986. endexceptlabel:=oldendexceptlabel;
  987. { restore the control flow labels }
  988. current_procinfo.aktexitlabel:=oldaktexitlabel;
  989. if assigned(oldaktbreaklabel) then
  990. begin
  991. aktcontinuelabel:=oldaktcontinuelabel;
  992. aktbreaklabel:=oldaktbreaklabel;
  993. end;
  994. { return all used control flow statements }
  995. flowcontrol:=oldflowcontrol+exceptflowcontrol+
  996. tryflowcontrol;
  997. end;
  998. procedure tcgonnode.pass_2;
  999. var
  1000. nextonlabel,
  1001. exitonlabel,
  1002. continueonlabel,
  1003. breakonlabel,
  1004. oldaktexitlabel,
  1005. oldaktcontinuelabel,
  1006. doobjectdestroyandreraise,
  1007. doobjectdestroy,
  1008. oldaktbreaklabel : tasmlabel;
  1009. oldflowcontrol : tflowcontrol;
  1010. excepttemps : texceptiontemps;
  1011. exceptref,
  1012. href2: treference;
  1013. paraloc1 : tcgpara;
  1014. begin
  1015. paraloc1.init;
  1016. location_reset(location,LOC_VOID,OS_NO);
  1017. oldflowcontrol:=flowcontrol;
  1018. flowcontrol:=[];
  1019. objectlibrary.getlabel(nextonlabel);
  1020. { send the vmt parameter }
  1021. reference_reset_symbol(href2,objectlibrary.newasmsymbol(excepttype.vmt_mangledname,AB_EXTERNAL,AT_DATA),0);
  1022. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1023. paramanager.allocparaloc(exprasmlist,paraloc1);
  1024. cg.a_paramaddr_ref(exprasmlist,href2,paraloc1);
  1025. paramanager.freeparaloc(exprasmlist,paraloc1);
  1026. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1027. cg.a_call_name(exprasmlist,'FPC_CATCHES');
  1028. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1029. { is it this catch? No. go to next onlabel }
  1030. cg.a_cmp_const_reg_label(exprasmlist,OS_ADDR,OC_EQ,0,NR_FUNCTION_RESULT_REG,nextonlabel);
  1031. { what a hack ! }
  1032. if assigned(exceptsymtable) then
  1033. begin
  1034. tlocalvarsym(exceptsymtable.symindex.first).localloc.loc:=LOC_REFERENCE;
  1035. tlocalvarsym(exceptsymtable.symindex.first).localloc.size:=OS_ADDR;
  1036. tg.GetLocal(exprasmlist,sizeof(aint),voidpointertype.def,
  1037. tlocalvarsym(exceptsymtable.symindex.first).localloc.reference);
  1038. cg.a_load_reg_ref(exprasmlist,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,tlocalvarsym(exceptsymtable.symindex.first).localloc.reference);
  1039. end
  1040. else
  1041. begin
  1042. tg.GetTemp(exprasmlist,sizeof(aint),tt_normal,exceptref);
  1043. cg.a_load_reg_ref(exprasmlist,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,exceptref);
  1044. end;
  1045. { in the case that another exception is risen
  1046. we've to destroy the old one }
  1047. objectlibrary.getlabel(doobjectdestroyandreraise);
  1048. { call setjmp, and jump to finally label on non-zero result }
  1049. get_exception_temps(exprasmlist,excepttemps);
  1050. new_exception(exprasmlist,excepttemps,doobjectdestroyandreraise);
  1051. oldaktbreaklabel:=nil;
  1052. oldaktcontinuelabel:=nil;
  1053. if assigned(right) then
  1054. begin
  1055. oldaktexitlabel:=current_procinfo.aktexitlabel;
  1056. objectlibrary.getlabel(exitonlabel);
  1057. current_procinfo.aktexitlabel:=exitonlabel;
  1058. if assigned(aktbreaklabel) then
  1059. begin
  1060. oldaktcontinuelabel:=aktcontinuelabel;
  1061. oldaktbreaklabel:=aktbreaklabel;
  1062. objectlibrary.getlabel(breakonlabel);
  1063. objectlibrary.getlabel(continueonlabel);
  1064. aktcontinuelabel:=continueonlabel;
  1065. aktbreaklabel:=breakonlabel;
  1066. end;
  1067. secondpass(right);
  1068. end;
  1069. objectlibrary.getlabel(doobjectdestroy);
  1070. cg.a_label(exprasmlist,doobjectdestroyandreraise);
  1071. free_exception(exprasmlist,excepttemps,0,doobjectdestroy,false);
  1072. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1073. cg.a_call_name(exprasmlist,'FPC_POPSECONDOBJECTSTACK');
  1074. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1075. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1076. paramanager.allocparaloc(exprasmlist,paraloc1);
  1077. cg.a_param_reg(exprasmlist, OS_ADDR, NR_FUNCTION_RESULT_REG, paraloc1);
  1078. paramanager.freeparaloc(exprasmlist,paraloc1);
  1079. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1080. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  1081. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1082. { we don't need to restore esi here because reraise never }
  1083. { returns }
  1084. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1085. cg.a_label(exprasmlist,doobjectdestroy);
  1086. cleanupobjectstack;
  1087. { clear some stuff }
  1088. if assigned(exceptsymtable) then
  1089. begin
  1090. tg.UngetLocal(exprasmlist,tlocalvarsym(exceptsymtable.symindex.first).localloc.reference);
  1091. tlocalvarsym(exceptsymtable.symindex.first).localloc.loc:=LOC_INVALID;
  1092. end
  1093. else
  1094. tg.Ungettemp(exprasmlist,exceptref);
  1095. cg.a_jmp_always(exprasmlist,endexceptlabel);
  1096. if assigned(right) then
  1097. begin
  1098. { special handling for control flow instructions }
  1099. if fc_exit in flowcontrol then
  1100. begin
  1101. { the address and object pop does secondtryexcept }
  1102. cg.a_label(exprasmlist,exitonlabel);
  1103. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  1104. end;
  1105. if fc_break in flowcontrol then
  1106. begin
  1107. { the address and object pop does secondtryexcept }
  1108. cg.a_label(exprasmlist,breakonlabel);
  1109. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  1110. end;
  1111. if fc_continue in flowcontrol then
  1112. begin
  1113. { the address and object pop does secondtryexcept }
  1114. cg.a_label(exprasmlist,continueonlabel);
  1115. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  1116. end;
  1117. current_procinfo.aktexitlabel:=oldaktexitlabel;
  1118. if assigned(oldaktbreaklabel) then
  1119. begin
  1120. aktcontinuelabel:=oldaktcontinuelabel;
  1121. aktbreaklabel:=oldaktbreaklabel;
  1122. end;
  1123. end;
  1124. unget_exception_temps(exprasmlist,excepttemps);
  1125. cg.a_label(exprasmlist,nextonlabel);
  1126. flowcontrol:=oldflowcontrol+flowcontrol;
  1127. paraloc1.done;
  1128. { next on node }
  1129. if assigned(left) then
  1130. secondpass(left);
  1131. end;
  1132. {*****************************************************************************
  1133. SecondTryFinally
  1134. *****************************************************************************}
  1135. procedure tcgtryfinallynode.pass_2;
  1136. var
  1137. reraiselabel,
  1138. finallylabel,
  1139. endfinallylabel,
  1140. exitfinallylabel,
  1141. continuefinallylabel,
  1142. breakfinallylabel,
  1143. oldaktexitlabel,
  1144. oldaktcontinuelabel,
  1145. oldaktbreaklabel : tasmlabel;
  1146. oldflowcontrol,tryflowcontrol : tflowcontrol;
  1147. decconst : longint;
  1148. excepttemps : texceptiontemps;
  1149. begin
  1150. location_reset(location,LOC_VOID,OS_NO);
  1151. { check if child nodes do a break/continue/exit }
  1152. oldflowcontrol:=flowcontrol;
  1153. flowcontrol:=[];
  1154. objectlibrary.getlabel(finallylabel);
  1155. objectlibrary.getlabel(endfinallylabel);
  1156. objectlibrary.getlabel(reraiselabel);
  1157. { the finally block must catch break, continue and exit }
  1158. { statements }
  1159. oldaktexitlabel:=current_procinfo.aktexitlabel;
  1160. if implicitframe then
  1161. exitfinallylabel:=finallylabel
  1162. else
  1163. objectlibrary.getlabel(exitfinallylabel);
  1164. current_procinfo.aktexitlabel:=exitfinallylabel;
  1165. if assigned(aktbreaklabel) then
  1166. begin
  1167. oldaktcontinuelabel:=aktcontinuelabel;
  1168. oldaktbreaklabel:=aktbreaklabel;
  1169. if implicitframe then
  1170. begin
  1171. breakfinallylabel:=finallylabel;
  1172. continuefinallylabel:=finallylabel;
  1173. end
  1174. else
  1175. begin
  1176. objectlibrary.getlabel(breakfinallylabel);
  1177. objectlibrary.getlabel(continuefinallylabel);
  1178. end;
  1179. aktcontinuelabel:=continuefinallylabel;
  1180. aktbreaklabel:=breakfinallylabel;
  1181. end;
  1182. { call setjmp, and jump to finally label on non-zero result }
  1183. get_exception_temps(exprasmlist,excepttemps);
  1184. new_exception(exprasmlist,excepttemps,finallylabel);
  1185. { try code }
  1186. if assigned(left) then
  1187. begin
  1188. secondpass(left);
  1189. tryflowcontrol:=flowcontrol;
  1190. if codegenerror then
  1191. exit;
  1192. end;
  1193. cg.a_label(exprasmlist,finallylabel);
  1194. { just free the frame information }
  1195. free_exception(exprasmlist,excepttemps,1,finallylabel,true);
  1196. { finally code }
  1197. flowcontrol:=[];
  1198. secondpass(right);
  1199. if flowcontrol<>[] then
  1200. CGMessage(cg_e_control_flow_outside_finally);
  1201. if codegenerror then
  1202. exit;
  1203. { the value should now be in the exception handler }
  1204. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  1205. if implicitframe then
  1206. begin
  1207. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,endfinallylabel);
  1208. { finally code only needed to be executed on exception }
  1209. flowcontrol:=[];
  1210. secondpass(t1);
  1211. if flowcontrol<>[] then
  1212. CGMessage(cg_e_control_flow_outside_finally);
  1213. if codegenerror then
  1214. exit;
  1215. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1216. end
  1217. else
  1218. begin
  1219. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,endfinallylabel);
  1220. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_INT,1,NR_FUNCTION_RESULT_REG);
  1221. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,reraiselabel);
  1222. if fc_exit in tryflowcontrol then
  1223. begin
  1224. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_INT,1,NR_FUNCTION_RESULT_REG);
  1225. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldaktexitlabel);
  1226. decconst:=1;
  1227. end
  1228. else
  1229. decconst:=2;
  1230. if fc_break in tryflowcontrol then
  1231. begin
  1232. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_INT,decconst,NR_FUNCTION_RESULT_REG);
  1233. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldaktbreaklabel);
  1234. decconst:=1;
  1235. end
  1236. else
  1237. inc(decconst);
  1238. if fc_continue in tryflowcontrol then
  1239. begin
  1240. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_INT,decconst,NR_FUNCTION_RESULT_REG);
  1241. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldaktcontinuelabel);
  1242. end;
  1243. cg.a_label(exprasmlist,reraiselabel);
  1244. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1245. { do some magic for exit,break,continue in the try block }
  1246. if fc_exit in tryflowcontrol then
  1247. begin
  1248. cg.a_label(exprasmlist,exitfinallylabel);
  1249. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  1250. cg.g_exception_reason_save_const(exprasmlist,excepttemps.reasonbuf,2);
  1251. cg.a_jmp_always(exprasmlist,finallylabel);
  1252. end;
  1253. if fc_break in tryflowcontrol then
  1254. begin
  1255. cg.a_label(exprasmlist,breakfinallylabel);
  1256. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  1257. cg.g_exception_reason_save_const(exprasmlist,excepttemps.reasonbuf,3);
  1258. cg.a_jmp_always(exprasmlist,finallylabel);
  1259. end;
  1260. if fc_continue in tryflowcontrol then
  1261. begin
  1262. cg.a_label(exprasmlist,continuefinallylabel);
  1263. cg.g_exception_reason_load(exprasmlist,excepttemps.reasonbuf);
  1264. cg.g_exception_reason_save_const(exprasmlist,excepttemps.reasonbuf,4);
  1265. cg.a_jmp_always(exprasmlist,finallylabel);
  1266. end;
  1267. end;
  1268. unget_exception_temps(exprasmlist,excepttemps);
  1269. cg.a_label(exprasmlist,endfinallylabel);
  1270. current_procinfo.aktexitlabel:=oldaktexitlabel;
  1271. if assigned(aktbreaklabel) then
  1272. begin
  1273. aktcontinuelabel:=oldaktcontinuelabel;
  1274. aktbreaklabel:=oldaktbreaklabel;
  1275. end;
  1276. flowcontrol:=oldflowcontrol+tryflowcontrol;
  1277. end;
  1278. begin
  1279. cwhilerepeatnode:=tcgwhilerepeatnode;
  1280. cifnode:=tcgifnode;
  1281. cfornode:=tcgfornode;
  1282. cexitnode:=tcgexitnode;
  1283. cbreaknode:=tcgbreaknode;
  1284. ccontinuenode:=tcgcontinuenode;
  1285. cgotonode:=tcggotonode;
  1286. clabelnode:=tcglabelnode;
  1287. craisenode:=tcgraisenode;
  1288. ctryexceptnode:=tcgtryexceptnode;
  1289. ctryfinallynode:=tcgtryfinallynode;
  1290. connode:=tcgonnode;
  1291. end.