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 = getRegs False
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
57 regs = case conv of Native -> getRegs isCall
61 _ -> panic "unrecognized calling convention"
62 (sizes, assignments) = unzip $ assignArguments' reps (sum sizes) regs
63 assignArguments' [] _ _ = []
64 assignArguments' (r:rs) offset avails =
65 (size, (r,assignment)):assignArguments' rs new_offset remaining
67 (assignment, new_offset, size, remaining) =
68 assign_reg assign_slot_pos (arg_ty r) offset avails
69 cvt (l, RegisterParam r) = (l, RegisterParam r)
70 cvt (l, StackParam off) = (l, StackParam $ off * wORD_SIZE)
72 argumentsSize :: (a -> CmmType) -> [a] -> WordOff
73 argumentsSize f reps = maximum (0 : map arg_top args)
75 args = assignArguments f reps
76 arg_top (_, StackParam offset) = -offset
77 arg_top (_, RegisterParam _) = 0
79 -----------------------------------------------------------------------------
80 -- Local information about the registers available
82 type AvailRegs = ( [VGcPtr -> GlobalReg] -- available vanilla regs.
83 , [GlobalReg] -- floats
84 , [GlobalReg] -- doubles
85 , [GlobalReg] -- longs (int64 and word64)
88 -- Vanilla registers can contain pointers, Ints, Chars.
89 -- Floats and doubles have separate register supplies.
91 -- We take these register supplies from the *real* registers, i.e. those
92 -- that are guaranteed to map to machine registers.
94 useVanillaRegs, useFloatRegs, useDoubleRegs, useLongRegs :: Int
95 useVanillaRegs | opt_Unregisterised = 0
96 | otherwise = mAX_Real_Vanilla_REG
97 useFloatRegs | opt_Unregisterised = 0
98 | otherwise = mAX_Real_Float_REG
99 useDoubleRegs | opt_Unregisterised = 0
100 | otherwise = mAX_Real_Double_REG
101 useLongRegs | opt_Unregisterised = 0
102 | otherwise = mAX_Real_Long_REG
104 getRegs :: Bool -> AvailRegs
105 getRegs reserveNode =
106 (if reserveNode then filter (\r -> r VGcPtr /= node) intRegs else intRegs,
107 regList FloatReg useFloatRegs,
108 regList DoubleReg useDoubleRegs,
109 regList LongReg useLongRegs)
111 regList f max = map f [1 .. max]
112 intRegs = regList VanillaReg useVanillaRegs
115 noStack = (map VanillaReg any, map FloatReg any, map DoubleReg any, map LongReg any)
119 noRegs = ([], [], [], [])
121 -- Round the size of a local register up to the nearest word.
125 slot_size :: LocalReg -> Int
126 slot_size reg = slot_size' (typeWidth (localRegType reg))
129 slot_size' :: Width -> Int
130 slot_size' reg = ((widthInBytes reg - 1) `div` wORD_SIZE) + 1
132 type Assignment = (ParamLocation WordOff, WordOff, WordOff, AvailRegs)
133 type SlotAssigner = Width -> Int -> AvailRegs -> Assignment
135 assign_reg :: SlotAssigner -> CmmType -> WordOff -> AvailRegs -> Assignment
136 assign_reg slot ty off avails
137 | isFloatType ty = assign_float_reg slot width off avails
138 | otherwise = assign_bits_reg slot width off gcp avails
141 gcp | isGcPtrType ty = VGcPtr
142 | otherwise = VNonGcPtr
144 -- Assigning a slot using negative offsets from the stack pointer.
145 -- JD: I don't know why this convention stops using all the registers
146 -- after running out of one class of registers.
147 assign_slot_neg :: SlotAssigner
148 assign_slot_neg width off _regs =
149 (StackParam $ off, off + size, size, ([], [], [], [])) where size = slot_size' width
151 -- Assigning a slot using positive offsets into a CallArea.
152 assign_slot_pos :: SlotAssigner
153 assign_slot_pos width off _regs =
154 (StackParam $ off, off - size, size, ([], [], [], []))
155 where size = slot_size' width
157 -- On calls in the native convention, `node` is used to hold the environment
158 -- for the closure, so we can't pass arguments in that register.
159 assign_bits_reg :: SlotAssigner -> Width -> WordOff -> VGcPtr -> AvailRegs
161 assign_bits_reg _ W128 _ _ _ = panic "W128 is not a supported register type"
162 assign_bits_reg assign_slot w off gcp regs@(v:vs, fs, ds, ls) =
163 if widthInBits w <= widthInBits wordWidth then
164 (RegisterParam (v gcp), off, 0, (vs, fs, ds, ls))
165 else assign_slot w off regs
166 assign_bits_reg assign_slot w off _ regs@([], _, _, _) =
167 assign_slot w off regs
169 assign_float_reg :: SlotAssigner -> Width -> WordOff -> AvailRegs -> Assignment
170 assign_float_reg _ W32 off (vs, f:fs, ds, ls) = (RegisterParam $ f, off, 0, (vs, fs, ds, ls))
171 assign_float_reg _ W64 off (vs, fs, d:ds, ls) = (RegisterParam $ d, off, 0, (vs, fs, ds, ls))
172 assign_float_reg _ W80 _ _ = panic "F80 is not a supported register type"
173 assign_float_reg assign_slot width off r = assign_slot width off r