ncgflw.pas 65 KB

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