cg386flw.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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. {$i defines.inc}
  20. interface
  21. uses
  22. tree;
  23. procedure second_while_repeatn(var p : ptree);
  24. procedure secondifn(var p : ptree);
  25. procedure secondfor(var p : ptree);
  26. procedure secondexitn(var p : ptree);
  27. procedure secondbreakn(var p : ptree);
  28. procedure secondcontinuen(var p : ptree);
  29. procedure secondgoto(var p : ptree);
  30. procedure secondlabel(var p : ptree);
  31. procedure secondraise(var p : ptree);
  32. procedure secondtryexcept(var p : ptree);
  33. procedure secondtryfinally(var p : ptree);
  34. procedure secondon(var p : ptree);
  35. procedure secondfail(var p : ptree);
  36. type
  37. tenumflowcontrol = (fc_exit,fc_break,fc_continue);
  38. tflowcontrol = set of tenumflowcontrol;
  39. var
  40. flowcontrol : tflowcontrol;
  41. implementation
  42. uses
  43. cobjects,verbose,globtype,globals,systems,
  44. symconst,symtable,aasm,types,
  45. hcodegen,temp_gen,pass_2,
  46. cpubase,cpuasm,
  47. cgai386,tgeni386,tcflw;
  48. {*****************************************************************************
  49. Second_While_RepeatN
  50. *****************************************************************************}
  51. procedure second_while_repeatn(var p : ptree);
  52. var
  53. lcont,lbreak,lloop,
  54. oldclabel,oldblabel : pasmlabel;
  55. otlabel,oflabel : pasmlabel;
  56. begin
  57. getlabel(lloop);
  58. getlabel(lcont);
  59. getlabel(lbreak);
  60. { arrange continue and breaklabels: }
  61. oldclabel:=aktcontinuelabel;
  62. oldblabel:=aktbreaklabel;
  63. { handling code at the end as it is much more efficient, and makes
  64. while equal to repeat loop, only the end true/false is swapped (PFV) }
  65. if p^.treetype=whilen then
  66. emitjmp(C_None,lcont);
  67. emitlab(lloop);
  68. aktcontinuelabel:=lcont;
  69. aktbreaklabel:=lbreak;
  70. cleartempgen;
  71. if assigned(p^.right) then
  72. secondpass(p^.right);
  73. emitlab(lcont);
  74. otlabel:=truelabel;
  75. oflabel:=falselabel;
  76. if p^.treetype=whilen then
  77. begin
  78. truelabel:=lloop;
  79. falselabel:=lbreak;
  80. end
  81. { repeatn }
  82. else
  83. begin
  84. truelabel:=lbreak;
  85. falselabel:=lloop;
  86. end;
  87. cleartempgen;
  88. secondpass(p^.left);
  89. maketojumpbool(p^.left);
  90. emitlab(lbreak);
  91. truelabel:=otlabel;
  92. falselabel:=oflabel;
  93. aktcontinuelabel:=oldclabel;
  94. aktbreaklabel:=oldblabel;
  95. { a break/continue in a while/repeat block can't be seen outside }
  96. flowcontrol:=flowcontrol-[fc_break,fc_continue];
  97. end;
  98. {*****************************************************************************
  99. SecondIfN
  100. *****************************************************************************}
  101. procedure secondifn(var p : ptree);
  102. var
  103. hl,otlabel,oflabel : pasmlabel;
  104. begin
  105. otlabel:=truelabel;
  106. oflabel:=falselabel;
  107. getlabel(truelabel);
  108. getlabel(falselabel);
  109. cleartempgen;
  110. secondpass(p^.left);
  111. maketojumpbool(p^.left);
  112. if assigned(p^.right) then
  113. begin
  114. emitlab(truelabel);
  115. cleartempgen;
  116. secondpass(p^.right);
  117. end;
  118. if assigned(p^.t1) then
  119. begin
  120. if assigned(p^.right) then
  121. begin
  122. getlabel(hl);
  123. { do go back to if line !! }
  124. aktfilepos:=exprasmlist^.getlasttaifilepos^;
  125. emitjmp(C_None,hl);
  126. end;
  127. emitlab(falselabel);
  128. cleartempgen;
  129. secondpass(p^.t1);
  130. if assigned(p^.right) then
  131. emitlab(hl);
  132. end
  133. else
  134. begin
  135. emitlab(falselabel);
  136. end;
  137. if not(assigned(p^.right)) then
  138. begin
  139. emitlab(truelabel);
  140. end;
  141. truelabel:=otlabel;
  142. falselabel:=oflabel;
  143. end;
  144. {*****************************************************************************
  145. SecondFor
  146. *****************************************************************************}
  147. procedure secondfor(var p : ptree);
  148. var
  149. l3,oldclabel,oldblabel : pasmlabel;
  150. omitfirstcomp,temptovalue : boolean;
  151. hs : byte;
  152. temp1 : treference;
  153. hop : tasmop;
  154. hcond : tasmcond;
  155. cmpreg,cmp32 : tregister;
  156. opsize : topsize;
  157. count_var_is_signed : boolean;
  158. begin
  159. oldclabel:=aktcontinuelabel;
  160. oldblabel:=aktbreaklabel;
  161. getlabel(aktcontinuelabel);
  162. getlabel(aktbreaklabel);
  163. getlabel(l3);
  164. { could we spare the first comparison ? }
  165. omitfirstcomp:=false;
  166. if p^.right^.treetype=ordconstn then
  167. if p^.left^.right^.treetype=ordconstn then
  168. omitfirstcomp:=(p^.backward and (p^.left^.right^.value>=p^.right^.value))
  169. or (not(p^.backward) and (p^.left^.right^.value<=p^.right^.value));
  170. { only calculate reference }
  171. cleartempgen;
  172. secondpass(p^.t2);
  173. hs:=p^.t2^.resulttype^.size;
  174. if p^.t2^.location.loc <> LOC_CREGISTER then
  175. cmp32:=getregister32;
  176. case hs of
  177. 1 : begin
  178. opsize:=S_B;
  179. if p^.t2^.location.loc <> LOC_CREGISTER then
  180. cmpreg:=reg32toreg8(cmp32);
  181. end;
  182. 2 : begin
  183. opsize:=S_W;
  184. if p^.t2^.location.loc <> LOC_CREGISTER then
  185. cmpreg:=reg32toreg16(cmp32);
  186. end;
  187. 4 : begin
  188. opsize:=S_L;
  189. if p^.t2^.location.loc <> LOC_CREGISTER then
  190. cmpreg:=cmp32;
  191. end;
  192. end;
  193. { first set the to value
  194. because the count var can be in the expression !! }
  195. cleartempgen;
  196. secondpass(p^.right);
  197. { calculate pointer value and check if changeable and if so }
  198. { load into temporary variable }
  199. if p^.right^.treetype<>ordconstn then
  200. begin
  201. temp1.symbol:=nil;
  202. gettempofsizereference(hs,temp1);
  203. temptovalue:=true;
  204. if (p^.right^.location.loc=LOC_REGISTER) or
  205. (p^.right^.location.loc=LOC_CREGISTER) then
  206. begin
  207. emit_reg_ref(A_MOV,opsize,p^.right^.location.register,
  208. newreference(temp1));
  209. end
  210. else
  211. concatcopy(p^.right^.location.reference,temp1,hs,false,false);
  212. end
  213. else
  214. temptovalue:=false;
  215. { produce start assignment }
  216. cleartempgen;
  217. secondpass(p^.left);
  218. count_var_is_signed:=is_signed(porddef(p^.t2^.resulttype));
  219. if temptovalue then
  220. begin
  221. if p^.t2^.location.loc=LOC_CREGISTER then
  222. begin
  223. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  224. p^.t2^.location.register);
  225. end
  226. else
  227. begin
  228. emit_ref_reg(A_MOV,opsize,newreference(p^.t2^.location.reference),
  229. cmpreg);
  230. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  231. cmpreg);
  232. { temp register not necessary anymore currently (JM) }
  233. ungetregister32(cmp32);
  234. end;
  235. end
  236. else
  237. begin
  238. if not(omitfirstcomp) then
  239. begin
  240. if p^.t2^.location.loc=LOC_CREGISTER then
  241. emit_const_reg(A_CMP,opsize,p^.right^.value,
  242. p^.t2^.location.register)
  243. else
  244. emit_const_ref(A_CMP,opsize,p^.right^.value,
  245. newreference(p^.t2^.location.reference));
  246. end;
  247. end;
  248. if p^.backward then
  249. if count_var_is_signed then
  250. hcond:=C_L
  251. else
  252. hcond:=C_B
  253. else
  254. if count_var_is_signed then
  255. hcond:=C_G
  256. else
  257. hcond:=C_A;
  258. if not(omitfirstcomp) or temptovalue then
  259. emitjmp(hcond,aktbreaklabel);
  260. { align loop target }
  261. if not(cs_littlesize in aktglobalswitches) then
  262. exprasmlist^.concat(new(pai_align,init_op(4,$90)));
  263. emitlab(l3);
  264. { help register must not be in instruction block }
  265. cleartempgen;
  266. if assigned(p^.t1) then
  267. secondpass(p^.t1);
  268. emitlab(aktcontinuelabel);
  269. { makes no problems there }
  270. cleartempgen;
  271. if (p^.t2^.location.loc <> LOC_CREGISTER) then
  272. begin
  273. { demand help register again }
  274. cmp32:=getregister32;
  275. case hs of
  276. 1 : cmpreg:=reg32toreg8(cmp32);
  277. 2 : cmpreg:=reg32toreg16(cmp32);
  278. 4 : cmpreg:=cmp32;
  279. end;
  280. end;
  281. { produce comparison and the corresponding }
  282. { jump }
  283. if temptovalue then
  284. begin
  285. if p^.t2^.location.loc=LOC_CREGISTER then
  286. begin
  287. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  288. p^.t2^.location.register);
  289. end
  290. else
  291. begin
  292. emit_ref_reg(A_MOV,opsize,newreference(p^.t2^.location.reference),
  293. cmpreg);
  294. emit_ref_reg(A_CMP,opsize,newreference(temp1),
  295. cmpreg);
  296. end;
  297. end
  298. else
  299. begin
  300. if p^.t2^.location.loc=LOC_CREGISTER then
  301. emit_const_reg(A_CMP,opsize,p^.right^.value,
  302. p^.t2^.location.register)
  303. else
  304. emit_const_ref(A_CMP,opsize,p^.right^.value,
  305. newreference(p^.t2^.location.reference));
  306. end;
  307. if p^.backward then
  308. if count_var_is_signed then
  309. hcond:=C_LE
  310. else
  311. hcond:=C_BE
  312. else
  313. if count_var_is_signed then
  314. hcond:=C_GE
  315. else
  316. hcond:=C_AE;
  317. emitjmp(hcond,aktbreaklabel);
  318. { according to count direction DEC or INC... }
  319. { must be after the test because of 0to 255 for bytes !! }
  320. if p^.backward then
  321. hop:=A_DEC
  322. else
  323. hop:=A_INC;
  324. if p^.t2^.location.loc=LOC_CREGISTER then
  325. emit_reg(hop,opsize,p^.t2^.location.register)
  326. else
  327. emit_ref(hop,opsize,newreference(p^.t2^.location.reference));
  328. emitjmp(C_None,l3);
  329. if (p^.t2^.location.loc <> LOC_CREGISTER) then
  330. ungetregister32(cmp32);
  331. if temptovalue then
  332. ungetiftemp(temp1);
  333. { this is the break label: }
  334. emitlab(aktbreaklabel);
  335. aktcontinuelabel:=oldclabel;
  336. aktbreaklabel:=oldblabel;
  337. { a break/continue in a for block can't be seen outside }
  338. flowcontrol:=flowcontrol-[fc_break,fc_continue];
  339. end;
  340. {*****************************************************************************
  341. SecondExitN
  342. *****************************************************************************}
  343. procedure secondexitn(var p : ptree);
  344. var
  345. is_mem : boolean;
  346. {op : tasmop;
  347. s : topsize;}
  348. otlabel,oflabel : pasmlabel;
  349. r : preference;
  350. label
  351. do_jmp;
  352. begin
  353. include(flowcontrol,fc_exit);
  354. if assigned(p^.left) then
  355. if p^.left^.treetype=assignn then
  356. begin
  357. { just do a normal assignment followed by exit }
  358. secondpass(p^.left);
  359. emitjmp(C_None,aktexitlabel);
  360. end
  361. else
  362. begin
  363. otlabel:=truelabel;
  364. oflabel:=falselabel;
  365. getlabel(truelabel);
  366. getlabel(falselabel);
  367. secondpass(p^.left);
  368. case p^.left^.location.loc of
  369. LOC_FPU : goto do_jmp;
  370. LOC_MEM,
  371. LOC_REFERENCE : is_mem:=true;
  372. LOC_CREGISTER,
  373. LOC_REGISTER : is_mem:=false;
  374. LOC_FLAGS : begin
  375. emit_flag2reg(p^.left^.location.resflags,R_AL);
  376. goto do_jmp;
  377. end;
  378. LOC_JUMP : begin
  379. emitlab(truelabel);
  380. emit_const_reg(A_MOV,S_B,1,R_AL);
  381. emitjmp(C_None,aktexit2label);
  382. emitlab(falselabel);
  383. emit_reg_reg(A_XOR,S_B,R_AL,R_AL);
  384. goto do_jmp;
  385. end;
  386. else
  387. internalerror(2001);
  388. end;
  389. case procinfo^.returntype.def^.deftype of
  390. pointerdef,
  391. procvardef : begin
  392. if is_mem then
  393. emit_ref_reg(A_MOV,S_L,
  394. newreference(p^.left^.location.reference),R_EAX)
  395. else
  396. emit_reg_reg(A_MOV,S_L,
  397. p^.left^.location.register,R_EAX);
  398. end;
  399. floatdef : begin
  400. if pfloatdef(procinfo^.returntype.def)^.typ=f32bit then
  401. begin
  402. if is_mem then
  403. emit_ref_reg(A_MOV,S_L,
  404. newreference(p^.left^.location.reference),R_EAX)
  405. else
  406. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,R_EAX);
  407. end
  408. else
  409. if is_mem then
  410. floatload(pfloatdef(procinfo^.returntype.def)^.typ,p^.left^.location.reference);
  411. end;
  412. { orddef,
  413. enumdef : }
  414. else
  415. { it can be anything shorter than 4 bytes PM
  416. this caused form bug 711 }
  417. begin
  418. case procinfo^.returntype.def^.size of
  419. { it can be a qword/int64 too ... }
  420. 8 : if is_mem then
  421. begin
  422. emit_ref_reg(A_MOV,S_L,
  423. newreference(p^.left^.location.reference),R_EAX);
  424. r:=newreference(p^.left^.location.reference);
  425. inc(r^.offset,4);
  426. emit_ref_reg(A_MOV,S_L,r,R_EDX);
  427. end
  428. else
  429. begin
  430. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,R_EAX);
  431. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerhigh,R_EDX);
  432. end;
  433. { if its 3 bytes only we can still
  434. copy one of garbage ! PM }
  435. 4,3 : if is_mem then
  436. emit_ref_reg(A_MOV,S_L,
  437. newreference(p^.left^.location.reference),R_EAX)
  438. else
  439. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,R_EAX);
  440. 2 : if is_mem then
  441. emit_ref_reg(A_MOV,S_W,
  442. newreference(p^.left^.location.reference),R_AX)
  443. else
  444. emit_reg_reg(A_MOV,S_W,makereg16(p^.left^.location.register),R_AX);
  445. 1 : if is_mem then
  446. emit_ref_reg(A_MOV,S_B,
  447. newreference(p^.left^.location.reference),R_AL)
  448. else
  449. emit_reg_reg(A_MOV,S_B,makereg8(p^.left^.location.register),R_AL);
  450. else internalerror(605001);
  451. end;
  452. end;
  453. end;
  454. do_jmp:
  455. truelabel:=otlabel;
  456. falselabel:=oflabel;
  457. emitjmp(C_None,aktexit2label);
  458. end
  459. else
  460. begin
  461. emitjmp(C_None,aktexitlabel);
  462. end;
  463. end;
  464. {*****************************************************************************
  465. SecondBreakN
  466. *****************************************************************************}
  467. procedure secondbreakn(var p : ptree);
  468. begin
  469. include(flowcontrol,fc_break);
  470. if aktbreaklabel<>nil then
  471. emitjmp(C_None,aktbreaklabel)
  472. else
  473. CGMessage(cg_e_break_not_allowed);
  474. end;
  475. {*****************************************************************************
  476. SecondContinueN
  477. *****************************************************************************}
  478. procedure secondcontinuen(var p : ptree);
  479. begin
  480. include(flowcontrol,fc_continue);
  481. if aktcontinuelabel<>nil then
  482. emitjmp(C_None,aktcontinuelabel)
  483. else
  484. CGMessage(cg_e_continue_not_allowed);
  485. end;
  486. {*****************************************************************************
  487. SecondGoto
  488. *****************************************************************************}
  489. procedure secondgoto(var p : ptree);
  490. begin
  491. emitjmp(C_None,p^.labelnr);
  492. { the assigned avoids only crashes if the label isn't defined }
  493. if assigned(p^.labsym) and
  494. assigned(p^.labsym^.code) and
  495. (aktexceptblock<>ptree(p^.labsym^.code)^.exceptionblock) then
  496. CGMessage(cg_e_goto_inout_of_exception_block);
  497. end;
  498. {*****************************************************************************
  499. SecondLabel
  500. *****************************************************************************}
  501. procedure secondlabel(var p : ptree);
  502. begin
  503. emitlab(p^.labelnr);
  504. cleartempgen;
  505. secondpass(p^.left);
  506. end;
  507. {*****************************************************************************
  508. SecondRaise
  509. *****************************************************************************}
  510. procedure secondraise(var p : ptree);
  511. var
  512. a : pasmlabel;
  513. begin
  514. if assigned(p^.left) then
  515. begin
  516. { multiple parameters? }
  517. if assigned(p^.right) then
  518. begin
  519. { push frame }
  520. if assigned(p^.frametree) then
  521. begin
  522. secondpass(p^.frametree);
  523. if codegenerror then
  524. exit;
  525. emit_push_loc(p^.frametree^.location);
  526. end
  527. else
  528. emit_const(A_PUSH,S_L,0);
  529. { push address }
  530. secondpass(p^.right);
  531. if codegenerror then
  532. exit;
  533. emit_push_loc(p^.right^.location);
  534. end
  535. else
  536. begin
  537. getaddrlabel(a);
  538. emitlab(a);
  539. emit_reg(A_PUSH,S_L,R_EBP);
  540. emit_sym(A_PUSH,S_L,a);
  541. end;
  542. { push object }
  543. secondpass(p^.left);
  544. if codegenerror then
  545. exit;
  546. emit_push_loc(p^.left^.location);
  547. emitcall('FPC_RAISEEXCEPTION');
  548. end
  549. else
  550. begin
  551. emitcall('FPC_POPADDRSTACK');
  552. emitcall('FPC_RERAISE');
  553. end;
  554. end;
  555. {*****************************************************************************
  556. SecondTryExcept
  557. *****************************************************************************}
  558. var
  559. endexceptlabel : pasmlabel;
  560. { does the necessary things to clean up the object stack }
  561. { in the except block }
  562. procedure cleanupobjectstack;
  563. begin
  564. emitcall('FPC_POPOBJECTSTACK');
  565. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  566. emit_reg(A_PUSH,S_L,R_EAX);
  567. emitcall('FPC_DESTROYEXCEPTION');
  568. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  569. maybe_loadesi;
  570. end;
  571. { pops one element from the exception address stack }
  572. { and removes the flag }
  573. procedure cleanupaddrstack;
  574. begin
  575. emitcall('FPC_POPADDRSTACK');
  576. { allocate eax }
  577. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  578. emit_reg(A_POP,S_L,R_EAX);
  579. { deallocate eax }
  580. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  581. end;
  582. procedure secondtryexcept(var p : ptree);
  583. var
  584. exceptlabel,doexceptlabel,oldendexceptlabel,
  585. lastonlabel,
  586. exitexceptlabel,
  587. continueexceptlabel,
  588. breakexceptlabel,
  589. exittrylabel,
  590. continuetrylabel,
  591. breaktrylabel,
  592. doobjectdestroy,
  593. doobjectdestroyandreraise,
  594. oldaktexitlabel,
  595. oldaktexit2label,
  596. oldaktcontinuelabel,
  597. oldaktbreaklabel : pasmlabel;
  598. oldexceptblock : ptree;
  599. oldflowcontrol,tryflowcontrol,
  600. exceptflowcontrol : tflowcontrol;
  601. label
  602. errorexit;
  603. begin
  604. oldflowcontrol:=flowcontrol;
  605. flowcontrol:=[];
  606. { this can be called recursivly }
  607. oldendexceptlabel:=endexceptlabel;
  608. { we modify EAX }
  609. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  610. { save the old labels for control flow statements }
  611. oldaktexitlabel:=aktexitlabel;
  612. oldaktexit2label:=aktexit2label;
  613. if assigned(aktbreaklabel) then
  614. begin
  615. oldaktcontinuelabel:=aktcontinuelabel;
  616. oldaktbreaklabel:=aktbreaklabel;
  617. end;
  618. { get new labels for the control flow statements }
  619. getlabel(exittrylabel);
  620. getlabel(exitexceptlabel);
  621. if assigned(aktbreaklabel) then
  622. begin
  623. getlabel(breaktrylabel);
  624. getlabel(continuetrylabel);
  625. getlabel(breakexceptlabel);
  626. getlabel(continueexceptlabel);
  627. end;
  628. getlabel(exceptlabel);
  629. getlabel(doexceptlabel);
  630. getlabel(endexceptlabel);
  631. getlabel(lastonlabel);
  632. push_int (1); { push type of exceptionframe }
  633. emitcall('FPC_PUSHEXCEPTADDR');
  634. { allocate eax }
  635. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  636. emit_reg(A_PUSH,S_L,R_EAX);
  637. emitcall('FPC_SETJMP');
  638. emit_reg(A_PUSH,S_L,R_EAX);
  639. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  640. { deallocate eax }
  641. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  642. emitjmp(C_NE,exceptlabel);
  643. { try block }
  644. { set control flow labels for the try block }
  645. aktexitlabel:=exittrylabel;
  646. aktexit2label:=exittrylabel;
  647. if assigned(oldaktbreaklabel) then
  648. begin
  649. aktcontinuelabel:=continuetrylabel;
  650. aktbreaklabel:=breaktrylabel;
  651. end;
  652. oldexceptblock:=aktexceptblock;
  653. aktexceptblock:=p^.left;
  654. flowcontrol:=[];
  655. secondpass(p^.left);
  656. tryflowcontrol:=flowcontrol;
  657. aktexceptblock:=oldexceptblock;
  658. if codegenerror then
  659. goto errorexit;
  660. emitlab(exceptlabel);
  661. emitcall('FPC_POPADDRSTACK');
  662. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  663. emit_reg(A_POP,S_L,R_EAX);
  664. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  665. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  666. emitjmp(C_E,endexceptlabel);
  667. emitlab(doexceptlabel);
  668. { set control flow labels for the except block }
  669. { and the on statements }
  670. aktexitlabel:=exitexceptlabel;
  671. aktexit2label:=exitexceptlabel;
  672. if assigned(oldaktbreaklabel) then
  673. begin
  674. aktcontinuelabel:=continueexceptlabel;
  675. aktbreaklabel:=breakexceptlabel;
  676. end;
  677. flowcontrol:=[];
  678. { on statements }
  679. if assigned(p^.right) then
  680. begin
  681. oldexceptblock:=aktexceptblock;
  682. aktexceptblock:=p^.right;
  683. secondpass(p^.right);
  684. aktexceptblock:=oldexceptblock;
  685. end;
  686. emitlab(lastonlabel);
  687. { default handling except handling }
  688. if assigned(p^.t1) then
  689. begin
  690. { FPC_CATCHES must be called with
  691. 'default handler' flag (=-1)
  692. }
  693. push_int (-1);
  694. emitcall('FPC_CATCHES');
  695. maybe_loadesi;
  696. { the destruction of the exception object must be also }
  697. { guarded by an exception frame }
  698. getlabel(doobjectdestroy);
  699. getlabel(doobjectdestroyandreraise);
  700. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,1)));
  701. emitcall('FPC_PUSHEXCEPTADDR');
  702. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  703. exprasmlist^.concat(new(paicpu,
  704. op_reg(A_PUSH,S_L,R_EAX)));
  705. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  706. emitcall('FPC_SETJMP');
  707. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  708. exprasmlist^.concat(new(paicpu,
  709. op_reg(A_PUSH,S_L,R_EAX)));
  710. exprasmlist^.concat(new(paicpu,
  711. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  712. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  713. emitjmp(C_NE,doobjectdestroyandreraise);
  714. oldexceptblock:=aktexceptblock;
  715. aktexceptblock:=p^.t1;
  716. { here we don't have to reset flowcontrol }
  717. { the default and on flowcontrols are handled equal }
  718. secondpass(p^.t1);
  719. exceptflowcontrol:=flowcontrol;
  720. aktexceptblock:=oldexceptblock;
  721. emitlab(doobjectdestroyandreraise);
  722. emitcall('FPC_POPADDRSTACK');
  723. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  724. exprasmlist^.concat(new(paicpu,
  725. op_reg(A_POP,S_L,R_EAX)));
  726. exprasmlist^.concat(new(paicpu,
  727. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  728. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  729. emitjmp(C_E,doobjectdestroy);
  730. emitcall('FPC_POPSECONDOBJECTSTACK');
  731. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  732. emit_reg(A_PUSH,S_L,R_EAX);
  733. emitcall('FPC_DESTROYEXCEPTION');
  734. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  735. { we don't need to restore esi here because reraise never }
  736. { returns }
  737. emitcall('FPC_RERAISE');
  738. emitlab(doobjectdestroy);
  739. cleanupobjectstack;
  740. emitjmp(C_None,endexceptlabel);
  741. end
  742. else
  743. begin
  744. emitcall('FPC_RERAISE');
  745. exceptflowcontrol:=flowcontrol;
  746. end;
  747. if fc_exit in exceptflowcontrol then
  748. begin
  749. { do some magic for exit in the try block }
  750. emitlab(exitexceptlabel);
  751. { we must also destroy the address frame which guards }
  752. { exception object }
  753. cleanupaddrstack;
  754. cleanupobjectstack;
  755. emitjmp(C_None,oldaktexitlabel);
  756. end;
  757. if fc_break in exceptflowcontrol then
  758. begin
  759. emitlab(breakexceptlabel);
  760. { we must also destroy the address frame which guards }
  761. { exception object }
  762. cleanupaddrstack;
  763. cleanupobjectstack;
  764. emitjmp(C_None,oldaktbreaklabel);
  765. end;
  766. if fc_continue in exceptflowcontrol then
  767. begin
  768. emitlab(continueexceptlabel);
  769. { we must also destroy the address frame which guards }
  770. { exception object }
  771. cleanupaddrstack;
  772. cleanupobjectstack;
  773. emitjmp(C_None,oldaktcontinuelabel);
  774. end;
  775. if fc_exit in tryflowcontrol then
  776. begin
  777. { do some magic for exit in the try block }
  778. emitlab(exittrylabel);
  779. cleanupaddrstack;
  780. emitjmp(C_None,oldaktexitlabel);
  781. end;
  782. if fc_break in tryflowcontrol then
  783. begin
  784. emitlab(breaktrylabel);
  785. cleanupaddrstack;
  786. emitjmp(C_None,oldaktbreaklabel);
  787. end;
  788. if fc_continue in tryflowcontrol then
  789. begin
  790. emitlab(continuetrylabel);
  791. cleanupaddrstack;
  792. emitjmp(C_None,oldaktcontinuelabel);
  793. end;
  794. emitlab(endexceptlabel);
  795. errorexit:
  796. { restore all saved labels }
  797. endexceptlabel:=oldendexceptlabel;
  798. { restore the control flow labels }
  799. aktexitlabel:=oldaktexitlabel;
  800. aktexit2label:=oldaktexit2label;
  801. if assigned(oldaktbreaklabel) then
  802. begin
  803. aktcontinuelabel:=oldaktcontinuelabel;
  804. aktbreaklabel:=oldaktbreaklabel;
  805. end;
  806. { return all used control flow statements }
  807. flowcontrol:=oldflowcontrol+exceptflowcontrol+
  808. tryflowcontrol;
  809. end;
  810. procedure secondon(var p : ptree);
  811. var
  812. nextonlabel,
  813. exitonlabel,
  814. continueonlabel,
  815. breakonlabel,
  816. oldaktexitlabel,
  817. oldaktexit2label,
  818. oldaktcontinuelabel,
  819. doobjectdestroyandreraise,
  820. doobjectdestroy,
  821. oldaktbreaklabel : pasmlabel;
  822. ref : treference;
  823. oldexceptblock : ptree;
  824. oldflowcontrol : tflowcontrol;
  825. begin
  826. oldflowcontrol:=flowcontrol;
  827. flowcontrol:=[];
  828. getlabel(nextonlabel);
  829. { push the vmt }
  830. emit_sym(A_PUSH,S_L,
  831. newasmsymbol(p^.excepttype^.vmt_mangledname));
  832. emitcall('FPC_CATCHES');
  833. { allocate eax }
  834. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  835. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  836. emitjmp(C_E,nextonlabel);
  837. ref.symbol:=nil;
  838. gettempofsizereference(4,ref);
  839. { what a hack ! }
  840. if assigned(p^.exceptsymtable) then
  841. pvarsym(p^.exceptsymtable^.symindex^.first)^.address:=ref.offset;
  842. emit_reg_ref(A_MOV,S_L,
  843. R_EAX,newreference(ref));
  844. { deallocate eax }
  845. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  846. { in the case that another exception is risen }
  847. { we've to destroy the old one }
  848. getlabel(doobjectdestroyandreraise);
  849. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,1)));
  850. emitcall('FPC_PUSHEXCEPTADDR');
  851. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  852. exprasmlist^.concat(new(paicpu,
  853. op_reg(A_PUSH,S_L,R_EAX)));
  854. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  855. emitcall('FPC_SETJMP');
  856. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  857. exprasmlist^.concat(new(paicpu,
  858. op_reg(A_PUSH,S_L,R_EAX)));
  859. exprasmlist^.concat(new(paicpu,
  860. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  861. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  862. emitjmp(C_NE,doobjectdestroyandreraise);
  863. if assigned(p^.right) then
  864. begin
  865. oldaktexitlabel:=aktexitlabel;
  866. oldaktexit2label:=aktexit2label;
  867. getlabel(exitonlabel);
  868. aktexitlabel:=exitonlabel;
  869. aktexit2label:=exitonlabel;
  870. if assigned(aktbreaklabel) then
  871. begin
  872. oldaktcontinuelabel:=aktcontinuelabel;
  873. oldaktbreaklabel:=aktbreaklabel;
  874. getlabel(breakonlabel);
  875. getlabel(continueonlabel);
  876. aktcontinuelabel:=continueonlabel;
  877. aktbreaklabel:=breakonlabel;
  878. end;
  879. { esi is destroyed by FPC_CATCHES }
  880. maybe_loadesi;
  881. oldexceptblock:=aktexceptblock;
  882. aktexceptblock:=p^.right;
  883. secondpass(p^.right);
  884. aktexceptblock:=oldexceptblock;
  885. end;
  886. getlabel(doobjectdestroy);
  887. emitlab(doobjectdestroyandreraise);
  888. emitcall('FPC_POPADDRSTACK');
  889. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  890. exprasmlist^.concat(new(paicpu,
  891. op_reg(A_POP,S_L,R_EAX)));
  892. exprasmlist^.concat(new(paicpu,
  893. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  894. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  895. emitjmp(C_E,doobjectdestroy);
  896. emitcall('FPC_POPSECONDOBJECTSTACK');
  897. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  898. emit_reg(A_PUSH,S_L,R_EAX);
  899. emitcall('FPC_DESTROYEXCEPTION');
  900. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  901. { we don't need to restore esi here because reraise never }
  902. { returns }
  903. emitcall('FPC_RERAISE');
  904. emitlab(doobjectdestroy);
  905. cleanupobjectstack;
  906. { clear some stuff }
  907. ungetiftemp(ref);
  908. emitjmp(C_None,endexceptlabel);
  909. if assigned(p^.right) then
  910. begin
  911. { special handling for control flow instructions }
  912. if fc_exit in flowcontrol then
  913. begin
  914. { the address and object pop does secondtryexcept }
  915. emitlab(exitonlabel);
  916. emitjmp(C_None,oldaktexitlabel);
  917. end;
  918. if fc_break in flowcontrol then
  919. begin
  920. { the address and object pop does secondtryexcept }
  921. emitlab(breakonlabel);
  922. emitjmp(C_None,oldaktbreaklabel);
  923. end;
  924. if fc_continue in flowcontrol then
  925. begin
  926. { the address and object pop does secondtryexcept }
  927. emitlab(continueonlabel);
  928. emitjmp(C_None,oldaktcontinuelabel);
  929. end;
  930. aktexitlabel:=oldaktexitlabel;
  931. aktexit2label:=oldaktexit2label;
  932. if assigned(oldaktbreaklabel) then
  933. begin
  934. aktcontinuelabel:=oldaktcontinuelabel;
  935. aktbreaklabel:=oldaktbreaklabel;
  936. end;
  937. end;
  938. emitlab(nextonlabel);
  939. flowcontrol:=oldflowcontrol+flowcontrol;
  940. { next on node }
  941. if assigned(p^.left) then
  942. begin
  943. cleartempgen;
  944. secondpass(p^.left);
  945. end;
  946. end;
  947. {*****************************************************************************
  948. SecondTryFinally
  949. *****************************************************************************}
  950. procedure secondtryfinally(var p : ptree);
  951. var
  952. reraiselabel,
  953. finallylabel,
  954. endfinallylabel,
  955. exitfinallylabel,
  956. continuefinallylabel,
  957. breakfinallylabel,
  958. oldaktexitlabel,
  959. oldaktexit2label,
  960. oldaktcontinuelabel,
  961. oldaktbreaklabel : pasmlabel;
  962. oldexceptblock : ptree;
  963. oldflowcontrol,tryflowcontrol : tflowcontrol;
  964. decconst : longint;
  965. begin
  966. { check if child nodes do a break/continue/exit }
  967. oldflowcontrol:=flowcontrol;
  968. flowcontrol:=[];
  969. { we modify EAX }
  970. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  971. getlabel(finallylabel);
  972. getlabel(endfinallylabel);
  973. getlabel(reraiselabel);
  974. { the finally block must catch break, continue and exit }
  975. { statements }
  976. oldaktexitlabel:=aktexitlabel;
  977. oldaktexit2label:=aktexit2label;
  978. getlabel(exitfinallylabel);
  979. aktexitlabel:=exitfinallylabel;
  980. aktexit2label:=exitfinallylabel;
  981. if assigned(aktbreaklabel) then
  982. begin
  983. oldaktcontinuelabel:=aktcontinuelabel;
  984. oldaktbreaklabel:=aktbreaklabel;
  985. getlabel(breakfinallylabel);
  986. getlabel(continuefinallylabel);
  987. aktcontinuelabel:=continuefinallylabel;
  988. aktbreaklabel:=breakfinallylabel;
  989. end;
  990. push_int(1); { Type of stack-frame must be pushed}
  991. emitcall('FPC_PUSHEXCEPTADDR');
  992. { allocate eax }
  993. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  994. emit_reg(A_PUSH,S_L,R_EAX);
  995. emitcall('FPC_SETJMP');
  996. emit_reg(A_PUSH,S_L,R_EAX);
  997. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  998. { deallocate eax }
  999. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  1000. emitjmp(C_NE,finallylabel);
  1001. { try code }
  1002. if assigned(p^.left) then
  1003. begin
  1004. oldexceptblock:=aktexceptblock;
  1005. aktexceptblock:=p^.left;
  1006. secondpass(p^.left);
  1007. tryflowcontrol:=flowcontrol;
  1008. if codegenerror then
  1009. exit;
  1010. aktexceptblock:=oldexceptblock;
  1011. end;
  1012. emitlab(finallylabel);
  1013. emitcall('FPC_POPADDRSTACK');
  1014. { finally code }
  1015. oldexceptblock:=aktexceptblock;
  1016. aktexceptblock:=p^.right;
  1017. flowcontrol:=[];
  1018. secondpass(p^.right);
  1019. if flowcontrol<>[] then
  1020. CGMessage(cg_e_control_flow_outside_finally);
  1021. aktexceptblock:=oldexceptblock;
  1022. if codegenerror then
  1023. exit;
  1024. { allocate eax }
  1025. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  1026. emit_reg(A_POP,S_L,R_EAX);
  1027. emit_reg_reg(A_TEST,S_L,R_EAX,R_EAX);
  1028. emitjmp(C_E,endfinallylabel);
  1029. emit_reg(A_DEC,S_L,R_EAX);
  1030. emitjmp(C_Z,reraiselabel);
  1031. if fc_exit in tryflowcontrol then
  1032. begin
  1033. emit_reg(A_DEC,S_L,R_EAX);
  1034. emitjmp(C_Z,oldaktexitlabel);
  1035. decconst:=1;
  1036. end
  1037. else
  1038. decconst:=2;
  1039. if fc_break in tryflowcontrol then
  1040. begin
  1041. emit_const_reg(A_SUB,S_L,decconst,R_EAX);
  1042. emitjmp(C_Z,oldaktbreaklabel);
  1043. decconst:=1;
  1044. end
  1045. else
  1046. inc(decconst);
  1047. if fc_continue in tryflowcontrol then
  1048. begin
  1049. emit_const_reg(A_SUB,S_L,decconst,R_EAX);
  1050. emitjmp(C_Z,oldaktcontinuelabel);
  1051. end;
  1052. { deallocate eax }
  1053. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  1054. emitlab(reraiselabel);
  1055. emitcall('FPC_RERAISE');
  1056. { do some magic for exit,break,continue in the try block }
  1057. if fc_exit in tryflowcontrol then
  1058. begin
  1059. emitlab(exitfinallylabel);
  1060. { allocate eax }
  1061. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  1062. emit_reg(A_POP,S_L,R_EAX);
  1063. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  1064. emit_const(A_PUSH,S_L,2);
  1065. emitjmp(C_NONE,finallylabel);
  1066. end;
  1067. if fc_break in tryflowcontrol then
  1068. begin
  1069. emitlab(breakfinallylabel);
  1070. { allocate eax }
  1071. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  1072. emit_reg(A_POP,S_L,R_EAX);
  1073. { deallocate eax }
  1074. exprasmlist^.concat(new(pairegalloc,dealloc(R_EAX)));
  1075. emit_const(A_PUSH,S_L,3);
  1076. emitjmp(C_NONE,finallylabel);
  1077. end;
  1078. if fc_continue in tryflowcontrol then
  1079. begin
  1080. emitlab(continuefinallylabel);
  1081. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  1082. emit_reg(A_POP,S_L,R_EAX);
  1083. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  1084. emit_const(A_PUSH,S_L,4);
  1085. emitjmp(C_NONE,finallylabel);
  1086. end;
  1087. emitlab(endfinallylabel);
  1088. aktexitlabel:=oldaktexitlabel;
  1089. aktexit2label:=oldaktexit2label;
  1090. if assigned(aktbreaklabel) then
  1091. begin
  1092. aktcontinuelabel:=oldaktcontinuelabel;
  1093. aktbreaklabel:=oldaktbreaklabel;
  1094. end;
  1095. flowcontrol:=oldflowcontrol+tryflowcontrol;
  1096. end;
  1097. {*****************************************************************************
  1098. SecondFail
  1099. *****************************************************************************}
  1100. procedure secondfail(var p : ptree);
  1101. begin
  1102. emitjmp(C_None,faillabel);
  1103. end;
  1104. end.
  1105. {
  1106. $Log$
  1107. Revision 1.1 2000-10-14 10:14:56 peter
  1108. * moehrendorf oct 2000 rewrite
  1109. Revision 1.6 2000/09/24 21:19:49 peter
  1110. * delphi compile fixes
  1111. Revision 1.5 2000/08/29 18:41:02 peter
  1112. * push ebp instead of 0 for raise without address
  1113. Revision 1.4 2000/08/13 08:41:07 peter
  1114. * restore labels when error in except block (merged)
  1115. Revision 1.3 2000/07/21 15:14:02 jonas
  1116. + added is_addr field for labels, if they are only used for getting the address
  1117. (e.g. for io checks) and corresponding getaddrlabel() procedure
  1118. Revision 1.2 2000/07/13 11:32:33 michael
  1119. + removed logs
  1120. }