ncgflw.pas 62 KB

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