Merge branch 'master' of http://darcs.haskell.org/ghc
[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 instance Outputable ArgRep where ppr = text . argRepString
51
52 argRepString :: ArgRep -> String
53 argRepString P = "P"
54 argRepString N = "N"
55 argRepString L = "L"
56 argRepString V = "V"
57 argRepString F = "F"
58 argRepString D = "D"
59 argRepString V16 = "V16"
60
61 toArgRep :: PrimRep -> ArgRep
62 toArgRep VoidRep = V
63 toArgRep PtrRep = P
64 toArgRep IntRep = N
65 toArgRep WordRep = N
66 toArgRep AddrRep = N
67 toArgRep Int64Rep = L
68 toArgRep Word64Rep = L
69 toArgRep FloatRep = F
70 toArgRep DoubleRep = D
71 toArgRep (VecRep len elem)
72 | len*primElemRepSizeB elem == 16 = V16
73 | otherwise = error "toArgRep: bad vector primrep"
74
75 isNonV :: ArgRep -> Bool
76 isNonV V = False
77 isNonV _ = True
78
79 argRepSizeW :: DynFlags -> ArgRep -> WordOff -- Size in words
80 argRepSizeW _ N = 1
81 argRepSizeW _ P = 1
82 argRepSizeW _ F = 1
83 argRepSizeW dflags L = wORD64_SIZE `quot` wORD_SIZE dflags
84 argRepSizeW dflags D = dOUBLE_SIZE dflags `quot` wORD_SIZE dflags
85 argRepSizeW _ V = 0
86 argRepSizeW dflags V16 = 16 `quot` wORD_SIZE dflags
87
88 idArgRep :: Id -> ArgRep
89 idArgRep = toArgRep . idPrimRep
90
91 -- This list of argument patterns should be kept in sync with at least
92 -- the following:
93 --
94 -- * StgCmmLayout.stdPattern maybe to some degree?
95 --
96 -- * the RTS_RET(stg_ap_*) and RTS_FUN_DECL(stg_ap_*_fast)
97 -- declarations in includes/stg/MiscClosures.h
98 --
99 -- * the SLOW_CALL_*_ctr declarations in includes/stg/Ticky.h,
100 --
101 -- * the TICK_SLOW_CALL_*() #defines in includes/Cmm.h,
102 --
103 -- * the PR_CTR(SLOW_CALL_*_ctr) calls in rts/Ticky.c,
104 --
105 -- * and the SymI_HasProto(stg_ap_*_{ret,info,fast}) calls and
106 -- SymI_HasProto(SLOW_CALL_*_ctr) calls in rts/Linker.c
107 --
108 -- There may be more places that I haven't found; I merely igrep'd for
109 -- pppppp and excluded things that seemed ghci-specific.
110 --
111 -- Also, it seems at the moment that ticky counters with void
112 -- arguments will never be bumped, but I'm still declaring those
113 -- counters, defensively.
114 --
115 -- NSF 6 Mar 2013
116
117 -- These cases were found to cover about 99% of all slow calls:
118 slowCallPattern :: [ArgRep] -> (FastString, RepArity)
119 -- Returns the generic apply function and arity
120 slowCallPattern (P: P: P: P: P: P: _) = (fsLit "stg_ap_pppppp", 6)
121 slowCallPattern (P: P: P: P: P: _) = (fsLit "stg_ap_ppppp", 5)
122 slowCallPattern (P: P: P: P: _) = (fsLit "stg_ap_pppp", 4)
123 slowCallPattern (P: P: P: V: _) = (fsLit "stg_ap_pppv", 4)
124 slowCallPattern (P: P: P: _) = (fsLit "stg_ap_ppp", 3)
125 slowCallPattern (P: P: V: _) = (fsLit "stg_ap_ppv", 3)
126 slowCallPattern (P: P: _) = (fsLit "stg_ap_pp", 2)
127 slowCallPattern (P: V: _) = (fsLit "stg_ap_pv", 2)
128 slowCallPattern (P: _) = (fsLit "stg_ap_p", 1)
129 slowCallPattern (V: _) = (fsLit "stg_ap_v", 1)
130 slowCallPattern (N: _) = (fsLit "stg_ap_n", 1)
131 slowCallPattern (F: _) = (fsLit "stg_ap_f", 1)
132 slowCallPattern (D: _) = (fsLit "stg_ap_d", 1)
133 slowCallPattern (L: _) = (fsLit "stg_ap_l", 1)
134 slowCallPattern (V16: _) = (fsLit "stg_ap_v16", 1)
135 slowCallPattern [] = (fsLit "stg_ap_0", 0)