work on Integer.to_s , not a simple task as it turns out

This commit is contained in:
Torsten Ruger
2014-05-15 16:54:23 +03:00
parent 918ede1c02
commit b4c79d218f
12 changed files with 80 additions and 11 deletions

View File

@ -72,8 +72,57 @@ module Arm
syscall( block , 4 )
end
# make a string out of the integer.
# as we don't have memory manegement yet, you have to pass the string in (ouch)
# in a weird twist the string is actually a string, while we actually use its address.
def integer_to_s block , string
number = Vm::Integer.new(0)
tos = Vm::Block.new("integer_to_s") # need to create a block to jump to
block.add_code(tos) # and then use the new block to add code
#STMFD sp!, {r9, r10, lr} #function entry save working regs (for recursion)
tos.add_code( push :regs => [:lr ]) #and the return address.
# MOV r9, r1 # preserve arguments over following
# MOV r10, r2 # function calls
# pin data, ie no saving
remainder = Vm::Integer.new( number.register + 2)
# BL udiv10 # r1 = r1 / 10
div10( tos , number , remainder )
# ADD r10, r10, 48 #'0' # make char out of digit (by using ascii encoding)
tos.add_code( add( left: remainder , right: remainder , extra: 48 ))
#STRB r10, [r1], 1 # store digit at end of buffer
tos.add_code( strb( left: string , right: remainder )) #and increment TODO check
# CMP r1, #0 # quotient non-zero?
tos.add_code( cmp left: number , right: 0 )
#BLNE utoa # conditional recursive call to utoa
tos.add_code( callne( left: tos ))
#LDMFD sp!, {r9, r10, pc} # function exit - restore and return
tos.add_code( pop regs: [:pc])
end
private
# the number (a Vm::integer) is (itself) divided by 10, ie overwritten by the result
# and the remainder is overwritten (ie an out argument)
# not really a function, more a macro, hence private
def div10 block, number , remainder
# takes argument in r1
# returns quotient in r1, remainder in r2
# SUB r2, r1, #10 # keep (x-10) for later
block.add_code sub(:left => remainder , :right => number , :extra => 10 )
# SUB r1, r1, r1, lsr #2
# ADD r1, r1, r1, lsr #4
# ADD r1, r1, r1, lsr #8
# ADD r1, r1, r1, lsr #16
# MOV r1, r1, lsr #3
# ADD r3, r1, r1, asl #2
# SUBS r2, r2, r3, asl #1 # calc (x-10) - (x/10)*10
# ADDPL r1, r1, #1 # fix-up quotient
# ADDMI r2, r2, #10 # fix-up remainder
# MOV pc, lr
end
def syscall block , num
block.add_code mov( :left => :r7 , :right => num )
block.add_code swi( :left => 0 )

View File

@ -50,14 +50,15 @@ module Arm
raise "reference offset too large/small (max 4095) #{arg} #{inspect}"
end
end
elsif (arg.is_a?(Vm::StringConstant)) #use pc relative
elsif (arg.is_a?(Vm::StringConstant) ) #use pc relative
@rn = :pc
@operand = arg.position - self.position - 8 #stringtable is after code
@add_offset = 1
if (@operand.abs > 4095)
raise "reference offset too large/small (max 4095) #{arg} #{inspect}"
end
elsif (arg.is_a?(Arm::Label) or arg.is_a?(Vm::IntegerConstant))
elsif( arg.is_a?(Vm::IntegerConstant) )
raise "is this working ?? #{arg} #{inspect}"
@pre_post_index = 1
@rn = pc
@use_addrtable_reloc = true