ncgflw.pas 60 KB

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