fix the tests with new block syntax

This commit is contained in:
Torsten Ruger 2014-04-25 21:08:19 +03:00
parent 92beb638de
commit 88ed97ac3b
3 changed files with 36 additions and 26 deletions

View File

@ -14,12 +14,14 @@ module Asm
# Blocks are also used to create instructions, and so Block has functions for every cpu instruction
# and to make using the apu function easier, there are functions that create registers as well
class Block < Code
extend Forwardable # forward block call back to program
def_delegator :@program, :block
def initialize(asm)
super()
@codes = []
@position = 0
@asm = asm
@program = asm
end
ArmMachine::REGISTERS.each do |reg , number|
@ -34,8 +36,8 @@ module Asm
elsif (arg.is_a?(Integer))
arg_nodes << Asm::NumLiteral.new(arg)
elsif (arg.is_a?(String))
arg_nodes << @asm.add_string(arg)
elsif (arg.is_a?(Asm::Label))
arg_nodes << @program.add_string(arg)
elsif (arg.is_a?(Asm::Block))
arg_nodes << arg
else
raise "Invalid argument #{arg.inspect} for instruction"
@ -86,13 +88,14 @@ module Asm
# For backwards jumps, positions of blocks are known at creation, but for forward off course not.
# So then one can create a block, branch to it and set it later.
def set!
@asm.add_block self
@program.add_block self
self
end
# Label has no length , 0
# length of the codes. In arm it would be the length * 4
# (strings are stored globally in the Program)
def length
@codes.sum :length
@codes.inject(0) {| sum , item | sum + item.length}
end
def add_code(kode)

View File

@ -6,7 +6,7 @@ module Asm
# Currently string are stored "inline" , ie in the code segment.
# Mainly because that works an i aint no elf expert.
class StringLiteral
class StringLiteral < Code
# currently aligned to 4 (ie padded with 0) and off course 0 at the end
def initialize(str)

View File

@ -9,24 +9,30 @@ require_relative 'helper'
class TestSmallProg < MiniTest::Test
# need a code generator, for arm
def setup
@generator = Asm::ArmAssembler.new
@program = Asm::Program.new
end
def test_loop
@generator.instance_eval {
@program.block do |main|
main.instance_eval do
mov r0, 5 #1
start = label!(:loop_start)
main.block do |start|
start.instance_eval do
subs r0, r0, 1 #2
bne start #3
end
mov r7, 1 #4
swi 0 #5 5 instructions
}
end
end
end
write( 5 , "loop" )
end
def test_hello
hello = "Hello Raisa\n"
@generator.instance_eval {
@program.block do |main|
main.instance_eval do
mov r7, 4 # 4 == write
mov r0 , 1 # stdout
add r1 , pc , hello # address of "hello Raisa"
@ -34,14 +40,15 @@ class TestSmallProg < MiniTest::Test
swi 0 #software interupt, ie kernel syscall
mov r7, 1 # 1 == exit
swi 0
}
end
end
write(7 + hello.length/4 + 1 , 'hello')
end
#helper to write the file
def write len ,name
writer = Elf::ObjectWriter.new(Elf::Constants::TARGET_ARM)
assembly = @generator.assemble_to_string
assembly = @program.assemble_to_string
assert_equal len * 4 , assembly.length
writer.set_text assembly
writer.save("#{name}_test.o")