merge GHC HEAD
[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 CmmExpr
12 import SMRep
13 import Cmm (Convention(..))
14 import PprCmm ()
15
16 import Constants
17 import qualified Data.List as L
18 import StaticFlags (opt_Unregisterised)
19 import Outputable
20
21 -- Calculate the 'GlobalReg' or stack locations for function call
22 -- parameters as used by the Cmm calling convention.
23
24 data ParamLocation a
25   = RegisterParam GlobalReg
26   | StackParam a
27
28 instance (Outputable a) => Outputable (ParamLocation a) where
29   ppr (RegisterParam g) = ppr g
30   ppr (StackParam p)    = ppr p
31
32 type ArgumentFormat a b = [(a, ParamLocation b)]
33
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
37
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
51                (_,   GC)               -> allRegs
52                (_,   PrimOpCall)       -> allRegs
53                ([_], PrimOpReturn)     -> allRegs
54                (_,   PrimOpReturn)     -> getRegsWithNode
55                (_,   Slow)             -> noRegs
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
64                                PrimOpReturn -> part
65                                GC | length stk_args /= 0 -> panic "Failed to allocate registers for GC call"
66                                _            -> stk_args
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
71
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"
78                         _ -> (assts, (r:rs))
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))
85                       _   -> (assts, (r:rs))
86               k (asst, regs') = assign_regs ((r, asst) : assts) rs regs'
87               ty = arg_ty r
88               w  = typeWidth ty
89               gcp | isGcPtrType ty = VGcPtr
90                   | otherwise      = VNonGcPtr
91
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
96               off' = offset + size
97        
98      
99 argumentsSize :: (a -> CmmType) -> [a] -> WordOff
100 argumentsSize f reps = maximum (0 : map arg_top args)
101     where
102       args = assignArguments f reps
103       arg_top (_, StackParam offset) = -offset
104       arg_top (_, RegisterParam _) = 0
105
106 -----------------------------------------------------------------------------
107 -- Local information about the registers available
108
109 type AvailRegs = ( [VGcPtr -> GlobalReg]   -- available vanilla regs.
110                  , [GlobalReg]   -- floats
111                  , [GlobalReg]   -- doubles
112                  , [GlobalReg]   -- longs (int64 and word64)
113                  )
114
115 -- Vanilla registers can contain pointers, Ints, Chars.
116 -- Floats and doubles have separate register supplies.
117 --
118 -- We take these register supplies from the *real* registers, i.e. those
119 -- that are guaranteed to map to machine registers.
120
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
130
131 -- 
132 getRegsWithoutNode, getRegsWithNode :: AvailRegs
133 getRegsWithoutNode =
134   (filter (\r -> r VGcPtr /= node) intRegs,
135    map FloatReg  floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
136     where intRegs = map VanillaReg vanillaRegNos
137 getRegsWithNode =
138   (intRegs, map FloatReg  floatRegNos, map DoubleReg doubleRegNos, map LongReg longRegNos)
139     where intRegs = map VanillaReg vanillaRegNos
140
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
146
147 regList :: Int -> [Int]
148 regList n = [1 .. n]
149
150 allRegs :: AvailRegs
151 allRegs = (map VanillaReg allVanillaRegNos, map FloatReg allFloatRegNos,
152            map DoubleReg  allDoubleRegNos,  map LongReg  allLongRegNos)
153
154 noRegs :: AvailRegs
155 noRegs    = ([], [], [], [])