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