Class: Duby::Compiler::JVM
Defined Under Namespace
Modules: JVMLogger
Classes: ClosureCompiler, ImplicitSelf
Constant Summary
collapse
- Types =
Duby::JVM::Types
Class Attribute Summary collapse
#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
-
#bootstrap_dynamic ⇒ 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
-
#compile_self ⇒ Object
-
#constructor(node) ⇒ Object
-
#declare_argument(name, type) ⇒ Object
-
#declare_field(name, type, annotations) ⇒ Object
-
#declare_local(scope, name, type) ⇒ Object
-
#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
-
#field(name, type, annotations) ⇒ Object
-
#field_assign(name, type, expression, value, annotations) ⇒ Object
-
#field_declare(name, type, annotations) ⇒ Object
-
#find_ensures(before) ⇒ Object
-
#finish_main ⇒ Object
-
#get_binding(type) ⇒ Object
-
#handle_ensures(nodes) ⇒ Object
-
#initialize(filename) ⇒ JVM
constructor
-
#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
-
#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?
#compile, #define_class, #define_main, #generate, #import, #log, #scoped_body, #scoped_local_name, #toplevel_class, #with
Constructor Details
#initialize(filename) ⇒ JVM
Returns a new instance of JVM.
56
57
58
59
60
61
62
|
# File 'lib/duby/jvm/compiler.rb', line 56
def initialize(filename)
super
BiteScript.bytecode_version = BiteScript::JAVA1_5
@file = BiteScript::FileBuilder.new(@filename)
AST.type_factory.define_types(@file)
@jump_scope = []
end
|
Class Attribute Details
.verbose ⇒ Object
Returns the value of attribute verbose.
28
29
30
|
# File 'lib/duby/jvm/compiler.rb', line 28
def verbose
@verbose
end
|
Class Method Details
.classname_from_filename(filename) ⇒ Object
34
35
36
37
|
# File 'lib/duby/jvm/compiler.rb', line 34
def classname_from_filename(filename)
basename = File.basename(filename, '.duby')
basename.split(/_/).map{|x| x[0...1].upcase + x[1..-1]}.join
end
|
.log(message) ⇒ Object
30
31
32
|
# File 'lib/duby/jvm/compiler.rb', line 30
def log(message)
puts "* [#{name}] #{message}" if JVM.verbose
end
|
Instance Method Details
#_raise(exception) ⇒ Object
721
722
723
724
|
# File 'lib/duby/jvm/compiler.rb', line 721
def _raise(exception)
exception.compile(self, true)
@method.athrow
end
|
#annotate(builder, annotations) ⇒ Object
452
453
454
455
456
457
458
459
460
461
462
|
# File 'lib/duby/jvm/compiler.rb', line 452
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
# File 'lib/duby/jvm/compiler.rb', line 464
def annotation_value(builder, name, value)
case value
when Duby::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
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
|
# File 'lib/duby/jvm/compiler.rb', line 646
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
89
90
91
92
|
# File 'lib/duby/jvm/compiler.rb', line 89
def begin_main
@method.local('argv', AST.type('string', true))
end
|
#binding_reference ⇒ Object
686
687
688
|
# File 'lib/duby/jvm/compiler.rb', line 686
def binding_reference
@method.aload(@method.local('$binding'))
end
|
#body(body, expression) ⇒ Object
426
427
428
429
430
431
|
# File 'lib/duby/jvm/compiler.rb', line 426
def body(body, expression)
super(body, expression) do |last|
last.compile(self, expression)
end
end
|
#boolean(value) ⇒ Object
636
637
638
|
# File 'lib/duby/jvm/compiler.rb', line 636
def boolean(value)
value ? @method.iconst_1 : @method.iconst_0
end
|
#bootstrap_dynamic ⇒ Object
779
780
781
782
783
784
785
786
787
788
789
790
791
|
# File 'lib/duby/jvm/compiler.rb', line 779
def bootstrap_dynamic
unless defined? @class.bootstrapped
def @class.bootstrapped; true; end
method = @class.public_static_method("<clinit>", [], Java::void)
method.start
method.ldc org.jruby.duby.DynalangBootstrap
method.ldc "bootstrap"
method.invokestatic java.dyn.Linkage, "registerBootstrapMethod", [method.void, java.lang.Class, method.string]
method.returnvoid
method.stop
end
end
|
#branch(iff, expression) ⇒ Object
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
|
# File 'lib/duby/jvm/compiler.rb', line 211
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
297
298
299
300
|
# File 'lib/duby/jvm/compiler.rb', line 297
def break(node)
handle_ensures(find_ensures(Duby::AST::Loop))
@method.goto(@break_label)
end
|
#build_string(nodes, expression) ⇒ Object
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
|
# File 'lib/duby/jvm/compiler.rb', line 598
def build_string(nodes, expression)
if expression
builder_class = Duby::AST.type('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
324
325
326
327
328
329
330
331
332
333
334
335
|
# File 'lib/duby/jvm/compiler.rb', line 324
def call(call, expression)
target = call.target.inferred_type
params = call.parameters.map do |param|
param.inferred_type
end
method = target.get_method(call.name, params)
if method
method.call(self, call, expression)
else
raise "Missing method #{target}.#{call.name}(#{params.join ', '})"
end
end
|
#captured_local(scope, name, type) ⇒ Object
516
517
518
519
520
|
# File 'lib/duby/jvm/compiler.rb', line 516
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
522
523
524
525
526
527
528
529
|
# File 'lib/duby/jvm/compiler.rb', line 522
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
508
509
510
511
512
513
514
|
# File 'lib/duby/jvm/compiler.rb', line 508
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
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
|
# File 'lib/duby/jvm/compiler.rb', line 372
def cast(fcall, expression)
castee = fcall.parameters[0]
source_type_name = castee.inferred_type.name
target_type_name = fcall.inferred_type.name
case source_type_name
when "byte", "short", "char", "int", "long", "float", "double"
case target_type_name
when "byte", "short", "char", "int", "long", "float", "double"
primitive = true
else
raise TypeError.new "not a reference type: #{castee.inferred_type}"
end
when "boolean"
if target_type_name != "boolean"
raise TypeError.new "not a boolean type: #{castee.inferred_type}"
end
primitive = true
else
case target_type_name
when "byte", "short", "char", "int", "long", "float", "double"
raise TypeError.new "not a primitive type: #{castee.inferred_type}"
else
primitive = false
end
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
@method.checkcast fcall.inferred_type
end
end
end
end
|
#compile_self ⇒ Object
690
691
692
|
# File 'lib/duby/jvm/compiler.rb', line 690
def compile_self
method.aload(0)
end
|
#constructor(node) ⇒ Object
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
|
# File 'lib/duby/jvm/compiler.rb', line 149
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_argument(name, type) ⇒ Object
207
208
209
|
# File 'lib/duby/jvm/compiler.rb', line 207
def declare_argument(name, type)
end
|
#declare_field(name, type, annotations) ⇒ Object
553
554
555
556
557
558
559
560
561
562
563
564
|
# File 'lib/duby/jvm/compiler.rb', line 553
def declare_field(name, type, annotations)
unless declared_fields[name]
declared_fields[name] = type
field = if static
@class.private_static_field name, type
else
@class.private_field name, type
end
annotate(field, annotations)
end
end
|
#declare_local(scope, name, type) ⇒ Object
485
486
487
488
489
490
491
492
|
# File 'lib/duby/jvm/compiler.rb', line 485
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
|
#declared_captures(binding = nil) ⇒ Object
504
505
506
|
# File 'lib/duby/jvm/compiler.rb', line 504
def declared_captures(binding=nil)
@captured_locals[binding || @binding]
end
|
#declared_fields ⇒ Object
548
549
550
551
|
# File 'lib/duby/jvm/compiler.rb', line 548
def declared_fields
@declared_fields ||= {}
@declared_fields[@class] ||= {}
end
|
#declared_locals ⇒ Object
448
449
450
|
# File 'lib/duby/jvm/compiler.rb', line 448
def declared_locals
@declared_locals ||= {}
end
|
#define_closure(class_def, expression) ⇒ Object
202
203
204
205
|
# File 'lib/duby/jvm/compiler.rb', line 202
def define_closure(class_def, expression)
compiler = ClosureCompiler.new(@file, @type, self)
compiler.define_class(class_def, expression)
end
|
#define_method(node) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/duby/jvm/compiler.rb', line 117
def define_method(node)
push_jump_scope(node) do
super(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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/duby/jvm/compiler.rb', line 130
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.children[0].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
774
775
776
777
|
# File 'lib/duby/jvm/compiler.rb', line 774
def empty_array(type, size)
size.compile(self, true)
type.newarray(@method)
end
|
#ensure(node, expression) ⇒ Object
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
|
# File 'lib/duby/jvm/compiler.rb', line 756
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
|
#field(name, type, annotations) ⇒ Object
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
# File 'lib/duby/jvm/compiler.rb', line 531
def field(name, type, annotations)
name = name[1..-1]
real_type = declared_fields[name] || type
declare_field(name, real_type, annotations)
method.aload 0 unless static
if static
@method.getstatic(@class, name, type)
else
@method.getfield(@class, name, type)
end
end
|
#field_assign(name, type, expression, value, annotations) ⇒ Object
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
# File 'lib/duby/jvm/compiler.rb', line 571
def field_assign(name, type, expression, value, annotations)
name = name[1..-1]
real_type = declared_fields[name] || type
declare_field(name, real_type, annotations)
method.aload 0 unless static
value.compile(self, true)
if expression
instruction = 'dup'
instruction << '2' if type.wide?
instruction << '_x1' unless static
method.send instruction
end
if static
@method.putstatic(@class, name, real_type)
else
@method.putfield(@class, name, real_type)
end
end
|
#field_declare(name, type, annotations) ⇒ Object
566
567
568
569
|
# File 'lib/duby/jvm/compiler.rb', line 566
def field_declare(name, type, annotations)
name = name[1..-1]
declare_field(name, type, annotations)
end
|
#find_ensures(before) ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/duby/jvm/compiler.rb', line 78
def find_ensures(before)
found = []
@jump_scope.reverse_each do |scope|
if Duby::AST::Ensure === scope
found << scope
end
break if scope === before
end
found
end
|
#finish_main ⇒ Object
94
95
96
|
# File 'lib/duby/jvm/compiler.rb', line 94
def finish_main
@method.returnvoid
end
|
#get_binding(type) ⇒ Object
500
501
502
|
# File 'lib/duby/jvm/compiler.rb', line 500
def get_binding(type)
@bindings[type]
end
|
#handle_ensures(nodes) ⇒ Object
750
751
752
753
754
|
# File 'lib/duby/jvm/compiler.rb', line 750
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
|
# File 'lib/duby/jvm/compiler.rb', line 312
def jump_if(predicate, target)
raise "Expected boolean, found #{predicate.inferred_type}" unless predicate.inferred_type == Types::Boolean
predicate.compile(self, true)
@method.ifne(target)
end
|
#jump_if_not(predicate, target) ⇒ Object
318
319
320
321
322
|
# File 'lib/duby/jvm/compiler.rb', line 318
def jump_if_not(predicate, target)
raise "Expected boolean, found #{predicate.inferred_type}" unless predicate.inferred_type == Types::Boolean
predicate.compile(self, true)
@method.ifeq(target)
end
|
#line(num) ⇒ Object
694
695
696
|
# File 'lib/duby/jvm/compiler.rb', line 694
def line(num)
@method.line(num) if @method
end
|
#local(scope, name, type) ⇒ Object
433
434
435
|
# File 'lib/duby/jvm/compiler.rb', line 433
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
437
438
439
440
441
442
443
444
445
446
|
# File 'lib/duby/jvm/compiler.rb', line 437
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
494
495
496
497
498
|
# File 'lib/duby/jvm/compiler.rb', line 494
def local_declare(scope, name, type)
declare_local(scope, name, type)
type.init_value(@method)
type.store(@method, @method.local(scoped_local_name(name, scope), type))
end
|
#loop(loop, expression) ⇒ Object
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
|
# File 'lib/duby/jvm/compiler.rb', line 243
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.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/duby/jvm/compiler.rb', line 178
def method_body(method, args, node, return_type)
body = node.body
with(:method => method,
:declared_locals => {}) do
method.start
if args
args.each {|arg| @method.local(arg.name, arg.inferred_type)}
end
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
302
303
304
305
|
# File 'lib/duby/jvm/compiler.rb', line 302
def next(node)
handle_ensures(find_ensures(Duby::AST::Loop))
@method.goto(@next_label)
end
|
#null ⇒ Object
682
683
684
|
# File 'lib/duby/jvm/compiler.rb', line 682
def null
@method.aconst_null
end
|
#output_type ⇒ Object
64
65
66
|
# File 'lib/duby/jvm/compiler.rb', line 64
def output_type
"classes"
end
|
#prepare_binding(scope) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/duby/jvm/compiler.rb', line 98
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]
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
|
# File 'lib/duby/jvm/compiler.rb', line 698
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
68
69
70
71
72
73
74
75
76
|
# File 'lib/duby/jvm/compiler.rb', line 68
def push_jump_scope(node)
raise "Not a node" unless Duby::AST::Node === node
begin
@jump_scope << node
yield
ensure
@jump_scope.pop
end
end
|
#redo(node) ⇒ Object
307
308
309
310
|
# File 'lib/duby/jvm/compiler.rb', line 307
def redo(node)
handle_ensures(find_ensures(Duby::AST::Loop))
@method.goto(@redo_label)
end
|
#regexp(value, flags = 0) ⇒ Object
640
641
642
643
644
|
# File 'lib/duby/jvm/compiler.rb', line 640
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
# File 'lib/duby/jvm/compiler.rb', line 726
def rescue(rescue_node, expression)
start = @method.label.set!
body_end = @method.label
done = @method.label
rescue_node.body.compile(self, expression)
body_end.set!
@method.goto(done)
rescue_node.clauses.each do |clause|
target = @method.label.set!
if clause.name
@method.astore(@method.push_local(clause.name, clause.type))
else
@method.pop
end
clause.body.compile(self, expression)
@method.pop_local(clause.name) if clause.name
@method.goto(done)
clause.types.each do |type|
@method.trycatch(start, body_end, target, type)
end
end
done.set!
end
|
#return(return_node) ⇒ Object
715
716
717
718
719
|
# File 'lib/duby/jvm/compiler.rb', line 715
def return(return_node)
return_node.value.compile(self, true)
handle_ensures(find_ensures(Duby::AST::MethodDefinition))
return_node.inferred_type.return(@method)
end
|
#self_call(fcall, expression) ⇒ Object
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
# File 'lib/duby/jvm/compiler.rb', line 337
def self_call(fcall, expression)
return cast(fcall, expression) if fcall.cast?
type = @type
type = type.meta if @static
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
594
595
596
|
# File 'lib/duby/jvm/compiler.rb', line 594
def string(value)
@method.ldc(value)
end
|
#super_call(sup, expression) ⇒ Object
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
# File 'lib/duby/jvm/compiler.rb', line 356
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
626
627
628
629
630
631
632
633
634
|
# File 'lib/duby/jvm/compiler.rb', line 626
def to_string(body, expression)
if expression
body.compile(self, true)
body.inferred_type.box(@method) if body.inferred_type.primitive?
@method.invokevirtual @method.object, "toString", [@method.string]
else
body.compile(self, false)
end
end
|