2014-04-16 11:10:58 +02:00
|
|
|
require "asm/nodes"
|
2014-04-14 20:52:16 +02:00
|
|
|
|
2014-04-14 17:09:56 +02:00
|
|
|
module Asm
|
|
|
|
module Arm
|
|
|
|
# ADDRESSING MODE 2
|
|
|
|
# Implemented: immediate offset with offset=0
|
|
|
|
class BuilderB
|
2014-04-14 20:52:16 +02:00
|
|
|
include Asm::Arm::InstructionTools
|
2014-04-14 17:09:56 +02:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
@cond = 0b1110
|
|
|
|
@inst_class = 0
|
|
|
|
@i = 0 #I flag (third bit)
|
|
|
|
@pre_post_index = 0 #P flag
|
|
|
|
@add_offset = 0 #U flag
|
|
|
|
@byte_access = 0 #B flag
|
|
|
|
@w = 0 #W flag
|
|
|
|
@load_store = 0 #L flag
|
|
|
|
@rn = 0
|
|
|
|
@rd = 0
|
|
|
|
@operand = 0
|
|
|
|
end
|
|
|
|
attr_accessor :cond, :inst_class, :i, :pre_post_index, :add_offset,
|
|
|
|
:byte_access, :w, :load_store, :rn, :rd, :operand
|
|
|
|
|
|
|
|
def self.make(inst_class, byte_access, load_store)
|
|
|
|
a = new
|
|
|
|
a.inst_class = inst_class
|
|
|
|
a.byte_access = byte_access
|
|
|
|
a.load_store = load_store
|
|
|
|
a
|
|
|
|
end
|
|
|
|
|
2014-04-16 11:03:39 +02:00
|
|
|
class MathReferenceArgNode < Asm::ReferenceArgNode
|
2014-04-14 17:09:56 +02:00
|
|
|
attr_accessor :op, :right
|
|
|
|
end
|
|
|
|
def simplify_reference(arg)
|
|
|
|
node = MathReferenceArgNode.new
|
|
|
|
|
2014-04-16 11:03:39 +02:00
|
|
|
if (arg.is_a?(Asm::MathNode))
|
2014-04-14 17:09:56 +02:00
|
|
|
node.argument = arg.left
|
|
|
|
node.op = arg.op
|
|
|
|
node.right = arg.right
|
|
|
|
else
|
|
|
|
node.argument = arg
|
|
|
|
end
|
|
|
|
|
|
|
|
node
|
|
|
|
end
|
|
|
|
|
|
|
|
# Build representation for target address
|
|
|
|
def build_operand(arg1)
|
2014-04-16 11:03:39 +02:00
|
|
|
if (arg1.is_a?(Asm::ReferenceArgNode))
|
2014-04-14 17:09:56 +02:00
|
|
|
argr = simplify_reference(arg1.argument)
|
|
|
|
arg = argr.argument
|
2014-04-16 11:03:39 +02:00
|
|
|
if (arg.is_a?(Asm::RegisterArgNode))
|
2014-04-14 17:09:56 +02:00
|
|
|
@i = 0
|
|
|
|
@pre_post_index = 1
|
|
|
|
@w = 0
|
|
|
|
@rn = reg_ref(arg)
|
|
|
|
@operand = 0
|
|
|
|
|
2014-04-16 11:03:39 +02:00
|
|
|
if (argr.op and argr.right.is_a?(Asm::NumLiteralArgNode))
|
2014-04-14 17:09:56 +02:00
|
|
|
val = argr.right.value
|
|
|
|
if (val < 0)
|
|
|
|
@add_offset = 0
|
|
|
|
val *= -1
|
|
|
|
else
|
|
|
|
@add_offset = 1
|
|
|
|
end
|
|
|
|
if (val.abs > 4095)
|
|
|
|
raise Asm::AssemblyError.new('reference offset too large/small (max 4095)', argr.right)
|
|
|
|
end
|
|
|
|
@operand = val
|
|
|
|
elsif (argr.op)
|
|
|
|
raise Asm::AssemblyError.new('reference offset must be an integer literal', argr.right)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise Asm::AssemblyError.new(Asm::ERRSTR_INVALID_ARG, arg)
|
|
|
|
end
|
2014-04-16 11:03:39 +02:00
|
|
|
elsif (arg1.is_a?(Asm::LabelEquivAddrArgNode) or arg1.is_a?(Asm::NumEquivAddrArgNode))
|
2014-04-14 17:09:56 +02:00
|
|
|
@i = 0
|
|
|
|
@pre_post_index = 1
|
|
|
|
@w = 0
|
|
|
|
@rn = 15 # pc
|
|
|
|
@operand = 0
|
|
|
|
@use_addrtable_reloc = true
|
|
|
|
@addrtable_reloc_target = arg1
|
|
|
|
else
|
|
|
|
raise Asm::AssemblyError.new(Asm::ERRSTR_INVALID_ARG, arg1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(io, as, ast_asm, inst)
|
|
|
|
val = operand | (rd << 12) | (rn << 12+4) |
|
|
|
|
(load_store << 12+4+4) | (w << 12+4+4+1) |
|
|
|
|
(byte_access << 12+4+4+1+1) | (add_offset << 12+4+4+1+1+1) |
|
|
|
|
(pre_post_index << 12+4+4+1+1+1+1) | (i << 12+4+4+1+1+1+1+1) |
|
|
|
|
(inst_class << 12+4+4+1+1+1+1+1+1) | (cond << 12+4+4+1+1+1+1+1+1+2)
|
|
|
|
if (@use_addrtable_reloc)
|
2014-04-14 20:52:16 +02:00
|
|
|
closest_addrtable = Asm::Arm.closest_addrtable(as)
|
2014-04-16 11:03:39 +02:00
|
|
|
if (@addrtable_reloc_target.is_a?(Asm::LabelEquivAddrArgNode))
|
2014-04-14 17:09:56 +02:00
|
|
|
obj = ast_asm.object_for_label(@addrtable_reloc_target.label, inst)
|
|
|
|
ref_label = closest_addrtable.add_label(obj)
|
2014-04-16 11:03:39 +02:00
|
|
|
elsif (@addrtable_reloc_target.is_a?(Asm::NumEquivAddrArgNode))
|
2014-04-14 17:09:56 +02:00
|
|
|
ref_label = closest_addrtable.add_const(@addrtable_reloc_target.value)
|
|
|
|
end
|
2014-04-14 20:52:16 +02:00
|
|
|
as.add_relocation io.tell, ref_label, Asm::Arm::R_ARM_PC12,
|
|
|
|
Asm::Arm::Instruction::RelocHandler
|
2014-04-14 17:09:56 +02:00
|
|
|
end
|
|
|
|
io.write_uint32 val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|