cg386flw.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 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. unit cg386flw;
  19. interface
  20. uses
  21. tree;
  22. procedure second_while_repeatn(var p : ptree);
  23. procedure secondifn(var p : ptree);
  24. procedure secondfor(var p : ptree);
  25. procedure secondexitn(var p : ptree);
  26. procedure secondbreakn(var p : ptree);
  27. procedure secondcontinuen(var p : ptree);
  28. procedure secondgoto(var p : ptree);
  29. procedure secondlabel(var p : ptree);
  30. procedure secondraise(var p : ptree);
  31. procedure secondtryexcept(var p : ptree);
  32. procedure secondtryfinally(var p : ptree);
  33. procedure secondon(var p : ptree);
  34. procedure secondfail(var p : ptree);
  35. implementation
  36. uses
  37. cobjects,verbose,globals,systems,
  38. symtable,aasm,types,
  39. hcodegen,temp_gen,pass_2,
  40. i386base,i386asm,
  41. cgai386,tgeni386;
  42. {*****************************************************************************
  43. Second_While_RepeatN
  44. *****************************************************************************}
  45. procedure second_while_repeatn(var p : ptree);
  46. var
  47. lcont,lbreak,lloop,
  48. oldclabel,oldblabel : pasmlabel;
  49. otlabel,oflabel : pasmlabel;
  50. begin
  51. getlabel(lloop);
  52. getlabel(lcont);
  53. getlabel(lbreak);
  54. { arrange continue and breaklabels: }
  55. oldclabel:=aktcontinuelabel;
  56. oldblabel:=aktbreaklabel;
  57. { handling code at the end as it is much more efficient, and makes
  58. while equal to repeat loop, only the end true/false is swapped (PFV) }
  59. if p^.treetype=whilen then
  60. emitjmp(C_None,lcont);
  61. emitlab(lloop);
  62. aktcontinuelabel:=lcont;
  63. aktbreaklabel:=lbreak;
  64. cleartempgen;
  65. if assigned(p^.right) then
  66. secondpass(p^.right);
  67. emitlab(lcont);
  68. otlabel:=truelabel;
  69. oflabel:=falselabel;
  70. if p^.treetype=whilen then
  71. begin
  72. truelabel:=lloop;
  73. falselabel:=lbreak;
  74. end
  75. { repeatn }
  76. else
  77. begin
  78. truelabel:=lbreak;
  79. falselabel:=lloop;
  80. end;
  81. cleartempgen;
  82. secondpass(p^.left);
  83. maketojumpbool(p^.left);
  84. emitlab(lbreak);
  85. freelabel(lloop);
  86. freelabel(lcont);
  87. freelabel(lbreak);
  88. truelabel:=otlabel;
  89. falselabel:=oflabel;
  90. aktcontinuelabel:=oldclabel;
  91. aktbreaklabel:=oldblabel;
  92. end;
  93. {*****************************************************************************
  94. SecondIfN
  95. *****************************************************************************}
  96. procedure secondifn(var p : ptree);
  97. var
  98. hl,otlabel,oflabel : pasmlabel;
  99. begin
  100. otlabel:=truelabel;
  101. oflabel:=falselabel;
  102. getlabel(truelabel);
  103. getlabel(falselabel);
  104. cleartempgen;
  105. secondpass(p^.left);
  106. maketojumpbool(p^.left);
  107. if assigned(p^.right) then
  108. begin
  109. emitlab(truelabel);
  110. cleartempgen;
  111. secondpass(p^.right);
  112. end;
  113. if assigned(p^.t1) then
  114. begin
  115. if assigned(p^.right) then
  116. begin
  117. getlabel(hl);
  118. { do go back to if line !! }
  119. aktfilepos:=exprasmlist^.getlasttaifilepos^;
  120. emitjmp(C_None,hl);
  121. end;
  122. emitlab(falselabel);
  123. cleartempgen;
  124. secondpass(p^.t1);
  125. if assigned(p^.right) then
  126. emitlab(hl);
  127. end
  128. else
  129. begin
  130. emitlab(falselabel);
  131. end;
  132. if not(assigned(p^.right)) then
  133. begin
  134. emitlab(truelabel);
  135. end;
  136. freelabel(truelabel);
  137. freelabel(falselabel);
  138. truelabel:=otlabel;
  139. falselabel:=oflabel;
  140. end;
  141. {*****************************************************************************
  142. SecondFor
  143. *****************************************************************************}
  144. procedure secondfor(var p : ptree);
  145. var
  146. l3,oldclabel,oldblabel : pasmlabel;
  147. omitfirstcomp,temptovalue : boolean;
  148. hs : byte;
  149. temp1 : treference;
  150. hop : tasmop;
  151. hcond : tasmcond;
  152. cmpreg,cmp32 : tregister;
  153. opsize : topsize;
  154. count_var_is_signed : boolean;
  155. begin
  156. oldclabel:=aktcontinuelabel;
  157. oldblabel:=aktbreaklabel;
  158. getlabel(aktcontinuelabel);
  159. getlabel(aktbreaklabel);
  160. getlabel(l3);
  161. { could we spare the first comparison ? }
  162. omitfirstcomp:=false;
  163. if p^.right^.treetype=ordconstn then
  164. if p^.left^.right^.treetype=ordconstn then
  165. omitfirstcomp:=(p^.backward and (p^.left^.right^.value>=p^.right^.value))
  166. or (not(p^.backward) and (p^.left^.right^.value<=p^.right^.value));
  167. { only calculate reference }
  168. cleartempgen;
  169. secondpass(p^.t2);
  170. {$ifndef OLDFORVER}
  171. hs:=p^.t2^.resulttype^.size;
  172. cmp32:=getregister32;
  173. case hs of
  174. 1 : begin
  175. opsize:=S_B;
  176. cmpreg:=reg32toreg8(cmp32);
  177. end;
  178. 2 : begin
  179. opsize:=S_W;
  180. cmpreg:=reg32toreg16(cmp32);
  181. end;
  182. 4 : begin
  183. opsize:=S_L;
  184. cmpreg:=cmp32;
  185. end;
  186. end;
  187. (*
  188. if not(simple_loadn) then
  189. CGMessage(cg_e_illegal_count_var);
  190. already done in firstfor !! *)
  191. { first set the to value
  192. because the count var can be in the expression !! }
  193. cleartempgen;
  194. secondpass(p^.right);
  195. { calculate pointer value and check if changeable and if so }
  196. { load into temporary variable }
  197. if p^.right^.treetype<>ordconstn then
  198. begin
  199. temp1.symbol:=nil;
  200. gettempofsizereference(hs,temp1);
  201. temptovalue:=true;
  202. if (p^.right^.location.loc=LOC_REGISTER) or
  203. (p^.right^.location.loc=LOC_CREGISTER) then
  204. begin
  205. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,p^.right^.location.register,
  206. newreference(temp1))));
  207. end
  208. else
  209. concatcopy(p^.right^.location.reference,temp1,hs,false,false);
  210. end
  211. else temptovalue:=false;
  212. {$endif OLDFORVER}
  213. { produce start assignment }
  214. cleartempgen;
  215. secondpass(p^.left);
  216. count_var_is_signed:=is_signed(porddef(p^.t2^.resulttype));
  217. {$ifdef OLDFORVER}
  218. hs:=p^.t2^.resulttype^.size;
  219. cmp32:=getregister32;
  220. case hs of
  221. 1 : begin
  222. opsize:=S_B;
  223. cmpreg:=reg32toreg8(cmp32);
  224. end;
  225. 2 : begin
  226. opsize:=S_W;
  227. cmpreg:=reg32toreg16(cmp32);
  228. end;
  229. 4 : begin
  230. opsize:=S_L;
  231. cmpreg:=cmp32;
  232. end;
  233. end;
  234. cleartempgen;
  235. secondpass(p^.right);
  236. { calculate pointer value and check if changeable and if so }
  237. { load into temporary variable }
  238. if p^.right^.treetype<>ordconstn then
  239. begin
  240. temp1.symbol:=nil;
  241. gettempofsizereference(hs,temp1);
  242. temptovalue:=true;
  243. if (p^.right^.location.loc=LOC_REGISTER) or
  244. (p^.right^.location.loc=LOC_CREGISTER) then
  245. begin
  246. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,p^.right^.location.register,
  247. newreference(temp1))));
  248. end
  249. else
  250. concatcopy(p^.right^.location.reference,temp1,hs,false,false);
  251. end
  252. else temptovalue:=false;
  253. {$endif OLDFORVER}
  254. if temptovalue then
  255. begin
  256. if p^.t2^.location.loc=LOC_CREGISTER then
  257. begin
  258. exprasmlist^.concat(new(pai386,op_ref_reg(A_CMP,opsize,newreference(temp1),
  259. p^.t2^.location.register)));
  260. end
  261. else
  262. begin
  263. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.t2^.location.reference),
  264. cmpreg)));
  265. exprasmlist^.concat(new(pai386,op_ref_reg(A_CMP,opsize,newreference(temp1),
  266. cmpreg)));
  267. end;
  268. end
  269. else
  270. begin
  271. if not(omitfirstcomp) then
  272. begin
  273. if p^.t2^.location.loc=LOC_CREGISTER then
  274. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^.right^.value,
  275. p^.t2^.location.register)))
  276. else
  277. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,opsize,p^.right^.value,
  278. newreference(p^.t2^.location.reference))));
  279. end;
  280. end;
  281. if p^.backward then
  282. if count_var_is_signed then
  283. hcond:=C_L
  284. else
  285. hcond:=C_B
  286. else
  287. if count_var_is_signed then
  288. hcond:=C_G
  289. else
  290. hcond:=C_A;
  291. if not(omitfirstcomp) or temptovalue then
  292. emitjmp(hcond,aktbreaklabel);
  293. emitlab(l3);
  294. { help register must not be in instruction block }
  295. cleartempgen;
  296. if assigned(p^.t1) then
  297. secondpass(p^.t1);
  298. emitlab(aktcontinuelabel);
  299. { makes no problems there }
  300. cleartempgen;
  301. { demand help register again }
  302. cmp32:=getregister32;
  303. case hs of
  304. 1 : begin
  305. opsize:=S_B;
  306. cmpreg:=reg32toreg8(cmp32);
  307. end;
  308. 2 : begin
  309. opsize:=S_W;
  310. cmpreg:=reg32toreg16(cmp32);
  311. end;
  312. 4 : opsize:=S_L;
  313. end;
  314. { produce comparison and the corresponding }
  315. { jump }
  316. if temptovalue then
  317. begin
  318. if p^.t2^.location.loc=LOC_CREGISTER then
  319. begin
  320. exprasmlist^.concat(new(pai386,op_ref_reg(A_CMP,opsize,newreference(temp1),
  321. p^.t2^.location.register)));
  322. end
  323. else
  324. begin
  325. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.t2^.location.reference),
  326. cmpreg)));
  327. exprasmlist^.concat(new(pai386,op_ref_reg(A_CMP,opsize,newreference(temp1),
  328. cmpreg)));
  329. end;
  330. end
  331. else
  332. begin
  333. if p^.t2^.location.loc=LOC_CREGISTER then
  334. exprasmlist^.concat(new(pai386,op_const_reg(A_CMP,opsize,p^.right^.value,
  335. p^.t2^.location.register)))
  336. else
  337. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,opsize,p^.right^.value,
  338. newreference(p^.t2^.location.reference))));
  339. end;
  340. if p^.backward then
  341. if count_var_is_signed then
  342. hcond:=C_LE
  343. else
  344. hcond:=C_BE
  345. else
  346. if count_var_is_signed then
  347. hcond:=C_GE
  348. else
  349. hcond:=C_AE;
  350. emitjmp(hcond,aktbreaklabel);
  351. { according to count direction DEC or INC... }
  352. { must be after the test because of 0to 255 for bytes !! }
  353. if p^.backward then
  354. hop:=A_DEC
  355. else
  356. hop:=A_INC;
  357. if p^.t2^.location.loc=LOC_CREGISTER then
  358. exprasmlist^.concat(new(pai386,op_reg(hop,opsize,p^.t2^.location.register)))
  359. else
  360. exprasmlist^.concat(new(pai386,op_ref(hop,opsize,newreference(p^.t2^.location.reference))));
  361. emitjmp(C_None,l3);
  362. { this is the break label: }
  363. emitlab(aktbreaklabel);
  364. ungetregister32(cmp32);
  365. if temptovalue then
  366. ungetiftemp(temp1);
  367. freelabel(aktcontinuelabel);
  368. freelabel(aktbreaklabel);
  369. freelabel(l3);
  370. aktcontinuelabel:=oldclabel;
  371. aktbreaklabel:=oldblabel;
  372. end;
  373. {*****************************************************************************
  374. SecondExitN
  375. *****************************************************************************}
  376. procedure secondexitn(var p : ptree);
  377. var
  378. is_mem : boolean;
  379. {op : tasmop;
  380. s : topsize;}
  381. otlabel,oflabel : pasmlabel;
  382. label
  383. do_jmp;
  384. begin
  385. if assigned(p^.left) then
  386. begin
  387. otlabel:=truelabel;
  388. oflabel:=falselabel;
  389. getlabel(truelabel);
  390. getlabel(falselabel);
  391. secondpass(p^.left);
  392. case p^.left^.location.loc of
  393. LOC_FPU : goto do_jmp;
  394. LOC_MEM,
  395. LOC_REFERENCE : is_mem:=true;
  396. LOC_CREGISTER,
  397. LOC_REGISTER : is_mem:=false;
  398. LOC_FLAGS : begin
  399. emit_flag2reg(p^.left^.location.resflags,R_AL);
  400. goto do_jmp;
  401. end;
  402. LOC_JUMP : begin
  403. emitlab(truelabel);
  404. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_B,1,R_AL)));
  405. emitjmp(C_None,aktexit2label);
  406. emitlab(falselabel);
  407. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_B,R_AL,R_AL)));
  408. goto do_jmp;
  409. end;
  410. else
  411. internalerror(2001);
  412. end;
  413. case procinfo.retdef^.deftype of
  414. orddef,
  415. enumdef : begin
  416. case procinfo.retdef^.size of
  417. 4 : if is_mem then
  418. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  419. newreference(p^.left^.location.reference),R_EAX)))
  420. else
  421. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,R_EAX);
  422. 2 : if is_mem then
  423. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,
  424. newreference(p^.left^.location.reference),R_AX)))
  425. else
  426. emit_reg_reg(A_MOV,S_W,makereg16(p^.left^.location.register),R_AX);
  427. 1 : if is_mem then
  428. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_B,
  429. newreference(p^.left^.location.reference),R_AL)))
  430. else
  431. emit_reg_reg(A_MOV,S_B,makereg8(p^.left^.location.register),R_AL);
  432. end;
  433. end;
  434. pointerdef,
  435. procvardef : begin
  436. if is_mem then
  437. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  438. newreference(p^.left^.location.reference),R_EAX)))
  439. else
  440. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  441. p^.left^.location.register,R_EAX)));
  442. end;
  443. floatdef : begin
  444. if pfloatdef(procinfo.retdef)^.typ=f32bit then
  445. begin
  446. if is_mem then
  447. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  448. newreference(p^.left^.location.reference),R_EAX)))
  449. else
  450. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,R_EAX);
  451. end
  452. else
  453. if is_mem then
  454. floatload(pfloatdef(procinfo.retdef)^.typ,p^.left^.location.reference);
  455. end;
  456. end;
  457. do_jmp:
  458. freelabel(truelabel);
  459. freelabel(falselabel);
  460. truelabel:=otlabel;
  461. falselabel:=oflabel;
  462. emitjmp(C_None,aktexit2label);
  463. end
  464. else
  465. begin
  466. emitjmp(C_None,aktexitlabel);
  467. end;
  468. end;
  469. {*****************************************************************************
  470. SecondBreakN
  471. *****************************************************************************}
  472. procedure secondbreakn(var p : ptree);
  473. begin
  474. if aktbreaklabel<>nil then
  475. emitjmp(C_None,aktbreaklabel)
  476. else
  477. CGMessage(cg_e_break_not_allowed);
  478. end;
  479. {*****************************************************************************
  480. SecondContinueN
  481. *****************************************************************************}
  482. procedure secondcontinuen(var p : ptree);
  483. begin
  484. if aktcontinuelabel<>nil then
  485. emitjmp(C_None,aktcontinuelabel)
  486. else
  487. CGMessage(cg_e_continue_not_allowed);
  488. end;
  489. {*****************************************************************************
  490. SecondGoto
  491. *****************************************************************************}
  492. procedure secondgoto(var p : ptree);
  493. begin
  494. emitjmp(C_None,p^.labelnr);
  495. end;
  496. {*****************************************************************************
  497. SecondLabel
  498. *****************************************************************************}
  499. procedure secondlabel(var p : ptree);
  500. begin
  501. emitlab(p^.labelnr);
  502. cleartempgen;
  503. secondpass(p^.left);
  504. end;
  505. {*****************************************************************************
  506. SecondRaise
  507. *****************************************************************************}
  508. procedure secondraise(var p : ptree);
  509. var
  510. a : pasmlabel;
  511. begin
  512. if assigned(p^.left) then
  513. begin
  514. { generate the address }
  515. if assigned(p^.right) then
  516. begin
  517. secondpass(p^.right);
  518. if codegenerror then
  519. exit;
  520. end
  521. else
  522. begin
  523. getlabel(a);
  524. emitlab(a);
  525. exprasmlist^.concat(new(pai386,
  526. op_sym(A_PUSH,S_L,a)));
  527. end;
  528. secondpass(p^.left);
  529. if codegenerror then
  530. exit;
  531. case p^.left^.location.loc of
  532. LOC_MEM,LOC_REFERENCE:
  533. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,
  534. newreference(p^.left^.location.reference))));
  535. LOC_CREGISTER,LOC_REGISTER : exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,
  536. p^.left^.location.register)));
  537. else CGMessage(type_e_mismatch);
  538. end;
  539. emitcall('FPC_RAISEEXCEPTION');
  540. end
  541. else
  542. begin
  543. emitcall('FPC_RERAISE');
  544. end;
  545. end;
  546. {*****************************************************************************
  547. SecondTryExcept
  548. *****************************************************************************}
  549. var
  550. endexceptlabel : pasmlabel;
  551. procedure secondtryexcept(var p : ptree);
  552. var
  553. exceptlabel,doexceptlabel,oldendexceptlabel,
  554. lastonlabel : pasmlabel;
  555. begin
  556. { this can be called recursivly }
  557. oldendexceptlabel:=endexceptlabel;
  558. { we modify EAX }
  559. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  560. getlabel(exceptlabel);
  561. getlabel(doexceptlabel);
  562. getlabel(endexceptlabel);
  563. getlabel(lastonlabel);
  564. push_int (1); { push type of exceptionframe }
  565. emitcall('FPC_PUSHEXCEPTADDR');
  566. exprasmlist^.concat(new(pai386,
  567. op_reg(A_PUSH,S_L,R_EAX)));
  568. emitcall('FPC_SETJMP');
  569. exprasmlist^.concat(new(pai386,
  570. op_reg(A_PUSH,S_L,R_EAX)));
  571. exprasmlist^.concat(new(pai386,
  572. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  573. emitjmp(C_NE,exceptlabel);
  574. { try code }
  575. secondpass(p^.left);
  576. if codegenerror then
  577. exit;
  578. emitlab(exceptlabel);
  579. exprasmlist^.concat(new(pai386,
  580. op_reg(A_POP,S_L,R_EAX)));
  581. exprasmlist^.concat(new(pai386,
  582. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  583. emitjmp(C_NE,doexceptlabel);
  584. emitcall('FPC_POPADDRSTACK');
  585. emitjmp(C_None,endexceptlabel);
  586. emitlab(doexceptlabel);
  587. if assigned(p^.right) then
  588. secondpass(p^.right);
  589. emitlab(lastonlabel);
  590. { default handling }
  591. if assigned(p^.t1) then
  592. begin
  593. { FPC_CATCHES must be called with
  594. 'default handler' flag (=-1)
  595. }
  596. push_int (-1);
  597. emitcall('FPC_CATCHES');
  598. secondpass(p^.t1);
  599. emitcall('FPC_POPOBJECTSTACK');
  600. end
  601. else
  602. emitcall('FPC_RERAISE');
  603. emitlab(endexceptlabel);
  604. freelabel(exceptlabel);
  605. freelabel(doexceptlabel);
  606. freelabel(endexceptlabel);
  607. freelabel(lastonlabel);
  608. endexceptlabel:=oldendexceptlabel;
  609. end;
  610. procedure secondon(var p : ptree);
  611. var
  612. nextonlabel : pasmlabel;
  613. ref : treference;
  614. begin
  615. getlabel(nextonlabel);
  616. { push the vmt }
  617. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,
  618. newasmsymbol(p^.excepttype^.vmt_mangledname))));
  619. emitcall('FPC_CATCHES');
  620. exprasmlist^.concat(new(pai386,
  621. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  622. emitjmp(C_E,nextonlabel);
  623. ref.symbol:=nil;
  624. gettempofsizereference(4,ref);
  625. { what a hack ! }
  626. if assigned(p^.exceptsymtable) then
  627. pvarsym(p^.exceptsymtable^.symindex^.first)^.address:=ref.offset;
  628. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  629. R_EAX,newreference(ref))));
  630. if assigned(p^.right) then
  631. secondpass(p^.right);
  632. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,
  633. newreference(ref))));
  634. emitcall('FPC_DESTROYEXCEPTION');
  635. emitcall('FPC_POPOBJECTSTACK');
  636. { clear some stuff }
  637. ungetiftemp(ref);
  638. emitjmp(C_None,endexceptlabel);
  639. emitlab(nextonlabel);
  640. { next on node }
  641. if assigned(p^.left) then
  642. secondpass(p^.left);
  643. end;
  644. {*****************************************************************************
  645. SecondTryFinally
  646. *****************************************************************************}
  647. procedure secondtryfinally(var p : ptree);
  648. var
  649. finallylabel,noreraiselabel : pasmlabel;
  650. begin
  651. { we modify EAX }
  652. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  653. getlabel(finallylabel);
  654. getlabel(noreraiselabel);
  655. push_int(1); { Type of stack-frame must be pushed}
  656. emitcall('FPC_PUSHEXCEPTADDR');
  657. exprasmlist^.concat(new(pai386,
  658. op_reg(A_PUSH,S_L,R_EAX)));
  659. emitcall('FPC_SETJMP');
  660. exprasmlist^.concat(new(pai386,
  661. op_reg(A_PUSH,S_L,R_EAX)));
  662. exprasmlist^.concat(new(pai386,
  663. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  664. emitjmp(C_NE,finallylabel);
  665. { try code }
  666. secondpass(p^.left);
  667. if codegenerror then
  668. exit;
  669. emitlab(finallylabel);
  670. { finally code }
  671. secondpass(p^.right);
  672. if codegenerror then
  673. exit;
  674. exprasmlist^.concat(new(pai386,
  675. op_reg(A_POP,S_L,R_EAX)));
  676. exprasmlist^.concat(new(pai386,
  677. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  678. emitjmp(C_E,noreraiselabel);
  679. emitcall('FPC_RERAISE');
  680. emitlab(noreraiselabel);
  681. emitcall('FPC_POPADDRSTACK');
  682. end;
  683. {*****************************************************************************
  684. SecondFail
  685. *****************************************************************************}
  686. procedure secondfail(var p : ptree);
  687. var
  688. hp : preference;
  689. begin
  690. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_ESI,R_ESI)));
  691. { also reset to zero in the stack }
  692. new(hp);
  693. reset_reference(hp^);
  694. hp^.offset:=procinfo.ESI_offset;
  695. hp^.base:=procinfo.framepointer;
  696. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_ESI,hp)));
  697. emitjmp(C_None,quickexitlabel);
  698. end;
  699. end.
  700. {
  701. $Log$
  702. Revision 1.40 1999-06-14 00:43:35 peter
  703. * merged
  704. Revision 1.39.2.1 1999/06/14 00:39:29 peter
  705. * don't pop object stack in catches, because it's needed for reraise
  706. Revision 1.39 1999/05/27 19:44:12 peter
  707. * removed oldasm
  708. * plabel -> pasmlabel
  709. * -a switches to source writing automaticly
  710. * assembler readers OOPed
  711. * asmsymbol automaticly external
  712. * jumptables and other label fixes for asm readers
  713. Revision 1.38 1999/05/21 13:54:48 peter
  714. * NEWLAB for label as symbol
  715. Revision 1.37 1999/05/17 21:57:01 florian
  716. * new temporary ansistring handling
  717. Revision 1.36 1999/05/13 21:59:21 peter
  718. * removed oldppu code
  719. * warning if objpas is loaded from uses
  720. * first things for new deref writing
  721. Revision 1.35 1999/05/01 13:24:07 peter
  722. * merged nasm compiler
  723. * old asm moved to oldasm/
  724. Revision 1.34 1999/04/26 13:31:25 peter
  725. * release storenumber,double_checksum
  726. Revision 1.33 1999/04/21 09:43:29 peter
  727. * storenumber works
  728. * fixed some typos in double_checksum
  729. + incompatible types type1 and type2 message (with storenumber)
  730. Revision 1.32 1999/04/17 13:10:58 peter
  731. * fixed exit()
  732. Revision 1.31 1999/04/14 09:14:46 peter
  733. * first things to store the symbol/def number in the ppu
  734. Revision 1.30 1999/03/05 16:14:59 peter
  735. * fixed exit() with word/byte return
  736. Revision 1.29 1999/02/25 21:02:26 peter
  737. * ag386bin updates
  738. + coff writer
  739. Revision 1.28 1999/02/22 02:15:09 peter
  740. * updates for ag386bin
  741. Revision 1.27 1999/01/26 11:26:21 pierre
  742. * bug0152 for i:=1 to i-5 do (i-5) evaluated first
  743. Revision 1.26 1998/12/19 00:23:44 florian
  744. * ansistring memory leaks fixed
  745. Revision 1.25 1998/11/30 09:43:03 pierre
  746. * some range check bugs fixed (still not working !)
  747. + added DLL writing support for win32 (also accepts variables)
  748. + TempAnsi for code that could be used for Temporary ansi strings
  749. handling
  750. Revision 1.24 1998/11/18 15:44:09 peter
  751. * VALUEPARA for tp7 compatible value parameters
  752. Revision 1.23 1998/11/12 16:43:32 florian
  753. * functions with ansi strings as result didn't work, solved
  754. Revision 1.22 1998/10/29 15:42:44 florian
  755. + partial disposing of temp. ansistrings
  756. Revision 1.21 1998/10/26 22:58:16 florian
  757. * new introduded problem with classes fix, the parent class wasn't set
  758. correct, if the class was defined forward before
  759. Revision 1.20 1998/10/06 17:16:42 pierre
  760. * some memory leaks fixed (thanks to Peter for heaptrc !)
  761. Revision 1.19 1998/09/28 12:13:53 peter
  762. * fixed repeat continue until true;
  763. Revision 1.18 1998/09/26 15:03:04 florian
  764. * small problems with DOM and excpetions fixed (code generation
  765. of raise was wrong and self was sometimes destroyed :()
  766. Revision 1.17 1998/09/17 09:42:14 peter
  767. + pass_2 for cg386
  768. * Message() -> CGMessage() for pass_1/pass_2
  769. Revision 1.16 1998/09/14 10:43:48 peter
  770. * all internal RTL functions start with FPC_
  771. Revision 1.15 1998/09/04 08:41:39 peter
  772. * updated some error CGMessages
  773. Revision 1.14 1998/09/03 17:08:39 pierre
  774. * better lines for stabs
  775. (no scroll back to if before else part
  776. no return to case line at jump outside case)
  777. + source lines also if not in order
  778. Revision 1.13 1998/09/01 12:47:58 peter
  779. * use pdef^.size instead of orddef^.typ
  780. Revision 1.12 1998/08/28 10:56:58 peter
  781. * removed warnings
  782. Revision 1.11 1998/08/05 16:00:10 florian
  783. * some fixes for ansi strings
  784. Revision 1.10 1998/08/04 16:26:26 jonas
  785. * converted // comment to TP comment
  786. Revision 1.9 1998/07/31 11:36:34 michael
  787. Default exception handler also needs to call FPC_CATCHES
  788. Revision 1.8 1998/07/30 13:30:32 florian
  789. * final implemenation of exception support, maybe it needs
  790. some fixes :)
  791. Revision 1.7 1998/07/30 11:18:13 florian
  792. + first implementation of try ... except on .. do end;
  793. * limitiation of 65535 bytes parameters for cdecl removed
  794. Revision 1.6 1998/07/29 13:29:11 michael
  795. + Corrected try.. code. Type of exception fram is pushed
  796. Revision 1.5 1998/07/28 21:52:49 florian
  797. + implementation of raise and try..finally
  798. + some misc. exception stuff
  799. Revision 1.4 1998/07/24 22:16:53 florian
  800. * internal error 10 together with array access fixed. I hope
  801. that's the final fix.
  802. Revision 1.3 1998/06/25 08:48:08 florian
  803. * first version of rtti support
  804. Revision 1.2 1998/06/08 13:13:33 pierre
  805. + temporary variables now in temp_gen.pas unit
  806. because it is processor independent
  807. * mppc68k.bat modified to undefine i386 and support_mmx
  808. (which are defaults for i386)
  809. Revision 1.1 1998/06/05 17:44:12 peter
  810. * splitted cgi386
  811. }