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 # 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 # and to make using the apu function easier, there are functions that create registers as well
class Block < Code class Block < Code
extend Forwardable # forward block call back to program
def_delegator :@program, :block
def initialize(asm) def initialize(asm)
super() super()
@codes = [] @codes = []
@position = 0 @position = 0
@asm = asm @program = asm
end end
ArmMachine::REGISTERS.each do |reg , number| ArmMachine::REGISTERS.each do |reg , number|
@ -34,8 +36,8 @@ module Asm
elsif (arg.is_a?(Integer)) elsif (arg.is_a?(Integer))
arg_nodes << Asm::NumLiteral.new(arg) arg_nodes << Asm::NumLiteral.new(arg)
elsif (arg.is_a?(String)) elsif (arg.is_a?(String))
arg_nodes << @asm.add_string(arg) arg_nodes << @program.add_string(arg)
elsif (arg.is_a?(Asm::Label)) elsif (arg.is_a?(Asm::Block))
arg_nodes << arg arg_nodes << arg
else else
raise "Invalid argument #{arg.inspect} for instruction" 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. # 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. # So then one can create a block, branch to it and set it later.
def set! def set!
@asm.add_block self @program.add_block self
self self
end 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 def length
@codes.sum :length @codes.inject(0) {| sum , item | sum + item.length}
end end
def add_code(kode) def add_code(kode)

View File

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

View File

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