From 49c4d170ce13e3c3a0ef241b42a5c2ac5e2e9be3 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 17 May 2018 09:31:36 +0300 Subject: [PATCH] make the interpreter platform but still using the risc_instruction stream --- lib/risc/interpreter_platform.rb | 38 ++++++++++++++++ lib/risc/machine.rb | 2 +- lib/risc/platform.rb | 5 ++- test/arm/test_arm_platform.rb | 18 ++++++++ test/risc/helper.rb | 3 ++ test/risc/test_interpreter_platform.rb | 61 ++++++++++++++++++++++++++ test/risc/test_platform.rb | 17 ++----- test/risc/test_translator.rb | 5 +-- test/support/risc_interpreter.rb | 2 +- 9 files changed, 131 insertions(+), 20 deletions(-) create mode 100644 lib/risc/interpreter_platform.rb create mode 100644 test/arm/test_arm_platform.rb create mode 100644 test/risc/test_interpreter_platform.rb diff --git a/lib/risc/interpreter_platform.rb b/lib/risc/interpreter_platform.rb new file mode 100644 index 00000000..caf152d1 --- /dev/null +++ b/lib/risc/interpreter_platform.rb @@ -0,0 +1,38 @@ + +module Risc + class InterpreterPlatform + def translator + IdentityTranslator.new + end + def loaded_at + 0x90 + end + def padding + 0x100 - loaded_at + end + end + class Instruction + def nil_next + @next = nil + end + def byte_length + 4 + end + def assemble(io) + io.write_unsigned_int_32(self) + end + class Branch < Instruction + def first + self + end + end + end + class IdentityTranslator + def translate(code) + return Label.new( code.source , code.name ) if code.is_a?(Label) + ret = code.dup + ret.nil_next + ret + end + end +end diff --git a/lib/risc/machine.rb b/lib/risc/machine.rb index b47167d4..5c770f6c 100644 --- a/lib/risc/machine.rb +++ b/lib/risc/machine.rb @@ -125,7 +125,7 @@ module Risc writer = BinaryWriter.new(method.binary) writer.assemble(method.cpu_instructions) end - log.debug "BinaryInit #{cpu_init.first.object_id.to_s(16)}" + log.debug "BinaryInit #{cpu_init.object_id.to_s(16)}" end def boot diff --git a/lib/risc/platform.rb b/lib/risc/platform.rb index e7f75226..b7befb13 100644 --- a/lib/risc/platform.rb +++ b/lib/risc/platform.rb @@ -15,14 +15,17 @@ module Risc end # Factory method to create a Platform object according to the platform # string given. - # Currently only "Arm" + # Currently only "Arm" and "Interpreter" def self.for( name ) case name when "Arm" return Arm::ArmPlatform.new + when "Interpreter" + return Risc::InterpreterPlatform.new else raise "not recignized platform #{name}" end end end end +require_relative "interpreter_platform" diff --git a/test/arm/test_arm_platform.rb b/test/arm/test_arm_platform.rb new file mode 100644 index 00000000..10bf0600 --- /dev/null +++ b/test/arm/test_arm_platform.rb @@ -0,0 +1,18 @@ +require_relative "helper" + +module Arm + class TestArmPlatform < MiniTest::Test + def setup + @arm = Risc::Platform.for("Arm") + end + def test_platform_class + assert_equal Arm::ArmPlatform , @arm.class + end + def test_platform_translator_class + assert_equal Arm::Translator , @arm.translator.class + end + def test_platform_loaded_class + assert_equal Fixnum , @arm.loaded_at.class + end + end +end diff --git a/test/risc/helper.rb b/test/risc/helper.rb index 26258082..9bf1b04b 100644 --- a/test/risc/helper.rb +++ b/test/risc/helper.rb @@ -1 +1,4 @@ require_relative "../helper" +class DevNull + def write_unsigned_int_32( _ );end +end diff --git a/test/risc/test_interpreter_platform.rb b/test/risc/test_interpreter_platform.rb new file mode 100644 index 00000000..bcda9bb2 --- /dev/null +++ b/test/risc/test_interpreter_platform.rb @@ -0,0 +1,61 @@ +require_relative "helper" + +module Risc + class TestInterpreterPlatform < MiniTest::Test + def setup + @inter = Platform.for("Interpreter") + end + def test_platform_class + assert_equal Risc::InterpreterPlatform , @inter.class + end + def test_platform_translator_class + assert_equal Risc::IdentityTranslator , @inter.translator.class + end + def test_platform_loaded_class + assert_equal Fixnum , @inter.loaded_at.class + end + def test_translator + assert IdentityTranslator.new + end + end + class TestTranslator < MiniTest::Test + + def setup + @machine = Risc.machine.boot + @translator = IdentityTranslator.new + end + + def test_translate_label + label = Parfait.object_space.get_main.risc_instructions + assert_equal "Space_Type.main" ,label.to_cpu(@translator).name , label + end + + def test_translate_space + assert @machine.translate(:interpreter) + end + + def test_no_loops_in_chain + @machine.translate(:interpreter) + @machine.position_all + init = Parfait.object_space.get_init + all = [] + init.cpu_instructions.each do |ins| + assert !all.include?(ins) + all << ins + end + end + def test_no_risc #by assembling, risc doesnt have assemble method + @machine.translate(:interpreter) + @machine.position_all + @machine.create_binary + @machine.objects.each do |id , method| + next unless method.is_a? Parfait::TypedMethod + method.cpu_instructions.each do |ins| + ins.assemble(DevNull.new) + end + end + end + + end + +end diff --git a/test/risc/test_platform.rb b/test/risc/test_platform.rb index 2381bea9..6a1c722b 100644 --- a/test/risc/test_platform.rb +++ b/test/risc/test_platform.rb @@ -3,23 +3,14 @@ require_relative "../helper" module Risc class TestPlaform < MiniTest::Test - def test_factory_exists + def test_arm_factory_exists assert Platform.for("Arm") end + def test_inter_factory_exists + assert Platform.for("Interpreter") + end def test_factory_raise assert_raises{ Platform.for("NotArm")} end - def test_platform_class - arm = Platform.for("Arm") - assert_equal Arm::ArmPlatform , arm.class - end - def test_platform_translator_class - arm = Platform.for("Arm") - assert_equal Arm::Translator , arm.translator.class - end - def test_platform_loaded_class - arm = Platform.for("Arm") - assert_equal Fixnum , arm.loaded_at.class - end end end diff --git a/test/risc/test_translator.rb b/test/risc/test_translator.rb index 807c1604..1c579e8d 100644 --- a/test/risc/test_translator.rb +++ b/test/risc/test_translator.rb @@ -1,8 +1,5 @@ -require_relative "../helper" +require_relative "helper" -class DevNull - def write_unsigned_int_32( _ );end -end module Risc class TestTranslator < MiniTest::Test diff --git a/test/support/risc_interpreter.rb b/test/support/risc_interpreter.rb index 3d61c211..aaad2af2 100644 --- a/test/support/risc_interpreter.rb +++ b/test/support/risc_interpreter.rb @@ -9,7 +9,7 @@ module Risc def setup Risc.machine.boot do_clean_compile - Vool::VoolCompiler.ruby_to_vool( @string_input ) + Vool::VoolCompiler.ruby_to_binary( @string_input , :interpreter) Collector.collect_space @interpreter = Interpreter.new @interpreter.start Risc.machine.risc_init