pass_2.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit handles the codegeneration pass
  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 pass_2;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. tree;
  23. { produces assembler for the expression in variable p }
  24. { and produces an assembler node at the end }
  25. procedure generatecode(var p : ptree);
  26. { produces the actual code }
  27. function do_secondpass(var p : ptree) : boolean;
  28. procedure secondpass(var p : ptree);
  29. implementation
  30. uses
  31. globtype,systems,
  32. cobjects,comphook,verbose,globals,fmodule,
  33. symconst,symtable,types,aasm,scanner,
  34. pass_1,hcodegen,temp_gen,cpubase,cpuasm,regvars
  35. {$ifndef newcg}
  36. ,tcflw
  37. {$endif newcg}
  38. {$ifdef GDB}
  39. ,gdb
  40. {$endif}
  41. {$ifdef i386}
  42. ,tgeni386,cgai386
  43. ,cg386con,cg386mat,cg386cnv,cg386set,cg386add
  44. ,cg386mem,cg386cal,cg386ld,cg386flw,cg386inl
  45. {$endif}
  46. {$ifdef m68k}
  47. ,tgen68k,cga68k
  48. ,cg68kcon,cg68kmat,cg68kcnv,cg68kset,cg68kadd
  49. ,cg68kmem,cg68kcal,cg68kld,cg68kflw,cg68kinl
  50. {$endif}
  51. ;
  52. {*****************************************************************************
  53. SecondPass
  54. *****************************************************************************}
  55. type
  56. secondpassproc = procedure(var p : ptree);
  57. procedure secondnothing(var p : ptree);
  58. begin
  59. end;
  60. procedure seconderror(var p : ptree);
  61. begin
  62. p^.error:=true;
  63. codegenerror:=true;
  64. end;
  65. procedure secondstatement(var p : ptree);
  66. var
  67. hp : ptree;
  68. begin
  69. hp:=p;
  70. while assigned(hp) do
  71. begin
  72. if assigned(hp^.right) then
  73. begin
  74. cleartempgen;
  75. {!!!!!!
  76. oldrl:=temptoremove;
  77. temptoremove:=new(plinkedlist,init);
  78. }
  79. secondpass(hp^.right);
  80. { !!!!!!!
  81. some temporary data which can't be released elsewhere
  82. removetemps(exprasmlist,temptoremove);
  83. dispose(temptoremove,done);
  84. temptoremove:=oldrl;
  85. }
  86. end;
  87. hp:=hp^.left;
  88. end;
  89. end;
  90. procedure secondblockn(var p : ptree);
  91. begin
  92. { do second pass on left node }
  93. if assigned(p^.left) then
  94. secondpass(p^.left);
  95. end;
  96. procedure secondasm(var p : ptree);
  97. procedure ReLabel(var p:pasmsymbol);
  98. begin
  99. if p^.proclocal then
  100. begin
  101. if not assigned(p^.altsymbol) then
  102. begin
  103. p^.GenerateAltSymbol;
  104. UsedAsmSymbolListInsert(p);
  105. end;
  106. p:=p^.altsymbol;
  107. end;
  108. end;
  109. var
  110. hp,hp2 : pai;
  111. localfixup,parafixup,
  112. i : longint;
  113. skipnode : boolean;
  114. begin
  115. if inlining_procedure then
  116. begin
  117. InitUsedAsmSymbolList;
  118. localfixup:=aktprocsym^.definition^.localst^.address_fixup;
  119. parafixup:=aktprocsym^.definition^.parast^.address_fixup;
  120. hp:=pai(p^.p_asm^.first);
  121. while assigned(hp) do
  122. begin
  123. hp2:=pai(hp^.getcopy);
  124. skipnode:=false;
  125. case hp2^.typ of
  126. ait_label :
  127. begin
  128. { regenerate the labels by setting altsymbol }
  129. ReLabel(pasmsymbol(pai_label(hp2)^.l));
  130. end;
  131. ait_const_rva,
  132. ait_const_symbol :
  133. begin
  134. ReLabel(pai_const_symbol(hp2)^.sym);
  135. end;
  136. ait_instruction :
  137. begin
  138. {$ifdef i386}
  139. { fixup the references }
  140. for i:=1 to paicpu(hp2)^.ops do
  141. begin
  142. with paicpu(hp2)^.oper[i-1] do
  143. begin
  144. case typ of
  145. top_ref :
  146. begin
  147. case ref^.options of
  148. ref_parafixup :
  149. ref^.offsetfixup:=parafixup;
  150. ref_localfixup :
  151. ref^.offsetfixup:=localfixup;
  152. end;
  153. if assigned(ref^.symbol) then
  154. ReLabel(ref^.symbol);
  155. end;
  156. top_symbol :
  157. begin
  158. ReLabel(sym);
  159. end;
  160. end;
  161. end;
  162. end;
  163. {$endif i386}
  164. end;
  165. ait_marker :
  166. begin
  167. { it's not an assembler block anymore }
  168. if (pai_marker(hp2)^.kind in [AsmBlockStart, AsmBlockEnd]) then
  169. skipnode:=true;
  170. end;
  171. else
  172. end;
  173. if not skipnode then
  174. exprasmlist^.concat(hp2)
  175. else
  176. dispose(hp2,done);
  177. hp:=pai(hp^.next);
  178. end;
  179. { restore used symbols }
  180. UsedAsmSymbolListResetAltSym;
  181. DoneUsedAsmSymbolList;
  182. end
  183. else
  184. begin
  185. { if the routine is an inline routine, then we must hold a copy
  186. becuase it can be necessary for inlining later }
  187. if (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  188. exprasmlist^.concatlistcopy(p^.p_asm)
  189. else
  190. exprasmlist^.concatlist(p^.p_asm);
  191. end;
  192. if not p^.object_preserved then
  193. begin
  194. {$ifdef i386}
  195. maybe_loadesi;
  196. {$endif}
  197. {$ifdef m68k}
  198. maybe_loada5;
  199. {$endif}
  200. end;
  201. end;
  202. {$ifdef logsecondpass}
  203. procedure logsecond(const s: string; entry: boolean);
  204. var p: pchar;
  205. begin
  206. if entry then
  207. p := strpnew(s+' (entry)')
  208. else p := strpnew(s+' (exit)');
  209. exprasmlist^.concat(new(pai_asm_comment,init(p)));
  210. end;
  211. {$endif logsecondpass}
  212. procedure secondpass(var p : ptree);
  213. const
  214. procedures : array[ttreetyp] of secondpassproc =
  215. (secondadd, {addn}
  216. secondadd, {muln}
  217. secondadd, {subn}
  218. secondmoddiv, {divn}
  219. secondadd, {symdifn}
  220. secondmoddiv, {modn}
  221. secondassignment, {assignn}
  222. secondload, {loadn}
  223. secondnothing, {range}
  224. secondadd, {ltn}
  225. secondadd, {lten}
  226. secondadd, {gtn}
  227. secondadd, {gten}
  228. secondadd, {equaln}
  229. secondadd, {unequaln}
  230. secondin, {inn}
  231. secondadd, {orn}
  232. secondadd, {xorn}
  233. secondshlshr, {shrn}
  234. secondshlshr, {shln}
  235. secondadd, {slashn}
  236. secondadd, {andn}
  237. secondsubscriptn, {subscriptn}
  238. secondderef, {derefn}
  239. secondaddr, {addrn}
  240. seconddoubleaddr, {doubleaddrn}
  241. secondordconst, {ordconstn}
  242. secondtypeconv, {typeconvn}
  243. secondcalln, {calln}
  244. secondnothing, {callparan}
  245. secondrealconst, {realconstn}
  246. secondfixconst, {fixconstn}
  247. secondunaryminus, {unaryminusn}
  248. secondasm, {asmn}
  249. secondvecn, {vecn}
  250. secondpointerconst, {pointerconstn}
  251. secondstringconst, {stringconstn}
  252. secondfuncret, {funcretn}
  253. secondselfn, {selfn}
  254. secondnot, {notn}
  255. secondinline, {inlinen}
  256. secondniln, {niln}
  257. seconderror, {errorn}
  258. secondnothing, {typen}
  259. secondhnewn, {hnewn}
  260. secondhdisposen, {hdisposen}
  261. secondnewn, {newn}
  262. secondsimplenewdispose, {simpledisposen}
  263. secondsetelement, {setelementn}
  264. secondsetconst, {setconstn}
  265. secondblockn, {blockn}
  266. secondstatement, {statementn}
  267. secondnothing, {loopn}
  268. secondifn, {ifn}
  269. secondbreakn, {breakn}
  270. secondcontinuen, {continuen}
  271. second_while_repeatn, {repeatn}
  272. second_while_repeatn, {whilen}
  273. secondfor, {forn}
  274. secondexitn, {exitn}
  275. secondwith, {withn}
  276. secondcase, {casen}
  277. secondlabel, {labeln}
  278. secondgoto, {goton}
  279. secondsimplenewdispose, {simplenewn}
  280. secondtryexcept, {tryexceptn}
  281. secondraise, {raisen}
  282. secondnothing, {switchesn}
  283. secondtryfinally, {tryfinallyn}
  284. secondon, {onn}
  285. secondis, {isn}
  286. secondas, {asn}
  287. seconderror, {caretn}
  288. secondfail, {failn}
  289. secondadd, {starstarn}
  290. secondprocinline, {procinlinen}
  291. secondarrayconstruct, {arrayconstructn}
  292. secondnothing, {arrayconstructrangen}
  293. secondnothing, {nothingn}
  294. secondloadvmt {loadvmtn}
  295. );
  296. {$ifdef logsecondpass}
  297. secondnames: array[ttreetyp] of string[13] =
  298. ('add-addn', {addn}
  299. 'add-muln)', {muln}
  300. 'add-subn', {subn}
  301. 'moddiv-divn', {divn}
  302. 'add-symdifn', {symdifn}
  303. 'moddiv-modn', {modn}
  304. 'assignment', {assignn}
  305. 'load', {loadn}
  306. 'nothing-range', {range}
  307. 'add-ltn', {ltn}
  308. 'add-lten', {lten}
  309. 'add-gtn', {gtn}
  310. 'add-gten', {gten}
  311. 'add-equaln', {equaln}
  312. 'add-unequaln', {unequaln}
  313. 'in', {inn}
  314. 'add-orn', {orn}
  315. 'add-xorn', {xorn}
  316. 'shlshr-shrn', {shrn}
  317. 'shlshr-shln', {shln}
  318. 'add-slashn', {slashn}
  319. 'add-andn', {andn}
  320. 'subscriptn', {subscriptn}
  321. 'dderef', {derefn}
  322. 'addr', {addrn}
  323. 'doubleaddr', {doubleaddrn}
  324. 'ordconst', {ordconstn}
  325. 'typeconv', {typeconvn}
  326. 'calln', {calln}
  327. 'nothing-callp', {callparan}
  328. 'realconst', {realconstn}
  329. 'fixconst', {fixconstn}
  330. 'unaryminus', {unaryminusn}
  331. 'asm', {asmn}
  332. 'vecn', {vecn}
  333. 'pointerconst', {pointerconstn}
  334. 'stringconst', {stringconstn}
  335. 'funcret', {funcretn}
  336. 'selfn', {selfn}
  337. 'not', {notn}
  338. 'inline', {inlinen}
  339. 'niln', {niln}
  340. 'error', {errorn}
  341. 'nothing-typen', {typen}
  342. 'hnewn', {hnewn}
  343. 'hdisposen', {hdisposen}
  344. 'newn', {newn}
  345. 'simplenewDISP', {simpledisposen}
  346. 'setelement', {setelementn}
  347. 'setconst', {setconstn}
  348. 'blockn', {blockn}
  349. 'statement', {statementn}
  350. 'nothing-loopn', {loopn}
  351. 'ifn', {ifn}
  352. 'breakn', {breakn}
  353. 'continuen', {continuen}
  354. '_while_REPEAT', {repeatn}
  355. '_WHILE_repeat', {whilen}
  356. 'for', {forn}
  357. 'exitn', {exitn}
  358. 'with', {withn}
  359. 'case', {casen}
  360. 'label', {labeln}
  361. 'goto', {goton}
  362. 'simpleNEWdisp', {simplenewn}
  363. 'tryexcept', {tryexceptn}
  364. 'raise', {raisen}
  365. 'nothing-swtch', {switchesn}
  366. 'tryfinally', {tryfinallyn}
  367. 'on', {onn}
  368. 'is', {isn}
  369. 'as', {asn}
  370. 'error-caret', {caretn}
  371. 'fail', {failn}
  372. 'add-startstar', {starstarn}
  373. 'procinline', {procinlinen}
  374. 'arrayconstruc', {arrayconstructn}
  375. 'noth-arrcnstr', {arrayconstructrangen}
  376. 'nothing-nothg', {nothingn}
  377. 'loadvmt' {loadvmtn}
  378. );
  379. {$endif logsecondpass}
  380. var
  381. oldcodegenerror : boolean;
  382. oldlocalswitches : tlocalswitches;
  383. oldpos : tfileposinfo;
  384. {$ifdef TEMPREGDEBUG}
  385. prevp : pptree;
  386. {$endif TEMPREGDEBUG}
  387. begin
  388. if not(p^.error) then
  389. begin
  390. oldcodegenerror:=codegenerror;
  391. oldlocalswitches:=aktlocalswitches;
  392. oldpos:=aktfilepos;
  393. {$ifdef TEMPREGDEBUG}
  394. testregisters32;
  395. prevp:=curptree;
  396. curptree:=@p;
  397. p^.usableregs:=usablereg32;
  398. {$endif TEMPREGDEBUG}
  399. aktfilepos:=p^.fileinfo;
  400. aktlocalswitches:=p^.localswitches;
  401. codegenerror:=false;
  402. {$ifdef logsecondpass}
  403. logsecond('second'+secondnames[p^.treetype],true);
  404. {$endif logsecondpass}
  405. procedures[p^.treetype](p);
  406. {$ifdef logsecondpass}
  407. logsecond('second'+secondnames[p^.treetype],false);
  408. {$endif logsecondpass}
  409. p^.error:=codegenerror;
  410. codegenerror:=codegenerror or oldcodegenerror;
  411. aktlocalswitches:=oldlocalswitches;
  412. aktfilepos:=oldpos;
  413. {$ifdef TEMPREGDEBUG}
  414. curptree:=prevp;
  415. {$endif TEMPREGDEBUG}
  416. {$ifdef EXTTEMPREGDEBUG}
  417. if p^.usableregs-usablereg32>p^.reallyusedregs then
  418. p^.reallyusedregs:=p^.usableregs-usablereg32;
  419. if p^.reallyusedregs<p^.registers32 then
  420. Comment(V_Debug,'registers32 overestimated '+tostr(p^.registers32)+
  421. '>'+tostr(p^.reallyusedregs));
  422. {$endif EXTTEMPREGDEBUG}
  423. end
  424. else
  425. codegenerror:=true;
  426. end;
  427. function do_secondpass(var p : ptree) : boolean;
  428. begin
  429. codegenerror:=false;
  430. if not(p^.error) then
  431. secondpass(p);
  432. do_secondpass:=codegenerror;
  433. end;
  434. procedure clearrefs(p : pnamedindexobject);
  435. begin
  436. if (psym(p)^.typ=varsym) then
  437. if pvarsym(p)^.refs>1 then
  438. pvarsym(p)^.refs:=1;
  439. end;
  440. procedure generatecode(var p : ptree);
  441. begin
  442. cleartempgen;
  443. flowcontrol:=[];
  444. { when size optimization only count occurrence }
  445. if cs_littlesize in aktglobalswitches then
  446. t_times:=1
  447. else
  448. { reference for repetition is 100 }
  449. t_times:=100;
  450. { clear register count }
  451. clearregistercount;
  452. use_esp_stackframe:=false;
  453. aktexceptblock:=nil;
  454. symtablestack^.foreach(@clearrefs);
  455. symtablestack^.next^.foreach(@clearrefs);
  456. if not(do_firstpass(p)) then
  457. begin
  458. if (cs_regalloc in aktglobalswitches) and
  459. ((procinfo^.flags and (pi_uses_asm or pi_uses_exceptions))=0) then
  460. begin
  461. { can we omit the stack frame ? }
  462. { conditions:
  463. 1. procedure (not main block)
  464. 2. no constructor or destructor
  465. 3. no call to other procedures
  466. 4. no interrupt handler
  467. }
  468. {!!!!!! this doesn work yet, because of problems with
  469. with linux and windows
  470. }
  471. (*
  472. if assigned(aktprocsym) then
  473. begin
  474. if not(assigned(procinfo^._class)) and
  475. not(aktprocsym^.definition^.proctypeoption in [potype_constructor,potype_destructor]) and
  476. not(po_interrupt in aktprocsym^.definition^.procoptions) and
  477. ((procinfo^.flags and pi_do_call)=0) and
  478. (lexlevel>=normal_function_level) then
  479. begin
  480. { use ESP as frame pointer }
  481. procinfo^.framepointer:=stack_pointer;
  482. use_esp_stackframe:=true;
  483. { calc parameter distance new }
  484. dec(procinfo^.framepointer_offset,4);
  485. dec(procinfo^.selfpointer_offset,4);
  486. { is this correct ???}
  487. { retoffset can be negativ for results in eax !! }
  488. { the value should be decreased only if positive }
  489. if procinfo^.retoffset>=0 then
  490. dec(procinfo^.retoffset,4);
  491. dec(procinfo^.para_offset,4);
  492. aktprocsym^.definition^.parast^.address_fixup:=procinfo^.para_offset;
  493. end;
  494. end;
  495. *)
  496. end;
  497. { process register variable stuff (JM) }
  498. assign_regvars(p);
  499. load_regvars(procinfo^.aktentrycode,p);
  500. cleanup_regvars(procinfo^.aktexitcode);
  501. if assigned(aktprocsym) and
  502. (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  503. make_const_global:=true;
  504. do_secondpass(p);
  505. if assigned(procinfo^.def) then
  506. procinfo^.def^.fpu_used:=p^.registersfpu;
  507. end;
  508. procinfo^.aktproccode^.concatlist(exprasmlist);
  509. make_const_global:=false;
  510. end;
  511. end.
  512. {
  513. $Log$
  514. Revision 1.8 2000-09-24 15:06:21 peter
  515. * use defines.inc
  516. Revision 1.7 2000/08/27 16:11:51 peter
  517. * moved some util functions from globals,cobjects to cutils
  518. * splitted files into finput,fmodule
  519. Revision 1.6 2000/08/12 15:34:22 peter
  520. + usedasmsymbollist to check and reset only the used symbols (merged)
  521. Revision 1.5 2000/08/03 13:17:25 jonas
  522. + allow regvars to be used inside inlined procs, which required the
  523. following changes:
  524. + load regvars in genentrycode/free them in genexitcode (cgai386)
  525. * moved all regvar related code to new regvars unit
  526. + added pregvarinfo type to hcodegen
  527. + added regvarinfo field to tprocinfo (symdef/symdefh)
  528. * deallocate the regvars of the caller in secondprocinline before
  529. inlining the called procedure and reallocate them afterwards
  530. Revision 1.4 2000/08/03 11:15:42 jonas
  531. - disable regvars for inlined procedures (merged from fixes branch)
  532. Revision 1.3 2000/07/21 15:14:02 jonas
  533. + added is_addr field for labels, if they are only used for getting the address
  534. (e.g. for io checks) and corresponding getaddrlabel() procedure
  535. Revision 1.2 2000/07/13 11:32:44 michael
  536. + removed logs
  537. }