1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
%
% (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
%
\section{Generate Java}
\begin{code}
module JavaGen( javaGen ) where
import Java
import Literal ( Literal(..) )
import Id ( Id, isDataConId_maybe, isId, idName, isDeadBinder )
import Name ( NamedThing(..), getOccString, isGlobalName )
import DataCon ( DataCon, dataConRepArity, dataConId )
import qualified CoreSyn
import CoreSyn ( CoreBind, CoreExpr, CoreAlt, CoreBndr,
Bind(..), Alt, AltCon(..), collectBinders, isValArg
)
import CoreUtils( exprIsValue, exprIsTrivial )
import Module ( Module, moduleString )
import TyCon ( TyCon, isDataTyCon, tyConDataCons )
import Outputable
#include "HsVersions.h"
\end{code}
\begin{code}
javaGen :: Module -> [Module] -> [TyCon] -> [CoreBind] -> CompilationUnit
javaGen mod import_mods tycons binds
= Package [moduleString mod] decls
where
decls = [Import [moduleString mod] | mod <- import_mods] ++
concat (map javaTyCon (filter isDataTyCon tycons)) ++
concat (map javaTopBind binds)
\end{code}
%************************************************************************
%* *
\subsection{Type declarations}
%* *
%************************************************************************
\begin{code}
javaTyCon :: TyCon -> [Decl]
-- public class List {}
--
-- public class $wCons extends List {
-- Object f1; Object f2
-- }
-- public class $wNil extends List {}
javaTyCon tycon
= tycon_jclass : map constr_class constrs
where
constrs = tyConDataCons tycon
tycon_jclass_jname = javaName tycon
tycon_jclass = Class [Public] tycon_jclass_jname [] [] []
constr_class data_con
= Class [Public] constr_jname [tycon_jclass_jname] [] field_decls
where
constr_jname = javaConstrWkrName data_con
enter_meth = Method [Public] objectType enterName [] stmts
n_val_args = dataConRepArity data_con
field_names = map fieldName [1..n_val_args]
field_decls = [Field [Public] objectType f Nothing | f <- field_names]
stmts = vmCOLLECT n_val_args (Var thisName) ++
[var [Final] objectType f vmPOP | f <- field_names] ++
[Return (New constr_jname (map Var field_names) Nothing)]
\end{code}
%************************************************************************
%* *
\subsection{Bindings}
%* *
%************************************************************************
\begin{code}
javaTopBind :: CoreBind -> [Decl]
javaTopBind (NonRec bndr rhs) = [java_top_bind bndr rhs]
javaTopBind (Rec prs) = [java_top_bind bndr rhs | (bndr,rhs) <- prs]
java_top_bind :: Id -> CoreExpr -> Decl
-- public class f implements Code {
-- public Object ENTER() { ...translation of rhs... }
-- }
java_top_bind bndr rhs
= Class [Public] (javaName bndr) [] [codeName] [enter_meth]
where
enter_meth = Method [Public] objectType enterName [] (javaExpr rhs)
\end{code}
%************************************************************************
%* *
\subsection{Expressions}
%* *
%************************************************************************
\begin{code}
javaVar :: Id -> Expr
javaVar v | isGlobalName (idName v) = New (javaName v) [] Nothing
| otherwise = Var (javaName v)
javaLit :: Literal.Literal -> Lit
javaLit (MachInt i) = UIntLit (fromInteger i)
javaLit (MachChar c) = UCharLit c
javaLit other = pprPanic "javaLit" (ppr other)
javaExpr :: CoreExpr -> [Statement]
-- Generate code to apply the value of
-- the expression to the arguments aleady on the stack
javaExpr (CoreSyn.Var v) = [Return (javaVar v)]
javaExpr (CoreSyn.Lit l) = [Return (Literal (javaLit l))]
javaExpr (CoreSyn.App f a) = javaApp f [a]
javaExpr e@(CoreSyn.Lam _ _) = javaLam (collectBinders e)
javaExpr (CoreSyn.Case e x alts) = javaCase e x alts
javaExpr (CoreSyn.Let bind body) = javaBind bind ++ javaExpr body
javaExpr (CoreSyn.Note _ e) = javaExpr e
javaCase :: CoreExpr -> Id -> [CoreAlt] -> [Statement]
-- case e of x { Nil -> r1
-- Cons p q -> r2 }
-- ==>
-- final Object x = VM.WHNF(...code for e...)
-- else if x instance_of Nil {
-- ...translation of r1...
-- } else if x instance_of Cons {
-- final Object p = ((Cons) x).f1
-- final Object q = ((Cons) x).f2
-- ...translation of r2...
-- } else return null
javaCase e x alts
= [var [Final] objectType (javaName x) (vmWHNF (javaArg e)),
IfThenElse (map mk_alt alts) Nothing]
where
mk_alt (DEFAULT, [], rhs) = (true, Block (javaExpr rhs))
mk_alt (DataAlt d, bs, rhs) = (instanceOf x d, Block (bind_args d bs ++ javaExpr rhs))
mk_alt alt@(LitAlt _, _, _) = pprPanic "mk_alt" (ppr alt)
bind_args d bs = [var [Final] objectType (javaName b)
(Access (Cast (Type (javaConstrWkrName d)) (javaVar x)) f)
| (b, f) <- filter isId bs `zip` map fieldName [1..],
not (isDeadBinder b)
]
javaBind (NonRec x rhs)
{-
x = ...rhs_x...
==>
final Object x = new Thunk( new Code() { ...code for rhs_x... } )
-}
= [var [Final] objectType (javaName x) (javaArg rhs)]
javaBind (Rec prs)
{- rec { x = ...rhs_x...; y = ...rhs_y... }
==>
class x implements Code {
Code x, y;
public Object ENTER() { ...code for rhs_x...}
}
...ditto for y...
final x x_inst = new x();
...ditto for y...
final Thunk x = new Thunk( x_inst );
...ditto for y...
x_inst.x = x;
x_inst.y = y;
...ditto for y...
-}
= (map mk_class prs) ++ (map mk_inst prs) ++
(map mk_thunk prs) ++ concat (map mk_knot prs)
where
mk_class (b,r) = Declaration (Class [] (javaName b) [] [codeName] stmts)
where
stmts = [Field [] codeType (javaName b) Nothing | (b,_) <- prs] ++
[Method [Public] objectType enterName [] (javaExpr r)]
mk_inst (b,r) = var [Final] (Type (javaName b)) (javaInstName b)
(New (javaName b) [] Nothing)
mk_thunk (b,r) = var [Final] thunkType (javaName b)
(New thunkName [Var (javaInstName b)] Nothing)
mk_knot (b,_) = [ExprStatement (Assign lhs rhs)
| (b',_) <- prs,
let lhs = Access (Var (javaInstName b)) (javaName b'),
let rhs = Var (javaName b')
]
javaLam :: ([CoreBndr], CoreExpr) -> [Statement]
javaLam (bndrs, body)
| null val_bndrs = javaExpr body
| otherwise
= vmCOLLECT (length val_bndrs) (Var thisName)
++ [var [Final] objectType (javaName n) vmPOP | n <- val_bndrs]
++ javaExpr body
where
val_bndrs = filter isId bndrs
javaApp :: CoreExpr -> [CoreExpr] -> [Statement]
javaApp (CoreSyn.App f a) as = javaApp f (a:as)
javaApp (CoreSyn.Var f) as
= case isDataConId_maybe f of {
Just dc | length as == dataConRepArity dc
-> -- Saturated constructors
[Return (New (javaName f) (javaArgs as) Nothing)]
; other -> -- Not a saturated constructor
java_apply (CoreSyn.Var f) as
}
javaApp f as = java_apply f as
java_apply :: CoreExpr -> [CoreExpr] -> [Statement]
java_apply f as = [ExprStatement (vmPUSH arg) | arg <- javaArgs as] ++ javaExpr f
javaArgs :: [CoreExpr] -> [Expr]
javaArgs args = [javaArg a | a <- args, isValArg a]
javaArg :: CoreExpr -> Expr
javaArg (CoreSyn.Type t) = pprPanic "javaArg" (ppr t)
javaArg e | exprIsValue e || exprIsTrivial e = newCode (javaExpr e)
| otherwise = newThunk (newCode (javaExpr e))
\end{code}
%************************************************************************
%* *
\subsection{Helper functions}
%* *
%************************************************************************
\begin{code}
true, this :: Expr
this = Var thisName
true = Var ["true"]
vmCOLLECT :: Int -> Expr -> [Statement]
vmCOLLECT 0 e = []
vmCOLLECT n e = [ExprStatement (Call (Var vmName) ["COLLECT"] [Literal (IntLit n), e])]
vmPOP :: Expr
vmPOP = Call (Var vmName) ["POP"] []
vmPUSH :: Expr -> Expr
vmPUSH e = Call (Var vmName) ["PUSH"] [e]
var :: [Modifier] -> Type -> Name -> Expr -> Statement
var ms ty field_name value = Declaration (Field ms ty field_name (Just value))
vmWHNF :: Expr -> Expr
vmWHNF e = Call (Var vmName) ["WHNF"] [e]
instanceOf :: Id -> DataCon -> Expr
instanceOf x data_con
= InstanceOf (Var (javaName x)) (Type (javaConstrWkrName data_con))
newCode :: [Statement] -> Expr
newCode [Return e] = e
newCode stmts = New codeName [] (Just [Method [Public] objectType enterName [] stmts])
newThunk :: Expr -> Expr
newThunk e = New thunkName [e] Nothing
\end{code}
%************************************************************************
%* *
\subsection{Name mangling}
%* *
%************************************************************************
\begin{code}
codeName, enterName, vmName :: Name
codeName = ["Code"]
thunkName = ["Thunk"]
enterName = ["ENTER"]
vmName = ["VM"]
thisName = ["this"]
fieldName :: Int -> Name -- Names for fields of a constructor
fieldName n = ["f" ++ show n]
javaName :: NamedThing a => a -> Name
javaName n = [getOccString n]
javaConstrWkrName :: DataCon -> Name
-- The function that makes the constructor
javaConstrWkrName con = [getOccString (dataConId con)]
javaInstName :: NamedThing a => a -> Name
-- Makes x_inst for Rec decls
javaInstName n = [getOccString n ++ "_inst"]
\end{code}
%************************************************************************
%* *
\subsection{Type mangling}
%* *
%************************************************************************
\begin{code}
codeType, thunkType, objectType :: Type
objectType = Type ["Object"]
codeType = Type codeName
thunkType = Type thunkName
\end{code}
|