ae3380e41a7a3c6ef26b64c60d25133df5c007e2
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsCore.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1996
3 %
4 %************************************************************************
5 %*                                                                      *
6 \section[HsCore]{Core-syntax unfoldings in Haskell interface files}
7 %*                                                                      *
8 %************************************************************************
9
10 We could either use this, or parameterise @GenCoreExpr@ on @Types@ and
11 @TyVars@ as well.  Currently trying the former... MEGA SIGH.
12
13 \begin{code}
14 module HsCore (
15         UfExpr(..), UfAlts(..), UfBinder(..), UfNote(..),
16         UfDefault(..), UfBinding(..),
17         UfArg(..), UfPrimOp(..)
18     ) where
19
20 #include "HsVersions.h"
21
22 -- friends:
23 import HsTypes          ( HsType, pprParendHsType )
24 import Kind             ( Kind {- instance Outputable -} )
25
26 -- others:
27 import Literal          ( Literal )
28 import Util             ( panic )
29 import CostCentre
30 import Outputable
31 \end{code}
32
33 %************************************************************************
34 %*                                                                      *
35 \subsection[HsCore-types]{Types for read/written Core unfoldings}
36 %*                                                                      *
37 %************************************************************************
38
39 \begin{code}
40 data UfExpr name
41   = UfVar       name
42   | UfLit       Literal
43   | UfCon       name [UfArg name]
44   | UfPrim      (UfPrimOp name) [UfArg name]
45   | UfLam       (UfBinder name)   (UfExpr name)
46   | UfApp       (UfExpr name) (UfArg name)
47   | UfCase      (UfExpr name) (UfAlts name)
48   | UfLet       (UfBinding name)  (UfExpr name)
49   | UfNote      (UfNote name) (UfExpr name)
50
51 data UfPrimOp name
52   = UfCCallOp   FAST_STRING          -- callee
53                 Bool                 -- True <=> casm, rather than ccall
54                 Bool                 -- True <=> might cause GC
55                 [HsType name] -- arg types, incl state token
56                                      -- (which will be first)
57                 (HsType name) -- return type
58
59   | UfOtherOp   name
60
61 data UfNote name = UfSCC CostCentre
62                  | UfCoerce (HsType name)
63                  | UfInlineCall
64
65 data UfAlts name
66   = UfAlgAlts  [(name, [name], UfExpr name)]
67                 (UfDefault name)
68   | UfPrimAlts [(Literal, UfExpr name)]
69                 (UfDefault name)
70
71 data UfDefault name
72   = UfNoDefault
73   | UfBindDefault name (UfExpr name)
74
75 data UfBinding name
76   = UfNonRec    (UfBinder name)
77                 (UfExpr name)
78   | UfRec       [(UfBinder name, UfExpr name)]
79
80 data UfBinder name
81   = UfValBinder name (HsType name)
82   | UfTyBinder  name Kind
83
84 data UfArg name
85   = UfVarArg    name
86   | UfLitArg    Literal
87   | UfTyArg     (HsType name)
88 \end{code}
89
90 %************************************************************************
91 %*                                                                      *
92 \subsection[HsCore-print]{Printing Core unfoldings}
93 %*                                                                      *
94 %************************************************************************
95
96 \begin{code}
97 instance Outputable name => Outputable (UfExpr name) where
98     ppr (UfVar v) = ppr v
99     ppr (UfLit l) = ppr l
100
101     ppr (UfCon c as)
102       = hsep [text "UfCon", ppr c, ppr as, char ')']
103     ppr (UfPrim o as)
104       = hsep [text "UfPrim", ppr o, ppr as, char ')']
105
106     ppr (UfLam b body)
107       = hsep [char '\\', ppr b, ptext SLIT("->"), ppr body]
108
109     ppr (UfApp fun (UfTyArg ty))
110       = hsep [ppr fun, char '@', pprParendHsType ty]
111
112     ppr (UfApp fun (UfLitArg lit))
113       = hsep [ppr fun, ppr lit]
114
115     ppr (UfApp fun (UfVarArg var))
116       = hsep [ppr fun, ppr var]
117
118     ppr (UfCase scrut alts)
119       = hsep [ptext SLIT("case"), ppr scrut, ptext SLIT("of {"), pp_alts alts, char '}']
120       where
121         pp_alts (UfAlgAlts alts deflt)
122           = hsep [hsep (punctuate semi (map pp_alt alts)), pp_deflt deflt]
123           where
124            pp_alt (c,bs,rhs) = hsep [ppr c, ppr bs, ppr_arrow, ppr rhs]
125         pp_alts (UfPrimAlts alts deflt)
126           = hsep [hsep (punctuate semi (map pp_alt alts)), pp_deflt deflt]
127           where
128            pp_alt (l,rhs) = hsep [ppr l, ppr_arrow, ppr rhs]
129
130         pp_deflt UfNoDefault = empty
131         pp_deflt (UfBindDefault b rhs) = hsep [ppr b, ppr_arrow, ppr rhs]
132
133         ppr_arrow = ptext SLIT("->")
134
135     ppr (UfLet (UfNonRec b rhs) body)
136       = hsep [ptext SLIT("let"), ppr b, equals, ppr rhs, ptext SLIT("in"), ppr body]
137     ppr (UfLet (UfRec pairs) body)
138       = hsep [ptext SLIT("letrec"), braces (hsep (punctuate semi (map pp_pair pairs))), ptext SLIT("in"), ppr body]
139       where
140         pp_pair (b,rhs) = hsep [ppr b, equals, ppr rhs]
141
142     ppr (UfNote note body)
143       = hsep [ptext SLIT("_NOTE_ [ToDo]>"), ppr body]
144
145 instance Outputable name => Outputable (UfPrimOp name) where
146     ppr (UfCCallOp str is_casm can_gc arg_tys result_ty)
147       = let
148             before = ptext (if is_casm then SLIT("_casm_ ``") else SLIT("_ccall_ "))
149             after  = if is_casm then text "'' " else space
150         in
151         hcat [before, ptext str, after,
152                    brackets (ppr arg_tys), space, ppr result_ty]
153
154     ppr (UfOtherOp op)
155       = ppr op
156
157 instance Outputable name => Outputable (UfArg name) where
158     ppr (UfVarArg v)    = ppr v
159     ppr (UfLitArg l)    = ppr l
160     ppr (UfTyArg ty)    = pprParendHsType ty
161
162 instance Outputable name => Outputable (UfBinder name) where
163     ppr (UfValBinder name ty)  = hsep [ppr name, ptext SLIT("::"), ppr ty]
164     ppr (UfTyBinder name kind) = hsep [ppr name, ptext SLIT("::"), ppr kind]
165 \end{code}
166