rename assembler to text_writer
as “assembly” really happens in the machine now
This commit is contained in:
parent
e012f16d7f
commit
3844a738cd
8
.reek
8
.reek
@ -9,7 +9,7 @@ FeatureEnvy:
|
|||||||
- "Vool::Compiler"
|
- "Vool::Compiler"
|
||||||
- "Vool::RubyCompiler"
|
- "Vool::RubyCompiler"
|
||||||
- "Risc::Interpreter"
|
- "Risc::Interpreter"
|
||||||
- "Risc::Assembler"
|
- "Risc::TextWriter"
|
||||||
- "Arm::Translator"
|
- "Arm::Translator"
|
||||||
- "Vm::ToCode"
|
- "Vm::ToCode"
|
||||||
TooManyMethods:
|
TooManyMethods:
|
||||||
@ -17,7 +17,7 @@ TooManyMethods:
|
|||||||
- "Vool::Compiler"
|
- "Vool::Compiler"
|
||||||
- "Vool::RubyCompiler"
|
- "Vool::RubyCompiler"
|
||||||
- "Risc::Interpreter"
|
- "Risc::Interpreter"
|
||||||
- "Risc::Assembler"
|
- "Risc::TextWriter"
|
||||||
- "Arm::Translator"
|
- "Arm::Translator"
|
||||||
- "Vm::ToCode"
|
- "Vm::ToCode"
|
||||||
UtilityFunction:
|
UtilityFunction:
|
||||||
@ -25,14 +25,14 @@ UtilityFunction:
|
|||||||
- "Vool::Compiler"
|
- "Vool::Compiler"
|
||||||
- "Vool::RubyCompiler"
|
- "Vool::RubyCompiler"
|
||||||
- "Risc::Interpreter"
|
- "Risc::Interpreter"
|
||||||
- "Risc::Assembler"
|
- "Risc::TextWriter"
|
||||||
- "Arm::Translator"
|
- "Arm::Translator"
|
||||||
- "Vm::ToCode"
|
- "Vm::ToCode"
|
||||||
UncommunicativeMethodName:
|
UncommunicativeMethodName:
|
||||||
exclude:
|
exclude:
|
||||||
- "Vool::Compiler"
|
- "Vool::Compiler"
|
||||||
- "Vool::RubyCompiler"
|
- "Vool::RubyCompiler"
|
||||||
- "Risc::Assembler"
|
- "Risc::TextWriter"
|
||||||
- "Risc::Interpreter"
|
- "Risc::Interpreter"
|
||||||
- "Arm::Translator"
|
- "Arm::Translator"
|
||||||
- "Vm::ToCode"
|
- "Vm::ToCode"
|
||||||
|
@ -20,7 +20,7 @@ module Elf
|
|||||||
@text = Elf::TextSection.new(".text")
|
@text = Elf::TextSection.new(".text")
|
||||||
@object.add_section @text
|
@object.add_section @text
|
||||||
|
|
||||||
assembler = Risc::Assembler.new(@machine , @objects)
|
assembler = Risc::TextWriter.new(@machine , @objects)
|
||||||
set_text assembler.write_as_string
|
set_text assembler.write_as_string
|
||||||
|
|
||||||
# for debug add labels for labels
|
# for debug add labels for labels
|
||||||
|
@ -28,5 +28,5 @@ end
|
|||||||
|
|
||||||
require_relative "risc/instruction"
|
require_relative "risc/instruction"
|
||||||
require_relative "risc/register_value"
|
require_relative "risc/register_value"
|
||||||
require_relative "risc/assembler"
|
require_relative "risc/text_writer"
|
||||||
require_relative "risc/builtin/space"
|
require_relative "risc/builtin/space"
|
||||||
|
@ -10,7 +10,7 @@ module Positioned
|
|||||||
pos = self.positions[object]
|
pos = self.positions[object]
|
||||||
if pos == nil
|
if pos == nil
|
||||||
str = "position accessed but not set, "
|
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]}"
|
str += "for #{object.class} byte_length #{object.byte_length if object.respond_to?(:byte_length)} for #{object.inspect[0...100]}"
|
||||||
raise str
|
raise str
|
||||||
end
|
end
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
module Risc
|
module Risc
|
||||||
# Assemble the object machine into a binary.
|
# To create a binary, we need a so called Text element. Bad name for what is the code
|
||||||
# Assemble first to get positions, then write
|
#
|
||||||
|
# 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
|
class TextWriter
|
||||||
# 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
|
|
||||||
include Logging
|
include Logging
|
||||||
log_level :info
|
log_level :info
|
||||||
|
|
||||||
@ -18,7 +19,10 @@ module Risc
|
|||||||
@load_at = 0x8054 # this is linux/arm
|
@load_at = 0x8054 # this is linux/arm
|
||||||
end
|
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
|
def write_as_string
|
||||||
@stream = StringIO.new
|
@stream = StringIO.new
|
||||||
write_any(@machine.binary_init)
|
write_any(@machine.binary_init)
|
||||||
@ -29,15 +33,15 @@ module Risc
|
|||||||
return @stream.string
|
return @stream.string
|
||||||
end
|
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
|
def write_debug
|
||||||
all = @objects.values.sort{|a,b| Positioned.position(a) <=> Positioned.position(b)}
|
@objects.each do |id , objekt|
|
||||||
all.each do |objekt|
|
|
||||||
next if objekt.is_a?(Risc::Label)
|
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)}"
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Write all the objects
|
||||||
def write_objects
|
def write_objects
|
||||||
# then the objects , not code yet
|
# then the objects , not code yet
|
||||||
@objects.each do | id, objekt|
|
@objects.each do | id, objekt|
|
||||||
@ -60,6 +64,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Write any object just logs a bit and passes to write_any_out
|
||||||
def write_any( obj )
|
def write_any( obj )
|
||||||
write_any_log( obj , "Write")
|
write_any_log( obj , "Write")
|
||||||
if @stream.length != Positioned.position(obj)
|
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)}"
|
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
|
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)
|
def write_any_out(obj)
|
||||||
case obj
|
case obj
|
||||||
when Parfait::Word, Symbol
|
when Parfait::Word, Symbol
|
||||||
@ -84,6 +91,7 @@ module Risc
|
|||||||
write_object obj
|
write_object obj
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# write type of the instance, and the variables that are passed
|
# 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
|
# variables ar values, ie int or refs. For refs the object needs to save the object first
|
||||||
def write_object( object )
|
def write_object( object )
|
||||||
@ -179,7 +187,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
|
|
||||||
# pad_after is always in bytes and pads (writes 0's) up to the next 8 word boundary
|
# 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
|
before = stream_position
|
||||||
pad = Padding.padding_for(length) - 4 # four is for the MARKER we write
|
pad = Padding.padding_for(length) - 4 # four is for the MARKER we write
|
||||||
pad.times do
|
pad.times do
|
||||||
@ -195,5 +203,5 @@ module Risc
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
RxFile::Volotile.add(Assembler , [:objects])
|
RxFile::Volotile.add(TextWriter , [:objects])
|
||||||
end
|
end
|
@ -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
|
|
22
test/risc/test_text_writer.rb
Normal file
22
test/risc/test_text_writer.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user