-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathjvm_bytecode.rb
851 lines (732 loc) · 27.8 KB
/
jvm_bytecode.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
module Mirah
module JVM
module Compiler
class JVMBytecode < Base
java_import java.lang.System
java_import java.io.PrintStream
include Mirah::JVM::MethodLookup
Types = Mirah::JVM::Types
class << self
attr_accessor :verbose
def log(message)
puts "* [#{name}] #{message}" if JVMBytecode.verbose
end
def classname_from_filename(filename)
basename = File.basename(filename).sub(/\.(duby|mirah)$/, '')
basename.split(/[_-]/).map{|x| x[0...1].upcase + x[1..-1]}.join
end
end
module JVMLogger
def log(message); JVMBytecode.log(message); end
end
class ImplicitSelf
attr_reader :inferred_type
def initialize(type)
@inferred_type = type
end
def compile(compiler, expression)
compiler.compile_self if expression
end
end
def initialize
super
@jump_scope = []
end
def file_builder(filename)
builder = BiteScript::FileBuilder.new(filename)
AST.type_factory.define_types(builder)
builder
end
def output_type
"classes"
end
def push_jump_scope(node)
raise "Not a node" unless Mirah::AST::Node === node
begin
@jump_scope << node
yield
ensure
@jump_scope.pop
end
end
def find_ensures(before)
found = []
@jump_scope.reverse_each do |scope|
if Mirah::AST::Ensure === scope
found << scope
end
break if before === scope
end
found
end
def begin_main
# declare argv variable
@method.local('argv', AST.type(nil, 'string', true))
end
def finish_main
@method.returnvoid
end
def prepare_binding(scope)
if scope.has_binding?
type = scope.binding_type
@binding = @bindings[type]
@method.new type
@method.dup
@method.invokespecial type, "<init>", [@method.void]
if scope.respond_to? :arguments
scope.arguments.args.each do |param|
name = param.name
param_type = param.inferred_type
if scope.static_scope.captured?(param.name)
@method.dup
type.load(@method, @method.local(name, param_type))
@method.putfield(type, name, param_type)
end
end
end
type.store(@method, @method.local('$binding', type))
end
begin
yield
ensure
if scope.has_binding?
@binding.stop
@binding = nil
end
end
end
def define_method(node)
push_jump_scope(node) do
base_define_method(node, true) do |method, arg_types|
return if @class.interface?
log "Starting new #{node.static? ? 'static ' : ''}method #{node.name}(#{arg_types})"
args = node.arguments.args
method_body(method, args, node, node.signature[:return])
log "Method #{node.name}(#{arg_types}) complete!"
end
end
end
def define_optarg_chain(name, arg, return_type,
args_for_opt, arg_types_for_opt)
# declare all args so they get their values
@method.aload(0) unless @static
args_for_opt.each do |req_arg|
req_arg.inferred_type.load(@method, @method.local(req_arg.name, req_arg.inferred_type))
end
arg.value.compile(self, true)
# invoke the next one in the chain
if @static
@method.invokestatic(@class, name.to_s, [return_type] + arg_types_for_opt + [arg.inferred_type])
else
@method.invokevirtual(@class, name.to_s, [return_type] + arg_types_for_opt + [arg.inferred_type])
end
return_type.return(@method)
end
def constructor(node)
push_jump_scope(node) do
super(node, true) do |method, args|
method_body(method, args, node, Types::Void) do
method.aload 0
if node.delegate_args
if node.calls_super
delegate_class = @type.superclass
else
delegate_class = @type
end
delegate_types = node.delegate_args.map do |arg|
arg.inferred_type
end
constructor = delegate_class.constructor(*delegate_types)
node.delegate_args.each do |arg|
arg.compile(self, true)
end
method.invokespecial(
delegate_class, "<init>",
[@method.void, *constructor.argument_types])
else
method.invokespecial @class.superclass, "<init>", [@method.void]
end
end
end
end
end
def method_body(method, args, node, return_type)
body = node.body
with(:method => method,
:declared_locals => {}) do
method.start
scope = node.static_scope
# declare all args so they get their values
if args
args.each {|arg| declare_local(scope, arg.name, arg.inferred_type)}
end
declare_locals(scope)
yield if block_given?
prepare_binding(node) do
expression = return_type != Types::Void
body.compile(self, expression) if body
end
return_type.return(@method)
@method.stop
end
end
def define_closure(class_def, expression)
compiler = ClosureCompiler.new(@file, @type, self)
compiler.define_class(class_def, expression)
end
def branch(iff, expression)
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
if iff.body || expression
jump_if_not(predicate, elselabel)
if iff.body
iff.body.compile(self, expression)
elsif expression
iff.inferred_type.init_value(@method)
end
@method.goto(donelabel)
else
jump_if(predicate, donelabel)
end
elselabel.set!
if iff.else
iff.else.compile(self, expression)
elsif expression
iff.inferred_type.init_value(@method)
end
donelabel.set!
end
def loop(loop, expression)
push_jump_scope(loop) do
with(:break_label => @method.label,
:redo_label => @method.label,
:next_label => @method.label) do
predicate = loop.condition.predicate
loop.init.compile(self, false) if loop.init?
pre_label = @redo_label
if loop.check_first
@next_label.set! unless loop.post?
if loop.negative
# if condition, exit
jump_if(predicate, @break_label)
else
# if not condition, exit
jump_if_not(predicate, @break_label)
end
end
if loop.pre?
pre_label = method.label
pre_label.set!
loop.pre.compile(self, false)
end
@redo_label.set!
loop.body.compile(self, false) if loop.body
if loop.check_first && !loop.post?
@method.goto(@next_label)
else
@next_label.set!
loop.post.compile(self, false) if loop.post?
if loop.negative
# if not condition, continue
jump_if_not(predicate, pre_label)
else
# if condition, continue
jump_if(predicate, pre_label)
end
end
@break_label.set!
# loops always evaluate to null
@method.aconst_null if expression
end
end
end
def break(node)
error("break outside of loop", node) unless @break_label
handle_ensures(find_ensures(Mirah::AST::Loop))
@method.goto(@break_label)
end
def next(node)
error("next outside of loop", node) unless @next_label
handle_ensures(find_ensures(Mirah::AST::Loop))
@method.goto(@next_label)
end
def redo(node)
error("redo outside of loop", node) unless @redo_label
handle_ensures(find_ensures(Mirah::AST::Loop))
@method.goto(@redo_label)
end
def jump_if(predicate, target)
unless predicate.inferred_type == Types::Boolean
raise "Expected boolean, found #{predicate.inferred_type}"
end
if Mirah::AST::Call === predicate
method = extract_method(predicate)
if method.respond_to? :jump_if
method.jump_if(self, predicate, target)
return
end
end
predicate.compile(self, true)
@method.ifne(target)
end
def jump_if_not(predicate, target)
unless predicate.inferred_type == Types::Boolean
raise "Expected boolean, found #{predicate.inferred_type}"
end
if Mirah::AST::Call === predicate
method = extract_method(predicate)
if method.respond_to? :jump_if_not
method.jump_if_not(self, predicate, target)
return
end
end
predicate.compile(self, true)
@method.ifeq(target)
end
def extract_method(call)
target = call.target.inferred_type!
params = call.parameters.map do |param|
param.inferred_type!
end
target.get_method(call.name, params)
end
def call(call, expression)
return cast(call, expression) if call.cast?
method = extract_method(call)
if method
method.call(self, call, expression)
else
raise "Missing method #{target}.#{call.name}(#{params.join ', '})"
end
end
def self_call(fcall, expression)
return cast(fcall, expression) if fcall.cast?
type = fcall.scope.static_scope.self_type
type = type.meta if (@static && type == @type)
fcall.target = ImplicitSelf.new(type)
params = fcall.parameters.map do |param|
param.inferred_type
end
method = type.get_method(fcall.name, params)
unless method
target = static ? @class.name : 'self'
raise NameError, "No method %s.%s(%s)" %
[target, fcall.name, params.join(', ')]
end
method.call(self, fcall, expression)
end
def super_call(sup, expression)
type = @type.superclass
sup.target = ImplicitSelf.new(type)
params = sup.parameters.map do |param|
param.inferred_type
end
method = type.get_method(sup.name, params)
unless method
raise NameError, "No method %s.%s(%s)" %
[type, sup.name, params.join(', ')]
end
method.call_special(self, sup, expression)
end
def cast(fcall, expression)
# casting operation, not a call
castee = fcall.parameters[0]
# TODO move errors to inference phase
source_type_name = castee.inferred_type.name
target_type_name = fcall.inferred_type.name
if castee.inferred_type.primitive?
if fcall.inferred_type.primitive?
if source_type_name == 'boolean' && target_type_name != "boolean"
raise TypeError.new "not a boolean type: #{castee.inferred_type}"
end
# ok
primitive = true
else
raise TypeError.new "Cannot cast #{castee.inferred_type} to #{fcall.inferred_type}: not a reference type."
end
elsif fcall.inferred_type.primitive?
raise TypeError.new "not a primitive type: #{castee.inferred_type}"
else
# ok
primitive = false
end
castee.compile(self, expression)
if expression
if primitive
source_type_name = 'int' if %w[byte short char].include? source_type_name
if (source_type_name != 'int') && (%w[byte short char].include? target_type_name)
target_type_name = 'int'
end
if source_type_name != target_type_name
if RUBY_VERSION == "1.9"
@method.send "#{source_type_name[0]}2#{target_type_name[0]}"
else
@method.send "#{source_type_name[0].chr}2#{target_type_name[0].chr}"
end
end
else
if (source_type_name != target_type_name ||
castee.inferred_type.array? != fcall.inferred_type.array?)
@method.checkcast fcall.inferred_type
end
end
end
end
def body(body, expression)
# last element is an expression only if the body is an expression
super(body, expression) do |last|
compile(last, expression)
end
end
def local(scope, name, type)
type.load(@method, @method.local(scoped_local_name(name, scope), type))
end
def local_assign(scope, name, type, expression, value)
declare_local(scope, name, type)
value.compile(self, true)
# if expression, dup the value we're assigning
@method.dup if expression
type.store(@method, @method.local(scoped_local_name(name, scope), type))
end
def declared_locals
@declared_locals ||= {}
end
def annotate(builder, annotations)
annotations.each do |annotation|
type = annotation.type
type = type.jvm_type if type.respond_to?(:jvm_type)
builder.annotate(type, annotation.runtime?) do |visitor|
annotation.values.each do |name, value|
annotation_value(visitor, name, value)
end
end
end
end
def annotation_value(builder, name, value)
case value
when Mirah::AST::Annotation
type = value.type
type = type.jvm_type if type.respond_to?(:jvm_type)
builder.annotation(name, type) do |child|
value.values.each do |name, value|
annotation_value(child, name, value)
end
end
when ::Array
builder.array(name) do |array|
value.each do |item|
annotation_value(array, nil, item)
end
end
else
builder.value(name, value)
end
end
def declared?(scope, name)
declared_locals.include?(scoped_local_name(name, scope))
end
def declare_local(scope, name, type)
# TODO confirm types are compatible
name = scoped_local_name(name, scope)
unless declared_locals[name]
declared_locals[name] = type
index = @method.local(name, type)
end
end
def local_declare(scope, name, type)
end
def declare_locals(scope)
scope.locals.each do |name|
unless scope.captured?(name) || declared?(scope, name)
type = scope.local_type(name)
declare_local(scope, name, type)
type.init_value(@method)
type.store(@method, @method.local(scoped_local_name(name, scope), type))
end
end
end
def get_binding(type)
@bindings[type]
end
def declared_captures(binding=nil)
@captured_locals[binding || @binding]
end
def captured_local_declare(scope, name, type)
unless declared_captures[name]
declared_captures[name] = type
# default should be fine, but I don't think bitescript supports it.
@binding.protected_field(name, type)
end
end
def captured_local(scope, name, type)
captured_local_declare(scope, name, type)
binding_reference
@method.getfield(scope.binding_type, name, type)
end
def captured_local_assign(node, expression)
scope, name, type = node.containing_scope, node.name, node.inferred_type
captured_local_declare(scope, name, type)
binding_reference
node.value.compile(self, true)
@method.dup_x2 if expression
@method.putfield(scope.binding_type, name, type)
end
def field(name, type, annotations, static_field)
name = name[1..-1] if name =~ /^@/
real_type = declared_fields[name] || type
declare_field(name, real_type, annotations, static_field)
# load self object unless static
method.aload 0 unless static || static_field
if static || static_field
@method.getstatic(@class, name, type)
else
@method.getfield(@class, name, type)
end
end
def declared_fields
@declared_fields ||= {}
@declared_fields[@class] ||= {}
end
def declare_field(name, type, annotations, static_field)
# TODO confirm types are compatible
unless declared_fields[name]
declared_fields[name] = type
field = if static || static_field
@class.private_static_field name, type
else
@class.private_field name, type
end
annotate(field, annotations)
end
end
def field_declare(name, type, annotations, static_field)
name = name[1..-1] if name =~ /^@/
declare_field(name, type, annotations, static_field)
end
def field_assign(name, type, expression, value, annotations, static_field)
name = name[1..-1] if name =~ /^@/
real_type = declared_fields[name] || type
declare_field(name, real_type, annotations, static_field)
method.aload 0 unless static || static_field
value.compile(self, true)
if expression
instruction = 'dup'
instruction << '2' if type.wide?
instruction << '_x1' unless static || static_field
method.send instruction
end
if static || static_field
@method.putstatic(@class, name, real_type)
else
@method.putfield(@class, name, real_type)
end
end
def string(value)
@method.ldc(value)
end
def build_string(nodes, expression)
if expression
# could probably be more efficient with non-default constructor
builder_class = Mirah::AST.type(nil, 'java.lang.StringBuilder')
@method.new builder_class
@method.dup
@method.invokespecial builder_class, "<init>", [@method.void]
nodes.each do |node|
node.compile(self, true)
method = find_method(builder_class, "append", [node.inferred_type], false)
if method
@method.invokevirtual builder_class, "append", [method.return_type, *method.argument_types]
else
log "Could not find a match for #{java::lang::StringBuilder}.append(#{node.inferred_type})"
fail "Could not compile"
end
end
# convert to string
@method.invokevirtual java::lang::StringBuilder.java_class, "toString", [@method.string]
else
nodes.each do |node|
node.compile(self, false)
end
end
end
def to_string(body, expression)
if expression
body.compile(self, true)
body.inferred_type.box(@method) if body.inferred_type.primitive?
null = method.label
done = method.label
method.dup
method.ifnull(null)
@method.invokevirtual @method.object, "toString", [@method.string]
@method.goto(done)
null.set!
method.pop
method.ldc("null")
done.set!
else
body.compile(self, false)
end
end
def boolean(value)
value ? @method.iconst_1 : @method.iconst_0
end
def regexp(value, flags = 0)
# TODO: translate flags to Java-appropriate values
@method.ldc(value)
@method.invokestatic java::util::regex::Pattern, "compile", [java::util::regex::Pattern, @method.string]
end
def array(node, expression)
if expression
# create basic arraylist
@method.new java::util::ArrayList
@method.dup
@method.ldc_int node.children ? node.children.size : 0
@method.invokespecial java::util::ArrayList, "<init>", [@method.void, @method.int]
# elements, as expressions
# TODO: ensure they're all reference types!
node.children.each do |n|
@method.dup
n.compile(self, true)
# TODO this feels like it should be in the node.compile itself
if n.inferred_type!.primitive?
n.inferred_type.box(@method)
end
@method.invokeinterface java::util::List, "add", [@method.boolean, @method.object]
@method.pop
end
# make it unmodifiable
@method.invokestatic java::util::Collections, "unmodifiableList", [java::util::List, java::util::List]
else
# elements, as non-expressions
# TODO: ensure they're all reference types!
node.children.each do |n|
n.compile(self, true)
# TODO this feels like it should be in the node.compile itself
if n.inferred_type.primitive?
n.inferred_type.box(@method)
end
end
end
end
def null
@method.aconst_null
end
def binding_reference
@method.aload(@method.local('$binding'))
end
def real_self
method.aload(0)
end
def line(num)
@method.line(num - 1) if @method
end
def print(print_node)
@method.getstatic System, "out", PrintStream
print_node.parameters.each {|param| param.compile(self, true)}
params = print_node.parameters.map {|param| param.inferred_type.jvm_type}
method_name = print_node.println ? "println" : "print"
method = find_method(PrintStream.java_class, method_name, params, false)
if (method)
@method.invokevirtual(
PrintStream,
method_name,
[method.return_type, *method.parameter_types])
else
log "Could not find a match for #{PrintStream}.#{method_name}(#{params})"
fail "Could not compile"
end
end
def return(return_node)
return_node.value.compile(self, true) if return_node.value
handle_ensures(find_ensures(Mirah::AST::MethodDefinition))
return_node.inferred_type.return(@method)
end
def _raise(exception)
exception.compile(self, true)
@method.athrow
end
def rescue(rescue_node, expression)
start = @method.label.set!
body_end = @method.label
done = @method.label
rescue_node.body.compile(self, expression && rescue_node.else_node.nil?) if rescue_node.body
body_end.set!
rescue_node.else_node.compile(self, expression) if rescue_node.else_node
return if start.label.offset == body_end.label.offset
@method.goto(done)
rescue_node.clauses.each do |clause|
target = @method.label.set!
if clause.name
@method.astore(declare_local(clause.static_scope, clause.name, clause.type))
else
@method.pop
end
declare_locals(clause.static_scope)
clause.body.compile(self, expression)
@method.goto(done)
clause.types.each do |type|
@method.trycatch(start, body_end, target, type)
end
end
done.set!
end
def handle_ensures(nodes)
nodes.each do |ensure_node|
ensure_node.clause.compile(self, false)
end
end
def ensure(node, expression)
node.state = @method.label # Save the ensure target for JumpNodes
start = @method.label.set!
body_end = @method.label
done = @method.label
push_jump_scope(node) do
node.body.compile(self, expression) # First compile the body
end
body_end.set!
handle_ensures([node]) # run the ensure clause
@method.goto(done) # and continue on after the exception handler
target = @method.label.set! # Finally, create the exception handler
@method.trycatch(start, body_end, target, nil)
handle_ensures([node])
@method.athrow
done.set!
end
def empty_array(type, size)
size.compile(self, true)
type.newarray(@method)
end
class ClosureCompiler < JVMBytecode
def initialize(file, type, parent)
@file = file
@type = type
@jump_scope = []
@parent = parent
end
def prepare_binding(scope)
if scope.has_binding?
type = scope.binding_type
@binding = @parent.get_binding(type)
@method.aload 0
@method.getfield(@class, 'binding', @binding)
type.store(@method, @method.local('$binding', type))
end
begin
yield
ensure
if scope.has_binding?
@binding = nil
end
end
end
def declared_captures
@parent.declared_captures(@binding)
end
end
end
end
end
end