2014-04-20 17:59:21 +02:00
|
|
|
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
|
2014-04-21 10:02:15 +02:00
|
|
|
# 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)
|
2014-04-20 17:59:21 +02:00
|
|
|
|
|
|
|
class TestSmallProg < MiniTest::Test
|
|
|
|
# need a code generator, for arm
|
|
|
|
def setup
|
2014-05-08 13:14:15 +02:00
|
|
|
@program = Vm::Program.new "Arm"
|
2014-04-20 17:59:21 +02:00
|
|
|
end
|
|
|
|
|
2014-04-22 22:55:47 +02:00
|
|
|
def test_loop
|
2014-05-08 13:14:15 +02:00
|
|
|
@program.main.instance_eval do
|
2014-05-15 20:35:45 +02:00
|
|
|
mov :r0, right: 5 #1
|
2014-05-08 20:14:26 +02:00
|
|
|
start = Vm::Block.new("start")
|
|
|
|
add_code start
|
|
|
|
start.instance_eval do
|
2014-05-18 11:18:57 +02:00
|
|
|
sub :r0, :r0, 1 , :update_status => 1 #2
|
2014-05-15 18:41:51 +02:00
|
|
|
bne start ,{} #3
|
2014-04-25 20:08:19 +02:00
|
|
|
end
|
|
|
|
end
|
2014-05-08 20:14:26 +02:00
|
|
|
write( 6 , "loop" )
|
2014-04-21 10:02:15 +02:00
|
|
|
end
|
2014-04-22 22:55:47 +02:00
|
|
|
|
|
|
|
def test_hello
|
2014-05-13 16:06:42 +02:00
|
|
|
hello = Vm::StringConstant.new "Hello Raisa\n"
|
2014-05-08 13:14:15 +02:00
|
|
|
@program.add_object hello
|
|
|
|
@program.main.instance_eval do
|
2014-05-15 20:35:45 +02:00
|
|
|
mov :r7, right: 4 # 4 == write
|
|
|
|
mov :r0 , right: 1 # stdout
|
2014-05-18 11:18:57 +02:00
|
|
|
add :r1 , hello , nil # address of "hello Raisa"
|
2014-05-15 20:35:45 +02:00
|
|
|
mov :r2 , right: hello.length
|
2014-05-15 18:41:51 +02:00
|
|
|
swi 0 , {} #software interupt, ie kernel syscall
|
2014-04-25 20:08:19 +02:00
|
|
|
end
|
2014-05-14 09:47:30 +02:00
|
|
|
write(7 + hello.length/4 + 1 , 'hello')
|
2014-04-22 22:55:47 +02:00
|
|
|
end
|
|
|
|
|
2014-04-21 10:02:15 +02:00
|
|
|
#helper to write the file
|
|
|
|
def write len ,name
|
2014-04-23 12:57:34 +02:00
|
|
|
writer = Elf::ObjectWriter.new(Elf::Constants::TARGET_ARM)
|
2014-05-08 13:14:15 +02:00
|
|
|
io = @program.assemble(StringIO.new)
|
|
|
|
assembly = io.string
|
2014-04-21 10:02:15 +02:00
|
|
|
assert_equal len * 4 , assembly.length
|
2014-04-20 17:59:21 +02:00
|
|
|
writer.set_text assembly
|
2014-04-21 16:27:05 +02:00
|
|
|
writer.save("#{name}_test.o")
|
2014-04-20 17:59:21 +02:00
|
|
|
end
|
|
|
|
end
|
2014-04-22 22:55:47 +02:00
|
|
|
# _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
|