From 97f68a1a54c5dbcddc69df15d07eae44db68e8e2 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 26 Aug 2014 22:35:56 +0300 Subject: [PATCH] basic assembly frame --- lib/register/assembler.rb | 74 ++++++++++++++++++++++++++++++++++++--- lib/virtual/boot_space.rb | 35 ------------------ test/virtual/hello.rb | 2 +- 3 files changed, 71 insertions(+), 40 deletions(-) diff --git a/lib/register/assembler.rb b/lib/register/assembler.rb index 683ebeac..abaa7ca9 100644 --- a/lib/register/assembler.rb +++ b/lib/register/assembler.rb @@ -17,6 +17,24 @@ module Register link_object(@space , 0) end + def assemble + link + @stream = StringIO.new + assemble_object( @space ) + puts "leng #{@stream.length}" + end + + def assemble_object object + slot = @objects[object.object_id] + raise "No slot for #{object_id}" unless slot + if object.is_a? Instruction + object.assemble( @stream , self ) + else + clazz = object.class.name.split("::").last + len = send("assemble_#{clazz}".to_sym , object) + end + end + def link_object(object , at) slot = @objects[object.object_id] unless slot @@ -24,11 +42,11 @@ module Register @objects[object.object_id] = slot end if object.is_a? Instruction + len = 4 + else clazz = object.class.name.split("::").last len = send("link_#{clazz}".to_sym , object , at) - else - len = 4 - end + end slot.length = len len end @@ -38,9 +56,22 @@ module Register space.classes.values.each do |cl| len += link_object(cl , at + len) end + space.objects.each do |o| + len += link_object(o , at + len) + end len end + def assemble_BootSpace(space) + # assemble in the same order as linked + space.classes.values.each do |cl| + assemble_object(cl) + end + space.objects.each do |o| + assemble_object(o) + end + end + def link_BootClass(clazz , at) len = link_object(clazz.name , at) len += link_object(clazz.super_class_name , at + len) @@ -50,14 +81,29 @@ module Register len 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 + end + def link_MethodDefinition(method , at) len = link_object method.name ,at method.blocks.each do |block| - link_object( block , at + len) + len += link_object( block , at + len) end len end + def assemble_MethodDefinition(method) + assemble_object(method.name) + method.blocks.each do |block| + assemble_object(block) + end + end + def link_Block(block , at) len = 0 block.codes.each do |code| @@ -66,14 +112,34 @@ module Register len end + def assemble_Block(block) + block.codes.each do |code| + assemble_object(code) + end + end + def link_String( str , at) return (str.length / 4) + 1 + 2 end + def assemble_String( str ) + @stream.write str + end + def link_Symbol(sym , at) return link_String(sym.to_s , at) end + def assemble_Symbol(sym) + return assemble_String(sym.to_s) + end + + def link_StringConstant( sc , at) + return link_String(sc.string,at) + end + def assemble_StringConstant( sc) + return assemble_String(sc.string) + end end Sof::Volotile.add(Register::Assembler , [:objects]) diff --git a/lib/virtual/boot_space.rb b/lib/virtual/boot_space.rb index 8a599866..32ad4d25 100644 --- a/lib/virtual/boot_space.rb +++ b/lib/virtual/boot_space.rb @@ -102,40 +102,5 @@ module Virtual end c end - - # linking entry , exit , main , classes , objects - def link_at( start , context) - super - @entry.link_at( start , context ) - start += @entry.length - @exit.link_at( start , context) - start += @exit.length - @main.link_at( start , context ) - start += @main.length - @classes.values.each do |clazz| - clazz.link_at(start , context) - start += clazz.length - end - @objects.each do |o| - o.link_at(start , context) - start += o.length - end - end - - # assemble in the same order as linked - def assemble( io ) - link_at( @position , nil) #second link in case of forward declarations - @entry.assemble( io ) - @exit.assemble( io ) - @main.assemble( io ) - @classes.values.each do |clazz| - clazz.assemble(io) - end - @objects.each do |o| - o.assemble(io) - end - io - end - end end diff --git a/test/virtual/hello.rb b/test/virtual/hello.rb index a4ef4ad7..67ae8a16 100644 --- a/test/virtual/hello.rb +++ b/test/virtual/hello.rb @@ -9,7 +9,7 @@ class HelloTest < MiniTest::Test puts "" Virtual::BootSpace.space.run_passes ass = Register::Assembler.new(Virtual::BootSpace.space) - ass.link + ass.assemble puts Sof::Writer.write(expressions) # puts Sof::Writer.write(Virtual::BootSpace.space) end