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