d40edae4820b1cebfb4d451eb15edd1cff1ba018
[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 StaticFlags (opt_Unregisterised)
17 import Outputable
18 import Panic
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 = 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 -- The first argument tells us whether we are assigning positions for call arguments
50 -- or return results. The distinction matters because some conventions use different
51 -- global registers in each case. In particular, the native calling convention
52 -- uses the `node' register to pass the closure environment.
53 assignArgumentsPos :: (Outputable a) => Convention -> Bool -> (a -> CmmType) -> [a] ->
54                       ArgumentFormat a ByteOff
55 assignArgumentsPos conv isCall arg_ty reps = map cvt assignments
56     where -- The calling conventions (CgCallConv.hs) are complicated, to say the least
57       regs = if isCall then
58                case (reps, conv) of
59                  (_, NativeCall) -> getRegsWithoutNode
60                  (_, GC    ) -> getRegsWithNode
61                  (_, PrimOpCall) -> allRegs
62                  (_, Slow  ) -> noRegs
63                  _ -> pprPanic "Unknown calling convention" (ppr conv)
64              else
65                case (reps, conv) of
66                  ([_], _)    -> allRegs
67                  (_, NativeCall)   -> getRegsWithNode
68                  (_, NativeReturn) -> getRegsWithNode
69                  (_, GC    ) -> getRegsWithNode
70                  (_, PrimOpReturn) -> getRegsWithNode
71                  (_, Slow  ) -> noRegs
72                  _ -> pprPanic "Unknown calling convention" (ppr conv)
73       (sizes, assignments) = unzip $ assignArguments' reps (sum sizes) regs
74       assignArguments' [] _ _ = []
75       assignArguments' (r:rs) offset avails =
76           (size, (r,assignment)):assignArguments' rs new_offset remaining
77           where 
78             (assignment, new_offset, size, remaining) =
79                 assign_reg assign_slot_pos (arg_ty r) offset avails
80       cvt (l, RegisterParam r) = (l, RegisterParam r)
81       cvt (l, StackParam off)  = (l, StackParam $ off * wORD_SIZE)
82
83 argumentsSize :: (a -> CmmType) -> [a] -> WordOff
84 argumentsSize f reps = maximum (0 : map arg_top args)
85     where
86       args = assignArguments f reps
87       arg_top (_, StackParam offset) = -offset
88       arg_top (_, RegisterParam _) = 0
89
90 -----------------------------------------------------------------------------
91 -- Local information about the registers available
92
93 type AvailRegs = ( [VGcPtr -> GlobalReg]   -- available vanilla regs.
94                  , [GlobalReg]   -- floats
95                  , [GlobalReg]   -- doubles
96                  , [GlobalReg]   -- longs (int64 and word64)
97                  )
98
99 -- Vanilla registers can contain pointers, Ints, Chars.
100 -- Floats and doubles have separate register supplies.
101 --
102 -- We take these register supplies from the *real* registers, i.e. those
103 -- that are guaranteed to map to machine registers.
104
105 vanillaRegNos, floatRegNos, doubleRegNos, longRegNos :: [Int]
106 vanillaRegNos | opt_Unregisterised = []
107               | otherwise          = regList mAX_Real_Vanilla_REG
108 floatRegNos       | opt_Unregisterised = []
109               | otherwise          = regList mAX_Real_Float_REG
110 doubleRegNos  | opt_Unregisterised = []
111               | otherwise          = regList mAX_Real_Double_REG
112 longRegNos        | opt_Unregisterised = []
113               | otherwise          = regList mAX_Real_Long_REG
114
115 -- 
116 getRegsWithoutNode, getRegsWithNode :: AvailRegs
117 getRegsWithoutNode =
118   (filter (\r -> r VGcPtr /= node) intRegs,
119    map FloatReg  floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
120     where intRegs = map VanillaReg vanillaRegNos
121 getRegsWithNode =
122   (intRegs, map FloatReg  floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
123     where intRegs = map VanillaReg vanillaRegNos
124
125 allVanillaRegNos, allFloatRegNos, allDoubleRegNos, allLongRegNos :: [Int]
126 allVanillaRegNos = regList mAX_Vanilla_REG
127 allFloatRegNos   = regList mAX_Float_REG
128 allDoubleRegNos  = regList mAX_Double_REG
129 allLongRegNos      = regList mAX_Long_REG
130
131 regList :: Int -> [Int]
132 regList n = [1 .. n]
133
134 allRegs :: AvailRegs
135 allRegs = (map VanillaReg allVanillaRegNos, map FloatReg allFloatRegNos,
136            map DoubleReg  allDoubleRegNos,  map LongReg  allLongRegNos)
137
138 noRegs :: AvailRegs
139 noRegs    = ([], [], [], [])
140
141 -- Round the size of a local register up to the nearest word.
142 {-
143 UNUSED 2008-12-29
144
145 slot_size :: LocalReg -> Int
146 slot_size reg = slot_size' (typeWidth (localRegType reg))
147 -}
148
149 slot_size' :: Width -> Int
150 slot_size' reg = ((widthInBytes reg - 1) `div` wORD_SIZE) + 1
151
152 type Assignment = (ParamLocation WordOff, WordOff, WordOff, AvailRegs)
153 type SlotAssigner = Width -> Int -> AvailRegs -> Assignment
154
155 assign_reg :: SlotAssigner -> CmmType -> WordOff -> AvailRegs -> Assignment
156 assign_reg slot ty off avails
157   | isFloatType ty = assign_float_reg slot width off avails
158   | otherwise      = assign_bits_reg  slot width off gcp avails
159   where
160     width = typeWidth ty
161     gcp | isGcPtrType ty = VGcPtr
162         | otherwise      = VNonGcPtr
163
164 -- Assigning a slot using negative offsets from the stack pointer.
165 -- JD: I don't know why this convention stops using all the registers
166 --     after running out of one class of registers.
167 assign_slot_neg :: SlotAssigner
168 assign_slot_neg width off _regs =
169   (StackParam $ off, off + size, size, ([], [], [], [])) where size = slot_size' width
170
171 -- Assigning a slot using positive offsets into a CallArea.
172 assign_slot_pos :: SlotAssigner
173 assign_slot_pos width off _regs =
174   (StackParam $ off, off - size, size, ([], [], [], []))
175   where size = slot_size' width
176
177 -- On calls in the native convention, `node` is used to hold the environment
178 -- for the closure, so we can't pass arguments in that register.
179 assign_bits_reg :: SlotAssigner -> Width -> WordOff -> VGcPtr -> AvailRegs -> Assignment
180 assign_bits_reg _ W128 _ _ _ = panic "W128 is not a supported register type"
181 assign_bits_reg _ w off gcp (v:vs, fs, ds, ls)
182   | widthInBits w <= widthInBits wordWidth =
183         pprTrace "long regs" (ppr ls <+> ppr wordWidth <+> ppr mAX_Real_Long_REG) $ (RegisterParam (v gcp), off, 0, (vs, fs, ds, ls))
184 assign_bits_reg _ w off _ (vs, fs, ds, l:ls)
185   | widthInBits w > widthInBits wordWidth =
186         pprTrace "long regs" (ppr ls <+> ppr wordWidth <+> ppr mAX_Real_Long_REG) $ (RegisterParam l, off, 0, (vs, fs, ds, ls))
187 assign_bits_reg assign_slot w off _ regs@(_, _, _, ls) =
188   pprTrace "long regs" (ppr w <+> ppr ls <+> ppr wordWidth <+> ppr mAX_Real_Long_REG <+> ppr mAX_Long_REG) $ assign_slot w off regs
189
190 assign_float_reg :: SlotAssigner -> Width -> WordOff -> AvailRegs -> Assignment
191 assign_float_reg _ W32 off (vs, f:fs, ds, ls) = (RegisterParam $ f, off, 0, (vs, fs, ds, ls))
192 assign_float_reg _ W64 off (vs, fs, d:ds, ls) = (RegisterParam $ d, off, 0, (vs, fs, ds, ls))
193 assign_float_reg _ W80 _   _                  = panic "F80 is not a supported register type"
194 assign_float_reg assign_slot width off r = assign_slot width off r