2015-07-18 13:33:09 +02:00
|
|
|
require 'parslet/convenience'
|
2015-10-18 18:27:02 +02:00
|
|
|
require_relative "collector"
|
2015-10-22 17:16:29 +02:00
|
|
|
module Register
|
2015-10-24 10:42:36 +02:00
|
|
|
# The Register Machine is a object based virtual machine on which ruby will be implemented.
|
2014-06-25 15:00:24 +02:00
|
|
|
#
|
2015-10-24 10:42:36 +02:00
|
|
|
|
2015-06-20 22:49:30 +02:00
|
|
|
# The ast is transformed to virtual-machine objects, some of which represent code, some data.
|
2014-06-25 01:33:44 +02:00
|
|
|
#
|
2015-06-20 22:49:30 +02:00
|
|
|
# The next step transforms to the register machine layer, which is quite close to what actually
|
|
|
|
# executes. The step after transforms to Arm, which creates executables.
|
2014-06-25 01:33:44 +02:00
|
|
|
#
|
2015-06-20 22:49:30 +02:00
|
|
|
|
2014-06-25 15:00:24 +02:00
|
|
|
class Machine
|
2015-10-18 18:27:02 +02:00
|
|
|
include Collector
|
2015-07-18 10:53:04 +02:00
|
|
|
|
2014-06-25 15:00:24 +02:00
|
|
|
def initialize
|
2014-07-30 20:07:48 +02:00
|
|
|
@parser = Parser::Salama.new
|
2015-07-21 14:40:25 +02:00
|
|
|
@objects = {}
|
2015-06-01 07:40:17 +02:00
|
|
|
@booted = false
|
2014-06-25 15:00:24 +02:00
|
|
|
end
|
2015-10-24 10:42:36 +02:00
|
|
|
attr_reader :space , :class_mappings , :init , :objects , :booted
|
|
|
|
|
2015-05-12 14:36:44 +02:00
|
|
|
|
2015-10-24 10:42:36 +02:00
|
|
|
# idea being that later method missing could catch translate_xxx and translate to target xxx
|
|
|
|
# now we just instantiate ArmTranslater and pass instructions
|
|
|
|
def translate_arm
|
|
|
|
translator = Arm::Translator.new
|
|
|
|
methods = []
|
2015-06-12 17:52:06 +02:00
|
|
|
@space.classes.values.each do |c|
|
|
|
|
c.instance_methods.each do |f|
|
2015-10-24 10:42:36 +02:00
|
|
|
methods << f.source
|
2015-05-12 14:36:44 +02:00
|
|
|
end
|
2015-06-12 17:52:06 +02:00
|
|
|
end
|
2015-10-24 10:42:36 +02:00
|
|
|
methods.each do |method|
|
|
|
|
instruction = method.instructions
|
|
|
|
begin
|
|
|
|
nekst = instruction.next
|
|
|
|
t = translate(translator , nekst) # returning nil means no replace
|
|
|
|
instruction.replace_next(t) if t
|
|
|
|
instruction = nekst
|
|
|
|
end while instruction.next
|
2015-06-12 17:52:06 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-24 10:42:36 +02:00
|
|
|
# translator should translate from register instructio set to it's own (arm eg)
|
|
|
|
# for each instruction we call the translator with translate_XXX
|
|
|
|
# with XXX being the class name.
|
|
|
|
# the result is replaced in the stream
|
|
|
|
def translate translator , instruction
|
|
|
|
class_name = instruction.class.name.split("::").last
|
|
|
|
translator.send( "translate_#{class_name}".to_sym , instruction)
|
2015-05-12 14:36:44 +02:00
|
|
|
end
|
|
|
|
|
2015-06-12 17:52:06 +02:00
|
|
|
|
2015-05-31 12:02:29 +02:00
|
|
|
# Objects are data and get assembled after functions
|
|
|
|
def add_object o
|
2015-07-21 14:40:25 +02:00
|
|
|
return false if @objects[o.object_id]
|
2015-09-27 15:06:48 +02:00
|
|
|
return if o.is_a? Fixnum
|
2015-07-21 18:41:30 +02:00
|
|
|
raise "adding non parfait #{o.class}" unless o.is_a? Parfait::Object or o.is_a? Symbol
|
2015-07-21 14:40:25 +02:00
|
|
|
@objects[o.object_id] = o
|
2015-05-31 12:02:29 +02:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-06-12 17:52:06 +02:00
|
|
|
def boot
|
|
|
|
boot_parfait!
|
2015-10-23 20:27:36 +02:00
|
|
|
@init = Branch.new( "__init__" , self.space.get_init.source.instructions )
|
2015-06-12 17:52:06 +02:00
|
|
|
@booted = true
|
2015-07-18 10:53:04 +02:00
|
|
|
self
|
2015-06-12 17:52:06 +02:00
|
|
|
end
|
|
|
|
|
2015-10-05 23:27:13 +02:00
|
|
|
def parse_and_compile bytes
|
2014-07-30 20:43:12 +02:00
|
|
|
syntax = @parser.parse_with_debug(bytes)
|
|
|
|
parts = Parser::Transform.new.apply(syntax)
|
2015-09-27 21:39:10 +02:00
|
|
|
#puts parts.inspect
|
2015-10-23 13:22:55 +02:00
|
|
|
Soml::Compiler.compile( parts )
|
2014-07-30 20:07:48 +02:00
|
|
|
end
|
2015-06-12 17:52:06 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
def run_blocks_for pass_class
|
2015-07-17 10:39:20 +02:00
|
|
|
parts = pass_class.split("::")
|
|
|
|
pass = Object.const_get(parts[0]).const_get parts[1]
|
2015-06-12 17:52:06 +02:00
|
|
|
raise "no such pass-class as #{pass_class}" unless pass
|
|
|
|
@blocks.each do |block|
|
|
|
|
raise "nil block " unless block
|
|
|
|
pass.new.run(block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-25 01:33:44 +02:00
|
|
|
end
|
2015-06-01 07:40:17 +02:00
|
|
|
|
2015-06-12 17:52:06 +02:00
|
|
|
# Module function to retrieve singleton
|
2015-06-01 07:40:17 +02:00
|
|
|
def self.machine
|
|
|
|
unless defined?(@machine)
|
|
|
|
@machine = Machine.new
|
|
|
|
end
|
|
|
|
@machine
|
|
|
|
end
|
2015-06-12 17:52:06 +02:00
|
|
|
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
2015-05-19 19:29:33 +02:00
|
|
|
|
2015-07-18 15:12:50 +02:00
|
|
|
Parfait::Method.class_eval do
|
|
|
|
# for testing we need to reuse the main function (or do we?)
|
|
|
|
# so remove the code that is there
|
|
|
|
def clear_source
|
|
|
|
self.source.send :initialize , self
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-05-19 19:29:33 +02:00
|
|
|
require_relative "boot"
|