From 53666e8b33537ed9d50efa81f4e8fd8bdb824e67 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 16 May 2014 23:33:25 +0300 Subject: [PATCH] bit of evaling --- lib/arm/arm_machine.rb | 32 ++++++++++++++++++-------------- lib/core/kernel.rb | 28 ++++++++++++++++------------ 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/lib/arm/arm_machine.rb b/lib/arm/arm_machine.rb index 8308eabf..43bf1079 100644 --- a/lib/arm/arm_machine.rb +++ b/lib/arm/arm_machine.rb @@ -65,9 +65,11 @@ module Arm # assumes string in r0 and r1 and moves them along for the syscall def write_stdout block - block.add_code mov( :r2 , right: :r1 ) - block.add_code mov( :r1 , right: :r0 ) - block.add_code mov( :r0 , right: 1 ) # 1 == stdout + block.instance_eval do + mov( :r2 , right: :r1 ) + mov( :r1 , right: :r0 ) + mov( :r0 , right: 1 ) # 1 == stdout + end syscall( block , 4 ) end @@ -79,17 +81,19 @@ module Arm # Note about division: devision is MUCH more expensive than one would have thought # And coding it is a bit of a mind leap: it's all about finding a a result that gets the # remainder smaller than an int. i'll post some links sometime. This is from the arm manual - block.add_code sub( remainder , left: number , right: 10 ) - block.add_code sub( number , left: number , right: number , shift_lsr: 2) - block.add_code add( number , left: number , right: number , shift_lsr: 4) - block.add_code add( number , left: number , right: number , shift_lsr: 8) - block.add_code add( number , left: number , right: number , shift_lsr: 16) - block.add_code mov( number , right: number , shift_lsr: 3) - tmp = Vm::Integer.new( remainder.register + 1) - block.add_code add( tmp , left: number , right: number , shift_lsl: 2) - block.add_code sub( remainder , left: remainder , right: tmp , shift_lsl: 1 , update_status: 1) - block.add_code add( number , left: number, right: 1 , condition_code: :pl ) - block.add_code add( remainder , left: remainder , right: 10 , condition_code: :mi ) + block.instance_eval do + sub( remainder , left: number , right: 10 ) + sub( number , left: number , right: number , shift_lsr: 2) + add( number , left: number , right: number , shift_lsr: 4) + add( number , left: number , right: number , shift_lsr: 8) + add( number , left: number , right: number , shift_lsr: 16) + mov( number , right: number , shift_lsr: 3) + tmp = Vm::Integer.new( remainder.register + 1) + add( tmp , left: number , right: number , shift_lsl: 2) + sub( remainder , left: remainder , right: tmp , shift_lsl: 1 , update_status: 1) + add( number , left: number, right: 1 , condition_code: :pl ) + add( remainder , left: remainder , right: 10 , condition_code: :mi ) + end end def syscall block , num diff --git a/lib/core/kernel.rb b/lib/core/kernel.rb index d4e896c6..816ec624 100644 --- a/lib/core/kernel.rb +++ b/lib/core/kernel.rb @@ -38,16 +38,18 @@ module Core buffer = Vm::StringConstant.new(" ") context.program.add_object buffer str_addr = Vm::Integer.new(0) # address of the - reg1 = Vm::Integer.new(1) - block.add_code Vm::CMachine.instance.mov( reg1 , right: str_addr ) #move arg up - block.add_code Vm::CMachine.instance.add( str_addr , left: buffer ) # string to write to - block.add_code Vm::CMachine.instance.add( str_addr , left: str_addr , right: (buffer.length-3)) # string to write to context.str_addr = str_addr + reg1 = Vm::Integer.new(1) itos_fun = context.program.get_or_create_function(:utoa) - block.add_code Vm::CMachine.instance.call( itos_fun , {}) + block.instance_eval do + mov( reg1 , right: str_addr ) #move arg up + add( str_addr , left: buffer ) # string to write to + add( str_addr , left: str_addr , right: (buffer.length-3)) + call( itos_fun , {}) # And now we "just" have to print it, using the write_stdout - block.add_code Vm::CMachine.instance.add( str_addr , left: buffer ) # string to write to - block.add_code Vm::CMachine.instance.mov( reg1 , right: buffer.length ) + add( str_addr , left: buffer ) # string to write to + mov( reg1 , right: buffer.length ) + end ret = Vm::CMachine.instance.write_stdout(block) function end @@ -64,11 +66,13 @@ module Core remainder = Vm::Integer.new( number.register + 1) Vm::CMachine.instance.div10( block , number , remainder ) # make char out of digit (by using ascii encoding) 48 == "0" - block.add_code Vm::CMachine.instance.add( remainder , left: remainder , right: 48 ) - block.add_code Vm::CMachine.instance.strb( remainder, right: str_addr ) - block.add_code Vm::CMachine.instance.sub( str_addr, left: str_addr , right: 1 ) - block.add_code Vm::CMachine.instance.cmp( number , right: 0 ) - block.add_code Vm::CMachine.instance.callne( function , {} ) + block.instance_eval do + add( remainder , left: remainder , right: 48 ) + strb( remainder, right: str_addr ) + sub( str_addr, left: str_addr , right: 1 ) + cmp( number , right: 0 ) + callne( function , {} ) + end return function end end