diff --git a/lib/parfait/data_object.rb b/lib/parfait/data_object.rb index 8c61c6a5..3f8b76ae 100644 --- a/lib/parfait/data_object.rb +++ b/lib/parfait/data_object.rb @@ -22,9 +22,6 @@ module Parfait class DataObject < Object - def self.integer_index - type_length - end def self.type_length raise "called #{self}" end diff --git a/lib/parfait/integer.rb b/lib/parfait/integer.rb index 7c1a9a31..5d660d9b 100644 --- a/lib/parfait/integer.rb +++ b/lib/parfait/integer.rb @@ -22,6 +22,11 @@ module Parfait def self.type_length 2 # 0 type, 1 next_i end + # index at which the next integer reference is. Used in risc reneration + def self.next_index + 1 + end + # index at which the actual integer is. Used in risc reneration def self.integer_index type_length end diff --git a/lib/risc/builtin/integer.rb b/lib/risc/builtin/integer.rb index 41e6a020..eba3fc37 100644 --- a/lib/risc/builtin/integer.rb +++ b/lib/risc/builtin/integer.rb @@ -1,25 +1,28 @@ module Risc module Builtin # integer related kernel functions - # all these functions (return the functione they implement) assume interger input + # all these functions (return the function they implement) assume interger input + # Also the returned integer object has to be passed in to avoid having to allocate it. # - # This means they will have to be renamed at some point and wrapped + # This means the methods will have to be renamed at some point and wrapped module Integer module ClassMethods include CompileHelper - # div by 4, ie shift two left + # div by 4, ie shift right by 2 # Mostly created for testing at this point, as it is short # return new int with result def div4(context) compiler = compiler_for(:Integer,:div4 ,{}) - compiler.builder(compiler.source).build do - integer! << message[:receiver] - integer.reduce_int - integer_reg! << 2 - integer.op :>> , integer_reg - add_new_int("div4", integer , integer_reg) - message[:return_value] << integer_reg + builder = compiler.builder(compiler.source) + integer_tmp = builder.allocate_int + builder.build do + integer_self! << message[:receiver] + integer_self.reduce_int + integer_1! << 2 + integer_self.op :>> , integer_1 + integer_tmp[Parfait::Integer.integer_index] << integer_self + message[:return_value] << integer_tmp end compiler.add_mom( Mom::ReturnSequence.new) return compiler @@ -49,13 +52,13 @@ module Risc # - load true or false object into return, depending on check # - return def comparison( operator ) - compiler = compiler_for(:Integer, operator ,{other: :Integer}) + compiler = compiler_for(:Integer, operator ,{other: :Integer }) builder = compiler.builder(compiler.source) builder.build do integer! << message[:receiver] - integer_reg! << message[:arguments] - integer_reg << integer_reg[ 1] integer.reduce_int + integer_reg! << message[:arguments] + integer_reg << integer_reg[Parfait::NamedList.type_length + 0] #"other" is at index 0 integer_reg.reduce_int swap_names(:integer , :integer_reg) if(operator.to_s.start_with?('<') ) integer.op :- , integer_reg @@ -86,17 +89,18 @@ module Risc # - gets a new integer and stores the result # - returns the new int def operator_method( op_sym ) - compiler = compiler_for(:Integer, op_sym ,{other: :Integer}) + compiler = compiler_for(:Integer, op_sym ,{other: :Integer }) builder = compiler.builder(compiler.source) + integer_tmp = builder.allocate_int builder.build do integer! << message[:receiver] - integer_reg! << message[:arguments] - integer_reg << integer_reg[ 1] integer.reduce_int + integer_reg! << message[:arguments] + integer_reg << integer_reg[Parfait::NamedList.type_length + 0] #"other" is at index 0 integer_reg.reduce_int integer.op op_sym , integer_reg - add_new_int op_sym.to_s , integer , integer_reg - message[:return_value] << integer_reg + integer_tmp[Parfait::Integer.integer_index] << integer + message[:return_value] << integer_tmp end compiler.add_mom( Mom::ReturnSequence.new) return compiler @@ -114,56 +118,57 @@ module Risc s = "div_10 " compiler = compiler_for(:Integer,:div10 ,{}) builder = compiler.builder(compiler.source) + integer_tmp = builder.allocate_int builder.build do integer_self! << message[:receiver] integer_self.reduce_int - integer_tmp! << integer_self + integer_1! << integer_self integer_reg! << integer_self integer_const! << 1 - integer_tmp.op :>> , integer_const + integer_1.op :>> , integer_const integer_const << 2 integer_reg.op :>> , integer_const - integer_reg.op :+ , integer_tmp + integer_reg.op :+ , integer_1 integer_const << 4 - integer_tmp << integer_reg - integer_reg.op :>> , integer_tmp + integer_1 << integer_reg + integer_reg.op :>> , integer_1 - integer_reg.op :+ , integer_tmp + integer_reg.op :+ , integer_1 integer_const << 8 - integer_tmp << integer_reg - integer_tmp.op :>> , integer_const + integer_1 << integer_reg + integer_1.op :>> , integer_const - integer_reg.op :+ , integer_tmp + integer_reg.op :+ , integer_1 integer_const << 16 - integer_tmp << integer_reg - integer_tmp.op :>> , integer_const + integer_1 << integer_reg + integer_1.op :>> , integer_const - integer_reg.op :+ , integer_tmp + integer_reg.op :+ , integer_1 integer_const << 3 integer_reg.op :>> , integer_const integer_const << 10 - integer_tmp << integer_reg - integer_tmp.op :* , integer_const + integer_1 << integer_reg + integer_1.op :* , integer_const - integer_self.op :- , integer_tmp - integer_tmp << integer_self + integer_self.op :- , integer_1 + integer_1 << integer_self integer_const << 6 - integer_tmp.op :+ , integer_const + integer_1.op :+ , integer_const integer_const << 4 - integer_tmp.op :>> , integer_const + integer_1.op :>> , integer_const - integer_reg.op :+ , integer_tmp + integer_reg.op :+ , integer_1 - add_new_int(s,integer_reg , integer_tmp) + integer_tmp[Parfait::Integer.integer_index] << integer_reg message[:return_value] << integer_tmp end diff --git a/lib/risc/builtin/object.rb b/lib/risc/builtin/object.rb index bed019a6..1a52279e 100644 --- a/lib/risc/builtin/object.rb +++ b/lib/risc/builtin/object.rb @@ -11,7 +11,7 @@ module Risc compiler.builder(compiler.source).build do object! << message[:receiver] integer! << message[:arguments] - integer << integer[1] + integer << integer[Parfait::NamedList.type_length + 0] #"at" is at index 0 integer.reduce_int object << object[integer] message[:return_value] << object @@ -23,12 +23,12 @@ module Risc # self[index] = val basically. Index is the first arg , value the second # return the value passed in def set_internal_word( context ) - compiler = compiler_for(:Object , :set_internal_word , {at: :Integer, :value => :Object} ) + compiler = compiler_for(:Object , :set_internal_word , {at: :Integer, value: :Object} ) compiler.builder(compiler.source).build do object! << message[:receiver] integer! << message[:arguments] - object_reg! << integer[ 2] - integer << integer[1] + object_reg! << integer[Parfait::NamedList.type_length + 1] #"value" is at index 1 + integer << integer[Parfait::NamedList.type_length + 0] #"at" is at index 0 integer.reduce_int object[integer] << object_reg message[:return_value] << object_reg @@ -125,7 +125,7 @@ module Risc # restore the message that we save in r8 # get a new int and save the c return into it - # tht int gets retured, ie is the return_value of the message + # the int gets retured, ie is the return_value of the message def restore_message(builder) r8 = RegisterValue.new( :r8 , :Message) int = builder.compiler.use_reg(:Integer) diff --git a/lib/risc/builtin/word.rb b/lib/risc/builtin/word.rb index 586df497..1b797fe1 100644 --- a/lib/risc/builtin/word.rb +++ b/lib/risc/builtin/word.rb @@ -24,37 +24,39 @@ module Risc # self[index] basically. Index is the first arg > 0 # return a word sized new int, in return_value + # + # Note: no index (or type) checking. Method should be internal and check before. + # Which means the returned integer could be passed in, instead of allocated. def get_internal_byte( context) compiler = compiler_for(:Word , :get_internal_byte , {at: :Integer}) - compiler.builder(compiler.source).build do + builder = compiler.builder(compiler.source) + integer_tmp = builder.allocate_int + builder.build do object! << message[:receiver] integer! << message[:arguments] - integer << integer[1] + integer << integer[Parfait::NamedList.type_length + 0] #"at" is at index 0 integer.reduce_int object <= object[integer] - add_new_int("get_internal_byte", object , integer) - message[:return_value] << integer + integer_tmp[Parfait::Integer.integer_index] << object + message[:return_value] << integer_tmp end compiler.add_mom( Mom::ReturnSequence.new) return compiler end - # self[index] = val basically. Index is the first arg ( >0), - # value the second - # return value + # self[index] = val basically. Index is the first arg ( >0 , unchecked), + # value the second, which is also returned def set_internal_byte( context ) - compiler = compiler_for(:Word, :set_internal_byte , {at: :Integer , :value => :Integer} ) + compiler = compiler_for(:Word, :set_internal_byte , {at: :Integer , value: :Integer} ) compiler.builder(compiler.source).build do word! << message[:receiver] integer! << message[:arguments] - integer << integer[1] - integer_reg! << message[:arguments] - integer_obj! << integer_reg[2] - integer_reg << integer_reg[2] + integer_reg! << integer[Parfait::NamedList.type_length + 1] #"value" is at index 1 + message[:return_value] << integer_reg + integer << integer[Parfait::NamedList.type_length + 0] #"at" is at index 0 integer.reduce_int integer_reg.reduce_int word[integer] <= integer_reg - message[:return_value] << integer_obj end compiler.add_mom( Mom::ReturnSequence.new) return compiler diff --git a/test/parfait/test_integer.rb b/test/parfait/test_integer.rb index e9ba501f..9b273a0d 100644 --- a/test/parfait/test_integer.rb +++ b/test/parfait/test_integer.rb @@ -7,7 +7,12 @@ module Parfait super @int = Integer.new(10) end - + def test_index + assert_equal 2 , Integer.integer_index + end + def test_next_index + assert_equal 1 , Integer.next_index + end def test_class assert_equal :Integer, @int.get_type.object_class.name end