ncgflw.pas 62 KB

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