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