cg386flw.pas 29 KB

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