From 69b04d930ff639b804bfbc18e24a27d7f26bf44e Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 5 May 2014 15:59:29 +0300 Subject: [PATCH] cleanup --- lib/support/hash_attributes.rb | 6 ++++-- lib/vm/code.rb | 2 +- lib/vm/context.rb | 2 +- lib/vm/instruction.rb | 2 +- lib/vm/program.rb | 27 ++++++++++++++++++++------- test/test_runner.rb | 7 +++++++ 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/support/hash_attributes.rb b/lib/support/hash_attributes.rb index 5ac0c4ad..68c76716 100644 --- a/lib/support/hash_attributes.rb +++ b/lib/support/hash_attributes.rb @@ -11,12 +11,14 @@ module Support name = name.to_s if args.length == 1 #must be assignemnt for ir attr= val if name.include? "=" - return @attributes[name.chop] = args[0] + #puts "setting :#{name.chop}:#{args[0]}" + return @attributes[name.chop.to_sym] = args[0] else super end else - return @attributes[name] + #puts "getting :#{name}:#{@attributes[name.to_sym]}" + return @attributes[name.to_sym] end end end diff --git a/lib/vm/code.rb b/lib/vm/code.rb index e0e6451f..64c3c5b1 100644 --- a/lib/vm/code.rb +++ b/lib/vm/code.rb @@ -39,7 +39,7 @@ module Vm # so currently the interface passes the io (usually string_io) in for the code to assemble itself. # this may change as the writing is still done externally (or that will change) def assemble(io) - throw "Not implemented #{self}" + raise "Not implemented #{self.inspect}" end end end \ No newline at end of file diff --git a/lib/vm/context.rb b/lib/vm/context.rb index 524f475a..cdc9dead 100644 --- a/lib/vm/context.rb +++ b/lib/vm/context.rb @@ -10,7 +10,7 @@ module Vm def initialize program @attributes = {} - @attributes["program"] = program + @attributes[:program] = program end end diff --git a/lib/vm/instruction.rb b/lib/vm/instruction.rb index 2fef02aa..3a6c7826 100644 --- a/lib/vm/instruction.rb +++ b/lib/vm/instruction.rb @@ -26,7 +26,7 @@ module Vm include Support::HashAttributes def initialize options - @options = options + @attributes = options end end diff --git a/lib/vm/program.rb b/lib/vm/program.rb index 74614e8f..46cb01d9 100644 --- a/lib/vm/program.rb +++ b/lib/vm/program.rb @@ -54,12 +54,16 @@ module Vm fun end + # linking entry , main , exit + # functions , objects def link_at( start , context) @position = start @entry.link_at( start , context ) start += @entry.length @main.link_at( start , context ) start += @main.length + @exit.link_at( start , context) + start += @exit.length @functions.each do |function| function.link_at(start , context) start += function.length @@ -68,17 +72,26 @@ module Vm o.link_at(start , context) start += o.length end - @exit.link_at( start , context) - start += @exit.length + end + + # assemble in the same order as linked + def assemble( io ) + link_at( @position , {}) #second link in case of forward declarations + @entry.assemble( io ) + @main.assemble( io ) + @exit.assemble( io ) + @functions.each do |function| + function.assemble(io) + end + @objects.each do |o| + o.assemble(io) + end + io end def main= code @main = code end - - private - # the main function - def create_main - end + end end diff --git a/test/test_runner.rb b/test/test_runner.rb index 2c4b873d..e1fcbd7f 100644 --- a/test/test_runner.rb +++ b/test/test_runner.rb @@ -29,6 +29,13 @@ class TestRunner < MiniTest::Test binary = program.assemble(StringIO.new ) + writer = Elf::ObjectWriter.new(Elf::Constants::TARGET_ARM) + + assembly = program.assemble(StringIO.new) + + writer.set_text assembly + writer.save("#{file}_test.o") + puts program.to_yaml end