ncgflw.pas 65 KB

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