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.
|
2014-06-25 01:33:44 +02:00
|
|
|
#
|
|
|
|
# The next step transforms to the register machine layer, which is what actually executes.
|
|
|
|
#
|
|
|
|
|
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
|
2014-06-25 15:00:24 +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.
|
|
|
|
#
|
2014-07-25 10:48:06 +02:00
|
|
|
# The machine is virtual in the sense that it is completely modeled in software,
|
|
|
|
# it's complete state explicitly available (not implicitly by walking stacks or something)
|
2014-06-25 15:00:24 +02:00
|
|
|
|
2014-06-25 01:33:44 +02:00
|
|
|
# The machine has a no register, but local variables, a scope at each point in time.
|
|
|
|
# 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.
|
2014-06-25 01:33:44 +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
|
2014-07-10 16:14:38 +02:00
|
|
|
the_end = Halt.new
|
2014-07-25 10:48:06 +02:00
|
|
|
@message = Message.new(the_end , the_end , :Object)
|
2014-06-25 15:00:24 +02:00
|
|
|
end
|
2014-07-25 10:48:06 +02:00
|
|
|
attr_reader :message
|
2014-07-01 17:58:25 +02:00
|
|
|
|
2014-07-30 20:07:48 +02:00
|
|
|
def self.boot
|
|
|
|
machine = Machine.new
|
|
|
|
machine.boot
|
2014-07-30 20:43:12 +02:00
|
|
|
machine
|
2014-07-30 20:07:48 +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|
|
|
|
|
bytes = File.read(File.join( File.dirname( __FILE__ ) , ".." , "parfait" , "#{clazz}.rb") )
|
|
|
|
# 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
|
2014-07-30 20:43:12 +02:00
|
|
|
expressions = parts.compile( main , self.message )
|
2014-07-30 20:07:48 +02:00
|
|
|
end
|
|
|
|
|
2014-07-30 20:43:12 +02:00
|
|
|
|
2014-06-26 10:34:48 +02:00
|
|
|
# run the instruction stream given. Instructions are a graph and executing means traversing it.
|
|
|
|
# If there is no next instruction the machine stops
|
2014-06-25 15:00:24 +02:00
|
|
|
def run instruction
|
2014-06-26 10:34:48 +02:00
|
|
|
while instruction do
|
|
|
|
next_instruction = instruction.next
|
|
|
|
instruction.execute
|
|
|
|
instruction = next_instruction
|
|
|
|
end
|
2014-06-25 15:00:24 +02:00
|
|
|
end
|
2014-06-25 01:33:44 +02:00
|
|
|
end
|
|
|
|
end
|
2014-06-25 15:00:24 +02:00
|
|
|
|
2014-06-26 17:39:02 +02:00
|
|
|
require_relative "instruction"
|
2014-08-28 07:10:33 +02:00
|
|
|
require_relative "compiled_method"
|
2014-06-29 18:05:35 +02:00
|
|
|
require_relative "frame"
|
2014-07-16 23:53:19 +02:00
|
|
|
require_relative "message"
|
2014-06-26 17:39:02 +02:00
|
|
|
require_relative "value"
|
2014-07-15 08:31:25 +02:00
|
|
|
require_relative "type"
|
2014-06-26 17:39:02 +02:00
|
|
|
require_relative "object"
|
|
|
|
require_relative "constants"
|
2014-08-23 23:25:15 +02:00
|
|
|
require_relative "boot_space"
|
2014-10-02 15:11:45 +02:00
|
|
|
require_relative "passes/send_implementation"
|
|
|
|
require_relative "passes/get_implementation"
|
|
|
|
require_relative "passes/frame_implementation"
|
2014-10-03 13:33:06 +02:00
|
|
|
|
|
|
|
Sof.Volotile.add(Virtual::Block , [:method])
|
|
|
|
Sof.Volotile.add(Virtual::CompiledMethod , [:current])
|