adds position to instruction

This commit is contained in:
Torsten Ruger 2014-04-21 21:21:45 +03:00
parent fc81122bc4
commit f724883691
3 changed files with 17 additions and 6 deletions

View File

@ -11,9 +11,8 @@ module Asm
include InstructionTools include InstructionTools
COND_POSTFIXES = Regexp.union(%w(eq ne cs cc mi pl vs vc hi ls ge lt gt le al)).source COND_POSTFIXES = Regexp.union(%w(eq ne cs cc mi pl vs vc hi ls ge lt gt le al)).source
def initialize(node, ast_asm = nil) def initialize(node)
@node = node @node = node
@ast_asm = ast_asm
opcode = node.opcode opcode = node.opcode
args = node.args args = node.args
@ -38,6 +37,14 @@ module Asm
@s @s
end end
def at position
@position = position
end
def length
4
end
OPC_DATA_PROCESSING = 0b00 OPC_DATA_PROCESSING = 0b00
OPC_MEMORY_ACCESS = 0b01 OPC_MEMORY_ACCESS = 0b01
OPC_STACK = 0b10 OPC_STACK = 0b10
@ -105,13 +112,13 @@ module Asm
builder.cond = COND_BITS[@cond] builder.cond = COND_BITS[@cond]
builder.rd = reg_ref(args[1]) builder.rd = reg_ref(args[1])
builder.build_operand args[0] builder.build_operand args[0]
builder.assemble io, as, @ast_asm, self builder.assemble io, as, self
when :ldrb, :ldr when :ldrb, :ldr
builder = MemoryAccessBuilder.new(OPC_MEMORY_ACCESS, (opcode == :ldrb ? 1 : 0), 1) builder = MemoryAccessBuilder.new(OPC_MEMORY_ACCESS, (opcode == :ldrb ? 1 : 0), 1)
builder.cond = COND_BITS[@cond] builder.cond = COND_BITS[@cond]
builder.rd = reg_ref(args[0]) builder.rd = reg_ref(args[0])
builder.build_operand args[1] builder.build_operand args[1]
builder.assemble io, as, @ast_asm, self builder.assemble io, as, self
when :push, :pop when :push, :pop
# downward growing, decrement before memory access # downward growing, decrement before memory access
# official ARM style stack as used by gas # official ARM style stack as used by gas
@ -133,6 +140,7 @@ module Asm
# 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::LabelRefNode)) elsif (arg.is_a?(Asm::LabelObject) or arg.is_a?(Asm::LabelRefNode))
#not yet tested/supported
arg = @ast_asm.object_for_label(arg.label, self) if arg.is_a?(Asm::LabelRefNode) arg = @ast_asm.object_for_label(arg.label, self) if arg.is_a?(Asm::LabelRefNode)
as.add_relocation(io.tell, arg, Asm::Arm::R_ARM_PC24, RelocHandler) as.add_relocation(io.tell, arg, Asm::Arm::R_ARM_PC24, RelocHandler)
#write 0 "for now" and let relocation happen #write 0 "for now" and let relocation happen

View File

@ -65,7 +65,7 @@ module Asm
end end
end end
def assemble(io, as, generator, inst) def assemble(io, as, inst)
#not sure about these 2 constants. They produce the correct output for str r0 , r1 #not sure about these 2 constants. They produce the correct output for str r0 , r1
# but i can't help thinking that that is because they are not used in that instruction and # but i can't help thinking that that is because they are not used in that instruction and
# so it doesn't matter. Will see # so it doesn't matter. Will see

View File

@ -7,15 +7,18 @@ module Asm
class Assembler class Assembler
def initialize def initialize
@objects = [] @objects = []
@position = 0
@label_objects = [] @label_objects = []
@relocations = [] @relocations = []
end end
attr_reader :relocations, :objects attr_reader :relocations, :objects
def add_object(obj) def add_object(obj)
obj.at(@position)
@position += obj.length
@objects << obj @objects << obj
end end
def add_relocation(*args) def add_relocation(*args)
reloc = Asm::Relocation.new(*args) reloc = Asm::Relocation.new(*args)
#raise "reloc #{reloc.inspect}" #raise "reloc #{reloc.inspect}"