1 {-# OPTIONS -Wall -fno-warn-name-shadowing #-}
4 ( CmmExpr(..), cmmExprRep, maybeInvertCmmExpr
5 , CmmReg(..), cmmRegRep
6 , CmmLit(..), cmmLitRep
7 , LocalReg(..), localRegRep, localRegGCFollow, GCKind(..)
8 , GlobalReg(..), globalRegRep, spReg, hpReg, spLimReg, nodeReg, node
9 , UserOfLocalRegs, foldRegsUsed
10 , RegSet, emptyRegSet, elemRegSet, extendRegSet, deleteFromRegSet, mkRegSet
11 , plusRegSet, minusRegSet, timesRegSet
20 -----------------------------------------------------------------------------
22 -- An expression. Expressions have no side effects.
23 -----------------------------------------------------------------------------
26 = CmmLit CmmLit -- Literal
27 | CmmLoad CmmExpr MachRep -- Read memory location
28 | CmmReg CmmReg -- Contents of register
29 | CmmMachOp MachOp [CmmExpr] -- Machine operation (+, -, *, etc.)
30 | CmmRegOff CmmReg Int
32 -- ** is shorthand only, meaning **
33 -- CmmMachOp (MO_S_Add rep (CmmReg reg) (CmmLit (CmmInt i rep)))
34 -- where rep = cmmRegRep reg
43 = CmmInt Integer MachRep
44 -- Interpretation: the 2's complement representation of the value
45 -- is truncated to the specified size. This is easier than trying
46 -- to keep the value within range, because we don't know whether
47 -- it will be used as a signed or unsigned value (the MachRep doesn't
48 -- distinguish between signed & unsigned).
49 | CmmFloat Rational MachRep
50 | CmmLabel CLabel -- Address of label
51 | CmmLabelOff CLabel Int -- Address of label + byte offset
53 -- Due to limitations in the C backend, the following
54 -- MUST ONLY be used inside the info table indicated by label2
55 -- (label2 must be the info label), and label1 must be an
56 -- SRT, a slow entrypoint or a large bitmap (see the Mangler)
57 -- Don't use it at all unless tablesNextToCode.
58 -- It is also used inside the NCG during when generating
59 -- position-independent code.
60 | CmmLabelDiffOff CLabel CLabel Int -- label1 - label2 + offset
63 instance Eq LocalReg where
64 (LocalReg u1 _ _) == (LocalReg u2 _ _) = u1 == u2
66 instance Uniquable LocalReg where
67 getUnique (LocalReg uniq _ _) = uniq
70 --- Negation for conditional branches
72 maybeInvertCmmExpr :: CmmExpr -> Maybe CmmExpr
73 maybeInvertCmmExpr (CmmMachOp op args) = do op' <- maybeInvertComparison op
74 return (CmmMachOp op' args)
75 maybeInvertCmmExpr _ = Nothing
77 -----------------------------------------------------------------------------
79 -----------------------------------------------------------------------------
81 -- | Whether a 'LocalReg' is a GC followable pointer
82 data GCKind = GCKindPtr | GCKindNonPtr deriving (Eq)
86 !Unique -- ^ Identifier
88 GCKind -- ^ Should the GC follow as a pointer
90 -- | Sets of local registers
92 type RegSet = UniqSet LocalReg
94 elemRegSet :: LocalReg -> RegSet -> Bool
95 extendRegSet :: RegSet -> LocalReg -> RegSet
96 deleteFromRegSet :: RegSet -> LocalReg -> RegSet
97 mkRegSet :: [LocalReg] -> RegSet
98 minusRegSet, plusRegSet, timesRegSet :: RegSet -> RegSet -> RegSet
100 emptyRegSet = emptyUniqSet
101 elemRegSet = elementOfUniqSet
102 extendRegSet = addOneToUniqSet
103 deleteFromRegSet = delOneFromUniqSet
105 minusRegSet = minusUniqSet
106 plusRegSet = unionUniqSets
107 timesRegSet = intersectUniqSets
109 -----------------------------------------------------------------------------
110 -- Register-use information for expressions and other types
111 -----------------------------------------------------------------------------
113 class UserOfLocalRegs a where
114 foldRegsUsed :: (b -> LocalReg -> b) -> b -> a -> b
116 instance UserOfLocalRegs CmmReg where
117 foldRegsUsed f z (CmmLocal reg) = f z reg
118 foldRegsUsed _ z (CmmGlobal _) = z
120 instance UserOfLocalRegs LocalReg where
121 foldRegsUsed f z r = f z r
123 instance UserOfLocalRegs RegSet where
124 foldRegsUsed f = foldUniqSet (flip f)
126 instance UserOfLocalRegs CmmExpr where
127 foldRegsUsed f z e = expr z e
128 where expr z (CmmLit _) = z
129 expr z (CmmLoad addr _) = foldRegsUsed f z addr
130 expr z (CmmReg r) = foldRegsUsed f z r
131 expr z (CmmMachOp _ exprs) = foldRegsUsed f z exprs
132 expr z (CmmRegOff r _) = foldRegsUsed f z r
134 instance UserOfLocalRegs a => UserOfLocalRegs [a] where
135 foldRegsUsed _ set [] = set
136 foldRegsUsed f set (x:xs) = foldRegsUsed f (foldRegsUsed f set x) xs
138 -----------------------------------------------------------------------------
140 -----------------------------------------------------------------------------
144 cmmExprRep :: CmmExpr -> MachRep
145 cmmExprRep (CmmLit lit) = cmmLitRep lit
146 cmmExprRep (CmmLoad _ rep) = rep
147 cmmExprRep (CmmReg reg) = cmmRegRep reg
148 cmmExprRep (CmmMachOp op _) = resultRepOfMachOp op
149 cmmExprRep (CmmRegOff reg _) = cmmRegRep reg
151 cmmRegRep :: CmmReg -> MachRep
152 cmmRegRep (CmmLocal reg) = localRegRep reg
153 cmmRegRep (CmmGlobal reg) = globalRegRep reg
155 localRegRep :: LocalReg -> MachRep
156 localRegRep (LocalReg _ rep _) = rep
159 localRegGCFollow :: LocalReg -> GCKind
160 localRegGCFollow (LocalReg _ _ p) = p
162 cmmLitRep :: CmmLit -> MachRep
163 cmmLitRep (CmmInt _ rep) = rep
164 cmmLitRep (CmmFloat _ rep) = rep
165 cmmLitRep (CmmLabel _) = wordRep
166 cmmLitRep (CmmLabelOff _ _) = wordRep
167 cmmLitRep (CmmLabelDiffOff _ _ _) = wordRep
169 -----------------------------------------------------------------------------
170 -- Global STG registers
171 -----------------------------------------------------------------------------
174 -- Argument and return registers
175 = VanillaReg -- pointers, unboxed ints and chars
176 {-# UNPACK #-} !Int -- its number
178 | FloatReg -- single-precision floating-point registers
179 {-# UNPACK #-} !Int -- its number
181 | DoubleReg -- double-precision floating-point registers
182 {-# UNPACK #-} !Int -- its number
184 | LongReg -- long int registers (64-bit, really)
185 {-# UNPACK #-} !Int -- its number
188 | Sp -- Stack ptr; points to last occupied stack location.
189 | SpLim -- Stack limit
190 | Hp -- Heap ptr; points to last occupied heap location.
191 | HpLim -- Heap limit register
192 | CurrentTSO -- pointer to current thread's TSO
193 | CurrentNursery -- pointer to allocation area
194 | HpAlloc -- allocation count for heap check failure
196 -- We keep the address of some commonly-called
197 -- functions in the register table, to keep code
199 | GCEnter1 -- stg_gc_enter_1
200 | GCFun -- stg_gc_fun
202 -- Base offset for the register table, used for accessing registers
203 -- which do not have real registers assigned to them. This register
204 -- will only appear after we have expanded GlobalReg into memory accesses
205 -- (where necessary) in the native code generator.
208 -- Base Register for PIC (position-independent code) calculations
209 -- Only used inside the native code generator. It's exact meaning differs
210 -- from platform to platform (see module PositionIndependentCode).
213 deriving( Eq , Show )
215 -- convenient aliases
216 spReg, hpReg, spLimReg, nodeReg :: CmmReg
219 spLimReg = CmmGlobal SpLim
220 nodeReg = CmmGlobal node
225 globalRegRep :: GlobalReg -> MachRep
226 globalRegRep (VanillaReg _) = wordRep
227 globalRegRep (FloatReg _) = F32
228 globalRegRep (DoubleReg _) = F64
229 globalRegRep (LongReg _) = I64
230 globalRegRep _ = wordRep