move space to parfait
Also make the machine the singleton and space hang off it Many repercussions, not all fixed in this commit
This commit is contained in:
@@ -20,7 +20,7 @@ also in an similar way that objects have their classes at runtime.
|
||||
*Ast* instances get created by the salama-reader gem from source.
|
||||
Here we add compile functions to ast classes and comile the ast layer into Virtual:: objects
|
||||
|
||||
The main objects are BootSpace (lots of objects), BootClass (represents a class),
|
||||
The main objects are Space (lots of objects), BootClass (represents a class),
|
||||
CompiledMethod (with Blocks and Instruction).
|
||||
|
||||
**Virtual** Instructions get further transformed into **register** instructions.
|
||||
|
@@ -1,139 +0,0 @@
|
||||
require "register/builtin/object"
|
||||
|
||||
module Virtual
|
||||
# The BootSpace contains all objects for a program. In functional terms it is a program, but in oo
|
||||
# it is a collection of objects, some of which are data, some classes, some functions
|
||||
|
||||
# The main entry is a function called (of all things) "main", This _must be supplied by the compling
|
||||
# There is a start and exit block that call main, which receives an array of strings
|
||||
|
||||
# While data ususally would live in a .data section, we may also "inline" it into the code
|
||||
# in an oo system all data is represented as objects
|
||||
|
||||
class BootSpace < Virtual::ObjectConstant
|
||||
|
||||
def initialize
|
||||
super()
|
||||
@classes = Parfait::Hash.new_object
|
||||
#global objects (data)
|
||||
@objects = []
|
||||
@symbols = []
|
||||
@frames = 100.times.collect{ ::Parfait::Frame.new([],[])}
|
||||
@messages = 100.times.collect{ ::Parfait::Message.new }
|
||||
@next_message = @messages.first
|
||||
@next_frame = @frames.first
|
||||
@passes = [ "Virtual::SendImplementation" ]
|
||||
end
|
||||
attr_reader :init , :main , :classes , :objects , :symbols,:messages, :next_message , :next_frame
|
||||
|
||||
def run_passes
|
||||
@passes.each do |pass_class|
|
||||
blocks = [@init] + main.blocks
|
||||
@classes.values.each do |c|
|
||||
c.instance_methods.each {|f| blocks += f.blocks }
|
||||
end
|
||||
#puts "running #{pass_class}"
|
||||
all.each do |block|
|
||||
pass = eval pass_class
|
||||
raise "no such pass-class as #{pass_class}" unless pass
|
||||
pass.new.run(block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.space
|
||||
if defined? @@space
|
||||
@@space
|
||||
else
|
||||
@@space = BootSpace.new
|
||||
@@space
|
||||
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
|
||||
# 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)
|
||||
def boot_classes!
|
||||
# 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
|
||||
obj = get_or_create_class :Object
|
||||
[:index_of , :_get_instance_variable , :_set_instance_variable].each do |f|
|
||||
obj.add_instance_method Builtin::Object.send(f , nil)
|
||||
end
|
||||
obj = get_or_create_class :Kernel
|
||||
# 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"
|
||||
@init = Virtual::Block.new(:_init_ , nil )
|
||||
@init.add_code(Register::RegisterMain.new(underscore_init))
|
||||
obj = get_or_create_class :Integer
|
||||
[:putint,:fibo].each do |f|
|
||||
obj.add_instance_method Builtin::Integer.send(f , nil)
|
||||
end
|
||||
obj = get_or_create_class :String
|
||||
[:get , :set , :puts].each do |f|
|
||||
obj.add_instance_method Builtin::String.send(f , nil)
|
||||
end
|
||||
obj = get_or_create_class :Array
|
||||
[:get , :set , :push].each do |f|
|
||||
obj.add_instance_method Builtin::Array.send(f , nil)
|
||||
end
|
||||
end
|
||||
|
||||
@@SPACE = { :names => [:classes,:objects,:symbols,:messages, :next_message , :next_frame] ,
|
||||
:types => [Virtual::Reference,Virtual::Reference,Virtual::Reference,Virtual::Reference,Virtual::Reference]}
|
||||
def layout
|
||||
@@SPACE
|
||||
end
|
||||
|
||||
# Objects are data and get assembled after functions
|
||||
def add_object o
|
||||
return if @objects.include?(o)
|
||||
@objects << o
|
||||
if o.is_a? Symbol
|
||||
@symbols << o
|
||||
end
|
||||
end
|
||||
|
||||
# this is the way to instantiate classes (not BootClass.new)
|
||||
# so we get and keep exactly one per name
|
||||
def get_or_create_class name
|
||||
raise "uups #{name}.#{name.class}" unless name.is_a? Symbol
|
||||
c = @classes[name]
|
||||
unless c
|
||||
c = BootClass.new(name)
|
||||
@classes[name] = c
|
||||
end
|
||||
c
|
||||
end
|
||||
def mem_length
|
||||
padded_words( 5 )
|
||||
end
|
||||
end
|
||||
end
|
@@ -62,7 +62,7 @@ module Virtual
|
||||
|
||||
|
||||
def self.compile_module expression , method
|
||||
clazz = BootSpace.space.get_or_create_class name
|
||||
clazz = Space.space.get_or_create_class name
|
||||
raise "uups #{clazz}.#{name}" unless clazz
|
||||
to = Return.new(Reference , clazz )
|
||||
method.add_code Set.new( to , clazz )
|
||||
@@ -73,7 +73,7 @@ module Virtual
|
||||
def self.compile_string expression , method
|
||||
value = StringConstant.new(expression.string)
|
||||
to = Return.new(Reference , value)
|
||||
BootSpace.space.add_object value
|
||||
Space.space.add_object value
|
||||
method.add_code Set.new( to , value )
|
||||
to
|
||||
end
|
||||
|
@@ -9,7 +9,7 @@ module Virtual
|
||||
r = expression.receiver ? Compiler.compile(expression.receiver, method ) : Self.new()
|
||||
new_method = CompiledMethod.new(expression.name , args , r )
|
||||
new_method.class_name = r.is_a?(BootClass) ? r.name : method.class_name
|
||||
clazz = BootSpace.space.get_or_create_class(new_method.class_name)
|
||||
clazz = Space.space.get_or_create_class(new_method.class_name)
|
||||
clazz.add_instance_method new_method
|
||||
|
||||
#frame = frame.new_frame
|
||||
|
@@ -6,7 +6,7 @@ module Virtual
|
||||
end
|
||||
|
||||
def self.compile_class expression , method
|
||||
clazz = ::BootSpace.space.get_or_create_class expression.name
|
||||
clazz = ::Space.space.get_or_create_class expression.name
|
||||
puts "Created class #{clazz.name.inspect}"
|
||||
expression.expressions.each do |expr|
|
||||
# check if it's a function definition and add
|
||||
|
@@ -49,7 +49,7 @@ module Virtual
|
||||
class_for(MoveInstruction).new(value , self , :opcode => :mov)
|
||||
end
|
||||
def clazz
|
||||
BootSpace.space.get_or_create_class(:String)
|
||||
Space.space.get_or_create_class(:String)
|
||||
end
|
||||
def layout
|
||||
Virtual::Object.layout
|
||||
|
@@ -35,15 +35,92 @@ module Virtual
|
||||
def initialize
|
||||
@parser = Parser::Salama.new
|
||||
the_end = Halt.new
|
||||
@passes = [ "Virtual::SendImplementation" ]
|
||||
@space = Parfait::Space.new
|
||||
# @message = Message.new(the_end , the_end , :Object)
|
||||
end
|
||||
attr_reader :message
|
||||
attr_reader :message , :passes , :space
|
||||
|
||||
def run_passes
|
||||
@passes.each do |pass_class|
|
||||
blocks = [@init] + main.blocks
|
||||
@classes.values.each do |c|
|
||||
c.instance_methods.each {|f| blocks += f.blocks }
|
||||
end
|
||||
#puts "running #{pass_class}"
|
||||
all.each do |block|
|
||||
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
|
||||
|
||||
def self.boot
|
||||
machine = Machine.new
|
||||
BootSpace.space.boot_classes! # boot is a verb here
|
||||
machine.boot
|
||||
machine
|
||||
instance = self.instance
|
||||
instance.boot_classes! # boot is a verb here
|
||||
instance.boot
|
||||
instance
|
||||
end
|
||||
def self.instance
|
||||
@instance ||= Machine.new
|
||||
end
|
||||
|
||||
# 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)
|
||||
def boot_classes!
|
||||
# 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
|
||||
obj = get_or_create_class :Object
|
||||
[:index_of , :_get_instance_variable , :_set_instance_variable].each do |f|
|
||||
obj.add_instance_method Builtin::Object.send(f , nil)
|
||||
end
|
||||
obj = get_or_create_class :Kernel
|
||||
# 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"
|
||||
@init = Virtual::Block.new(:_init_ , nil )
|
||||
@init.add_code(Register::RegisterMain.new(underscore_init))
|
||||
obj = get_or_create_class :Integer
|
||||
[:putint,:fibo].each do |f|
|
||||
obj.add_instance_method Builtin::Integer.send(f , nil)
|
||||
end
|
||||
obj = get_or_create_class :String
|
||||
[:get , :set , :puts].each do |f|
|
||||
obj.add_instance_method Builtin::String.send(f , nil)
|
||||
end
|
||||
obj = get_or_create_class :Array
|
||||
[:get , :set , :push].each do |f|
|
||||
obj.add_instance_method Builtin::Array.send(f , nil)
|
||||
end
|
||||
end
|
||||
|
||||
def boot
|
||||
|
@@ -26,7 +26,7 @@ module Virtual
|
||||
# Object Object
|
||||
# BootClass Class
|
||||
# MetaClass self/Object
|
||||
# BootSpace ObjectSpace
|
||||
# Space ObjectSpace
|
||||
# CompiledMethod Function
|
||||
# (ruby)Array Array
|
||||
# String String
|
||||
@@ -68,7 +68,7 @@ module Virtual
|
||||
@@HASH.merge :keys => object.keys , :values => object.values
|
||||
when Virtual::BootClass
|
||||
@@CLAZZ
|
||||
when Virtual::BootSpace
|
||||
when Virtual::Space
|
||||
@@SPACE
|
||||
else
|
||||
raise "linker encounters unknown class #{object.class}"
|
||||
|
@@ -13,5 +13,5 @@ module Virtual
|
||||
end
|
||||
end
|
||||
end
|
||||
Virtual::BootSpace.space.add_pass "Virtual::EnterImplementation"
|
||||
Virtual::Machine.instance.add_pass "Virtual::EnterImplementation"
|
||||
end
|
||||
|
@@ -23,7 +23,7 @@ module Virtual
|
||||
else
|
||||
next
|
||||
end
|
||||
space = BootSpace.space
|
||||
space = Space.space
|
||||
slot = Virtual::Slot
|
||||
# a place to store a reference to the space, we grab the next_frame from the space
|
||||
space_tmp = Register::RegisterReference.new(Virtual::Message::TMP_REG)
|
||||
@@ -46,5 +46,5 @@ module Virtual
|
||||
end
|
||||
end
|
||||
end
|
||||
Virtual::BootSpace.space.add_pass "Virtual::FrameImplementation"
|
||||
Virtual::Machine.instance.add_pass "Virtual::FrameImplementation"
|
||||
end
|
||||
|
@@ -11,5 +11,5 @@ module Virtual
|
||||
end
|
||||
end
|
||||
end
|
||||
Virtual::BootSpace.space.add_pass "Virtual::GetImplementation"
|
||||
Virtual::Machine.instance.add_pass "Virtual::GetImplementation"
|
||||
end
|
||||
|
@@ -26,7 +26,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::BootSpace.space.get_or_create_class(:Kernel)
|
||||
kernel = Virtual::Space.space.get_or_create_class(:Kernel)
|
||||
method = kernel.get_instance_method(:__send)
|
||||
new_codes << MethodCall.new( method )
|
||||
raise "unimplemented #{code}"
|
||||
|
Reference in New Issue
Block a user