ncgflw.pas 61 KB

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