Make CPS account for on-stack arguments when doing the stack check
[ghc-hetmet.git] / compiler / cmm / CmmCallConv.hs
1 module CmmCallConv (
2   ParamLocation(..),
3   ArgumentFormat,
4   assignArguments,
5   argumentsSize,
6 ) where
7
8 #include "HsVersions.h"
9
10 import Cmm
11 import MachOp
12 import SMRep
13
14 import Constants
15 import StaticFlags (opt_Unregisterised)
16 import Panic
17
18 -- Calculate the 'GlobalReg' or stack locations for function call
19 -- parameters as used by the Cmm calling convention.
20
21 data ParamLocation
22   = RegisterParam GlobalReg
23   | StackParam WordOff
24
25 type ArgumentFormat a = [(a, ParamLocation)]
26
27 assignArguments :: (a -> MachRep) -> [a] -> ArgumentFormat a
28 assignArguments f reps = assignArguments' reps 0 availRegs
29     where
30       assignArguments' [] offset availRegs = []
31       assignArguments' (r:rs) offset availRegs =
32           (r,assignment):assignArguments' rs new_offset remaining
33           where 
34             (assignment, new_offset, remaining) =
35                 assign_reg (f r) offset availRegs
36
37 argumentsSize :: (a -> MachRep) -> [a] -> WordOff
38 argumentsSize f reps = maximum (0 : map arg_top args)
39     where
40       args = assignArguments f reps
41
42       arg_top (a, StackParam offset) = -offset
43       arg_top (_, RegisterParam _) = 0
44
45 -----------------------------------------------------------------------------
46 -- Local information about the registers available
47
48 type AvailRegs = ( [GlobalReg]   -- available vanilla regs.
49                  , [GlobalReg]   -- floats
50                  , [GlobalReg]   -- doubles
51                  , [GlobalReg]   -- longs (int64 and word64)
52                  )
53
54 -- Vanilla registers can contain pointers, Ints, Chars.
55 -- Floats and doubles have separate register supplies.
56 --
57 -- We take these register supplies from the *real* registers, i.e. those
58 -- that are guaranteed to map to machine registers.
59
60 useVanillaRegs | opt_Unregisterised = 0
61                | otherwise          = mAX_Real_Vanilla_REG
62 useFloatRegs   | opt_Unregisterised = 0
63                | otherwise          = mAX_Real_Float_REG
64 useDoubleRegs  | opt_Unregisterised = 0
65                | otherwise          = mAX_Real_Double_REG
66 useLongRegs    | opt_Unregisterised = 0
67                | otherwise          = mAX_Real_Long_REG
68
69 availRegs = (regList VanillaReg useVanillaRegs,
70              regList FloatReg useFloatRegs,
71              regList DoubleReg useDoubleRegs,
72              regList LongReg useLongRegs)
73     where
74       regList f max = map f [1 .. max]
75
76 slot_size :: LocalReg -> Int
77 slot_size reg =
78     ((machRepByteWidth (localRegRep reg) - 1) `div` wORD_SIZE) + 1
79
80 slot_size' :: MachRep -> Int
81 slot_size' reg = ((machRepByteWidth reg - 1) `div` wORD_SIZE) + 1
82
83 assign_reg :: MachRep -> WordOff -> AvailRegs -> (ParamLocation, WordOff, AvailRegs)
84 assign_reg I8  off (v:vs, fs, ds, ls) = (RegisterParam $ v, off, (vs, fs, ds, ls))
85 assign_reg I16 off (v:vs, fs, ds, ls) = (RegisterParam $ v, off, (vs, fs, ds, ls))
86 assign_reg I32 off (v:vs, fs, ds, ls) = (RegisterParam $ v, off, (vs, fs, ds, ls))
87 assign_reg I64 off (vs, fs, ds, l:ls) = (RegisterParam $ l, off, (vs, fs, ds, ls))
88 assign_reg I128 off _                 = panic "I128 is not a supported register type"
89 assign_reg F32 off (vs, f:fs, ds, ls) = (RegisterParam $ f, off, (vs, fs, ds, ls))
90 assign_reg F64 off (vs, fs, d:ds, ls) = (RegisterParam $ d, off, (vs, fs, ds, ls))
91 assign_reg F80 off _                  = panic "F80 is not a supported register type"
92 assign_reg reg off _                  = (StackParam $ off - size, off - size, ([], [], [], [])) where size = slot_size' reg