Merge branch 'master' of darcs.haskell.org:/srv/darcs//ghc
[ghc.git] / compiler / codeGen / CgUtils.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Code generator utilities; mostly monadic
4 --
5 -- (c) The University of Glasgow 2004-2006
6 --
7 -----------------------------------------------------------------------------
8
9 module CgUtils ( fixStgRegisters ) where
10
11 #include "HsVersions.h"
12
13 import CodeGen.Platform
14 import OldCmm
15 import CLabel
16 import DynFlags
17 import Outputable
18
19 -- -----------------------------------------------------------------------------
20 -- Information about global registers
21
22 baseRegOffset :: DynFlags -> GlobalReg -> Int
23
24 baseRegOffset dflags (VanillaReg 1 _) = oFFSET_StgRegTable_rR1 dflags
25 baseRegOffset dflags (VanillaReg 2 _) = oFFSET_StgRegTable_rR2 dflags
26 baseRegOffset dflags (VanillaReg 3 _) = oFFSET_StgRegTable_rR3 dflags
27 baseRegOffset dflags (VanillaReg 4 _) = oFFSET_StgRegTable_rR4 dflags
28 baseRegOffset dflags (VanillaReg 5 _) = oFFSET_StgRegTable_rR5 dflags
29 baseRegOffset dflags (VanillaReg 6 _) = oFFSET_StgRegTable_rR6 dflags
30 baseRegOffset dflags (VanillaReg 7 _) = oFFSET_StgRegTable_rR7 dflags
31 baseRegOffset dflags (VanillaReg 8 _) = oFFSET_StgRegTable_rR8 dflags
32 baseRegOffset dflags (VanillaReg 9 _) = oFFSET_StgRegTable_rR9 dflags
33 baseRegOffset dflags (VanillaReg 10 _) = oFFSET_StgRegTable_rR10 dflags
34 baseRegOffset _ (VanillaReg n _) = panic ("Registers above R10 are not supported (tried to use R" ++ show n ++ ")")
35 baseRegOffset dflags (FloatReg 1) = oFFSET_StgRegTable_rF1 dflags
36 baseRegOffset dflags (FloatReg 2) = oFFSET_StgRegTable_rF2 dflags
37 baseRegOffset dflags (FloatReg 3) = oFFSET_StgRegTable_rF3 dflags
38 baseRegOffset dflags (FloatReg 4) = oFFSET_StgRegTable_rF4 dflags
39 baseRegOffset _ (FloatReg n) = panic ("Registers above F4 are not supported (tried to use F" ++ show n ++ ")")
40 baseRegOffset dflags (DoubleReg 1) = oFFSET_StgRegTable_rD1 dflags
41 baseRegOffset dflags (DoubleReg 2) = oFFSET_StgRegTable_rD2 dflags
42 baseRegOffset _ (DoubleReg n) = panic ("Registers above D2 are not supported (tried to use D" ++ show n ++ ")")
43 baseRegOffset dflags Sp = oFFSET_StgRegTable_rSp dflags
44 baseRegOffset dflags SpLim = oFFSET_StgRegTable_rSpLim dflags
45 baseRegOffset dflags (LongReg 1) = oFFSET_StgRegTable_rL1 dflags
46 baseRegOffset _ (LongReg n) = panic ("Registers above L1 are not supported (tried to use L" ++ show n ++ ")")
47 baseRegOffset dflags Hp = oFFSET_StgRegTable_rHp dflags
48 baseRegOffset dflags HpLim = oFFSET_StgRegTable_rHpLim dflags
49 baseRegOffset dflags CCCS = oFFSET_StgRegTable_rCCCS dflags
50 baseRegOffset dflags CurrentTSO = oFFSET_StgRegTable_rCurrentTSO dflags
51 baseRegOffset dflags CurrentNursery = oFFSET_StgRegTable_rCurrentNursery dflags
52 baseRegOffset dflags HpAlloc = oFFSET_StgRegTable_rHpAlloc dflags
53 baseRegOffset dflags EagerBlackholeInfo = oFFSET_stgEagerBlackholeInfo dflags
54 baseRegOffset dflags GCEnter1 = oFFSET_stgGCEnter1 dflags
55 baseRegOffset dflags GCFun = oFFSET_stgGCFun dflags
56 baseRegOffset _ BaseReg = panic "baseRegOffset:BaseReg"
57 baseRegOffset _ PicBaseReg = panic "baseRegOffset:PicBaseReg"
58
59
60 -- -----------------------------------------------------------------------------
61 --
62 -- STG/Cmm GlobalReg
63 --
64 -- -----------------------------------------------------------------------------
65
66 -- | We map STG registers onto appropriate CmmExprs. Either they map
67 -- to real machine registers or stored as offsets from BaseReg. Given
68 -- a GlobalReg, get_GlobalReg_addr always produces the
69 -- register table address for it.
70 get_GlobalReg_addr :: DynFlags -> GlobalReg -> CmmExpr
71 get_GlobalReg_addr dflags BaseReg = regTableOffset dflags 0
72 get_GlobalReg_addr dflags mid
73 = get_Regtable_addr_from_offset dflags
74 (globalRegType dflags mid) (baseRegOffset dflags mid)
75
76 -- Calculate a literal representing an offset into the register table.
77 -- Used when we don't have an actual BaseReg to offset from.
78 regTableOffset :: DynFlags -> Int -> CmmExpr
79 regTableOffset dflags n =
80 CmmLit (CmmLabelOff mkMainCapabilityLabel (oFFSET_Capability_r dflags + n))
81
82 get_Regtable_addr_from_offset :: DynFlags -> CmmType -> Int -> CmmExpr
83 get_Regtable_addr_from_offset dflags _ offset =
84 if haveRegBase (targetPlatform dflags)
85 then CmmRegOff (CmmGlobal BaseReg) offset
86 else regTableOffset dflags offset
87
88 -- | Fixup global registers so that they assign to locations within the
89 -- RegTable if they aren't pinned for the current target.
90 fixStgRegisters :: DynFlags -> RawCmmDecl -> RawCmmDecl
91 fixStgRegisters _ top@(CmmData _ _) = top
92
93 fixStgRegisters dflags (CmmProc info lbl (ListGraph blocks)) =
94 let blocks' = map (fixStgRegBlock dflags) blocks
95 in CmmProc info lbl $ ListGraph blocks'
96
97 fixStgRegBlock :: DynFlags -> CmmBasicBlock -> CmmBasicBlock
98 fixStgRegBlock dflags (BasicBlock id stmts) =
99 let stmts' = map (fixStgRegStmt dflags) stmts
100 in BasicBlock id stmts'
101
102 fixStgRegStmt :: DynFlags -> CmmStmt -> CmmStmt
103 fixStgRegStmt dflags stmt
104 = case stmt of
105 CmmAssign (CmmGlobal reg) src ->
106 let src' = fixStgRegExpr dflags src
107 baseAddr = get_GlobalReg_addr dflags reg
108 in case reg `elem` activeStgRegs platform of
109 True -> CmmAssign (CmmGlobal reg) src'
110 False -> CmmStore baseAddr src'
111
112 CmmAssign reg src ->
113 let src' = fixStgRegExpr dflags src
114 in CmmAssign reg src'
115
116 CmmStore addr src -> CmmStore (fixStgRegExpr dflags addr) (fixStgRegExpr dflags src)
117
118 CmmCall target regs args returns ->
119 let target' = case target of
120 CmmCallee e conv -> CmmCallee (fixStgRegExpr dflags e) conv
121 CmmPrim op mStmts ->
122 CmmPrim op (fmap (map (fixStgRegStmt dflags)) mStmts)
123 args' = map (\(CmmHinted arg hint) ->
124 (CmmHinted (fixStgRegExpr dflags arg) hint)) args
125 in CmmCall target' regs args' returns
126
127 CmmCondBranch test dest -> CmmCondBranch (fixStgRegExpr dflags test) dest
128
129 CmmSwitch expr ids -> CmmSwitch (fixStgRegExpr dflags expr) ids
130
131 CmmJump addr live -> CmmJump (fixStgRegExpr dflags addr) live
132
133 -- CmmNop, CmmComment, CmmBranch, CmmReturn
134 _other -> stmt
135 where platform = targetPlatform dflags
136
137
138 fixStgRegExpr :: DynFlags -> CmmExpr -> CmmExpr
139 fixStgRegExpr dflags expr
140 = case expr of
141 CmmLoad addr ty -> CmmLoad (fixStgRegExpr dflags addr) ty
142
143 CmmMachOp mop args -> CmmMachOp mop args'
144 where args' = map (fixStgRegExpr dflags) args
145
146 CmmReg (CmmGlobal reg) ->
147 -- Replace register leaves with appropriate StixTrees for
148 -- the given target. MagicIds which map to a reg on this
149 -- arch are left unchanged. For the rest, BaseReg is taken
150 -- to mean the address of the reg table in MainCapability,
151 -- and for all others we generate an indirection to its
152 -- location in the register table.
153 case reg `elem` activeStgRegs platform of
154 True -> expr
155 False ->
156 let baseAddr = get_GlobalReg_addr dflags reg
157 in case reg of
158 BaseReg -> fixStgRegExpr dflags baseAddr
159 _other -> fixStgRegExpr dflags
160 (CmmLoad baseAddr (globalRegType dflags reg))
161
162 CmmRegOff (CmmGlobal reg) offset ->
163 -- RegOf leaves are just a shorthand form. If the reg maps
164 -- to a real reg, we keep the shorthand, otherwise, we just
165 -- expand it and defer to the above code.
166 case reg `elem` activeStgRegs platform of
167 True -> expr
168 False -> fixStgRegExpr dflags (CmmMachOp (MO_Add (wordWidth dflags)) [
169 CmmReg (CmmGlobal reg),
170 CmmLit (CmmInt (fromIntegral offset)
171 (wordWidth dflags))])
172
173 -- CmmLit, CmmReg (CmmLocal), CmmStackSlot
174 _other -> expr
175 where platform = targetPlatform dflags
176