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-22 21:51:36 +02:00
|
|
|
values = [ "Value" , "Integer" , "Kernel" , "Object"].collect {|cl| Virtual.new_word(cl) }
|
2015-05-24 12:53:49 +02:00
|
|
|
value_classes = values.collect { |cl| @space.create_class(cl,nil) }
|
2015-05-23 11:15:06 +02:00
|
|
|
layouts = { "Word" => [] ,
|
2015-05-24 12:31:33 +02:00
|
|
|
"List" => [] ,
|
2015-05-24 14:06:35 +02:00
|
|
|
"Message" => [],
|
2015-05-30 10:55:46 +02:00
|
|
|
"BinaryCode" => [],
|
2015-05-24 14:06:35 +02:00
|
|
|
"Space" => ["classes","objects","frames","messages","next_message","next_frame"],
|
|
|
|
"Frame" => ["locals" , "tmps" ],
|
2015-05-23 11:15:06 +02:00
|
|
|
"Layout" => ["object_class"] ,
|
|
|
|
"Class" => ["object_layout"],
|
2015-05-24 12:31:33 +02:00
|
|
|
"Dictionary" => ["keys" , "values"] ,
|
2015-05-24 15:24:57 +02:00
|
|
|
"Method" => ["name" , "code" ,"arg_names" , "locals" , "tmps"] ,
|
2015-05-24 12:31:33 +02:00
|
|
|
"Module" => ["name" , "instance_methods", "super_class", "meta_class"]
|
|
|
|
}
|
2015-05-23 11:15:06 +02:00
|
|
|
layouts.each do |name , layout|
|
2015-05-24 12:53:49 +02:00
|
|
|
class_mappings[name] = @space.create_class(Virtual.new_word(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-31 10:07:49 +02:00
|
|
|
supers = { "BinaryCode" => "Word", "Layout" => "List", "Class" => "Module"}
|
|
|
|
supers.each do |clazz , superclaszz| # set_super_class has no sideeffects, so setting twice ok
|
|
|
|
class_mappings[clazz].set_super_class class_mappings[superclaszz]
|
|
|
|
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-23 11:15:06 +02:00
|
|
|
clazz.object_layout.add_instance_variable Virtual.new_word(var_name)
|
2015-05-22 21:51:36 +02:00
|
|
|
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-24 12:31:33 +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
|
|
|
# add space and instances which get created before the objects list
|
2015-05-30 10:55:46 +02:00
|
|
|
[@space,@space.classes,@space.classes.keys, @space.classes.values,@space.objects].each do |o|
|
|
|
|
@space.add_object o
|
|
|
|
end
|
2015-05-24 18:59:19 +02:00
|
|
|
@space.late_init
|
2015-05-31 10:07:49 +02:00
|
|
|
values.each {|v| v.init_layout }
|
|
|
|
|
2015-05-22 21:51:36 +02:00
|
|
|
# now update the layout on all objects created so far,
|
|
|
|
# go through objects in space
|
|
|
|
@space.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-31 10:07:49 +02:00
|
|
|
@space.double_check
|
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
|
|
|
|
# TODO: go through the virtual parfait layer and adjust function names to what they really are
|
2015-05-24 12:31:33 +02:00
|
|
|
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-24 12:31:33 +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-24 12:31:33 +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
|
|
|
|
|
|
|
# and the @init block in turn _jumps_ to __init__
|
|
|
|
# the point of which is that by the time main executes, all is "normal"
|
|
|
|
@init = Block.new(:_init_ , nil )
|
|
|
|
@init.add_code(Register::RegisterMain.new(underscore_init))
|
2015-05-19 19:29:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|