From 5726d2c18161e48383cabe98055345656bab1563 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 1 Jun 2015 08:40:17 +0300 Subject: [PATCH] move machine to module level makes for shorter, more concise, access also remove one more bug possibility (reinitiation) --- lib/arm/passes/call_implementation.rb | 2 +- lib/arm/passes/constant_implementation.rb | 2 +- lib/arm/passes/get_implementation.rb | 2 +- lib/arm/passes/main_implementation.rb | 2 +- lib/arm/passes/return_implementation.rb | 2 +- lib/arm/passes/save_implementation.rb | 2 +- lib/arm/passes/set_implementation.rb | 2 +- lib/arm/passes/transfer_implementation.rb | 2 +- lib/register/builtin/kernel.rb | 2 +- lib/register/builtin/object.rb | 2 +- lib/register/passes/call_implementation.rb | 2 +- lib/register/passes/return_implementation.rb | 2 +- lib/register/passes/set_implementation.rb | 2 +- lib/virtual/compiled_method_info.rb | 2 +- lib/virtual/machine.rb | 17 +++++++++++------ lib/virtual/parfait_adapter.rb | 10 +++++----- lib/virtual/passes/collector.rb | 2 +- lib/virtual/passes/enter_implementation.rb | 2 +- lib/virtual/passes/frame_implementation.rb | 2 +- lib/virtual/passes/get_implementation.rb | 2 +- lib/virtual/passes/send_implementation.rb | 2 +- 21 files changed, 35 insertions(+), 30 deletions(-) diff --git a/lib/arm/passes/call_implementation.rb b/lib/arm/passes/call_implementation.rb index e8e9fb35..339687a3 100644 --- a/lib/arm/passes/call_implementation.rb +++ b/lib/arm/passes/call_implementation.rb @@ -15,5 +15,5 @@ module Arm end end end - Virtual::Machine.instance.add_pass "Arm::CallImplementation" + Virtual.machine.add_pass "Arm::CallImplementation" end diff --git a/lib/arm/passes/constant_implementation.rb b/lib/arm/passes/constant_implementation.rb index 42aacdf8..a497c980 100644 --- a/lib/arm/passes/constant_implementation.rb +++ b/lib/arm/passes/constant_implementation.rb @@ -10,5 +10,5 @@ module Arm end end end - Virtual::Machine.instance.add_pass "Arm::ConstantImplementation" + Virtual.machine.add_pass "Arm::ConstantImplementation" end diff --git a/lib/arm/passes/get_implementation.rb b/lib/arm/passes/get_implementation.rb index 5ced4a9b..fb065789 100644 --- a/lib/arm/passes/get_implementation.rb +++ b/lib/arm/passes/get_implementation.rb @@ -9,5 +9,5 @@ module Arm end end end - Virtual::Machine.instance.add_pass "Arm::GetImplementation" + Virtual.machine.add_pass "Arm::GetImplementation" end diff --git a/lib/arm/passes/main_implementation.rb b/lib/arm/passes/main_implementation.rb index b30c0598..a458d598 100644 --- a/lib/arm/passes/main_implementation.rb +++ b/lib/arm/passes/main_implementation.rb @@ -11,5 +11,5 @@ module Arm end end end - Virtual::Machine.instance.add_pass "Arm::MainImplementation" + Virtual.machine.add_pass "Arm::MainImplementation" end diff --git a/lib/arm/passes/return_implementation.rb b/lib/arm/passes/return_implementation.rb index 37bb7bd6..d92f3191 100644 --- a/lib/arm/passes/return_implementation.rb +++ b/lib/arm/passes/return_implementation.rb @@ -9,5 +9,5 @@ module Arm end end end - Virtual::Machine.instance.add_pass "Arm::ReturnImplementation" + Virtual.machine.add_pass "Arm::ReturnImplementation" end diff --git a/lib/arm/passes/save_implementation.rb b/lib/arm/passes/save_implementation.rb index bce8babb..5416436e 100644 --- a/lib/arm/passes/save_implementation.rb +++ b/lib/arm/passes/save_implementation.rb @@ -15,5 +15,5 @@ module Arm end end end - Virtual::Machine.instance.add_pass "Arm::SaveImplementation" + Virtual.machine.add_pass "Arm::SaveImplementation" end diff --git a/lib/arm/passes/set_implementation.rb b/lib/arm/passes/set_implementation.rb index a1f46e32..fca646c2 100644 --- a/lib/arm/passes/set_implementation.rb +++ b/lib/arm/passes/set_implementation.rb @@ -9,5 +9,5 @@ module Arm end end end - Virtual::Machine.instance.add_pass "Arm::SetImplementation" + Virtual.machine.add_pass "Arm::SetImplementation" end diff --git a/lib/arm/passes/transfer_implementation.rb b/lib/arm/passes/transfer_implementation.rb index 534b50fb..061027fe 100644 --- a/lib/arm/passes/transfer_implementation.rb +++ b/lib/arm/passes/transfer_implementation.rb @@ -9,5 +9,5 @@ module Arm end end end - Virtual::Machine.instance.add_pass "Arm::TransferImplementation" + Virtual.machine.add_pass "Arm::TransferImplementation" end diff --git a/lib/register/builtin/kernel.rb b/lib/register/builtin/kernel.rb index ede7333f..7648ba0c 100644 --- a/lib/register/builtin/kernel.rb +++ b/lib/register/builtin/kernel.rb @@ -8,7 +8,7 @@ module Builtin function = Virtual::CompiledMethodInfo.create_method(:Kernel,:__init__ , []) # puts "INIT LAYOUT #{function.get_layout.get_layout}" function.info.return_type = Virtual::Integer - main = Virtual::Machine.instance.space.get_main + main = Virtual.machine.space.get_main me = Virtual::Self.new(Virtual::Reference) code = Virtual::Set.new(Virtual::Self.new(me.type), me) function.info.add_code(code) diff --git a/lib/register/builtin/object.rb b/lib/register/builtin/object.rb index 9d1166f0..2ba621a5 100644 --- a/lib/register/builtin/object.rb +++ b/lib/register/builtin/object.rb @@ -23,7 +23,7 @@ module Builtin var_name = get_function.args.first return_to = get_function.return_type - index_function = ::Virtual::Machine.instance.space.get_class_by_name(:Object).resolve_method(:index_of) + index_function = ::Virtual.machine.space.get_class_by_name(:Object).resolve_method(:index_of) # get_function.push( [me] ) # index = get_function.call( index_function ) diff --git a/lib/register/passes/call_implementation.rb b/lib/register/passes/call_implementation.rb index 519fab22..14597932 100644 --- a/lib/register/passes/call_implementation.rb +++ b/lib/register/passes/call_implementation.rb @@ -23,5 +23,5 @@ module Register end end end - Virtual::Machine.instance.add_pass "Register::CallImplementation" + Virtual.machine.add_pass "Register::CallImplementation" end diff --git a/lib/register/passes/return_implementation.rb b/lib/register/passes/return_implementation.rb index c77f5d57..8b6b9694 100644 --- a/lib/register/passes/return_implementation.rb +++ b/lib/register/passes/return_implementation.rb @@ -18,5 +18,5 @@ module Register end end end - Virtual::Machine.instance.add_pass "Register::ReturnImplementation" + Virtual.machine.add_pass "Register::ReturnImplementation" end diff --git a/lib/register/passes/set_implementation.rb b/lib/register/passes/set_implementation.rb index d8118d3e..af95f10c 100644 --- a/lib/register/passes/set_implementation.rb +++ b/lib/register/passes/set_implementation.rb @@ -42,5 +42,5 @@ module Register end end end - Virtual::Machine.instance.add_pass "Register::SetImplementation" + Virtual.machine.add_pass "Register::SetImplementation" end diff --git a/lib/virtual/compiled_method_info.rb b/lib/virtual/compiled_method_info.rb index f59ea2d4..12f544c0 100644 --- a/lib/virtual/compiled_method_info.rb +++ b/lib/virtual/compiled_method_info.rb @@ -37,7 +37,7 @@ module Virtual def self.create_method( class_name , method_name , args) raise "uups #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol raise "uups #{method_name}.#{method_name.class}" unless class_name.is_a? Symbol - clazz = Machine.instance.space.get_class_by_name class_name + clazz = Virtual.machine.space.get_class_by_name class_name raise "No such class #{class_name}" unless clazz method = clazz.create_instance_method( method_name , Virtual.new_list(args)) method.info = CompiledMethodInfo.new diff --git a/lib/virtual/machine.rb b/lib/virtual/machine.rb index 4b7a61e0..c17d562d 100644 --- a/lib/virtual/machine.rb +++ b/lib/virtual/machine.rb @@ -39,6 +39,7 @@ module Virtual @parser = Parser::Salama.new @passes = [ "Virtual::SendImplementation" ] @objects = [] + @booted = false end attr_reader :passes , :space , :class_mappings , :init , :objects @@ -91,20 +92,17 @@ module Virtual end def self.boot - me = self.instance + me = Virtual.machine # boot is a verb here. this is a somewhat tricky process which is in it's own file, boot.rb + raise "already booted" if @booted me.boot_parfait! me end - def self.instance - @instance ||= Machine.new - end # for testing, make sure no old artefacts hang around #maybe should be moved to test dir def self.reboot - @instance = nil - self.boot + raise "redo" end def compile_main bytes syntax = @parser.parse_with_debug(bytes) @@ -112,6 +110,13 @@ module Virtual Compiler.compile( parts , @space.get_main ) end end + + def self.machine + unless defined?(@machine) + @machine = Machine.new + end + @machine + end end require_relative "boot" diff --git a/lib/virtual/parfait_adapter.rb b/lib/virtual/parfait_adapter.rb index 291cd2be..5fa7dc73 100644 --- a/lib/virtual/parfait_adapter.rb +++ b/lib/virtual/parfait_adapter.rb @@ -9,7 +9,7 @@ module FakeMem super() @memory = [0,nil] @position = nil - if Virtual::Machine.instance.class_mappings + if Virtual.machine.class_mappings init_layout else #puts "No init for #{self.class}:#{self.object_id}" @@ -17,7 +17,7 @@ module FakeMem end def init_layout vm_name = self.class.name.split("::").last.to_sym - clazz = Virtual::Machine.instance.class_mappings[vm_name] + clazz = Virtual.machine.class_mappings[vm_name] raise "Class not found #{vm_name}" unless clazz raise "Layout not set #{vm_name}" unless clazz.object_layout self.set_layout clazz.object_layout @@ -25,7 +25,7 @@ module FakeMem #TODO, this is copied from module Positioned, maybe avoid duplication ? def position if @position.nil? - str = "IN machine #{Virtual::Machine.instance.objects.include?(self)}\n" + str = "IN machine #{Virtual.machine.objects.include?(self)}\n" raise str + "position not set for #{self.class} at #{mem_length} for #{self.inspect[0...100]}" end @position @@ -71,7 +71,7 @@ class Symbol true end def get_layout - Virtual::Machine.instance.class_mappings[:Word].object_layout + Virtual.machine.class_mappings[:Word].object_layout end def mem_length to_s.length @@ -87,7 +87,7 @@ class Symbol pos = cache_positions[self] if pos == nil str = "position accessed but not set, " - str += "Machine has object=#{Virtual::Machine.instance.objects.include?(self)} " + str += "Machine has object=#{Virtual.machine.objects.include?(self)} " raise str + " for Symbol:#{self}" end pos diff --git a/lib/virtual/passes/collector.rb b/lib/virtual/passes/collector.rb index 1707f319..be76db7b 100644 --- a/lib/virtual/passes/collector.rb +++ b/lib/virtual/passes/collector.rb @@ -9,7 +9,7 @@ module Virtual def keep object return if object.nil? - return unless Machine.instance.add_object object + return unless Virtual.machine.add_object object #puts "adding #{object.class}" unless object.has_layout? object.init_layout diff --git a/lib/virtual/passes/enter_implementation.rb b/lib/virtual/passes/enter_implementation.rb index e306c01d..793f25bf 100644 --- a/lib/virtual/passes/enter_implementation.rb +++ b/lib/virtual/passes/enter_implementation.rb @@ -13,5 +13,5 @@ module Virtual end end end - Virtual::Machine.instance.add_pass "Virtual::EnterImplementation" + Virtual.machine.add_pass "Virtual::EnterImplementation" end diff --git a/lib/virtual/passes/frame_implementation.rb b/lib/virtual/passes/frame_implementation.rb index eecad898..35d276fd 100644 --- a/lib/virtual/passes/frame_implementation.rb +++ b/lib/virtual/passes/frame_implementation.rb @@ -48,5 +48,5 @@ module Virtual end end end - Virtual::Machine.instance.add_pass "Virtual::FrameImplementation" + Virtual.machine.add_pass "Virtual::FrameImplementation" end diff --git a/lib/virtual/passes/get_implementation.rb b/lib/virtual/passes/get_implementation.rb index 64f69e1c..4fe97835 100644 --- a/lib/virtual/passes/get_implementation.rb +++ b/lib/virtual/passes/get_implementation.rb @@ -13,5 +13,5 @@ module Virtual end end end - Virtual::Machine.instance.add_pass "Virtual::GetImplementation" + Virtual.machine.add_pass "Virtual::GetImplementation" end diff --git a/lib/virtual/passes/send_implementation.rb b/lib/virtual/passes/send_implementation.rb index 6e5ce675..181cdfae 100644 --- a/lib/virtual/passes/send_implementation.rb +++ b/lib/virtual/passes/send_implementation.rb @@ -30,7 +30,7 @@ module Virtual else # note: this is the current view: call internal send, even the method name says else # but send is "special" and accesses the internal method name and resolves. - kernel = Virtual::Machine.instance.space.get_class_by_name("Kernel") + kernel = Virtual.machine.space.get_class_by_name("Kernel") method = kernel.get_instance_method(:__send) new_codes << MethodCall.new( method ) raise "unimplemented: \n#{code}"