2016-12-30 10:59:38 +01:00
|
|
|
require_relative "../helper"
|
2016-12-14 12:43:13 +01:00
|
|
|
|
|
|
|
# try to test that the generation of basic instructions works
|
|
|
|
# one instruction at a time, reverse testing from objdump --demangle -Sfghxp
|
|
|
|
# tests are named as per assembler code, ie test_mov testing mov instruction
|
|
|
|
|
2017-01-03 21:37:55 +01:00
|
|
|
module Arm
|
2018-05-11 17:36:45 +02:00
|
|
|
class FakeBin
|
|
|
|
def byte_length
|
|
|
|
4
|
|
|
|
end
|
2018-05-24 20:20:56 +02:00
|
|
|
def is_a?(_)
|
|
|
|
true
|
|
|
|
end
|
2018-05-11 17:36:45 +02:00
|
|
|
end
|
2018-05-30 09:29:38 +02:00
|
|
|
class FakeInt
|
|
|
|
attr_reader :value
|
|
|
|
def initialize(val)
|
|
|
|
set_value(val)
|
|
|
|
end
|
|
|
|
def is_a?(clazz)
|
|
|
|
clazz == Parfait::Integer
|
|
|
|
end
|
|
|
|
def byte_length
|
|
|
|
4
|
|
|
|
end
|
|
|
|
def set_value(val)
|
|
|
|
@value = val
|
|
|
|
end
|
|
|
|
end
|
2017-01-03 21:37:55 +01:00
|
|
|
module ArmHelper
|
|
|
|
def setup
|
|
|
|
@machine = Arm::ArmMachine
|
2018-05-11 17:36:45 +02:00
|
|
|
@binary = FakeBin.new
|
2018-05-24 20:20:56 +02:00
|
|
|
Risc::Position.clear_positions
|
2018-05-11 17:36:45 +02:00
|
|
|
Risc::Position.set(@binary , 0)
|
2017-01-03 21:37:55 +01:00
|
|
|
end
|
2016-12-14 12:43:13 +01:00
|
|
|
|
2017-01-03 21:37:55 +01:00
|
|
|
# code is what the generator spits out, at least one instruction worth (.first)
|
|
|
|
# the op code is wat was witten as assembler in the first place and the binary result
|
2018-04-03 13:46:07 +02:00
|
|
|
def assert_code( code , op , should )
|
2017-01-03 21:37:55 +01:00
|
|
|
assert_equal op , code.opcode
|
|
|
|
io = StringIO.new
|
|
|
|
code.assemble(io)
|
|
|
|
binary = io.string
|
|
|
|
assert_equal should.length , binary.length , "code length wrong for #{code.inspect}"
|
|
|
|
index = 0
|
|
|
|
binary.each_byte do |byte |
|
|
|
|
assert_equal should[index] , byte , "byte #{index} 0x#{should[index].to_s(16)} != 0x#{byte.to_s(16)}"
|
|
|
|
index += 1
|
|
|
|
end
|
2016-12-14 12:43:13 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|