rename assembler to text_writer

as “assembly” really happens in the machine now
This commit is contained in:
Torsten Ruger 2018-03-29 18:17:19 +03:00
parent e012f16d7f
commit 3844a738cd
7 changed files with 50 additions and 42 deletions

8
.reek
View File

@ -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"

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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