2cf4cb3483f7d18cef0ed69d8f112af1287914fc
[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 {-# LANGUAGE GADTs #-}
10 module CgUtils ( fixStgRegisters ) where
11
12 #include "HsVersions.h"
13
14 import CodeGen.Platform
15 import Cmm
16 import Hoopl
17 import CmmUtils
18 import CLabel
19 import DynFlags
20 import Outputable
21
22 -- -----------------------------------------------------------------------------
23 -- Information about global registers
24
25 baseRegOffset :: DynFlags -> GlobalReg -> Int
26
27 baseRegOffset dflags (VanillaReg 1 _) = oFFSET_StgRegTable_rR1 dflags
28 baseRegOffset dflags (VanillaReg 2 _) = oFFSET_StgRegTable_rR2 dflags
29 baseRegOffset dflags (VanillaReg 3 _) = oFFSET_StgRegTable_rR3 dflags
30 baseRegOffset dflags (VanillaReg 4 _) = oFFSET_StgRegTable_rR4 dflags
31 baseRegOffset dflags (VanillaReg 5 _) = oFFSET_StgRegTable_rR5 dflags
32 baseRegOffset dflags (VanillaReg 6 _) = oFFSET_StgRegTable_rR6 dflags
33 baseRegOffset dflags (VanillaReg 7 _) = oFFSET_StgRegTable_rR7 dflags
34 baseRegOffset dflags (VanillaReg 8 _) = oFFSET_StgRegTable_rR8 dflags
35 baseRegOffset dflags (VanillaReg 9 _) = oFFSET_StgRegTable_rR9 dflags
36 baseRegOffset dflags (VanillaReg 10 _) = oFFSET_StgRegTable_rR10 dflags
37 baseRegOffset _ (VanillaReg n _) = panic ("Registers above R10 are not supported (tried to use R" ++ show n ++ ")")
38 baseRegOffset dflags (FloatReg 1) = oFFSET_StgRegTable_rF1 dflags
39 baseRegOffset dflags (FloatReg 2) = oFFSET_StgRegTable_rF2 dflags
40 baseRegOffset dflags (FloatReg 3) = oFFSET_StgRegTable_rF3 dflags
41 baseRegOffset dflags (FloatReg 4) = oFFSET_StgRegTable_rF4 dflags
42 baseRegOffset dflags (FloatReg 5) = oFFSET_StgRegTable_rF5 dflags
43 baseRegOffset dflags (FloatReg 6) = oFFSET_StgRegTable_rF6 dflags
44 baseRegOffset _ (FloatReg n) = panic ("Registers above F6 are not supported (tried to use F" ++ show n ++ ")")
45 baseRegOffset dflags (DoubleReg 1) = oFFSET_StgRegTable_rD1 dflags
46 baseRegOffset dflags (DoubleReg 2) = oFFSET_StgRegTable_rD2 dflags
47 baseRegOffset dflags (DoubleReg 3) = oFFSET_StgRegTable_rD3 dflags
48 baseRegOffset dflags (DoubleReg 4) = oFFSET_StgRegTable_rD4 dflags
49 baseRegOffset dflags (DoubleReg 5) = oFFSET_StgRegTable_rD5 dflags
50 baseRegOffset dflags (DoubleReg 6) = oFFSET_StgRegTable_rD6 dflags
51 baseRegOffset _ (DoubleReg n) = panic ("Registers above D6 are not supported (tried to use D" ++ show n ++ ")")
52 baseRegOffset dflags (XmmReg 1) = oFFSET_StgRegTable_rXMM1 dflags
53 baseRegOffset dflags (XmmReg 2) = oFFSET_StgRegTable_rXMM2 dflags
54 baseRegOffset dflags (XmmReg 3) = oFFSET_StgRegTable_rXMM3 dflags
55 baseRegOffset dflags (XmmReg 4) = oFFSET_StgRegTable_rXMM4 dflags
56 baseRegOffset dflags (XmmReg 5) = oFFSET_StgRegTable_rXMM5 dflags
57 baseRegOffset dflags (XmmReg 6) = oFFSET_StgRegTable_rXMM6 dflags
58 baseRegOffset _ (XmmReg n) = panic ("Registers above XMM6 are not supported (tried to use XMM" ++ show n ++ ")")
59 baseRegOffset dflags (YmmReg 1) = oFFSET_StgRegTable_rYMM1 dflags
60 baseRegOffset dflags (YmmReg 2) = oFFSET_StgRegTable_rYMM2 dflags
61 baseRegOffset dflags (YmmReg 3) = oFFSET_StgRegTable_rYMM3 dflags
62 baseRegOffset dflags (YmmReg 4) = oFFSET_StgRegTable_rYMM4 dflags
63 baseRegOffset dflags (YmmReg 5) = oFFSET_StgRegTable_rYMM5 dflags
64 baseRegOffset dflags (YmmReg 6) = oFFSET_StgRegTable_rYMM6 dflags
65 baseRegOffset _ (YmmReg n) = panic ("Registers above YMM6 are not supported (tried to use YMM" ++ show n ++ ")")
66 baseRegOffset dflags Sp = oFFSET_StgRegTable_rSp dflags
67 baseRegOffset dflags SpLim = oFFSET_StgRegTable_rSpLim dflags
68 baseRegOffset dflags (LongReg 1) = oFFSET_StgRegTable_rL1 dflags
69 baseRegOffset _ (LongReg n) = panic ("Registers above L1 are not supported (tried to use L" ++ show n ++ ")")
70 baseRegOffset dflags Hp = oFFSET_StgRegTable_rHp dflags
71 baseRegOffset dflags HpLim = oFFSET_StgRegTable_rHpLim dflags
72 baseRegOffset dflags CCCS = oFFSET_StgRegTable_rCCCS dflags
73 baseRegOffset dflags CurrentTSO = oFFSET_StgRegTable_rCurrentTSO dflags
74 baseRegOffset dflags CurrentNursery = oFFSET_StgRegTable_rCurrentNursery dflags
75 baseRegOffset dflags HpAlloc = oFFSET_StgRegTable_rHpAlloc dflags
76 baseRegOffset dflags EagerBlackholeInfo = oFFSET_stgEagerBlackholeInfo dflags
77 baseRegOffset dflags GCEnter1 = oFFSET_stgGCEnter1 dflags
78 baseRegOffset dflags GCFun = oFFSET_stgGCFun dflags
79 baseRegOffset _ BaseReg = panic "baseRegOffset:BaseReg"
80 baseRegOffset _ PicBaseReg = panic "baseRegOffset:PicBaseReg"
81
82
83 -- -----------------------------------------------------------------------------
84 --
85 -- STG/Cmm GlobalReg
86 --
87 -- -----------------------------------------------------------------------------
88
89 -- | We map STG registers onto appropriate CmmExprs. Either they map
90 -- to real machine registers or stored as offsets from BaseReg. Given
91 -- a GlobalReg, get_GlobalReg_addr always produces the
92 -- register table address for it.
93 get_GlobalReg_addr :: DynFlags -> GlobalReg -> CmmExpr
94 get_GlobalReg_addr dflags BaseReg = regTableOffset dflags 0
95 get_GlobalReg_addr dflags mid
96 = get_Regtable_addr_from_offset dflags
97 (globalRegType dflags mid) (baseRegOffset dflags mid)
98
99 -- Calculate a literal representing an offset into the register table.
100 -- Used when we don't have an actual BaseReg to offset from.
101 regTableOffset :: DynFlags -> Int -> CmmExpr
102 regTableOffset dflags n =
103 CmmLit (CmmLabelOff mkMainCapabilityLabel (oFFSET_Capability_r dflags + n))
104
105 get_Regtable_addr_from_offset :: DynFlags -> CmmType -> Int -> CmmExpr
106 get_Regtable_addr_from_offset dflags _ offset =
107 if haveRegBase (targetPlatform dflags)
108 then CmmRegOff (CmmGlobal BaseReg) offset
109 else regTableOffset dflags offset
110
111 -- | Fixup global registers so that they assign to locations within the
112 -- RegTable if they aren't pinned for the current target.
113 fixStgRegisters :: DynFlags -> RawCmmDecl -> RawCmmDecl
114 fixStgRegisters _ top@(CmmData _ _) = top
115
116 fixStgRegisters dflags (CmmProc info lbl live graph) =
117 let graph' = modifyGraph (mapGraphBlocks (fixStgRegBlock dflags)) graph
118 in CmmProc info lbl live graph'
119
120 fixStgRegBlock :: DynFlags -> Block CmmNode e x -> Block CmmNode e x
121 fixStgRegBlock dflags block = mapBlock (fixStgRegStmt dflags) block
122
123 fixStgRegStmt :: DynFlags -> CmmNode e x -> CmmNode e x
124 fixStgRegStmt dflags stmt = fixAssign $ mapExpDeep fixExpr stmt
125 where
126 platform = targetPlatform dflags
127
128 fixAssign stmt =
129 case stmt of
130 CmmAssign (CmmGlobal reg) src ->
131 let baseAddr = get_GlobalReg_addr dflags reg
132 in case reg `elem` activeStgRegs (targetPlatform dflags) of
133 True -> CmmAssign (CmmGlobal reg) src
134 False -> CmmStore baseAddr src
135 other_stmt -> other_stmt
136
137 fixExpr expr = case expr of
138 CmmReg (CmmGlobal reg) ->
139 -- Replace register leaves with appropriate StixTrees for
140 -- the given target. MagicIds which map to a reg on this
141 -- arch are left unchanged. For the rest, BaseReg is taken
142 -- to mean the address of the reg table in MainCapability,
143 -- and for all others we generate an indirection to its
144 -- location in the register table.
145 case reg `elem` activeStgRegs platform of
146 True -> expr
147 False ->
148 let baseAddr = get_GlobalReg_addr dflags reg
149 in case reg of
150 BaseReg -> baseAddr
151 _other -> CmmLoad baseAddr (globalRegType dflags reg)
152
153 CmmRegOff (CmmGlobal reg) offset ->
154 -- RegOf leaves are just a shorthand form. If the reg maps
155 -- to a real reg, we keep the shorthand, otherwise, we just
156 -- expand it and defer to the above code.
157 case reg `elem` activeStgRegs platform of
158 True -> expr
159 False -> CmmMachOp (MO_Add (wordWidth dflags)) [
160 fixExpr (CmmReg (CmmGlobal reg)),
161 CmmLit (CmmInt (fromIntegral offset)
162 (wordWidth dflags))]
163
164 other_expr -> other_expr
165