Fold integer-gmp.git into ghc.git (re #8545)
[ghc.git] / compiler / codeGen / StgCmmArgRep.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Argument representations used in StgCmmLayout.
4 --
5 -- (c) The University of Glasgow 2013
6 --
7 -----------------------------------------------------------------------------
8
9 module StgCmmArgRep (
10 ArgRep(..), toArgRep, argRepSizeW,
11
12 argRepString, isNonV, idArgRep,
13
14 slowCallPattern,
15
16 ) where
17
18 import StgCmmClosure ( idPrimRep )
19
20 import SMRep ( WordOff )
21 import Id ( Id )
22 import TyCon ( PrimRep(..), primElemRepSizeB )
23 import BasicTypes ( RepArity )
24 import Constants ( wORD64_SIZE )
25 import DynFlags
26
27 import Outputable
28 import FastString
29
30 -- I extricated this code as this new module in order to avoid a
31 -- cyclic dependency between StgCmmLayout and StgCmmTicky.
32 --
33 -- NSF 18 Feb 2013
34
35 -------------------------------------------------------------------------
36 -- Classifying arguments: ArgRep
37 -------------------------------------------------------------------------
38
39 -- ArgRep is re-exported by StgCmmLayout, but only for use in the
40 -- byte-code generator which also needs to know about the
41 -- classification of arguments.
42
43 data ArgRep = P -- GC Ptr
44 | N -- Word-sized non-ptr
45 | L -- 64-bit non-ptr (long)
46 | V -- Void
47 | F -- Float
48 | D -- Double
49 | V16 -- 16-byte (128-bit) vectors of Float/Double/Int8/Word32/etc.
50 | V32 -- 32-byte (256-bit) vectors of Float/Double/Int8/Word32/etc.
51 | V64 -- 64-byte (512-bit) vectors of Float/Double/Int8/Word32/etc.
52 instance Outputable ArgRep where ppr = text . argRepString
53
54 argRepString :: ArgRep -> String
55 argRepString P = "P"
56 argRepString N = "N"
57 argRepString L = "L"
58 argRepString V = "V"
59 argRepString F = "F"
60 argRepString D = "D"
61 argRepString V16 = "V16"
62 argRepString V32 = "V32"
63 argRepString V64 = "V64"
64
65 toArgRep :: PrimRep -> ArgRep
66 toArgRep VoidRep = V
67 toArgRep PtrRep = P
68 toArgRep IntRep = N
69 toArgRep WordRep = N
70 toArgRep AddrRep = N
71 toArgRep Int64Rep = L
72 toArgRep Word64Rep = L
73 toArgRep FloatRep = F
74 toArgRep DoubleRep = D
75 toArgRep (VecRep len elem) = case len*primElemRepSizeB elem of
76 16 -> V16
77 32 -> V32
78 64 -> V64
79 _ -> error "toArgRep: bad vector primrep"
80
81 isNonV :: ArgRep -> Bool
82 isNonV V = False
83 isNonV _ = True
84
85 argRepSizeW :: DynFlags -> ArgRep -> WordOff -- Size in words
86 argRepSizeW _ N = 1
87 argRepSizeW _ P = 1
88 argRepSizeW _ F = 1
89 argRepSizeW dflags L = wORD64_SIZE `quot` wORD_SIZE dflags
90 argRepSizeW dflags D = dOUBLE_SIZE dflags `quot` wORD_SIZE dflags
91 argRepSizeW _ V = 0
92 argRepSizeW dflags V16 = 16 `quot` wORD_SIZE dflags
93 argRepSizeW dflags V32 = 32 `quot` wORD_SIZE dflags
94 argRepSizeW dflags V64 = 64 `quot` wORD_SIZE dflags
95
96 idArgRep :: Id -> ArgRep
97 idArgRep = toArgRep . idPrimRep
98
99 -- This list of argument patterns should be kept in sync with at least
100 -- the following:
101 --
102 -- * StgCmmLayout.stdPattern maybe to some degree?
103 --
104 -- * the RTS_RET(stg_ap_*) and RTS_FUN_DECL(stg_ap_*_fast)
105 -- declarations in includes/stg/MiscClosures.h
106 --
107 -- * the SLOW_CALL_*_ctr declarations in includes/stg/Ticky.h,
108 --
109 -- * the TICK_SLOW_CALL_*() #defines in includes/Cmm.h,
110 --
111 -- * the PR_CTR(SLOW_CALL_*_ctr) calls in rts/Ticky.c,
112 --
113 -- * and the SymI_HasProto(stg_ap_*_{ret,info,fast}) calls and
114 -- SymI_HasProto(SLOW_CALL_*_ctr) calls in rts/Linker.c
115 --
116 -- There may be more places that I haven't found; I merely igrep'd for
117 -- pppppp and excluded things that seemed ghci-specific.
118 --
119 -- Also, it seems at the moment that ticky counters with void
120 -- arguments will never be bumped, but I'm still declaring those
121 -- counters, defensively.
122 --
123 -- NSF 6 Mar 2013
124
125 slowCallPattern :: [ArgRep] -> (FastString, RepArity)
126 -- Returns the generic apply function and arity
127 --
128 -- The first batch of cases match (some) specialised entries
129 -- The last group deals exhaustively with the cases for the first argument
130 -- (and the zero-argument case)
131 --
132 -- In 99% of cases this function will match *all* the arguments in one batch
133
134 slowCallPattern (P: P: P: P: P: P: _) = (fsLit "stg_ap_pppppp", 6)
135 slowCallPattern (P: P: P: P: P: _) = (fsLit "stg_ap_ppppp", 5)
136 slowCallPattern (P: P: P: P: _) = (fsLit "stg_ap_pppp", 4)
137 slowCallPattern (P: P: P: V: _) = (fsLit "stg_ap_pppv", 4)
138 slowCallPattern (P: P: P: _) = (fsLit "stg_ap_ppp", 3)
139 slowCallPattern (P: P: V: _) = (fsLit "stg_ap_ppv", 3)
140 slowCallPattern (P: P: _) = (fsLit "stg_ap_pp", 2)
141 slowCallPattern (P: V: _) = (fsLit "stg_ap_pv", 2)
142 slowCallPattern (P: _) = (fsLit "stg_ap_p", 1)
143 slowCallPattern (V: _) = (fsLit "stg_ap_v", 1)
144 slowCallPattern (N: _) = (fsLit "stg_ap_n", 1)
145 slowCallPattern (F: _) = (fsLit "stg_ap_f", 1)
146 slowCallPattern (D: _) = (fsLit "stg_ap_d", 1)
147 slowCallPattern (L: _) = (fsLit "stg_ap_l", 1)
148 slowCallPattern (V16: _) = (fsLit "stg_ap_v16", 1)
149 slowCallPattern (V32: _) = (fsLit "stg_ap_v32", 1)
150 slowCallPattern (V64: _) = (fsLit "stg_ap_v64", 1)
151 slowCallPattern [] = (fsLit "stg_ap_0", 0)