ncgflw.pas 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate assembler for nodes that influence the flow which are
  5. the same for all (most?) processors
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit ncgflw;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. node,nflw;
  24. type
  25. tcgwhilerepeatnode = class(twhilerepeatnode)
  26. procedure pass_2;override;
  27. end;
  28. tcgifnode = class(tifnode)
  29. procedure pass_2;override;
  30. end;
  31. tcgfornode = class(tfornode)
  32. procedure pass_2;override;
  33. end;
  34. tcgexitnode = class(texitnode)
  35. procedure pass_2;override;
  36. end;
  37. tcgbreaknode = class(tbreaknode)
  38. procedure pass_2;override;
  39. end;
  40. tcgcontinuenode = class(tcontinuenode)
  41. procedure pass_2;override;
  42. end;
  43. tcggotonode = class(tgotonode)
  44. procedure pass_2;override;
  45. end;
  46. tcglabelnode = class(tlabelnode)
  47. procedure pass_2;override;
  48. end;
  49. tcgraisenode = class(traisenode)
  50. procedure pass_2;override;
  51. end;
  52. tcgtryexceptnode = class(ttryexceptnode)
  53. procedure pass_2;override;
  54. end;
  55. tcgtryfinallynode = class(ttryfinallynode)
  56. procedure pass_2;override;
  57. end;
  58. tcgonnode = class(tonnode)
  59. procedure pass_2;override;
  60. end;
  61. implementation
  62. uses
  63. verbose,globals,systems,globtype,
  64. symconst,symsym,aasmbase,aasmtai,aasmcpu,defutil,
  65. cginfo,cgbase,pass_2,
  66. cpubase,cpuinfo,
  67. nld,ncon,
  68. ncgutil,
  69. tgobj,rgobj,paramgr,
  70. regvars,cgobj
  71. {$ifndef cpu64bit}
  72. ,cg64f32
  73. {$endif cpu64bit}
  74. ;
  75. const
  76. EXCEPT_BUF_SIZE = 12;
  77. {*****************************************************************************
  78. Second_While_RepeatN
  79. *****************************************************************************}
  80. procedure tcgwhilerepeatnode.pass_2;
  81. var
  82. lcont,lbreak,lloop,
  83. oldclabel,oldblabel : tasmlabel;
  84. otlabel,oflabel : tasmlabel;
  85. oldflowcontrol : tflowcontrol;
  86. begin
  87. location_reset(location,LOC_VOID,OS_NO);
  88. objectlibrary.getlabel(lloop);
  89. objectlibrary.getlabel(lcont);
  90. objectlibrary.getlabel(lbreak);
  91. { arrange continue and breaklabels: }
  92. oldflowcontrol:=flowcontrol;
  93. oldclabel:=aktcontinuelabel;
  94. oldblabel:=aktbreaklabel;
  95. load_all_regvars(exprasmlist);
  96. { handling code at the end as it is much more efficient, and makes
  97. while equal to repeat loop, only the end true/false is swapped (PFV) }
  98. if lnf_testatbegin in loopflags then
  99. cg.a_jmp_always(exprasmlist,lcont);
  100. if not(cs_littlesize in aktglobalswitches) then
  101. { align loop target }
  102. exprasmList.concat(Tai_align.Create(aktalignment.loopalign));
  103. cg.a_label(exprasmlist,lloop);
  104. aktcontinuelabel:=lcont;
  105. aktbreaklabel:=lbreak;
  106. {$ifndef newra}
  107. rg.cleartempgen;
  108. {$endif}
  109. if assigned(right) then
  110. secondpass(right);
  111. load_all_regvars(exprasmlist);
  112. cg.a_label(exprasmlist,lcont);
  113. otlabel:=truelabel;
  114. oflabel:=falselabel;
  115. if lnf_checknegate in loopflags then
  116. begin
  117. truelabel:=lbreak;
  118. falselabel:=lloop;
  119. end
  120. else
  121. begin
  122. truelabel:=lloop;
  123. falselabel:=lbreak;
  124. end;
  125. {$ifndef newra}
  126. rg.cleartempgen;
  127. {$endif}
  128. secondpass(left);
  129. maketojumpbool(exprasmlist,left,lr_load_regvars);
  130. cg.a_label(exprasmlist,lbreak);
  131. truelabel:=otlabel;
  132. falselabel:=oflabel;
  133. aktcontinuelabel:=oldclabel;
  134. aktbreaklabel:=oldblabel;
  135. { a break/continue in a while/repeat block can't be seen outside }
  136. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue]);
  137. end;
  138. {*****************************************************************************
  139. tcgIFNODE
  140. *****************************************************************************}
  141. procedure tcgifnode.pass_2;
  142. var
  143. hl,otlabel,oflabel : tasmlabel;
  144. {$ifdef i386}
  145. org_regvar_loaded_other,
  146. then_regvar_loaded_other,
  147. else_regvar_loaded_other : regvarother_booleanarray;
  148. org_regvar_loaded_int,
  149. then_regvar_loaded_int,
  150. else_regvar_loaded_int : Tsupregset;
  151. org_list,
  152. then_list,
  153. else_list : taasmoutput;
  154. {$endif i386}
  155. begin
  156. location_reset(location,LOC_VOID,OS_NO);
  157. otlabel:=truelabel;
  158. oflabel:=falselabel;
  159. objectlibrary.getlabel(truelabel);
  160. objectlibrary.getlabel(falselabel);
  161. {$ifndef newra}
  162. rg.cleartempgen;
  163. {$endif}
  164. secondpass(left);
  165. {$ifdef i386}
  166. { save regvars loaded in the beginning so that we can restore them }
  167. { when processing the else-block }
  168. if cs_regvars in aktglobalswitches then
  169. begin
  170. org_list := exprasmlist;
  171. exprasmlist := taasmoutput.create;
  172. end;
  173. {$endif i386}
  174. maketojumpbool(exprasmlist,left,lr_dont_load_regvars);
  175. {$ifdef i386}
  176. if cs_regvars in aktglobalswitches then
  177. begin
  178. org_regvar_loaded_int := rg.regvar_loaded_int;
  179. org_regvar_loaded_other := rg.regvar_loaded_other;
  180. end;
  181. {$endif i386}
  182. if assigned(right) then
  183. begin
  184. cg.a_label(exprasmlist,truelabel);
  185. {$ifndef newra}
  186. rg.cleartempgen;
  187. {$endif}
  188. secondpass(right);
  189. end;
  190. {$ifdef i386}
  191. { save current asmlist (previous instructions + then-block) and }
  192. { loaded regvar state and create new clean ones }
  193. if cs_regvars in aktglobalswitches then
  194. begin
  195. then_regvar_loaded_int := rg.regvar_loaded_int;
  196. then_regvar_loaded_other := rg.regvar_loaded_other;
  197. rg.regvar_loaded_int := org_regvar_loaded_int;
  198. rg.regvar_loaded_other := org_regvar_loaded_other;
  199. then_list := exprasmlist;
  200. exprasmlist := taasmoutput.create;
  201. end;
  202. {$endif i386}
  203. if assigned(t1) then
  204. begin
  205. if assigned(right) then
  206. begin
  207. objectlibrary.getlabel(hl);
  208. { do go back to if line !! }
  209. {$ifdef i386}
  210. if not(cs_regvars in aktglobalswitches) then
  211. {$endif i386}
  212. aktfilepos:=exprasmList.getlasttaifilepos^
  213. {$ifdef i386}
  214. else
  215. aktfilepos:=then_list.getlasttaifilepos^
  216. {$endif i386}
  217. ;
  218. cg.a_jmp_always(exprasmlist,hl);
  219. end;
  220. cg.a_label(exprasmlist,falselabel);
  221. {$ifndef newra}
  222. rg.cleartempgen;
  223. {$endif}
  224. secondpass(t1);
  225. {$ifdef i386}
  226. { save current asmlist (previous instructions + else-block) }
  227. { and loaded regvar state and create a new clean list }
  228. if cs_regvars in aktglobalswitches then
  229. begin
  230. else_regvar_loaded_int := rg.regvar_loaded_int;
  231. else_regvar_loaded_other := rg.regvar_loaded_other;
  232. else_list := exprasmlist;
  233. exprasmlist := taasmoutput.create;
  234. end;
  235. {$endif i386}
  236. if assigned(right) then
  237. cg.a_label(exprasmlist,hl);
  238. end
  239. else
  240. begin
  241. {$ifdef i386}
  242. if cs_regvars in aktglobalswitches then
  243. begin
  244. else_regvar_loaded_int := rg.regvar_loaded_int;
  245. else_regvar_loaded_other := rg.regvar_loaded_other;
  246. else_list := exprasmlist;
  247. exprasmlist := taasmoutput.create;
  248. end;
  249. {$endif i386}
  250. cg.a_label(exprasmlist,falselabel);
  251. end;
  252. if not(assigned(right)) then
  253. begin
  254. cg.a_label(exprasmlist,truelabel);
  255. end;
  256. {$ifdef i386}
  257. if cs_regvars in aktglobalswitches then
  258. begin
  259. { add loads of regvars at the end of the then- and else-blocks }
  260. { so that at the end of both blocks the same regvars are loaded }
  261. { no else block? }
  262. if not assigned(t1) then
  263. begin
  264. sync_regvars_int(org_list,then_list,org_regvar_loaded_int,then_regvar_loaded_int);
  265. sync_regvars_other(org_list,then_list,org_regvar_loaded_other,then_regvar_loaded_other);
  266. end
  267. { no then block? }
  268. else if not assigned(right) then
  269. begin
  270. sync_regvars_int(org_list,else_list,org_regvar_loaded_int,else_regvar_loaded_int);
  271. sync_regvars_other(org_list,else_list,org_regvar_loaded_other,else_regvar_loaded_other);
  272. end
  273. { both else and then blocks }
  274. else
  275. begin
  276. sync_regvars_int(then_list,else_list,then_regvar_loaded_int,else_regvar_loaded_int);
  277. sync_regvars_other(then_list,else_list,then_regvar_loaded_other,else_regvar_loaded_other);
  278. end;
  279. { add all lists together }
  280. org_list.concatlist(then_list);
  281. then_list.free;
  282. org_list.concatlist(else_list);
  283. else_list.free;
  284. org_list.concatlist(exprasmlist);
  285. exprasmlist.free;
  286. exprasmlist := org_list;
  287. end;
  288. {$endif i386}
  289. truelabel:=otlabel;
  290. falselabel:=oflabel;
  291. end;
  292. {*****************************************************************************
  293. SecondFor
  294. *****************************************************************************}
  295. procedure tcgfornode.pass_2;
  296. var
  297. l3,oldclabel,oldblabel : tasmlabel;
  298. temptovalue : boolean;
  299. hs : byte;
  300. temp1 : treference;
  301. hop : topcg;
  302. hcond : topcmp;
  303. opsize : tcgsize;
  304. count_var_is_signed,do_loopvar_at_end : boolean;
  305. cmp_const:Tconstexprint;
  306. oldflowcontrol : tflowcontrol;
  307. begin
  308. location_reset(location,LOC_VOID,OS_NO);
  309. oldflowcontrol:=flowcontrol;
  310. oldclabel:=aktcontinuelabel;
  311. oldblabel:=aktbreaklabel;
  312. objectlibrary.getlabel(aktcontinuelabel);
  313. objectlibrary.getlabel(aktbreaklabel);
  314. objectlibrary.getlabel(l3);
  315. { only calculate reference }
  316. {$ifndef newra}
  317. rg.cleartempgen;
  318. {$endif}
  319. secondpass(t2);
  320. hs := t2.resulttype.def.size;
  321. opsize := def_cgsize(t2.resulttype.def);
  322. { first set the to value
  323. because the count var can be in the expression !! }
  324. {$ifndef newra}
  325. rg.cleartempgen;
  326. {$endif}
  327. do_loopvar_at_end:=lnf_dont_mind_loopvar_on_exit in loopflags;
  328. secondpass(right);
  329. { calculate pointer value and check if changeable and if so }
  330. { load into temporary variable }
  331. if right.nodetype<>ordconstn then
  332. begin
  333. do_loopvar_at_end:=false;
  334. tg.GetTemp(exprasmlist,hs,tt_normal,temp1);
  335. temptovalue:=true;
  336. if (right.location.loc=LOC_REGISTER) or
  337. (right.location.loc=LOC_CREGISTER) then
  338. begin
  339. cg.a_load_reg_ref(exprasmlist,opsize,opsize,right.location.register,temp1);
  340. rg.ungetregisterint(exprasmlist,right.location.register);
  341. end
  342. else
  343. cg.g_concatcopy(exprasmlist,right.location.reference,temp1,
  344. hs,true,false);
  345. end
  346. else
  347. temptovalue:=false;
  348. { produce start assignment }
  349. {$ifndef newra}
  350. rg.cleartempgen;
  351. {$endif}
  352. secondpass(left);
  353. count_var_is_signed:=is_signed(t2.resulttype.def);
  354. if lnf_backward in loopflags then
  355. if count_var_is_signed then
  356. hcond:=OC_LT
  357. else
  358. hcond:=OC_B
  359. else
  360. if count_var_is_signed then
  361. hcond:=OC_GT
  362. else
  363. hcond:=OC_A;
  364. load_all_regvars(exprasmlist);
  365. if temptovalue then
  366. begin
  367. cg.a_cmp_ref_loc_label(exprasmlist,opsize,hcond,
  368. temp1,t2.location,aktbreaklabel);
  369. end
  370. else
  371. begin
  372. if lnf_testatbegin in loopflags then
  373. begin
  374. cg.a_cmp_const_loc_label(exprasmlist,opsize,hcond,
  375. aword(tordconstnode(right).value),
  376. t2.location,aktbreaklabel);
  377. end;
  378. end;
  379. {If the loopvar doesn't mind on exit, we avoid this ugly
  380. dec instruction and do the loopvar inc/dec after the loop
  381. body.}
  382. if not do_loopvar_at_end then
  383. begin
  384. if lnf_backward in loopflags then
  385. hop:=OP_ADD
  386. else
  387. hop:=OP_SUB;
  388. cg.a_op_const_loc(exprasmlist,hop,1,t2.location);
  389. end;
  390. if not(cs_littlesize in aktglobalswitches) then
  391. { align loop target }
  392. exprasmList.concat(Tai_align.Create(aktalignment.loopalign));
  393. cg.a_label(exprasmlist,l3);
  394. {If the loopvar doesn't mind on exit, we avoid the loopvar inc/dec
  395. after the loop body instead of here.}
  396. if not do_loopvar_at_end then
  397. begin
  398. { according to count direction DEC or INC... }
  399. if lnf_backward in loopflags then
  400. hop:=OP_SUB
  401. else
  402. hop:=OP_ADD;
  403. cg.a_op_const_loc(exprasmlist,hop,1,t2.location);
  404. end;
  405. { help register must not be in instruction block }
  406. {$ifndef newra}
  407. rg.cleartempgen;
  408. {$endif}
  409. if assigned(t1) then
  410. begin
  411. secondpass(t1);
  412. load_all_regvars(exprasmlist);
  413. end;
  414. {If the loopvar doesn't mind on exit, we do the loopvar inc/dec
  415. after the loop body instead of here.}
  416. if do_loopvar_at_end then
  417. begin
  418. { according to count direction DEC or INC... }
  419. if lnf_backward in loopflags then
  420. hop:=OP_SUB
  421. else
  422. hop:=OP_ADD;
  423. cg.a_op_const_loc(exprasmlist,hop,1,t2.location);
  424. end;
  425. cg.a_label(exprasmlist,aktcontinuelabel);
  426. { makes no problems there }
  427. {$ifndef newra}
  428. rg.cleartempgen;
  429. {$endif}
  430. if do_loopvar_at_end then
  431. if lnf_backward in loopflags then
  432. if count_var_is_signed then
  433. hcond:=OC_GTE
  434. else
  435. hcond:=OC_AE
  436. else
  437. if count_var_is_signed then
  438. hcond:=OC_LTE
  439. else
  440. hcond:=OC_BE
  441. else
  442. if lnf_backward in loopflags then
  443. if count_var_is_signed then
  444. hcond:=OC_GT
  445. else
  446. hcond:=OC_A
  447. else
  448. if count_var_is_signed then
  449. hcond:=OC_LT
  450. else
  451. hcond:=OC_B;
  452. load_all_regvars(exprasmlist);
  453. cmp_const:=aword(Tordconstnode(right).value);
  454. if do_loopvar_at_end then
  455. begin
  456. {Watch out for wrap around 255 -> 0.}
  457. {Ugly: This code is way to long... Use tables?}
  458. case opsize of
  459. OS_8:
  460. begin
  461. if lnf_backward in loopflags then
  462. begin
  463. if byte(cmp_const)=low(byte) then
  464. begin
  465. hcond:=OC_NE;
  466. cmp_const:=high(byte);
  467. end
  468. end
  469. else
  470. begin
  471. if byte(cmp_const)=high(byte) then
  472. begin
  473. hcond:=OC_NE;
  474. cmp_const:=low(byte);
  475. end
  476. end
  477. end;
  478. OS_16:
  479. begin
  480. if lnf_backward in loopflags then
  481. begin
  482. if word(cmp_const)=high(word) then
  483. begin
  484. hcond:=OC_NE;
  485. cmp_const:=low(word);
  486. end
  487. end
  488. else
  489. begin
  490. if word(cmp_const)=low(word) then
  491. begin
  492. hcond:=OC_NE;
  493. cmp_const:=high(word);
  494. end
  495. end
  496. end;
  497. OS_32:
  498. begin
  499. if lnf_backward in loopflags then
  500. begin
  501. if cardinal(cmp_const)=high(cardinal) then
  502. begin
  503. hcond:=OC_NE;
  504. cmp_const:=low(cardinal);
  505. end
  506. end
  507. else
  508. begin
  509. if cardinal(cmp_const)=low(cardinal) then
  510. begin
  511. hcond:=OC_NE;
  512. cmp_const:=high(cardinal);
  513. end
  514. end
  515. end;
  516. OS_64:
  517. begin
  518. if lnf_backward in loopflags then
  519. begin
  520. if qword(cmp_const)=high(qword) then
  521. begin
  522. hcond:=OC_NE;
  523. cmp_const:=low(qword);
  524. end
  525. end
  526. else
  527. begin
  528. if qword(cmp_const)=low(qword) then
  529. begin
  530. hcond:=OC_NE;
  531. cmp_const:=high(qword);
  532. end
  533. end
  534. end;
  535. OS_S8:
  536. begin
  537. if lnf_backward in loopflags then
  538. begin
  539. if shortint(cmp_const)=low(shortint) then
  540. begin
  541. hcond:=OC_NE;
  542. cmp_const:=high(shortint);
  543. end
  544. end
  545. else
  546. begin
  547. if shortint(cmp_const)=high(shortint) then
  548. begin
  549. hcond:=OC_NE;
  550. cmp_const:=low(shortint);
  551. end
  552. end
  553. end;
  554. OS_S16:
  555. begin
  556. if lnf_backward in loopflags then
  557. begin
  558. if integer(cmp_const)=high(integer) then
  559. begin
  560. hcond:=OC_NE;
  561. cmp_const:=low(integer);
  562. end
  563. end
  564. else
  565. begin
  566. if integer(cmp_const)=low(integer) then
  567. begin
  568. hcond:=OC_NE;
  569. cmp_const:=high(integer);
  570. end
  571. end
  572. end;
  573. OS_S32:
  574. begin
  575. if lnf_backward in loopflags then
  576. begin
  577. if longint(cmp_const)=high(longint) then
  578. begin
  579. hcond:=OC_NE;
  580. cmp_const:=low(longint);
  581. end
  582. end
  583. else
  584. begin
  585. if longint(cmp_const)=low(longint) then
  586. begin
  587. hcond:=OC_NE;
  588. cmp_const:=high(longint);
  589. end
  590. end
  591. end;
  592. OS_S64:
  593. begin
  594. if lnf_backward in loopflags then
  595. begin
  596. if int64(cmp_const)=high(int64) then
  597. begin
  598. hcond:=OC_NE;
  599. cmp_const:=low(int64);
  600. end
  601. end
  602. else
  603. begin
  604. if int64(cmp_const)=low(int64) then
  605. begin
  606. hcond:=OC_NE;
  607. cmp_const:=high(int64);
  608. end
  609. end
  610. end;
  611. else
  612. internalerror(200201021);
  613. end;
  614. end;
  615. { produce comparison and the corresponding }
  616. { jump }
  617. if temptovalue then
  618. begin
  619. cg.a_cmp_ref_loc_label(exprasmlist,opsize,hcond,temp1,
  620. t2.location,l3);
  621. end
  622. else
  623. begin
  624. cg.a_cmp_const_loc_label(exprasmlist,opsize,hcond,
  625. cmp_const,t2.location,l3);
  626. end;
  627. if temptovalue then
  628. tg.ungetiftemp(exprasmlist,temp1);
  629. { this is the break label: }
  630. cg.a_label(exprasmlist,aktbreaklabel);
  631. aktcontinuelabel:=oldclabel;
  632. aktbreaklabel:=oldblabel;
  633. { a break/continue in a while/repeat block can't be seen outside }
  634. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue]);
  635. end;
  636. {*****************************************************************************
  637. SecondExitN
  638. *****************************************************************************}
  639. procedure tcgexitnode.pass_2;
  640. begin
  641. location_reset(location,LOC_VOID,OS_NO);
  642. include(flowcontrol,fc_exit);
  643. if assigned(left) then
  644. secondpass(left);
  645. cg.a_jmp_always(exprasmlist,current_procinfo.aktexitlabel);
  646. end;
  647. {*****************************************************************************
  648. SecondBreakN
  649. *****************************************************************************}
  650. procedure tcgbreaknode.pass_2;
  651. begin
  652. location_reset(location,LOC_VOID,OS_NO);
  653. include(flowcontrol,fc_break);
  654. if aktbreaklabel<>nil then
  655. begin
  656. load_all_regvars(exprasmlist);
  657. cg.a_jmp_always(exprasmlist,aktbreaklabel)
  658. end
  659. else
  660. CGMessage(cg_e_break_not_allowed);
  661. end;
  662. {*****************************************************************************
  663. SecondContinueN
  664. *****************************************************************************}
  665. procedure tcgcontinuenode.pass_2;
  666. begin
  667. location_reset(location,LOC_VOID,OS_NO);
  668. include(flowcontrol,fc_continue);
  669. if aktcontinuelabel<>nil then
  670. begin
  671. load_all_regvars(exprasmlist);
  672. cg.a_jmp_always(exprasmlist,aktcontinuelabel)
  673. end
  674. else
  675. CGMessage(cg_e_continue_not_allowed);
  676. end;
  677. {*****************************************************************************
  678. SecondGoto
  679. *****************************************************************************}
  680. procedure tcggotonode.pass_2;
  681. begin
  682. location_reset(location,LOC_VOID,OS_NO);
  683. load_all_regvars(exprasmlist);
  684. cg.a_jmp_always(exprasmlist,labsym.lab)
  685. end;
  686. {*****************************************************************************
  687. SecondLabel
  688. *****************************************************************************}
  689. procedure tcglabelnode.pass_2;
  690. begin
  691. location_reset(location,LOC_VOID,OS_NO);
  692. load_all_regvars(exprasmlist);
  693. cg.a_label(exprasmlist,labelnr);
  694. {$ifndef newra}
  695. rg.cleartempgen;
  696. {$endif newra}
  697. secondpass(left);
  698. end;
  699. {*****************************************************************************
  700. SecondRaise
  701. *****************************************************************************}
  702. procedure tcgraisenode.pass_2;
  703. var
  704. a : tasmlabel;
  705. href2: treference;
  706. r:Tregister;
  707. begin
  708. location_reset(location,LOC_VOID,OS_NO);
  709. if assigned(left) then
  710. begin
  711. {$ifdef callparatemp}
  712. { process object (may contain a call) }
  713. secondpass(left);
  714. if codegenerror then
  715. exit;
  716. {$endif callparatemp}
  717. { multiple parameters? }
  718. if assigned(right) then
  719. begin
  720. {$ifdef callparatemp}
  721. { process address (in case it contains a call) }
  722. secondpass(right);
  723. if codegenerror then
  724. exit;
  725. {$endif callparatemp}
  726. { push frame }
  727. if assigned(frametree) then
  728. begin
  729. secondpass(frametree);
  730. if codegenerror then
  731. exit;
  732. cg.a_param_loc(exprasmlist,frametree.location,paramanager.getintparaloc(exprasmlist,3));
  733. end
  734. else
  735. cg.a_param_const(exprasmlist,OS_INT,0,paramanager.getintparaloc(exprasmlist,3));
  736. {$ifndef callparatemp}
  737. { push address }
  738. secondpass(right);
  739. if codegenerror then
  740. exit;
  741. {$endif not callparatemp}
  742. cg.a_param_loc(exprasmlist,right.location,paramanager.getintparaloc(exprasmlist,2));
  743. end
  744. else
  745. begin
  746. { get current address }
  747. objectlibrary.getaddrlabel(a);
  748. cg.a_label(exprasmlist,a);
  749. reference_reset_symbol(href2,a,0);
  750. { push current frame }
  751. r.enum:=R_INTREGISTER;
  752. r.number:=NR_FRAME_POINTER_REG;
  753. cg.a_param_reg(exprasmlist,OS_ADDR,r,paramanager.getintparaloc(exprasmlist,3));
  754. { push current address }
  755. cg.a_paramaddr_ref(exprasmlist,href2,paramanager.getintparaloc(exprasmlist,2));
  756. end;
  757. {$ifndef callparatemp}
  758. { push object }
  759. secondpass(left);
  760. if codegenerror then
  761. exit;
  762. {$endif not callparatemp}
  763. cg.a_param_loc(exprasmlist,left.location,paramanager.getintparaloc(exprasmlist,1));
  764. paramanager.freeintparaloc(exprasmlist,3);
  765. paramanager.freeintparaloc(exprasmlist,2);
  766. paramanager.freeintparaloc(exprasmlist,1);
  767. {$ifdef newra}
  768. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  769. {$endif newra}
  770. cg.a_call_name(exprasmlist,'FPC_RAISEEXCEPTION');
  771. {$ifdef newra}
  772. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  773. {$endif newra}
  774. end
  775. else
  776. begin
  777. {$ifdef newra}
  778. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  779. {$endif newra}
  780. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  781. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  782. {$ifdef newra}
  783. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  784. {$endif newra}
  785. end;
  786. end;
  787. {*****************************************************************************
  788. SecondTryExcept
  789. *****************************************************************************}
  790. var
  791. endexceptlabel : tasmlabel;
  792. procedure try_new_exception(list : taasmoutput;var jmpbuf,envbuf, href : treference;
  793. a : aword; exceptlabel : tasmlabel);
  794. begin
  795. tg.GetTemp(list,EXCEPT_BUF_SIZE,tt_persistent,envbuf);
  796. tg.GetTemp(list,JMP_BUF_SIZE,tt_persistent,jmpbuf);
  797. tg.GetTemp(list,sizeof(aword),tt_persistent,href);
  798. new_exception(list, jmpbuf,envbuf, href, a, exceptlabel);
  799. end;
  800. procedure try_free_exception(list : taasmoutput;var jmpbuf, envbuf, href : treference;
  801. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  802. begin
  803. free_exception(list, jmpbuf, envbuf, href, a, endexceptlabel, onlyfree);
  804. tg.ungettemp(list,href);
  805. tg.Ungettemp(list,jmpbuf);
  806. tg.ungettemp(list,envbuf);
  807. end;
  808. { does the necessary things to clean up the object stack }
  809. { in the except block }
  810. procedure cleanupobjectstack;
  811. var r:Tregister;
  812. begin
  813. {$ifdef newra}
  814. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  815. {$endif newra}
  816. cg.a_call_name(exprasmlist,'FPC_POPOBJECTSTACK');
  817. {$ifdef newra}
  818. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  819. {$endif newra}
  820. r.enum:=R_INTREGISTER;
  821. r.number:=NR_FUNCTION_RESULT_REG;
  822. cg.a_param_reg(exprasmlist,OS_ADDR,r,paramanager.getintparaloc(exprasmlist,1));
  823. paramanager.freeintparaloc(exprasmlist,1);
  824. {$ifdef newra}
  825. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  826. {$endif newra}
  827. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  828. {$ifdef newra}
  829. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  830. {$endif newra}
  831. end;
  832. procedure tcgtryexceptnode.pass_2;
  833. var
  834. exceptlabel,doexceptlabel,oldendexceptlabel,
  835. lastonlabel,
  836. exitexceptlabel,
  837. continueexceptlabel,
  838. breakexceptlabel,
  839. exittrylabel,
  840. continuetrylabel,
  841. breaktrylabel,
  842. doobjectdestroy,
  843. doobjectdestroyandreraise,
  844. oldaktexitlabel,
  845. oldaktcontinuelabel,
  846. oldaktbreaklabel : tasmlabel;
  847. oldflowcontrol,tryflowcontrol,
  848. exceptflowcontrol : tflowcontrol;
  849. tempbuf,tempaddr : treference;
  850. href : treference;
  851. r:Tregister;
  852. label
  853. errorexit;
  854. begin
  855. location_reset(location,LOC_VOID,OS_NO);
  856. oldflowcontrol:=flowcontrol;
  857. flowcontrol:=[];
  858. { this can be called recursivly }
  859. oldendexceptlabel:=endexceptlabel;
  860. { save the old labels for control flow statements }
  861. oldaktexitlabel:=current_procinfo.aktexitlabel;
  862. if assigned(aktbreaklabel) then
  863. begin
  864. oldaktcontinuelabel:=aktcontinuelabel;
  865. oldaktbreaklabel:=aktbreaklabel;
  866. end;
  867. { get new labels for the control flow statements }
  868. objectlibrary.getlabel(exittrylabel);
  869. objectlibrary.getlabel(exitexceptlabel);
  870. if assigned(aktbreaklabel) then
  871. begin
  872. objectlibrary.getlabel(breaktrylabel);
  873. objectlibrary.getlabel(continuetrylabel);
  874. objectlibrary.getlabel(breakexceptlabel);
  875. objectlibrary.getlabel(continueexceptlabel);
  876. end;
  877. objectlibrary.getlabel(exceptlabel);
  878. objectlibrary.getlabel(doexceptlabel);
  879. objectlibrary.getlabel(endexceptlabel);
  880. objectlibrary.getlabel(lastonlabel);
  881. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,exceptlabel);
  882. { try block }
  883. { set control flow labels for the try block }
  884. current_procinfo.aktexitlabel:=exittrylabel;
  885. if assigned(oldaktbreaklabel) then
  886. begin
  887. aktcontinuelabel:=continuetrylabel;
  888. aktbreaklabel:=breaktrylabel;
  889. end;
  890. flowcontrol:=[];
  891. secondpass(left);
  892. tryflowcontrol:=flowcontrol;
  893. if codegenerror then
  894. goto errorexit;
  895. cg.a_label(exprasmlist,exceptlabel);
  896. try_free_exception(exprasmlist,tempbuf,tempaddr,href,0,endexceptlabel,false);
  897. cg.a_label(exprasmlist,doexceptlabel);
  898. { set control flow labels for the except block }
  899. { and the on statements }
  900. current_procinfo.aktexitlabel:=exitexceptlabel;
  901. if assigned(oldaktbreaklabel) then
  902. begin
  903. aktcontinuelabel:=continueexceptlabel;
  904. aktbreaklabel:=breakexceptlabel;
  905. end;
  906. flowcontrol:=[];
  907. { on statements }
  908. if assigned(right) then
  909. secondpass(right);
  910. cg.a_label(exprasmlist,lastonlabel);
  911. { default handling except handling }
  912. if assigned(t1) then
  913. begin
  914. { FPC_CATCHES must be called with
  915. 'default handler' flag (=-1)
  916. }
  917. cg.a_param_const(exprasmlist,OS_ADDR,aword(-1),paramanager.getintparaloc(exprasmlist,1));
  918. paramanager.freeintparaloc(exprasmlist,1);
  919. {$ifdef newra}
  920. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  921. {$endif newra}
  922. cg.a_call_name(exprasmlist,'FPC_CATCHES');
  923. {$ifdef newra}
  924. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  925. {$endif newra}
  926. { the destruction of the exception object must be also }
  927. { guarded by an exception frame }
  928. objectlibrary.getlabel(doobjectdestroy);
  929. objectlibrary.getlabel(doobjectdestroyandreraise);
  930. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,doobjectdestroyandreraise);
  931. { here we don't have to reset flowcontrol }
  932. { the default and on flowcontrols are handled equal }
  933. secondpass(t1);
  934. exceptflowcontrol:=flowcontrol;
  935. cg.a_label(exprasmlist,doobjectdestroyandreraise);
  936. try_free_exception(exprasmlist,tempbuf,tempaddr,href,0,doobjectdestroy,false);
  937. {$ifdef newra}
  938. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  939. {$endif newra}
  940. cg.a_call_name(exprasmlist,'FPC_POPSECONDOBJECTSTACK');
  941. {$ifdef newra}
  942. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  943. {$endif newra}
  944. r.enum:=R_INTREGISTER;
  945. r.number:=NR_FUNCTION_RESULT_REG;
  946. cg.a_param_reg(exprasmlist, OS_ADDR, r, paramanager.getintparaloc(exprasmlist,1));
  947. paramanager.freeintparaloc(exprasmlist,1);
  948. {$ifdef newra}
  949. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  950. {$endif newra}
  951. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  952. {$ifdef newra}
  953. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  954. {$endif newra}
  955. { we don't need to restore esi here because reraise never }
  956. { returns }
  957. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  958. cg.a_label(exprasmlist,doobjectdestroy);
  959. cleanupobjectstack;
  960. cg.a_jmp_always(exprasmlist,endexceptlabel);
  961. end
  962. else
  963. begin
  964. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  965. exceptflowcontrol:=flowcontrol;
  966. end;
  967. if fc_exit in exceptflowcontrol then
  968. begin
  969. { do some magic for exit in the try block }
  970. cg.a_label(exprasmlist,exitexceptlabel);
  971. { we must also destroy the address frame which guards }
  972. { exception object }
  973. {$ifdef newra}
  974. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  975. {$endif newra}
  976. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  977. {$ifdef newra}
  978. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  979. {$endif newra}
  980. cg.g_exception_reason_load(exprasmlist,href);
  981. cleanupobjectstack;
  982. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  983. end;
  984. if fc_break in exceptflowcontrol then
  985. begin
  986. cg.a_label(exprasmlist,breakexceptlabel);
  987. { we must also destroy the address frame which guards }
  988. { exception object }
  989. {$ifdef newra}
  990. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  991. {$endif newra}
  992. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  993. {$ifdef newra}
  994. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  995. {$endif newra}
  996. cg.g_exception_reason_load(exprasmlist,href);
  997. cleanupobjectstack;
  998. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  999. end;
  1000. if fc_continue in exceptflowcontrol then
  1001. begin
  1002. cg.a_label(exprasmlist,continueexceptlabel);
  1003. { we must also destroy the address frame which guards }
  1004. { exception object }
  1005. {$ifdef newra}
  1006. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1007. {$endif newra}
  1008. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1009. {$ifdef newra}
  1010. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1011. {$endif newra}
  1012. cg.g_exception_reason_load(exprasmlist,href);
  1013. cleanupobjectstack;
  1014. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  1015. end;
  1016. if fc_exit in tryflowcontrol then
  1017. begin
  1018. { do some magic for exit in the try block }
  1019. cg.a_label(exprasmlist,exittrylabel);
  1020. {$ifdef newra}
  1021. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1022. {$endif newra}
  1023. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1024. {$ifdef newra}
  1025. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1026. {$endif newra}
  1027. cg.g_exception_reason_load(exprasmlist,href);
  1028. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  1029. end;
  1030. if fc_break in tryflowcontrol then
  1031. begin
  1032. cg.a_label(exprasmlist,breaktrylabel);
  1033. {$ifdef newra}
  1034. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1035. {$endif newra}
  1036. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1037. {$ifdef newra}
  1038. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1039. {$endif newra}
  1040. cg.g_exception_reason_load(exprasmlist,href);
  1041. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  1042. end;
  1043. if fc_continue in tryflowcontrol then
  1044. begin
  1045. cg.a_label(exprasmlist,continuetrylabel);
  1046. {$ifdef newra}
  1047. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1048. {$endif newra}
  1049. cg.a_call_name(exprasmlist,'FPC_POPADDRSTACK');
  1050. {$ifdef newra}
  1051. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1052. {$endif newra}
  1053. cg.g_exception_reason_load(exprasmlist,href);
  1054. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  1055. end;
  1056. cg.a_label(exprasmlist,endexceptlabel);
  1057. errorexit:
  1058. { restore all saved labels }
  1059. endexceptlabel:=oldendexceptlabel;
  1060. { restore the control flow labels }
  1061. current_procinfo.aktexitlabel:=oldaktexitlabel;
  1062. if assigned(oldaktbreaklabel) then
  1063. begin
  1064. aktcontinuelabel:=oldaktcontinuelabel;
  1065. aktbreaklabel:=oldaktbreaklabel;
  1066. end;
  1067. { return all used control flow statements }
  1068. flowcontrol:=oldflowcontrol+exceptflowcontrol+
  1069. tryflowcontrol;
  1070. end;
  1071. procedure tcgonnode.pass_2;
  1072. var
  1073. nextonlabel,
  1074. exitonlabel,
  1075. continueonlabel,
  1076. breakonlabel,
  1077. oldaktexitlabel,
  1078. oldaktcontinuelabel,
  1079. doobjectdestroyandreraise,
  1080. doobjectdestroy,
  1081. oldaktbreaklabel : tasmlabel;
  1082. ref : treference;
  1083. oldflowcontrol : tflowcontrol;
  1084. tempbuf,tempaddr : treference;
  1085. href : treference;
  1086. href2: treference;
  1087. r:Tregister;
  1088. begin
  1089. location_reset(location,LOC_VOID,OS_NO);
  1090. oldflowcontrol:=flowcontrol;
  1091. flowcontrol:=[];
  1092. objectlibrary.getlabel(nextonlabel);
  1093. { send the vmt parameter }
  1094. reference_reset_symbol(href2,objectlibrary.newasmsymboldata(excepttype.vmt_mangledname),0);
  1095. cg.a_paramaddr_ref(exprasmlist,href2,paramanager.getintparaloc(exprasmlist,1));
  1096. paramanager.freeintparaloc(exprasmlist,1);
  1097. {$ifdef newra}
  1098. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1099. {$endif newra}
  1100. cg.a_call_name(exprasmlist,'FPC_CATCHES');
  1101. {$ifdef newra}
  1102. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1103. {$endif newra}
  1104. { is it this catch? No. go to next onlabel }
  1105. r.enum:=R_INTREGISTER;
  1106. r.number:=NR_FUNCTION_RESULT_REG;
  1107. cg.a_cmp_const_reg_label(exprasmlist,OS_ADDR,OC_EQ,0,r,nextonlabel);
  1108. ref.symbol:=nil;
  1109. tg.GetTemp(exprasmlist,pointer_size,tt_normal,ref);
  1110. { what a hack ! }
  1111. if assigned(exceptsymtable) then
  1112. tvarsym(exceptsymtable.symindex.first).address:=ref.offset;
  1113. cg.a_load_reg_ref(exprasmlist,OS_ADDR,OS_ADDR,r,ref);
  1114. { in the case that another exception is risen }
  1115. { we've to destroy the old one }
  1116. objectlibrary.getlabel(doobjectdestroyandreraise);
  1117. { call setjmp, and jump to finally label on non-zero result }
  1118. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,doobjectdestroyandreraise);
  1119. if assigned(right) then
  1120. begin
  1121. oldaktexitlabel:=current_procinfo.aktexitlabel;
  1122. objectlibrary.getlabel(exitonlabel);
  1123. current_procinfo.aktexitlabel:=exitonlabel;
  1124. if assigned(aktbreaklabel) then
  1125. begin
  1126. oldaktcontinuelabel:=aktcontinuelabel;
  1127. oldaktbreaklabel:=aktbreaklabel;
  1128. objectlibrary.getlabel(breakonlabel);
  1129. objectlibrary.getlabel(continueonlabel);
  1130. aktcontinuelabel:=continueonlabel;
  1131. aktbreaklabel:=breakonlabel;
  1132. end;
  1133. secondpass(right);
  1134. end;
  1135. objectlibrary.getlabel(doobjectdestroy);
  1136. cg.a_label(exprasmlist,doobjectdestroyandreraise);
  1137. try_free_exception(exprasmlist,tempbuf,tempaddr,href,0,doobjectdestroy,false);
  1138. {$ifdef newra}
  1139. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1140. {$endif newra}
  1141. cg.a_call_name(exprasmlist,'FPC_POPSECONDOBJECTSTACK');
  1142. {$ifdef newra}
  1143. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1144. {$endif newra}
  1145. cg.a_param_reg(exprasmlist, OS_ADDR, r, paramanager.getintparaloc(exprasmlist,1));
  1146. paramanager.freeintparaloc(exprasmlist,1);
  1147. {$ifdef newra}
  1148. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1149. {$endif newra}
  1150. cg.a_call_name(exprasmlist,'FPC_DESTROYEXCEPTION');
  1151. {$ifdef newra}
  1152. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  1153. {$endif newra}
  1154. { we don't need to restore esi here because reraise never }
  1155. { returns }
  1156. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1157. cg.a_label(exprasmlist,doobjectdestroy);
  1158. cleanupobjectstack;
  1159. { clear some stuff }
  1160. tg.ungetiftemp(exprasmlist,ref);
  1161. cg.a_jmp_always(exprasmlist,endexceptlabel);
  1162. if assigned(right) then
  1163. begin
  1164. { special handling for control flow instructions }
  1165. if fc_exit in flowcontrol then
  1166. begin
  1167. { the address and object pop does secondtryexcept }
  1168. cg.a_label(exprasmlist,exitonlabel);
  1169. cg.a_jmp_always(exprasmlist,oldaktexitlabel);
  1170. end;
  1171. if fc_break in flowcontrol then
  1172. begin
  1173. { the address and object pop does secondtryexcept }
  1174. cg.a_label(exprasmlist,breakonlabel);
  1175. cg.a_jmp_always(exprasmlist,oldaktbreaklabel);
  1176. end;
  1177. if fc_continue in flowcontrol then
  1178. begin
  1179. { the address and object pop does secondtryexcept }
  1180. cg.a_label(exprasmlist,continueonlabel);
  1181. cg.a_jmp_always(exprasmlist,oldaktcontinuelabel);
  1182. end;
  1183. current_procinfo.aktexitlabel:=oldaktexitlabel;
  1184. if assigned(oldaktbreaklabel) then
  1185. begin
  1186. aktcontinuelabel:=oldaktcontinuelabel;
  1187. aktbreaklabel:=oldaktbreaklabel;
  1188. end;
  1189. end;
  1190. cg.a_label(exprasmlist,nextonlabel);
  1191. flowcontrol:=oldflowcontrol+flowcontrol;
  1192. { next on node }
  1193. if assigned(left) then
  1194. begin
  1195. {$ifndef newra}
  1196. rg.cleartempgen;
  1197. {$endif newra}
  1198. secondpass(left);
  1199. end;
  1200. end;
  1201. {*****************************************************************************
  1202. SecondTryFinally
  1203. *****************************************************************************}
  1204. procedure tcgtryfinallynode.pass_2;
  1205. var
  1206. reraiselabel,
  1207. finallylabel,
  1208. endfinallylabel,
  1209. exitfinallylabel,
  1210. continuefinallylabel,
  1211. breakfinallylabel,
  1212. oldaktexitlabel,
  1213. oldaktcontinuelabel,
  1214. oldaktbreaklabel : tasmlabel;
  1215. oldflowcontrol,tryflowcontrol : tflowcontrol;
  1216. decconst : longint;
  1217. tempbuf,tempaddr : treference;
  1218. href : treference;
  1219. r:Tregister;
  1220. begin
  1221. location_reset(location,LOC_VOID,OS_NO);
  1222. { check if child nodes do a break/continue/exit }
  1223. oldflowcontrol:=flowcontrol;
  1224. flowcontrol:=[];
  1225. objectlibrary.getlabel(finallylabel);
  1226. objectlibrary.getlabel(endfinallylabel);
  1227. objectlibrary.getlabel(reraiselabel);
  1228. { the finally block must catch break, continue and exit }
  1229. { statements }
  1230. oldaktexitlabel:=current_procinfo.aktexitlabel;
  1231. if implicitframe then
  1232. exitfinallylabel:=finallylabel
  1233. else
  1234. objectlibrary.getlabel(exitfinallylabel);
  1235. current_procinfo.aktexitlabel:=exitfinallylabel;
  1236. if assigned(aktbreaklabel) then
  1237. begin
  1238. oldaktcontinuelabel:=aktcontinuelabel;
  1239. oldaktbreaklabel:=aktbreaklabel;
  1240. if implicitframe then
  1241. begin
  1242. breakfinallylabel:=finallylabel;
  1243. continuefinallylabel:=finallylabel;
  1244. end
  1245. else
  1246. begin
  1247. objectlibrary.getlabel(breakfinallylabel);
  1248. objectlibrary.getlabel(continuefinallylabel);
  1249. end;
  1250. aktcontinuelabel:=continuefinallylabel;
  1251. aktbreaklabel:=breakfinallylabel;
  1252. end;
  1253. { call setjmp, and jump to finally label on non-zero result }
  1254. try_new_exception(exprasmlist,tempbuf,tempaddr,href,1,finallylabel);
  1255. { try code }
  1256. if assigned(left) then
  1257. begin
  1258. secondpass(left);
  1259. tryflowcontrol:=flowcontrol;
  1260. if codegenerror then
  1261. exit;
  1262. end;
  1263. cg.a_label(exprasmlist,finallylabel);
  1264. { just free the frame information }
  1265. try_free_exception(exprasmlist,tempbuf,tempaddr,href,1,finallylabel,true);
  1266. { finally code }
  1267. flowcontrol:=[];
  1268. secondpass(right);
  1269. if flowcontrol<>[] then
  1270. CGMessage(cg_e_control_flow_outside_finally);
  1271. if codegenerror then
  1272. exit;
  1273. { the value should now be in the exception handler }
  1274. cg.g_exception_reason_load(exprasmlist,href);
  1275. r.enum:=R_INTREGISTER;
  1276. r.number:=NR_FUNCTION_RESULT_REG;
  1277. if implicitframe then
  1278. begin
  1279. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,endfinallylabel);
  1280. { finally code only needed to be executed on exception }
  1281. flowcontrol:=[];
  1282. secondpass(t1);
  1283. if flowcontrol<>[] then
  1284. CGMessage(cg_e_control_flow_outside_finally);
  1285. if codegenerror then
  1286. exit;
  1287. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1288. end
  1289. else
  1290. begin
  1291. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,endfinallylabel);
  1292. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_32,1,r);
  1293. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,reraiselabel);
  1294. if fc_exit in tryflowcontrol then
  1295. begin
  1296. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_32,1,r);
  1297. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,oldaktexitlabel);
  1298. decconst:=1;
  1299. end
  1300. else
  1301. decconst:=2;
  1302. if fc_break in tryflowcontrol then
  1303. begin
  1304. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_32,decconst,r);
  1305. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,oldaktbreaklabel);
  1306. decconst:=1;
  1307. end
  1308. else
  1309. inc(decconst);
  1310. if fc_continue in tryflowcontrol then
  1311. begin
  1312. cg.a_op_const_reg(exprasmlist,OP_SUB,OS_32,decconst,r);
  1313. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_EQ,0,r,oldaktcontinuelabel);
  1314. end;
  1315. cg.a_label(exprasmlist,reraiselabel);
  1316. cg.a_call_name(exprasmlist,'FPC_RERAISE');
  1317. { do some magic for exit,break,continue in the try block }
  1318. if fc_exit in tryflowcontrol then
  1319. begin
  1320. cg.a_label(exprasmlist,exitfinallylabel);
  1321. cg.g_exception_reason_load(exprasmlist,href);
  1322. cg.g_exception_reason_save_const(exprasmlist,href,2);
  1323. cg.a_jmp_always(exprasmlist,finallylabel);
  1324. end;
  1325. if fc_break in tryflowcontrol then
  1326. begin
  1327. cg.a_label(exprasmlist,breakfinallylabel);
  1328. cg.g_exception_reason_load(exprasmlist,href);
  1329. cg.g_exception_reason_save_const(exprasmlist,href,3);
  1330. cg.a_jmp_always(exprasmlist,finallylabel);
  1331. end;
  1332. if fc_continue in tryflowcontrol then
  1333. begin
  1334. cg.a_label(exprasmlist,continuefinallylabel);
  1335. cg.g_exception_reason_load(exprasmlist,href);
  1336. cg.g_exception_reason_save_const(exprasmlist,href,4);
  1337. cg.a_jmp_always(exprasmlist,finallylabel);
  1338. end;
  1339. end;
  1340. cg.a_label(exprasmlist,endfinallylabel);
  1341. current_procinfo.aktexitlabel:=oldaktexitlabel;
  1342. if assigned(aktbreaklabel) then
  1343. begin
  1344. aktcontinuelabel:=oldaktcontinuelabel;
  1345. aktbreaklabel:=oldaktbreaklabel;
  1346. end;
  1347. flowcontrol:=oldflowcontrol+tryflowcontrol;
  1348. end;
  1349. begin
  1350. cwhilerepeatnode:=tcgwhilerepeatnode;
  1351. cifnode:=tcgifnode;
  1352. cfornode:=tcgfornode;
  1353. cexitnode:=tcgexitnode;
  1354. cbreaknode:=tcgbreaknode;
  1355. ccontinuenode:=tcgcontinuenode;
  1356. cgotonode:=tcggotonode;
  1357. clabelnode:=tcglabelnode;
  1358. craisenode:=tcgraisenode;
  1359. ctryexceptnode:=tcgtryexceptnode;
  1360. ctryfinallynode:=tcgtryfinallynode;
  1361. connode:=tcgonnode;
  1362. end.
  1363. {
  1364. $Log$
  1365. Revision 1.75 2003-08-10 17:25:23 peter
  1366. * fixed some reported bugs
  1367. Revision 1.74 2003/08/09 18:56:54 daniel
  1368. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1369. allocator
  1370. * Some preventive changes to i386 spillinh code
  1371. Revision 1.73 2003/07/23 11:01:14 jonas
  1372. * several rg.allocexplicitregistersint/rg.deallocexplicitregistersint
  1373. pairs round calls to helpers
  1374. Revision 1.72 2003/06/13 21:19:30 peter
  1375. * current_procdef removed, use current_procinfo.procdef instead
  1376. Revision 1.71 2003/06/09 14:38:52 jonas
  1377. * fixed for callparatemp
  1378. Revision 1.70 2003/06/09 12:23:30 peter
  1379. * init/final of procedure data splitted from genentrycode
  1380. * use asmnode getposition to insert final at the correct position
  1381. als for the implicit try...finally
  1382. Revision 1.69 2003/06/07 18:57:04 jonas
  1383. + added freeintparaloc
  1384. * ppc get/freeintparaloc now check whether the parameter regs are
  1385. properly allocated/deallocated (and get an extra list para)
  1386. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1387. * fixed lot of missing pi_do_call's
  1388. Revision 1.68 2003/06/03 21:11:09 peter
  1389. * cg.a_load_* get a from and to size specifier
  1390. * makeregsize only accepts newregister
  1391. * i386 uses generic tcgnotnode,tcgunaryminus
  1392. Revision 1.67 2003/06/01 21:38:06 peter
  1393. * getregisterfpu size parameter added
  1394. * op_const_reg size parameter added
  1395. * sparc updates
  1396. Revision 1.66 2003/05/30 23:57:08 peter
  1397. * more sparc cleanup
  1398. * accumulator removed, splitted in function_return_reg (called) and
  1399. function_result_reg (caller)
  1400. Revision 1.65 2003/05/30 18:55:21 jonas
  1401. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  1402. works for ppc
  1403. Revision 1.64 2003/05/26 21:17:17 peter
  1404. * procinlinenode removed
  1405. * aktexit2label removed, fast exit removed
  1406. + tcallnode.inlined_pass_2 added
  1407. Revision 1.63 2003/05/23 14:27:35 peter
  1408. * remove some unit dependencies
  1409. * current_procinfo changes to store more info
  1410. Revision 1.62 2003/05/17 13:30:08 jonas
  1411. * changed tt_persistant to tt_persistent :)
  1412. * tempcreatenode now doesn't accept a boolean anymore for persistent
  1413. temps, but a ttemptype, so you can also create ansistring temps etc
  1414. Revision 1.61 2003/05/16 14:33:31 peter
  1415. * regvar fixes
  1416. Revision 1.60 2003/05/13 19:14:41 peter
  1417. * failn removed
  1418. * inherited result code check moven to pexpr
  1419. Revision 1.59 2003/05/11 21:37:03 peter
  1420. * moved implicit exception frame from ncgutil to psub
  1421. * constructor/destructor helpers moved from cobj/ncgutil to psub
  1422. Revision 1.58 2003/04/30 15:45:35 florian
  1423. * merged more x86-64/i386 code
  1424. Revision 1.57 2003/04/29 07:29:14 michael
  1425. + Patch from peter to fix wrong pushing of ansistring function results in open array
  1426. Revision 1.56 2003/04/27 11:21:33 peter
  1427. * aktprocdef renamed to current_procinfo.procdef
  1428. * procinfo renamed to current_procinfo
  1429. * procinfo will now be stored in current_module so it can be
  1430. cleaned up properly
  1431. * gen_main_procsym changed to create_main_proc and release_main_proc
  1432. to also generate a tprocinfo structure
  1433. * fixed unit implicit initfinal
  1434. Revision 1.55 2003/04/22 23:50:22 peter
  1435. * firstpass uses expectloc
  1436. * checks if there are differences between the expectloc and
  1437. location.loc from secondpass in EXTDEBUG
  1438. Revision 1.54 2003/04/17 07:50:24 daniel
  1439. * Some work on interference graph construction
  1440. Revision 1.53 2003/04/06 21:11:23 olle
  1441. * changed newasmsymbol to newasmsymboldata for data symbols
  1442. Revision 1.52 2003/03/28 19:16:56 peter
  1443. * generic constructor working for i386
  1444. * remove fixed self register
  1445. * esi added as address register for i386
  1446. Revision 1.51 2003/02/19 22:00:14 daniel
  1447. * Code generator converted to new register notation
  1448. - Horribily outdated todo.txt removed
  1449. Revision 1.50 2003/02/15 22:17:38 carl
  1450. * bugfix of FPU emulation code
  1451. Revision 1.49 2003/01/08 18:43:56 daniel
  1452. * Tregister changed into a record
  1453. Revision 1.48 2003/01/03 09:51:58 daniel
  1454. * Compiler now cycles with var_notification
  1455. Revision 1.47 2003/01/02 15:29:25 daniel
  1456. * Some debugging on for loop optimization
  1457. Revision 1.46 2002/12/31 09:55:58 daniel
  1458. + Notification implementation complete
  1459. + Add for loop code optimization using notifications
  1460. results in 1.5-1.9% speed improvement in nestloop benchmark
  1461. Optimization incomplete, compiler does not cycle yet with
  1462. notifications enabled.
  1463. Revision 1.45 2002/11/28 11:17:01 florian
  1464. * loop node flags from node flags splitted
  1465. Revision 1.44 2002/11/25 17:43:17 peter
  1466. * splitted defbase in defutil,symutil,defcmp
  1467. * merged isconvertable and is_equal into compare_defs(_ext)
  1468. * made operator search faster by walking the list only once
  1469. Revision 1.43 2002/09/30 07:00:45 florian
  1470. * fixes to common code to get the alpha compiler compiled applied
  1471. Revision 1.42 2002/09/07 15:25:02 peter
  1472. * old logs removed and tabs fixed
  1473. Revision 1.41 2002/09/01 18:47:00 peter
  1474. * assignn check in exitnode changed to use a separate boolean as the
  1475. assignn can be changed to a calln
  1476. Revision 1.40 2002/09/01 14:41:47 peter
  1477. * increase refcount in exit(arg) for arg
  1478. Revision 1.39 2002/08/24 18:41:52 peter
  1479. * fixed wrong label in jump of except block (was also in n386flw wrong)
  1480. * fixed wrong pushing of raise parameters
  1481. * fixed wrong compare in finally
  1482. Revision 1.38 2002/08/23 16:14:48 peter
  1483. * tempgen cleanup
  1484. * tt_noreuse temp type added that will be used in genentrycode
  1485. Revision 1.37 2002/08/19 19:36:43 peter
  1486. * More fixes for cross unit inlining, all tnodes are now implemented
  1487. * Moved pocall_internconst to po_internconst because it is not a
  1488. calling type at all and it conflicted when inlining of these small
  1489. functions was requested
  1490. Revision 1.36 2002/08/15 15:15:55 carl
  1491. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  1492. * more generic nodes for maths
  1493. * several fixes for better m68k support
  1494. Revision 1.35 2002/08/13 18:01:52 carl
  1495. * rename swatoperands to swapoperands
  1496. + m68k first compilable version (still needs a lot of testing):
  1497. assembler generator, system information , inline
  1498. assembler reader.
  1499. Revision 1.34 2002/08/11 14:32:26 peter
  1500. * renamed current_library to objectlibrary
  1501. Revision 1.33 2002/08/11 13:24:11 peter
  1502. * saving of asmsymbols in ppu supported
  1503. * asmsymbollist global is removed and moved into a new class
  1504. tasmlibrarydata that will hold the info of a .a file which
  1505. corresponds with a single module. Added librarydata to tmodule
  1506. to keep the library info stored for the module. In the future the
  1507. objectfiles will also be stored to the tasmlibrarydata class
  1508. * all getlabel/newasmsymbol and friends are moved to the new class
  1509. Revision 1.32 2002/08/09 19:10:59 carl
  1510. * fixed generic exception management
  1511. Revision 1.31 2002/08/04 19:06:41 carl
  1512. + added generic exception support (still does not work!)
  1513. + more documentation
  1514. Revision 1.30 2002/07/27 19:53:51 jonas
  1515. + generic implementation of tcg.g_flags2ref()
  1516. * tcg.flags2xxx() now also needs a size parameter
  1517. Revision 1.29 2002/07/25 17:56:29 carl
  1518. + FPURESULTREG -> FPU_RESULT_REG
  1519. Revision 1.28 2002/07/21 06:58:49 daniel
  1520. * Changed booleans into flags
  1521. Revision 1.27 2002/07/20 12:54:53 daniel
  1522. * Optimized the code generated for for nodes. The shootout/nestloop benchmark
  1523. now runs 5% faster on my computer.
  1524. Revision 1.26 2002/07/20 11:57:54 florian
  1525. * types.pas renamed to defbase.pas because D6 contains a types
  1526. unit so this would conflicts if D6 programms are compiled
  1527. + Willamette/SSE2 instructions to assembler added
  1528. Revision 1.25 2002/07/20 11:15:51 daniel
  1529. * The for node does a check if the first comparision can be skipped. I moved
  1530. the check from the second pass to the resulttype pass. The advantage is
  1531. that the state tracker can now decide to skip the first comparision too.
  1532. Revision 1.24 2002/07/20 08:14:24 daniel
  1533. * Loops should not be aligned when optimizing for size
  1534. Revision 1.23 2002/07/19 11:41:35 daniel
  1535. * State tracker work
  1536. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1537. allows the state tracker to change while nodes automatically into
  1538. repeat nodes.
  1539. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1540. 'not(a>b)' is optimized into 'a<=b'.
  1541. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1542. by removing the notn and later switchting the true and falselabels. The
  1543. same is done with 'repeat until not a'.
  1544. Revision 1.22 2002/07/04 20:43:01 florian
  1545. * first x86-64 patches
  1546. }