ncgflw.pas 63 KB

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