CodeGen.lhs 5.67 KB
Newer Older
1
%
2
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
%
\section[CodeGen]{@CodeGen@: main module of the code generator}

This module says how things get going at the top level.

@codeGen@ is the interface to the outside world.  The \tr{cgTop*}
functions drive the mangling of top-level bindings.

%************************************************************************
%*									*
\subsection[codeGen-outside-interface]{The code generator's offering to the world}
%*									*
%************************************************************************

\begin{code}
#include "HsVersions.h"

20
module CodeGen ( codeGen ) where
21

22
IMP_Ubiq(){-uitous-}
23

24 25 26 27
import StgSyn
import CgMonad
import AbsCSyn

28 29
import AbsCUtils	( mkAbstractCs, mkAbsCStmts )
import Bag		( foldBag )
30
import CgBindery	( CgIdInfo )
31 32
import CgClosure	( cgTopRhsClosure )
import CgCon		( cgTopRhsCon )
33 34
import CgConTbls	( genStaticConBits )
import ClosureInfo	( mkClosureLFInfo )
35
import CmdLineOpts	( opt_SccProfilingOn, opt_CompilingGhcInternals,
36 37 38
			  opt_EnsureSplittableC, opt_SccGroup
			)
import CStrings		( modnameToC )
39
import FiniteMap	( FiniteMap )
40 41 42
import Maybes		( maybeToBool )
import PrimRep		( getPrimRepSize, PrimRep(..) )
import Util		( panic, assertPanic )
43 44 45 46 47 48
\end{code}

\begin{code}
codeGen :: FAST_STRING		-- module name
	-> ([CostCentre],	-- local cost-centres needing declaring/registering
	    [CostCentre])	-- "extern" cost-centres needing declaring
49
	-> [Module]		-- import names
50
	-> [TyCon]		-- tycons with data constructors to convert
51
	-> FiniteMap TyCon [(Bool, [Maybe Type])]
52
				-- tycon specialisation info
53
	-> [StgBinding]	-- bindings to convert
54 55
	-> AbstractC		-- output

56
codeGen mod_name (local_CCs, extern_CCs) import_names gen_tycons tycon_specs stg_pgm
57
  = let
58
	doing_profiling   = opt_SccProfilingOn
59
	compiling_prelude = opt_CompilingGhcInternals
60
	maybe_split       = if maybeToBool (opt_EnsureSplittableC)
61 62
			    then CSplitMarker
			    else AbsCNop
63

64
	cinfo = MkCompInfo mod_name
65 66 67 68
    in
    if not doing_profiling then
	mkAbstractCs [
	    genStaticConBits cinfo gen_tycons tycon_specs,
69
	    initC cinfo (cgTopBindings maybe_split stg_pgm) ]
70 71 72 73 74 75 76 77 78 79 80 81

    else -- yes, cost-centre profiling:
	 -- Besides the usual stuff, we must produce:
	 --
    	 -- * Declarations for the cost-centres defined in this module;
	 -- * Code to participate in "registering" all the cost-centres
	 --   in the program (done at startup time when the pgm is run).
	 --
	 -- (The local cost-centres involved in this are passed
	 -- into the code-generator, as are the imported-modules' names.)
	 --
	 -- Note: we don't register/etc if compiling Prelude bits.
82

83 84 85 86 87 88 89 90
	mkAbstractCs [
		if compiling_prelude
		then AbsCNop
		else mkAbstractCs [mkAbstractCs (map (CCostCentreDecl True)  local_CCs),
				   mkAbstractCs (map (CCostCentreDecl False) extern_CCs),
				   mkCcRegister local_CCs import_names],

		genStaticConBits cinfo gen_tycons tycon_specs,
91
		initC cinfo (cgTopBindings maybe_split stg_pgm) ]
92 93
  where
    -----------------
94
    grp_name  = case opt_SccGroup of
95
		  Just xx -> _PK_ xx
96 97 98 99
		  Nothing -> mod_name	-- default: module name

    -----------------
    mkCcRegister ccs import_names
100
      = let
101
	    register_ccs     = mkAbstractCs (map mk_register ccs)
102
	    register_imports
103
	      = foldr (mkAbsCStmts . mk_import_register) AbsCNop import_names
104 105
	in
	mkAbstractCs [
106
	    CCallProfCCMacro SLIT("START_REGISTER_CCS") [CLitLit (modnameToC (SLIT("_reg") _APPEND_ mod_name)) AddrRep],
107 108 109 110 111 112 113 114 115
	    register_ccs,
	    register_imports,
	    CCallProfCCMacro SLIT("END_REGISTER_CCS") []
	]
      where
	mk_register cc
	  = CCallProfCCMacro SLIT("REGISTER_CC") [mkCCostCentre cc]

	mk_import_register import_name
116
	  = CCallProfCCMacro SLIT("REGISTER_IMPORT") [CLitLit (modnameToC (SLIT("_reg") _APPEND_ import_name)) AddrRep]
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
\end{code}

%************************************************************************
%*									*
\subsection[codegen-top-bindings]{Converting top-level STG bindings}
%*									*
%************************************************************************

@cgTopBindings@ is only used for top-level bindings, since they need
to be allocated statically (not in the heap) and need to be labelled.
No unboxed bindings can happen at top level.

In the code below, the static bindings are accumulated in the
@MkCgState@, and transferred into the ``statics'' slot by @forkStatics@.
This is so that we can write the top level processing in a compositional
style, with the increasing static environment being plumbed as a state
variable.

\begin{code}
136
cgTopBindings :: AbstractC -> [StgBinding] -> Code
137

138
cgTopBindings split bindings = mapCs (cgTopBinding split) bindings
139

140 141 142
cgTopBinding :: AbstractC -> StgBinding -> Code

cgTopBinding split (StgNonRec name rhs)
143
  = absC split		`thenC`
144 145 146
    cgTopRhs name rhs	`thenFC` \ (name, info) ->
    addBindC name info

147
cgTopBinding split (StgRec pairs)
148
  = absC split		`thenC`
149 150 151 152 153 154 155 156 157
    fixC (\ new_binds -> addBindsC new_binds	`thenC`
			 mapFCs ( \ (b,e) -> cgTopRhs b e ) pairs
    )			`thenFC` \ new_binds ->
    addBindsC new_binds

-- Urgh!  I tried moving the forkStatics call from the rhss of cgTopRhs
-- to enclose the listFCs in cgTopBinding, but that tickled the
-- statics "error" call in initC.  I DON'T UNDERSTAND WHY!

158
cgTopRhs :: Id -> StgRhs -> FCode (Id, CgIdInfo)
159 160 161 162 163
	-- the Id is passed along for setting up a binding...

cgTopRhs name (StgRhsCon cc con args)
  = forkStatics (cgTopRhsCon name con args (all zero_size args))
  where
164
    zero_size atom = getPrimRepSize (getArgPrimRep atom) == 0
165 166 167 168 169 170 171

cgTopRhs name (StgRhsClosure cc bi fvs upd_flag args body)
  = ASSERT(null fvs) -- There should be no free variables
    forkStatics (cgTopRhsClosure name cc bi args body lf_info)
  where
    lf_info = mkClosureLFInfo True{-top level-} [{-no fvs-}] upd_flag args body
\end{code}