reduced label mess

This commit is contained in:
Torsten Ruger 2014-04-23 13:52:34 +03:00
parent bc60e1d265
commit 69c1f8fccc
7 changed files with 50 additions and 69 deletions

View File

@ -1,6 +1,5 @@
require 'asm/arm_assembler'
require 'asm/instruction'
require 'asm/generator_label'
require 'asm/nodes'
require 'stream_reader'
require 'stringio'
@ -38,9 +37,7 @@ module Asm
arg_nodes << Asm::NumLiteral.new(arg)
elsif (arg.is_a?(String))
arg_nodes << add_string(arg)
elsif (arg.is_a?(Symbol))
arg_nodes << Asm::Label.new(arg.to_s)
elsif (arg.is_a?(Asm::GeneratorLabel))
elsif (arg.is_a?(Asm::Label))
arg_nodes << arg
else
raise 'Invalid argument `%s\' for instruction' % arg.inspect
@ -99,14 +96,14 @@ module Asm
@values << val
end
def label
label = Asm::GeneratorLabel.new(self)
def label name
label = Label.new(name , self)
@labels << label
label
end
def label!
label.set!
def label! name
label(name).set!
end
def assemble(io)

View File

@ -1,17 +0,0 @@
require "asm/label_object"
class Asm::GeneratorLabel < Asm::LabelObject
def initialize(asm)
@asm = asm
end
def at pos
@position = pos
end
def length
0
end
def set!
@asm.add_value self
self
end
end

View File

@ -3,6 +3,7 @@ require "asm/instruction_tools"
require "asm/normal_builder"
require "asm/memory_access_builder"
require "asm/stack_builder"
require "asm/label"
module Asm
@ -91,18 +92,16 @@ module Asm
builder.assemble io, as
when :b, :bl
arg = args[0]
if arg.is_a? Label
diff = arg.position - self.position - 8
arg = NumLiteral.new(diff)
end
if (arg.is_a?(Asm::NumLiteral))
jmp_val = arg.value >> 2
packed = [jmp_val].pack('l')
# signed 32-bit, condense to 24-bit
# TODO add check that the value fits into 24 bits
io << packed[0,3]
elsif (arg.is_a?(Asm::LabelObject) or arg.is_a?(Asm::Label))
#not yet tested/supported
# arg = @ast_asm.object_for_label(arg.label, self) if arg.is_a?(Asm::Label)
#write 0 "for now" and let relocation happen
raise "not coded #{arg.inspect}"
io << "\x00\x00\x00"
else
raise "else not coded #{arg.inspect}"
end

37
lib/asm/label.rb Normal file
View File

@ -0,0 +1,37 @@
module Asm
class Label
def initialize(name , asm)
@@oh = 1
@name = name
@asm = asm
@position = nil
end
attr_writer :position , :name
def position
if (@position.nil?)
raise 'Tried to use label object that has not been set'
end
@position
end
def at pos
puts "called #{self}"
@position = pos
end
def length
0
end
def assemble(io, as)
self.position = io.tell
end
def set!
@asm.add_value self
self
end
end
end

View File

@ -1,22 +0,0 @@
module Asm
class LabelObject
def initialize
@address = nil
end
attr_writer :address
def address
if (@address.nil?)
raise 'Tried to use label object that has not been set'
end
@address
end
def assemble(io, as)
self.address = io.tell
end
end
end

View File

@ -40,17 +40,4 @@ module Asm
end
end
class Label
attr_accessor :label, :label_object
def initialize label , object = nil
@label = label
@label_object = object
end
end
class ParseError < StandardError
def initialize(message, s)
super(message)
end
end
end

View File

@ -15,9 +15,9 @@ class TestSmallProg < MiniTest::Test
def test_loop
@generator.instance_eval {
mov r0, 5 #1
loop_start = label!
start = label!(:loop_start)
subs r0, r0, 1 #2
bne loop_start #3
bne start #3
mov r7, 1 #4
swi 0 #5 5 instructions
}