Code simplifications due to call/return separation; some improvements to how node...
[ghc-hetmet.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 = 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
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 assignArgumentsPos :: (Outputable a) => Convention -> (a -> CmmType) -> [a] ->
50                       ArgumentFormat a ByteOff
51 assignArgumentsPos conv arg_ty reps = map cvt assignments
52     where -- The calling conventions (CgCallConv.hs) are complicated, to say the least
53       regs = case (reps, conv) of
54                (_,   NativeNodeCall)   -> getRegsWithNode
55                (_,   NativeDirectCall) -> getRegsWithoutNode
56                ([_], NativeReturn)     -> allRegs
57                (_,   NativeReturn)     -> getRegsWithNode
58                (_,   GC)               -> getRegsWithNode
59                (_,   PrimOpCall)       -> allRegs
60                ([_], PrimOpReturn)     -> allRegs
61                (_,   PrimOpReturn)     -> getRegsWithNode
62                (_,   Slow)             -> noRegs
63                _ -> pprPanic "Unknown calling convention" (ppr conv)
64       (sizes, assignments) = unzip $ assignArguments' reps (sum sizes) regs
65       assignArguments' [] _ _ = []
66       assignArguments' (r:rs) offset avails =
67           (size, (r,assignment)):assignArguments' rs new_offset remaining
68           where 
69             (assignment, new_offset, size, remaining) =
70                 assign_reg assign_slot_pos (arg_ty r) offset avails
71       cvt (l, RegisterParam r) = (l, RegisterParam r)
72       cvt (l, StackParam off)  = (l, StackParam $ off * wORD_SIZE)
73
74 argumentsSize :: (a -> CmmType) -> [a] -> WordOff
75 argumentsSize f reps = maximum (0 : map arg_top args)
76     where
77       args = assignArguments f reps
78       arg_top (_, StackParam offset) = -offset
79       arg_top (_, RegisterParam _) = 0
80
81 -----------------------------------------------------------------------------
82 -- Local information about the registers available
83
84 type AvailRegs = ( [VGcPtr -> GlobalReg]   -- available vanilla regs.
85                  , [GlobalReg]   -- floats
86                  , [GlobalReg]   -- doubles
87                  , [GlobalReg]   -- longs (int64 and word64)
88                  )
89
90 -- Vanilla registers can contain pointers, Ints, Chars.
91 -- Floats and doubles have separate register supplies.
92 --
93 -- We take these register supplies from the *real* registers, i.e. those
94 -- that are guaranteed to map to machine registers.
95
96 vanillaRegNos, floatRegNos, doubleRegNos, longRegNos :: [Int]
97 vanillaRegNos | opt_Unregisterised = []
98               | otherwise          = regList mAX_Real_Vanilla_REG
99 floatRegNos       | opt_Unregisterised = []
100               | otherwise          = regList mAX_Real_Float_REG
101 doubleRegNos  | opt_Unregisterised = []
102               | otherwise          = regList mAX_Real_Double_REG
103 longRegNos        | opt_Unregisterised = []
104               | otherwise          = regList mAX_Real_Long_REG
105
106 -- 
107 getRegsWithoutNode, getRegsWithNode :: AvailRegs
108 getRegsWithoutNode =
109   (filter (\r -> r VGcPtr /= node) intRegs,
110    map FloatReg  floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
111     where intRegs = map VanillaReg vanillaRegNos
112 getRegsWithNode =
113   (intRegs, map FloatReg  floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
114     where intRegs = map VanillaReg vanillaRegNos
115
116 allVanillaRegNos, allFloatRegNos, allDoubleRegNos, allLongRegNos :: [Int]
117 allVanillaRegNos = regList mAX_Vanilla_REG
118 allFloatRegNos   = regList mAX_Float_REG
119 allDoubleRegNos  = regList mAX_Double_REG
120 allLongRegNos      = regList mAX_Long_REG
121
122 regList :: Int -> [Int]
123 regList n = [1 .. n]
124
125 allRegs :: AvailRegs
126 allRegs = (map VanillaReg allVanillaRegNos, map FloatReg allFloatRegNos,
127            map DoubleReg  allDoubleRegNos,  map LongReg  allLongRegNos)
128
129 noRegs :: AvailRegs
130 noRegs    = ([], [], [], [])
131
132 -- Round the size of a local register up to the nearest word.
133 {-
134 UNUSED 2008-12-29
135
136 slot_size :: LocalReg -> Int
137 slot_size reg = slot_size' (typeWidth (localRegType reg))
138 -}
139
140 slot_size' :: Width -> Int
141 slot_size' reg = ((widthInBytes reg - 1) `div` wORD_SIZE) + 1
142
143 type Assignment = (ParamLocation WordOff, WordOff, WordOff, AvailRegs)
144 type SlotAssigner = Width -> Int -> AvailRegs -> Assignment
145
146 assign_reg :: SlotAssigner -> CmmType -> WordOff -> AvailRegs -> Assignment
147 assign_reg slot ty off avails
148   | isFloatType ty = assign_float_reg slot width off avails
149   | otherwise      = assign_bits_reg  slot width off gcp avails
150   where
151     width = typeWidth ty
152     gcp | isGcPtrType ty = VGcPtr
153         | otherwise      = VNonGcPtr
154
155 -- Assigning a slot using negative offsets from the stack pointer.
156 -- JD: I don't know why this convention stops using all the registers
157 --     after running out of one class of registers.
158 assign_slot_neg :: SlotAssigner
159 assign_slot_neg width off _regs =
160   (StackParam $ off, off + size, size, ([], [], [], [])) where size = slot_size' width
161
162 -- Assigning a slot using positive offsets into a CallArea.
163 assign_slot_pos :: SlotAssigner
164 assign_slot_pos width off _regs =
165   (StackParam $ off, off - size, size, ([], [], [], []))
166   where size = slot_size' width
167
168 -- On calls in the native convention, `node` is used to hold the environment
169 -- for the closure, so we can't pass arguments in that register.
170 assign_bits_reg :: SlotAssigner -> Width -> WordOff -> VGcPtr -> AvailRegs -> Assignment
171 assign_bits_reg _ W128 _ _ _ = panic "W128 is not a supported register type"
172 assign_bits_reg _ w off gcp (v:vs, fs, ds, ls)
173   | widthInBits w <= widthInBits wordWidth =
174         pprTrace "long regs" (ppr ls <+> ppr wordWidth <+> ppr mAX_Real_Long_REG) $ (RegisterParam (v gcp), off, 0, (vs, fs, ds, ls))
175 assign_bits_reg _ w off _ (vs, fs, ds, l:ls)
176   | widthInBits w > widthInBits wordWidth =
177         pprTrace "long regs" (ppr ls <+> ppr wordWidth <+> ppr mAX_Real_Long_REG) $ (RegisterParam l, off, 0, (vs, fs, ds, ls))
178 assign_bits_reg assign_slot w off _ regs@(_, _, _, ls) =
179   pprTrace "long regs" (ppr w <+> ppr ls <+> ppr wordWidth <+> ppr mAX_Real_Long_REG <+> ppr mAX_Long_REG) $ assign_slot w off regs
180
181 assign_float_reg :: SlotAssigner -> Width -> WordOff -> AvailRegs -> Assignment
182 assign_float_reg _ W32 off (vs, f:fs, ds, ls) = (RegisterParam $ f, off, 0, (vs, fs, ds, ls))
183 assign_float_reg _ W64 off (vs, fs, d:ds, ls) = (RegisterParam $ d, off, 0, (vs, fs, ds, ls))
184 assign_float_reg _ W80 _   _                  = panic "F80 is not a supported register type"
185 assign_float_reg assign_slot width off r = assign_slot width off r