2014-06-26 16:48:33 +02:00
|
|
|
module Virtual
|
2014-06-25 15:00:24 +02:00
|
|
|
# The Virtual Machine is a value based virtual machine in which ruby is implemented. While it is value based,
|
|
|
|
# it resembles oo in basic ways of object encapsulation and method invokation, it is a "closed" / static sytem
|
|
|
|
# in that all types are know and there is no dynamic dispatch (so we don't bite our tail here).
|
|
|
|
#
|
2014-06-25 01:33:44 +02:00
|
|
|
# It is minimal and realistic and low level
|
|
|
|
# - minimal means that if one thing can be implemented by another, it is left out. This is quite the opposite from
|
|
|
|
# ruby, which has several loops, many redundant if forms and the like.
|
|
|
|
# - realistic means it is easy to implement on a 32 bit machine (arm) and possibly 64 bit. Memory access, a stack,
|
|
|
|
# some registers of same size are the underlying hardware. (not ie byte machine)
|
|
|
|
# - low level means it's basic instructions are realively easily implemented in a register machine. ie send is not
|
|
|
|
# a an instruction but a function.
|
|
|
|
#
|
2014-07-25 10:48:06 +02:00
|
|
|
# So the memory model of the machine allows for indexed access into an "object" . A fixed number of objects exist
|
2014-06-25 01:33:44 +02:00
|
|
|
# (ie garbage collection is reclaming, not destroying and recreating) although there may be a way to increase that number.
|
|
|
|
#
|
2014-06-25 15:00:24 +02:00
|
|
|
# The ast is transformed to virtaul-machine objects, some of which represent code, some data.
|
2015-05-04 10:10:40 +02:00
|
|
|
#
|
|
|
|
# The next step transforms to the register machine layer, which is what actually executes.
|
2014-06-25 01:33:44 +02:00
|
|
|
#
|
|
|
|
|
2014-07-25 10:48:06 +02:00
|
|
|
# More concretely, a virtual machine is a sort of oo turing machine, it has a current instruction, executes the
|
2015-05-04 10:10:40 +02:00
|
|
|
# instructions, fetches the next one and so on.
|
2014-06-25 01:33:44 +02:00
|
|
|
# Off course the instructions are not soo simple, but in oo terms quite so.
|
2015-05-04 10:10:40 +02:00
|
|
|
#
|
|
|
|
# The machine is virtual in the sense that it is completely modeled in software,
|
2014-07-25 10:48:06 +02:00
|
|
|
# it's complete state explicitly available (not implicitly by walking stacks or something)
|
2014-06-25 15:00:24 +02:00
|
|
|
|
2015-05-04 10:10:40 +02:00
|
|
|
# The machine has a no register, but local variables, a scope at each point in time.
|
2014-06-25 01:33:44 +02:00
|
|
|
# Scope changes with calls and blocks, but is saved at each level. In terms of lower level implementation this means
|
2014-06-25 15:00:24 +02:00
|
|
|
# that the the model is such that what is a variable in ruby, never ends up being just on the pysical stack.
|
2015-05-04 10:10:40 +02:00
|
|
|
#
|
2014-06-25 15:00:24 +02:00
|
|
|
class Machine
|
|
|
|
|
|
|
|
def initialize
|
2014-07-30 20:07:48 +02:00
|
|
|
@parser = Parser::Salama.new
|
2015-05-16 11:53:10 +02:00
|
|
|
#the_end = Halt.new
|
2015-05-12 14:36:44 +02:00
|
|
|
@passes = [ "Virtual::SendImplementation" ]
|
|
|
|
@space = Parfait::Space.new
|
2015-05-16 19:16:49 +02:00
|
|
|
# @message = Message.new(the_end , the_end , "Object" )
|
2014-06-25 15:00:24 +02:00
|
|
|
end
|
2015-05-16 11:53:10 +02:00
|
|
|
attr_reader :message , :passes , :space , :init , :main
|
2015-05-12 14:36:44 +02:00
|
|
|
|
|
|
|
def run_passes
|
|
|
|
@passes.each do |pass_class|
|
2015-05-16 11:53:10 +02:00
|
|
|
blocks = [@init] + @main.blocks
|
|
|
|
@space.classes.values.each do |c|
|
2015-05-12 14:36:44 +02:00
|
|
|
c.instance_methods.each {|f| blocks += f.blocks }
|
|
|
|
end
|
|
|
|
#puts "running #{pass_class}"
|
2015-05-16 11:53:10 +02:00
|
|
|
blocks.each do |block|
|
2015-05-12 14:36:44 +02:00
|
|
|
pass = eval pass_class
|
|
|
|
raise "no such pass-class as #{pass_class}" unless pass
|
|
|
|
pass.new.run(block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Passes may be added to by anyone who wants
|
|
|
|
# This is intentionally quite flexible, though one sometimes has to watch the order of them
|
|
|
|
# most ordering is achieved by ordering the requires and using add_pass
|
|
|
|
# but more precise control is possible with the _after and _before versions
|
|
|
|
|
|
|
|
def add_pass pass
|
|
|
|
@passes << pass
|
|
|
|
end
|
|
|
|
def add_pass_after( pass , after)
|
|
|
|
index = @passes.index(after)
|
|
|
|
raise "No such pass (#{pass}) to add after: #{after}" unless index
|
|
|
|
@passes.insert(index+1 , pass)
|
|
|
|
end
|
|
|
|
def add_pass_before( pass , after)
|
|
|
|
index = @passes.index(after)
|
|
|
|
raise "No such pass to add after: #{after}" unless index
|
|
|
|
@passes.insert(index , pass)
|
|
|
|
end
|
2014-07-01 17:58:25 +02:00
|
|
|
|
2014-07-30 20:07:48 +02:00
|
|
|
def self.boot
|
2015-05-12 14:36:44 +02:00
|
|
|
instance = self.instance
|
2015-05-16 19:16:49 +02:00
|
|
|
instance.boot_parfait! # boot is a verb here
|
2015-05-12 14:36:44 +02:00
|
|
|
instance.boot
|
|
|
|
instance
|
|
|
|
end
|
|
|
|
def self.instance
|
|
|
|
@instance ||= Machine.new
|
|
|
|
end
|
2015-05-17 13:41:18 +02:00
|
|
|
|
2015-05-16 19:16:49 +02:00
|
|
|
# for testing, make sure no old artefacts hang around
|
|
|
|
#maybe should be moved to test dir
|
|
|
|
def self.reboot
|
|
|
|
@instance = nil
|
|
|
|
self.boot
|
|
|
|
end
|
2015-05-12 14:36:44 +02:00
|
|
|
# boot the classes, ie create a minimal set of classes with a minimal set of functions
|
|
|
|
# minimal means only that which can not be coded in ruby
|
|
|
|
# CompiledMethods are grabbed from respective modules by sending the method name. This should return the
|
|
|
|
# implementation of the method (ie a method object), not actually try to implement it (as that's impossible in ruby)
|
2015-05-16 19:16:49 +02:00
|
|
|
def boot_parfait!
|
|
|
|
boot_classes!
|
2015-05-16 11:53:10 +02:00
|
|
|
# very fiddly chicken 'n egg problem. Functions need to be in the right order, and in fact we
|
|
|
|
# have to define some dummies, just for the other to compile
|
|
|
|
# TODO: go through the virtual parfait layer and adjust function names to what they really are
|
2015-05-16 19:16:49 +02:00
|
|
|
obj = @space.get_class_by_name "Object"
|
2015-05-12 14:36:44 +02:00
|
|
|
[:index_of , :_get_instance_variable , :_set_instance_variable].each do |f|
|
|
|
|
obj.add_instance_method Builtin::Object.send(f , nil)
|
|
|
|
end
|
2015-05-16 19:16:49 +02:00
|
|
|
obj = @space.get_class_by_name "Kernel"
|
2015-05-12 14:36:44 +02:00
|
|
|
# create main first, __init__ calls it
|
|
|
|
@main = Builtin::Kernel.send(:main , @context)
|
|
|
|
obj.add_instance_method @main
|
|
|
|
underscore_init = Builtin::Kernel.send(:__init__ ,nil) #store , so we don't have to resolve it below
|
|
|
|
obj.add_instance_method underscore_init
|
|
|
|
[:putstring,:exit,:__send].each do |f|
|
|
|
|
obj.add_instance_method Builtin::Kernel.send(f , nil)
|
|
|
|
end
|
|
|
|
# and the @init block in turn _jumps_ to __init__
|
|
|
|
# the point of which is that by the time main executes, all is "normal"
|
2015-05-12 17:52:01 +02:00
|
|
|
@init = Block.new(:_init_ , nil )
|
2015-05-12 14:36:44 +02:00
|
|
|
@init.add_code(Register::RegisterMain.new(underscore_init))
|
2015-05-16 19:16:49 +02:00
|
|
|
obj = @space.get_class_by_name "Integer"
|
2015-05-12 14:36:44 +02:00
|
|
|
[:putint,:fibo].each do |f|
|
|
|
|
obj.add_instance_method Builtin::Integer.send(f , nil)
|
|
|
|
end
|
2015-05-16 19:16:49 +02:00
|
|
|
obj = @space.get_class_by_name "Word"
|
2015-05-12 14:36:44 +02:00
|
|
|
[:get , :set , :puts].each do |f|
|
2015-05-13 15:17:10 +02:00
|
|
|
obj.add_instance_method Builtin::Word.send(f , nil)
|
2015-05-12 14:36:44 +02:00
|
|
|
end
|
2015-05-16 19:16:49 +02:00
|
|
|
obj = space.get_class_by_name "List"
|
2015-05-12 14:36:44 +02:00
|
|
|
[:get , :set , :push].each do |f|
|
|
|
|
obj.add_instance_method Builtin::Array.send(f , nil)
|
|
|
|
end
|
2014-07-30 20:07:48 +02:00
|
|
|
end
|
|
|
|
|
2015-05-16 19:16:49 +02:00
|
|
|
def boot_classes!
|
|
|
|
values = [ "Integer" , "Object" , "Value" , "Kernel"]
|
|
|
|
rest = ["Word" , "Class" , "Dictionary" , "Space" , "List", "Layout"]
|
|
|
|
(values + rest).each { |cl| @space.create_class(cl , []) }
|
|
|
|
value_class = @space.get_class_by_name "Value"
|
|
|
|
@space.get_class_by_name("Integer").set_super_class( value_class )
|
|
|
|
object_class = @space.get_class_by_name("Object")
|
|
|
|
object_class.set_super_class( value_class )
|
|
|
|
rest.each do |name|
|
|
|
|
cl = @space.get_class_by_name( name )
|
|
|
|
cl.set_super_class(object_class)
|
|
|
|
end
|
2015-05-17 13:41:18 +02:00
|
|
|
#boot_layouts
|
2015-05-16 19:16:49 +02:00
|
|
|
end
|
2014-07-30 20:43:12 +02:00
|
|
|
def boot
|
|
|
|
# read all the files needed for a minimal system at compile
|
|
|
|
classes = ["object"]
|
|
|
|
classes.each do |clazz|
|
2015-05-04 22:03:52 +02:00
|
|
|
bytes = File.read(File.join( File.dirname( __FILE__ ) , ".." , "parfait" , "#{clazz}.rb") )
|
|
|
|
bytes = 0 #shuts up my atom linter
|
2014-07-30 20:43:12 +02:00
|
|
|
# expression = compile_main(bytes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def compile_main bytes
|
|
|
|
syntax = @parser.parse_with_debug(bytes)
|
|
|
|
parts = Parser::Transform.new.apply(syntax)
|
2014-08-28 07:10:33 +02:00
|
|
|
main = Virtual::CompiledMethod.main
|
2015-05-06 14:15:33 +02:00
|
|
|
Compiler.compile( parts , main )
|
2014-07-30 20:07:48 +02:00
|
|
|
end
|
2014-06-25 01:33:44 +02:00
|
|
|
end
|
2014-06-25 15:00:24 +02:00
|
|
|
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|