lots of rework on assembly. constant object for layouts, hash implementation.

This commit is contained in:
Torsten Ruger 2014-08-30 13:48:52 +03:00
parent d33077c2b1
commit 50af6a8f41

View File

@ -1,10 +1,12 @@
module Register
class LinkSlot
def initialize
def initialize o
@position = -1
@length = -1
@object = o
end
attr_reader :object
attr_accessor :position , :length , :layout
end
@ -25,6 +27,12 @@ module Register
link_object(@space)
at = 0
@objects.each do |id , slot|
next unless slot.object.is_a? Virtual::CompiledMethod
slot.position = at
at += slot.length
end
@objects.each do |id , slot|
next if slot.object.is_a? Virtual::CompiledMethod
slot.position = at
at += slot.length
end
@ -33,33 +41,35 @@ module Register
def assemble
link
@stream = StringIO.new
assemble_object( @space )
puts "leng #{@stream.length}"
@objects.each do |id , slot|
next unless slot.object.is_a? Virtual::CompiledMethod
assemble_object( slot.object )
end
@objects.each do |id , slot|
next if slot.object.is_a? Virtual::CompiledMethod
assemble_object( slot.object )
end
puts "Assembled #{@stream.length}"
end
def link_object(object)
slot = @objects[object.object_id]
unless slot
slot = LinkSlot.new
@objects[object.object_id] = slot
end
slot.layout = layout_for(object)
return slot.length if slot
slot = LinkSlot.new object
@objects[object.object_id] = slot
slot.layout = layout_for(object)
clazz = object.class.name.split("::").last
slot.length = send("link_#{clazz}".to_sym , object)
link_object(slot.layout[:names])
slot.length
end
def assemble_object object
slot = get_slot(object)
raise "Object not linked #{object_id}=>#{object.class}" unless slot
raise "Object not linked #{object_id}=>#{object.class}, #{object.inspect}" unless slot
clazz = object.class.name.split("::").last
send("assemble_#{clazz}".to_sym , object)
end
def link_layout(object)
slot = get_slot(object)
layout = slot.layout
length = link_object(layout[:names])
padded( layout[:names].length)
send("assemble_#{clazz}".to_sym , slot)
slot.position
end
# write type and layout of the instance, and the variables that are passed
@ -69,12 +79,12 @@ module Register
raise "Object not linked #{object.inspect}" unless slot
layout = slot.layout
@stream.write_uint32( 0 ) #TODO types
@stream.write_uint32( assemble_object(layout[:names]) ) #ref
write_ref(layout[:names])
variables.each do |var|
@stream.write_uint32 var
write_ref var
end
## padding to the nearest 8
((padded(variables.length) - variables)/4).times do
((padded(variables.length) - variables.length)).times do
@stream.write_uint32 0
end
slot.position
@ -88,9 +98,8 @@ module Register
padded(array.length)
end
def assemble_Array array
slot = get_slot(array)
raise "Array not linked #{object.inspect}" unless slot
def assemble_Array slot
array = slot.object
layout = slot.layout
@stream.write_uint32( 0 ) #TODO types
@stream.write_uint32( assemble_object(layout[:names]) ) #ref
@ -105,23 +114,26 @@ module Register
end
def link_Hash( hash )
link_object(hash.keys)
link_object(hash.values)
link_layout(hash)
slot = get_slot(hash)
#hook the key/values arrays into the layout (just because it was around)
link_object(slot.layout[:keys])
link_object(slot.layout[:values])
padded(2)
end
def assemble_Hash hash
assemble_self( hash , [ hash.keys , hash.values] )
def assemble_Hash slot
# so here we can be sure to have _identical_ keys/values arrays
assemble_self( slot.object , [ slot.layout[:keys] , slot.layout[:values] ] )
end
def link_BootSpace(space)
link_object( space.classes)
link_object(space.classes)
link_object(space.objects)
padded( 2 )
end
def assemble_BootSpace(space)
def assemble_BootSpace(slot)
space = slot.object
assemble_self(space , [space.classes,space.objects] )
end
@ -132,28 +144,23 @@ module Register
padded(3)
end
def assemble_BootClass(clazz)
assemble_object(clazz.name)
assemble_object(clazz.super_class_name)
clazz.instance_methods.each do |meth|
assemble_object(meth)
end
def assemble_BootClass(slot)
clazz = slot.object
assemble_self( clazz , [clazz.name , clazz.super_class_name, clazz.instance_methods] )
end
def link_CompiledMethod(method)
length = 0
# NOT an ARRAY, just a bag of bytes
method.blocks.each do |block|
block.codes.each do |code|
length += code.length
end
end
length = method.blocks.inject(0) { |c , block| c += block.length }
padded(length)
end
def assemble_CompiledMethod(method)
assemble_object(method.name)
def assemble_CompiledMethod(slot)
method = slot.object
@stream.write_uint32( 0 ) #TODO types
write_ref(slot.layout[:names]) #ref of layout
# TODO the assembly may have to move to the object to be more extensible
method.blocks.each do |block|
block.codes.each do |code|
code.assemble( @stream , self )
@ -169,11 +176,16 @@ module Register
return link_String(sym.to_s)
end
def link_StringConstant( sc)
def link_StringConstant(sc)
return link_String(sc.string)
end
def assemble_String( str )
slot = get_slot(str)
raise "String not linked #{str}" unless slot
layout = slot.layout
@stream.write_uint32( 0 ) #TODO types
@stream.write_uint32( assemble_object(layout[:names]) ) #ref
@stream.write str
end
@ -187,7 +199,22 @@ module Register
private
def get_slot(object)
@objects[object.object_id]
slot = @objects[object.object_id]
return slot if slot
if(object.is_a? Array)
@objects.each do |k,slot|
next unless slot.object.is_a? Array
if(slot.object.length == object.length)
same = true
slot.object.each_with_index do |v,index|
same = false unless v == object[index]
end
puts slot.object.first.class if same
return slot if same
end
end
end
nil
end
def write_ref object
@ -203,16 +230,22 @@ module Register
8 * (1 + (len + 1) / 8)
end
# class variables to have _identical_ objects passed back (stops recursion)
@@ARRAY = { :names => [] , :types => []}
@@HASH = { :names => [:keys,:values] , :types => [Virtual::Reference,Virtual::Reference]}
@@CLAZZ = { :names => [:name , :super_class_name , :instance_methods] , :types => [Virtual::Reference,Virtual::Reference,Virtual::Reference]}
@@SPACE = { :names => [:classes,:objects] , :types => [Virtual::Reference,Virtual::Reference]}
def layout_for(object)
case object
when Array , Symbol , String , Virtual::CompiledMethod , Virtual::Block , Virtual::StringConstant
{ :names => [] , :types => []}
@@ARRAY
when Hash
{ :names => [:keys,:values] , :types => [Virtual::Reference,Virtual::Reference]}
@@HASH.merge :keys => object.keys , :values => object.values
when Virtual::BootClass
{ :names => [:name , :super_class_name , :instance_methods] , :types => [Virtual::Reference,Virtual::Reference,Virtual::Reference]}
@@CLAZZ
when Virtual::BootSpace
{ :names => [:classes,:objects] , :types => [Virtual::Reference,Virtual::Reference]}
@@SPACE
else
raise "linker encounters unknown class #{object.class}"
end