cg386flw.pas 31 KB

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