ncgflw.pas 62 KB

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