2
0

cg386flw.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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,globtype,globals,systems,
  38. symconst,symtable,aasm,types,
  39. hcodegen,temp_gen,pass_2,
  40. cpubase,cpuasm,
  41. cgai386,tgeni386,tcflw;
  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. truelabel:=otlabel;
  86. falselabel:=oflabel;
  87. aktcontinuelabel:=oldclabel;
  88. aktbreaklabel:=oldblabel;
  89. end;
  90. {*****************************************************************************
  91. SecondIfN
  92. *****************************************************************************}
  93. procedure secondifn(var p : ptree);
  94. var
  95. hl,otlabel,oflabel : pasmlabel;
  96. begin
  97. otlabel:=truelabel;
  98. oflabel:=falselabel;
  99. getlabel(truelabel);
  100. getlabel(falselabel);
  101. cleartempgen;
  102. secondpass(p^.left);
  103. maketojumpbool(p^.left);
  104. if assigned(p^.right) then
  105. begin
  106. emitlab(truelabel);
  107. cleartempgen;
  108. secondpass(p^.right);
  109. end;
  110. if assigned(p^.t1) then
  111. begin
  112. if assigned(p^.right) then
  113. begin
  114. getlabel(hl);
  115. { do go back to if line !! }
  116. aktfilepos:=exprasmlist^.getlasttaifilepos^;
  117. emitjmp(C_None,hl);
  118. end;
  119. emitlab(falselabel);
  120. cleartempgen;
  121. secondpass(p^.t1);
  122. if assigned(p^.right) then
  123. emitlab(hl);
  124. end
  125. else
  126. begin
  127. emitlab(falselabel);
  128. end;
  129. if not(assigned(p^.right)) then
  130. begin
  131. emitlab(truelabel);
  132. end;
  133. truelabel:=otlabel;
  134. falselabel:=oflabel;
  135. end;
  136. {*****************************************************************************
  137. SecondFor
  138. *****************************************************************************}
  139. procedure secondfor(var p : ptree);
  140. var
  141. l3,oldclabel,oldblabel : pasmlabel;
  142. omitfirstcomp,temptovalue : boolean;
  143. hs : byte;
  144. temp1 : treference;
  145. hop : tasmop;
  146. hcond : tasmcond;
  147. cmpreg,cmp32 : tregister;
  148. opsize : topsize;
  149. count_var_is_signed : boolean;
  150. begin
  151. oldclabel:=aktcontinuelabel;
  152. oldblabel:=aktbreaklabel;
  153. getlabel(aktcontinuelabel);
  154. getlabel(aktbreaklabel);
  155. getlabel(l3);
  156. { could we spare the first comparison ? }
  157. omitfirstcomp:=false;
  158. if p^.right^.treetype=ordconstn then
  159. if p^.left^.right^.treetype=ordconstn then
  160. omitfirstcomp:=(p^.backward and (p^.left^.right^.value>=p^.right^.value))
  161. or (not(p^.backward) and (p^.left^.right^.value<=p^.right^.value));
  162. { only calculate reference }
  163. cleartempgen;
  164. secondpass(p^.t2);
  165. hs:=p^.t2^.resulttype^.size;
  166. cmp32:=getregister32;
  167. case hs of
  168. 1 : begin
  169. opsize:=S_B;
  170. cmpreg:=reg32toreg8(cmp32);
  171. end;
  172. 2 : begin
  173. opsize:=S_W;
  174. cmpreg:=reg32toreg16(cmp32);
  175. end;
  176. 4 : begin
  177. opsize:=S_L;
  178. cmpreg:=cmp32;
  179. end;
  180. end;
  181. { first set the to value
  182. because the count var can be in the expression !! }
  183. cleartempgen;
  184. secondpass(p^.right);
  185. { calculate pointer value and check if changeable and if so }
  186. { load into temporary variable }
  187. if p^.right^.treetype<>ordconstn then
  188. begin
  189. temp1.symbol:=nil;
  190. gettempofsizereference(hs,temp1);
  191. temptovalue:=true;
  192. if (p^.right^.location.loc=LOC_REGISTER) or
  193. (p^.right^.location.loc=LOC_CREGISTER) then
  194. begin
  195. emit_reg_ref(A_MOV,opsize,p^.right^.location.register,
  196. newreference(temp1));
  197. end
  198. else
  199. concatcopy(p^.right^.location.reference,temp1,hs,false,false);
  200. end
  201. else
  202. temptovalue:=false;
  203. { produce start assignment }
  204. cleartempgen;
  205. secondpass(p^.left);
  206. count_var_is_signed:=is_signed(porddef(p^.t2^.resulttype));
  207. if temptovalue then
  208. begin
  209. if p^.t2^.location.loc=LOC_CREGISTER then
  210. begin
  211. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  212. p^.t2^.location.register);
  213. end
  214. else
  215. begin
  216. emit_ref_reg(A_MOV,opsize,newreference(p^.t2^.location.reference),
  217. cmpreg);
  218. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  219. cmpreg);
  220. end;
  221. end
  222. else
  223. begin
  224. if not(omitfirstcomp) then
  225. begin
  226. if p^.t2^.location.loc=LOC_CREGISTER then
  227. emit_const_reg(A_CMP,opsize,p^.right^.value,
  228. p^.t2^.location.register)
  229. else
  230. emit_const_ref(A_CMP,opsize,p^.right^.value,
  231. newreference(p^.t2^.location.reference));
  232. end;
  233. end;
  234. if p^.backward then
  235. if count_var_is_signed then
  236. hcond:=C_L
  237. else
  238. hcond:=C_B
  239. else
  240. if count_var_is_signed then
  241. hcond:=C_G
  242. else
  243. hcond:=C_A;
  244. if not(omitfirstcomp) or temptovalue then
  245. emitjmp(hcond,aktbreaklabel);
  246. { align loop target }
  247. if not(cs_littlesize in aktglobalswitches) then
  248. exprasmlist^.concat(new(pai_align,init_op(4,$90)));
  249. emitlab(l3);
  250. { help register must not be in instruction block }
  251. cleartempgen;
  252. if assigned(p^.t1) then
  253. secondpass(p^.t1);
  254. emitlab(aktcontinuelabel);
  255. { makes no problems there }
  256. cleartempgen;
  257. { demand help register again }
  258. cmp32:=getregister32;
  259. case hs of
  260. 1 : begin
  261. opsize:=S_B;
  262. cmpreg:=reg32toreg8(cmp32);
  263. end;
  264. 2 : begin
  265. opsize:=S_W;
  266. cmpreg:=reg32toreg16(cmp32);
  267. end;
  268. 4 : opsize:=S_L;
  269. end;
  270. { produce comparison and the corresponding }
  271. { jump }
  272. if temptovalue then
  273. begin
  274. if p^.t2^.location.loc=LOC_CREGISTER then
  275. begin
  276. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  277. p^.t2^.location.register);
  278. end
  279. else
  280. begin
  281. emit_ref_reg(A_MOV,opsize,newreference(p^.t2^.location.reference),
  282. cmpreg);
  283. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  284. cmpreg);
  285. end;
  286. end
  287. else
  288. begin
  289. if p^.t2^.location.loc=LOC_CREGISTER then
  290. emit_const_reg(A_CMP,opsize,p^.right^.value,
  291. p^.t2^.location.register)
  292. else
  293. emit_const_ref(A_CMP,opsize,p^.right^.value,
  294. newreference(p^.t2^.location.reference));
  295. end;
  296. if p^.backward then
  297. if count_var_is_signed then
  298. hcond:=C_LE
  299. else
  300. hcond:=C_BE
  301. else
  302. if count_var_is_signed then
  303. hcond:=C_GE
  304. else
  305. hcond:=C_AE;
  306. emitjmp(hcond,aktbreaklabel);
  307. { according to count direction DEC or INC... }
  308. { must be after the test because of 0to 255 for bytes !! }
  309. if p^.backward then
  310. hop:=A_DEC
  311. else
  312. hop:=A_INC;
  313. if p^.t2^.location.loc=LOC_CREGISTER then
  314. emit_reg(hop,opsize,p^.t2^.location.register)
  315. else
  316. emit_ref(hop,opsize,newreference(p^.t2^.location.reference));
  317. emitjmp(C_None,l3);
  318. { this is the break label: }
  319. emitlab(aktbreaklabel);
  320. ungetregister32(cmp32);
  321. if temptovalue then
  322. ungetiftemp(temp1);
  323. aktcontinuelabel:=oldclabel;
  324. aktbreaklabel:=oldblabel;
  325. end;
  326. {*****************************************************************************
  327. SecondExitN
  328. *****************************************************************************}
  329. procedure secondexitn(var p : ptree);
  330. var
  331. is_mem : boolean;
  332. {op : tasmop;
  333. s : topsize;}
  334. otlabel,oflabel : pasmlabel;
  335. label
  336. do_jmp;
  337. begin
  338. if assigned(p^.left) then
  339. if p^.left^.treetype=assignn then
  340. begin
  341. { just do a normal assignment followed by exit }
  342. secondpass(p^.left);
  343. emitjmp(C_None,aktexitlabel);
  344. end
  345. else
  346. begin
  347. otlabel:=truelabel;
  348. oflabel:=falselabel;
  349. getlabel(truelabel);
  350. getlabel(falselabel);
  351. secondpass(p^.left);
  352. case p^.left^.location.loc of
  353. LOC_FPU : goto do_jmp;
  354. LOC_MEM,
  355. LOC_REFERENCE : is_mem:=true;
  356. LOC_CREGISTER,
  357. LOC_REGISTER : is_mem:=false;
  358. LOC_FLAGS : begin
  359. emit_flag2reg(p^.left^.location.resflags,R_AL);
  360. goto do_jmp;
  361. end;
  362. LOC_JUMP : begin
  363. emitlab(truelabel);
  364. emit_const_reg(A_MOV,S_B,1,R_AL);
  365. emitjmp(C_None,aktexit2label);
  366. emitlab(falselabel);
  367. emit_reg_reg(A_XOR,S_B,R_AL,R_AL);
  368. goto do_jmp;
  369. end;
  370. else
  371. internalerror(2001);
  372. end;
  373. case procinfo^.returntype.def^.deftype of
  374. pointerdef,
  375. procvardef : begin
  376. if is_mem then
  377. emit_ref_reg(A_MOV,S_L,
  378. newreference(p^.left^.location.reference),R_EAX)
  379. else
  380. emit_reg_reg(A_MOV,S_L,
  381. p^.left^.location.register,R_EAX);
  382. end;
  383. floatdef : begin
  384. if pfloatdef(procinfo^.returntype.def)^.typ=f32bit then
  385. begin
  386. if is_mem then
  387. emit_ref_reg(A_MOV,S_L,
  388. newreference(p^.left^.location.reference),R_EAX)
  389. else
  390. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,R_EAX);
  391. end
  392. else
  393. if is_mem then
  394. floatload(pfloatdef(procinfo^.returntype.def)^.typ,p^.left^.location.reference);
  395. end;
  396. { orddef,
  397. enumdef : }
  398. else
  399. { it can be anything shorter than 4 bytes PM
  400. this caused form bug 711 }
  401. begin
  402. case procinfo^.returntype.def^.size of
  403. { if its 3 bytes only we can still
  404. copy one of garbage ! PM }
  405. 4,3 : if is_mem then
  406. emit_ref_reg(A_MOV,S_L,
  407. newreference(p^.left^.location.reference),R_EAX)
  408. else
  409. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,R_EAX);
  410. 2 : if is_mem then
  411. emit_ref_reg(A_MOV,S_W,
  412. newreference(p^.left^.location.reference),R_AX)
  413. else
  414. emit_reg_reg(A_MOV,S_W,makereg16(p^.left^.location.register),R_AX);
  415. 1 : if is_mem then
  416. emit_ref_reg(A_MOV,S_B,
  417. newreference(p^.left^.location.reference),R_AL)
  418. else
  419. emit_reg_reg(A_MOV,S_B,makereg8(p^.left^.location.register),R_AL);
  420. end;
  421. end;
  422. end;
  423. do_jmp:
  424. truelabel:=otlabel;
  425. falselabel:=oflabel;
  426. emitjmp(C_None,aktexit2label);
  427. end
  428. else
  429. begin
  430. emitjmp(C_None,aktexitlabel);
  431. end;
  432. end;
  433. {*****************************************************************************
  434. SecondBreakN
  435. *****************************************************************************}
  436. procedure secondbreakn(var p : ptree);
  437. begin
  438. if aktbreaklabel<>nil then
  439. emitjmp(C_None,aktbreaklabel)
  440. else
  441. CGMessage(cg_e_break_not_allowed);
  442. end;
  443. {*****************************************************************************
  444. SecondContinueN
  445. *****************************************************************************}
  446. procedure secondcontinuen(var p : ptree);
  447. begin
  448. if aktcontinuelabel<>nil then
  449. emitjmp(C_None,aktcontinuelabel)
  450. else
  451. CGMessage(cg_e_continue_not_allowed);
  452. end;
  453. {*****************************************************************************
  454. SecondGoto
  455. *****************************************************************************}
  456. procedure secondgoto(var p : ptree);
  457. begin
  458. emitjmp(C_None,p^.labelnr);
  459. { the assigned avoids only crashes if the label isn't defined }
  460. if assigned(p^.labsym) and
  461. assigned(p^.labsym^.code) and
  462. (aktexceptblock<>ptree(p^.labsym^.code)^.exceptionblock) then
  463. CGMessage(cg_e_goto_inout_of_exception_block);
  464. end;
  465. {*****************************************************************************
  466. SecondLabel
  467. *****************************************************************************}
  468. procedure secondlabel(var p : ptree);
  469. begin
  470. emitlab(p^.labelnr);
  471. cleartempgen;
  472. secondpass(p^.left);
  473. end;
  474. {*****************************************************************************
  475. SecondRaise
  476. *****************************************************************************}
  477. procedure secondraise(var p : ptree);
  478. var
  479. a : pasmlabel;
  480. begin
  481. if assigned(p^.left) then
  482. begin
  483. { generate the address }
  484. if assigned(p^.right) then
  485. begin
  486. secondpass(p^.right);
  487. if codegenerror then
  488. exit;
  489. emit_push_loc(p^.right^.location);
  490. end
  491. else
  492. begin
  493. getlabel(a);
  494. emitlab(a);
  495. emit_sym(A_PUSH,S_L,a);
  496. end;
  497. secondpass(p^.left);
  498. if codegenerror then
  499. exit;
  500. case p^.left^.location.loc of
  501. LOC_MEM,LOC_REFERENCE:
  502. emit_ref(A_PUSH,S_L,
  503. newreference(p^.left^.location.reference));
  504. LOC_CREGISTER,LOC_REGISTER : emit_reg(A_PUSH,S_L,
  505. p^.left^.location.register);
  506. else CGMessage(type_e_mismatch);
  507. end;
  508. emitcall('FPC_RAISEEXCEPTION');
  509. end
  510. else
  511. begin
  512. emitcall('FPC_POPADDRSTACK');
  513. emitcall('FPC_RERAISE');
  514. end;
  515. end;
  516. {*****************************************************************************
  517. SecondTryExcept
  518. *****************************************************************************}
  519. var
  520. endexceptlabel : pasmlabel;
  521. procedure secondtryexcept(var p : ptree);
  522. var
  523. exceptlabel,doexceptlabel,oldendexceptlabel,
  524. lastonlabel,
  525. exitexceptlabel,
  526. continueexceptlabel,
  527. breakexceptlabel,
  528. oldaktexitlabel,
  529. oldaktexit2label,
  530. oldaktcontinuelabel,
  531. oldaktbreaklabel : pasmlabel;
  532. oldexceptblock : ptree;
  533. begin
  534. { this can be called recursivly }
  535. oldendexceptlabel:=endexceptlabel;
  536. { we modify EAX }
  537. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  538. oldaktexitlabel:=aktexitlabel;
  539. oldaktexit2label:=aktexit2label;
  540. getlabel(exitexceptlabel);
  541. aktexitlabel:=exitexceptlabel;
  542. aktexit2label:=exitexceptlabel;
  543. if assigned(aktbreaklabel) then
  544. begin
  545. oldaktcontinuelabel:=aktcontinuelabel;
  546. oldaktbreaklabel:=aktbreaklabel;
  547. getlabel(breakexceptlabel);
  548. getlabel(continueexceptlabel);
  549. aktcontinuelabel:=continueexceptlabel;
  550. aktbreaklabel:=breakexceptlabel;
  551. end;
  552. getlabel(exceptlabel);
  553. getlabel(doexceptlabel);
  554. getlabel(endexceptlabel);
  555. getlabel(lastonlabel);
  556. push_int (1); { push type of exceptionframe }
  557. emitcall('FPC_PUSHEXCEPTADDR');
  558. emit_reg(A_PUSH,S_L,R_EAX);
  559. emitcall('FPC_SETJMP');
  560. emit_reg(A_PUSH,S_L,R_EAX);
  561. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  562. emitjmp(C_NE,exceptlabel);
  563. { try code }
  564. oldexceptblock:=aktexceptblock;
  565. aktexceptblock:=p^.left;
  566. secondpass(p^.left);
  567. aktexceptblock:=oldexceptblock;
  568. if codegenerror then
  569. exit;
  570. emitlab(exceptlabel);
  571. emitcall('FPC_POPADDRSTACK');
  572. emit_reg(A_POP,S_L,R_EAX);
  573. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  574. emitjmp(C_E,endexceptlabel);
  575. emitlab(doexceptlabel);
  576. if assigned(p^.right) then
  577. begin
  578. oldexceptblock:=aktexceptblock;
  579. aktexceptblock:=p^.right;
  580. secondpass(p^.right);
  581. aktexceptblock:=oldexceptblock;
  582. end;
  583. emitlab(lastonlabel);
  584. { default handling }
  585. if assigned(p^.t1) then
  586. begin
  587. { FPC_CATCHES must be called with
  588. 'default handler' flag (=-1)
  589. }
  590. push_int (-1);
  591. emitcall('FPC_CATCHES');
  592. if assigned(p^.t1) then
  593. begin
  594. maybe_loadesi;
  595. oldexceptblock:=aktexceptblock;
  596. aktexceptblock:=p^.t1;
  597. secondpass(p^.t1);
  598. aktexceptblock:=oldexceptblock;
  599. end;
  600. emitcall('FPC_POPOBJECTSTACK');
  601. maybe_loadesi;
  602. end
  603. else
  604. emitcall('FPC_RERAISE');
  605. { do some magic for exit in the try block }
  606. emitlab(exitexceptlabel);
  607. emitcall('FPC_POPADDRSTACK');
  608. emit_reg(A_POP,S_L,R_EAX);
  609. emitjmp(C_None,oldaktexitlabel);
  610. if assigned(aktbreaklabel) then
  611. begin
  612. emitlab(breakexceptlabel);
  613. emitcall('FPC_POPADDRSTACK');
  614. emit_reg(A_POP,S_L,R_EAX);
  615. emitjmp(C_None,oldaktbreaklabel);
  616. emitlab(continueexceptlabel);
  617. emitcall('FPC_POPADDRSTACK');
  618. emit_reg(A_POP,S_L,R_EAX);
  619. emitjmp(C_None,oldaktcontinuelabel);
  620. end;
  621. emitlab(endexceptlabel);
  622. endexceptlabel:=oldendexceptlabel;
  623. aktexitlabel:=oldaktexitlabel;
  624. aktexit2label:=oldaktexit2label;
  625. if assigned(aktbreaklabel) then
  626. begin
  627. aktcontinuelabel:=oldaktcontinuelabel;
  628. aktbreaklabel:=oldaktbreaklabel;
  629. end;
  630. end;
  631. procedure secondon(var p : ptree);
  632. var
  633. nextonlabel : pasmlabel;
  634. ref : treference;
  635. oldexceptblock : ptree;
  636. begin
  637. getlabel(nextonlabel);
  638. { push the vmt }
  639. emit_sym(A_PUSH,S_L,
  640. newasmsymbol(p^.excepttype^.vmt_mangledname));
  641. emitcall('FPC_CATCHES');
  642. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  643. emitjmp(C_E,nextonlabel);
  644. ref.symbol:=nil;
  645. gettempofsizereference(4,ref);
  646. { what a hack ! }
  647. if assigned(p^.exceptsymtable) then
  648. pvarsym(p^.exceptsymtable^.symindex^.first)^.address:=ref.offset;
  649. emit_reg_ref(A_MOV,S_L,
  650. R_EAX,newreference(ref));
  651. if assigned(p^.right) then
  652. begin
  653. { esi is destroyed by FPC_CATCHES }
  654. maybe_loadesi;
  655. oldexceptblock:=aktexceptblock;
  656. aktexceptblock:=p^.right;
  657. secondpass(p^.right);
  658. aktexceptblock:=oldexceptblock;
  659. end;
  660. emit_ref(A_PUSH,S_L,newreference(ref));
  661. emitcall('FPC_DESTROYEXCEPTION');
  662. emitcall('FPC_POPOBJECTSTACK');
  663. maybe_loadesi;
  664. { clear some stuff }
  665. ungetiftemp(ref);
  666. emitjmp(C_None,endexceptlabel);
  667. emitlab(nextonlabel);
  668. { next on node }
  669. if assigned(p^.left) then
  670. secondpass(p^.left);
  671. end;
  672. {*****************************************************************************
  673. SecondTryFinally
  674. *****************************************************************************}
  675. procedure secondtryfinally(var p : ptree);
  676. var
  677. reraiselabel,
  678. finallylabel,
  679. endfinallylabel,
  680. exitfinallylabel,
  681. continuefinallylabel,
  682. breakfinallylabel,
  683. oldaktexitlabel,
  684. oldaktexit2label,
  685. oldaktcontinuelabel,
  686. oldaktbreaklabel : pasmlabel;
  687. oldexceptblock : ptree;
  688. begin
  689. { we modify EAX }
  690. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  691. getlabel(finallylabel);
  692. getlabel(endfinallylabel);
  693. oldaktexitlabel:=aktexitlabel;
  694. oldaktexit2label:=aktexit2label;
  695. getlabel(reraiselabel);
  696. getlabel(exitfinallylabel);
  697. aktexitlabel:=exitfinallylabel;
  698. aktexit2label:=exitfinallylabel;
  699. if assigned(aktbreaklabel) then
  700. begin
  701. oldaktcontinuelabel:=aktcontinuelabel;
  702. oldaktbreaklabel:=aktbreaklabel;
  703. getlabel(breakfinallylabel);
  704. getlabel(continuefinallylabel);
  705. aktcontinuelabel:=continuefinallylabel;
  706. aktbreaklabel:=breakfinallylabel;
  707. end;
  708. push_int(1); { Type of stack-frame must be pushed}
  709. emitcall('FPC_PUSHEXCEPTADDR');
  710. emit_reg(A_PUSH,S_L,R_EAX);
  711. emitcall('FPC_SETJMP');
  712. emit_reg(A_PUSH,S_L,R_EAX);
  713. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  714. emitjmp(C_NE,finallylabel);
  715. { try code }
  716. if assigned(p^.left) then
  717. begin
  718. oldexceptblock:=aktexceptblock;
  719. aktexceptblock:=p^.left;
  720. secondpass(p^.left);
  721. if codegenerror then
  722. exit;
  723. aktexceptblock:=oldexceptblock;
  724. end;
  725. emitlab(finallylabel);
  726. emitcall('FPC_POPADDRSTACK');
  727. { finally code }
  728. oldexceptblock:=aktexceptblock;
  729. aktexceptblock:=p^.right;
  730. secondpass(p^.right);
  731. aktexceptblock:=oldexceptblock;
  732. if codegenerror then
  733. exit;
  734. emit_reg(A_POP,S_L,R_EAX);
  735. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  736. emitjmp(C_E,endfinallylabel);
  737. emit_reg(A_DEC,S_L,R_EAX);
  738. emitjmp(C_Z,reraiselabel);
  739. emit_reg(A_DEC,S_L,R_EAX);
  740. emitjmp(C_Z,oldaktexitlabel);
  741. if assigned(aktbreaklabel) then
  742. begin
  743. emit_reg(A_DEC,S_L,R_EAX);
  744. emitjmp(C_Z,oldaktbreaklabel);
  745. emit_reg(A_DEC,S_L,R_EAX);
  746. emitjmp(C_Z,oldaktcontinuelabel);
  747. end;
  748. emitlab(reraiselabel);
  749. emitcall('FPC_RERAISE');
  750. { do some magic for exit,break,continue in the try block }
  751. emitlab(exitfinallylabel);
  752. emit_reg(A_POP,S_L,R_EAX);
  753. emit_const(A_PUSH,S_L,2);
  754. emitjmp(C_NONE,finallylabel);
  755. if assigned(aktbreaklabel) then
  756. begin
  757. emitlab(breakfinallylabel);
  758. emit_reg(A_POP,S_L,R_EAX);
  759. emit_const(A_PUSH,S_L,3);
  760. emitjmp(C_NONE,finallylabel);
  761. emitlab(continuefinallylabel);
  762. emit_reg(A_POP,S_L,R_EAX);
  763. emit_const(A_PUSH,S_L,4);
  764. emitjmp(C_NONE,finallylabel);
  765. end;
  766. emitlab(endfinallylabel);
  767. aktexitlabel:=oldaktexitlabel;
  768. aktexit2label:=oldaktexit2label;
  769. if assigned(aktbreaklabel) then
  770. begin
  771. aktcontinuelabel:=oldaktcontinuelabel;
  772. aktbreaklabel:=oldaktbreaklabel;
  773. end;
  774. end;
  775. {*****************************************************************************
  776. SecondFail
  777. *****************************************************************************}
  778. procedure secondfail(var p : ptree);
  779. begin
  780. emitjmp(C_None,faillabel);
  781. end;
  782. end.
  783. {
  784. $Log$
  785. Revision 1.66 2000-01-07 01:14:20 peter
  786. * updated copyright to 2000
  787. Revision 1.65 1999/12/22 23:30:06 peter
  788. * nested try blocks work again
  789. Revision 1.64 1999/12/22 01:01:46 peter
  790. - removed freelabel()
  791. * added undefined label detection in internal assembler, this prevents
  792. a lot of ld crashes and wrong .o files
  793. * .o files aren't written anymore if errors have occured
  794. * inlining of assembler labels is now correct
  795. Revision 1.63 1999/12/19 17:02:45 peter
  796. * support exit,break,continue in try...except
  797. * support break,continue in try...finally
  798. Revision 1.62 1999/12/17 11:20:06 florian
  799. * made the goto checking for excpetions more fool proof against errors
  800. Revision 1.61 1999/12/14 09:58:41 florian
  801. + compiler checks now if a goto leaves an exception block
  802. Revision 1.60 1999/12/01 12:36:23 peter
  803. * fixed selfpointer after destroyexception
  804. Revision 1.59 1999/11/30 10:40:42 peter
  805. + ttype, tsymlist
  806. Revision 1.58 1999/11/28 23:15:23 pierre
  807. * fix for form bug 721
  808. Revision 1.57 1999/11/15 21:49:09 peter
  809. * push address also for raise at
  810. Revision 1.56 1999/11/06 14:34:17 peter
  811. * truncated log to 20 revs
  812. Revision 1.55 1999/10/30 17:35:26 peter
  813. * fpc_freemem fpc_getmem new callings updated
  814. Revision 1.54 1999/10/21 16:41:37 florian
  815. * problems with readln fixed: esi wasn't restored correctly when
  816. reading ordinal fields of objects futher the register allocation
  817. didn't take care of the extra register when reading ordinal values
  818. * enumerations can now be used in constant indexes of properties
  819. Revision 1.53 1999/10/05 22:01:52 pierre
  820. * bug exit('test') + fail for classes
  821. Revision 1.52 1999/09/27 23:44:46 peter
  822. * procinfo is now a pointer
  823. * support for result setting in sub procedure
  824. Revision 1.51 1999/09/26 13:26:05 florian
  825. * exception patch of Romio nevertheless the excpetion handling
  826. needs some corections regarding register saving
  827. * gettempansistring is again a procedure
  828. Revision 1.50 1999/09/20 16:35:43 peter
  829. * restored old alignment, saves 40k on ppc386
  830. Revision 1.49 1999/09/15 20:35:37 florian
  831. * small fix to operator overloading when in MMX mode
  832. + the compiler uses now fldz and fld1 if possible
  833. + some fixes to floating point registers
  834. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  835. * .... ???
  836. Revision 1.48 1999/09/07 07:56:37 peter
  837. * reload esi in except block to allow virtual methods
  838. Revision 1.47 1999/08/25 11:59:42 jonas
  839. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  840. Revision 1.46 1999/08/19 13:06:47 pierre
  841. * _FAIL will now free memory correctly
  842. and reset VMT field on static instances
  843. (not yet tested for classes, is it allowed ?)
  844. + emit_.... all variant defined here to avoid tons of
  845. exprasmlist^.concat(new(paicpu,...) in cg386***
  846. Revision 1.45 1999/08/04 00:22:46 florian
  847. * renamed i386asm and i386base to cpuasm and cpubase
  848. Revision 1.44 1999/08/03 22:02:39 peter
  849. * moved bitmask constants to sets
  850. * some other type/const renamings
  851. Revision 1.43 1999/07/26 12:13:45 florian
  852. * exit in try..finally blocks needed a second fix
  853. * a raise in a try..finally lead into a endless loop, fixed
  854. Revision 1.42 1999/07/26 09:41:59 florian
  855. * bugs 494-496 fixed
  856. Revision 1.41 1999/07/05 20:13:09 peter
  857. * removed temp defines
  858. Revision 1.40 1999/06/14 00:43:35 peter
  859. * merged
  860. Revision 1.39.2.1 1999/06/14 00:39:29 peter
  861. * don't pop object stack in catches, because it's needed for reraise
  862. Revision 1.39 1999/05/27 19:44:12 peter
  863. * removed oldasm
  864. * plabel -> pasmlabel
  865. * -a switches to source writing automaticly
  866. * assembler readers OOPed
  867. * asmsymbol automaticly external
  868. * jumptables and other label fixes for asm readers
  869. Revision 1.38 1999/05/21 13:54:48 peter
  870. * NEWLAB for label as symbol
  871. Revision 1.37 1999/05/17 21:57:01 florian
  872. * new temporary ansistring handling
  873. }