de86c4ed330f024688f0a907085bc9fdc51bcb8d
[ghc.git] / compiler / cmm / CmmCallConv.hs
1
2 module CmmCallConv (
3 ParamLocation(..),
4 assignArgumentsPos,
5 assignStack,
6 realArgRegsCover
7 ) where
8
9 #include "HsVersions.h"
10
11 import CmmExpr
12 import SMRep
13 import Cmm (Convention(..))
14 import PprCmm ()
15
16 import DynFlags
17 import Platform
18 import Outputable
19
20 -- Calculate the 'GlobalReg' or stack locations for function call
21 -- parameters as used by the Cmm calling convention.
22
23 data ParamLocation
24 = RegisterParam GlobalReg
25 | StackParam ByteOff
26
27 instance Outputable ParamLocation where
28 ppr (RegisterParam g) = ppr g
29 ppr (StackParam p) = ppr p
30
31 -- |
32 -- Given a list of arguments, and a function that tells their types,
33 -- return a list showing where each argument is passed
34 --
35 assignArgumentsPos :: DynFlags
36 -> ByteOff -- stack offset to start with
37 -> Convention
38 -> (a -> CmmType) -- how to get a type from an arg
39 -> [a] -- args
40 -> (
41 ByteOff -- bytes of stack args
42 , [(a, ParamLocation)] -- args and locations
43 )
44
45 assignArgumentsPos dflags off conv arg_ty reps = (stk_off, assignments)
46 where
47 regs = case (reps, conv) of
48 (_, NativeNodeCall) -> getRegsWithNode dflags
49 (_, NativeDirectCall) -> getRegsWithoutNode dflags
50 ([_], NativeReturn) -> allRegs dflags
51 (_, NativeReturn) -> getRegsWithNode dflags
52 -- GC calling convention *must* put values in registers
53 (_, GC) -> allRegs dflags
54 (_, Slow) -> nodeOnly
55 -- The calling conventions first assign arguments to registers,
56 -- then switch to the stack when we first run out of registers
57 -- (even if there are still available registers for args of a
58 -- different type). When returning an unboxed tuple, we also
59 -- separate the stack arguments by pointerhood.
60 (reg_assts, stk_args) = assign_regs [] reps regs
61 (stk_off, stk_assts) = assignStack dflags off arg_ty stk_args
62 assignments = reg_assts ++ stk_assts
63
64 assign_regs assts [] _ = (assts, [])
65 assign_regs assts (r:rs) regs | isVecType ty = vec
66 | isFloatType ty = float
67 | otherwise = int
68 where vec = case (w, regs) of
69 (W128, (vs, fs, ds, ls, s:ss)) -> k (RegisterParam (XmmReg s), (vs, fs, ds, ls, ss))
70 (W256, (vs, fs, ds, ls, s:ss)) -> k (RegisterParam (YmmReg s), (vs, fs, ds, ls, ss))
71 _ -> (assts, (r:rs))
72 float = case (w, regs) of
73 (W32, (vs, fs, ds, ls, s:ss))
74 | passFloatInXmm -> k (RegisterParam (FloatReg s), (vs, fs, ds, ls, ss))
75 (W32, (vs, f:fs, ds, ls, ss))
76 | not passFloatInXmm -> k (RegisterParam f, (vs, fs, ds, ls, ss))
77 (W64, (vs, fs, ds, ls, s:ss))
78 | passFloatInXmm -> k (RegisterParam (DoubleReg s), (vs, fs, ds, ls, ss))
79 (W64, (vs, fs, d:ds, ls, ss))
80 | not passFloatInXmm -> k (RegisterParam d, (vs, fs, ds, ls, ss))
81 (W80, _) -> panic "F80 unsupported register type"
82 _ -> (assts, (r:rs))
83 int = case (w, regs) of
84 (W128, _) -> panic "W128 unsupported register type"
85 (_, (v:vs, fs, ds, ls, ss)) | widthInBits w <= widthInBits (wordWidth dflags)
86 -> k (RegisterParam (v gcp), (vs, fs, ds, ls, ss))
87 (_, (vs, fs, ds, l:ls, ss)) | widthInBits w > widthInBits (wordWidth dflags)
88 -> k (RegisterParam l, (vs, fs, ds, ls, ss))
89 _ -> (assts, (r:rs))
90 k (asst, regs') = assign_regs ((r, asst) : assts) rs regs'
91 ty = arg_ty r
92 w = typeWidth ty
93 gcp | isGcPtrType ty = VGcPtr
94 | otherwise = VNonGcPtr
95 passFloatInXmm = passFloatArgsInXmm dflags
96
97 passFloatArgsInXmm :: DynFlags -> Bool
98 passFloatArgsInXmm dflags = case platformArch (targetPlatform dflags) of
99 ArchX86_64 -> True
100 _ -> False
101
102 assignStack :: DynFlags -> ByteOff -> (a -> CmmType) -> [a]
103 -> (
104 ByteOff -- bytes of stack args
105 , [(a, ParamLocation)] -- args and locations
106 )
107 assignStack dflags offset arg_ty args = assign_stk offset [] (reverse args)
108 where
109 assign_stk offset assts [] = (offset, assts)
110 assign_stk offset assts (r:rs)
111 = assign_stk off' ((r, StackParam off') : assts) rs
112 where w = typeWidth (arg_ty r)
113 size = (((widthInBytes w - 1) `div` word_size) + 1) * word_size
114 off' = offset + size
115 word_size = wORD_SIZE dflags
116
117 -----------------------------------------------------------------------------
118 -- Local information about the registers available
119
120 type AvailRegs = ( [VGcPtr -> GlobalReg] -- available vanilla regs.
121 , [GlobalReg] -- floats
122 , [GlobalReg] -- doubles
123 , [GlobalReg] -- longs (int64 and word64)
124 , [Int] -- XMM (floats and doubles)
125 )
126
127 -- Vanilla registers can contain pointers, Ints, Chars.
128 -- Floats and doubles have separate register supplies.
129 --
130 -- We take these register supplies from the *real* registers, i.e. those
131 -- that are guaranteed to map to machine registers.
132
133 getRegsWithoutNode, getRegsWithNode :: DynFlags -> AvailRegs
134 getRegsWithoutNode dflags =
135 ( filter (\r -> r VGcPtr /= node) (realVanillaRegs dflags)
136 , realFloatRegs dflags
137 , realDoubleRegs dflags
138 , realLongRegs dflags
139 , realXmmRegNos dflags)
140
141 -- getRegsWithNode uses R1/node even if it isn't a register
142 getRegsWithNode dflags =
143 ( if null (realVanillaRegs dflags)
144 then [VanillaReg 1]
145 else realVanillaRegs dflags
146 , realFloatRegs dflags
147 , realDoubleRegs dflags
148 , realLongRegs dflags
149 , realXmmRegNos dflags)
150
151 allFloatRegs, allDoubleRegs, allLongRegs :: DynFlags -> [GlobalReg]
152 allVanillaRegs :: DynFlags -> [VGcPtr -> GlobalReg]
153 allXmmRegs :: DynFlags -> [Int]
154
155 allVanillaRegs dflags = map VanillaReg $ regList (mAX_Vanilla_REG dflags)
156 allFloatRegs dflags = map FloatReg $ regList (mAX_Float_REG dflags)
157 allDoubleRegs dflags = map DoubleReg $ regList (mAX_Double_REG dflags)
158 allLongRegs dflags = map LongReg $ regList (mAX_Long_REG dflags)
159 allXmmRegs dflags = regList (mAX_XMM_REG dflags)
160
161 realFloatRegs, realDoubleRegs, realLongRegs :: DynFlags -> [GlobalReg]
162 realVanillaRegs :: DynFlags -> [VGcPtr -> GlobalReg]
163 realXmmRegNos :: DynFlags -> [Int]
164
165 realVanillaRegs dflags = map VanillaReg $ regList (mAX_Real_Vanilla_REG dflags)
166 realFloatRegs dflags = map FloatReg $ regList (mAX_Real_Float_REG dflags)
167 realDoubleRegs dflags = map DoubleReg $ regList (mAX_Real_Double_REG dflags)
168 realLongRegs dflags = map LongReg $ regList (mAX_Real_Long_REG dflags)
169
170 realXmmRegNos dflags
171 | isSse2Enabled dflags = regList (mAX_Real_XMM_REG dflags)
172 | otherwise = []
173
174 regList :: Int -> [Int]
175 regList n = [1 .. n]
176
177 allRegs :: DynFlags -> AvailRegs
178 allRegs dflags = (allVanillaRegs dflags,
179 allFloatRegs dflags,
180 allDoubleRegs dflags,
181 allLongRegs dflags,
182 allXmmRegs dflags)
183
184 nodeOnly :: AvailRegs
185 nodeOnly = ([VanillaReg 1], [], [], [], [])
186
187 -- This returns the set of global registers that *cover* the machine registers
188 -- used for argument passing. On platforms where registers can overlap---right
189 -- now just x86-64, where Float and Double registers overlap---passing this set
190 -- of registers is guaranteed to preserve the contents of all live registers. We
191 -- only use this functionality in hand-written C-- code in the RTS.
192 realArgRegsCover :: DynFlags -> [GlobalReg]
193 realArgRegsCover dflags
194 | passFloatArgsInXmm dflags = map ($VGcPtr) (realVanillaRegs dflags) ++
195 realLongRegs dflags ++
196 map XmmReg (realXmmRegNos dflags)
197 | otherwise = map ($VGcPtr) (realVanillaRegs dflags) ++
198 realFloatRegs dflags ++
199 realDoubleRegs dflags ++
200 realLongRegs dflags ++
201 map XmmReg (realXmmRegNos dflags)