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 calling convention *must* put values in registers
52 (_, PrimOpCall) -> allRegs
53 ([_], PrimOpReturn) -> allRegs
54 (_, PrimOpReturn) -> getRegsWithNode
56 _ -> pprPanic "Unknown calling convention" (ppr conv)
57 -- The calling conventions first assign arguments to registers,
58 -- then switch to the stack when we first run out of registers
59 -- (even if there are still available registers for args of a different type).
60 -- When returning an unboxed tuple, we also separate the stack
61 -- arguments by pointerhood.
62 (reg_assts, stk_args) = assign_regs [] reps regs
63 stk_args' = case conv of NativeReturn -> part
65 GC | length stk_args /= 0 -> panic "Failed to allocate registers for GC call"
67 where part = uncurry (++)
68 (L.partition (not . isGcPtrType . arg_ty) stk_args)
69 stk_assts = assign_stk 0 [] (reverse stk_args')
70 assignments = reg_assts ++ stk_assts
72 assign_regs assts [] _ = (assts, [])
73 assign_regs assts (r:rs) regs = if isFloatType ty then float else int
74 where float = case (w, regs) of
75 (W32, (vs, f:fs, ds, ls)) -> k (RegisterParam f, (vs, fs, ds, ls))
76 (W64, (vs, fs, d:ds, ls)) -> k (RegisterParam d, (vs, fs, ds, ls))
77 (W80, _) -> panic "F80 unsupported register type"
79 int = case (w, regs) of
80 (W128, _) -> panic "W128 unsupported register type"
81 (_, (v:vs, fs, ds, ls)) | widthInBits w <= widthInBits wordWidth
82 -> k (RegisterParam (v gcp), (vs, fs, ds, ls))
83 (_, (vs, fs, ds, l:ls)) | widthInBits w > widthInBits wordWidth
84 -> k (RegisterParam l, (vs, fs, ds, ls))
86 k (asst, regs') = assign_regs ((r, asst) : assts) rs regs'
89 gcp | isGcPtrType ty = VGcPtr
90 | otherwise = VNonGcPtr
92 assign_stk _ assts [] = assts
93 assign_stk offset assts (r:rs) = assign_stk off' ((r, StackParam off') : assts) rs
94 where w = typeWidth (arg_ty r)
95 size = (((widthInBytes w - 1) `div` wORD_SIZE) + 1) * wORD_SIZE
99 argumentsSize :: (a -> CmmType) -> [a] -> WordOff
100 argumentsSize f reps = maximum (0 : map arg_top args)
102 args = assignArguments f reps
103 arg_top (_, StackParam offset) = -offset
104 arg_top (_, RegisterParam _) = 0
106 -----------------------------------------------------------------------------
107 -- Local information about the registers available
109 type AvailRegs = ( [VGcPtr -> GlobalReg] -- available vanilla regs.
110 , [GlobalReg] -- floats
111 , [GlobalReg] -- doubles
112 , [GlobalReg] -- longs (int64 and word64)
115 -- Vanilla registers can contain pointers, Ints, Chars.
116 -- Floats and doubles have separate register supplies.
118 -- We take these register supplies from the *real* registers, i.e. those
119 -- that are guaranteed to map to machine registers.
121 vanillaRegNos, floatRegNos, doubleRegNos, longRegNos :: [Int]
122 vanillaRegNos | opt_Unregisterised = []
123 | otherwise = regList mAX_Real_Vanilla_REG
124 floatRegNos | opt_Unregisterised = []
125 | otherwise = regList mAX_Real_Float_REG
126 doubleRegNos | opt_Unregisterised = []
127 | otherwise = regList mAX_Real_Double_REG
128 longRegNos | opt_Unregisterised = []
129 | otherwise = regList mAX_Real_Long_REG
132 getRegsWithoutNode, getRegsWithNode :: AvailRegs
134 (filter (\r -> r VGcPtr /= node) intRegs,
135 map FloatReg floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
136 where intRegs = map VanillaReg vanillaRegNos
138 (intRegs, map FloatReg floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
139 where intRegs = map VanillaReg vanillaRegNos
141 allVanillaRegNos, allFloatRegNos, allDoubleRegNos, allLongRegNos :: [Int]
142 allVanillaRegNos = regList mAX_Vanilla_REG
143 allFloatRegNos = regList mAX_Float_REG
144 allDoubleRegNos = regList mAX_Double_REG
145 allLongRegNos = regList mAX_Long_REG
147 regList :: Int -> [Int]
151 allRegs = (map VanillaReg allVanillaRegNos, map FloatReg allFloatRegNos,
152 map DoubleReg allDoubleRegNos, map LongReg allLongRegNos)
155 noRegs = ([], [], [], [])