cgflw.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate assembler for nodes that influence the flow
  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. { in sync with cg386flw rev 1.55 of the main branch }
  19. unit cg386flw;
  20. interface
  21. uses
  22. tree;
  23. procedure second_while_repeatn(list: paasmoutput; var p : ptree);
  24. procedure secondifn(list: paasmoutput; var p : ptree);
  25. procedure secondfor(list: paasmoutput; var p : ptree);
  26. procedure secondexitn(list: paasmoutput; var p : ptree);
  27. procedure secondbreakn(list: paasmoutput; var p : ptree);
  28. procedure secondcontinuen( list: paasmoutput; var p : ptree);
  29. procedure secondgoto(list: paasmoutput; var p : ptree);
  30. procedure secondlabel(list: paasmoutput; var p : ptree);
  31. procedure secondraise(list: paasmoutput; var p : ptree);
  32. procedure secondtryexcept(list: paasmoutput; var p : ptree);
  33. procedure secondtryfinally(list: paasmoutput; var p : ptree);
  34. procedure secondon(list: paasmoutput; var p : ptree);
  35. procedure secondfail(list: paasmoutput; var p : ptree);
  36. implementation
  37. uses
  38. cobjects,verbose,globtype,globals,systems,
  39. symconst,symtable,aasm,types,
  40. hcodegen,temp_gen,pass_2,
  41. cpubase,cpuasm{,
  42. cgai386,tgeni386}, cgcpu;
  43. {*****************************************************************************
  44. Second_While_RepeatN
  45. *****************************************************************************}
  46. procedure second_while_repeatn(list: paasmoutput; var p : ptree);
  47. { converted, problems left: }
  48. { * maketojumpbool }
  49. var
  50. lcont,lbreak,lloop,
  51. oldclabel,oldblabel : pasmlabel;
  52. otlabel,oflabel : pasmlabel;
  53. begin
  54. getlabel(lloop);
  55. getlabel(lcont);
  56. getlabel(lbreak);
  57. { arrange continue and breaklabels: }
  58. oldclabel:=aktcontinuelabel;
  59. oldblabel:=aktbreaklabel;
  60. { handling code at the end as it is much more efficient, and makes
  61. while equal to repeat loop, only the end true/false is swapped (PFV) }
  62. if p^.treetype=whilen then
  63. a_jmp_cond(list,C_None,lcont);
  64. a_label(list,lloop);
  65. aktcontinuelabel:=lcont;
  66. aktbreaklabel:=lbreak;
  67. cleartempgen;
  68. if assigned(p^.right) then
  69. secondpass(list,p^.right);
  70. a_label(list,lcont);
  71. otlabel:=truelabel;
  72. oflabel:=falselabel;
  73. if p^.treetype=whilen then
  74. begin
  75. truelabel:=lloop;
  76. falselabel:=lbreak;
  77. end
  78. { repeatn }
  79. else
  80. begin
  81. truelabel:=lbreak;
  82. falselabel:=lloop;
  83. end;
  84. cleartempgen;
  85. secondpass(list,p^.left);
  86. { has to be implemented processor dependently !!!! }
  87. maketojumpbool(list, p^.left);
  88. a_label(ist,lbreak);
  89. freelabel(lloop);
  90. freelabel(lcont);
  91. freelabel(lbreak);
  92. truelabel:=otlabel;
  93. falselabel:=oflabel;
  94. aktcontinuelabel:=oldclabel;
  95. aktbreaklabel:=oldblabel;
  96. end;
  97. {*****************************************************************************
  98. SecondIfN
  99. *****************************************************************************}
  100. procedure secondifn(list: paasmoutput; var p : ptree);
  101. { converted, problems left: }
  102. { * maketojumpbool }
  103. var
  104. hl,otlabel,oflabel : pasmlabel;
  105. begin
  106. otlabel:=truelabel;
  107. oflabel:=falselabel;
  108. getlabel(truelabel);
  109. getlabel(falselabel);
  110. cleartempgen;
  111. secondpass(list,p^.left);
  112. maketojumpbool(list,p^.left);
  113. if assigned(p^.right) then
  114. begin
  115. a_label(list, truelabel);
  116. cleartempgen;
  117. secondpass(list,p^.right);
  118. end;
  119. if assigned(p^.t1) then
  120. begin
  121. if assigned(p^.right) then
  122. begin
  123. getlabel(hl);
  124. { do go back to if line !! }
  125. aktfilepos:=list^.getlasttaifilepos^;
  126. a_jmp_cond(list,C_None,hl);
  127. end;
  128. a_label(list,falselabel);
  129. cleartempgen;
  130. secondpass(list,p^.t1);
  131. if assigned(p^.right) then
  132. a_label(list,hl);
  133. end
  134. else
  135. begin
  136. a_label(list,falselabel);
  137. end;
  138. if not(assigned(p^.right)) then
  139. begin
  140. a_label(list,truelabel);
  141. end;
  142. freelabel(truelabel);
  143. freelabel(falselabel);
  144. truelabel:=otlabel;
  145. falselabel:=oflabel;
  146. end;
  147. {*****************************************************************************
  148. SecondFor
  149. *****************************************************************************}
  150. procedure secondfor(list: paasmoutput; var p : ptree);
  151. { still being converted, problems left: }
  152. { * getregister32 }
  153. { * size issues (" p^.t2^.resulttype^.size") }
  154. var
  155. l3,oldclabel,oldblabel : pasmlabel;
  156. omitfirstcomp,temptovalue : boolean;
  157. hs : byte;
  158. temp1 : treference;
  159. hop : tasmop;
  160. hcond : tasmcond;
  161. cmpreg,cmp32 : tregister;
  162. opsize : topsize;
  163. count_var_is_signed : boolean;
  164. begin
  165. oldclabel:=aktcontinuelabel;
  166. oldblabel:=aktbreaklabel;
  167. getlabel(aktcontinuelabel);
  168. getlabel(aktbreaklabel);
  169. getlabel(l3);
  170. { could we spare the first comparison ? }
  171. omitfirstcomp:=false;
  172. if p^.right^.treetype=ordconstn then
  173. if p^.left^.right^.treetype=ordconstn then
  174. omitfirstcomp:=(p^.backward and (p^.left^.right^.value>=p^.right^.value))
  175. or (not(p^.backward) and (p^.left^.right^.value<=p^.right^.value));
  176. { only calculate reference }
  177. cleartempgen;
  178. secondpass(list,p^.t2);
  179. hs:=p^.t2^.resulttype^.size;
  180. cmp32:=getregister32;
  181. case hs of
  182. 1 : begin
  183. opsize:=S_B;
  184. cmpreg:=reg32toreg8(cmp32);
  185. end;
  186. 2 : begin
  187. opsize:=S_W;
  188. cmpreg:=reg32toreg16(cmp32);
  189. end;
  190. 4 : begin
  191. opsize:=S_L;
  192. cmpreg:=cmp32;
  193. end;
  194. end;
  195. { first set the to value
  196. because the count var can be in the expression !! }
  197. cleartempgen;
  198. secondpass(list,p^.right);
  199. { calculate pointer value and check if changeable and if so }
  200. { load into temporary variable }
  201. if p^.right^.treetype<>ordconstn then
  202. begin
  203. temp1.symbol:=nil;
  204. gettempofsizereference(hs,temp1);
  205. temptovalue:=true;
  206. if (p^.right^.location.loc=LOC_REGISTER) or
  207. (p^.right^.location.loc=LOC_CREGISTER) then
  208. begin
  209. emit_reg_ref(A_MOV,opsize,p^.right^.location.register,
  210. newreference(temp1));
  211. end
  212. else
  213. concatcopy(p^.right^.location.reference,temp1,hs,false,false);
  214. end
  215. else
  216. temptovalue:=false;
  217. { produce start assignment }
  218. cleartempgen;
  219. secondpass(p^.left);
  220. count_var_is_signed:=is_signed(porddef(p^.t2^.resulttype));
  221. if temptovalue then
  222. begin
  223. if p^.t2^.location.loc=LOC_CREGISTER then
  224. begin
  225. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  226. p^.t2^.location.register);
  227. end
  228. else
  229. begin
  230. emit_ref_reg(A_MOV,opsize,newreference(p^.t2^.location.reference),
  231. cmpreg);
  232. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  233. cmpreg);
  234. end;
  235. end
  236. else
  237. begin
  238. if not(omitfirstcomp) then
  239. begin
  240. if p^.t2^.location.loc=LOC_CREGISTER then
  241. emit_const_reg(A_CMP,opsize,p^.right^.value,
  242. p^.t2^.location.register)
  243. else
  244. emit_const_ref(A_CMP,opsize,p^.right^.value,
  245. newreference(p^.t2^.location.reference));
  246. end;
  247. end;
  248. if p^.backward then
  249. if count_var_is_signed then
  250. hcond:=C_L
  251. else
  252. hcond:=C_B
  253. else
  254. if count_var_is_signed then
  255. hcond:=C_G
  256. else
  257. hcond:=C_A;
  258. if not(omitfirstcomp) or temptovalue then
  259. emitjmp(hcond,aktbreaklabel);
  260. { align loop target }
  261. if not(cs_littlesize in aktglobalswitches) then
  262. exprasmlist^.concat(new(pai_align,init_op(4,$90)));
  263. emitlab(l3);
  264. { help register must not be in instruction block }
  265. cleartempgen;
  266. if assigned(p^.t1) then
  267. secondpass(p^.t1);
  268. emitlab(aktcontinuelabel);
  269. { makes no problems there }
  270. cleartempgen;
  271. { demand help register again }
  272. cmp32:=getregister32;
  273. case hs of
  274. 1 : begin
  275. opsize:=S_B;
  276. cmpreg:=reg32toreg8(cmp32);
  277. end;
  278. 2 : begin
  279. opsize:=S_W;
  280. cmpreg:=reg32toreg16(cmp32);
  281. end;
  282. 4 : opsize:=S_L;
  283. end;
  284. { produce comparison and the corresponding }
  285. { jump }
  286. if temptovalue then
  287. begin
  288. if p^.t2^.location.loc=LOC_CREGISTER then
  289. begin
  290. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  291. p^.t2^.location.register);
  292. end
  293. else
  294. begin
  295. emit_ref_reg(A_MOV,opsize,newreference(p^.t2^.location.reference),
  296. cmpreg);
  297. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  298. cmpreg);
  299. end;
  300. end
  301. else
  302. begin
  303. if p^.t2^.location.loc=LOC_CREGISTER then
  304. emit_const_reg(A_CMP,opsize,p^.right^.value,
  305. p^.t2^.location.register)
  306. else
  307. emit_const_ref(A_CMP,opsize,p^.right^.value,
  308. newreference(p^.t2^.location.reference));
  309. end;
  310. if p^.backward then
  311. if count_var_is_signed then
  312. hcond:=C_LE
  313. else
  314. hcond:=C_BE
  315. else
  316. if count_var_is_signed then
  317. hcond:=C_GE
  318. else
  319. hcond:=C_AE;
  320. emitjmp(hcond,aktbreaklabel);
  321. { according to count direction DEC or INC... }
  322. { must be after the test because of 0to 255 for bytes !! }
  323. if p^.backward then
  324. hop:=A_DEC
  325. else
  326. hop:=A_INC;
  327. if p^.t2^.location.loc=LOC_CREGISTER then
  328. emit_reg(hop,opsize,p^.t2^.location.register)
  329. else
  330. emit_ref(hop,opsize,newreference(p^.t2^.location.reference));
  331. emitjmp(C_None,l3);
  332. { this is the break label: }
  333. emitlab(aktbreaklabel);
  334. ungetregister32(cmp32);
  335. if temptovalue then
  336. ungetiftemp(temp1);
  337. freelabel(aktcontinuelabel);
  338. freelabel(aktbreaklabel);
  339. freelabel(l3);
  340. aktcontinuelabel:=oldclabel;
  341. aktbreaklabel:=oldblabel;
  342. end;
  343. {*****************************************************************************
  344. SecondExitN
  345. *****************************************************************************}
  346. procedure secondexitn(list: paasmoutput; var p : ptree);
  347. var
  348. is_mem : boolean;
  349. {op : tasmop;
  350. s : topsize;}
  351. otlabel,oflabel : pasmlabel;
  352. label
  353. do_jmp;
  354. begin
  355. if assigned(p^.left) then
  356. if p^.left^.treetype=assignn then
  357. begin
  358. { just do a normal assignment followed by exit }
  359. secondpass(p^.left);
  360. emitjmp(C_None,aktexitlabel);
  361. end
  362. else
  363. begin
  364. otlabel:=truelabel;
  365. oflabel:=falselabel;
  366. getlabel(truelabel);
  367. getlabel(falselabel);
  368. secondpass(p^.left);
  369. case p^.left^.location.loc of
  370. LOC_FPU : goto do_jmp;
  371. LOC_MEM,
  372. LOC_REFERENCE : is_mem:=true;
  373. LOC_CREGISTER,
  374. LOC_REGISTER : is_mem:=false;
  375. LOC_FLAGS : begin
  376. emit_flag2reg(p^.left^.location.resflags,R_AL);
  377. goto do_jmp;
  378. end;
  379. LOC_JUMP : begin
  380. emitlab(truelabel);
  381. emit_const_reg(A_MOV,S_B,1,R_AL);
  382. emitjmp(C_None,aktexit2label);
  383. emitlab(falselabel);
  384. emit_reg_reg(A_XOR,S_B,R_AL,R_AL);
  385. goto do_jmp;
  386. end;
  387. else
  388. internalerror(2001);
  389. end;
  390. case procinfo^.retdef^.deftype of
  391. orddef,
  392. enumdef : begin
  393. case procinfo^.retdef^.size of
  394. 4 : if is_mem then
  395. emit_ref_reg(A_MOV,S_L,
  396. newreference(p^.left^.location.reference),R_EAX)
  397. else
  398. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,R_EAX);
  399. 2 : if is_mem then
  400. emit_ref_reg(A_MOV,S_W,
  401. newreference(p^.left^.location.reference),R_AX)
  402. else
  403. emit_reg_reg(A_MOV,S_W,makereg16(p^.left^.location.register),R_AX);
  404. 1 : if is_mem then
  405. emit_ref_reg(A_MOV,S_B,
  406. newreference(p^.left^.location.reference),R_AL)
  407. else
  408. emit_reg_reg(A_MOV,S_B,makereg8(p^.left^.location.register),R_AL);
  409. end;
  410. end;
  411. pointerdef,
  412. procvardef : begin
  413. if is_mem then
  414. emit_ref_reg(A_MOV,S_L,
  415. newreference(p^.left^.location.reference),R_EAX)
  416. else
  417. emit_reg_reg(A_MOV,S_L,
  418. p^.left^.location.register,R_EAX);
  419. end;
  420. floatdef : begin
  421. if pfloatdef(procinfo^.retdef)^.typ=f32bit then
  422. begin
  423. if is_mem then
  424. emit_ref_reg(A_MOV,S_L,
  425. newreference(p^.left^.location.reference),R_EAX)
  426. else
  427. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,R_EAX);
  428. end
  429. else
  430. if is_mem then
  431. floatload(pfloatdef(procinfo^.retdef)^.typ,p^.left^.location.reference);
  432. end;
  433. end;
  434. do_jmp:
  435. freelabel(truelabel);
  436. freelabel(falselabel);
  437. truelabel:=otlabel;
  438. falselabel:=oflabel;
  439. emitjmp(C_None,aktexit2label);
  440. end
  441. else
  442. begin
  443. emitjmp(C_None,aktexitlabel);
  444. end;
  445. end;
  446. {*****************************************************************************
  447. SecondBreakN
  448. *****************************************************************************}
  449. procedure secondbreakn(list: paasmoutput; var p : ptree);
  450. begin
  451. if aktbreaklabel<>nil then
  452. emitjmp(C_None,aktbreaklabel)
  453. else
  454. CGMessage(cg_e_break_not_allowed);
  455. end;
  456. {*****************************************************************************
  457. SecondContinueN
  458. *****************************************************************************}
  459. procedure secondcontinuen(list: paasmoutput; var p : ptree);
  460. begin
  461. if aktcontinuelabel<>nil then
  462. emitjmp(C_None,aktcontinuelabel)
  463. else
  464. CGMessage(cg_e_continue_not_allowed);
  465. end;
  466. {*****************************************************************************
  467. SecondGoto
  468. *****************************************************************************}
  469. procedure secondgoto(list: paasmoutput; var p : ptree);
  470. begin
  471. emitjmp(C_None,p^.labelnr);
  472. end;
  473. {*****************************************************************************
  474. SecondLabel
  475. *****************************************************************************}
  476. procedure secondlabel(list: paasmoutput; var p : ptree);
  477. begin
  478. emitlab(p^.labelnr);
  479. cleartempgen;
  480. secondpass(p^.left);
  481. end;
  482. {*****************************************************************************
  483. SecondRaise
  484. *****************************************************************************}
  485. procedure secondraise(list: paasmoutput; var p : ptree);
  486. var
  487. a : pasmlabel;
  488. begin
  489. if assigned(p^.left) then
  490. begin
  491. { generate the address }
  492. if assigned(p^.right) then
  493. begin
  494. secondpass(p^.right);
  495. if codegenerror then
  496. exit;
  497. end
  498. else
  499. begin
  500. getlabel(a);
  501. emitlab(a);
  502. emit_sym(A_PUSH,S_L,a);
  503. end;
  504. secondpass(p^.left);
  505. if codegenerror then
  506. exit;
  507. case p^.left^.location.loc of
  508. LOC_MEM,LOC_REFERENCE:
  509. emit_ref(A_PUSH,S_L,
  510. newreference(p^.left^.location.reference));
  511. LOC_CREGISTER,LOC_REGISTER : emit_reg(A_PUSH,S_L,
  512. p^.left^.location.register);
  513. else CGMessage(type_e_mismatch);
  514. end;
  515. emitcall('FPC_RAISEEXCEPTION');
  516. end
  517. else
  518. begin
  519. emitcall('FPC_POPADDRSTACK');
  520. emitcall('FPC_RERAISE');
  521. end;
  522. end;
  523. {*****************************************************************************
  524. SecondTryExcept
  525. *****************************************************************************}
  526. var
  527. endexceptlabel : pasmlabel;
  528. procedure secondtryexcept(list: paasmoutput; var p : ptree);
  529. var
  530. exceptlabel,doexceptlabel,oldendexceptlabel,
  531. lastonlabel : pasmlabel;
  532. begin
  533. { this can be called recursivly }
  534. oldendexceptlabel:=endexceptlabel;
  535. { we modify EAX }
  536. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  537. getlabel(exceptlabel);
  538. getlabel(doexceptlabel);
  539. getlabel(endexceptlabel);
  540. getlabel(lastonlabel);
  541. push_int (1); { push type of exceptionframe }
  542. emitcall('FPC_PUSHEXCEPTADDR');
  543. emit_reg(A_PUSH,S_L,R_EAX);
  544. emitcall('FPC_SETJMP');
  545. emit_reg(A_PUSH,S_L,R_EAX);
  546. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  547. emitjmp(C_NE,exceptlabel);
  548. { try code }
  549. secondpass(p^.left);
  550. if codegenerror then
  551. exit;
  552. emitlab(exceptlabel);
  553. emitcall('FPC_POPADDRSTACK');
  554. emit_reg(A_POP,S_L,R_EAX);
  555. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  556. emitjmp(C_E,endexceptlabel);
  557. emitlab(doexceptlabel);
  558. if assigned(p^.right) then
  559. secondpass(p^.right);
  560. emitlab(lastonlabel);
  561. { default handling }
  562. if assigned(p^.t1) then
  563. begin
  564. { FPC_CATCHES must be called with
  565. 'default handler' flag (=-1)
  566. }
  567. push_int (-1);
  568. emitcall('FPC_CATCHES');
  569. maybe_loadesi;
  570. secondpass(p^.t1);
  571. emitcall('FPC_POPOBJECTSTACK');
  572. maybe_loadesi;
  573. end
  574. else
  575. emitcall('FPC_RERAISE');
  576. { reraise doesn't need a maybe_loadesi because it never }
  577. { returns (FK) }
  578. emitlab(endexceptlabel);
  579. freelabel(exceptlabel);
  580. freelabel(doexceptlabel);
  581. freelabel(endexceptlabel);
  582. freelabel(lastonlabel);
  583. endexceptlabel:=oldendexceptlabel;
  584. end;
  585. procedure secondon(list: paasmoutput; var p : ptree);
  586. var
  587. nextonlabel : pasmlabel;
  588. ref : treference;
  589. begin
  590. getlabel(nextonlabel);
  591. { push the vmt }
  592. emit_sym(A_PUSH,S_L,
  593. newasmsymbol(p^.excepttype^.vmt_mangledname));
  594. emitcall('FPC_CATCHES');
  595. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  596. emitjmp(C_E,nextonlabel);
  597. ref.symbol:=nil;
  598. gettempofsizereference(4,ref);
  599. { what a hack ! }
  600. if assigned(p^.exceptsymtable) then
  601. pvarsym(p^.exceptsymtable^.symindex^.first)^.address:=ref.offset;
  602. emit_reg_ref(A_MOV,S_L,
  603. R_EAX,newreference(ref));
  604. if assigned(p^.right) then
  605. begin
  606. { esi is destroyed by FPC_CATCHES }
  607. maybe_loadesi;
  608. secondpass(p^.right);
  609. end;
  610. emit_ref(A_PUSH,S_L,
  611. newreference(ref));
  612. emitcall('FPC_DESTROYEXCEPTION');
  613. emitcall('FPC_POPOBJECTSTACK');
  614. { clear some stuff }
  615. ungetiftemp(ref);
  616. emitjmp(C_None,endexceptlabel);
  617. emitlab(nextonlabel);
  618. { next on node }
  619. if assigned(p^.left) then
  620. secondpass(p^.left);
  621. end;
  622. {*****************************************************************************
  623. SecondTryFinally
  624. *****************************************************************************}
  625. procedure secondtryfinally(list: paasmoutput; var p : ptree);
  626. var
  627. finallylabel,noreraiselabel : pasmlabel;
  628. oldaktexitlabel,exitfinallylabel : pasmlabel;
  629. oldaktexit2label : pasmlabel;
  630. begin
  631. { we modify EAX }
  632. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  633. getlabel(finallylabel);
  634. getlabel(noreraiselabel);
  635. oldaktexitlabel:=aktexitlabel;
  636. oldaktexit2label:=aktexit2label;
  637. getlabel(exitfinallylabel);
  638. aktexitlabel:=exitfinallylabel;
  639. aktexit2label:=exitfinallylabel;
  640. push_int(1); { Type of stack-frame must be pushed}
  641. emitcall('FPC_PUSHEXCEPTADDR');
  642. emit_reg(A_PUSH,S_L,R_EAX);
  643. emitcall('FPC_SETJMP');
  644. emit_reg(A_PUSH,S_L,R_EAX);
  645. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  646. emitjmp(C_NE,finallylabel);
  647. { try code }
  648. secondpass(p^.left);
  649. if codegenerror then
  650. exit;
  651. emitlab(finallylabel);
  652. emitcall('FPC_POPADDRSTACK');
  653. { finally code }
  654. secondpass(p^.right);
  655. if codegenerror then
  656. exit;
  657. emit_reg(A_POP,S_L,R_EAX);
  658. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  659. emitjmp(C_E,noreraiselabel);
  660. emit_reg(A_DEC,S_L,R_EAX);
  661. emitjmp(C_NE,oldaktexitlabel);
  662. emitcall('FPC_RERAISE');
  663. { reraise never returns ! }
  664. emitlab(exitfinallylabel);
  665. { do some magic for exit in the try block }
  666. emit_reg(A_POP,S_L,R_EAX);
  667. emit_const(A_PUSH,S_L,2);
  668. emitjmp(C_NONE,finallylabel);
  669. emitlab(noreraiselabel);
  670. aktexitlabel:=oldaktexitlabel;
  671. aktexit2label:=oldaktexit2label;
  672. end;
  673. {*****************************************************************************
  674. SecondFail
  675. *****************************************************************************}
  676. procedure secondfail(list: paasmoutput; var p : ptree);
  677. begin
  678. emitjmp(C_None,faillabel);
  679. end;
  680. end.
  681. {
  682. $Log$
  683. Revision 1.1 2000-07-13 06:30:07 michael
  684. + Initial import
  685. Revision 1.2 2000/01/07 01:14:52 peter
  686. * updated copyright to 2000
  687. Revision 1.1 1999/11/03 14:13:59 jonas
  688. + initial implementation
  689. }