2014-05-05 21:21:11 +02:00
|
|
|
require_relative "nodes"
|
2014-05-03 14:13:15 +02:00
|
|
|
|
2014-05-03 21:18:04 +02:00
|
|
|
module Arm
|
2014-05-03 14:13:15 +02:00
|
|
|
# ADDRESSING MODE 2
|
|
|
|
# Implemented: immediate offset with offset=0
|
2014-05-03 21:18:04 +02:00
|
|
|
class MemoryInstruction < Vm::MemoryInstruction
|
2014-05-05 21:21:11 +02:00
|
|
|
include Arm::Constants
|
2014-05-03 14:13:15 +02:00
|
|
|
|
2014-05-07 13:58:38 +02:00
|
|
|
def initialize(attributes)
|
|
|
|
super(attributes)
|
2014-05-10 14:59:46 +02:00
|
|
|
@attributes[:update_status_flag] = 0
|
|
|
|
@attributes[:condition_code] = :al
|
2014-05-06 20:36:28 +02:00
|
|
|
@operand = 0
|
|
|
|
|
2014-05-03 14:13:15 +02:00
|
|
|
@pre_post_index = 0 #P flag
|
|
|
|
@add_offset = 0 #U flag
|
|
|
|
@is_load = opcode.to_s[0] == "l" ? 1 : 0 #L (load) flag
|
2014-05-06 20:36:28 +02:00
|
|
|
@rn = :r0 # register zero = zero bit pattern
|
|
|
|
@rd = :r0 # register zero = zero bit pattern
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
2014-05-10 14:59:46 +02:00
|
|
|
# attr_accessor :i, :pre_post_index, :add_offset, :byte_access, :w, :is_load, :rn, :rd
|
2014-05-03 14:13:15 +02:00
|
|
|
|
2014-05-05 21:21:11 +02:00
|
|
|
# arm intrucioons are pretty sensible, and always 4 bytes (thumb not supported)
|
|
|
|
def length
|
|
|
|
4
|
|
|
|
end
|
|
|
|
|
2014-05-03 14:13:15 +02:00
|
|
|
# Build representation for target address
|
|
|
|
def build
|
|
|
|
if( @is_load )
|
2014-05-10 14:47:27 +02:00
|
|
|
@rd = @attributes[:left]
|
|
|
|
arg = @attributes[:right]
|
2014-05-03 14:13:15 +02:00
|
|
|
else #store
|
2014-05-10 14:47:27 +02:00
|
|
|
@rd = @attributes[:right]
|
|
|
|
arg = @attributes[:left]
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
|
|
|
#str / ldr are _serious instructions. With BIG possibilities not half are implemented
|
2014-05-06 20:36:28 +02:00
|
|
|
if (arg.is_a?(Symbol)) #symbol is register
|
2014-05-03 14:13:15 +02:00
|
|
|
@rn = arg
|
2014-05-07 13:58:38 +02:00
|
|
|
if @attributes[:offset]
|
|
|
|
@operand = @attributes[:offset]
|
2014-05-03 14:13:15 +02:00
|
|
|
if (@operand < 0)
|
|
|
|
@add_offset = 0
|
|
|
|
#TODO test/check/understand
|
|
|
|
@operand *= -1
|
|
|
|
else
|
|
|
|
@add_offset = 1
|
|
|
|
end
|
|
|
|
if (@operand.abs > 4095)
|
2014-05-05 21:21:11 +02:00
|
|
|
raise "reference offset too large/small (max 4095) #{arg} #{inspect}"
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
|
|
|
end
|
2014-05-13 15:24:19 +02:00
|
|
|
elsif (arg.is_a?(Vm::StringConstant)) #use pc relative
|
2014-05-06 20:36:28 +02:00
|
|
|
@rn = :pc
|
|
|
|
@operand = arg.position - self.position - 8 #stringtable is after code
|
|
|
|
@add_offset = 1
|
|
|
|
if (@operand.abs > 4095)
|
|
|
|
raise "reference offset too large/small (max 4095) #{arg} #{inspect}"
|
|
|
|
end
|
2014-05-05 21:21:11 +02:00
|
|
|
elsif (arg.is_a?(Arm::Label) or arg.is_a?(Arm::NumLiteral))
|
2014-05-03 14:13:15 +02:00
|
|
|
@pre_post_index = 1
|
|
|
|
@rn = pc
|
|
|
|
@use_addrtable_reloc = true
|
|
|
|
@addrtable_reloc_target = arg
|
|
|
|
else
|
2014-05-05 21:21:11 +02:00
|
|
|
raise "invalid operand argument #{arg.inspect} #{inspect}"
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assemble(io)
|
|
|
|
build
|
2014-05-10 15:08:53 +02:00
|
|
|
i = 0 #I flag (third bit)
|
2014-05-03 14:13:15 +02:00
|
|
|
#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
|
|
|
|
@add_offset = 1
|
|
|
|
@pre_post_index = 1
|
2014-05-10 15:08:53 +02:00
|
|
|
w = 0 #W flag
|
|
|
|
byte_access = opcode.to_s[-1] == "b" ? 1 : 0 #B (byte) flag
|
2014-05-03 14:13:15 +02:00
|
|
|
instuction_class = 0b01 # OPC_MEMORY_ACCESS
|
2014-05-06 20:36:28 +02:00
|
|
|
val = @operand.is_a?(Symbol) ? reg_code(@operand) : @operand
|
2014-05-10 14:59:46 +02:00
|
|
|
val |= (reg_code(@rd) << 12 )
|
|
|
|
val |= (reg_code(@rn) << 12+4) #16
|
|
|
|
val |= (@is_load << 12+4 +4)
|
2014-05-10 15:08:53 +02:00
|
|
|
val |= (w << 12+4 +4+1)
|
|
|
|
val |= (byte_access << 12+4 +4+1+1)
|
2014-05-10 14:59:46 +02:00
|
|
|
val |= (@add_offset << 12+4 +4+1+1+1)
|
|
|
|
val |= (@pre_post_index << 12+4 +4+1+1+1+1)#24
|
2014-05-10 15:08:53 +02:00
|
|
|
val |= (i << 12+4 +4+1+1+1+1 +1)
|
2014-05-03 14:13:15 +02:00
|
|
|
val |= (instuction_class<<12+4 +4+1+1+1+1 +1+1)
|
|
|
|
val |= (cond_bit_code << 12+4 +4+1+1+1+1 +1+1+2)
|
|
|
|
io.write_uint32 val
|
2014-05-06 20:36:28 +02:00
|
|
|
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|