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/arm_assembler'
require 'asm/instruction' require 'asm/instruction'
require 'asm/generator_label'
require 'asm/nodes' require 'asm/nodes'
require 'stream_reader' require 'stream_reader'
require 'stringio' require 'stringio'
@ -38,9 +37,7 @@ module Asm
arg_nodes << Asm::NumLiteral.new(arg) arg_nodes << Asm::NumLiteral.new(arg)
elsif (arg.is_a?(String)) elsif (arg.is_a?(String))
arg_nodes << add_string(arg) arg_nodes << add_string(arg)
elsif (arg.is_a?(Symbol)) elsif (arg.is_a?(Asm::Label))
arg_nodes << Asm::Label.new(arg.to_s)
elsif (arg.is_a?(Asm::GeneratorLabel))
arg_nodes << arg arg_nodes << arg
else else
raise 'Invalid argument `%s\' for instruction' % arg.inspect raise 'Invalid argument `%s\' for instruction' % arg.inspect
@ -99,14 +96,14 @@ module Asm
@values << val @values << val
end end
def label def label name
label = Asm::GeneratorLabel.new(self) label = Label.new(name , self)
@labels << label @labels << label
label label
end end
def label! def label! name
label.set! label(name).set!
end end
def assemble(io) 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/normal_builder"
require "asm/memory_access_builder" require "asm/memory_access_builder"
require "asm/stack_builder" require "asm/stack_builder"
require "asm/label"
module Asm module Asm
@ -91,18 +92,16 @@ module Asm
builder.assemble io, as builder.assemble io, as
when :b, :bl when :b, :bl
arg = args[0] 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)) if (arg.is_a?(Asm::NumLiteral))
jmp_val = arg.value >> 2 jmp_val = arg.value >> 2
packed = [jmp_val].pack('l') packed = [jmp_val].pack('l')
# signed 32-bit, condense to 24-bit # signed 32-bit, condense to 24-bit
# TODO add check that the value fits into 24 bits # TODO add check that the value fits into 24 bits
io << packed[0,3] 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 else
raise "else not coded #{arg.inspect}" raise "else not coded #{arg.inspect}"
end 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
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 end

View File

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