ncgflw.pas 60 KB

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