ncgflw.pas 55 KB

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