ncgflw.pas 61 KB

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