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