From 32986512380ee5b413f3693eec8e4550126ff736 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 17 Jun 2018 13:53:17 +0300 Subject: [PATCH] split create_binary into two phases Which gives instructions a chance to check everything and in Arms case check the constant loads/ instruction adding So that during assembly no more change happens (and we don't have to reassemble) --- lib/arm/instructions/instruction.rb | 9 +++++++++ lib/risc/instruction.rb | 4 ++++ lib/risc/machine.rb | 18 +++++++++++++++--- lib/util/dev_null.rb | 7 +++++++ test/risc/helper.rb | 3 --- test/risc/test_interpreter_platform.rb | 2 +- test/risc/test_translator.rb | 2 +- 7 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 lib/util/dev_null.rb diff --git a/lib/arm/instructions/instruction.rb b/lib/arm/instructions/instruction.rb index b49dcec1..b6345a05 100644 --- a/lib/arm/instructions/instruction.rb +++ b/lib/arm/instructions/instruction.rb @@ -1,4 +1,6 @@ require "util/list" +require "util/dev_null" + module Arm # Arm instruction base class # Mostly linked list functionality that all instructions have @@ -21,6 +23,13 @@ module Arm ret end + # precheck that everything is ok, before asembly + # in arm, we use the oppertunity to assemble to dev_null, so any + # additions are done _before_ assemnly + def precheck + assemble(Util::DevNull.new) + end + def insert(instruction) ret = super Risc::Position.get(self).trigger_inserted if Risc::Position.set?(self) diff --git a/lib/risc/instruction.rb b/lib/risc/instruction.rb index 0e17e941..46ac2ffc 100644 --- a/lib/risc/instruction.rb +++ b/lib/risc/instruction.rb @@ -37,6 +37,10 @@ module Risc ret end + # just part of the protocol, noop in this case + def precheck + end + def to_cpu( translator ) translator.translate( self ) end diff --git a/lib/risc/machine.rb b/lib/risc/machine.rb index 357709d6..efe19e53 100644 --- a/lib/risc/machine.rb +++ b/lib/risc/machine.rb @@ -22,6 +22,7 @@ module Risc @constants = [] @next_address = nil end + attr_reader :constants , :cpu_init attr_reader :booted , :translated attr_reader :platform @@ -143,12 +144,23 @@ module Risc # constant loads into one instruction. # def create_binary - object_positions.keys.each do |method| - next unless method.is_a? Parfait::TypedMethod + methods = object_positions.keys.find_all{|obj| obj.is_a? Parfait::TypedMethod} + prerun(methods) + assemble(methods) + log.debug "BinaryInit #{cpu_init.object_id.to_s(16)}" + end + + def prerun(methods) + methods.each do |method| + method.cpu_instructions.each {|i| i.precheck } + end + end + + def assemble(methods) + methods.each do |method| writer = BinaryWriter.new(method.binary) writer.assemble(method.cpu_instructions) end - log.debug "BinaryInit #{cpu_init.object_id.to_s(16)}" end def boot diff --git a/lib/util/dev_null.rb b/lib/util/dev_null.rb new file mode 100644 index 00000000..e1e5866c --- /dev/null +++ b/lib/util/dev_null.rb @@ -0,0 +1,7 @@ +module Util + # A class that does not write, or swallows all incoming data + # Used in Arm to do a dry run (and in testing, where it was born) + class DevNull + def write_unsigned_int_32( _ );end + end +end diff --git a/test/risc/helper.rb b/test/risc/helper.rb index 9bf1b04b..26258082 100644 --- a/test/risc/helper.rb +++ b/test/risc/helper.rb @@ -1,4 +1 @@ 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 index 8baae9d9..2bc75314 100644 --- a/test/risc/test_interpreter_platform.rb +++ b/test/risc/test_interpreter_platform.rb @@ -57,7 +57,7 @@ module Risc @machine.object_positions.each do | method , position| next unless method.is_a? Parfait::TypedMethod method.cpu_instructions.each do |ins| - ins.assemble(DevNull.new) + ins.assemble(Util::DevNull.new) end end end diff --git a/test/risc/test_translator.rb b/test/risc/test_translator.rb index 8ade50a2..bdd76f6b 100644 --- a/test/risc/test_translator.rb +++ b/test/risc/test_translator.rb @@ -33,7 +33,7 @@ module Risc @machine.object_positions.keys.each do |method| next unless method.is_a? Parfait::TypedMethod method.cpu_instructions.each do |ins| - ins.assemble(DevNull.new) + ins.assemble(Util::DevNull.new) end end end