From 3844a738cd7fb7d8d34c221864cd5f5606dcb61b Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 29 Mar 2018 18:17:19 +0300 Subject: [PATCH] rename assembler to text_writer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit as “assembly” really happens in the machine now --- .reek | 8 +++--- lib/elf/object_writer.rb | 2 +- lib/risc.rb | 2 +- lib/risc/positioned.rb | 2 +- lib/risc/{assembler.rb => text_writer.rb} | 34 ++++++++++++++--------- test/risc/test_assembler.rb | 22 --------------- test/risc/test_text_writer.rb | 22 +++++++++++++++ 7 files changed, 50 insertions(+), 42 deletions(-) rename lib/risc/{assembler.rb => text_writer.rb} (86%) delete mode 100644 test/risc/test_assembler.rb create mode 100644 test/risc/test_text_writer.rb diff --git a/.reek b/.reek index 15dd969a..2d2cf8ea 100644 --- a/.reek +++ b/.reek @@ -9,7 +9,7 @@ FeatureEnvy: - "Vool::Compiler" - "Vool::RubyCompiler" - "Risc::Interpreter" - - "Risc::Assembler" + - "Risc::TextWriter" - "Arm::Translator" - "Vm::ToCode" TooManyMethods: @@ -17,7 +17,7 @@ TooManyMethods: - "Vool::Compiler" - "Vool::RubyCompiler" - "Risc::Interpreter" - - "Risc::Assembler" + - "Risc::TextWriter" - "Arm::Translator" - "Vm::ToCode" UtilityFunction: @@ -25,14 +25,14 @@ UtilityFunction: - "Vool::Compiler" - "Vool::RubyCompiler" - "Risc::Interpreter" - - "Risc::Assembler" + - "Risc::TextWriter" - "Arm::Translator" - "Vm::ToCode" UncommunicativeMethodName: exclude: - "Vool::Compiler" - "Vool::RubyCompiler" - - "Risc::Assembler" + - "Risc::TextWriter" - "Risc::Interpreter" - "Arm::Translator" - "Vm::ToCode" diff --git a/lib/elf/object_writer.rb b/lib/elf/object_writer.rb index 0b850859..d586f8a4 100644 --- a/lib/elf/object_writer.rb +++ b/lib/elf/object_writer.rb @@ -20,7 +20,7 @@ module Elf @text = Elf::TextSection.new(".text") @object.add_section @text - assembler = Risc::Assembler.new(@machine , @objects) + assembler = Risc::TextWriter.new(@machine , @objects) set_text assembler.write_as_string # for debug add labels for labels diff --git a/lib/risc.rb b/lib/risc.rb index d0914660..db19875b 100644 --- a/lib/risc.rb +++ b/lib/risc.rb @@ -28,5 +28,5 @@ end require_relative "risc/instruction" require_relative "risc/register_value" -require_relative "risc/assembler" +require_relative "risc/text_writer" require_relative "risc/builtin/space" diff --git a/lib/risc/positioned.rb b/lib/risc/positioned.rb index 2854c622..70773d5c 100644 --- a/lib/risc/positioned.rb +++ b/lib/risc/positioned.rb @@ -10,7 +10,7 @@ module Positioned pos = self.positions[object] if pos == nil str = "position accessed but not set, " - str += "#{object.object_id.to_s(16)}\n" + str += "0x#{object.object_id.to_s(16)}\n" str += "for #{object.class} byte_length #{object.byte_length if object.respond_to?(:byte_length)} for #{object.inspect[0...100]}" raise str end diff --git a/lib/risc/assembler.rb b/lib/risc/text_writer.rb similarity index 86% rename from lib/risc/assembler.rb rename to lib/risc/text_writer.rb index 60a468b3..350eb3ed 100644 --- a/lib/risc/assembler.rb +++ b/lib/risc/text_writer.rb @@ -1,12 +1,13 @@ module Risc - # Assemble the object machine into a binary. - # Assemble first to get positions, then write + # To create a binary, we need a so called Text element. Bad name for what is the code + # + # Binary code is already created by the Machine (by translating risc to arm to binary) + # + # This class serves to write all the objects of the machine (wich also contain the code) + # into one stream or binary text object. This is then written to an ELF text section. + # - # The assemble function determines the length of an object and then actually - # writes the bytes they are pretty much dependant. In an earlier version they were - # functions on the objects, but now it has gone to a visitor pattern. - - class Assembler + class TextWriter include Logging log_level :info @@ -18,7 +19,10 @@ module Risc @load_at = 0x8054 # this is linux/arm end - # objects must be written in same order as positioned / assembled + # objects must be written in same order as positioned by the machine, namely + # - intial jump + # - all objects + # - all BinaryCode def write_as_string @stream = StringIO.new write_any(@machine.binary_init) @@ -29,15 +33,15 @@ module Risc return @stream.string end - # debugging loop accesses all positions to force an error if it's not set + # debugging loop to write out positions (in debug) def write_debug - all = @objects.values.sort{|a,b| Positioned.position(a) <=> Positioned.position(b)} - all.each do |objekt| + @objects.each do |id , objekt| next if objekt.is_a?(Risc::Label) log.debug "Linked #{objekt.class}:0x#{objekt.object_id.to_s(16)} at 0x#{Positioned.position(objekt).to_s(16)} / 0x#{objekt.padded_length.to_s(16)}" end end + # Write all the objects def write_objects # then the objects , not code yet @objects.each do | id, objekt| @@ -60,6 +64,7 @@ module Risc end end + # Write any object just logs a bit and passes to write_any_out def write_any( obj ) write_any_log( obj , "Write") if @stream.length != Positioned.position(obj) @@ -74,6 +79,8 @@ module Risc log.debug "#{at} #{obj.class}:0x#{obj.object_id.to_s(16)} at stream 0x#{stream_position.to_s(16)} pos:0x#{Positioned.position(obj).to_s(16)} , len:0x#{obj.padded_length.to_s(16)}" end + # Most objects are the same and get passed to write_object + # But Strings and BinaryCode write out binary, so they have different methods (for now) def write_any_out(obj) case obj when Parfait::Word, Symbol @@ -84,6 +91,7 @@ module Risc write_object obj end end + # write type of the instance, and the variables that are passed # variables ar values, ie int or refs. For refs the object needs to save the object first def write_object( object ) @@ -179,7 +187,7 @@ module Risc end # pad_after is always in bytes and pads (writes 0's) up to the next 8 word boundary - def pad_after length + def pad_after( length ) before = stream_position pad = Padding.padding_for(length) - 4 # four is for the MARKER we write pad.times do @@ -195,5 +203,5 @@ module Risc end end - RxFile::Volotile.add(Assembler , [:objects]) + RxFile::Volotile.add(TextWriter , [:objects]) end diff --git a/test/risc/test_assembler.rb b/test/risc/test_assembler.rb deleted file mode 100644 index ffe57bcb..00000000 --- a/test/risc/test_assembler.rb +++ /dev/null @@ -1,22 +0,0 @@ -require_relative "../helper" - -module Risc - class TestAssembler < MiniTest::Test - - def setup - @machine = Risc.machine.boot - end - def test_init - @assembler = Assembler.new(@machine) - end - def test_write_fails - @assembler = Assembler.new(@machine) - assert_raises{ @assembler.write_as_string} #must translate first - end - def test_write_space - assert @machine.position_all - @assembler = Assembler.new(@machine) - assert @assembler.write_as_string - end - end -end diff --git a/test/risc/test_text_writer.rb b/test/risc/test_text_writer.rb new file mode 100644 index 00000000..dd09702a --- /dev/null +++ b/test/risc/test_text_writer.rb @@ -0,0 +1,22 @@ +require_relative "../helper" + +module Risc + class TestTextWriter < MiniTest::Test + + def setup + @machine = Risc.machine.boot + end + def test_init + @text_writer = TextWriter.new(@machine) + end + def test_write_fails + @text_writer = TextWriter.new(@machine) + assert_raises{ @text_writer.write_as_string} #must translate first + end + def test_write_space + assert @machine.position_all + @text_writer = TextWriter.new(@machine) + assert @text_writer.write_as_string + end + end +end