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