ncgflw.pas 62 KB

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