improve argument handling for puntint

This commit is contained in:
Torsten Ruger 2014-05-19 12:18:01 +03:00
parent 8ba79d8de8
commit 76ce9aa654
2 changed files with 48 additions and 32 deletions

View File

@ -33,47 +33,45 @@ module Core
end end
def putint context , arg = Vm::Integer def putint context , arg = Vm::Integer
arg = Vm::Integer.new( 0 ) putint_function = Vm::Function.new(:putint , [arg] , arg )
function = Vm::Function.new(:putint , [arg ] , arg ) buffer = Vm::StringConstant.new(" ") # create a buffer
buffer = Vm::StringConstant.new(" ") context.program.add_object buffer # and save it (function local variable: a no no)
context.program.add_object buffer int = putint_function.args.first
str_addr = Vm::Integer.new(1) moved_int = Vm::Integer.new(1)
context.str_addr = str_addr utoa = context.program.get_or_create_function(:utoa)
reg1 = Vm::Integer.new(2) putint_function.body.instance_eval do
itos_fun = context.program.get_or_create_function(:utoa) mov( moved_int , int ) #move arg up
function.body.instance_eval do add( int , buffer ,nil ) # string to write to
mov( reg1 , str_addr ) #move arg up add( int , int , (buffer.length-3))
add( str_addr , buffer ,nil ) # string to write to call( utoa )
add( str_addr , str_addr , (buffer.length-3))
call( itos_fun )
# And now we "just" have to print it, using the write_stdout # And now we "just" have to print it, using the write_stdout
add( str_addr , buffer , nil ) # string to write to add( int , buffer , nil ) # string to write to
mov( reg1 , buffer.length ) mov( moved_int , buffer.length )
end end
ret = Vm::CMachine.instance.write_stdout(function.body) Vm::CMachine.instance.write_stdout(putint_function.body)
function putint_function
end end
# The conversion to base10 is quite a bit more complicated than i thought. The bulk of it is in div10 # The conversion to base10 is quite a bit more complicated than i thought. The bulk of it is in div10
# We set up variables, do the devision and write the result to the string # We set up variables, do the devision and write the result to the string
# then check if were done and recurse if neccessary # then check if were done and recurse if neccessary
# As we write before we recurse (save a push) we write the number backwards # As we write before we recurse (save a push) we write the number backwards
# arguments: string address , integer
def utoa context def utoa context
function = Vm::Function.new(:utoa , [Vm::Integer , Vm::Integer ] ) utoa_function = Vm::Function.new(:utoa , [Vm::Integer , Vm::Integer ] , Vm::Integer )
block = function.body str_addr = utoa_function.args[0]
str_addr = context.str_addr number = utoa_function.args[1]
number = Vm::Integer.new(str_addr.register + 1)
remainder = Vm::Integer.new( number.register + 1) remainder = Vm::Integer.new( number.register + 1)
Vm::CMachine.instance.div10( block , number , remainder ) Vm::CMachine.instance.div10( utoa_function.body , number , remainder )
# make char out of digit (by using ascii encoding) 48 == "0" # make char out of digit (by using ascii encoding) 48 == "0"
block.instance_eval do utoa_function.body.instance_eval do
add( remainder , remainder , 48 ) add( remainder , remainder , 48 )
strb( remainder, right: str_addr ) strb( remainder, right: str_addr )
sub( str_addr, str_addr , 1 ) sub( str_addr, str_addr , 1 )
cmp( number , 0 ) cmp( number , 0 )
callne( function ) callne( utoa_function )
end end
return function return utoa_function
end end
end end

View File

@ -22,8 +22,26 @@ module Vm
def initialize(name , args = [] , return_type = nil) def initialize(name , args = [] , return_type = nil)
super() super()
@name = name @name = name
@args = args @args = Array.new(args.length)
@return_type = return_type args.each_with_index do |arg , i|
if arg.is_a?(Value)
@args[i] = arg
raise "arg in non std register #{arg.inspect}" unless i == arg.register
else
@args[i] = arg.new(i)
end
end
@return_type = return_type || Vm::Integer
if @return_type.is_a?(Value)
raise "return in non std register #{@return_type.inspect}" unless 0 == @return_type.register
else
@return_type = @return_type.new(0)
end
@entry = Core::Kernel::function_entry( Vm::Block.new("#{name}_entry") ,name )
@exit = Core::Kernel::function_exit( Vm::Block.new("#{name}_exit") , name )
@body = Block.new("#{name}_body")
@reg_count = 0
branch_body
@entry = Core::Kernel::function_entry( Vm::Block.new("#{name}_entry") ,name ) @entry = Core::Kernel::function_entry( Vm::Block.new("#{name}_entry") ,name )
@exit = Core::Kernel::function_exit( Vm::Block.new("#{name}_exit") , name ) @exit = Core::Kernel::function_exit( Vm::Block.new("#{name}_exit") , name )
@body = Block.new("#{name}_body") @body = Block.new("#{name}_body")