2014-05-03 14:13:15 +02:00
|
|
|
require_relative "instruction"
|
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
|
|
|
|
|
|
|
|
# A call has the bl code as someone thought "branch with link" is a useful name.
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def initialize(options)
|
|
|
|
super(options)
|
|
|
|
@update_status_flag = 0
|
|
|
|
@condition_code = :al
|
|
|
|
@opcode = options[:opcode]
|
|
|
|
@args = [options[:left] , options[:right] , options[:extra]]
|
|
|
|
@operand = 0
|
|
|
|
end
|
2014-05-03 14:13:15 +02:00
|
|
|
|
|
|
|
def assemble(io)
|
2014-05-05 21:21:11 +02:00
|
|
|
case @opcode
|
2014-05-03 14:13:15 +02:00
|
|
|
when :b, :bl
|
2014-05-05 21:21:11 +02:00
|
|
|
arg = @args[0]
|
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-05 21:21:11 +02:00
|
|
|
raise "else not coded #{inspect}"
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
|
|
|
io.write_uint8 OPCODES[opcode] | (COND_CODES[@condition_code] << 4)
|
|
|
|
when :swi
|
2014-05-05 21:21:11 +02:00
|
|
|
arg = @args[0]
|
|
|
|
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
|
|
|
|
io.write_uint8 0b1111 | (COND_CODES[@condition_code] << 4)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end#class
|
|
|
|
end
|