refactor call_instruction
This commit is contained in:
parent
bf4ddd16ee
commit
56bf875f36
@ -34,59 +34,50 @@ module Arm
|
|||||||
def assemble(io)
|
def assemble(io)
|
||||||
case @attributes[:opcode]
|
case @attributes[:opcode]
|
||||||
when :b, :call
|
when :b, :call
|
||||||
arg = @first
|
handle_call(io)
|
||||||
if arg.is_a?(Register::Label) or arg.is_a?(Parfait::TypedMethod)
|
|
||||||
#relative addressing for jumps/calls
|
|
||||||
# but because of the arm "theoretical" 3- stage pipeline,
|
|
||||||
# we have to subtract 2 words (fetch/decode)
|
|
||||||
if(arg.is_a? Register::Label)
|
|
||||||
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.binary.position - self.position
|
|
||||||
end
|
|
||||||
arg = diff
|
|
||||||
end
|
|
||||||
if (arg.is_a?(Numeric))
|
|
||||||
jmp_val = arg >> 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
|
|
||||||
raise "else not Attributed arg =\n#{arg.to_s[0..1000]}: #{inspect[0..1000]}"
|
|
||||||
end
|
|
||||||
io.write_uint8 op_bit_code | (COND_CODES[@attributes[:condition_code]] << 4)
|
|
||||||
when :swi
|
when :swi
|
||||||
arg = @first
|
handle_swi(io)
|
||||||
if (arg.is_a?(Numeric))
|
|
||||||
packed = [arg].pack('L')[0,3]
|
|
||||||
io << packed
|
|
||||||
io.write_uint8 0b1111 | (COND_CODES[@attributes[:condition_code]] << 4)
|
|
||||||
else
|
|
||||||
raise "invalid operand argument expected literal not #{arg} #{inspect}"
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
raise "Should not be the case #{inspect}"
|
raise "Should not be the case #{inspect}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def uses
|
def handle_call(io)
|
||||||
if opcode == :call
|
case @first
|
||||||
@first.args.collect {|arg| arg.register }
|
when Register::Label
|
||||||
|
# relative addressing for jumps/calls
|
||||||
|
# but because of the arm "theoretical" 3- stage pipeline,
|
||||||
|
# we have to subtract 2 words (fetch/decode)
|
||||||
|
arg = arg.position - self.position - 8
|
||||||
|
when Parfait::TypedMethod
|
||||||
|
# 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
|
||||||
|
arg = arg.binary.position - self.position
|
||||||
else
|
else
|
||||||
[]
|
arg = @first
|
||||||
end
|
end
|
||||||
|
write_call(arg, io)
|
||||||
end
|
end
|
||||||
def assigns
|
|
||||||
if opcode == :call
|
def write_call(arg,io)
|
||||||
[RegisterValue.new(RegisterMachine.instance.return_register)]
|
raise "else not Attributed arg =\n#{arg.to_s[0..1000]}: #{inspect[0..1000]}" unless (arg.is_a?(Numeric))
|
||||||
else
|
jmp_val = arg >> 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]
|
||||||
|
io.write_uint8 op_bit_code | (COND_CODES[@attributes[:condition_code]] << 4)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_swi(io)
|
||||||
|
arg = @first
|
||||||
|
raise "expected literal not #{arg} #{inspect}" unless (arg.is_a?(Numeric))
|
||||||
|
packed = [arg].pack('L')[0,3]
|
||||||
|
io << packed
|
||||||
|
io.write_uint8 0b1111 | (COND_CODES[@attributes[:condition_code]] << 4)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
"#{opcode} #{@first} #{super}"
|
"#{opcode} #{@first} #{super}"
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user