pass_2.pas 19 KB

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