From 6f936f190d728ddd105ec1d02435c99df25d627a Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 4 Jul 2018 08:28:29 +0300 Subject: [PATCH] misc to_s and small fixes --- lib/mom/instruction/return_sequence.rb | 2 +- lib/parfait/message.rb | 3 +++ lib/parfait/named_list.rb | 7 +++++++ lib/risc/interpreter.rb | 15 ++++----------- lib/risc/linker.rb | 1 - lib/risc/position/branch_listener.rb | 2 +- lib/rubyx/rubyx_compiler.rb | 1 + lib/vool/basic_values.rb | 17 ++++++++++++++++- test/risc/test_interpreter.rb | 4 ++-- test/risc/test_linker.rb | 4 ++-- test/risc/test_linker1.rb | 7 +++++++ test/rubyx/test_rubyx_compiler2.rb | 2 +- test/support/compiling.rb | 2 +- 13 files changed, 46 insertions(+), 21 deletions(-) diff --git a/lib/mom/instruction/return_sequence.rb b/lib/mom/instruction/return_sequence.rb index 342aabdc..d3ae8c6d 100644 --- a/lib/mom/instruction/return_sequence.rb +++ b/lib/mom/instruction/return_sequence.rb @@ -33,7 +33,7 @@ module Mom end def to_s - "ReturnSequence" + "ReturnSequence in (#{compiler.method.name})" end end diff --git a/lib/parfait/message.rb b/lib/parfait/message.rb index 49e02066..da0b3dc8 100644 --- a/lib/parfait/message.rb +++ b/lib/parfait/message.rb @@ -39,5 +39,8 @@ module Parfait index = @type.get_index(name) get_at(index) end + def to_s + "Message:#{name}(#{arguments.get_length})" + end end end diff --git a/lib/parfait/named_list.rb b/lib/parfait/named_list.rb index 1a136377..ade4b17b 100644 --- a/lib/parfait/named_list.rb +++ b/lib/parfait/named_list.rb @@ -23,6 +23,13 @@ module Parfait class NamedList < Object + def to_s + str = "NamedList len= #{get_length}" + str += " at #{Risc::Position.get(self)}" if Risc::Position.set?(self) + end + def get_length + get_type.get_length - 1 + end def self.type_for( arguments ) my_class = Parfait.object_space.classes[:NamedList] Type.for_hash( my_class , {type: my_class.instance_type}.merge(arguments)) diff --git a/lib/risc/interpreter.rb b/lib/risc/interpreter.rb index 8d2c375e..861d7ca2 100644 --- a/lib/risc/interpreter.rb +++ b/lib/risc/interpreter.rb @@ -33,8 +33,7 @@ module Risc end def start_program(linker = nil) - @linker = linker || @linker - initialize(@linker) + initialize(linker || @linker) init = @linker.cpu_init set_state(:running) set_pc( Position.get(init).at ) @@ -51,12 +50,7 @@ module Risc raise "Not int #{pos}" unless pos.is_a? Numeric position = Position.at(pos) raise "No position at 0x#{pos.to_s(16)}" unless position - if position.is_a?(CodeListener) - raise "Setting Code #{clock}-#{position}, #{position.method}" - #return set_pc(position.at + Parfait::BinaryCode.byte_offset) - end log.debug "Setting Position #{clock}-#{position}, " - #raise "not instruction position #{position}-#{position.class}-#{position.object.class}" unless position.is_a?(InstructionPosition) set_instruction( position.object ) @clock += 1 @pc = position.at @@ -166,7 +160,7 @@ module Risc else value = object.get_internal_word( index ) end - log.debug "#{@instruction} == #{object}(#{object.class}) (#{value}|#{index})" + log.debug "#{@instruction} == #{object}(#{Position.get(object)}) (#{value}|#{index})" set_register( @instruction.register , value ) true end @@ -219,16 +213,15 @@ module Risc def execute_FunctionCall meth = @instruction.method - at = Position.get(meth.binary).at + at = Position.get(meth.binary) log.debug "Call to #{meth.name} at:#{at}" set_pc(at + Parfait::BinaryCode.byte_offset) - #set_instruction @instruction.method.risc_instructions false end def execute_FunctionReturn link = get_register( @instruction.register ) - log.debug "Return to #{link} #{link.class}" + log.debug "Return to #{link.to_s(16)}" set_pc link false end diff --git a/lib/risc/linker.rb b/lib/risc/linker.rb index 792787a7..bdf5e743 100644 --- a/lib/risc/linker.rb +++ b/lib/risc/linker.rb @@ -82,7 +82,6 @@ module Risc # start at code_start. def position_code(code_start) assemblers.each do |asm| - #next unless method.name == :main or method.name == :__init__ Position.log.debug "Method start #{code_start.to_s(16)} #{asm.method.name}" code_pos = CodeListener.init(asm.method.binary, platform) instructions = asm.instructions diff --git a/lib/risc/position/branch_listener.rb b/lib/risc/position/branch_listener.rb index bce8e461..c301e755 100644 --- a/lib/risc/position/branch_listener.rb +++ b/lib/risc/position/branch_listener.rb @@ -7,7 +7,7 @@ module Risc @branch = branch end - # incoming position is the labels + # incoming position is the label def position_changed(position) @branch.precheck end diff --git a/lib/rubyx/rubyx_compiler.rb b/lib/rubyx/rubyx_compiler.rb index 45b9aead..1abf4a2a 100644 --- a/lib/rubyx/rubyx_compiler.rb +++ b/lib/rubyx/rubyx_compiler.rb @@ -11,6 +11,7 @@ module RubyX def ruby_to_vool vool = RubyCompiler.compile( source ) vool = vool.normalize + #puts vool vool end diff --git a/lib/vool/basic_values.rb b/lib/vool/basic_values.rb index 7d2876d1..50af9f4f 100644 --- a/lib/vool/basic_values.rb +++ b/lib/vool/basic_values.rb @@ -38,6 +38,9 @@ module Vool def slot_definition(method) return Mom::SlotDefinition.new(Parfait.object_space.true_object , []) end + def to_s(depth = 0) + "true" + end end class FalseConstant < Constant def ct_type @@ -46,6 +49,9 @@ module Vool def slot_definition(method) return Mom::SlotDefinition.new(Parfait.object_space.false_object , []) end + def to_s(depth = 0) + "false" + end end class NilConstant < Constant def ct_type @@ -54,6 +60,9 @@ module Vool def slot_definition(method) return Mom::SlotDefinition.new(Parfait.object_space.nil_object , []) end + def to_s(depth = 0) + "nil" + end end class SelfExpression < Expression attr_reader :my_type @@ -67,11 +76,14 @@ module Vool def ct_type @my_type end - def to_s + def to_s(depth = 0) "self" end end class SuperExpression < Statement + def to_s(depth = 0) + "super" + end end class StringConstant < Constant attr_reader :value @@ -84,6 +96,9 @@ module Vool def ct_type Parfait.object_space.get_class_by_name(:Word).instance_type end + def to_s(depth = 0) + "'#{@value}'" + end end class SymbolConstant < StringConstant def ct_type diff --git a/test/risc/test_interpreter.rb b/test/risc/test_interpreter.rb index 80be4b81..c8799eaa 100644 --- a/test/risc/test_interpreter.rb +++ b/test/risc/test_interpreter.rb @@ -54,7 +54,7 @@ module Risc end def test_pc1 @interpreter.tick - assert_equal 21304 , @interpreter.pc + assert_equal 21432 , @interpreter.pc end def test_tick2 @interpreter.tick @@ -68,7 +68,7 @@ module Risc def test_pc2 @interpreter.tick @interpreter.tick - assert_equal 21308 , @interpreter.pc + assert_equal 21436 , @interpreter.pc end def test_tick_14_jump 14.times {@interpreter.tick} diff --git a/test/risc/test_linker.rb b/test/risc/test_linker.rb index 7e07b036..1b4befa3 100644 --- a/test/risc/test_linker.rb +++ b/test/risc/test_linker.rb @@ -1,7 +1,7 @@ require_relative "helper" module Risc - class TestMachineObjects < MiniTest::Test + class TestLinkerObjects < MiniTest::Test def setup Parfait.boot! @@ -17,7 +17,7 @@ module Risc assert_raises {@machine.add_constant( 1 )} end end - class TestMachineInit < MiniTest::Test + class TestLinkerInit < MiniTest::Test def setup Parfait.boot! Risc.boot! diff --git a/test/risc/test_linker1.rb b/test/risc/test_linker1.rb index 79461f67..0f9fae6d 100644 --- a/test/risc/test_linker1.rb +++ b/test/risc/test_linker1.rb @@ -13,5 +13,12 @@ module Risc assert Position.get(obj).valid? , "#{Position.get(obj)} , #{obj.object_id.to_s(16)}" end end + def test_one_main + mains = @linker.assemblers.find_all{|asm| asm.method.name == :main } + assert_equal 1 , mains.length + end + def test_assembler_num + assert_equal 23 , @linker.assemblers.length + end end end diff --git a/test/rubyx/test_rubyx_compiler2.rb b/test/rubyx/test_rubyx_compiler2.rb index 9f43f506..50cfe56d 100644 --- a/test/rubyx/test_rubyx_compiler2.rb +++ b/test/rubyx/test_rubyx_compiler2.rb @@ -14,7 +14,7 @@ module RubyX assert_equal Risc::Linker , @linker.class end def test_method - assert_equal :main , @linker.assemblers.last.method.name + assert_equal :main , @linker.assemblers.first.method.name end end end diff --git a/test/support/compiling.rb b/test/support/compiling.rb index a521bdf9..adadb173 100644 --- a/test/support/compiling.rb +++ b/test/support/compiling.rb @@ -10,7 +10,7 @@ module ScopeHelper end def as_main(statements) - in_Space("def main ; #{statements}; end") + in_Space("def main(arg) ; #{statements}; end") end def as_test_main( statements )