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