ncgflw.pas 59 KB

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