Fix warnings in CmmCallConv
[ghc.git] / compiler / cmm / CmmCallConv.hs
1 module CmmCallConv (
2 ParamLocation(..),
3 ArgumentFormat,
4 assignArguments,
5 assignArgumentsPos,
6 argumentsSize,
7 ) where
8
9 #include "HsVersions.h"
10
11 import Cmm
12 import SMRep
13 import ZipCfgCmmRep (Convention(..))
14
15 import Constants
16 import StaticFlags (opt_Unregisterised)
17 import Outputable
18 import Panic
19
20 -- Calculate the 'GlobalReg' or stack locations for function call
21 -- parameters as used by the Cmm calling convention.
22
23 data ParamLocation a
24 = RegisterParam GlobalReg
25 | StackParam a
26
27 instance (Outputable a) => Outputable (ParamLocation a) where
28 ppr (RegisterParam g) = ppr g
29 ppr (StackParam p) = ppr p
30
31 type ArgumentFormat a b = [(a, ParamLocation b)]
32
33 -- Stack parameters are returned as word offsets.
34 assignArguments :: (a -> CmmType) -> [a] -> ArgumentFormat a WordOff
35 assignArguments f reps = assignments
36 where
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
42 where
43 (assignment, new_offset, size, remaining) =
44 assign_reg assign_slot_neg (f r) offset availRegs
45
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
57 regs = case conv of Native -> getRegs isCall
58 GC -> getRegs False
59 PrimOp -> noStack
60 Slow -> noRegs
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
66 where
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)
71
72 argumentsSize :: (a -> CmmType) -> [a] -> WordOff
73 argumentsSize f reps = maximum (0 : map arg_top args)
74 where
75 args = assignArguments f reps
76 arg_top (_, StackParam offset) = -offset
77 arg_top (_, RegisterParam _) = 0
78
79 -----------------------------------------------------------------------------
80 -- Local information about the registers available
81
82 type AvailRegs = ( [VGcPtr -> GlobalReg] -- available vanilla regs.
83 , [GlobalReg] -- floats
84 , [GlobalReg] -- doubles
85 , [GlobalReg] -- longs (int64 and word64)
86 )
87
88 -- Vanilla registers can contain pointers, Ints, Chars.
89 -- Floats and doubles have separate register supplies.
90 --
91 -- We take these register supplies from the *real* registers, i.e. those
92 -- that are guaranteed to map to machine registers.
93
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
103
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)
110 where
111 regList f max = map f [1 .. max]
112 intRegs = regList VanillaReg useVanillaRegs
113
114 noStack :: AvailRegs
115 noStack = (map VanillaReg any, map FloatReg any, map DoubleReg any, map LongReg any)
116 where any = [1 .. ]
117
118 noRegs :: AvailRegs
119 noRegs = ([], [], [], [])
120
121 -- Round the size of a local register up to the nearest word.
122 {-
123 UNUSED 2008-12-29
124
125 slot_size :: LocalReg -> Int
126 slot_size reg = slot_size' (typeWidth (localRegType reg))
127 -}
128
129 slot_size' :: Width -> Int
130 slot_size' reg = ((widthInBytes reg - 1) `div` wORD_SIZE) + 1
131
132 type Assignment = (ParamLocation WordOff, WordOff, WordOff, AvailRegs)
133 type SlotAssigner = Width -> Int -> AvailRegs -> Assignment
134
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
139 where
140 width = typeWidth ty
141 gcp | isGcPtrType ty = VGcPtr
142 | otherwise = VNonGcPtr
143
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
150
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
156
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
160 -> Assignment
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
168
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