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