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