diff --git a/lib/arm/arm_machine.rb b/lib/arm/arm_machine.rb index 6da3276d..9f54c7c6 100644 --- a/lib/arm/arm_machine.rb +++ b/lib/arm/arm_machine.rb @@ -35,13 +35,6 @@ module Arm to 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 raise "Not CallSite #{call.inspect}" unless call.is_a? Vm::CallSite raise "Not linked #{call.inspect}" unless call.function diff --git a/lib/core/kernel.rb b/lib/core/kernel.rb index 49fa097e..948ce326 100644 --- a/lib/core/kernel.rb +++ b/lib/core/kernel.rb @@ -39,15 +39,16 @@ module Core int = putint_function.args.first moved_int = Vm::Integer.new(1) utoa = context.program.get_or_create_function(:utoa) - putint_function.body.instance_eval do - mov( moved_int , int ) #move arg up - add( int , buffer ,nil ) # string to write to - add( int , int , (buffer.length-3)) - call( utoa ) + b = putint_function.body + b.mov( moved_int , int ) #move arg up + #b.a buffer => int # string to write to + + 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 - add( int , buffer , nil ) # string to write to - mov( moved_int , buffer.length ) - end + b.add( int , buffer , nil ) # string to write to + b.mov( moved_int , buffer.length ) Vm::CMachine.instance.write_stdout(putint_function.body) putint_function end @@ -64,13 +65,12 @@ module Core remainder = Vm::Integer.new( number.register + 1) Vm::CMachine.instance.div10( utoa_function.body , number , remainder ) # make char out of digit (by using ascii encoding) 48 == "0" - utoa_function.body.instance_eval do - add( remainder , remainder , 48 ) - strb( remainder, right: str_addr ) - sub( str_addr, str_addr , 1 ) - cmp( number , 0 ) - callne( utoa_function ) - end + b = utoa_function.body + b.a remainder + 48 => remainder + b.strb( remainder, right: str_addr ) + b.sub( str_addr, str_addr , 1 ) + b.cmp( number , 0 ) + b.callne( utoa_function ) return utoa_function end end diff --git a/lib/vm/block.rb b/lib/vm/block.rb index d59024e6..74e7459a 100644 --- a/lib/vm/block.rb +++ b/lib/vm/block.rb @@ -34,12 +34,19 @@ module Vm end 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}" unless kode.is_a? Code @codes << kode self end alias :<< :add_code + alias :a :add_code def link_at pos , context @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 # (hopefully an instruction) added as code def method_missing(meth, *args, &block) + raise "hallo" if( meth.to_s[-1] == "=") add_code CMachine.instance.send(meth , *args) end diff --git a/lib/vm/constants.rb b/lib/vm/constants.rb index 69f6a283..1e3d2e02 100644 --- a/lib/vm/constants.rb +++ b/lib/vm/constants.rb @@ -33,8 +33,8 @@ module Vm @string = str + "\x00" * pad end - def load reg_num - Machine.instance.string_load self , reg_num + def result= value + class_for(MoveInstruction).new(value , self , :opcode => :mov) end # the strings length plus padding