pass_2.pas 13 KB

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