pass_2.pas 13 KB

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