Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Last tag attempt!

git-svn-id: http://svn.codehaus.org/jruby/tags/jruby-1_1_4@7570 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information...
commit 543db206e561b770d892c191946ef55cda8a9dd9 2 parents 5744d3a + 24be9a6
Thomas E Enebo enebo authored
45 lib/ruby/site_ruby/1.8/compiler/builder.rb
@@ -110,10 +110,15 @@ def method?
110 110 class ClassBuilder
111 111 include Util
112 112 include TypeNamespace
  113 +
  114 + begin
  115 + import "jruby.objectweb.asm.Opcodes"
  116 + import "jruby.objectweb.asm.ClassWriter"
  117 + rescue
  118 + import "org.objectweb.asm.Opcodes"
  119 + import "org.objectweb.asm.ClassWriter"
  120 + end
113 121
114   - import "jruby.objectweb.asm.Opcodes"
115   - import "jruby.objectweb.asm.ClassWriter"
116   - include Opcodes
117 122 import java.lang.Object
118 123 import java.lang.Void
119 124 include Signature
@@ -135,7 +140,7 @@ def initialize(file_builder, class_name, file_name, superclass = Object, *interf
135 140
136 141 interface_paths = []
137 142 interfaces.each {|interface| interface_paths << path(interface)}
138   - @class_writer.visit(V1_4, ACC_PUBLIC | ACC_SUPER, class_name, nil, path(superclass), interface_paths.to_java(:string))
  143 + @class_writer.visit(Opcodes::V1_4, Opcodes::ACC_PUBLIC | Opcodes::ACC_SUPER, class_name, nil, path(superclass), interface_paths.to_java(:string))
139 144 @class_writer.visit_source(file_name, nil)
140 145
141 146 @constructor = nil
@@ -158,7 +163,7 @@ def generate
158 163 if @constructor
159 164 @constructor.generate_constructor(@superclass)
160 165 else
161   - method = MethodBuilder.new(self, ACC_PUBLIC, "<init>", [])
  166 + method = MethodBuilder.new(self, Opcodes::ACC_PUBLIC, "<init>", [])
162 167 method.start
163 168 method.aload 0
164 169 method.invokespecial @superclass, "<init>", Void::TYPE
@@ -176,7 +181,7 @@ def generate
176 181
177 182 # generate fields
178 183 @fields.each do |name, field|
179   - @class_writer.visit_field(ACC_PROTECTED, field.name, class_id(field.type), nil, nil)
  184 + @class_writer.visit_field(Opcodes::ACC_PROTECTED, field.name, class_id(field.type), nil, nil)
180 185 end
181 186
182 187 String.from_java_bytes(@class_writer.to_byte_array)
@@ -218,20 +223,30 @@ def method(name, *signature, &block)
218 223 raise "Overloading not yet supported"
219 224 end
220 225
221   - mb = MethodBuilder.new(self, ACC_PUBLIC, name, signature)
  226 + mb = MethodBuilder.new(self, Opcodes::ACC_PUBLIC, name, signature)
222 227 deferred_builder = DeferredMethodBuilder.new(name, mb, signature, block)
223 228 if name == "<init>"
224 229 @constructor = deferred_builder
225 230 else
226 231 @instance_methods[name] = deferred_builder
227 232 end
  233 +
  234 + mb
228 235 end
229 236
230 237 # New version does not instance_eval, to allow for easier embedding
231 238 def static_method(name, *signature, &block)
232   - mb = MethodBuilder.new(self, ACC_PUBLIC | ACC_STATIC, name, signature)
233   - deferred_builder = DeferredMethodBuilder.new(name, mb, signature, block)
234   - @static_methods[name] = deferred_builder
  239 + mb = MethodBuilder.new(self, Opcodes::ACC_PUBLIC | Opcodes::ACC_STATIC, name, signature)
  240 +# deferred_builder = DeferredMethodBuilder.new(name, mb, signature, block)
  241 +# @static_methods[name] = deferred_builder
  242 +
  243 + if block
  244 + mb.start
  245 + block.yield(mb)
  246 + mb.stop
  247 + else
  248 + mb
  249 + end
235 250 end
236 251
237 252 # name for signature generation using the class being generated
@@ -318,8 +333,12 @@ def method?
318 333 end
319 334
320 335 class MethodBuilder
321   - import "jruby.objectweb.asm.Opcodes"
322   - include Opcodes
  336 + begin
  337 + import "jruby.objectweb.asm.Opcodes"
  338 + rescue
  339 + import "org.objectweb.asm.Opcodes"
  340 + end
  341 +
323 342 include Compiler::Bytecode
324 343
325 344 attr_reader :method_visitor
@@ -335,7 +354,7 @@ def initialize(class_builder, modifiers, name, signature)
335 354
336 355 @locals = {}
337 356
338   - @static = (modifiers & ACC_STATIC) != 0
  357 + @static = (modifiers & Opcodes::ACC_STATIC) != 0
339 358
340 359 @locals['this'] = TypedVariable.new("this", @class_builder, 0) unless @static
341 360 end
21 lib/ruby/site_ruby/1.8/compiler/builder2.rb
@@ -3,9 +3,14 @@
3 3
4 4 module Compiler
5 5 class ClassBuilder
6   - import "jruby.objectweb.asm.Opcodes"
7   - import "jruby.objectweb.asm.ClassWriter"
8   - include Opcodes
  6 + begin
  7 + import "jruby.objectweb.asm.Opcodes"
  8 + import "jruby.objectweb.asm.ClassWriter"
  9 + rescue
  10 + import "org.objectweb.asm.Opcodes"
  11 + import "org.objectweb.asm.ClassWriter"
  12 + end
  13 +
9 14 import java.lang.Object
10 15 import java.lang.Void
11 16 include Signature
@@ -18,7 +23,7 @@ def initialize(class_name, file_name, superclass, *interfaces)
18 23
19 24 interface_paths = []
20 25 interfaces.each {|interface| interface_paths << path(interface)}
21   - @class_writer.visit(V1_4, ACC_PUBLIC | ACC_SUPER, class_name, nil, path(superclass), interface_paths.to_java(:string))
  26 + @class_writer.visit(Opcodes::V1_4, Opcodes::ACC_PUBLIC | Opcodes::ACC_SUPER, class_name, nil, path(superclass), interface_paths.to_java(:string))
22 27 @class_writer.visit_source(file_name, nil)
23 28 end
24 29
@@ -33,21 +38,21 @@ def generate
33 38 end
34 39
35 40 def field(name, type)
36   - @class_writer.visitField(ACC_PUBLIC, name.to_s, ci(type), nil, nil)
  41 + @class_writer.visitField(Opcodes::ACC_PUBLIC, name.to_s, ci(type), nil, nil)
37 42 end
38 43
39 44 def constructor(*signature, &block)
40 45 signature.unshift Void::TYPE
41 46
42   - MethodBuilder.build(self, ACC_PUBLIC, "<init>", signature, &block)
  47 + MethodBuilder.build(self, Opcodes::ACC_PUBLIC, "<init>", signature, &block)
43 48 end
44 49
45 50 def method(name, *signature, &block)
46   - MethodBuilder.build(self, ACC_PUBLIC, name.to_s, signature, &block)
  51 + MethodBuilder.build(self, Opcodes::ACC_PUBLIC, name.to_s, signature, &block)
47 52 end
48 53
49 54 def static_method(name, *signature, &block)
50   - MethodBuilder.build(self, ACC_PUBLIC | ACC_STATIC, name.to_s, signature, &block)
  55 + MethodBuilder.build(self, Opcodes::ACC_PUBLIC | Opcodes::ACC_STATIC, name.to_s, signature, &block)
51 56 end
52 57
53 58 # name for signature generation using the class being generated
96 lib/ruby/site_ruby/1.8/compiler/bytecode.rb
@@ -5,9 +5,14 @@ module Compiler
5 5 # JVM assembly code. Included classes must just provide a method_visitor accessor
6 6 module Bytecode
7 7 include Signature
8   -
9   - import "jruby.objectweb.asm.Opcodes"
10   - import "jruby.objectweb.asm.Label"
  8 +
  9 + begin
  10 + import "jruby.objectweb.asm.Opcodes"
  11 + import "jruby.objectweb.asm.Label"
  12 + rescue Exception
  13 + import "org.objectweb.asm.Opcodes"
  14 + import "org.objectweb.asm.Label"
  15 + end
11 16
12 17 import java.lang.Object
13 18 import java.lang.System
@@ -20,11 +25,11 @@ module Bytecode
20 25
21 26 case const_name
22 27 when "ALOAD", "ASTORE",
23   - "BISTORE", "BILOAD",
24 28 "ISTORE", "ILOAD",
25 29 "LSTORE", "LLOAD",
26 30 "FSTORE", "FLOAD",
27   - "DSTORE", "DLOAD"
  31 + "DSTORE", "DLOAD",
  32 + "RET"
28 33 # variable instructions
29 34 eval "
30 35 def #{const_down}(var)
@@ -44,6 +49,13 @@ def #{const_down}(value)
44 49 method_visitor.visit_ldc_insn(value)
45 50 end
46 51 ", b, __FILE__, __LINE__
  52 +
  53 + when "BIPUSH", "SIPUSH"
  54 + eval "
  55 + def #{const_down}(value)
  56 + method_visitor.visit_int_insn(Opcodes::#{const_name}, value)
  57 + end
  58 + "
47 59
48 60 when "INVOKESTATIC", "INVOKEVIRTUAL", "INVOKEINTERFACE", "INVOKESPECIAL"
49 61 # method instructions
@@ -62,16 +74,20 @@ def returnvoid()
62 74 when "DUP", "SWAP", "POP", "POP2", "DUP_X1", "DUP_X2", "DUP2", "DUP2_X1", "DUP2_X2",
63 75 "NOP",
64 76 "ARRAYLENGTH",
65   - "ARETURN", "ATHROW", "ACONST_NULL", "AALOAD",
  77 + "ARETURN", "ATHROW", "ACONST_NULL", "AALOAD", "AASTORE",
66 78 "BALOAD", "BASTORE",
67   - "ICONST_0", "ICONST_1", "ICONST_2", "ICONST_3", "IRETURN", "IALOAD",
68   - "IADD", "IINC", "ISUB", "IDIV", "IMUL", "INEG", "IAND", "IOR", "IXOR",
69   - "LCONST_0", "LCONST_1", "LRETURN", "LALOAD",
70   - "LADD", "LINC", "LSUB", "LDIV", "LMUL", "LNEG", "LAND", "LOR", "LXOR",
71   - "FCONST_0", "FCONST_1", "FCONST_2", "FRETURN", "FALOAD",
72   - "FADD", "FINC", "FSUB", "FDIV", "FMUL", "FNEG",
73   - "DCONST_0", "DCONST_1", "DRETURN", "DALOAD",
74   - "DADD", "DINC", "DSUB", "DDIV", "DMUL", "DNEG"
  79 + "CALOAD", "CASTORE",
  80 + "SALOAD", "SASTORE",
  81 + "ICONST_M1", "ICONST_0", "ICONST_1", "ICONST_2", "ICONST_3", "ICONST_4", "ICONST_5", "IRETURN", "IALOAD",
  82 + "IADD", "IINC", "ISUB", "IDIV", "IMUL", "INEG", "IAND", "IOR", "IXOR", "IASTORE",
  83 + "IUSHR", "ISHL", "ISHR", "I2L", "I2S", "I2F", "I2D", "I2B", "IREM", "I2C",
  84 + "LCONST_0", "LCONST_1", "LRETURN", "LALOAD", "LASTORE", "LCMP", "LSHL", "LSHR", "LREM", "LUSHR",
  85 + "LADD", "LINC", "LSUB", "LDIV", "LMUL", "LNEG", "LAND", "LOR", "LXOR", "L2I", "L2F", "L2D",
  86 + "FCONST_0", "FCONST_1", "FCONST_2", "FRETURN", "FALOAD", "F2D", "F2I", "FASTORE",
  87 + "FADD", "FSUB", "FDIV", "FMUL", "FNEG", "FREM", "FCMPG", "F2L", "FCMPL",
  88 + "DCONST_0", "DCONST_1", "DRETURN", "DALOAD", "DASTORE", "D2I", "D2F", "D2L",
  89 + "DADD", "DINC", "DSUB", "DDIV", "DMUL", "DNEG", "DCMPL", "DCMPG", "DREM",
  90 + "MONITORENTER", "MONITOREXIT"
75 91 # bare instructions
76 92 eval "
77 93 def #{const_down}
@@ -96,7 +112,8 @@ def #{const_down}(type, name, field_sig)
96 112 ", b, __FILE__, __LINE__
97 113
98 114 when "GOTO", "IFEQ", "IFNE", "IF_ACMPEQ", "IF_ACMPNE", "IF_ICMPEQ", "IF_ICMPNE", "IF_ICMPLT",
99   - "IF_ICMPGT", "IF_ICMPLE", "IF_ICMPGE", "IFNULL", "IFNONNULL"
  115 + "IF_ICMPGT", "IF_ICMPLE", "IF_ICMPGE", "IFNULL", "IFNONNULL", "JSR",
  116 + "IFLE", "IFGE", "IFLT", "IFGT"
100 117 # jump instructions
101 118 eval "
102 119 def #{const_down}(target)
@@ -104,6 +121,14 @@ def #{const_down}(target)
104 121 end
105 122 ", b, __FILE__, __LINE__
106 123
  124 + when "MULTIANEWARRAY"
  125 + # multi-dim array
  126 + eval "
  127 + def #{const_down}(type, dims)
  128 + method_visitor.visit_multi_anew_array_insn(ci(type), dims)
  129 + end
  130 + ", b, __FILE__, __LINE__
  131 +
107 132 when "LOOKUPSWITCH"
108 133 def lookupswitch(default, ints, cases)
109 134 method_visitor.visit_lookup_switch_insn(default, ints, cases)
@@ -113,6 +138,20 @@ def lookupswitch(default, ints, cases)
113 138 def tableswitch(min, max, default, cases)
114 139 method_visitor.visit_table_switch_insn(min, max, default, cases)
115 140 end
  141 +
  142 + when "F_FULL", "ACC_ENUM", "ACC_SYNTHETIC", "ACC_INTERFACE", "ACC_PUBLIC",
  143 + "ACC_PRIVATE", "ACC_PROTECTED", "ACC_DEPRECATED", "ACC_BRIDGE",
  144 + "ACC_VARARGS", "ACC_SUPER", "F_CHOP", "F_APPEND", "FLOAT", "F_SAME",
  145 + "T_LONG", "INTEGER", "T_BYTE", "ACC_STATIC", "ACC_SYNCHRONIZED",
  146 + "T_BOOLEAN", "ACC_ANNOTATION", "ACC_ABSTRACT", "LONG", "ACC_TRANSIENT",
  147 + "T_DOUBLE", "DOUBLE", "ACC_STRICT", "NULL", "T_FLOAT", "ACC_FINAL",
  148 + "F_SAME1", "ACC_NATIVE", "F_NEW", "T_CHAR", "T_INT", "ACC_VOLATILE",
  149 + "V1_6", "V1_5", "V1_4", "V1_3", "V1_2", "V1_1", "UNINITIALIZED_THIS",
  150 + "TOP", "T_SHORT"
  151 + # non-instructions
  152 +
  153 + else
  154 + raise "Unknown opcode: " + const_name
116 155
117 156 end
118 157 end
@@ -162,5 +201,32 @@ def swap2
162 201 def line(num)
163 202 method_visitor.visit_line_number num, Label.new
164 203 end
  204 +
  205 + def push_int(num)
  206 + if (num <= Java::java.lang.Byte::MAX_VALUE && num >= Java::java.lang.Byte::MIN_VALUE)
  207 + case num
  208 + when -1
  209 + iconst_m1
  210 + when 0
  211 + iconst_0
  212 + when 1
  213 + iconst_1
  214 + when 2
  215 + iconst_2
  216 + when 3
  217 + iconst_3
  218 + when 4
  219 + iconst_4
  220 + when 5
  221 + iconst_5
  222 + else
  223 + bipush(num)
  224 + end
  225 + elsif (num <= Java::java.lang.Short::MAX_VALUE && num >= Java::java.lang.Short::MIN_VALUE)
  226 + sipush(num)
  227 + else
  228 + ldc(num)
  229 + end
  230 + end
165 231 end
166 232 end
48 lib/ruby/site_ruby/1.8/duby/c_compiler.rb
@@ -5,12 +5,12 @@ module Duby
5 5 module Compiler
6 6 class C
7 7 class MathCompiler
8   - def call(compiler, name, recv, args)
9   - recv.compile(compiler)
  8 + def call(compiler, call)
  9 + call.target.compile(compiler)
10 10
11   - compiler.src << " #{name} "
  11 + compiler.src << " #{call.name} "
12 12
13   - args.each {|arg| arg.compile(compiler)}
  13 + call.parameters.each {|param| param.compile(compiler)}
14 14 end
15 15 end
16 16
@@ -28,58 +28,70 @@ def initialize(filename)
28 28 def compile(ast)
29 29 ast.compile(self)
30 30 end
  31 +
  32 + def define_main(body)
  33 + old_src, @src = @src, "int main(int argc, char **argv)\n{\n"
  34 +
  35 + body.compile(self)
  36 +
  37 + @src << "}\n\n"
  38 +
  39 + @src = old_src + @src
  40 + end
31 41
32 42 def define_method(name, signature, args, body)
33   - @src << "#{type_mapper[signature[:return]]} #{name}("
  43 + old_src, @src = @src, "#{type_mapper[signature[:return]]} #{name}("
34 44
35 45 args.compile(self)
36 46
37   - @src << ") {"
  47 + @src << ")\n{\n"
38 48
39 49 body.compile(self)
40 50
41   - @src << "}\n\n"
  51 + @src << "\n}\n\n"
  52 +
  53 + @src = old_src + @src
42 54 end
43 55
44 56 def declare_argument(name, type)
45 57 @src << "#{type_mapper[type]} #{name}"
46 58 end
47 59
48   - def branch(condition, body, els)
  60 + def branch(iff)
49 61 @src << "if ("
50 62
51   - condition.compile(self)
  63 + iff.condition.compile(self)
52 64
53 65 @src << ") {"
54 66
55   - body.compile(self)
  67 + iff.body.compile(self)
56 68
57   - if els
  69 + if iff.else
58 70 @src << "} else {"
59 71
60   - els.compile(self)
  72 + iff.else.compile(self)
61 73 end
62 74
63 75 @src << "}"
64 76 end
65 77
66   - def call(name, recv, args)
67   - call_compilers[recv.inferred_type].call(self, name, recv, args)
  78 + def call(call)
  79 + call_compilers[call.target.inferred_type].call(self, call)
68 80 end
69 81
70 82 def call_compilers
71 83 @call_compilers ||= {}
72 84 end
73 85
74   - def self_call(name, args)
75   - @src << "#{name}("
  86 + def self_call(fcall)
  87 + @src << "#{fcall.name}("
76 88
77   - args.each {|arg| arg.compile(self)}
  89 + fcall.parameters.each {|param| param.compile(self)}
78 90
79 91 @src << ")"
80 92 end
81 93
82   - def local(name)
  94 + def local(name, type)
83 95 @src << name
84 96 end
85 97
12 lib/ruby/site_ruby/1.8/duby/compiler.rb
@@ -34,13 +34,13 @@ def compile(compiler)
34 34
35 35 class Local
36 36 def compile(compiler)
37   - compiler.local(name)
  37 + compiler.local(name, inferred_type)
38 38 end
39 39 end
40 40
41 41 class LocalAssignment
42 42 def compile(compiler)
43   - compiler.local_assign(name) {
  43 + compiler.local_assign(name, inferred_type) {
44 44 value.compile(compiler)
45 45 }
46 46 end
@@ -48,7 +48,7 @@ def compile(compiler)
48 48
49 49 class Script
50 50 def compile(compiler)
51   - body.compile(compiler)
  51 + compiler.define_main(body)
52 52 end
53 53 end
54 54
@@ -72,7 +72,7 @@ def compile(compiler)
72 72
73 73 class If
74 74 def compile(compiler)
75   - compiler.branch(condition, body, self.else)
  75 + compiler.branch(self)
76 76 end
77 77 end
78 78
@@ -84,13 +84,13 @@ def compile(compiler)
84 84
85 85 class FunctionalCall
86 86 def compile(compiler)
87   - compiler.self_call(name, parameters)
  87 + compiler.self_call(self)
88 88 end
89 89 end
90 90
91 91 class Call
92 92 def compile(compiler)
93   - compiler.call(name, target, parameters)
  93 + compiler.call(self)
94 94 end
95 95 end
96 96 end
257 lib/ruby/site_ruby/1.8/duby/jvm_compiler.rb
... ... @@ -1,103 +1,172 @@
1 1 require 'duby'
  2 +require 'compiler/builder'
2 3
3 4 module Duby
4 5 module Compiler
5   - class C
  6 + class JVM
6 7 class MathCompiler
7   - def call(compiler, name, recv, args)
8   - recv.call
9   -
10   - compiler.src << " #{name} "
  8 + def call(compiler, call)
  9 + call.target.compile(compiler)
  10 + call.parameters.each {|param| param.compile(compiler)}
11 11
12   - args.call
  12 + target_type = call.target.inferred_type
  13 + case target_type
  14 + when AST.type(:fixnum)
  15 + case call.name
  16 + when '-'
  17 + compiler.method.isub
  18 + when '+'
  19 + compiler.method.iadd
  20 + else
  21 + compiler.method.invokevirtual(
  22 + compiler.type_mapper[call.target.inferred_type],
  23 + call.name,
  24 + [compiler.type_mapper[call.inferred_type], *call.parameters.map {|param| compiler.type_mapper[param.inferred_type]}])
  25 + end
  26 + else
  27 + compiler.method.invokevirtual(
  28 + compiler.type_mapper[call.target.inferred_type],
  29 + call.name,
  30 + [compiler.type_mapper[call.inferred_type], *call.parameters.map {|param| compiler.type_mapper[param.inferred_type]}])
  31 + end
13 32 end
14 33 end
15 34
16   - attr_accessor :filename, :src
  35 + attr_accessor :filename, :src, :method
17 36
18 37 def initialize(filename)
19 38 @filename = filename
20 39 @src = ""
21 40
22   - self.type_mapper[AST::TypeReference.new(:fixnum)] = "int"
  41 + self.type_mapper[AST.type(:fixnum)] = Java::int
  42 + self.type_mapper[AST.type(:string, true)] = Java::java.lang.String[]
23 43
24   - self.call_compilers[AST::TypeReference.new(:fixnum)] = MathCompiler.new
  44 + self.call_compilers[AST.type(:fixnum)] = MathCompiler.new
  45 +
  46 + @file = ::Compiler::FileBuilder.new(filename)
  47 + @class = @file.public_class(filename.split('.')[0])
25 48 end
26 49
27 50 def compile(ast)
28 51 ast.compile(this)
29 52 end
  53 +
  54 + def define_main(body)
  55 + oldmethod, @method = @method, @class.static_method("main", type_mapper[AST.type(:void)], type_mapper[AST.type(:string, true)])
  56 +
  57 + @method.start
  58 +
  59 + body.compile(self)
  60 +
  61 + @method.returnvoid
  62 + @method.stop
  63 +
  64 + @method = oldmethod
  65 + end
30 66
31 67 def define_method(name, signature, args, body)
32   - @src << "#{type_mapper[signature[:return]]} #{name}("
33   -
34   - args.call
  68 + oldmethod, @method = @method, @class.static_method(name.to_s, type_mapper[signature[:return]], *args.args.map {|arg| type_mapper[arg.inferred_type]})
  69 +
  70 + @method.start
35 71
36   - @src << ") {"
  72 + #args.call
37 73
38   - body.call
  74 + body.compile(self)
  75 +
  76 + case signature[:return]
  77 + when AST.type(:fixnum)
  78 + @method.ireturn
  79 + else
  80 + @method.aload 0
  81 + @method.areturn
  82 + end
39 83
40   - @src << "}\n\n"
  84 + @method.stop
  85 +
  86 + @method = oldmethod
41 87 end
42 88
43 89 def declare_argument(name, type)
44   - @src << "#{type_mapper[type]} #{name}"
  90 + # declare local vars for arguments here
45 91 end
46 92
47   - def branch(condition, body_proc, else_proc)
48   - @src << "if ("
49   -
50   - condition.call
  93 + def branch(iff)
  94 + elselabel = @method.label
  95 + donelabel = @method.label
51 96
52   - @src << ") {"
53   -
54   - body_proc.call
55   -
56   - if else_proc
57   - @src << "} else {"
58   -
59   - else_proc.call
  97 + # this is ugly...need a better way to abstract the idea of compiling a
  98 + # conditional branch while still fitting into JVM opcodes
  99 + predicate = iff.condition.predicate
  100 + case iff.condition.predicate
  101 + when AST::Call
  102 + case predicate.target.inferred_type
  103 + when AST.type(:fixnum)
  104 + # fixnum conditional, so we need to use JVM opcodes
  105 + case predicate.parameters[0].inferred_type
  106 + when AST.type(:fixnum)
  107 + # fixnum on fixnum, easy
  108 + case predicate.name
  109 + when '<'
  110 + predicate.target.compile(self)
  111 + predicate.parameters[0].compile(self)
  112 + @method.if_icmpge(elselabel)
  113 + else
  114 + raise "Unknown :fixnum on :fixnum predicate operation: " + predicate.name
  115 + end
  116 + else
  117 + raise "Unknown :fixnum on " + predicate.parameters[0].inferred_type + " predicate operations: " + predicate.name
  118 + end
  119 + else
  120 + raise "Unknown " + predicate.target.inferred_type + " on " + predicate.parameters[0].inferred_type + " predicate operations: " + predicate.name
  121 + end
60 122 end
61   -
62   - @src << "}"
  123 +
  124 + iff.body.compile(self)
  125 +
  126 + @method.goto(donelabel)
  127 +
  128 + elselabel.set!
  129 +
  130 + iff.else.compile(self) if iff.else
  131 +
  132 + donelabel.set!
63 133 end
64 134
65   - def call(name, recv_type, recv, args)
66   - call_compilers[recv_type].call(self, name, recv, args)
  135 + def call(call)
  136 + call_compilers[call.target.inferred_type].call(self, call)
67 137 end
68 138
69 139 def call_compilers
70 140 @call_compilers ||= {}
71 141 end
72 142
73   - def self_call(name, args)
74   - @src << "#{name}("
75   -
76   - args.call
77   -
78   - @src << ")"
  143 + def self_call(fcall)
  144 + fcall.parameters.each {|param| param.compile(self)}
  145 + @method.invokestatic(
  146 + @method.this,
  147 + fcall.name,
  148 + [type_mapper[fcall.inferred_type], *fcall.parameters.map {|param| type_mapper[param.inferred_type]}])
79 149 end
80 150
81   - def local(name)
82   - @src << name
  151 + def local(name, type)
  152 + case type
  153 + when AST.type(:fixnum)
  154 + @method.iload(@method.local(name))
  155 + else
  156 + @method.aload(@method.local(name))
  157 + end
83 158 end
84 159
85 160 def fixnum(value)
86   - @src << value.to_s
  161 + @method.push_int(value)
87 162 end
88 163
89 164 def newline
90   - @src << ";\n"
91   - end
92   -
93   - def ret
94   - @src << "return "
95   -
96   - yield
  165 + # TODO: line numbering
97 166 end
98 167
99 168 def generate
100   - @src
  169 + @file.generate{|filename, builder| File.open(filename, 'w') {|f| f.write(builder.generate)}}
101 170 end
102 171
103 172 def type_mapper
@@ -105,90 +174,6 @@ def type_mapper
105 174 end
106 175 end
107 176 end
108   -
109   - module AST
110   - class Script
111   - def compile(compiler)
112   - # preparations for the .c file would go here
113   - body.compile(compiler)
114   - end
115   - end
116   -
117   - class Body
118   - def compile(compiler)
119   - last = children[-1]
120   - children.each do |child|
121   - child.compile(compiler)
122   - compiler.newline
123   - end
124   - end
125   - end
126   -
127   - class MethodDefinition
128   - def compile(compiler)
129   - args_callback = proc {arguments.compile(compiler)}
130   - body_callback = proc {body.compile(compiler)}
131   - compiler.define_method(name, signature, args_callback, body_callback)
132   - end
133   - end
134   -
135   - class Arguments
136   - def compile(compiler)
137   - args.each {|arg| compiler.declare_argument(arg.name, arg.inferred_type)} if args
138   - end
139   - end
140   -
141   - class Noop
142   - def compile(compiler)
143   - # nothing
144   - end
145   - end
146   -
147   - class Fixnum
148   - def compile(compiler)
149   - compiler.fixnum(literal)
150   - end
151   - end
152   -
153   - class If
154   - def compile(compiler)
155   - cond_callback = proc { condition.compile(compiler) }
156   - body_callback = proc { body.compile(compiler) }
157   - else_callback = proc { self.else.compile(compiler)}
158   -
159   - compiler.branch(cond_callback, body_callback, else_callback)
160   - end
161   - end
162   -
163   - class Condition
164   - def compile(compiler)
165   - predicate.compile(compiler)
166   - end
167   - end
168   -
169   - class FunctionalCall
170   - def compile(compiler)
171   - args_callback = proc { parameters.each {|param| param.compile(compiler)}}
172   -
173   - compiler.self_call(name, args_callback)
174   - end
175   - end
176   -
177   - class Call
178   - def compile(compiler)
179   - recv_callback = proc { target.compile(compiler) }
180   - args_callback = proc { parameters.each {|param| param.compile(compiler)}}
181   -
182   - compiler.call(name, target.inferred_type, recv_callback, args_callback)
183   - end
184   - end
185   -
186   - class Local
187   - def compile(compiler)
188   - compiler.local(name)
189   - end
190   - end
191   - end
192 177 end
193 178
194 179 if __FILE__ == $0
@@ -198,10 +183,10 @@ def compile(compiler)
198 183 ast.infer(typer)
199 184 typer.resolve(true)
200 185
201   - compiler = Duby::Compiler::JVM.new("#{ARGV[0].split('.')[0]}.class")
  186 + compiler = Duby::Compiler::JVM.new(ARGV[0])
202 187 ast.compile(compiler)
203 188
204   - File.open(compiler.filename, "w") {|file| file.write(compiler.generate)}
  189 + compiler.generate
205 190 end
206 191 __END__
207 192 require 'duby'
@@ -250,7 +235,7 @@ def initialize(filename)
250 235 @filename = filename
251 236 @file_builder = FileBuilder.new(filename)
252 237
253   - fixnum_type = AST::TypeReference.new(:fixnum)
  238 + fixnum_type = AST.type(:fixnum)
254 239
255 240 self.type_mapper[fixnum_type] = Java::int.java_class
256 241
102 maven/jruby-complete/pom.xml
@@ -90,91 +90,8 @@
90 90 </dependencies>
91 91
92 92 <build>
93   - <sourceDirectory>${project.basedir}/../../src</sourceDirectory>
94   - <testSourceDirectory>${project.basedir}/../../test</testSourceDirectory>
95 93 <plugins>
96 94 <plugin>
97   - <artifactId>maven-dependency-plugin</artifactId>
98   - <executions>
99   - <execution>
100   - <phase>generate-resources</phase>
101   - <goals>
102   - <goal>unpack</goal>
103   - </goals>
104   - <configuration>
105   - <artifactItems>
106   - <artifactItem>
107   - <groupId>asm</groupId>
108   - <artifactId>asm</artifactId>
109   - <version>3.0</version>
110   - </artifactItem>
111   - <artifactItem>
112   - <groupId>asm</groupId>
113   - <artifactId>asm-commons</artifactId>
114   - <version>3.0</version>
115   - </artifactItem>
116   - <artifactItem>
117   - <groupId>asm</groupId>
118   - <artifactId>asm-util</artifactId>
119   - <version>3.0</version>
120   - </artifactItem>
121   - <artifactItem>
122   - <groupId>asm</groupId>
123   - <artifactId>asm-analysis</artifactId>
124   - <version>3.0</version>
125   - </artifactItem>
126   - <artifactItem>
127   - <groupId>asm</groupId>
128   - <artifactId>asm-tree</artifactId>
129   - <version>3.0</version>
130   - </artifactItem>
131   - <artifactItem>
132   - <groupId>biz.aQute</groupId>
133   - <artifactId>bndlib</artifactId>
134   - <version>0.0.249</version>
135   - </artifactItem>
136   - <artifactItem>
137   - <groupId>jline</groupId>
138   - <artifactId>jline</artifactId>
139   - <version>0.9.93</version>
140   - </artifactItem>
141   - <artifactItem>
142   - <groupId>org.jruby.joni</groupId>
143   - <artifactId>joni</artifactId>
144   - <version>1.0.3</version>
145   - </artifactItem>
146   - <artifactItem>
147   - <groupId>joda-time</groupId>
148   - <artifactId>joda-time</artifactId>
149   - <version>1.5.1</version>
150   - </artifactItem>
151   - <artifactItem>
152   - <groupId>org.jruby.extras</groupId>
153   - <artifactId>jna</artifactId>
154   - <version>3.0.2</version>
155   - </artifactItem>
156   - <artifactItem>
157   - <groupId>org.jruby.ext.posix</groupId>
158   - <artifactId>jna-posix</artifactId>
159   - <version>0.7</version>
160   - </artifactItem>
161   - <artifactItem>
162   - <groupId>org.jruby.extras</groupId>
163   - <artifactId>jvyamlb</artifactId>
164   - <version>0.2.3</version>
165   - </artifactItem>
166   - <artifactItem>
167   - <groupId>org.jruby.extras</groupId>
168   - <artifactId>bytelist</artifactId>
169   - <version>0.1</version>
170   - </artifactItem>
171   - </artifactItems>
172   - <outputDirectory>${project.build.outputDirectory}</outputDirectory>
173   - </configuration>
174   - </execution>
175   - </executions>
176   - </plugin>
177   - <plugin>
178 95 <groupId>org.codehaus.mojo</groupId>
179 96 <artifactId>exec-maven-plugin</artifactId>
180 97 <version>1.1</version>
@@ -196,30 +113,17 @@
196 113 <executions>
197 114 <execution>
198 115 <id>copy-jar</id>
199   - <phase>process-classes</phase>
  116 + <phase>package</phase>
200 117 <goals><goal>run</goal></goals>
201 118 <configuration>
202 119 <tasks>
203   -<echo message="whoo"/>
204   - <copy file="${project.basedir}/../../lib/jruby-complete.jar" tofile="${project.build.directory}/${project.build.finalName}.jar"/>
  120 + <echo>copy ${project.basedir}/../../lib/jruby-complete.jar to ${project.build.directory}/${project.build.finalName}.jar</echo>
  121 + <copy overwrite="true" file="${project.basedir}/../../lib/jruby-complete.jar" tofile="${project.build.directory}/${project.build.finalName}.jar"/>
205 122 </tasks>
206 123 </configuration>
207 124 </execution>
208 125 </executions>
209 126 </plugin>
210   - <plugin>
211   - <artifactId>maven-surefire-plugin</artifactId>
212   - <configuration>
213   - <includes>
214   - <!-- <include>org/jruby/test/MainTestSuite.java</include> -->
215   - <!-- <include>org/jruby/test/ScriptTestSuite.java</include> -->
216   - <!-- <include>org/jruby/test/TestUnitTestSuite.java</include> -->
217   - </includes>
218   - <excludes>
219   - <exclude>**/*.java</exclude>
220   - </excludes>
221   - </configuration>
222   - </plugin>
223 127 </plugins>
224 128 </build>
225 129 </project>
103 maven/jruby/pom.xml
@@ -90,91 +90,8 @@
90 90 </dependencies>
91 91
92 92 <build>
93   - <sourceDirectory>${basedir}/../../src</sourceDirectory>
94   - <testSourceDirectory>${basedir}/../../test</testSourceDirectory>
95 93 <plugins>
96 94 <plugin>
97   - <artifactId>maven-dependency-plugin</artifactId>
98   - <executions>
99   - <execution>
100   - <phase>generate-resources</phase>
101   - <goals>
102   - <goal>unpack</goal>
103   - </goals>
104   - <configuration>
105   - <artifactItems>
106   - <artifactItem>
107   - <groupId>asm</groupId>
108   - <artifactId>asm</artifactId>
109   - <version>3.0</version>
110   - </artifactItem>
111   - <artifactItem>
112   - <groupId>asm</groupId>
113   - <artifactId>asm-commons</artifactId>
114   - <version>3.0</version>
115   - </artifactItem>
116   - <artifactItem>
117   - <groupId>asm</groupId>
118   - <artifactId>asm-util</artifactId>
119   - <version>3.0</version>
120   - </artifactItem>
121   - <artifactItem>
122   - <groupId>asm</groupId>
123   - <artifactId>asm-analysis</artifactId>
124   - <version>3.0</version>
125   - </artifactItem>
126   - <artifactItem>
127   - <groupId>asm</groupId>
128   - <artifactId>asm-tree</artifactId>
129   - <version>3.0</version>
130   - </artifactItem>
131   - <artifactItem>
132   - <groupId>biz.aQute</groupId>
133   - <artifactId>bndlib</artifactId>
134   - <version>0.0.249</version>
135   - </artifactItem>
136   - <artifactItem>
137   - <groupId>jline</groupId>
138   - <artifactId>jline</artifactId>
139   - <version>0.9.93</version>
140   - </artifactItem>
141   - <artifactItem>
142   - <groupId>org.jruby.joni</groupId>
143   - <artifactId>joni</artifactId>
144   - <version>1.0.3</version>
145   - </artifactItem>
146   - <artifactItem>
147   - <groupId>joda-time</groupId>
148   - <artifactId>joda-time</artifactId>
149   - <version>1.5.1</version>
150   - </artifactItem>
151   - <artifactItem>
152   - <groupId>org.jruby.extras</groupId>
153   - <artifactId>jna</artifactId>
154   - <version>3.0.2</version>
155   - </artifactItem>
156   - <artifactItem>
157   - <groupId>org.jruby.ext.posix</groupId>
158   - <artifactId>jna-posix</artifactId>
159   - <version>0.7</version>
160   - </artifactItem>
161   - <artifactItem>
162   - <groupId>org.jruby.extras</groupId>
163   - <artifactId>jvyamlb</artifactId>
164   - <version>0.2.3</version>
165   - </artifactItem>
166   - <artifactItem>
167   - <groupId>org.jruby.extras</groupId>
168   - <artifactId>bytelist</artifactId>
169   - <version>0.1</version>
170   - </artifactItem>
171   - </artifactItems>
172   - <outputDirectory>${project.build.outputDirectory}</outputDirectory>
173   - </configuration>
174   - </execution>
175   - </executions>
176   - </plugin>
177   - <plugin>
178 95 <groupId>org.codehaus.mojo</groupId>
179 96 <artifactId>exec-maven-plugin</artifactId>
180 97 <version>1.1</version>
@@ -196,31 +113,17 @@
196 113 <executions>
197 114 <execution>
198 115 <id>copy-jar</id>
199   - <phase>process-classes</phase>
  116 + <phase>package</phase>
200 117 <goals><goal>run</goal></goals>
201 118 <configuration>
202 119 <tasks>
203   -<echo message="whoo"/>
204   - <copy file="${project.basedir}/../../lib/jruby.jar" tofile="${project.build.directory}/${project.build.finalName}.jar"/>
  120 + <echo>copy ${project.basedir}/../../lib/jruby.jar to ${project.build.directory}/${project.build.finalName}.jar</echo>
  121 + <copy overwrite="true" file="${project.basedir}/../../lib/jruby.jar" tofile="${project.build.directory}/${project.build.finalName}.jar"/>
205 122 </tasks>
206 123 </configuration>
207 124 </execution>
208 125 </executions>
209 126 </plugin>
210   - <plugin>
211   - <groupId>org.apache.maven.plugins</groupId>
212   - <artifactId>maven-surefire-plugin</artifactId>
213   - <configuration>
214   - <includes>
215   - <!-- <include>org/jruby/test/MainTestSuite.java</include> -->
216   - <!-- <include>org/jruby/test/ScriptTestSuite.java</include> -->
217   - <!-- <include>org/jruby/test/TestUnitTestSuite.java</include> -->
218   - </includes>
219   - <excludes>
220   - <exclude>**/*.java</exclude>
221   - </excludes>
222   - </configuration>
223   - </plugin>
224 127 </plugins>
225 128 </build>
226 129 </project>
136 pom.xml
@@ -126,141 +126,5 @@
126 126 <artifactId>wagon-webdav</artifactId>
127 127 </extension>
128 128 </extensions>
129   - <plugins>
130   - <plugin>
131   - <groupId>org.apache.maven.plugins</groupId>
132   - <artifactId>maven-jar-plugin</artifactId>
133   - <configuration>
134   - <archive>
135   - <manifest>
136   - <mainClass>${jruby.main.class}</mainClass>
137   - <packageName>${jruby.package}</packageName>
138   - </manifest>
139   - </archive>
140   - </configuration>
141   - </plugin>
142   - </plugins>
143 129 </build>
144   - <profiles>
145   - <profile>
146   - <id>jruby</id>
147   - <activation>
148   - <activeByDefault>true</activeByDefault>
149   - </activation>
150   - <build>
151   - <resources>
152   - <resource>
153   - <directory>${project.basedir}/../../src</directory>
154   - <includes>
155   - <include>**/*.rb</include>
156   - </includes>
157   - </resource>
158   - <resource>
159   - <directory>${project.basedir}/../../lib/ruby/site_ruby/1.8</directory>
160   - <includes>
161   - <include>builtin/**/*.rb</include>
162   - </includes>
163   - </resource>
164   - </resources>
165   - <testResources>
166   - <testResource>
167   - <directory>${project.basedir}/../..</directory>
168   - <includes>
169   - <include>build.xml</include>
170   - <include>test/**/*.rb</include>
171   - <include>test/**/*.txt</include>
172   - <include>test/*index</include>
173   - <include>test/**/*.yml</include>
174   - <include>test/**/*.jar</include>
175   - <include>test/**/*.properties</include>
176   - </includes>
177   - </testResource>
178   - <testResource>
179   - <directory>${project.basedir}/../../test</directory>
180   - <includes>
181   - <include>org/jruby/**/*.rb</include>
182   - </includes>
183   - </testResource>
184   - </testResources>
185   - <plugins>
186   - <plugin>
187   - <artifactId>maven-compiler-plugin</artifactId>
188   - <configuration>
189   - <source>${javac.version}</source>
190   - <target>${javac.version}</target>
191   - </configuration>
192   - </plugin>
193   - <plugin>