9 #include "HsVersions.h"
13 import Cmm (Convention(..))
17 import qualified Data.List as L
18 import StaticFlags (opt_Unregisterised)
21 -- Calculate the 'GlobalReg' or stack locations for function call
22 -- parameters as used by the Cmm calling convention.
25 = RegisterParam GlobalReg
28 instance (Outputable a) => Outputable (ParamLocation a) where
29 ppr (RegisterParam g) = ppr g
30 ppr (StackParam p) = ppr p
32 type ArgumentFormat a b = [(a, ParamLocation b)]
34 -- Stack parameters are returned as word offsets.
35 assignArguments :: (a -> CmmType) -> [a] -> ArgumentFormat a WordOff
36 assignArguments _ _ = panic "assignArguments only used in dead codegen" -- assignments
38 -- | JD: For the new stack story, I want arguments passed on the stack to manifest as
39 -- positive offsets in a CallArea, not negative offsets from the stack pointer.
40 -- Also, I want byte offsets, not word offsets.
41 assignArgumentsPos :: (Outputable a) => Convention -> (a -> CmmType) -> [a] ->
42 ArgumentFormat a ByteOff
43 assignArgumentsPos conv arg_ty reps = assignments
44 where -- The calling conventions (CgCallConv.hs) are complicated, to say the least
45 regs = case (reps, conv) of
46 (_, NativeNodeCall) -> getRegsWithNode
47 (_, NativeDirectCall) -> getRegsWithoutNode
48 ([_], NativeReturn) -> allRegs
49 (_, NativeReturn) -> getRegsWithNode
50 (_, GC) -> getRegsWithNode
51 (_, PrimOpCall) -> allRegs
52 ([_], PrimOpReturn) -> allRegs
53 (_, PrimOpReturn) -> getRegsWithNode
55 _ -> pprPanic "Unknown calling convention" (ppr conv)
56 -- The calling conventions first assign arguments to registers,
57 -- then switch to the stack when we first run out of registers
58 -- (even if there are still available registers for args of a different type).
59 -- When returning an unboxed tuple, we also separate the stack
60 -- arguments by pointerhood.
61 (reg_assts, stk_args) = assign_regs [] reps regs
62 stk_args' = case conv of NativeReturn -> part
65 where part = uncurry (++)
66 (L.partition (not . isGcPtrType . arg_ty) stk_args)
67 stk_assts = assign_stk 0 [] (reverse stk_args')
68 assignments = reg_assts ++ stk_assts
70 assign_regs assts [] _ = (assts, [])
71 assign_regs assts (r:rs) regs = if isFloatType ty then float else int
72 where float = case (w, regs) of
73 (W32, (vs, f:fs, ds, ls)) -> k (RegisterParam f, (vs, fs, ds, ls))
74 (W64, (vs, fs, d:ds, ls)) -> k (RegisterParam d, (vs, fs, ds, ls))
75 (W80, _) -> panic "F80 unsupported register type"
77 int = case (w, regs) of
78 (W128, _) -> panic "W128 unsupported register type"
79 (_, (v:vs, fs, ds, ls)) | widthInBits w <= widthInBits wordWidth
80 -> k (RegisterParam (v gcp), (vs, fs, ds, ls))
81 (_, (vs, fs, ds, l:ls)) | widthInBits w > widthInBits wordWidth
82 -> k (RegisterParam l, (vs, fs, ds, ls))
84 k (asst, regs') = assign_regs ((r, asst) : assts) rs regs'
87 gcp | isGcPtrType ty = VGcPtr
88 | otherwise = VNonGcPtr
90 assign_stk _ assts [] = assts
91 assign_stk offset assts (r:rs) = assign_stk off' ((r, StackParam off') : assts) rs
92 where w = typeWidth (arg_ty r)
93 size = (((widthInBytes w - 1) `div` wORD_SIZE) + 1) * wORD_SIZE
97 argumentsSize :: (a -> CmmType) -> [a] -> WordOff
98 argumentsSize f reps = maximum (0 : map arg_top args)
100 args = assignArguments f reps
101 arg_top (_, StackParam offset) = -offset
102 arg_top (_, RegisterParam _) = 0
104 -----------------------------------------------------------------------------
105 -- Local information about the registers available
107 type AvailRegs = ( [VGcPtr -> GlobalReg] -- available vanilla regs.
108 , [GlobalReg] -- floats
109 , [GlobalReg] -- doubles
110 , [GlobalReg] -- longs (int64 and word64)
113 -- Vanilla registers can contain pointers, Ints, Chars.
114 -- Floats and doubles have separate register supplies.
116 -- We take these register supplies from the *real* registers, i.e. those
117 -- that are guaranteed to map to machine registers.
119 vanillaRegNos, floatRegNos, doubleRegNos, longRegNos :: [Int]
120 vanillaRegNos | opt_Unregisterised = []
121 | otherwise = regList mAX_Real_Vanilla_REG
122 floatRegNos | opt_Unregisterised = []
123 | otherwise = regList mAX_Real_Float_REG
124 doubleRegNos | opt_Unregisterised = []
125 | otherwise = regList mAX_Real_Double_REG
126 longRegNos | opt_Unregisterised = []
127 | otherwise = regList mAX_Real_Long_REG
130 getRegsWithoutNode, getRegsWithNode :: AvailRegs
132 (filter (\r -> r VGcPtr /= node) intRegs,
133 map FloatReg floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
134 where intRegs = map VanillaReg vanillaRegNos
136 (intRegs, map FloatReg floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
137 where intRegs = map VanillaReg vanillaRegNos
139 allVanillaRegNos, allFloatRegNos, allDoubleRegNos, allLongRegNos :: [Int]
140 allVanillaRegNos = regList mAX_Vanilla_REG
141 allFloatRegNos = regList mAX_Float_REG
142 allDoubleRegNos = regList mAX_Double_REG
143 allLongRegNos = regList mAX_Long_REG
145 regList :: Int -> [Int]
149 allRegs = (map VanillaReg allVanillaRegNos, map FloatReg allFloatRegNos,
150 map DoubleReg allDoubleRegNos, map LongReg allLongRegNos)
153 noRegs = ([], [], [], [])