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