Class: Mirah::JVM::Compiler::JVMBytecode
- Inherits:
-
Base
- Object
- Base
- Mirah::JVM::Compiler::JVMBytecode
show all
- Includes:
- MethodLookup
- Defined in:
- lib/mirah/jvm/compiler/jvm_bytecode.rb
Defined Under Namespace
Modules: JVMLogger
Classes: ClosureCompiler, ImplicitSelf
Constant Summary
collapse
- Types =
Mirah::JVM::Types
Class Attribute Summary collapse
Attributes inherited from Base
#class, #filename, #method, #static
Class Method Summary
collapse
Instance Method Summary
collapse
-
#_raise(exception) ⇒ Object
-
#annotate(builder, annotations) ⇒ Object
-
#annotation_value(builder, name, value) ⇒ Object
-
#array(node, expression) ⇒ Object
-
#begin_main ⇒ Object
-
#binding_reference ⇒ Object
-
#body(body, expression) ⇒ Object
-
#boolean(value) ⇒ Object
-
#branch(iff, expression) ⇒ Object
-
#break(node) ⇒ Object
-
#build_string(nodes, expression) ⇒ Object
-
#call(call, expression) ⇒ Object
-
#captured_local(scope, name, type) ⇒ Object
-
#captured_local_assign(node, expression) ⇒ Object
-
#captured_local_declare(scope, name, type) ⇒ Object
-
#cast(fcall, expression) ⇒ Object
-
#constructor(node) ⇒ Object
-
#declare_field(name, type, annotations, static_field) ⇒ Object
-
#declare_local(scope, name, type) ⇒ Object
-
#declare_locals(scope) ⇒ Object
-
#declared?(scope, name) ⇒ Boolean
-
#declared_captures(binding = nil) ⇒ Object
-
#declared_fields ⇒ Object
-
#declared_locals ⇒ Object
-
#define_closure(class_def, expression) ⇒ Object
-
#define_method(node) ⇒ Object
-
#define_optarg_chain(name, arg, return_type, args_for_opt, arg_types_for_opt) ⇒ Object
-
#empty_array(type, size) ⇒ Object
-
#ensure(node, expression) ⇒ Object
-
#extract_method(call) ⇒ Object
-
#field(name, type, annotations, static_field) ⇒ Object
-
#field_assign(name, type, expression, value, annotations, static_field) ⇒ Object
-
#field_declare(name, type, annotations, static_field) ⇒ Object
-
#file_builder(filename) ⇒ Object
-
#find_ensures(before) ⇒ Object
-
#finish_main ⇒ Object
-
#get_binding(type) ⇒ Object
-
#handle_ensures(nodes) ⇒ Object
-
#initialize ⇒ JVMBytecode
constructor
A new instance of JVMBytecode.
-
#jump_if(predicate, target) ⇒ Object
-
#jump_if_not(predicate, target) ⇒ Object
-
#line(num) ⇒ Object
-
#local(scope, name, type) ⇒ Object
-
#local_assign(scope, name, type, expression, value) ⇒ Object
-
#local_declare(scope, name, type) ⇒ Object
-
#loop(loop, expression) ⇒ Object
-
#method_body(method, args, node, return_type) ⇒ Object
-
#next(node) ⇒ Object
-
#null ⇒ Object
-
#output_type ⇒ Object
-
#prepare_binding(scope) ⇒ Object
-
#print(print_node) ⇒ Object
-
#push_jump_scope(node) ⇒ Object
-
#real_self ⇒ Object
-
#redo(node) ⇒ Object
-
#regexp(value, flags = 0) ⇒ Object
-
#rescue(rescue_node, expression) ⇒ Object
-
#return(return_node) ⇒ Object
-
#self_call(fcall, expression) ⇒ Object
-
#string(value) ⇒ Object
-
#super_call(sup, expression) ⇒ Object
-
#to_string(body, expression) ⇒ Object
#each_is_exact, #each_is_exact_or_subtype_or_convertible, #field_lookup, #find_jls, #find_method, #inner_class, #is_more_specific?, #log, #phase1, #phase2, #phase3, #primitive_convertible?
Methods inherited from Base
#base_define_method, #compile, #compile_self, #create_method_builder, #declare_argument, #define_class, #define_main, #error, #fixnum, #generate, #import, #log, #original_create_method_builder, #scoped_body, #scoped_local_name, #toplevel_class, #with
Constructor Details
Returns a new instance of JVMBytecode.
39
40
41
42
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 39
def initialize
super
@jump_scope = []
end
|
Class Attribute Details
.verbose ⇒ Object
Returns the value of attribute verbose.
11
12
13
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 11
def verbose
@verbose
end
|
Class Method Details
.classname_from_filename(filename) ⇒ Object
17
18
19
20
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 17
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
|
.log(message) ⇒ Object
13
14
15
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 13
def log(message)
puts "* [#{name}] #{message}" if JVMBytecode.verbose
end
|
Instance Method Details
#_raise(exception) ⇒ Object
759
760
761
762
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 759
def _raise(exception)
exception.compile(self, true)
@method.athrow
end
|
#annotate(builder, annotations) ⇒ Object
469
470
471
472
473
474
475
476
477
478
479
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 469
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
|
#annotation_value(builder, name, value) ⇒ Object
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 481
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
|
#array(node, expression) ⇒ Object
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
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 684
def array(node, expression)
if expression
@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]
node.children.each do |n|
@method.dup
n.compile(self, true)
if n.inferred_type!.primitive?
n.inferred_type.box(@method)
end
@method.invokeinterface java::util::List, "add", [@method.boolean, @method.object]
@method.pop
end
@method.invokestatic java::util::Collections, "unmodifiableList", [java::util::List, java::util::List]
else
node.children.each do |n|
n.compile(self, true)
if n.inferred_type.primitive?
n.inferred_type.box(@method)
end
end
end
end
|
#begin_main ⇒ Object
75
76
77
78
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 75
def begin_main
@method.local('argv', AST.type(nil, 'string', true))
end
|
#binding_reference ⇒ Object
724
725
726
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 724
def binding_reference
@method.aload(@method.local('$binding'))
end
|
#body(body, expression) ⇒ Object
443
444
445
446
447
448
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 443
def body(body, expression)
super(body, expression) do |last|
compile(last, expression)
end
end
|
#boolean(value) ⇒ Object
674
675
676
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 674
def boolean(value)
value ? @method.iconst_1 : @method.iconst_0
end
|
#branch(iff, expression) ⇒ Object
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
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 208
def branch(iff, expression)
elselabel = @method.label
donelabel = @method.label
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
|
#break(node) ⇒ Object
294
295
296
297
298
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 294
def break(node)
error("break outside of loop", node) unless @break_label
handle_ensures(find_ensures(Mirah::AST::Loop))
@method.goto(@break_label)
end
|
#build_string(nodes, expression) ⇒ Object
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
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 627
def build_string(nodes, expression)
if expression
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
@method.invokevirtual java::lang::StringBuilder.java_class, "toString", [@method.string]
else
nodes.each do |node|
node.compile(self, false)
end
end
end
|
#call(call, expression) ⇒ Object
350
351
352
353
354
355
356
357
358
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 350
def call(call, expression)
return cast(call, expression) if call.cast?
method = (call)
if method
method.call(self, call, expression)
else
raise "Missing method #{target}.#{call.name}(#{params.join ', '})"
end
end
|
#captured_local(scope, name, type) ⇒ Object
545
546
547
548
549
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 545
def captured_local(scope, name, type)
captured_local_declare(scope, name, type)
binding_reference
@method.getfield(scope.binding_type, name, type)
end
|
#captured_local_assign(node, expression) ⇒ Object
551
552
553
554
555
556
557
558
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 551
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
|
#captured_local_declare(scope, name, type) ⇒ Object
537
538
539
540
541
542
543
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 537
def captured_local_declare(scope, name, type)
unless declared_captures[name]
declared_captures[name] = type
@binding.protected_field(name, type)
end
end
|
#cast(fcall, expression) ⇒ Object
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
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 395
def cast(fcall, expression)
castee = fcall.parameters[0]
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
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
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
|
#constructor(node) ⇒ Object
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
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 146
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
|
#declare_field(name, type, annotations, static_field) ⇒ Object
582
583
584
585
586
587
588
589
590
591
592
593
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 582
def declare_field(name, type, annotations, static_field)
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
|
#declare_local(scope, name, type) ⇒ Object
506
507
508
509
510
511
512
513
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 506
def declare_local(scope, name, type)
name = scoped_local_name(name, scope)
unless declared_locals[name]
declared_locals[name] = type
index = @method.local(name, type)
end
end
|
#declare_locals(scope) ⇒ Object
518
519
520
521
522
523
524
525
526
527
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 518
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
|
#declared?(scope, name) ⇒ Boolean
502
503
504
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 502
def declared?(scope, name)
declared_locals.include?(scoped_local_name(name, scope))
end
|
#declared_captures(binding = nil) ⇒ Object
533
534
535
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 533
def declared_captures(binding=nil)
@captured_locals[binding || @binding]
end
|
#declared_fields ⇒ Object
577
578
579
580
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 577
def declared_fields
@declared_fields ||= {}
@declared_fields[@class] ||= {}
end
|
#declared_locals ⇒ Object
465
466
467
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 465
def declared_locals
@declared_locals ||= {}
end
|
#define_closure(class_def, expression) ⇒ Object
203
204
205
206
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 203
def define_closure(class_def, expression)
compiler = ClosureCompiler.new(@file, @type, self)
compiler.define_class(class_def, expression)
end
|
#define_method(node) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 114
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
|
#define_optarg_chain(name, arg, return_type, args_for_opt, arg_types_for_opt) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 127
def define_optarg_chain(name, arg, return_type,
args_for_opt, arg_types_for_opt)
@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)
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
|
#empty_array(type, size) ⇒ Object
814
815
816
817
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 814
def empty_array(type, size)
size.compile(self, true)
type.newarray(@method)
end
|
#ensure(node, expression) ⇒ Object
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 796
def ensure(node, expression)
node.state = @method.label start = @method.label.set!
body_end = @method.label
done = @method.label
push_jump_scope(node) do
node.body.compile(self, expression) end
body_end.set!
handle_ensures([node]) @method.goto(done) target = @method.label.set! @method.trycatch(start, body_end, target, nil)
handle_ensures([node])
@method.athrow
done.set!
end
|
342
343
344
345
346
347
348
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 342
def (call)
target = call.target.inferred_type!
params = call.parameters.map do |param|
param.inferred_type!
end
target.get_method(call.name, params)
end
|
#field(name, type, annotations, static_field) ⇒ Object
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 560
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)
method.aload 0 unless static || static_field
if static || static_field
@method.getstatic(@class, name, type)
else
@method.getfield(@class, name, type)
end
end
|
#field_assign(name, type, expression, value, annotations, static_field) ⇒ Object
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 600
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
|
#field_declare(name, type, annotations, static_field) ⇒ Object
595
596
597
598
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 595
def field_declare(name, type, annotations, static_field)
name = name[1..-1] if name =~ /^@/
declare_field(name, type, annotations, static_field)
end
|
#file_builder(filename) ⇒ Object
44
45
46
47
48
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 44
def file_builder(filename)
builder = BiteScript::FileBuilder.new(filename)
AST.type_factory.define_types(builder)
builder
end
|
#find_ensures(before) ⇒ Object
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 64
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
|
#finish_main ⇒ Object
80
81
82
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 80
def finish_main
@method.returnvoid
end
|
#get_binding(type) ⇒ Object
529
530
531
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 529
def get_binding(type)
@bindings[type]
end
|
#handle_ensures(nodes) ⇒ Object
790
791
792
793
794
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 790
def handle_ensures(nodes)
nodes.each do |ensure_node|
ensure_node.clause.compile(self, false)
end
end
|
#jump_if(predicate, target) ⇒ Object
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 312
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 = (predicate)
if method.respond_to? :jump_if
method.jump_if(self, predicate, target)
return
end
end
predicate.compile(self, true)
@method.ifne(target)
end
|
#jump_if_not(predicate, target) ⇒ Object
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 327
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 = (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
|
#line(num) ⇒ Object
732
733
734
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 732
def line(num)
@method.line(num - 1) if @method
end
|
#local(scope, name, type) ⇒ Object
450
451
452
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 450
def local(scope, name, type)
type.load(@method, @method.local(scoped_local_name(name, scope), type))
end
|
#local_assign(scope, name, type, expression, value) ⇒ Object
454
455
456
457
458
459
460
461
462
463
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 454
def local_assign(scope, name, type, expression, value)
declare_local(scope, name, type)
value.compile(self, true)
@method.dup if expression
type.store(@method, @method.local(scoped_local_name(name, scope), type))
end
|
#local_declare(scope, name, type) ⇒ Object
515
516
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 515
def local_declare(scope, name, type)
end
|
#loop(loop, expression) ⇒ Object
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
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 240
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
jump_if(predicate, @break_label)
else
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
jump_if_not(predicate, pre_label)
else
jump_if(predicate, pre_label)
end
end
@break_label.set!
@method.aconst_null if expression
end
end
end
|
#method_body(method, args, node, return_type) ⇒ Object
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
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 175
def method_body(method, args, node, return_type)
body = node.body
with(:method => method,
:declared_locals => {}) do
method.start
scope = node.static_scope
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
|
#next(node) ⇒ Object
300
301
302
303
304
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 300
def next(node)
error("next outside of loop", node) unless @next_label
handle_ensures(find_ensures(Mirah::AST::Loop))
@method.goto(@next_label)
end
|
#null ⇒ Object
720
721
722
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 720
def null
@method.aconst_null
end
|
#output_type ⇒ Object
50
51
52
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 50
def output_type
"classes"
end
|
#prepare_binding(scope) ⇒ Object
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
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 84
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
|
#print(print_node) ⇒ Object
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 736
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
|
#push_jump_scope(node) ⇒ Object
54
55
56
57
58
59
60
61
62
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 54
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
|
#real_self ⇒ Object
728
729
730
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 728
def real_self
method.aload(0)
end
|
#redo(node) ⇒ Object
306
307
308
309
310
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 306
def redo(node)
error("redo outside of loop", node) unless @redo_label
handle_ensures(find_ensures(Mirah::AST::Loop))
@method.goto(@redo_label)
end
|
#regexp(value, flags = 0) ⇒ Object
678
679
680
681
682
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 678
def regexp(value, flags = 0)
@method.ldc(value)
@method.invokestatic java::util::regex::Pattern, "compile", [java::util::regex::Pattern, @method.string]
end
|
#rescue(rescue_node, expression) ⇒ Object
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
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 764
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
|
#return(return_node) ⇒ Object
753
754
755
756
757
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 753
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
|
#self_call(fcall, expression) ⇒ Object
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 360
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
|
#string(value) ⇒ Object
623
624
625
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 623
def string(value)
@method.ldc(value)
end
|
#super_call(sup, expression) ⇒ Object
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 379
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
|
#to_string(body, expression) ⇒ Object
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
# File 'lib/mirah/jvm/compiler/jvm_bytecode.rb', line 655
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
|