Produce new-style Cmm from the Cmm parser
[ghc.git] / compiler / cmm / CmmCallConv.hs
1 {-# OPTIONS -fno-warn-tabs #-}
2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and
4 -- detab the module (please do the detabbing in a separate patch). See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
6 -- for details
7
8 module CmmCallConv (
9 ParamLocation(..),
10 assignArgumentsPos,
11 assignStack,
12 globalArgRegs, realArgRegs
13 ) where
14
15 #include "HsVersions.h"
16
17 import CmmExpr
18 import SMRep
19 import Cmm (Convention(..))
20 import PprCmm ()
21
22 import DynFlags
23 import Outputable
24
25 -- Calculate the 'GlobalReg' or stack locations for function call
26 -- parameters as used by the Cmm calling convention.
27
28 data ParamLocation
29 = RegisterParam GlobalReg
30 | StackParam ByteOff
31
32 instance Outputable ParamLocation where
33 ppr (RegisterParam g) = ppr g
34 ppr (StackParam p) = ppr p
35
36 -- |
37 -- Given a list of arguments, and a function that tells their types,
38 -- return a list showing where each argument is passed
39 --
40 assignArgumentsPos :: DynFlags
41 -> ByteOff -- stack offset to start with
42 -> Convention
43 -> (a -> CmmType) -- how to get a type from an arg
44 -> [a] -- args
45 -> (
46 ByteOff -- bytes of stack args
47 , [(a, ParamLocation)] -- args and locations
48 )
49
50 assignArgumentsPos dflags off conv arg_ty reps = (stk_off, assignments)
51 where
52 regs = case (reps, conv) of
53 (_, NativeNodeCall) -> getRegsWithNode dflags
54 (_, NativeDirectCall) -> getRegsWithoutNode dflags
55 ([_], NativeReturn) -> allRegs dflags
56 (_, NativeReturn) -> getRegsWithNode dflags
57 -- GC calling convention *must* put values in registers
58 (_, GC) -> allRegs dflags
59 (_, Slow) -> noRegs
60 -- The calling conventions first assign arguments to registers,
61 -- then switch to the stack when we first run out of registers
62 -- (even if there are still available registers for args of a
63 -- different type). When returning an unboxed tuple, we also
64 -- separate the stack arguments by pointerhood.
65 (reg_assts, stk_args) = assign_regs [] reps regs
66 (stk_off, stk_assts) = assignStack dflags off arg_ty stk_args
67 assignments = reg_assts ++ stk_assts
68
69 assign_regs assts [] _ = (assts, [])
70 assign_regs assts (r:rs) regs = if isFloatType ty then float else int
71 where float = case (w, regs) of
72 (W32, (vs, f:fs, ds, ls)) -> k (RegisterParam f, (vs, fs, ds, ls))
73 (W64, (vs, fs, d:ds, ls)) -> k (RegisterParam d, (vs, fs, ds, ls))
74 (W80, _) -> panic "F80 unsupported register type"
75 _ -> (assts, (r:rs))
76 int = case (w, regs) of
77 (W128, _) -> panic "W128 unsupported register type"
78 (_, (v:vs, fs, ds, ls)) | widthInBits w <= widthInBits (wordWidth dflags)
79 -> k (RegisterParam (v gcp), (vs, fs, ds, ls))
80 (_, (vs, fs, ds, l:ls)) | widthInBits w > widthInBits (wordWidth dflags)
81 -> k (RegisterParam l, (vs, fs, ds, ls))
82 _ -> (assts, (r:rs))
83 k (asst, regs') = assign_regs ((r, asst) : assts) rs regs'
84 ty = arg_ty r
85 w = typeWidth ty
86 gcp | isGcPtrType ty = VGcPtr
87 | otherwise = VNonGcPtr
88
89
90 assignStack :: DynFlags -> ByteOff -> (a -> CmmType) -> [a]
91 -> (
92 ByteOff -- bytes of stack args
93 , [(a, ParamLocation)] -- args and locations
94 )
95 assignStack dflags offset arg_ty args = assign_stk offset [] (reverse args)
96 where
97 assign_stk offset assts [] = (offset, assts)
98 assign_stk offset assts (r:rs)
99 = assign_stk off' ((r, StackParam off') : assts) rs
100 where w = typeWidth (arg_ty r)
101 size = (((widthInBytes w - 1) `div` word_size) + 1) * word_size
102 off' = offset + size
103 word_size = wORD_SIZE dflags
104
105 -----------------------------------------------------------------------------
106 -- Local information about the registers available
107
108 type AvailRegs = ( [VGcPtr -> GlobalReg] -- available vanilla regs.
109 , [GlobalReg] -- floats
110 , [GlobalReg] -- doubles
111 , [GlobalReg] -- longs (int64 and word64)
112 )
113
114 -- Vanilla registers can contain pointers, Ints, Chars.
115 -- Floats and doubles have separate register supplies.
116 --
117 -- We take these register supplies from the *real* registers, i.e. those
118 -- that are guaranteed to map to machine registers.
119
120 getRegsWithoutNode, getRegsWithNode :: DynFlags -> AvailRegs
121 getRegsWithoutNode dflags =
122 ( filter (\r -> r VGcPtr /= node) (realVanillaRegs dflags)
123 , realFloatRegs dflags
124 , realDoubleRegs dflags
125 , realLongRegs dflags)
126
127 -- getRegsWithNode uses R1/node even if it isn't a register
128 getRegsWithNode dflags =
129 ( if null (realVanillaRegs dflags)
130 then [VanillaReg 1]
131 else realVanillaRegs dflags
132 , realFloatRegs dflags
133 , realDoubleRegs dflags
134 , realLongRegs dflags)
135
136 allFloatRegs, allDoubleRegs, allLongRegs :: DynFlags -> [GlobalReg]
137 allVanillaRegs :: DynFlags -> [VGcPtr -> GlobalReg]
138
139 allVanillaRegs dflags = map VanillaReg $ regList (mAX_Vanilla_REG dflags)
140 allFloatRegs dflags = map FloatReg $ regList (mAX_Float_REG dflags)
141 allDoubleRegs dflags = map DoubleReg $ regList (mAX_Double_REG dflags)
142 allLongRegs dflags = map LongReg $ regList (mAX_Long_REG dflags)
143
144 realFloatRegs, realDoubleRegs, realLongRegs :: DynFlags -> [GlobalReg]
145 realVanillaRegs :: DynFlags -> [VGcPtr -> GlobalReg]
146
147 realVanillaRegs dflags = map VanillaReg $ regList (mAX_Real_Vanilla_REG dflags)
148 realFloatRegs dflags = map FloatReg $ regList (mAX_Real_Float_REG dflags)
149 realDoubleRegs dflags = map DoubleReg $ regList (mAX_Real_Double_REG dflags)
150 realLongRegs dflags = map LongReg $ regList (mAX_Real_Long_REG dflags)
151
152 regList :: Int -> [Int]
153 regList n = [1 .. n]
154
155 allRegs :: DynFlags -> AvailRegs
156 allRegs dflags = (allVanillaRegs dflags,
157 allFloatRegs dflags,
158 allDoubleRegs dflags,
159 allLongRegs dflags)
160
161 noRegs :: AvailRegs
162 noRegs = ([], [], [], [])
163
164 globalArgRegs :: DynFlags -> [GlobalReg]
165 globalArgRegs dflags = map ($ VGcPtr) (allVanillaRegs dflags) ++
166 allFloatRegs dflags ++
167 allDoubleRegs dflags ++
168 allLongRegs dflags
169
170 realArgRegs :: DynFlags -> [GlobalReg]
171 realArgRegs dflags = map ($VGcPtr) (realVanillaRegs dflags) ++
172 realFloatRegs dflags ++
173 realDoubleRegs dflags ++
174 realLongRegs dflags