From e86ca5ae9ddc97a7bd9e66a2fa57a7229b3e5e64 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 30 May 2018 14:55:17 +0300 Subject: [PATCH] =?UTF-8?q?integer=20was=20wrong=20padded=C2=A7=20length?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit or rather right (2 as should be and will be) but currently we use next AND value + type means 3, rounded 4 because we can’t swap types at compile time (as is planned at runtime, we use next OR value) --- lib/parfait/data_object.rb | 28 +++---------- lib/risc/text_writer.rb | 14 ++++++- test/elf/test_hello.rb | 2 +- test/helper.rb | 2 +- test/parfait/test_data_object.rb | 46 +++++++++++++++++++++ test/risc/interpreter/calling/test_minus.rb | 2 +- test/risc/test_interpreter.rb | 4 +- test/risc/test_machine.rb | 4 +- 8 files changed, 72 insertions(+), 30 deletions(-) create mode 100644 test/parfait/test_data_object.rb diff --git a/lib/parfait/data_object.rb b/lib/parfait/data_object.rb index 4d09a596..c497baf7 100644 --- a/lib/parfait/data_object.rb +++ b/lib/parfait/data_object.rb @@ -27,47 +27,31 @@ module Parfait def self.integer_index type_length end - def data_length - raise "called #{self}" - end def self.type_length raise "called #{self}" end + def padded_length + self.class.memory_size * 4 + end + def data_length + self.class.memory_size + end end class Data4 < DataObject def self.memory_size 4 end - def data_length - 4 - end - def padded_length - 2 * 4 - end end - class Data8 < DataObject def self.memory_size 8 end - def data_length - 8 - end - def padded_length - 8 * 4 - end end class Data16 < DataObject def self.memory_size 16 end - def data_length - 16 - end - def padded_length - 16 * 4 - end end class Data32 < DataObject def self.memory_size diff --git a/lib/risc/text_writer.rb b/lib/risc/text_writer.rb index 034ebab4..b886e8bb 100644 --- a/lib/risc/text_writer.rb +++ b/lib/risc/text_writer.rb @@ -91,6 +91,8 @@ module Risc write_String obj when Parfait::BinaryCode write_BinaryCode obj + when Parfait::Integer + write_integer obj when Parfait::Data4 write_data4 obj else @@ -143,7 +145,17 @@ module Risc written end + def write_integer( int ) + write_ref_for( int.get_type ) + write_ref_for( int.next_integer ) + @stream.write_signed_int_32( int.value ) + @stream.write_signed_int_32( 0 ) + log.debug "Integer witten stream 0x#{@stream.length.to_s(16)}" + end + def write_data4( code ) + write_ref_for( code.get_type ) + write_ref_for( code.get_type ) write_ref_for( code.get_type ) write_ref_for( code.get_type ) log.debug "Data4 witten stream 0x#{@stream.length.to_s(16)}" @@ -179,7 +191,7 @@ module Risc write_ref_for( string.get_type ) #ref @stream.write_signed_int_32( str.length ) #int @stream.write str - pad_after(str.length + 8 ) # type , length + pad_after(str.length + 8 ) # type , length log.debug "String (0x#{string.length.to_s(16)}) stream 0x#{@stream.length.to_s(16)}" end diff --git a/test/elf/test_hello.rb b/test/elf/test_hello.rb index c0b4791f..b3bdc9a6 100644 --- a/test/elf/test_hello.rb +++ b/test/elf/test_hello.rb @@ -3,7 +3,7 @@ require_relative "../helper" class HelloTest < MiniTest::Test def setup - Risc.machine + Risc.machine.boot end def check Vool::VoolCompiler.ruby_to_binary( "class Space;def main(arg);#{@input};end;end" ) diff --git a/test/helper.rb b/test/helper.rb index 484db2ac..2673e23e 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -14,7 +14,7 @@ end require "minitest/color" require "minitest/autorun" -require "minitest/fail_fast" +#require "minitest/fail_fast" $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'test')) diff --git a/test/parfait/test_data_object.rb b/test/parfait/test_data_object.rb new file mode 100644 index 00000000..62755c15 --- /dev/null +++ b/test/parfait/test_data_object.rb @@ -0,0 +1,46 @@ +require_relative "helper" + +module Parfait + class TestClass < ParfaitTest + + def test_data_class_api_base + assert_raises {DataObject.type_length} + end + def test_data_class_api4 + assert_equal 4 , Data4.memory_size + end + def test_data_class_api8 + assert_equal 8 , Data8.memory_size + end + def test_data_class_api16 + assert_equal 16 , Data16.memory_size + end + def test_data_class_api32 + assert_equal 32 , Data32.memory_size + end + def test_int_len + assert_equal 4 , Integer.new(1).data_length + end + def test_true_len + assert_equal 4 , TrueClass.new.data_length + end + def test_false_len + assert_equal 4 , FalseClass.new.data_length + end + def test_nil_len + assert_equal 4 , NilClass.new.data_length + end + def test_int_pad + assert_equal 16 , Integer.new(2).padded_length + end + def test_true_pad + assert_equal 16 , TrueClass.new.padded_length + end + def test_false_pad + assert_equal 16 , FalseClass.new.padded_length + end + def test_nil_pad + assert_equal 16 , NilClass.new.padded_length + end + end +end diff --git a/test/risc/interpreter/calling/test_minus.rb b/test/risc/interpreter/calling/test_minus.rb index 7517ddd5..d695827e 100644 --- a/test/risc/interpreter/calling/test_minus.rb +++ b/test/risc/interpreter/calling/test_minus.rb @@ -46,7 +46,7 @@ module Risc ret = main_ticks(63) assert_equal FunctionReturn , ret.class assert_equal :r1 , ret.register.symbol - assert_equal 18628 , @interpreter.get_register(ret.register) + assert_equal 19460 , @interpreter.get_register(ret.register) end def test_sys sys = main_ticks(65) diff --git a/test/risc/test_interpreter.rb b/test/risc/test_interpreter.rb index 596e84f1..53570754 100644 --- a/test/risc/test_interpreter.rb +++ b/test/risc/test_interpreter.rb @@ -52,7 +52,7 @@ module Risc end def test_pc1 @interpreter.tick - assert_equal 18392 , @interpreter.pc + assert_equal 19224 , @interpreter.pc end def test_tick2 @interpreter.tick @@ -66,7 +66,7 @@ module Risc def test_pc2 @interpreter.tick @interpreter.tick - assert_equal 18396 , @interpreter.pc + assert_equal 19228 , @interpreter.pc end def test_tick_14_jump 14.times {@interpreter.tick} diff --git a/test/risc/test_machine.rb b/test/risc/test_machine.rb index b5d31418..a5aa8d6c 100644 --- a/test/risc/test_machine.rb +++ b/test/risc/test_machine.rb @@ -29,10 +29,10 @@ module Risc assert_equal 0 , Position.get(@machine.cpu_init).at end def test_cpu_at - assert_equal "0x5714" , Position.get(@machine.cpu_init.first).to_s + assert_equal "0x5a54" , Position.get(@machine.cpu_init.first).to_s end def test_cpu_bin - assert_equal "0x570c" , Position.get(Position.get(@machine.cpu_init.first).binary).to_s + assert_equal "0x5a4c" , Position.get(Position.get(@machine.cpu_init.first).binary).to_s end def test_cpu_label assert_equal Position::InstructionPosition , Position.get(@machine.cpu_init.first).class