pass_2.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. node;
  23. type
  24. tenumflowcontrol = (fc_exit,fc_break,fc_continue);
  25. tflowcontrol = set of tenumflowcontrol;
  26. var
  27. flowcontrol : tflowcontrol;
  28. { produces assembler for the expression in variable p }
  29. { and produces an assembler node at the end }
  30. procedure generatecode(var p : tnode);
  31. { produces the actual code }
  32. function do_secondpass(var p : tnode) : boolean;
  33. procedure secondpass(var p : tnode);
  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,nflw
  40. {$ifdef GDB}
  41. ,gdb
  42. {$endif}
  43. {$ifdef i386}
  44. ,tgeni386,cgai386
  45. {$endif}
  46. {$ifdef m68k}
  47. ,tgen68k,cga68k
  48. {$endif}
  49. ;
  50. {*****************************************************************************
  51. SecondPass
  52. *****************************************************************************}
  53. {$ifdef logsecondpass}
  54. procedure logsecond(const s: string; entry: boolean);
  55. var p: pchar;
  56. begin
  57. if entry then
  58. p := strpnew(s+' (entry)')
  59. else p := strpnew(s+' (exit)');
  60. exprasmlist^.concat(new(pai_asm_comment,init(p)));
  61. end;
  62. {$endif logsecondpass}
  63. procedure secondpass(var p : tnode);
  64. {$ifdef logsecondpass}
  65. secondnames: array[ttreetyp] of string[13] =
  66. ('add-addn', {addn}
  67. 'add-muln)', {muln}
  68. 'add-subn', {subn}
  69. 'moddiv-divn', {divn}
  70. 'add-symdifn', {symdifn}
  71. 'moddiv-modn', {modn}
  72. 'assignment', {assignn}
  73. 'load', {loadn}
  74. 'nothing-range', {range}
  75. 'add-ltn', {ltn}
  76. 'add-lten', {lten}
  77. 'add-gtn', {gtn}
  78. 'add-gten', {gten}
  79. 'add-equaln', {equaln}
  80. 'add-unequaln', {unequaln}
  81. 'in', {inn}
  82. 'add-orn', {orn}
  83. 'add-xorn', {xorn}
  84. 'shlshr-shrn', {shrn}
  85. 'shlshr-shln', {shln}
  86. 'add-slashn', {slashn}
  87. 'add-andn', {andn}
  88. 'subscriptn', {subscriptn}
  89. 'dderef', {derefn}
  90. 'addr', {addrn}
  91. 'doubleaddr', {doubleaddrn}
  92. 'ordconst', {ordconstn}
  93. 'typeconv', {typeconvn}
  94. 'calln', {calln}
  95. 'nothing-callp', {callparan}
  96. 'realconst', {realconstn}
  97. 'fixconst', {fixconstn}
  98. 'unaryminus', {unaryminusn}
  99. 'asm', {asmn}
  100. 'vecn', {vecn}
  101. 'pointerconst', {pointerconstn}
  102. 'stringconst', {stringconstn}
  103. 'funcret', {funcretn}
  104. 'selfn', {selfn}
  105. 'not', {notn}
  106. 'inline', {inlinen}
  107. 'niln', {niln}
  108. 'error', {errorn}
  109. 'nothing-typen', {typen}
  110. 'hnewn', {hnewn}
  111. 'hdisposen', {hdisposen}
  112. 'newn', {newn}
  113. 'simplenewDISP', {simpledisposen}
  114. 'setelement', {setelementn}
  115. 'setconst', {setconstn}
  116. 'blockn', {blockn}
  117. 'statement', {statementn}
  118. 'nothing-loopn', {loopn}
  119. 'ifn', {ifn}
  120. 'breakn', {breakn}
  121. 'continuen', {continuen}
  122. '_while_REPEAT', {repeatn}
  123. '_WHILE_repeat', {whilen}
  124. 'for', {forn}
  125. 'exitn', {exitn}
  126. 'with', {withn}
  127. 'case', {casen}
  128. 'label', {labeln}
  129. 'goto', {goton}
  130. 'simpleNEWdisp', {simplenewn}
  131. 'tryexcept', {tryexceptn}
  132. 'raise', {raisen}
  133. 'nothing-swtch', {switchesn}
  134. 'tryfinally', {tryfinallyn}
  135. 'on', {onn}
  136. 'is', {isn}
  137. 'as', {asn}
  138. 'error-caret', {caretn}
  139. 'fail', {failn}
  140. 'add-startstar', {starstarn}
  141. 'procinline', {procinlinen}
  142. 'arrayconstruc', {arrayconstructn}
  143. 'noth-arrcnstr', {arrayconstructrangen}
  144. 'nothing-nothg', {nothingn}
  145. 'loadvmt' {loadvmtn}
  146. );
  147. {$endif logsecondpass}
  148. var
  149. oldcodegenerror : boolean;
  150. oldlocalswitches : tlocalswitches;
  151. oldpos : tfileposinfo;
  152. {$ifdef TEMPREGDEBUG}
  153. prevp : pptree;
  154. {$endif TEMPREGDEBUG}
  155. begin
  156. if not(nf_error in p.flags) then
  157. begin
  158. oldcodegenerror:=codegenerror;
  159. oldlocalswitches:=aktlocalswitches;
  160. oldpos:=aktfilepos;
  161. {$ifdef TEMPREGDEBUG}
  162. testregisters32;
  163. prevp:=curptree;
  164. curptree:=@p;
  165. p^.usableregs:=usablereg32;
  166. {$endif TEMPREGDEBUG}
  167. aktfilepos:=p.fileinfo;
  168. aktlocalswitches:=p.localswitches;
  169. codegenerror:=false;
  170. {$ifdef logsecondpass}
  171. logsecond('second'+secondnames[p.nodetype],true);
  172. {$endif logsecondpass}
  173. p.pass_2;
  174. {$ifdef logsecondpass}
  175. logsecond('second'+secondnames[p.nodetype],false);
  176. {$endif logsecondpass}
  177. if codegenerror then
  178. include(p.flags,nf_error);
  179. codegenerror:=codegenerror or oldcodegenerror;
  180. aktlocalswitches:=oldlocalswitches;
  181. aktfilepos:=oldpos;
  182. {$ifdef TEMPREGDEBUG}
  183. curptree:=prevp;
  184. {$endif TEMPREGDEBUG}
  185. {$ifdef EXTTEMPREGDEBUG}
  186. if p.usableregs-usablereg32>p.reallyusedregs then
  187. p.reallyusedregs:=p.usableregs-usablereg32;
  188. if p.reallyusedregs<p.registers32 then
  189. Comment(V_Debug,'registers32 overestimated '+tostr(p^.registers32)+
  190. '>'+tostr(p^.reallyusedregs));
  191. {$endif EXTTEMPREGDEBUG}
  192. end
  193. else
  194. codegenerror:=true;
  195. end;
  196. function do_secondpass(var p : tnode) : boolean;
  197. begin
  198. codegenerror:=false;
  199. if not(nf_error in p.flags) then
  200. secondpass(p);
  201. do_secondpass:=codegenerror;
  202. end;
  203. procedure clearrefs(p : pnamedindexobject);
  204. begin
  205. if (psym(p)^.typ=varsym) then
  206. if pvarsym(p)^.refs>1 then
  207. pvarsym(p)^.refs:=1;
  208. end;
  209. procedure generatecode(var p : tnode);
  210. begin
  211. cleartempgen;
  212. flowcontrol:=[];
  213. { when size optimization only count occurrence }
  214. if cs_littlesize in aktglobalswitches then
  215. t_times:=1
  216. else
  217. { reference for repetition is 100 }
  218. t_times:=100;
  219. { clear register count }
  220. clearregistercount;
  221. use_esp_stackframe:=false;
  222. aktexceptblock:=nil;
  223. symtablestack^.foreach(@clearrefs);
  224. symtablestack^.next^.foreach(@clearrefs);
  225. if not(do_firstpass(p)) then
  226. begin
  227. if (cs_regalloc in aktglobalswitches) and
  228. ((procinfo^.flags and (pi_uses_asm or pi_uses_exceptions))=0) then
  229. begin
  230. { can we omit the stack frame ? }
  231. { conditions:
  232. 1. procedure (not main block)
  233. 2. no constructor or destructor
  234. 3. no call to other procedures
  235. 4. no interrupt handler
  236. }
  237. {!!!!!! this doesn work yet, because of problems with
  238. with linux and windows
  239. }
  240. (*
  241. if assigned(aktprocsym) then
  242. begin
  243. if not(assigned(procinfo^._class)) and
  244. not(aktprocsym^.definition^.proctypeoption in [potype_constructor,potype_destructor]) and
  245. not(po_interrupt in aktprocsym^.definition^.procoptions) and
  246. ((procinfo^.flags and pi_do_call)=0) and
  247. (lexlevel>=normal_function_level) then
  248. begin
  249. { use ESP as frame pointer }
  250. procinfo^.framepointer:=stack_pointer;
  251. use_esp_stackframe:=true;
  252. { calc parameter distance new }
  253. dec(procinfo^.framepointer_offset,4);
  254. dec(procinfo^.selfpointer_offset,4);
  255. { is this correct ???}
  256. { retoffset can be negativ for results in eax !! }
  257. { the value should be decreased only if positive }
  258. if procinfo^.retoffset>=0 then
  259. dec(procinfo^.retoffset,4);
  260. dec(procinfo^.para_offset,4);
  261. aktprocsym^.definition^.parast^.address_fixup:=procinfo^.para_offset;
  262. end;
  263. end;
  264. *)
  265. end;
  266. { process register variable stuff (JM) }
  267. assign_regvars(p);
  268. load_regvars(procinfo^.aktentrycode,p);
  269. cleanup_regvars(procinfo^.aktexitcode);
  270. if assigned(aktprocsym) and
  271. (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  272. make_const_global:=true;
  273. do_secondpass(p);
  274. if assigned(procinfo^.def) then
  275. procinfo^.def^.fpu_used:=p.registersfpu;
  276. end;
  277. procinfo^.aktproccode^.concatlist(exprasmlist);
  278. make_const_global:=false;
  279. end;
  280. end.
  281. {
  282. $Log$
  283. Revision 1.9 2000-10-14 10:14:51 peter
  284. * moehrendorf oct 2000 rewrite
  285. Revision 1.8 2000/09/24 15:06:21 peter
  286. * use defines.inc
  287. Revision 1.7 2000/08/27 16:11:51 peter
  288. * moved some util functions from globals,cobjects to cutils
  289. * splitted files into finput,fmodule
  290. Revision 1.6 2000/08/12 15:34:22 peter
  291. + usedasmsymbollist to check and reset only the used symbols (merged)
  292. Revision 1.5 2000/08/03 13:17:25 jonas
  293. + allow regvars to be used inside inlined procs, which required the
  294. following changes:
  295. + load regvars in genentrycode/free them in genexitcode (cgai386)
  296. * moved all regvar related code to new regvars unit
  297. + added pregvarinfo type to hcodegen
  298. + added regvarinfo field to tprocinfo (symdef/symdefh)
  299. * deallocate the regvars of the caller in secondprocinline before
  300. inlining the called procedure and reallocate them afterwards
  301. Revision 1.4 2000/08/03 11:15:42 jonas
  302. - disable regvars for inlined procedures (merged from fixes branch)
  303. Revision 1.3 2000/07/21 15:14:02 jonas
  304. + added is_addr field for labels, if they are only used for getting the address
  305. (e.g. for io checks) and corresponding getaddrlabel() procedure
  306. Revision 1.2 2000/07/13 11:32:44 michael
  307. + removed logs
  308. }