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" => [],
|
|
|
|
"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"] ,
|
|
|
|
"Method" => ["name" , "arg_names" , "locals" , "tmps"] ,
|
|
|
|
"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-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-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-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
|
|
|
|
# and go through the space instance variables which get created before the object list
|
2015-05-24 14:06:35 +02:00
|
|
|
@space.init_layout
|
|
|
|
@space.classes.init_layout
|
|
|
|
@space.objects.init_layout
|
|
|
|
@space.late_init
|
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-19 19:29:33 +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-24 12:31:33 +02:00
|
|
|
obj = @class_mappings["Kernel"]
|
2015-05-19 19:29:33 +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"
|
|
|
|
@init = Block.new(:_init_ , nil )
|
|
|
|
@init.add_code(Register::RegisterMain.new(underscore_init))
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|