9 #include "HsVersions.h"
13 import ZipCfgCmmRep (Convention(..))
16 import StaticFlags (opt_Unregisterised)
20 -- Calculate the 'GlobalReg' or stack locations for function call
21 -- parameters as used by the Cmm calling convention.
24 = RegisterParam GlobalReg
27 instance (Outputable a) => Outputable (ParamLocation a) where
28 ppr (RegisterParam g) = ppr g
29 ppr (StackParam p) = ppr p
31 type ArgumentFormat a b = [(a, ParamLocation b)]
33 -- Stack parameters are returned as word offsets.
34 assignArguments :: (a -> CmmType) -> [a] -> ArgumentFormat a WordOff
35 assignArguments f reps = assignments
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
43 (assignment, new_offset, size, remaining) =
44 assign_reg assign_slot_neg (f r) offset availRegs
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 = map cvt assignments
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
63 _ -> pprPanic "Unknown calling convention" (ppr conv)
64 (sizes, assignments) = unzip $ assignArguments' reps (sum sizes) regs
65 assignArguments' [] _ _ = []
66 assignArguments' (r:rs) offset avails =
67 (size, (r,assignment)):assignArguments' rs new_offset remaining
69 (assignment, new_offset, size, remaining) =
70 assign_reg assign_slot_pos (arg_ty r) offset avails
71 cvt (l, RegisterParam r) = (l, RegisterParam r)
72 cvt (l, StackParam off) = (l, StackParam $ off * wORD_SIZE)
74 argumentsSize :: (a -> CmmType) -> [a] -> WordOff
75 argumentsSize f reps = maximum (0 : map arg_top args)
77 args = assignArguments f reps
78 arg_top (_, StackParam offset) = -offset
79 arg_top (_, RegisterParam _) = 0
81 -----------------------------------------------------------------------------
82 -- Local information about the registers available
84 type AvailRegs = ( [VGcPtr -> GlobalReg] -- available vanilla regs.
85 , [GlobalReg] -- floats
86 , [GlobalReg] -- doubles
87 , [GlobalReg] -- longs (int64 and word64)
90 -- Vanilla registers can contain pointers, Ints, Chars.
91 -- Floats and doubles have separate register supplies.
93 -- We take these register supplies from the *real* registers, i.e. those
94 -- that are guaranteed to map to machine registers.
96 vanillaRegNos, floatRegNos, doubleRegNos, longRegNos :: [Int]
97 vanillaRegNos | opt_Unregisterised = []
98 | otherwise = regList mAX_Real_Vanilla_REG
99 floatRegNos | opt_Unregisterised = []
100 | otherwise = regList mAX_Real_Float_REG
101 doubleRegNos | opt_Unregisterised = []
102 | otherwise = regList mAX_Real_Double_REG
103 longRegNos | opt_Unregisterised = []
104 | otherwise = regList mAX_Real_Long_REG
107 getRegsWithoutNode, getRegsWithNode :: AvailRegs
109 (filter (\r -> r VGcPtr /= node) intRegs,
110 map FloatReg floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
111 where intRegs = map VanillaReg vanillaRegNos
113 (intRegs, map FloatReg floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
114 where intRegs = map VanillaReg vanillaRegNos
116 allVanillaRegNos, allFloatRegNos, allDoubleRegNos, allLongRegNos :: [Int]
117 allVanillaRegNos = regList mAX_Vanilla_REG
118 allFloatRegNos = regList mAX_Float_REG
119 allDoubleRegNos = regList mAX_Double_REG
120 allLongRegNos = regList mAX_Long_REG
122 regList :: Int -> [Int]
126 allRegs = (map VanillaReg allVanillaRegNos, map FloatReg allFloatRegNos,
127 map DoubleReg allDoubleRegNos, map LongReg allLongRegNos)
130 noRegs = ([], [], [], [])
132 -- Round the size of a local register up to the nearest word.
136 slot_size :: LocalReg -> Int
137 slot_size reg = slot_size' (typeWidth (localRegType reg))
140 slot_size' :: Width -> Int
141 slot_size' reg = ((widthInBytes reg - 1) `div` wORD_SIZE) + 1
143 type Assignment = (ParamLocation WordOff, WordOff, WordOff, AvailRegs)
144 type SlotAssigner = Width -> Int -> AvailRegs -> Assignment
146 assign_reg :: SlotAssigner -> CmmType -> WordOff -> AvailRegs -> Assignment
147 assign_reg slot ty off avails
148 | isFloatType ty = assign_float_reg slot width off avails
149 | otherwise = assign_bits_reg slot width off gcp avails
152 gcp | isGcPtrType ty = VGcPtr
153 | otherwise = VNonGcPtr
155 -- Assigning a slot using negative offsets from the stack pointer.
156 -- JD: I don't know why this convention stops using all the registers
157 -- after running out of one class of registers.
158 assign_slot_neg :: SlotAssigner
159 assign_slot_neg width off _regs =
160 (StackParam $ off, off + size, size, ([], [], [], [])) where size = slot_size' width
162 -- Assigning a slot using positive offsets into a CallArea.
163 assign_slot_pos :: SlotAssigner
164 assign_slot_pos width off _regs =
165 (StackParam $ off, off - size, size, ([], [], [], []))
166 where size = slot_size' width
168 -- On calls in the native convention, `node` is used to hold the environment
169 -- for the closure, so we can't pass arguments in that register.
170 assign_bits_reg :: SlotAssigner -> Width -> WordOff -> VGcPtr -> AvailRegs -> Assignment
171 assign_bits_reg _ W128 _ _ _ = panic "W128 is not a supported register type"
172 assign_bits_reg _ w off gcp (v:vs, fs, ds, ls)
173 | widthInBits w <= widthInBits wordWidth =
174 (RegisterParam (v gcp), off, 0, (vs, fs, ds, ls))
175 assign_bits_reg _ w off _ (vs, fs, ds, l:ls)
176 | widthInBits w > widthInBits wordWidth =
177 (RegisterParam l, off, 0, (vs, fs, ds, ls))
178 assign_bits_reg assign_slot w off _ regs@(_, _, _, _) = assign_slot w off regs
180 assign_float_reg :: SlotAssigner -> Width -> WordOff -> AvailRegs -> Assignment
181 assign_float_reg _ W32 off (vs, f:fs, ds, ls) = (RegisterParam $ f, off, 0, (vs, fs, ds, ls))
182 assign_float_reg _ W64 off (vs, fs, d:ds, ls) = (RegisterParam $ d, off, 0, (vs, fs, ds, ls))
183 assign_float_reg _ W80 _ _ = panic "F80 is not a supported register type"
184 assign_float_reg assign_slot width off r = assign_slot width off r