rubyx/test/arm/test_small_program.rb

83 lines
3.9 KiB
Ruby
Raw Normal View History

require_relative 'helper'
2014-04-20 22:47:44 +02:00
# test the generation of a whole program
# not many asserts, but assume all is well (ho ho)
# linking and running does not produce seqmentation fault, ie it works
# should really thiink about more asserts (but currently no way to execute as still running on mac )
# (have to fix the int being used in code generation as ruby int is only 31 bits, and that wont do)
class TestSmallProg < MiniTest::Test
# need a code generator, for arm
def setup
@program = Vm::Program.new "Arm"
end
def test_loop
s = Vm::Block.new("start",nil).scope binding
2014-05-20 10:03:18 +02:00
m = @program.main.scope binding
r0 = Vm::Integer.new(0)
m.r0 = 5 #1
m << s
2014-05-21 11:42:54 +02:00
s.r0 = (r0 - 1).set_update_status #2
s.bne s #3
@should = [0x0,0xb0,0xa0,0xe3,0x5,0x0,0xa0,0xe3,0x1,0x0,0x50,0xe2,0xfd,0xff,0xff,0x1a,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef]
2014-05-20 10:03:18 +02:00
write "loop"
end
def test_hello
2014-05-13 16:06:42 +02:00
hello = Vm::StringConstant.new "Hello Raisa\n"
@program.add_object hello
# these are only here because it's a test program, usually all coding happens with values
r0 = Vm::Integer.new(0)
r1 = Vm::Integer.new(1)
r2 = Vm::Integer.new(2)
r7 = Vm::Integer.new(7)
b = @program.main.scope binding
b.r7 = 4 # 4 == write
b.r0 = 1 # stdout
b.r1 = hello # address of "hello Raisa"
b.r2 = hello.length
b.swi 0 #software interupt, ie kernel syscall
@should = [0x0,0xb0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x10,0x10,0x8f,0xe2,0x10,0x20,0xa0,0xe3,0x0,0x0,0x0,0xef,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x48,0x65,0x6c,0x6c,0x6f,0x20,0x52,0x61,0x69,0x73,0x61,0xa,0x0,0x0,0x0,0x0]
write "hello"
end
# a hand coded version of the fibonachi numbers (moved to kernel to be able to call it)
# not my hand off course, found in the net from a basic introduction
def test_fibo
int = Vm::Integer.new(1)
fibo = @program.get_or_create_function(:fibo)
main = @program.main.scope binding
main.int = 10
ret = main.call( fibo )
main.mov( :r1 , :r7 )
putint = @program.get_or_create_function(:putint)
@program.main.call( putint )
@should = [0x0,0xb0,0xa0,0xe3,0xa,0x10,0xa0,0xe3,0x4,0x0,0x0,0xeb,0x7,0x10,0xa0,0xe1,0x22,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0x1,0x0,0x51,0xe3,0x1,0x70,0xa0,0xd1,0xe,0xf0,0xa0,0xd1,0x1c,0x40,0x2d,0xe9,0x1,0x30,0xa0,0xe3,0x0,0x40,0xa0,0xe3,0x2,0x20,0x41,0xe2,0x4,0x30,0x83,0xe0,0x4,0x40,0x43,0xe0,0x1,0x20,0x52,0xe2,0xfb,0xff,0xff,0x5a,0x3,0x70,0xa0,0xe1,0x1c,0x80,0xbd,0xe8,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0xa,0x30,0x42,0xe2,0x22,0x21,0x42,0xe0,0x22,0x22,0x82,0xe0,0x22,0x24,0x82,0xe0,0x22,0x28,0x82,0xe0,0xa2,0x21,0xa0,0xe1,0x2,0x41,0x82,0xe0,0x84,0x30,0x53,0xe0,0x1,0x20,0x82,0x52,0xa,0x30,0x83,0x42,0x30,0x30,0x83,0xe2,0x0,0x30,0xc1,0xe5,0x1,0x10,0x41,0xe2,0x0,0x0,0x52,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0]
write "fibo"
end
#helper to write the file
def write name
writer = Elf::ObjectWriter.new(@program , Elf::Constants::TARGET_ARM)
assembly = writer.text
# use this for getting the bytes to compare to :
puts assembly
writer.save("#{name}_test.o")
assembly.text.bytes.each_with_index do |byte , index|
is = @should[index]
assert_equal is.class, Fixnum
assert_equal byte , is , "@#{index.to_s(16)} #{byte.to_s(16)} != #{is.to_s(16)}"
end
end
end
# _start copied from dietc
# mov fp, #0 @ clear the frame pointer
# ldr a1, [sp] @ argc
# add a2, sp, #4 @ argv
# ldr ip, .L3
# add a3, a2, a1, lsl #2 @ &argv[argc]
# add a3, a3, #4 @ envp
# str a3, [ip, #0] @ environ = envp
# bl main