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-16 09:42:25 +02:00
|
|
|
def initialize(first, attributes)
|
|
|
|
super(first , attributes)
|
2014-05-16 18:56:13 +02:00
|
|
|
@attributes[:update_status] = 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-16 09:42:25 +02:00
|
|
|
arg = @first
|
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
|
2014-05-14 11:08:06 +02:00
|
|
|
arg = Vm::IntegerConstant.new( arg )
|
2014-05-05 21:21:11 +02:00
|
|
|
end
|
2014-05-14 11:54:23 +02:00
|
|
|
if arg.is_a?(Vm::Block) or arg.is_a?(Vm::Function)
|
2014-05-03 14:13:15 +02:00
|
|
|
diff = arg.position - self.position - 8
|
2014-05-14 11:08:06 +02:00
|
|
|
arg = Vm::IntegerConstant.new(diff)
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
2014-05-14 11:08:06 +02:00
|
|
|
if (arg.is_a?(Vm::IntegerConstant))
|
|
|
|
jmp_val = arg.integer >> 2
|
2014-05-03 14:13:15 +02:00
|
|
|
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-16 09:42:25 +02:00
|
|
|
arg = @first
|
2014-05-05 21:21:11 +02:00
|
|
|
if( arg.is_a? Fixnum ) #HACK to not have to change the code just now
|
2014-05-14 11:08:06 +02:00
|
|
|
arg = Vm::IntegerConstant.new( arg )
|
2014-05-05 21:21:11 +02:00
|
|
|
end
|
2014-05-14 11:08:06 +02:00
|
|
|
if (arg.is_a?(Vm::IntegerConstant))
|
|
|
|
packed = [arg.integer].pack('L')[0,3]
|
2014-05-03 14:13:15 +02:00
|
|
|
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
|