ncgflw.pas 65 KB

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