fixing address resolution cleans up hello world

This commit is contained in:
Torsten Ruger 2014-04-22 23:55:47 +03:00
parent 7ff8fa8802
commit b240dc5100
5 changed files with 40 additions and 51 deletions

View File

@ -31,7 +31,7 @@ module Asm
@opcode = opcode.downcase.to_sym @opcode = opcode.downcase.to_sym
@args = args @args = args
end end
attr_reader :opcode, :args attr_reader :opcode, :args , :position
def affect_status def affect_status
@s @s
@ -55,7 +55,7 @@ module Asm
builder.cond = COND_CODES[@cond] builder.cond = COND_CODES[@cond]
builder.rd = reg_ref(args[0]) builder.rd = reg_ref(args[0])
builder.rn = reg_ref(args[1]) builder.rn = reg_ref(args[1])
builder.build_operand args[2] builder.build_operand args[2] , self.position
builder.assemble io, as builder.assemble io, as
when :cmn, :cmp, :teq, :tst when :cmn, :cmp, :teq, :tst
builder = NormalBuilder.new(OPC_DATA_PROCESSING, OPCODES[opcode], 1) builder = NormalBuilder.new(OPC_DATA_PROCESSING, OPCODES[opcode], 1)

View File

@ -43,7 +43,14 @@ module Asm
end end
# Build representation for source value # Build representation for source value
def build_operand(arg) def build_operand(arg , position = 0)
#position only needed for calculating relative addresses to data objects
#there is a design stink here which makes my head ache. But shanti shanti
if arg.is_a?(Asm::DataObject)
# do pc relative addressing with the difference to the instuction
# 8 is for the funny pipeline adjustment (ie oc pointing to fetch and not execute)
arg = Asm::NumLiteralNode.new( arg.position - position - 8 )
end
if (arg.is_a?(Asm::NumLiteralNode)) if (arg.is_a?(Asm::NumLiteralNode))
if (arg.value.fits_u8?) if (arg.value.fits_u8?)
# no shifting needed # no shifting needed
@ -82,10 +89,7 @@ module Asm
end end
@operand = rm_ref | (shift_op << 4) | (shift_imm << 4+3) @operand = rm_ref | (shift_op << 4) | (shift_imm << 4+3)
elsif arg.is_a?(Asm::DataObject)
# do pc relative addressing with the difference to the instuction
else else
puts "#{self.inspect}"
raise Asm::AssemblyError.new(Asm::ERRSTR_INVALID_ARG + " " + arg.inspect, arg) raise Asm::AssemblyError.new(Asm::ERRSTR_INVALID_ARG + " " + arg.inspect, arg)
end end
end end

View File

@ -4,6 +4,10 @@ module Asm
@data = data @data = data
end end
def position
throw "Not set" unless @address
@address
end
def at address def at address
@address = address @address = address
end end

View File

@ -1,42 +0,0 @@
require_relative 'helper'
#try to call an extern function
class TestExtern < MiniTest::Test
# need a code generator, for arm
def setup
@generator = Asm::Arm::ArmAssembler.new
end
def test_extern
hello = "Hello Raisa"+ "\n\x00"
@generator.instance_eval {
mov r7, 4 #4 == write
mov r0 , 1 #stdout
add r1 , pc , hello # address of "hello Raisa"
mov r2 , 12 # length of hello
swi 0
mov r7, 1 #1 == exit
swi 0
}
@generator.add_string(hello)
write(7 , 'label')
end
#helper to write the file
def write len ,name
writer = Asm::ObjectWriter.new(Elf::Constants::TARGET_ARM)
assembly = @generator.assemble_to_string
#assert_equal len * 4 , assembly.length
writer.set_text assembly
writer.save("#{name}_test.o")
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

View File

@ -12,7 +12,7 @@ class TestSmallProg < MiniTest::Test
@generator = Asm::Arm::ArmAssembler.new @generator = Asm::Arm::ArmAssembler.new
end end
def test_generate_small def test_loop
@generator.instance_eval { @generator.instance_eval {
mov r0, 5 #1 mov r0, 5 #1
loop_start = label! loop_start = label!
@ -21,7 +21,21 @@ class TestSmallProg < MiniTest::Test
mov r7, 1 #4 mov r7, 1 #4
swi 0 #5 5 instructions swi 0 #5 5 instructions
} }
write( 5 , "small" ) write( 5 , "loop" )
end
def test_hello
hello = "Hello Raisa"+ "\n\x00"
@generator.instance_eval {
mov r7, 4 # 4 == write
mov r0 , 1 # stdout
add r1 , pc , hello # address of "hello Raisa"
mov r2 , hello.length
swi 0 #software interupt, ie kernel syscall
mov r7, 1 # 1 == exit
swi 0
}
write(7 , 'label')
end end
#test dropped along with functionality, didn't work and not needed (yet?) TODO #test dropped along with functionality, didn't work and not needed (yet?) TODO
@ -51,3 +65,12 @@ class TestSmallProg < MiniTest::Test
writer.save("#{name}_test.o") writer.save("#{name}_test.o")
end 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