putint works, work on syntax goes on

This commit is contained in:
Torsten Ruger 2014-05-19 15:44:12 +03:00
parent 76ce9aa654
commit 285988f173
4 changed files with 25 additions and 24 deletions

View File

@ -35,13 +35,6 @@ module Arm
to to
end end
def string_load block , str_lit , reg
block << add( "r#{reg}".to_sym , str_lit , nil ) #right is pc, implicit
#second arg is a hack to get the stringlength without coding
block << mov( "r#{reg+1}".to_sym , str_lit.length )
str_lit
end
def function_call into , call def function_call into , call
raise "Not CallSite #{call.inspect}" unless call.is_a? Vm::CallSite raise "Not CallSite #{call.inspect}" unless call.is_a? Vm::CallSite
raise "Not linked #{call.inspect}" unless call.function raise "Not linked #{call.inspect}" unless call.function

View File

@ -39,15 +39,16 @@ module Core
int = putint_function.args.first int = putint_function.args.first
moved_int = Vm::Integer.new(1) moved_int = Vm::Integer.new(1)
utoa = context.program.get_or_create_function(:utoa) utoa = context.program.get_or_create_function(:utoa)
putint_function.body.instance_eval do b = putint_function.body
mov( moved_int , int ) #move arg up b.mov( moved_int , int ) #move arg up
add( int , buffer ,nil ) # string to write to #b.a buffer => int # string to write to
add( int , int , (buffer.length-3))
call( utoa ) b.add( int , buffer ,nil ) # string to write to
b.a int + (buffer.length-3) => int
b.call( utoa )
# 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( int , buffer , nil ) # string to write to b.add( int , buffer , nil ) # string to write to
mov( moved_int , buffer.length ) b.mov( moved_int , buffer.length )
end
Vm::CMachine.instance.write_stdout(putint_function.body) Vm::CMachine.instance.write_stdout(putint_function.body)
putint_function putint_function
end end
@ -64,13 +65,12 @@ module Core
remainder = Vm::Integer.new( number.register + 1) remainder = Vm::Integer.new( number.register + 1)
Vm::CMachine.instance.div10( utoa_function.body , 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"
utoa_function.body.instance_eval do b = utoa_function.body
add( remainder , remainder , 48 ) b.a remainder + 48 => remainder
strb( remainder, right: str_addr ) b.strb( remainder, right: str_addr )
sub( str_addr, str_addr , 1 ) b.sub( str_addr, str_addr , 1 )
cmp( number , 0 ) b.cmp( number , 0 )
callne( utoa_function ) b.callne( utoa_function )
end
return utoa_function return utoa_function
end end
end end

View File

@ -34,12 +34,19 @@ module Vm
end end
def add_code(kode) def add_code(kode)
if kode.is_a? Hash
raise "Hack only for 1 element #{inspect} #{kode.inspect}" unless kode.length == 1
instruction , result = kode.first
instruction.result = result
kode = instruction
end
raise "alarm #{kode}" if kode.is_a? Word raise "alarm #{kode}" if kode.is_a? Word
raise "alarm #{kode}" unless kode.is_a? Code raise "alarm #{kode}" unless kode.is_a? Code
@codes << kode @codes << kode
self self
end end
alias :<< :add_code alias :<< :add_code
alias :a :add_code
def link_at pos , context def link_at pos , context
@position = pos @position = pos
@ -67,6 +74,7 @@ module Vm
# sugar to create instructions easily. Any method with one arg is sent to the machine and the result # sugar to create instructions easily. Any method with one arg is sent to the machine and the result
# (hopefully an instruction) added as code # (hopefully an instruction) added as code
def method_missing(meth, *args, &block) def method_missing(meth, *args, &block)
raise "hallo" if( meth.to_s[-1] == "=")
add_code CMachine.instance.send(meth , *args) add_code CMachine.instance.send(meth , *args)
end end

View File

@ -33,8 +33,8 @@ module Vm
@string = str + "\x00" * pad @string = str + "\x00" * pad
end end
def load reg_num def result= value
Machine.instance.string_load self , reg_num class_for(MoveInstruction).new(value , self , :opcode => :mov)
end end
# the strings length plus padding # the strings length plus padding