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