better testing for small assembly (for coming changes)
This commit is contained in:
parent
0fa47d204e
commit
fcf76eed34
@ -14,6 +14,12 @@ module Elf
|
|||||||
Elf::Constants::SHF_WRITE | Elf::Constants::SHF_ALLOC | Elf::Constants::SHF_EXECINSTR
|
Elf::Constants::SHF_WRITE | Elf::Constants::SHF_ALLOC | Elf::Constants::SHF_EXECINSTR
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def length
|
||||||
|
@text.length
|
||||||
|
end
|
||||||
|
def to_s
|
||||||
|
"[" + @text.bytes.join(",") + "]"
|
||||||
|
end
|
||||||
def alignment
|
def alignment
|
||||||
4
|
4
|
||||||
end
|
end
|
||||||
|
@ -63,6 +63,9 @@ module Vm
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def at where
|
||||||
|
@current = where
|
||||||
|
end
|
||||||
# set the next executed block after self.
|
# set the next executed block after self.
|
||||||
# why is this useful? if it's unconditional, why not merge them:
|
# why is this useful? if it's unconditional, why not merge them:
|
||||||
# So the second block can be used as a jump target. You standard loop needs a block to setup
|
# So the second block can be used as a jump target. You standard loop needs a block to setup
|
||||||
@ -74,7 +77,10 @@ module Vm
|
|||||||
# sugar to create instructions easily. Any method with one arg is sent to the machine and the result
|
# sugar to create instructions easily. Any method with one arg is sent to the machine and the result
|
||||||
# (hopefully an instruction) added as code
|
# (hopefully an instruction) added as code
|
||||||
def method_missing(meth, *args, &block)
|
def method_missing(meth, *args, &block)
|
||||||
raise "hallo" if( meth.to_s[-1] == "=")
|
if( meth.to_s[-1] == "=")
|
||||||
|
val = @current.eval meth.to_s[0 ... -1]
|
||||||
|
raise "hallo #{val}"
|
||||||
|
end
|
||||||
add_code CMachine.instance.send(meth , *args)
|
add_code CMachine.instance.send(meth , *args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@ class TestFibo < MiniTest::Test
|
|||||||
fibo = @program.get_or_create_function(:fibo)
|
fibo = @program.get_or_create_function(:fibo)
|
||||||
@program.main.mov( int , 10 )
|
@program.main.mov( int , 10 )
|
||||||
@program.main.call( fibo )
|
@program.main.call( fibo )
|
||||||
putint = @program.get_or_create_function(:putint)
|
# putint = @program.get_or_create_function(:putint)
|
||||||
@program.main.call( putint )
|
# @program.main.call( putint )
|
||||||
write 20 , "fibo"
|
write 20 , "fibo"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ class TestSmallProg < MiniTest::Test
|
|||||||
bne start ,{} #3
|
bne start ,{} #3
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@should = [0,176,160,227,5,0,160,227,1,0,80,226,253,255,255,26,1,112,160,227,0,0,0,239]
|
||||||
write( 6 , "loop" )
|
write( 6 , "loop" )
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -35,17 +36,18 @@ class TestSmallProg < MiniTest::Test
|
|||||||
mov :r2 , hello.length
|
mov :r2 , hello.length
|
||||||
swi 0 #software interupt, ie kernel syscall
|
swi 0 #software interupt, ie kernel syscall
|
||||||
end
|
end
|
||||||
write(7 + hello.length/4 + 1 , 'hello')
|
@should = [0,176,160,227,4,112,160,227,1,0,160,227,12,16,143,226,16,32,160,227,0,0,0,239,1,112,160,227,0,0,0,239,72,101,108,108,111,32,82,97,105,115,97,10,0,0,0,0]
|
||||||
|
write(7 + hello.length/4 + 1 , 'hello')
|
||||||
end
|
end
|
||||||
|
|
||||||
#helper to write the file
|
#helper to write the file
|
||||||
def write len ,name
|
def write len ,name
|
||||||
writer = Elf::ObjectWriter.new(Elf::Constants::TARGET_ARM)
|
writer = Elf::ObjectWriter.new(@program , Elf::Constants::TARGET_ARM)
|
||||||
io = @program.assemble(StringIO.new)
|
assembly = writer.text
|
||||||
assembly = io.string
|
assembly.text.bytes.each_with_index do |byte , index|
|
||||||
assert_equal len * 4 , assembly.length
|
assert_equal byte , @should[index] , "byte #{index}"
|
||||||
writer.set_text assembly
|
end
|
||||||
writer.save("#{name}_test.o")
|
# writer.save("#{name}_test.o")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# _start copied from dietc
|
# _start copied from dietc
|
||||||
|
Loading…
Reference in New Issue
Block a user