Skip to content

Commit

Permalink
* Stack maintenance fixes: after each line, pop remaining result; aft…
Browse files Browse the repository at this point in the history
…er assignments, push result. Better tracking of consumptive operations and whether a dup is necessary would make this more efficient.

git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@6183 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
headius committed Mar 12, 2008
1 parent f270ec4 commit f2be210
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
7 changes: 6 additions & 1 deletion lib/ruby/site_ruby/1.8/compiler/bytecode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ module Bytecode
const_down = const_name.downcase

case const_name
when "ALOAD", "ILOAD", "ASTORE", "ISTORE"
when "ALOAD", "ASTORE",
"BISTORE", "BILOAD",
"ISTORE", "ILOAD",
"LSTORE", "LLOAD",
"FSTORE", "FLOAD",
"DSTORE", "DLOAD"
# variable instructions
eval "
def #{const_down}(var)
Expand Down
54 changes: 47 additions & 7 deletions lib/ruby/site_ruby/1.8/compiler/duby/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ def compile(builder)

class BlockNode
def compile(builder)
builder.aconst_null
child_nodes.each do |node|
node = node.next_node while NewlineNode === node
next unless node

builder.line node.position.start_line

builder.pop
node.compile(builder)
end
end
Expand Down Expand Up @@ -130,12 +135,17 @@ def compile_args(builder)
end

def compile_primitive(type, builder)
receiver_node.compile(builder)

if !args_node || args_node.size != 1
raise CompileError.new(position, "Primitive operations must have exactly one argument")
if !args_node
raise CompileError.new(position, "Unary primitive operations are not yet supported")
end

if args_node.size != 1
raise CompileError.new(position, "Binary primitive operations require exactly one argument")
end

# binary operations consume receiver and arg and leave result, so no dup necessary
receiver_node.compile(builder)

node = args_node.get(0)
# TODO: check or cast types according to receiver's type
node.compile(builder)
Expand Down Expand Up @@ -382,9 +392,13 @@ def compile(builder)
class HashNode
def compile(builder)
@declared ||= false
unless @declared
# TODO: compile
super

if @declared
# hash was used for type declaration, so we just push a null to skip it
# TODO: it would be nice if we could just skip the null too, but BlockNode wants to pop it
builder.aconst_null
else
raise CompileError.new(position, "Literal hash syntax not yet supported")
end
end
end
Expand Down Expand Up @@ -437,18 +451,36 @@ def compile(builder)
class InstAsgnNode
def compile(builder)
builder.field(mapped_name(builder), value_node.type(builder))

# assignment consumes the value, so we dup it
# TODO inefficient if we don't need the result
value_node.compile(builder)
builder.dup

builder.putfield(mapped_name(builder))
end
end

class LocalAsgnNode
def compile(builder)
local_index = builder.local(name, value_node.type(builder))

# assignment consumes a value, so we dup it
# TODO: inefficient if we don't actually need the result
value_node.compile(builder)
builder.dup

case type(builder)
when Jboolean
builder.bistore(local_index)
when Jint
builder.istore(local_index)
when Jlong
builder.lstore(local_index)
when Jfloat
builder.fstore(local_index)
when Jdouble
builder.dstore(local_index)
else
builder.astore(local_index)
end
Expand All @@ -459,8 +491,16 @@ class LocalVarNode
def compile(builder)
local_index = builder.local(name)
case type(builder)
when Jboolean
builder.biload(local_index)
when Jint
builder.iload(local_index)
when Jlong
builder.lload(local_index)
when Jfloat
builder.fload(local_index)
when Jdouble
builder.dload(local_index)
else
builder.aload(local_index)
end
Expand Down
2 changes: 1 addition & 1 deletion samples/duby/operators.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class StupidInt
def main(args)
def self.main(args)
{args => :string[]}

int = 1
Expand Down

0 comments on commit f2be210

Please sign in to comment.