-
-
Notifications
You must be signed in to change notification settings - Fork 921
/
jvm_compiler.rb
339 lines (263 loc) · 9.09 KB
/
jvm_compiler.rb
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
require 'duby'
require 'compiler/builder'
module Duby
module Compiler
class JVM
class MathCompiler
def call(compiler, call)
call.target.compile(compiler)
call.parameters.each {|param| param.compile(compiler)}
target_type = call.target.inferred_type
case target_type
when AST.type(:fixnum)
case call.name
when '-'
compiler.method.isub
when '+'
compiler.method.iadd
else
compiler.method.invokevirtual(
compiler.type_mapper[call.target.inferred_type],
call.name,
[compiler.type_mapper[call.inferred_type], *call.parameters.map {|param| compiler.type_mapper[param.inferred_type]}])
end
else
compiler.method.invokevirtual(
compiler.type_mapper[call.target.inferred_type],
call.name,
[compiler.type_mapper[call.inferred_type], *call.parameters.map {|param| compiler.type_mapper[param.inferred_type]}])
end
end
end
attr_accessor :filename, :src, :method
def initialize(filename)
@filename = filename
@src = ""
self.type_mapper[AST.type(:fixnum)] = Java::int
self.type_mapper[AST.type(:string, true)] = Java::java.lang.String[]
self.call_compilers[AST.type(:fixnum)] = MathCompiler.new
@file = ::Compiler::FileBuilder.new(filename)
@class = @file.public_class(filename.split('.')[0])
end
def compile(ast)
ast.compile(this)
end
def define_main(body)
oldmethod, @method = @method, @class.static_method("main", type_mapper[AST.type(:void)], type_mapper[AST.type(:string, true)])
@method.start
body.compile(self)
@method.returnvoid
@method.stop
@method = oldmethod
end
def define_method(name, signature, args, body)
oldmethod, @method = @method, @class.static_method(name.to_s, type_mapper[signature[:return]], *args.args.map {|arg| type_mapper[arg.inferred_type]})
@method.start
#args.call
body.compile(self)
case signature[:return]
when AST.type(:fixnum)
@method.ireturn
else
@method.aload 0
@method.areturn
end
@method.stop
@method = oldmethod
end
def declare_argument(name, type)
# declare local vars for arguments here
end
def branch(iff)
elselabel = @method.label
donelabel = @method.label
# this is ugly...need a better way to abstract the idea of compiling a
# conditional branch while still fitting into JVM opcodes
predicate = iff.condition.predicate
case iff.condition.predicate
when AST::Call
case predicate.target.inferred_type
when AST.type(:fixnum)
# fixnum conditional, so we need to use JVM opcodes
case predicate.parameters[0].inferred_type
when AST.type(:fixnum)
# fixnum on fixnum, easy
case predicate.name
when '<'
predicate.target.compile(self)
predicate.parameters[0].compile(self)
@method.if_icmpge(elselabel)
else
raise "Unknown :fixnum on :fixnum predicate operation: " + predicate.name
end
else
raise "Unknown :fixnum on " + predicate.parameters[0].inferred_type + " predicate operations: " + predicate.name
end
else
raise "Unknown " + predicate.target.inferred_type + " on " + predicate.parameters[0].inferred_type + " predicate operations: " + predicate.name
end
end
iff.body.compile(self)
@method.goto(donelabel)
elselabel.set!
iff.else.compile(self) if iff.else
donelabel.set!
end
def call(call)
call_compilers[call.target.inferred_type].call(self, call)
end
def call_compilers
@call_compilers ||= {}
end
def self_call(fcall)
fcall.parameters.each {|param| param.compile(self)}
@method.invokestatic(
@method.this,
fcall.name,
[type_mapper[fcall.inferred_type], *fcall.parameters.map {|param| type_mapper[param.inferred_type]}])
end
def local(name, type)
case type
when AST.type(:fixnum)
@method.iload(@method.local(name))
else
@method.aload(@method.local(name))
end
end
def fixnum(value)
@method.push_int(value)
end
def newline
# TODO: line numbering
end
def generate
@file.generate{|filename, builder| File.open(filename, 'w') {|f| f.write(builder.generate)}}
end
def type_mapper
@type_mapper ||= {}
end
end
end
end
if __FILE__ == $0
ast = Duby::AST.parse(File.read(ARGV[0]))
typer = Duby::Typer::Simple.new(:script)
ast.infer(typer)
typer.resolve(true)
compiler = Duby::Compiler::JVM.new(ARGV[0])
ast.compile(compiler)
compiler.generate
end
__END__
require 'duby'
module Duby
module Compiler
class JVM
class CallCompiler < Proc
def call(method_builder, name, recv_type, signature, recv, args)
call(method_builder, name, recv_type, signature, recv, args)
end
end
class MathCompiler
def call(compiler, name, recv_type, recv, args)
recv.call
compiler.src << " #{name} "
args.call
end
end
class BranchCompiler < Proc
def branch(method_builder, predicate, body_callback, else_callback)
call(method_builder, predicate, body_callback, else_callback)
end
end
IntCallCompiler = proc do |builder, name, recv_callback, args_callback|
recv_callback.call
args_callback.call
end
IntReturnCompiler = proc do |builder, result|
result.call
builder.ireturn
end
IntLocalCompiler = proc do |builder, index|
builder.iload(index)
end
attr_accessor :filename, :src
def initialize(filename)
@filename = filename
@file_builder = FileBuilder.new(filename)
fixnum_type = AST.type(:fixnum)
self.type_mapper[fixnum_type] = Java::int.java_class
self.call_compilers[fixnum_type] = MathCompiler.new
self.return_compilers[fixnum_type] = IntReturnCompiler
self.local_compilers[fixnum_type] = IntLocalCompiler
self.branch_compilers[fixnum_type] = IntBranchCompiler
end
def compile(ast)
ast.compile(this)
end
def define_method(name, signature, args, body)
# if we encounter a loose method, init class builder with script name
@class_builder ||= @file_builder.public_class(@filename)
@class_builder.method(name, signature.map {|type| type_mapper[type]}) do |method_builder|
old_builder, @method_builder = @method_builder, method_builder
body.call
@method_builder = old_builder
end
end
def declare_argument(name, type)
#@src << "#{type_mapper[type]} #{name}"
end
def branch(condition_type, condition, body_proc, else_proc)
#condition compiles itself as appropriate type of jump
branch_compilers[condition_type].call(self, condition, body_proc, else_proc)
end
def call(name, recv_type, signature, recv, args)
call_compilers[recv_type].call(self, name, signature, recv, args)
end
def call_compilers
@call_compilers ||= {}
end
def self_call(name, signature, args)
args.call
call_compilers[self_type].self_call(self, name, signature, args)
end
def local(local_type, name)
index = @method_builder.local(name)
local_compilers[local_type].call(@method_builder, name)
end
def fixnum(value)
@method_builder.ldc_int(value)
end
def newline
@method_builder.pop
end
def ret(result_type, &result)
return_compilers[result_type].call(@method_builder, result)
end
def generate
@file_builder.generate
end
def type_mapper
@type_mapper ||= {}
end
def branch_compilers
@branch_compilers ||= {}
end
def local_compilers
@local_compilers ||= {}
end
def return_compilers
@return_compilers ||= {}
end
end
end
end
if __FILE__ == $0
ast = Duby::AST.parse(File.read(ARGV[0]))
typer = Duby::Typer::Simple.new(:script)
ast.infer(typer)
typer.resolve(true)
compiler = Duby::Compiler::JVM.new("#{ARGV[0]}.class")
ast.compile(compiler)
File.open(compiler.filename, "w") {|file| file.write(compiler.generate)}
end