From f72488369181551f620b2e508b5db50fd0a2202b Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 21 Apr 2014 21:21:45 +0300 Subject: [PATCH] adds position to instruction --- lib/asm/arm/instruction.rb | 16 ++++++++++++---- lib/asm/arm/memory_access_builder.rb | 2 +- lib/asm/assembler.rb | 5 ++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/asm/arm/instruction.rb b/lib/asm/arm/instruction.rb index feecdea5..cb9b1e71 100644 --- a/lib/asm/arm/instruction.rb +++ b/lib/asm/arm/instruction.rb @@ -11,9 +11,8 @@ module Asm include InstructionTools 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 - @ast_asm = ast_asm opcode = node.opcode args = node.args @@ -38,6 +37,14 @@ module Asm @s end + def at position + @position = position + end + + def length + 4 + end + OPC_DATA_PROCESSING = 0b00 OPC_MEMORY_ACCESS = 0b01 OPC_STACK = 0b10 @@ -105,13 +112,13 @@ module Asm builder.cond = COND_BITS[@cond] builder.rd = reg_ref(args[1]) builder.build_operand args[0] - builder.assemble io, as, @ast_asm, self + builder.assemble io, as, self when :ldrb, :ldr builder = MemoryAccessBuilder.new(OPC_MEMORY_ACCESS, (opcode == :ldrb ? 1 : 0), 1) builder.cond = COND_BITS[@cond] builder.rd = reg_ref(args[0]) builder.build_operand args[1] - builder.assemble io, as, @ast_asm, self + builder.assemble io, as, self when :push, :pop # downward growing, decrement before memory access # official ARM style stack as used by gas @@ -133,6 +140,7 @@ module Asm # 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::LabelRefNode)) + #not yet tested/supported 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) #write 0 "for now" and let relocation happen diff --git a/lib/asm/arm/memory_access_builder.rb b/lib/asm/arm/memory_access_builder.rb index 5b899c66..778c160e 100644 --- a/lib/asm/arm/memory_access_builder.rb +++ b/lib/asm/arm/memory_access_builder.rb @@ -65,7 +65,7 @@ module Asm 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 # 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 diff --git a/lib/asm/assembler.rb b/lib/asm/assembler.rb index f84a30dc..ac5f975c 100644 --- a/lib/asm/assembler.rb +++ b/lib/asm/assembler.rb @@ -7,15 +7,18 @@ module Asm class Assembler def initialize @objects = [] + @position = 0 @label_objects = [] @relocations = [] end attr_reader :relocations, :objects def add_object(obj) + obj.at(@position) + @position += obj.length @objects << obj end - + def add_relocation(*args) reloc = Asm::Relocation.new(*args) #raise "reloc #{reloc.inspect}"