rename data_object to string_node and move the padding there

This commit is contained in:
Torsten Ruger 2014-04-23 00:12:43 +03:00
parent b240dc5100
commit 778890298c
7 changed files with 33 additions and 30 deletions

View File

@ -6,7 +6,7 @@ require 'asm/arm/generator_label'
require 'asm/nodes' require 'asm/nodes'
require 'stream_reader' require 'stream_reader'
require 'stringio' require 'stringio'
require "asm/data_object" require "asm/string_node"
module Asm module Asm
module Arm module Arm

View File

@ -46,7 +46,7 @@ module Asm
def build_operand(arg , position = 0) def build_operand(arg , position = 0)
#position only needed for calculating relative addresses to data objects #position only needed for calculating relative addresses to data objects
#there is a design stink here which makes my head ache. But shanti shanti #there is a design stink here which makes my head ache. But shanti shanti
if arg.is_a?(Asm::DataObject) if arg.is_a?(Asm::StringNode)
# do pc relative addressing with the difference to the instuction # do pc relative addressing with the difference to the instuction
# 8 is for the funny pipeline adjustment (ie oc pointing to fetch and not execute) # 8 is for the funny pipeline adjustment (ie oc pointing to fetch and not execute)
arg = Asm::NumLiteralNode.new( arg.position - position - 8 ) arg = Asm::NumLiteralNode.new( arg.position - position - 8 )

View File

@ -16,7 +16,7 @@ module Asm
def add_string str def add_string str
value = @string_table[str] value = @string_table[str]
return value if value return value if value
data = Asm::DataObject.new(str) data = Asm::StringNode.new(str)
@string_table[str] = data @string_table[str] = data
end end
@ -27,8 +27,6 @@ module Asm
def add_value(val) def add_value(val)
val.at(@position) val.at(@position)
length = val.length length = val.length
#rounding up to the next 4
length = (((length - 1 ) / 4 ) + 1 ) * 4
@position += length @position += length
@values << val @values << val
end end

View File

@ -1,21 +0,0 @@
module Asm
class DataObject
def initialize(data)
@data = data
end
def position
throw "Not set" unless @address
@address
end
def at address
@address = address
end
def length
@data.length
end
def assemble(io, as)
io << @data
end
end
end

26
lib/asm/string_node.rb Normal file
View File

@ -0,0 +1,26 @@
module Asm
class StringNode
def initialize(str)
#align
length = str.length
# rounding up to the next 4 (always adding one for zero pad)
pad = ((length / 4 ) + 1 ) * 4 - length
raise "#{pad} #{self}" unless pad >= 1
@string = str + "\x00" * pad
end
def position
throw "Not set" unless @address
@address
end
def at address
@address = address
end
def length
@string.length
end
def assemble(io, as)
io << @string
end
end
end

View File

@ -25,7 +25,7 @@ class TestSmallProg < MiniTest::Test
end end
def test_hello def test_hello
hello = "Hello Raisa"+ "\n\x00" hello = "Hello Raisa\n"
@generator.instance_eval { @generator.instance_eval {
mov r7, 4 # 4 == write mov r7, 4 # 4 == write
mov r0 , 1 # stdout mov r0 , 1 # stdout
@ -35,7 +35,7 @@ class TestSmallProg < MiniTest::Test
mov r7, 1 # 1 == exit mov r7, 1 # 1 == exit
swi 0 swi 0
} }
write(7 , 'label') write(7 + hello.length/4 + 1 , 'hello')
end end
#test dropped along with functionality, didn't work and not needed (yet?) TODO #test dropped along with functionality, didn't work and not needed (yet?) TODO

View File

@ -35,10 +35,10 @@ module Asm
bytes = cmd.value.strip.split(/\s+/).map do |hex| bytes = cmd.value.strip.split(/\s+/).map do |hex|
hex.to_i(16) hex.to_i(16)
end.pack('C*') end.pack('C*')
@asm.add_value Asm::DataObject.new(bytes) @asm.add_value Asm::StringNode.new(bytes)
elsif (cmd.name == "asciz") elsif (cmd.name == "asciz")
str = eval(cmd.value) + "\x00" str = eval(cmd.value) + "\x00"
@asm.add_value Asm::DataObject.new(str) @asm.add_value Asm::StringNode.new(str)
elsif (defined?(Asm::Arm) and cmd.name == 'addrtable') elsif (defined?(Asm::Arm) and cmd.name == 'addrtable')
@asm.add_value Asm::Arm::AddrTableObject.new @asm.add_value Asm::Arm::AddrTableObject.new
else else