diff --git a/lib/asm/arm_assembler.rb b/lib/asm/arm_assembler.rb index 8fa94902..edd93183 100644 --- a/lib/asm/arm_assembler.rb +++ b/lib/asm/arm_assembler.rb @@ -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) diff --git a/lib/asm/generator_label.rb b/lib/asm/generator_label.rb deleted file mode 100644 index a15b81ea..00000000 --- a/lib/asm/generator_label.rb +++ /dev/null @@ -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 diff --git a/lib/asm/instruction.rb b/lib/asm/instruction.rb index d20377e6..b29219fa 100644 --- a/lib/asm/instruction.rb +++ b/lib/asm/instruction.rb @@ -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 diff --git a/lib/asm/label.rb b/lib/asm/label.rb new file mode 100644 index 00000000..79e26bab --- /dev/null +++ b/lib/asm/label.rb @@ -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 \ No newline at end of file diff --git a/lib/asm/label_object.rb b/lib/asm/label_object.rb deleted file mode 100644 index 36c19c9d..00000000 --- a/lib/asm/label_object.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/lib/asm/nodes.rb b/lib/asm/nodes.rb index bc6c99fc..d2f64185 100644 --- a/lib/asm/nodes.rb +++ b/lib/asm/nodes.rb @@ -23,7 +23,7 @@ module Asm end end - #maybe not used at all as code_gen::instruction raises if used. + # maybe not used at all as code_gen::instruction raises if used. # instead now using Arrays class RegisterList attr_accessor :registers @@ -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 diff --git a/test/small_program_test.rb b/test/small_program_test.rb index 313b6bdf..822d598f 100644 --- a/test/small_program_test.rb +++ b/test/small_program_test.rb @@ -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 }