2014-08-30 18:40:37 +02:00
|
|
|
module Arm
|
|
|
|
# There are only three call instructions in arm branch (b), call (bl) and syscall (swi)
|
2015-05-20 16:29:08 +02:00
|
|
|
|
2014-08-30 18:40:37 +02:00
|
|
|
# A branch could be called a jump as it has no notion of returning
|
2015-05-20 16:29:08 +02:00
|
|
|
|
2014-08-30 18:40:37 +02:00
|
|
|
# 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
|
2015-05-20 16:29:08 +02:00
|
|
|
|
2014-08-30 18:40:37 +02:00
|
|
|
# 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-10-02 21:28:34 +02:00
|
|
|
class CallInstruction < Instruction
|
2014-08-30 18:40:37 +02:00
|
|
|
include Arm::Constants
|
|
|
|
|
|
|
|
def initialize(first, attributes)
|
2014-10-02 21:28:34 +02:00
|
|
|
super(attributes)
|
2015-06-25 15:31:09 +02:00
|
|
|
raise "no target" if first.nil?
|
2014-10-02 21:28:34 +02:00
|
|
|
@first = first
|
|
|
|
opcode = @attributes[:opcode].to_s
|
|
|
|
if opcode.length == 3 and opcode[0] == "b"
|
|
|
|
@attributes[:condition_code] = opcode[1,2].to_sym
|
|
|
|
@attributes[:opcode] = :b
|
|
|
|
end
|
|
|
|
if opcode.length == 6 and opcode[0] == "c"
|
|
|
|
@attributes[:condition_code] = opcode[4,2].to_sym
|
|
|
|
@attributes[:opcode] = :call
|
|
|
|
end
|
2014-08-30 18:40:37 +02:00
|
|
|
@attributes[:update_status] = 0
|
|
|
|
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
|
|
|
|
end
|
2014-10-02 21:28:34 +02:00
|
|
|
|
2014-09-16 16:16:13 +02:00
|
|
|
def assemble(io)
|
2014-08-30 18:40:37 +02:00
|
|
|
case @attributes[:opcode]
|
|
|
|
when :b, :call
|
|
|
|
arg = @first
|
2015-05-29 11:33:40 +02:00
|
|
|
if arg.is_a?(Virtual::Block) or arg.is_a?(Parfait::Method)
|
2014-09-09 16:36:33 +02:00
|
|
|
#relative addressing for jumps/calls
|
2015-05-30 11:20:39 +02:00
|
|
|
# but because of the arm "theoretical" 3- stage pipeline,
|
|
|
|
# we have to subtract 2 words (fetch/decode)
|
2015-06-10 11:10:46 +02:00
|
|
|
if(arg.is_a? Virtual::Block)
|
|
|
|
diff = arg.position - self.position - 8
|
|
|
|
else
|
|
|
|
# But, for methods, this happens to be the size of the object header,
|
|
|
|
# so there it balances out, but not blocks
|
|
|
|
# have to use the code, not the mthod object for methods
|
|
|
|
diff = arg.code.position - self.position
|
|
|
|
end
|
2015-05-29 11:33:40 +02:00
|
|
|
arg = diff
|
2014-08-30 18:40:37 +02:00
|
|
|
end
|
2015-05-29 11:33:40 +02:00
|
|
|
if (arg.is_a?(Numeric))
|
|
|
|
jmp_val = arg >> 2
|
2014-08-30 18:40:37 +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
|
2015-05-29 11:33:40 +02:00
|
|
|
raise "else not coded arg =\n#{arg.to_s[0..1000]}: #{inspect[0..1000]}"
|
2014-08-30 18:40:37 +02:00
|
|
|
end
|
|
|
|
io.write_uint8 op_bit_code | (COND_CODES[@attributes[:condition_code]] << 4)
|
|
|
|
when :swi
|
|
|
|
arg = @first
|
2015-05-29 11:33:40 +02:00
|
|
|
if (arg.is_a?(Numeric))
|
2015-05-29 11:47:49 +02:00
|
|
|
packed = [arg].pack('L')[0,3]
|
2014-08-30 18:40:37 +02:00
|
|
|
io << packed
|
|
|
|
io.write_uint8 0b1111 | (COND_CODES[@attributes[:condition_code]] << 4)
|
|
|
|
else
|
|
|
|
raise "invalid operand argument expected literal not #{arg} #{inspect}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise "Should not be the case #{inspect}"
|
|
|
|
end
|
|
|
|
end
|
2014-10-02 21:28:34 +02:00
|
|
|
|
|
|
|
def uses
|
|
|
|
if opcode == :call
|
|
|
|
@first.args.collect {|arg| arg.register }
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def assigns
|
|
|
|
if opcode == :call
|
|
|
|
[RegisterReference.new(RegisterMachine.instance.return_register)]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def to_s
|
2015-07-18 11:15:07 +02:00
|
|
|
"#{opcode} #{@first} #{super}"
|
2014-10-02 21:28:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|