cg386flw.pas 42 KB

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