ncgflw.pas 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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 (t1) is not necessary (it cannot be a regvar, }
  335. { see webtbs/tw8883) }
  336. gen_sync_regvars(current_asmdata.CurrAsmList,usedregvars);
  337. end
  338. else
  339. begin
  340. gen_sync_regvars(current_asmdata.CurrAsmList,usedregvars);
  341. usedregvars.intregvars.done;
  342. usedregvars.fpuregvars.done;
  343. usedregvars.mmregvars.done;
  344. end;
  345. end;
  346. end;
  347. procedure tcgfornode.pass_generate_code;
  348. var
  349. l3,oldclabel,oldblabel : tasmlabel;
  350. temptovalue : boolean;
  351. hop : topcg;
  352. hcond : topcmp;
  353. opsize : tcgsize;
  354. count_var_is_signed,do_loopvar_at_end : boolean;
  355. cmp_const:Tconstexprint;
  356. oldflowcontrol : tflowcontrol;
  357. begin
  358. location_reset(location,LOC_VOID,OS_NO);
  359. oldflowcontrol:=flowcontrol;
  360. include(flowcontrol,fc_inflowcontrol);
  361. oldclabel:=current_procinfo.CurrContinueLabel;
  362. oldblabel:=current_procinfo.CurrBreakLabel;
  363. current_asmdata.getjumplabel(current_procinfo.CurrContinueLabel);
  364. current_asmdata.getjumplabel(current_procinfo.CurrBreakLabel);
  365. current_asmdata.getjumplabel(l3);
  366. { only calculate reference }
  367. opsize := def_cgsize(left.resultdef);
  368. count_var_is_signed:=is_signed(left.resultdef);
  369. { first set the to value
  370. because the count var can be in the expression ! }
  371. do_loopvar_at_end:=(lnf_dont_mind_loopvar_on_exit in loopflags)
  372. { if the loop is unrolled and there is a jump into the loop,
  373. then we can't do the trick with incrementing the loop var only at the
  374. end
  375. }
  376. and not(assigned(entrylabel));
  377. secondpass(t1);
  378. { calculate pointer value and check if changeable and if so }
  379. { load into temporary variable }
  380. if t1.nodetype<>ordconstn then
  381. begin
  382. do_loopvar_at_end:=false;
  383. location_force_reg(current_asmdata.CurrAsmList,t1.location,t1.location.size,false);
  384. temptovalue:=true;
  385. end
  386. else
  387. temptovalue:=false;
  388. { produce start assignment }
  389. secondpass(left);
  390. secondpass(right);
  391. case left.location.loc of
  392. LOC_REFERENCE,
  393. LOC_CREFERENCE :
  394. cg.a_load_loc_ref(current_asmdata.CurrAsmList,left.location.size,right.location,left.location.reference);
  395. LOC_REGISTER,
  396. LOC_CREGISTER:
  397. cg.a_load_loc_reg(current_asmdata.CurrAsmList,left.location.size,right.location,left.location.register);
  398. LOC_SUBSETREG,
  399. LOC_CSUBSETREG :
  400. cg.a_load_loc_subsetreg(current_asmdata.CurrAsmList,left.location.size,right.location,left.location.sreg);
  401. else
  402. internalerror(200501311);
  403. end;
  404. if lnf_backward in loopflags then
  405. if count_var_is_signed then
  406. hcond:=OC_LT
  407. else
  408. hcond:=OC_B
  409. else
  410. if count_var_is_signed then
  411. hcond:=OC_GT
  412. else
  413. hcond:=OC_A;
  414. sync_regvars(true);
  415. {$ifdef OLDREGVARS}
  416. load_all_regvars(current_asmdata.CurrAsmList);
  417. {$endif OLDREGVARS}
  418. if temptovalue then
  419. begin
  420. cg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,opsize,hcond,
  421. t1.location.register,left.location,current_procinfo.CurrBreakLabel);
  422. end
  423. else
  424. begin
  425. if lnf_testatbegin in loopflags then
  426. begin
  427. cg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,opsize,hcond,
  428. tordconstnode(t1).value,
  429. left.location,current_procinfo.CurrBreakLabel);
  430. end;
  431. end;
  432. {If the loopvar doesn't mind on exit, we avoid this ugly
  433. dec instruction and do the loopvar inc/dec after the loop
  434. body.}
  435. if not do_loopvar_at_end then
  436. begin
  437. if lnf_backward in loopflags then
  438. hop:=OP_ADD
  439. else
  440. hop:=OP_SUB;
  441. cg.a_op_const_loc(current_asmdata.CurrAsmList,hop,1,left.location);
  442. end;
  443. if assigned(entrylabel) then
  444. cg.a_jmp_always(current_asmdata.CurrAsmList,tcglabelnode(entrylabel).getasmlabel);
  445. { align loop target }
  446. if not(cs_opt_size in current_settings.optimizerswitches) then
  447. current_asmdata.CurrAsmList.concat(Tai_align.Create(current_settings.alignment.loopalign));
  448. cg.a_label(current_asmdata.CurrAsmList,l3);
  449. {If the loopvar doesn't mind on exit, we avoid the loopvar inc/dec
  450. after the loop body instead of here.}
  451. if not do_loopvar_at_end then
  452. begin
  453. { according to count direction DEC or INC... }
  454. if lnf_backward in loopflags then
  455. hop:=OP_SUB
  456. else
  457. hop:=OP_ADD;
  458. cg.a_op_const_loc(current_asmdata.CurrAsmList,hop,1,left.location);
  459. end;
  460. if assigned(t2) then
  461. begin
  462. secondpass(t2);
  463. {$ifdef OLDREGVARS}
  464. load_all_regvars(current_asmdata.CurrAsmList);
  465. {$endif OLDREGVARS}
  466. end;
  467. {If the loopvar doesn't mind on exit, we do the loopvar inc/dec
  468. after the loop body instead of here.}
  469. if do_loopvar_at_end then
  470. begin
  471. { according to count direction DEC or INC... }
  472. if lnf_backward in loopflags then
  473. hop:=OP_SUB
  474. else
  475. hop:=OP_ADD;
  476. cg.a_op_const_loc(current_asmdata.CurrAsmList,hop,1,left.location);
  477. end;
  478. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrContinueLabel);
  479. if do_loopvar_at_end then
  480. if lnf_backward in loopflags then
  481. if count_var_is_signed then
  482. hcond:=OC_GTE
  483. else
  484. hcond:=OC_AE
  485. else
  486. if count_var_is_signed then
  487. hcond:=OC_LTE
  488. else
  489. hcond:=OC_BE
  490. else
  491. if lnf_backward in loopflags then
  492. if count_var_is_signed then
  493. hcond:=OC_GT
  494. else
  495. hcond:=OC_A
  496. else
  497. if count_var_is_signed then
  498. hcond:=OC_LT
  499. else
  500. hcond:=OC_B;
  501. {$ifdef OLDREGVARS}
  502. load_all_regvars(current_asmdata.CurrAsmList);
  503. {$endif OLDREGVARS}
  504. { produce comparison and the corresponding }
  505. { jump }
  506. if temptovalue then
  507. begin
  508. cg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,opsize,hcond,t1.location.register,
  509. left.location,l3);
  510. end
  511. else
  512. begin
  513. cmp_const:=Tordconstnode(t1).value;
  514. if do_loopvar_at_end then
  515. begin
  516. {Watch out for wrap around 255 -> 0.}
  517. {Ugly: This code is way to long... Use tables?}
  518. case opsize of
  519. OS_8:
  520. begin
  521. if lnf_backward in loopflags then
  522. begin
  523. if byte(cmp_const)=low(byte) then
  524. begin
  525. hcond:=OC_NE;
  526. cmp_const:=high(byte);
  527. end
  528. end
  529. else
  530. begin
  531. if byte(cmp_const)=high(byte) then
  532. begin
  533. hcond:=OC_NE;
  534. cmp_const:=low(byte);
  535. end
  536. end
  537. end;
  538. OS_16:
  539. begin
  540. if lnf_backward in loopflags then
  541. begin
  542. if word(cmp_const)=high(word) then
  543. begin
  544. hcond:=OC_NE;
  545. cmp_const:=low(word);
  546. end
  547. end
  548. else
  549. begin
  550. if word(cmp_const)=low(word) then
  551. begin
  552. hcond:=OC_NE;
  553. cmp_const:=high(word);
  554. end
  555. end
  556. end;
  557. OS_32:
  558. begin
  559. if lnf_backward in loopflags then
  560. begin
  561. if cardinal(cmp_const)=high(cardinal) then
  562. begin
  563. hcond:=OC_NE;
  564. cmp_const:=low(cardinal);
  565. end
  566. end
  567. else
  568. begin
  569. if cardinal(cmp_const)=low(cardinal) then
  570. begin
  571. hcond:=OC_NE;
  572. cmp_const:=high(cardinal);
  573. end
  574. end
  575. end;
  576. OS_64:
  577. begin
  578. if lnf_backward in loopflags then
  579. begin
  580. if qword(cmp_const)=high(qword) then
  581. begin
  582. hcond:=OC_NE;
  583. cmp_const:=low(qword);
  584. end
  585. end
  586. else
  587. begin
  588. if qword(cmp_const)=low(qword) then
  589. begin
  590. hcond:=OC_NE;
  591. cmp_const:=high(qword);
  592. end
  593. end
  594. end;
  595. OS_S8:
  596. begin
  597. if lnf_backward in loopflags then
  598. begin
  599. if shortint(cmp_const)=low(shortint) then
  600. begin
  601. hcond:=OC_NE;
  602. cmp_const:=high(shortint);
  603. end
  604. end
  605. else
  606. begin
  607. if shortint(cmp_const)=high(shortint) then
  608. begin
  609. hcond:=OC_NE;
  610. cmp_const:=low(shortint);
  611. end
  612. end
  613. end;
  614. OS_S16:
  615. begin
  616. if lnf_backward in loopflags then
  617. begin
  618. if integer(cmp_const)=high(smallint) then
  619. begin
  620. hcond:=OC_NE;
  621. cmp_const:=low(smallint);
  622. end
  623. end
  624. else
  625. begin
  626. if integer(cmp_const)=low(smallint) then
  627. begin
  628. hcond:=OC_NE;
  629. cmp_const:=high(smallint);
  630. end
  631. end
  632. end;
  633. OS_S32:
  634. begin
  635. if lnf_backward in loopflags then
  636. begin
  637. if longint(cmp_const)=high(longint) then
  638. begin
  639. hcond:=OC_NE;
  640. cmp_const:=low(longint);
  641. end
  642. end
  643. else
  644. begin
  645. if longint(cmp_const)=low(longint) then
  646. begin
  647. hcond:=OC_NE;
  648. cmp_const:=high(longint);
  649. end
  650. end
  651. end;
  652. OS_S64:
  653. begin
  654. if lnf_backward in loopflags then
  655. begin
  656. if int64(cmp_const)=high(int64) then
  657. begin
  658. hcond:=OC_NE;
  659. cmp_const:=low(int64);
  660. end
  661. end
  662. else
  663. begin
  664. if int64(cmp_const)=low(int64) then
  665. begin
  666. hcond:=OC_NE;
  667. cmp_const:=high(int64);
  668. end
  669. end
  670. end;
  671. else
  672. internalerror(200201021);
  673. end;
  674. end;
  675. cg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,opsize,hcond,
  676. aint(cmp_const),left.location,l3);
  677. end;
  678. { this is the break label: }
  679. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrBreakLabel);
  680. sync_regvars(false);
  681. current_procinfo.CurrContinueLabel:=oldclabel;
  682. current_procinfo.CurrBreakLabel:=oldblabel;
  683. { a break/continue in a while/repeat block can't be seen outside }
  684. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);
  685. end;
  686. {*****************************************************************************
  687. SecondExitN
  688. *****************************************************************************}
  689. procedure tcgexitnode.pass_generate_code;
  690. begin
  691. location_reset(location,LOC_VOID,OS_NO);
  692. include(flowcontrol,fc_exit);
  693. if assigned(left) then
  694. secondpass(left);
  695. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrExitLabel);
  696. end;
  697. {*****************************************************************************
  698. SecondBreakN
  699. *****************************************************************************}
  700. procedure tcgbreaknode.pass_generate_code;
  701. begin
  702. location_reset(location,LOC_VOID,OS_NO);
  703. include(flowcontrol,fc_break);
  704. if current_procinfo.CurrBreakLabel<>nil then
  705. begin
  706. {$ifdef OLDREGVARS}
  707. load_all_regvars(current_asmdata.CurrAsmList);
  708. {$endif OLDREGVARS}
  709. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrBreakLabel)
  710. end
  711. else
  712. CGMessage(cg_e_break_not_allowed);
  713. end;
  714. {*****************************************************************************
  715. SecondContinueN
  716. *****************************************************************************}
  717. procedure tcgcontinuenode.pass_generate_code;
  718. begin
  719. location_reset(location,LOC_VOID,OS_NO);
  720. include(flowcontrol,fc_continue);
  721. if current_procinfo.CurrContinueLabel<>nil then
  722. begin
  723. {$ifdef OLDREGVARS}
  724. load_all_regvars(current_asmdata.CurrAsmList);
  725. {$endif OLDREGVARS}
  726. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrContinueLabel)
  727. end
  728. else
  729. CGMessage(cg_e_continue_not_allowed);
  730. end;
  731. {*****************************************************************************
  732. SecondGoto
  733. *****************************************************************************}
  734. procedure tcggotonode.pass_generate_code;
  735. begin
  736. location_reset(location,LOC_VOID,OS_NO);
  737. include(flowcontrol,fc_gotolabel);
  738. {$ifdef OLDREGVARS}
  739. load_all_regvars(current_asmdata.CurrAsmList);
  740. {$endif OLDREGVARS}
  741. cg.a_jmp_always(current_asmdata.CurrAsmList,tcglabelnode(labelnode).getasmlabel)
  742. end;
  743. {*****************************************************************************
  744. SecondLabel
  745. *****************************************************************************}
  746. function tcglabelnode.getasmlabel : tasmlabel;
  747. begin
  748. if not(assigned(asmlabel)) then
  749. current_asmdata.getjumplabel(asmlabel);
  750. result:=asmlabel
  751. end;
  752. procedure tcglabelnode.pass_generate_code;
  753. begin
  754. location_reset(location,LOC_VOID,OS_NO);
  755. include(flowcontrol,fc_gotolabel);
  756. {$ifdef OLDREGVARS}
  757. load_all_regvars(current_asmdata.CurrAsmList);
  758. {$endif OLDREGVARS}
  759. cg.a_label(current_asmdata.CurrAsmList,getasmlabel);
  760. secondpass(left);
  761. end;
  762. {*****************************************************************************
  763. SecondRaise
  764. *****************************************************************************}
  765. procedure tcgraisenode.pass_generate_code;
  766. var
  767. a : tasmlabel;
  768. href2: treference;
  769. paraloc1,paraloc2,paraloc3 : tcgpara;
  770. begin
  771. paraloc1.init;
  772. paraloc2.init;
  773. paraloc3.init;
  774. paramanager.getintparaloc(pocall_default,1,paraloc1);
  775. paramanager.getintparaloc(pocall_default,2,paraloc2);
  776. paramanager.getintparaloc(pocall_default,3,paraloc3);
  777. location_reset(location,LOC_VOID,OS_NO);
  778. if assigned(left) then
  779. begin
  780. { multiple parameters? }
  781. if assigned(right) then
  782. begin
  783. if assigned(frametree) then
  784. secondpass(frametree);
  785. secondpass(right);
  786. end;
  787. secondpass(left);
  788. if codegenerror then
  789. exit;
  790. { Push parameters }
  791. if assigned(right) then
  792. begin
  793. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc3);
  794. if assigned(frametree) then
  795. cg.a_param_loc(current_asmdata.CurrAsmList,frametree.location,paraloc3)
  796. else
  797. cg.a_param_const(current_asmdata.CurrAsmList,OS_INT,0,paraloc3);
  798. { push address }
  799. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  800. cg.a_param_loc(current_asmdata.CurrAsmList,right.location,paraloc2);
  801. end
  802. else
  803. begin
  804. { get current address }
  805. current_asmdata.getaddrlabel(a);
  806. cg.a_label(current_asmdata.CurrAsmList,a);
  807. reference_reset_symbol(href2,a,0);
  808. { push current frame }
  809. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc3);
  810. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,NR_FRAME_POINTER_REG,paraloc3);
  811. { push current address }
  812. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  813. if target_info.system <> system_powerpc_macos then
  814. cg.a_paramaddr_ref(current_asmdata.CurrAsmList,href2,paraloc2)
  815. else
  816. cg.a_param_const(current_asmdata.CurrAsmList,OS_INT,0,paraloc2);
  817. end;
  818. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  819. cg.a_param_loc(current_asmdata.CurrAsmList,left.location,paraloc1);
  820. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  821. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  822. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc3);
  823. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  824. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RAISEEXCEPTION');
  825. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  826. end
  827. else
  828. begin
  829. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  830. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK');
  831. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE');
  832. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  833. end;
  834. paraloc1.done;
  835. paraloc2.done;
  836. paraloc3.done;
  837. end;
  838. {*****************************************************************************
  839. SecondTryExcept
  840. *****************************************************************************}
  841. var
  842. endexceptlabel : tasmlabel;
  843. { does the necessary things to clean up the object stack }
  844. { in the except block }
  845. procedure cleanupobjectstack;
  846. var
  847. paraloc1 : tcgpara;
  848. begin
  849. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  850. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPOBJECTSTACK');
  851. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  852. paraloc1.init;
  853. paramanager.getintparaloc(pocall_default,1,paraloc1);
  854. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  855. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  856. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  857. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  858. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DESTROYEXCEPTION');
  859. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  860. paraloc1.done;
  861. end;
  862. procedure tcgtryexceptnode.pass_generate_code;
  863. var
  864. exceptlabel,doexceptlabel,oldendexceptlabel,
  865. lastonlabel,
  866. exitexceptlabel,
  867. continueexceptlabel,
  868. breakexceptlabel,
  869. exittrylabel,
  870. continuetrylabel,
  871. breaktrylabel,
  872. doobjectdestroy,
  873. doobjectdestroyandreraise,
  874. oldCurrExitLabel,
  875. oldContinueLabel,
  876. oldBreakLabel : tasmlabel;
  877. oldflowcontrol,tryflowcontrol,
  878. exceptflowcontrol : tflowcontrol;
  879. destroytemps,
  880. excepttemps : texceptiontemps;
  881. paraloc1 : tcgpara;
  882. label
  883. errorexit;
  884. begin
  885. location_reset(location,LOC_VOID,OS_NO);
  886. oldflowcontrol:=flowcontrol;
  887. flowcontrol:=[fc_inflowcontrol];
  888. { this can be called recursivly }
  889. oldBreakLabel:=nil;
  890. oldContinueLabel:=nil;
  891. oldendexceptlabel:=endexceptlabel;
  892. { save the old labels for control flow statements }
  893. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  894. if assigned(current_procinfo.CurrBreakLabel) then
  895. begin
  896. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  897. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  898. end;
  899. { get new labels for the control flow statements }
  900. current_asmdata.getjumplabel(exittrylabel);
  901. current_asmdata.getjumplabel(exitexceptlabel);
  902. if assigned(current_procinfo.CurrBreakLabel) then
  903. begin
  904. current_asmdata.getjumplabel(breaktrylabel);
  905. current_asmdata.getjumplabel(continuetrylabel);
  906. current_asmdata.getjumplabel(breakexceptlabel);
  907. current_asmdata.getjumplabel(continueexceptlabel);
  908. end;
  909. current_asmdata.getjumplabel(exceptlabel);
  910. current_asmdata.getjumplabel(doexceptlabel);
  911. current_asmdata.getjumplabel(endexceptlabel);
  912. current_asmdata.getjumplabel(lastonlabel);
  913. get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  914. new_exception(current_asmdata.CurrAsmList,excepttemps,exceptlabel);
  915. { try block }
  916. { set control flow labels for the try block }
  917. current_procinfo.CurrExitLabel:=exittrylabel;
  918. if assigned(oldBreakLabel) then
  919. begin
  920. current_procinfo.CurrContinueLabel:=continuetrylabel;
  921. current_procinfo.CurrBreakLabel:=breaktrylabel;
  922. end;
  923. flowcontrol:=[fc_inflowcontrol];
  924. secondpass(left);
  925. tryflowcontrol:=flowcontrol;
  926. if codegenerror then
  927. goto errorexit;
  928. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  929. free_exception(current_asmdata.CurrAsmList, excepttemps, 0, endexceptlabel, false);
  930. cg.a_label(current_asmdata.CurrAsmList,doexceptlabel);
  931. { set control flow labels for the except block }
  932. { and the on statements }
  933. current_procinfo.CurrExitLabel:=exitexceptlabel;
  934. if assigned(oldBreakLabel) then
  935. begin
  936. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  937. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  938. end;
  939. flowcontrol:=[fc_inflowcontrol];
  940. { on statements }
  941. if assigned(right) then
  942. secondpass(right);
  943. cg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  944. { default handling except handling }
  945. if assigned(t1) then
  946. begin
  947. { FPC_CATCHES must be called with
  948. 'default handler' flag (=-1)
  949. }
  950. paraloc1.init;
  951. paramanager.getintparaloc(pocall_default,1,paraloc1);
  952. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  953. cg.a_param_const(current_asmdata.CurrAsmList,OS_ADDR,-1,paraloc1);
  954. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  955. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  956. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CATCHES');
  957. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  958. paraloc1.done;
  959. { the destruction of the exception object must be also }
  960. { guarded by an exception frame }
  961. current_asmdata.getjumplabel(doobjectdestroy);
  962. current_asmdata.getjumplabel(doobjectdestroyandreraise);
  963. get_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  964. new_exception(current_asmdata.CurrAsmList,destroytemps,doobjectdestroyandreraise);
  965. { here we don't have to reset flowcontrol }
  966. { the default and on flowcontrols are handled equal }
  967. secondpass(t1);
  968. exceptflowcontrol:=flowcontrol;
  969. cg.a_label(current_asmdata.CurrAsmList,doobjectdestroyandreraise);
  970. free_exception(current_asmdata.CurrAsmList,destroytemps,0,doobjectdestroy,false);
  971. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  972. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPSECONDOBJECTSTACK');
  973. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  974. paraloc1.init;
  975. paramanager.getintparaloc(pocall_default,1,paraloc1);
  976. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  977. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR, NR_FUNCTION_RESULT_REG, paraloc1);
  978. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  979. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  980. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DESTROYEXCEPTION');
  981. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  982. paraloc1.done;
  983. { we don't need to restore esi here because reraise never }
  984. { returns }
  985. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE');
  986. cg.a_label(current_asmdata.CurrAsmList,doobjectdestroy);
  987. cleanupobjectstack;
  988. unget_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  989. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  990. end
  991. else
  992. begin
  993. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE');
  994. exceptflowcontrol:=flowcontrol;
  995. end;
  996. if fc_exit in exceptflowcontrol then
  997. begin
  998. { do some magic for exit in the try block }
  999. cg.a_label(current_asmdata.CurrAsmList,exitexceptlabel);
  1000. { we must also destroy the address frame which guards }
  1001. { exception object }
  1002. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1003. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK');
  1004. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1005. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1006. cleanupobjectstack;
  1007. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  1008. end;
  1009. if fc_break in exceptflowcontrol then
  1010. begin
  1011. cg.a_label(current_asmdata.CurrAsmList,breakexceptlabel);
  1012. { we must also destroy the address frame which guards }
  1013. { exception object }
  1014. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1015. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK');
  1016. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1017. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1018. cleanupobjectstack;
  1019. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  1020. end;
  1021. if fc_continue in exceptflowcontrol then
  1022. begin
  1023. cg.a_label(current_asmdata.CurrAsmList,continueexceptlabel);
  1024. { we must also destroy the address frame which guards }
  1025. { exception object }
  1026. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1027. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK');
  1028. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1029. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1030. cleanupobjectstack;
  1031. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  1032. end;
  1033. if fc_exit in tryflowcontrol then
  1034. begin
  1035. { do some magic for exit in the try block }
  1036. cg.a_label(current_asmdata.CurrAsmList,exittrylabel);
  1037. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1038. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK');
  1039. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1040. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1041. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  1042. end;
  1043. if fc_break in tryflowcontrol then
  1044. begin
  1045. cg.a_label(current_asmdata.CurrAsmList,breaktrylabel);
  1046. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1047. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK');
  1048. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1049. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1050. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  1051. end;
  1052. if fc_continue in tryflowcontrol then
  1053. begin
  1054. cg.a_label(current_asmdata.CurrAsmList,continuetrylabel);
  1055. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1056. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPADDRSTACK');
  1057. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1058. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1059. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  1060. end;
  1061. unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1062. cg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  1063. errorexit:
  1064. { restore all saved labels }
  1065. endexceptlabel:=oldendexceptlabel;
  1066. { restore the control flow labels }
  1067. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  1068. if assigned(oldBreakLabel) then
  1069. begin
  1070. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  1071. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  1072. end;
  1073. { return all used control flow statements }
  1074. flowcontrol:=oldflowcontrol+(exceptflowcontrol +
  1075. tryflowcontrol - [fc_inflowcontrol]);
  1076. end;
  1077. procedure tcgonnode.pass_generate_code;
  1078. var
  1079. nextonlabel,
  1080. exitonlabel,
  1081. continueonlabel,
  1082. breakonlabel,
  1083. oldCurrExitLabel,
  1084. oldContinueLabel,
  1085. doobjectdestroyandreraise,
  1086. doobjectdestroy,
  1087. oldBreakLabel : tasmlabel;
  1088. oldflowcontrol : tflowcontrol;
  1089. excepttemps : texceptiontemps;
  1090. exceptref,
  1091. href2: treference;
  1092. paraloc1 : tcgpara;
  1093. exceptvarsym : tlocalvarsym;
  1094. begin
  1095. paraloc1.init;
  1096. location_reset(location,LOC_VOID,OS_NO);
  1097. oldflowcontrol:=flowcontrol;
  1098. flowcontrol:=[fc_inflowcontrol];
  1099. current_asmdata.getjumplabel(nextonlabel);
  1100. { send the vmt parameter }
  1101. reference_reset_symbol(href2,current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname),0);
  1102. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1103. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  1104. cg.a_paramaddr_ref(current_asmdata.CurrAsmList,href2,paraloc1);
  1105. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  1106. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1107. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CATCHES');
  1108. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1109. { is it this catch? No. go to next onlabel }
  1110. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_EQ,0,NR_FUNCTION_RESULT_REG,nextonlabel);
  1111. { Retrieve exception variable }
  1112. if assigned(excepTSymtable) then
  1113. exceptvarsym:=tlocalvarsym(excepTSymtable.SymList[0])
  1114. else
  1115. exceptvarsym:=nil;
  1116. if assigned(exceptvarsym) then
  1117. begin
  1118. exceptvarsym.localloc.loc:=LOC_REFERENCE;
  1119. exceptvarsym.localloc.size:=OS_ADDR;
  1120. tg.GetLocal(current_asmdata.CurrAsmList,sizeof(aint),voidpointertype,exceptvarsym.localloc.reference);
  1121. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,exceptvarsym.localloc.reference);
  1122. end
  1123. else
  1124. begin
  1125. tg.GetTemp(current_asmdata.CurrAsmList,sizeof(aint),tt_normal,exceptref);
  1126. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,exceptref);
  1127. end;
  1128. { in the case that another exception is risen
  1129. we've to destroy the old one }
  1130. current_asmdata.getjumplabel(doobjectdestroyandreraise);
  1131. { call setjmp, and jump to finally label on non-zero result }
  1132. get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1133. new_exception(current_asmdata.CurrAsmList,excepttemps,doobjectdestroyandreraise);
  1134. oldBreakLabel:=nil;
  1135. oldContinueLabel:=nil;
  1136. if assigned(right) then
  1137. begin
  1138. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  1139. current_asmdata.getjumplabel(exitonlabel);
  1140. current_procinfo.CurrExitLabel:=exitonlabel;
  1141. if assigned(current_procinfo.CurrBreakLabel) then
  1142. begin
  1143. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  1144. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  1145. current_asmdata.getjumplabel(breakonlabel);
  1146. current_asmdata.getjumplabel(continueonlabel);
  1147. current_procinfo.CurrContinueLabel:=continueonlabel;
  1148. current_procinfo.CurrBreakLabel:=breakonlabel;
  1149. end;
  1150. secondpass(right);
  1151. end;
  1152. current_asmdata.getjumplabel(doobjectdestroy);
  1153. cg.a_label(current_asmdata.CurrAsmList,doobjectdestroyandreraise);
  1154. free_exception(current_asmdata.CurrAsmList,excepttemps,0,doobjectdestroy,false);
  1155. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1156. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPSECONDOBJECTSTACK');
  1157. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1158. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1159. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  1160. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR, NR_FUNCTION_RESULT_REG, paraloc1);
  1161. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  1162. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1163. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DESTROYEXCEPTION');
  1164. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1165. { we don't need to store/restore registers here because reraise never
  1166. returns }
  1167. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE');
  1168. cg.a_label(current_asmdata.CurrAsmList,doobjectdestroy);
  1169. cleanupobjectstack;
  1170. { clear some stuff }
  1171. if assigned(exceptvarsym) then
  1172. begin
  1173. tg.UngetLocal(current_asmdata.CurrAsmList,exceptvarsym.localloc.reference);
  1174. exceptvarsym.localloc.loc:=LOC_INVALID;
  1175. end
  1176. else
  1177. tg.Ungettemp(current_asmdata.CurrAsmList,exceptref);
  1178. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  1179. if assigned(right) then
  1180. begin
  1181. { special handling for control flow instructions }
  1182. if fc_exit in flowcontrol then
  1183. begin
  1184. { the address and object pop does secondtryexcept }
  1185. cg.a_label(current_asmdata.CurrAsmList,exitonlabel);
  1186. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  1187. end;
  1188. if fc_break in flowcontrol then
  1189. begin
  1190. { the address and object pop does secondtryexcept }
  1191. cg.a_label(current_asmdata.CurrAsmList,breakonlabel);
  1192. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  1193. end;
  1194. if fc_continue in flowcontrol then
  1195. begin
  1196. { the address and object pop does secondtryexcept }
  1197. cg.a_label(current_asmdata.CurrAsmList,continueonlabel);
  1198. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  1199. end;
  1200. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  1201. if assigned(oldBreakLabel) then
  1202. begin
  1203. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  1204. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  1205. end;
  1206. end;
  1207. unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1208. cg.a_label(current_asmdata.CurrAsmList,nextonlabel);
  1209. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_inflowcontrol]);
  1210. paraloc1.done;
  1211. { next on node }
  1212. if assigned(left) then
  1213. secondpass(left);
  1214. end;
  1215. {*****************************************************************************
  1216. SecondTryFinally
  1217. *****************************************************************************}
  1218. procedure tcgtryfinallynode.pass_generate_code;
  1219. var
  1220. reraiselabel,
  1221. finallylabel,
  1222. endfinallylabel,
  1223. exitfinallylabel,
  1224. continuefinallylabel,
  1225. breakfinallylabel,
  1226. oldCurrExitLabel,
  1227. oldContinueLabel,
  1228. oldBreakLabel : tasmlabel;
  1229. oldflowcontrol,tryflowcontrol : tflowcontrol;
  1230. decconst : longint;
  1231. excepttemps : texceptiontemps;
  1232. begin
  1233. location_reset(location,LOC_VOID,OS_NO);
  1234. { check if child nodes do a break/continue/exit }
  1235. oldflowcontrol:=flowcontrol;
  1236. flowcontrol:=[fc_inflowcontrol];
  1237. current_asmdata.getjumplabel(finallylabel);
  1238. current_asmdata.getjumplabel(endfinallylabel);
  1239. current_asmdata.getjumplabel(reraiselabel);
  1240. { the finally block must catch break, continue and exit }
  1241. { statements }
  1242. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  1243. if implicitframe then
  1244. exitfinallylabel:=finallylabel
  1245. else
  1246. current_asmdata.getjumplabel(exitfinallylabel);
  1247. current_procinfo.CurrExitLabel:=exitfinallylabel;
  1248. if assigned(current_procinfo.CurrBreakLabel) then
  1249. begin
  1250. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  1251. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  1252. if implicitframe then
  1253. begin
  1254. breakfinallylabel:=finallylabel;
  1255. continuefinallylabel:=finallylabel;
  1256. end
  1257. else
  1258. begin
  1259. current_asmdata.getjumplabel(breakfinallylabel);
  1260. current_asmdata.getjumplabel(continuefinallylabel);
  1261. end;
  1262. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  1263. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  1264. end;
  1265. { call setjmp, and jump to finally label on non-zero result }
  1266. get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1267. new_exception(current_asmdata.CurrAsmList,excepttemps,finallylabel);
  1268. { try code }
  1269. if assigned(left) then
  1270. begin
  1271. secondpass(left);
  1272. tryflowcontrol:=flowcontrol;
  1273. if codegenerror then
  1274. exit;
  1275. end;
  1276. cg.a_label(current_asmdata.CurrAsmList,finallylabel);
  1277. { just free the frame information }
  1278. free_exception(current_asmdata.CurrAsmList,excepttemps,1,finallylabel,true);
  1279. { finally code }
  1280. flowcontrol:=[fc_inflowcontrol];
  1281. secondpass(right);
  1282. if flowcontrol<>[fc_inflowcontrol] then
  1283. CGMessage(cg_e_control_flow_outside_finally);
  1284. if codegenerror then
  1285. exit;
  1286. { the value should now be in the exception handler }
  1287. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1288. if implicitframe then
  1289. begin
  1290. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,endfinallylabel);
  1291. { finally code only needed to be executed on exception }
  1292. flowcontrol:=[fc_inflowcontrol];
  1293. secondpass(t1);
  1294. if flowcontrol<>[fc_inflowcontrol] then
  1295. CGMessage(cg_e_control_flow_outside_finally);
  1296. if codegenerror then
  1297. exit;
  1298. {$if defined(x86) or defined(arm)}
  1299. if current_procinfo.procdef.proccalloption=pocall_safecall then
  1300. begin
  1301. { Remove and destroy the last exception object }
  1302. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_POPOBJECTSTACK');
  1303. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DESTROYEXCEPTION');
  1304. { Set return value of safecall procedure to indicate exception. }
  1305. { Exception will be raised after procedure exit based on return value }
  1306. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_ADDR,aint($8000FFFF),NR_FUNCTION_RETURN_REG);
  1307. end
  1308. else
  1309. {$endif}
  1310. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE');
  1311. end
  1312. else
  1313. begin
  1314. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,endfinallylabel);
  1315. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,1,NR_FUNCTION_RESULT_REG);
  1316. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,reraiselabel);
  1317. if fc_exit in tryflowcontrol then
  1318. begin
  1319. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,1,NR_FUNCTION_RESULT_REG);
  1320. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldCurrExitLabel);
  1321. decconst:=1;
  1322. end
  1323. else
  1324. decconst:=2;
  1325. if fc_break in tryflowcontrol then
  1326. begin
  1327. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,decconst,NR_FUNCTION_RESULT_REG);
  1328. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldBreakLabel);
  1329. decconst:=1;
  1330. end
  1331. else
  1332. inc(decconst);
  1333. if fc_continue in tryflowcontrol then
  1334. begin
  1335. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,decconst,NR_FUNCTION_RESULT_REG);
  1336. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,0,NR_FUNCTION_RESULT_REG,oldContinueLabel);
  1337. end;
  1338. cg.a_label(current_asmdata.CurrAsmList,reraiselabel);
  1339. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RERAISE');
  1340. { do some magic for exit,break,continue in the try block }
  1341. if fc_exit in tryflowcontrol then
  1342. begin
  1343. cg.a_label(current_asmdata.CurrAsmList,exitfinallylabel);
  1344. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1345. cg.g_exception_reason_save_const(current_asmdata.CurrAsmList,excepttemps.reasonbuf,2);
  1346. cg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  1347. end;
  1348. if fc_break in tryflowcontrol then
  1349. begin
  1350. cg.a_label(current_asmdata.CurrAsmList,breakfinallylabel);
  1351. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1352. cg.g_exception_reason_save_const(current_asmdata.CurrAsmList,excepttemps.reasonbuf,3);
  1353. cg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  1354. end;
  1355. if fc_continue in tryflowcontrol then
  1356. begin
  1357. cg.a_label(current_asmdata.CurrAsmList,continuefinallylabel);
  1358. cg.g_exception_reason_load(current_asmdata.CurrAsmList,excepttemps.reasonbuf);
  1359. cg.g_exception_reason_save_const(current_asmdata.CurrAsmList,excepttemps.reasonbuf,4);
  1360. cg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  1361. end;
  1362. end;
  1363. unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1364. cg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  1365. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  1366. if assigned(current_procinfo.CurrBreakLabel) then
  1367. begin
  1368. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  1369. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  1370. end;
  1371. flowcontrol:=oldflowcontrol+(tryflowcontrol-[fc_inflowcontrol]);
  1372. end;
  1373. begin
  1374. cwhilerepeatnode:=tcgwhilerepeatnode;
  1375. cifnode:=tcgifnode;
  1376. cfornode:=tcgfornode;
  1377. cexitnode:=tcgexitnode;
  1378. cbreaknode:=tcgbreaknode;
  1379. ccontinuenode:=tcgcontinuenode;
  1380. cgotonode:=tcggotonode;
  1381. clabelnode:=tcglabelnode;
  1382. craisenode:=tcgraisenode;
  1383. ctryexceptnode:=tcgtryexceptnode;
  1384. ctryfinallynode:=tcgtryfinallynode;
  1385. connode:=tcgonnode;
  1386. end.