ncgflw.pas 59 KB

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