Skip to content

Commit

Permalink
Track if a CM is for a block properly finally
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Phoenix committed May 6, 2010
1 parent 4aac89d commit f653ab9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 13 deletions.
62 changes: 58 additions & 4 deletions kernel/common/compiled_method.rb
Expand Up @@ -15,7 +15,7 @@ class CompiledMethod < Executable
# being used.

attr_accessor :hints # added by the VM to indicate how it's being used.
attr_accessor :__ivars__
attr_accessor :metadata # [Tuple] extra data
attr_accessor :name # [Symbol] name of the method
attr_accessor :iseq # [Tuple] instructions to execute
attr_accessor :stack_size # [Integer] size of stack at compile time
Expand Down Expand Up @@ -317,7 +317,7 @@ def first_line
#
# @return [Boolean]
def is_block?
@name =~ /__(?:(?:\w|_)+)?block__/
get_metadata(:for_block)
end

def describe
Expand All @@ -343,7 +343,7 @@ def change_name(name)

lits = Tuple.new(cm.literals.size)
cm.literals.each_with_index do |lit, idx|
if lit.kind_of? CompiledMethod
if lit.kind_of? CompiledMethod and lit.is_block?
lit = lit.change_name name
end

Expand Down Expand Up @@ -470,6 +470,60 @@ def arity()
end
end

def add_metadata(key, val)
raise TypeError, "key must be a symbol" unless key.kind_of? Symbol

case val
when true, false, Symbol, Fixnum, String
# ok
else
raise TypeError, "invalid type of value"
end

@metadata ||= nil # to deal with MRI seeing @metadata as not set

unless @metadata
@metadata = Tuple.new(2)
@metadata[0] = key
@metadata[1] = val
return val
end

i = 0
fin = @metadata.size

while i < fin
if @metadata[i] == key
@metadata[i + 1] = val
return val
end

i += 2
end

tup = Tuple.create(fin + 2)
tup.copy_from @metadata, 0, fin, 0
tup[fin] = key
tup[fin + 1] = val

return val
end

def get_metadata(key)
return nil unless @metadata.kind_of? Tuple

i = 0
while i < @metadata.size
if @metadata[i] == key
return @metadata[i + 1]
end

i += 2
end

return nil
end

##
# Represents virtual machine's CPU instruction.
# Instructions are organized into instruction
Expand All @@ -490,7 +544,7 @@ def initialize(inst, cm, ip, args_reg)
when :local
# TODO: Blocks should be able to retrieve local names as well,
# but need access to method corresponding to home context
if cm.local_names and cm.name != :__block__
if cm.local_names and !cm.is_block?
@comment = cm.local_names[args[i]].to_s
end
when :block_local
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/compiled_file.rb
Expand Up @@ -164,7 +164,7 @@ def unmarshal_data
raise "Unknown CompiledMethod version #{version}"
end
cm = CompiledMethod.new
cm.__ivars__ = unmarshal_data
cm.metadata = unmarshal_data
cm.primitive = unmarshal_data
cm.name = unmarshal_data
cm.iseq = unmarshal_data
Expand Down Expand Up @@ -284,7 +284,7 @@ def marshal(val)
str
when CompiledMethod
str = "M\n1\n"
str.append marshal(val.__ivars__)
str.append marshal(val.metadata)
str.append marshal(val.primitive)
str.append marshal(val.name)
str.append marshal(val.iseq)
Expand Down
4 changes: 4 additions & 0 deletions lib/compiler/generator.rb
Expand Up @@ -223,6 +223,10 @@ def package(klass)
cm.name = @name
cm.primitive = @primitive

if @for_block
cm.add_metadata :for_block, true
end

cm
end

Expand Down
2 changes: 2 additions & 0 deletions vm/builtin/compiledmethod.hpp
Expand Up @@ -22,6 +22,7 @@ namespace rubinius {
const static object_type type = CompiledMethodType;

private:
Object* metadata_; // slot
Symbol* name_; // slot
InstructionSequence* iseq_; // slot
Fixnum* stack_size_; // slot
Expand Down Expand Up @@ -57,6 +58,7 @@ namespace rubinius {
}
#endif

attr_accessor(metadata, Object);
attr_accessor(name, Symbol);
attr_accessor(iseq, InstructionSequence);
attr_accessor(stack_size, Fixnum);
Expand Down
8 changes: 4 additions & 4 deletions vm/llvm/offset.hpp
Expand Up @@ -41,10 +41,10 @@ namespace offset {
const static int tuple_full_size = 1;
const static int tuple_field = 2;

const static int cm_backend_method = 12;
const static int cm_jit_data = 13;
const static int cm_literals = 14;
const static int cm_static_scope = 11;
const static int cm_backend_method = 13;
const static int cm_jit_data = 14;
const static int cm_literals = 15;
const static int cm_static_scope = 12;

const static int msg_name = 0;
const static int msg_module = 1;
Expand Down
5 changes: 3 additions & 2 deletions vm/llvm/types.cpp.gen
Expand Up @@ -225,6 +225,7 @@ StructTy_struct_rubinius__Executable = cast<StructType>(StructTy_struct_rubinius


StructTy_struct_rubinius__CompiledMethod_fields.push_back(StructTy_struct_rubinius__Executable);
StructTy_struct_rubinius__CompiledMethod_fields.push_back(PointerTy_1);
StructTy_struct_rubinius__CompiledMethod_fields.push_back(PointerTy_5);
OpaqueType* OpaqueTy_struct_rubinius__InstructionSequence = OpaqueType::get(getGlobalContext());
mod->addTypeName("struct.rubinius::InstructionSequence", OpaqueTy_struct_rubinius__InstructionSequence);
Expand Down Expand Up @@ -364,8 +365,8 @@ mod->addTypeName("struct.rubinius::Object", StructTy_struct_rubinius__Object);
std::vector<const Type*>StructTy_struct_rubinius__ObjectFlags_fields;
StructTy_struct_rubinius__ObjectFlags_fields.push_back(IntegerType::get(getGlobalContext(), 16));
StructTy_struct_rubinius__ObjectFlags_fields.push_back(IntegerType::get(getGlobalContext(), 8));
StructTy_struct_rubinius__ObjectFlags_fields.push_back(IntegerType::get(getGlobalContext(), 8));
StructType* StructTy_struct_rubinius__ObjectFlags = StructType::get(mod->getContext(), StructTy_struct_rubinius__ObjectFlags_fields, /*isPacked=*/true);
StructTy_struct_rubinius__ObjectFlags_fields.push_back(IntegerType::get(getGlobalContext(), 32));
StructType* StructTy_struct_rubinius__ObjectFlags = StructType::get(mod->getContext(), StructTy_struct_rubinius__ObjectFlags_fields, /*isPacked=*/false);
mod->addTypeName("struct.rubinius::ObjectFlags", StructTy_struct_rubinius__ObjectFlags);

mod->addTypeName("struct.rubinius::ObjectHeader", StructTy_struct_rubinius__ObjectHeader);
Expand Down
2 changes: 1 addition & 1 deletion vm/marshal.cpp
Expand Up @@ -146,7 +146,7 @@ namespace rubinius {

CompiledMethod* cm = CompiledMethod::create(state);

cm->ivars(state, unmarshal());
cm->metadata(state, unmarshal());
cm->primitive(state, (Symbol*)unmarshal());
cm->name(state, (Symbol*)unmarshal());
cm->iseq(state, (InstructionSequence*)unmarshal());
Expand Down

0 comments on commit f653ab9

Please sign in to comment.