ncgflw.pas 61 KB

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