2015-05-19 19:29:33 +02:00
|
|
|
module Virtual
|
|
|
|
|
2015-05-24 12:31:33 +02:00
|
|
|
# Booting is a complicated, so it is extracted into this file, even it has only one entry point
|
|
|
|
|
2015-05-19 19:29:33 +02:00
|
|
|
class Machine
|
|
|
|
|
|
|
|
# The general idea is that compiling is creating an object graph. Functionally
|
|
|
|
# one tends to think of methods, and that is complicated enough, sure.
|
|
|
|
# but for an object system the graph includes classes and all instance variables
|
|
|
|
#
|
|
|
|
# And so we have a chicken and egg problem. At the end of the function we want to have a
|
|
|
|
# working Space object
|
|
|
|
# But that has instance variables (List and Dictionary) and off course a class.
|
|
|
|
# Or more precisely in salama, a Layout, that points to a class.
|
|
|
|
# So we need a Layout, but that has Layout and Class too. hmmm
|
|
|
|
#
|
|
|
|
# The way out is to build empty shell objects and stuff the neccessary data into them
|
|
|
|
# (not use the normal initialize way)
|
|
|
|
def boot_parfait!
|
|
|
|
@space = Parfait::Space.new_object
|
2015-05-24 12:31:33 +02:00
|
|
|
# map from the vm - class_name to the Parfait class (which carries parfait name)
|
|
|
|
class_mappings = {} #will later become instance variable
|
2015-05-19 19:29:33 +02:00
|
|
|
|
2015-05-31 17:34:18 +02:00
|
|
|
values = [ :Value , :Integer , :Kernel , :Object]
|
2015-05-24 12:53:49 +02:00
|
|
|
value_classes = values.collect { |cl| @space.create_class(cl,nil) }
|
2015-05-31 17:34:18 +02:00
|
|
|
layouts = { :Word => [] ,
|
|
|
|
:List => [] ,
|
|
|
|
:Message => [],
|
|
|
|
:MetaClass => [],
|
|
|
|
:BinaryCode => [],
|
2015-06-23 18:55:54 +02:00
|
|
|
:Space => [:classes ,:frames ,:messages ,:next_message ,:next_frame, :syscall_message],
|
2015-05-31 17:34:18 +02:00
|
|
|
:Frame => [:locals , :tmps ],
|
|
|
|
:Layout => [:object_class] ,
|
|
|
|
:Class => [:object_layout ],
|
|
|
|
:Dictionary => [:keys , :values ] ,
|
|
|
|
:Method => [:name , :code ,:arg_names , :locals , :tmps ] ,
|
|
|
|
:Module => [:name , :instance_methods , :super_class , :meta_class ]
|
2015-05-24 12:31:33 +02:00
|
|
|
}
|
2015-05-23 11:15:06 +02:00
|
|
|
layouts.each do |name , layout|
|
2015-05-31 17:34:18 +02:00
|
|
|
class_mappings[name] = @space.create_class(name , nil)
|
2015-05-23 11:15:06 +02:00
|
|
|
end
|
2015-05-24 12:31:33 +02:00
|
|
|
value_classes[1].set_super_class( value_classes[0] ) # #set superclass (value) for integer
|
2015-05-24 12:53:49 +02:00
|
|
|
value_classes[2].set_super_class( value_classes[0] ) # and kernel (TODO is module)
|
|
|
|
value_classes[3].set_super_class( value_classes[2] ) # and object (TODO hacked to kernel)
|
2015-05-23 11:15:06 +02:00
|
|
|
class_mappings.each do |name , clazz| # and the rest
|
|
|
|
clazz.set_super_class(value_classes[3]) # superclasses are object
|
2015-05-19 19:29:33 +02:00
|
|
|
end
|
2015-05-22 21:51:36 +02:00
|
|
|
# next create layouts by adding instance variable names to the layouts
|
2015-05-23 11:15:06 +02:00
|
|
|
class_mappings.each do |name , clazz|
|
|
|
|
variables = layouts[name]
|
2015-05-22 21:51:36 +02:00
|
|
|
variables.each do |var_name|
|
2015-05-31 17:34:18 +02:00
|
|
|
clazz.object_layout.add_instance_variable var_name
|
2015-05-22 21:51:36 +02:00
|
|
|
end
|
|
|
|
end
|
2015-05-31 13:45:28 +02:00
|
|
|
# superclass and layout corrections
|
2015-05-31 17:34:18 +02:00
|
|
|
supers = { :BinaryCode => :Word , :Layout => :List , :Class => :Module }
|
2015-05-31 13:45:28 +02:00
|
|
|
supers.each do |classname , superclass_name|
|
|
|
|
clazz = class_mappings[classname]
|
|
|
|
super_class = class_mappings[superclass_name]
|
|
|
|
# set_super_class has no sideeffects, so setting twice ok
|
|
|
|
clazz.set_super_class super_class
|
|
|
|
# Add superclass layout too
|
|
|
|
super_class.object_layout.each do |var|
|
|
|
|
clazz.object_layout.add_instance_variable var
|
|
|
|
end
|
|
|
|
end
|
2015-05-30 10:55:46 +02:00
|
|
|
|
2015-05-23 11:15:06 +02:00
|
|
|
# now store the classes so we can hand them out later during object creation
|
|
|
|
# this can not be done earlier, as parfait objects are all the time created and would
|
|
|
|
# lookup half created class info
|
|
|
|
# but it must be done before going through the objects (next step)
|
|
|
|
@class_mappings = class_mappings
|
2015-05-31 17:34:18 +02:00
|
|
|
class_mappings[:Integer ] = value_classes[1] #need for further booting
|
|
|
|
class_mappings[:Kernel ] = value_classes[2] #need for further booting
|
|
|
|
class_mappings[:Object ] = value_classes[3] #need for further booting
|
2015-05-23 11:15:06 +02:00
|
|
|
|
2015-05-24 18:59:19 +02:00
|
|
|
@space.late_init
|
2015-05-31 12:02:29 +02:00
|
|
|
|
|
|
|
# add_object @space
|
|
|
|
|
2015-05-31 10:07:49 +02:00
|
|
|
values.each {|v| v.init_layout }
|
2015-05-31 12:02:29 +02:00
|
|
|
|
2015-05-22 21:51:36 +02:00
|
|
|
# now update the layout on all objects created so far,
|
|
|
|
# go through objects in space
|
2015-05-31 12:02:29 +02:00
|
|
|
@objects.each do | o |
|
2015-05-23 11:15:06 +02:00
|
|
|
o.init_layout
|
2015-05-22 21:51:36 +02:00
|
|
|
end
|
2015-05-24 12:31:33 +02:00
|
|
|
boot_functions!
|
2015-05-19 19:29:33 +02:00
|
|
|
end
|
|
|
|
|
2015-05-24 12:31:33 +02:00
|
|
|
# classes have booted, now create a minimal set of functions
|
2015-05-19 19:29:33 +02:00
|
|
|
# minimal means only that which can not be coded in ruby
|
2015-05-24 12:31:33 +02:00
|
|
|
# Methods 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-19 19:29:33 +02:00
|
|
|
def boot_functions!
|
|
|
|
# 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
|
2015-05-31 17:34:18 +02:00
|
|
|
# TODO go through the virtual parfait layer and adjust function names to what they really are
|
|
|
|
obj = @class_mappings[:Object ]
|
2015-05-30 13:49:10 +02:00
|
|
|
[:main , :_get_instance_variable , :_set_instance_variable].each do |f|
|
2015-05-19 19:29:33 +02:00
|
|
|
obj.add_instance_method Builtin::Object.send(f , nil)
|
|
|
|
end
|
2015-05-31 17:34:18 +02:00
|
|
|
obj = @class_mappings[:Kernel ]
|
2015-05-26 19:17:03 +02:00
|
|
|
# create dummy main first, __init__ calls it
|
2015-05-30 13:49:10 +02:00
|
|
|
[:putstring,:exit,:__send ].each do |f|
|
2015-05-19 19:29:33 +02:00
|
|
|
obj.add_instance_method Builtin::Kernel.send(f , nil)
|
|
|
|
end
|
2015-05-26 19:17:03 +02:00
|
|
|
underscore_init = obj.add_instance_method Builtin::Kernel.send(:__init__, nil)
|
|
|
|
|
2015-05-31 17:34:18 +02:00
|
|
|
obj = @class_mappings[:Integer ]
|
2015-05-19 19:29:33 +02:00
|
|
|
[:putint,:fibo].each do |f|
|
|
|
|
obj.add_instance_method Builtin::Integer.send(f , nil)
|
|
|
|
end
|
2015-05-26 19:17:03 +02:00
|
|
|
|
2015-05-19 19:29:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|