Fixed calling convention for unboxed tuples
[ghc-hetmet.git] / compiler / cmm / CmmCallConv.hs
1 module CmmCallConv (
2   ParamLocation(..),
3   ArgumentFormat,
4   assignArguments,
5   assignArgumentsPos,
6   argumentsSize,
7 ) where
8
9 #include "HsVersions.h"
10
11 import Cmm
12 import SMRep
13 import ZipCfgCmmRep (Convention(..))
14
15 import Constants
16 import qualified Data.List as L
17 import StaticFlags (opt_Unregisterised)
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 a
24   = RegisterParam GlobalReg
25   | StackParam a
26
27 instance (Outputable a) => Outputable (ParamLocation a) where
28   ppr (RegisterParam g) = ppr g
29   ppr (StackParam p)    = ppr p
30
31 type ArgumentFormat a b = [(a, ParamLocation b)]
32
33 -- Stack parameters are returned as word offsets.
34 assignArguments :: (a -> CmmType) -> [a] -> ArgumentFormat a WordOff
35 assignArguments f reps = panic "assignArguments only used in dead codegen" -- assignments
36     where
37       availRegs = getRegsWithNode
38       (sizes, assignments) = unzip $ assignArguments' reps (negate (sum sizes)) availRegs
39       assignArguments' [] _ _ = []
40       assignArguments' (r:rs) offset availRegs =
41           (size,(r,assignment)):assignArguments' rs new_offset remaining
42           where 
43             (assignment, new_offset, size, remaining) =
44                 assign_reg assign_slot_neg (f r) offset availRegs
45
46 -- | JD: For the new stack story, I want arguments passed on the stack to manifest as
47 -- positive offsets in a CallArea, not negative offsets from the stack pointer.
48 -- Also, I want byte offsets, not word offsets.
49 assignArgumentsPos :: (Outputable a) => Convention -> (a -> CmmType) -> [a] ->
50                       ArgumentFormat a ByteOff
51 assignArgumentsPos conv arg_ty reps = assignments -- old_assts'
52     where -- The calling conventions (CgCallConv.hs) are complicated, to say the least
53       regs = case (reps, conv) of
54                (_,   NativeNodeCall)   -> getRegsWithNode
55                (_,   NativeDirectCall) -> getRegsWithoutNode
56                ([_], NativeReturn)     -> allRegs
57                (_,   NativeReturn)     -> getRegsWithNode
58                (_,   GC)               -> getRegsWithNode
59                (_,   PrimOpCall)       -> allRegs
60                ([_], PrimOpReturn)     -> allRegs
61                (_,   PrimOpReturn)     -> getRegsWithNode
62                (_,   Slow)             -> noRegs
63                _ -> pprPanic "Unknown calling convention" (ppr conv)
64       -- The calling conventions first assign arguments to registers,
65       -- then switch to the stack when we first run out of registers
66       -- (even if there are still available registers for args of a
67       -- different type).
68       -- When returning an unboxed tuple, we also separate the stack
69       -- arguments by pointerhood.
70       (reg_assts, stk_args) = assign_regs [] reps regs
71       stk_args' = case conv of NativeReturn -> part
72                                PrimOpReturn -> part
73                                _            -> stk_args
74                   where part = uncurry (++)
75                                        (L.partition (not . isGcPtrType . arg_ty) stk_args)
76       stk_assts = assign_stk 0 [] (reverse stk_args')
77       assignments = reg_assts ++ stk_assts
78
79       assign_regs assts []     _    = (assts, [])
80       assign_regs assts (r:rs) regs = if isFloatType ty then float else int
81         where float = case (w, regs) of
82                         (W32, (vs, f:fs, ds, ls)) -> k (RegisterParam f, (vs, fs, ds, ls))
83                         (W64, (vs, fs, d:ds, ls)) -> k (RegisterParam d, (vs, fs, ds, ls))
84                         (W80, _) -> panic "F80 unsupported register type"
85                         _ -> (assts, (r:rs))
86               int = case (w, regs) of
87                       (W128, _) -> panic "W128 unsupported register type"
88                       (_, (v:vs, fs, ds, ls)) | widthInBits w <= widthInBits wordWidth
89                           -> k (RegisterParam (v gcp), (vs, fs, ds, ls))
90                       (_, (vs, fs, ds, l:ls)) | widthInBits w > widthInBits wordWidth
91                           -> k (RegisterParam l, (vs, fs, ds, ls))
92                       _   -> (assts, (r:rs))
93               k (asst, regs') = assign_regs ((r, asst) : assts) rs regs'
94               ty = arg_ty r
95               w  = typeWidth ty
96               gcp | isGcPtrType ty = VGcPtr
97                   | otherwise      = VNonGcPtr
98
99       assign_stk offset assts [] = assts
100       assign_stk offset assts (r:rs) = assign_stk off' ((r, StackParam off') : assts) rs
101         where w    = typeWidth (arg_ty r)
102               size = (((widthInBytes w - 1) `div` wORD_SIZE) + 1) * wORD_SIZE
103               off' = offset + size
104        
105      
106       -- DEAD CODE:
107       (old_sizes, old_assignments)  = unzip $ assignArguments' reps (sum old_sizes) regs
108       old_assts' = map cvt old_assignments
109
110       assignArguments' [] _ _ = []
111       assignArguments' (r:rs) offset avails =
112           (size, (r,assignment)):assignArguments' rs new_offset remaining
113           where 
114             (assignment, new_offset, size, remaining) =
115                 assign_reg assign_slot_pos (arg_ty r) offset avails
116       cvt (l, RegisterParam r) = (l, RegisterParam r)
117       cvt (l, StackParam off)  = (l, StackParam $ off * wORD_SIZE)
118
119 argumentsSize :: (a -> CmmType) -> [a] -> WordOff
120 argumentsSize f reps = maximum (0 : map arg_top args)
121     where
122       args = assignArguments f reps
123       arg_top (_, StackParam offset) = -offset
124       arg_top (_, RegisterParam _) = 0
125
126 -----------------------------------------------------------------------------
127 -- Local information about the registers available
128
129 type AvailRegs = ( [VGcPtr -> GlobalReg]   -- available vanilla regs.
130                  , [GlobalReg]   -- floats
131                  , [GlobalReg]   -- doubles
132                  , [GlobalReg]   -- longs (int64 and word64)
133                  )
134
135 -- Vanilla registers can contain pointers, Ints, Chars.
136 -- Floats and doubles have separate register supplies.
137 --
138 -- We take these register supplies from the *real* registers, i.e. those
139 -- that are guaranteed to map to machine registers.
140
141 vanillaRegNos, floatRegNos, doubleRegNos, longRegNos :: [Int]
142 vanillaRegNos | opt_Unregisterised = []
143               | otherwise          = regList mAX_Real_Vanilla_REG
144 floatRegNos       | opt_Unregisterised = []
145               | otherwise          = regList mAX_Real_Float_REG
146 doubleRegNos  | opt_Unregisterised = []
147               | otherwise          = regList mAX_Real_Double_REG
148 longRegNos        | opt_Unregisterised = []
149               | otherwise          = regList mAX_Real_Long_REG
150
151 -- 
152 getRegsWithoutNode, getRegsWithNode :: AvailRegs
153 getRegsWithoutNode =
154   (filter (\r -> r VGcPtr /= node) intRegs,
155    map FloatReg  floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
156     where intRegs = map VanillaReg vanillaRegNos
157 getRegsWithNode =
158   (intRegs, map FloatReg  floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
159     where intRegs = map VanillaReg vanillaRegNos
160
161 allVanillaRegNos, allFloatRegNos, allDoubleRegNos, allLongRegNos :: [Int]
162 allVanillaRegNos = regList mAX_Vanilla_REG
163 allFloatRegNos   = regList mAX_Float_REG
164 allDoubleRegNos  = regList mAX_Double_REG
165 allLongRegNos      = regList mAX_Long_REG
166
167 regList :: Int -> [Int]
168 regList n = [1 .. n]
169
170 allRegs :: AvailRegs
171 allRegs = (map VanillaReg allVanillaRegNos, map FloatReg allFloatRegNos,
172            map DoubleReg  allDoubleRegNos,  map LongReg  allLongRegNos)
173
174 noRegs :: AvailRegs
175 noRegs    = ([], [], [], [])
176
177 -- Round the size of a local register up to the nearest word.
178 {-
179 UNUSED 2008-12-29
180
181 slot_size :: LocalReg -> Int
182 slot_size reg = slot_size' (typeWidth (localRegType reg))
183 -}
184
185 slot_size' :: Width -> Int
186 slot_size' reg = ((widthInBytes reg - 1) `div` wORD_SIZE) + 1
187
188 type Assignment = (ParamLocation WordOff, WordOff, WordOff, AvailRegs)
189 type SlotAssigner = Width -> Int -> AvailRegs -> Assignment
190
191 assign_reg :: SlotAssigner -> CmmType -> WordOff -> AvailRegs -> Assignment
192 assign_reg slot ty off avails
193   | isFloatType ty = assign_float_reg slot width off avails
194   | otherwise      = assign_bits_reg  slot width off gcp avails
195   where
196     width = typeWidth ty
197     gcp | isGcPtrType ty = VGcPtr
198         | otherwise      = VNonGcPtr
199
200 -- Assigning a slot using negative offsets from the stack pointer.
201 -- JD: I don't know why this convention stops using all the registers
202 --     after running out of one class of registers, but that's how it is.
203 assign_slot_neg :: SlotAssigner
204 assign_slot_neg width off _regs =
205   (StackParam $ off, off + size, size, ([], [], [], [])) where size = slot_size' width
206
207 -- Assigning a slot using positive offsets into a CallArea.
208 assign_slot_pos :: SlotAssigner
209 assign_slot_pos width off _regs =
210   (StackParam $ off, off - size, size, ([], [], [], []))
211   where size = slot_size' width
212
213 -- On calls in the native convention, `node` is used to hold the environment
214 -- for the closure, so we can't pass arguments in that register.
215 assign_bits_reg :: SlotAssigner -> Width -> WordOff -> VGcPtr -> AvailRegs -> Assignment
216 assign_bits_reg _ W128 _ _ _ = panic "W128 is not a supported register type"
217 assign_bits_reg _ w off gcp (v:vs, fs, ds, ls)
218   | widthInBits w <= widthInBits wordWidth =
219         (RegisterParam (v gcp), off, 0, (vs, fs, ds, ls))
220 assign_bits_reg _ w off _ (vs, fs, ds, l:ls)
221   | widthInBits w > widthInBits wordWidth =
222         (RegisterParam l, off, 0, (vs, fs, ds, ls))
223 assign_bits_reg assign_slot w off _ regs@(_, _, _, _) = assign_slot w off regs
224
225 assign_float_reg :: SlotAssigner -> Width -> WordOff -> AvailRegs -> Assignment
226 assign_float_reg _ W32 off (vs, f:fs, ds, ls) = (RegisterParam $ f, off, 0, (vs, fs, ds, ls))
227 assign_float_reg _ W64 off (vs, fs, d:ds, ls) = (RegisterParam $ d, off, 0, (vs, fs, ds, ls))
228 assign_float_reg _ W80 _   _                  = panic "F80 is not a supported register type"
229 assign_float_reg assign_slot width off r = assign_slot width off r