ncgflw.pas 62 KB

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