2014-08-23 23:25:15 +02:00
|
|
|
require "parfait/boot_class"
|
2014-06-13 23:19:12 +02:00
|
|
|
require "kernel/all"
|
2014-08-23 09:25:55 +02:00
|
|
|
|
2014-08-23 23:25:15 +02:00
|
|
|
module Virtual
|
2014-07-16 18:24:41 +02:00
|
|
|
# The BootSpace contains all objects for a program. In functional terms it is a program, but in oo
|
2014-05-31 11:52:29 +02:00
|
|
|
# it is a collection of objects, some of which are data, some classes, some functions
|
2014-07-12 20:59:17 +02:00
|
|
|
|
2014-05-03 14:13:44 +02:00
|
|
|
# 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
|
|
|
|
|
2014-07-16 18:24:41 +02:00
|
|
|
# While data ususally would live in a .data section, we may also "inline" it into the code
|
2014-05-03 14:13:44 +02:00
|
|
|
# in an oo system all data is represented as objects
|
|
|
|
|
2014-07-12 20:59:17 +02:00
|
|
|
class BootSpace
|
2014-05-03 14:13:44 +02:00
|
|
|
|
2014-05-03 17:51:47 +02:00
|
|
|
# Initialize with a string for cpu. Naming conventions are: for Machine XXX there exists a module XXX
|
2014-06-26 16:52:15 +02:00
|
|
|
# with a XXXMachine in it that derives from Virtual::RegisterMachine
|
2014-05-08 18:30:46 +02:00
|
|
|
def initialize machine = nil
|
2014-05-31 11:38:15 +02:00
|
|
|
super()
|
2014-05-31 13:35:33 +02:00
|
|
|
@classes = {}
|
2014-07-16 18:24:41 +02:00
|
|
|
@main = Virtual::MethodDefinition.new("main" , [] )
|
2014-05-05 10:03:43 +02:00
|
|
|
#global objects (data)
|
2014-05-03 14:13:44 +02:00
|
|
|
@objects = []
|
2014-06-03 13:49:02 +02:00
|
|
|
boot_classes
|
2014-08-23 09:25:55 +02:00
|
|
|
@passes = [ Virtual::SendImplementation ]
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
2014-08-23 09:25:55 +02:00
|
|
|
attr_reader :main , :classes , :objects
|
2014-06-11 10:41:50 +02:00
|
|
|
|
|
|
|
def run_passes
|
|
|
|
@passes.each do |pass|
|
2014-08-23 22:37:33 +02:00
|
|
|
puts "Runnning pass #{pass}"
|
2014-06-11 10:41:50 +02:00
|
|
|
all = main.blocks
|
|
|
|
@classes.each_value do |c|
|
2014-08-23 22:57:47 +02:00
|
|
|
c.instance_methods.each {|f| all += f.blocks }
|
2014-06-11 10:41:50 +02:00
|
|
|
end
|
|
|
|
all.each do |block|
|
2014-08-22 08:21:59 +02:00
|
|
|
pass.new.run(block)
|
2014-06-11 10:41:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-23 09:25:55 +02:00
|
|
|
# Passes are initiated empty and added to by anyone who want (basically)
|
|
|
|
# Even linking and assembly are passes and so there are quite a few system passes neccesary to result in a
|
|
|
|
# working binary. Other than that, this is intentionally quite flexible
|
|
|
|
|
|
|
|
def add_pass_after( pass , after)
|
|
|
|
index = @passes.index(after)
|
|
|
|
raise "No such 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
|
2014-06-11 20:36:22 +02:00
|
|
|
# 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
|
2014-07-16 18:24:41 +02:00
|
|
|
# MethodDefinitions 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)
|
2014-06-03 13:49:02 +02:00
|
|
|
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
|
2014-06-24 11:20:10 +02:00
|
|
|
[:index_of , :_get_instance_variable , :_set_instance_variable].each do |f|
|
|
|
|
#puts "Boot Object::#{f}"
|
2014-08-23 23:25:15 +02:00
|
|
|
obj.add_instance_method Salama::Object.send(f , @context)
|
2014-06-03 13:49:02 +02:00
|
|
|
end
|
2014-07-12 20:59:17 +02:00
|
|
|
[:putstring,:putint,:fibo,:exit].each do |f|
|
2014-06-24 11:20:10 +02:00
|
|
|
#puts "Boot Kernel::#{f}"
|
2014-08-23 22:57:47 +02:00
|
|
|
obj.add_instance_method Salama::Kernel.send(f , @context)
|
2014-06-13 22:41:45 +02:00
|
|
|
end
|
2014-06-05 17:17:00 +02:00
|
|
|
obj = get_or_create_class :String
|
2014-08-22 14:08:46 +02:00
|
|
|
[:get , :set , :puts].each do |f|
|
2014-06-24 11:20:10 +02:00
|
|
|
#puts "Boot String::#{f}"
|
2014-08-23 23:25:15 +02:00
|
|
|
obj.add_instance_method Salama::String.send(f , @context)
|
2014-06-05 17:17:00 +02:00
|
|
|
end
|
2014-06-03 13:49:02 +02:00
|
|
|
end
|
2014-06-11 20:36:22 +02:00
|
|
|
|
|
|
|
# Objects are data and get assembled after functions
|
2014-05-03 14:13:44 +02:00
|
|
|
def add_object o
|
2014-05-05 08:35:40 +02:00
|
|
|
return if @objects.include? o
|
2014-07-12 20:59:17 +02:00
|
|
|
# raise "must be derived from Code #{o.inspect}" unless o.is_a? Virtual::Code
|
2014-05-03 14:13:44 +02:00
|
|
|
@objects << o # TODO check type , no basic values allowed (must be wrapped)
|
|
|
|
end
|
2014-06-11 20:36:22 +02:00
|
|
|
|
2014-07-12 20:59:17 +02:00
|
|
|
# this is the way to instantiate classes (not BootClass.new)
|
2014-06-11 20:36:22 +02:00
|
|
|
# so we get and keep exactly one per name
|
2014-05-31 11:38:15 +02:00
|
|
|
def get_or_create_class name
|
2014-06-03 13:49:02 +02:00
|
|
|
raise "uups #{name}.#{name.class}" unless name.is_a? Symbol
|
2014-05-31 13:35:33 +02:00
|
|
|
c = @classes[name]
|
|
|
|
unless c
|
2014-07-14 13:06:09 +02:00
|
|
|
c = BootClass.new(name)
|
2014-05-31 13:35:33 +02:00
|
|
|
@classes[name] = c
|
|
|
|
end
|
|
|
|
c
|
2014-05-31 11:38:15 +02:00
|
|
|
end
|
2014-08-22 14:12:54 +02:00
|
|
|
|
|
|
|
# linking entry , exit , main , classes , objects
|
|
|
|
def link_at( start , context)
|
|
|
|
super
|
|
|
|
@entry.link_at( start , context )
|
|
|
|
start += @entry.length
|
|
|
|
@exit.link_at( start , context)
|
|
|
|
start += @exit.length
|
|
|
|
@main.link_at( start , context )
|
|
|
|
start += @main.length
|
|
|
|
@classes.values.each do |clazz|
|
|
|
|
clazz.link_at(start , context)
|
|
|
|
start += clazz.length
|
|
|
|
end
|
|
|
|
@objects.each do |o|
|
|
|
|
o.link_at(start , context)
|
|
|
|
start += o.length
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# assemble in the same order as linked
|
|
|
|
def assemble( io )
|
|
|
|
link_at( @position , nil) #second link in case of forward declarations
|
|
|
|
@entry.assemble( io )
|
|
|
|
@exit.assemble( io )
|
|
|
|
@main.assemble( io )
|
|
|
|
@classes.values.each do |clazz|
|
|
|
|
clazz.assemble(io)
|
|
|
|
end
|
|
|
|
@objects.each do |o|
|
|
|
|
o.assemble(io)
|
|
|
|
end
|
|
|
|
io
|
|
|
|
end
|
|
|
|
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
end
|