ncgflw.pas 63 KB

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