Generalize CmmUnwind and pass unwind information through NCG
[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 LiftedRep = P
68 toArgRep UnliftedRep = P
69 toArgRep IntRep = N
70 toArgRep WordRep = N
71 toArgRep AddrRep = N
72 toArgRep Int64Rep = L
73 toArgRep Word64Rep = L
74 toArgRep FloatRep = F
75 toArgRep DoubleRep = D
76 toArgRep (VecRep len elem) = case len*primElemRepSizeB elem of
77 16 -> V16
78 32 -> V32
79 64 -> V64
80 _ -> error "toArgRep: bad vector primrep"
81
82 isNonV :: ArgRep -> Bool
83 isNonV V = False
84 isNonV _ = True
85
86 argRepSizeW :: DynFlags -> ArgRep -> WordOff -- Size in words
87 argRepSizeW _ N = 1
88 argRepSizeW _ P = 1
89 argRepSizeW _ F = 1
90 argRepSizeW dflags L = wORD64_SIZE `quot` wORD_SIZE dflags
91 argRepSizeW dflags D = dOUBLE_SIZE dflags `quot` wORD_SIZE dflags
92 argRepSizeW _ V = 0
93 argRepSizeW dflags V16 = 16 `quot` wORD_SIZE dflags
94 argRepSizeW dflags V32 = 32 `quot` wORD_SIZE dflags
95 argRepSizeW dflags V64 = 64 `quot` wORD_SIZE dflags
96
97 idArgRep :: Id -> ArgRep
98 idArgRep = toArgRep . idPrimRep
99
100 -- This list of argument patterns should be kept in sync with at least
101 -- the following:
102 --
103 -- * StgCmmLayout.stdPattern maybe to some degree?
104 --
105 -- * the RTS_RET(stg_ap_*) and RTS_FUN_DECL(stg_ap_*_fast)
106 -- declarations in includes/stg/MiscClosures.h
107 --
108 -- * the SLOW_CALL_*_ctr declarations in includes/stg/Ticky.h,
109 --
110 -- * the TICK_SLOW_CALL_*() #defines in includes/Cmm.h,
111 --
112 -- * the PR_CTR(SLOW_CALL_*_ctr) calls in rts/Ticky.c,
113 --
114 -- * and the SymI_HasProto(stg_ap_*_{ret,info,fast}) calls and
115 -- SymI_HasProto(SLOW_CALL_*_ctr) calls in rts/Linker.c
116 --
117 -- There may be more places that I haven't found; I merely igrep'd for
118 -- pppppp and excluded things that seemed ghci-specific.
119 --
120 -- Also, it seems at the moment that ticky counters with void
121 -- arguments will never be bumped, but I'm still declaring those
122 -- counters, defensively.
123 --
124 -- NSF 6 Mar 2013
125
126 slowCallPattern :: [ArgRep] -> (FastString, RepArity)
127 -- Returns the generic apply function and arity
128 --
129 -- The first batch of cases match (some) specialised entries
130 -- The last group deals exhaustively with the cases for the first argument
131 -- (and the zero-argument case)
132 --
133 -- In 99% of cases this function will match *all* the arguments in one batch
134
135 slowCallPattern (P: P: P: P: P: P: _) = (fsLit "stg_ap_pppppp", 6)
136 slowCallPattern (P: P: P: P: P: _) = (fsLit "stg_ap_ppppp", 5)
137 slowCallPattern (P: P: P: P: _) = (fsLit "stg_ap_pppp", 4)
138 slowCallPattern (P: P: P: V: _) = (fsLit "stg_ap_pppv", 4)
139 slowCallPattern (P: P: P: _) = (fsLit "stg_ap_ppp", 3)
140 slowCallPattern (P: P: V: _) = (fsLit "stg_ap_ppv", 3)
141 slowCallPattern (P: P: _) = (fsLit "stg_ap_pp", 2)
142 slowCallPattern (P: V: _) = (fsLit "stg_ap_pv", 2)
143 slowCallPattern (P: _) = (fsLit "stg_ap_p", 1)
144 slowCallPattern (V: _) = (fsLit "stg_ap_v", 1)
145 slowCallPattern (N: _) = (fsLit "stg_ap_n", 1)
146 slowCallPattern (F: _) = (fsLit "stg_ap_f", 1)
147 slowCallPattern (D: _) = (fsLit "stg_ap_d", 1)
148 slowCallPattern (L: _) = (fsLit "stg_ap_l", 1)
149 slowCallPattern (V16: _) = (fsLit "stg_ap_v16", 1)
150 slowCallPattern (V32: _) = (fsLit "stg_ap_v32", 1)
151 slowCallPattern (V64: _) = (fsLit "stg_ap_v64", 1)
152 slowCallPattern [] = (fsLit "stg_ap_0", 0)