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
|
|
|
# There are only three call instructions in arm branch (b), call (bl) and syscall (swi)
|
|
|
|
|
|
|
|
# A branch could be called a jump as it has no notion of returning
|
|
|
|
|
|
|
|
# The pc is put into the link register to make a return possible
|
|
|
|
# a return is affected by moving the stored link register into the pc, effectively a branch
|
|
|
|
|
|
|
|
# swi (SoftWareInterrupt) or system call is how we call the kernel.
|
|
|
|
# in Arm the register layout is different and so we have to place the syscall code into register 7
|
|
|
|
# Registers 0-6 hold the call values as for a normal c call
|
|
|
|
|
2014-05-03 21:18:04 +02:00
|
|
|
class CallInstruction < Vm::CallInstruction
|
2014-05-05 21:21:11 +02:00
|
|
|
include Arm::Constants
|
|
|
|
|
|
|
|
# arm intrucioons are pretty sensible, and always 4 bytes (thumb not supported)
|
|
|
|
def length
|
|
|
|
4
|
|
|
|
end
|
|
|
|
|
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
|
2014-05-14 09:47:30 +02:00
|
|
|
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
|
2014-05-05 21:21:11 +02:00
|
|
|
end
|
2014-05-03 14:13:15 +02:00
|
|
|
|
|
|
|
def assemble(io)
|
2014-05-10 14:59:46 +02:00
|
|
|
case @attributes[:opcode]
|
2014-05-14 09:47:30 +02:00
|
|
|
when :b, :call
|
2014-05-10 14:47:27 +02:00
|
|
|
arg = @attributes[:left]
|
2014-05-05 23:12:04 +02:00
|
|
|
#puts "BLAB #{arg.inspect}"
|
2014-05-05 21:21:11 +02:00
|
|
|
if( arg.is_a? Fixnum ) #HACK to not have to change the code just now
|
|
|
|
arg = Arm::NumLiteral.new( arg )
|
|
|
|
end
|
|
|
|
if arg.is_a? Vm::Code
|
2014-05-03 14:13:15 +02:00
|
|
|
diff = arg.position - self.position - 8
|
2014-05-05 21:21:11 +02:00
|
|
|
arg = Arm::NumLiteral.new(diff)
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
2014-05-05 21:21:11 +02:00
|
|
|
if (arg.is_a?(Arm::NumLiteral))
|
2014-05-03 14:13:15 +02:00
|
|
|
jmp_val = arg.value >> 2
|
|
|
|
packed = [jmp_val].pack('l')
|
|
|
|
# signed 32-bit, condense to 24-bit
|
|
|
|
# TODO add check that the value fits into 24 bits
|
|
|
|
io << packed[0,3]
|
|
|
|
else
|
2014-05-14 09:47:30 +02:00
|
|
|
raise "else not coded arg =#{arg}: #{inspect}"
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
2014-05-14 09:47:30 +02:00
|
|
|
io.write_uint8 op_bit_code | (COND_CODES[@attributes[:condition_code]] << 4)
|
2014-05-03 14:13:15 +02:00
|
|
|
when :swi
|
2014-05-10 14:47:27 +02:00
|
|
|
arg = @attributes[:left]
|
2014-05-05 21:21:11 +02:00
|
|
|
if( arg.is_a? Fixnum ) #HACK to not have to change the code just now
|
|
|
|
arg = Arm::NumLiteral.new( arg )
|
|
|
|
end
|
|
|
|
if (arg.is_a?(Arm::NumLiteral))
|
2014-05-03 14:13:15 +02:00
|
|
|
packed = [arg.value].pack('L')[0,3]
|
|
|
|
io << packed
|
2014-05-10 14:59:46 +02:00
|
|
|
io.write_uint8 0b1111 | (COND_CODES[@attributes[:condition_code]] << 4)
|
2014-05-03 14:13:15 +02:00
|
|
|
else
|
2014-05-05 21:21:11 +02:00
|
|
|
raise "invalid operand argument expected literal not #{arg} #{inspect}"
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
2014-05-14 09:47:30 +02:00
|
|
|
else
|
|
|
|
raise "Should not be the case #{inspect}"
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end#class
|
|
|
|
end
|