Remove unused imports
[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
19 -- Calculate the 'GlobalReg' or stack locations for function call
20 -- parameters as used by the Cmm calling convention.
21
22 data ParamLocation a
23   = RegisterParam GlobalReg
24   | StackParam a
25
26 instance (Outputable a) => Outputable (ParamLocation a) where
27   ppr (RegisterParam g) = ppr g
28   ppr (StackParam p)    = ppr p
29
30 type ArgumentFormat a b = [(a, ParamLocation b)]
31
32 -- Stack parameters are returned as word offsets.
33 assignArguments :: (a -> CmmType) -> [a] -> ArgumentFormat a WordOff
34 assignArguments f reps = assignments
35     where
36       availRegs = getRegsWithNode
37       (sizes, assignments) = unzip $ assignArguments' reps (negate (sum sizes)) availRegs
38       assignArguments' [] _ _ = []
39       assignArguments' (r:rs) offset availRegs =
40           (size,(r,assignment)):assignArguments' rs new_offset remaining
41           where 
42             (assignment, new_offset, size, remaining) =
43                 assign_reg assign_slot_neg (f r) offset availRegs
44
45 -- | JD: For the new stack story, I want arguments passed on the stack to manifest as
46 -- positive offsets in a CallArea, not negative offsets from the stack pointer.
47 -- Also, I want byte offsets, not word offsets.
48 assignArgumentsPos :: (Outputable a) => Convention -> (a -> CmmType) -> [a] ->
49                       ArgumentFormat a ByteOff
50 assignArgumentsPos conv arg_ty reps = map cvt assignments
51     where -- The calling conventions (CgCallConv.hs) are complicated, to say the least
52       regs = case (reps, conv) of
53                (_,   NativeNodeCall)   -> getRegsWithNode
54                (_,   NativeDirectCall) -> getRegsWithoutNode
55                ([_], NativeReturn)     -> allRegs
56                (_,   NativeReturn)     -> getRegsWithNode
57                (_,   GC)               -> getRegsWithNode
58                (_,   PrimOpCall)       -> allRegs
59                ([_], PrimOpReturn)     -> allRegs
60                (_,   PrimOpReturn)     -> getRegsWithNode
61                (_,   Slow)             -> noRegs
62                _ -> pprPanic "Unknown calling convention" (ppr conv)
63       (sizes, assignments) = unzip $ assignArguments' reps (sum sizes) regs
64       assignArguments' [] _ _ = []
65       assignArguments' (r:rs) offset avails =
66           (size, (r,assignment)):assignArguments' rs new_offset remaining
67           where 
68             (assignment, new_offset, size, remaining) =
69                 assign_reg assign_slot_pos (arg_ty r) offset avails
70       cvt (l, RegisterParam r) = (l, RegisterParam r)
71       cvt (l, StackParam off)  = (l, StackParam $ off * wORD_SIZE)
72
73 argumentsSize :: (a -> CmmType) -> [a] -> WordOff
74 argumentsSize f reps = maximum (0 : map arg_top args)
75     where
76       args = assignArguments f reps
77       arg_top (_, StackParam offset) = -offset
78       arg_top (_, RegisterParam _) = 0
79
80 -----------------------------------------------------------------------------
81 -- Local information about the registers available
82
83 type AvailRegs = ( [VGcPtr -> GlobalReg]   -- available vanilla regs.
84                  , [GlobalReg]   -- floats
85                  , [GlobalReg]   -- doubles
86                  , [GlobalReg]   -- longs (int64 and word64)
87                  )
88
89 -- Vanilla registers can contain pointers, Ints, Chars.
90 -- Floats and doubles have separate register supplies.
91 --
92 -- We take these register supplies from the *real* registers, i.e. those
93 -- that are guaranteed to map to machine registers.
94
95 vanillaRegNos, floatRegNos, doubleRegNos, longRegNos :: [Int]
96 vanillaRegNos | opt_Unregisterised = []
97               | otherwise          = regList mAX_Real_Vanilla_REG
98 floatRegNos       | opt_Unregisterised = []
99               | otherwise          = regList mAX_Real_Float_REG
100 doubleRegNos  | opt_Unregisterised = []
101               | otherwise          = regList mAX_Real_Double_REG
102 longRegNos        | opt_Unregisterised = []
103               | otherwise          = regList mAX_Real_Long_REG
104
105 -- 
106 getRegsWithoutNode, getRegsWithNode :: AvailRegs
107 getRegsWithoutNode =
108   (filter (\r -> r VGcPtr /= node) intRegs,
109    map FloatReg  floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
110     where intRegs = map VanillaReg vanillaRegNos
111 getRegsWithNode =
112   (intRegs, map FloatReg  floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
113     where intRegs = map VanillaReg vanillaRegNos
114
115 allVanillaRegNos, allFloatRegNos, allDoubleRegNos, allLongRegNos :: [Int]
116 allVanillaRegNos = regList mAX_Vanilla_REG
117 allFloatRegNos   = regList mAX_Float_REG
118 allDoubleRegNos  = regList mAX_Double_REG
119 allLongRegNos      = regList mAX_Long_REG
120
121 regList :: Int -> [Int]
122 regList n = [1 .. n]
123
124 allRegs :: AvailRegs
125 allRegs = (map VanillaReg allVanillaRegNos, map FloatReg allFloatRegNos,
126            map DoubleReg  allDoubleRegNos,  map LongReg  allLongRegNos)
127
128 noRegs :: AvailRegs
129 noRegs    = ([], [], [], [])
130
131 -- Round the size of a local register up to the nearest word.
132 {-
133 UNUSED 2008-12-29
134
135 slot_size :: LocalReg -> Int
136 slot_size reg = slot_size' (typeWidth (localRegType reg))
137 -}
138
139 slot_size' :: Width -> Int
140 slot_size' reg = ((widthInBytes reg - 1) `div` wORD_SIZE) + 1
141
142 type Assignment = (ParamLocation WordOff, WordOff, WordOff, AvailRegs)
143 type SlotAssigner = Width -> Int -> AvailRegs -> Assignment
144
145 assign_reg :: SlotAssigner -> CmmType -> WordOff -> AvailRegs -> Assignment
146 assign_reg slot ty off avails
147   | isFloatType ty = assign_float_reg slot width off avails
148   | otherwise      = assign_bits_reg  slot width off gcp avails
149   where
150     width = typeWidth ty
151     gcp | isGcPtrType ty = VGcPtr
152         | otherwise      = VNonGcPtr
153
154 -- Assigning a slot using negative offsets from the stack pointer.
155 -- JD: I don't know why this convention stops using all the registers
156 --     after running out of one class of registers.
157 assign_slot_neg :: SlotAssigner
158 assign_slot_neg width off _regs =
159   (StackParam $ off, off + size, size, ([], [], [], [])) where size = slot_size' width
160
161 -- Assigning a slot using positive offsets into a CallArea.
162 assign_slot_pos :: SlotAssigner
163 assign_slot_pos width off _regs =
164   (StackParam $ off, off - size, size, ([], [], [], []))
165   where size = slot_size' width
166
167 -- On calls in the native convention, `node` is used to hold the environment
168 -- for the closure, so we can't pass arguments in that register.
169 assign_bits_reg :: SlotAssigner -> Width -> WordOff -> VGcPtr -> AvailRegs -> Assignment
170 assign_bits_reg _ W128 _ _ _ = panic "W128 is not a supported register type"
171 assign_bits_reg _ w off gcp (v:vs, fs, ds, ls)
172   | widthInBits w <= widthInBits wordWidth =
173         (RegisterParam (v gcp), off, 0, (vs, fs, ds, ls))
174 assign_bits_reg _ w off _ (vs, fs, ds, l:ls)
175   | widthInBits w > widthInBits wordWidth =
176         (RegisterParam l, off, 0, (vs, fs, ds, ls))
177 assign_bits_reg assign_slot w off _ regs@(_, _, _, _) = assign_slot w off regs
178
179 assign_float_reg :: SlotAssigner -> Width -> WordOff -> AvailRegs -> Assignment
180 assign_float_reg _ W32 off (vs, f:fs, ds, ls) = (RegisterParam $ f, off, 0, (vs, fs, ds, ls))
181 assign_float_reg _ W64 off (vs, fs, d:ds, ls) = (RegisterParam $ d, off, 0, (vs, fs, ds, ls))
182 assign_float_reg _ W80 _   _                  = panic "F80 is not a supported register type"
183 assign_float_reg assign_slot width off r = assign_slot width off r