2014-05-15 15:54:23 +02:00
|
|
|
require_relative "context"
|
2014-05-31 13:35:33 +02:00
|
|
|
require_relative "boot_class"
|
2014-05-13 20:15:02 +02:00
|
|
|
require_relative "call_site"
|
2014-05-03 14:13:44 +02:00
|
|
|
require "arm/arm_machine"
|
2014-05-15 15:54:23 +02:00
|
|
|
require "core/kernel"
|
2014-06-03 13:49:02 +02:00
|
|
|
require "boot/object"
|
2014-06-05 17:17:00 +02:00
|
|
|
require "boot/string"
|
2014-05-03 14:13:44 +02:00
|
|
|
|
|
|
|
module Vm
|
2014-05-31 11:52:29 +02:00
|
|
|
# The BootSpace is contains all objects for a program. In functional terms it is a program, but on oo
|
|
|
|
# it is a collection of objects, some of which are data, some classes, some functions
|
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
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# throwing in a context for unspecified use (well one is to pass the programm/globals around)
|
|
|
|
|
2014-05-31 11:52:29 +02:00
|
|
|
class BootSpace < Code
|
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-05-21 18:43:46 +02:00
|
|
|
# with a XXXMachine in it that derives from Vm::RegisterMachine
|
2014-05-08 18:30:46 +02:00
|
|
|
def initialize machine = nil
|
2014-05-31 11:38:15 +02:00
|
|
|
super()
|
2014-05-08 18:30:46 +02:00
|
|
|
machine = RbConfig::CONFIG["host_cpu"] unless machine
|
|
|
|
machine = "intel" if machine == "x86_64"
|
|
|
|
machine = machine.capitalize
|
2014-05-21 18:43:46 +02:00
|
|
|
RegisterMachine.instance = eval("#{machine}::#{machine}Machine").new
|
2014-05-31 13:35:33 +02:00
|
|
|
@classes = {}
|
2014-05-03 14:13:44 +02:00
|
|
|
@context = Context.new(self)
|
2014-05-31 13:35:33 +02:00
|
|
|
@context.current_class = get_or_create_class :Object
|
2014-06-10 23:38:46 +02:00
|
|
|
@main = Function.new("main")
|
|
|
|
@context.function = @main
|
2014-05-05 10:03:43 +02:00
|
|
|
#global objects (data)
|
2014-05-03 14:13:44 +02:00
|
|
|
@objects = []
|
2014-06-10 23:38:46 +02:00
|
|
|
@entry = RegisterMachine.instance.main_start @context
|
2014-05-05 10:03:43 +02:00
|
|
|
#main gets executed between entry and exit
|
2014-06-10 23:38:46 +02:00
|
|
|
@exit = RegisterMachine.instance.main_exit @context
|
2014-06-03 13:49:02 +02:00
|
|
|
boot_classes
|
2014-06-11 20:36:22 +02:00
|
|
|
@passes = [ MoveMoveReduction.new , LogicMoveReduction.new, NoopReduction.new, SaveLocals.new ]
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
2014-05-31 13:35:33 +02:00
|
|
|
attr_reader :context , :main , :classes , :entry , :exit
|
2014-06-11 10:41:50 +02:00
|
|
|
|
|
|
|
def run_passes
|
|
|
|
@passes.each do |pass|
|
|
|
|
all = main.blocks
|
|
|
|
@classes.each_value do |c|
|
|
|
|
c.functions.each {|f| all += f.blocks }
|
|
|
|
end
|
|
|
|
all.each do |block|
|
|
|
|
pass.run(block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
|
|
|
# Functions are grabbed from respective modules by sending the sunction name. This should return the
|
|
|
|
# implementation of the function (ie a function 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
|
|
|
|
[:index_of , :_get_instance_variable].each do |f|
|
2014-06-03 21:16:57 +02:00
|
|
|
puts "adding #{f}"
|
2014-06-03 13:49:02 +02:00
|
|
|
obj.add_function Boot::Object.send(f , @context)
|
|
|
|
end
|
2014-06-05 17:17:00 +02:00
|
|
|
obj = get_or_create_class :String
|
|
|
|
[:get , :set].each do |f|
|
|
|
|
puts "adding #{f}"
|
|
|
|
obj.add_function Boot::String.send(f , @context)
|
|
|
|
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-05-31 11:52:29 +02:00
|
|
|
raise "must be derived from Code #{o.inspect}" unless o.is_a? 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
|
|
|
|
|
|
|
# this is the way to instantiate classes (not BootBlass.new)
|
|
|
|
# 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
|
|
|
|
c = BootClass.new(name,@context)
|
|
|
|
@classes[name] = c
|
|
|
|
end
|
|
|
|
c
|
2014-05-31 11:38:15 +02:00
|
|
|
end
|
|
|
|
|
2014-06-11 20:36:22 +02:00
|
|
|
# linking entry , exit , main , classes , objects
|
2014-05-05 10:03:43 +02:00
|
|
|
def link_at( start , context)
|
2014-05-31 13:35:33 +02:00
|
|
|
super
|
2014-05-05 10:03:43 +02:00
|
|
|
@entry.link_at( start , context )
|
|
|
|
start += @entry.length
|
2014-05-05 14:59:29 +02:00
|
|
|
@exit.link_at( start , context)
|
|
|
|
start += @exit.length
|
2014-06-11 20:36:22 +02:00
|
|
|
@main.link_at( start , context )
|
|
|
|
start += @main.length
|
2014-05-31 13:35:33 +02:00
|
|
|
@classes.values.each do |clazz|
|
|
|
|
clazz.link_at(start , context)
|
|
|
|
start += clazz.length
|
2014-05-05 08:35:40 +02:00
|
|
|
end
|
|
|
|
@objects.each do |o|
|
2014-05-05 10:03:43 +02:00
|
|
|
o.link_at(start , context)
|
|
|
|
start += o.length
|
2014-05-05 08:35:40 +02:00
|
|
|
end
|
2014-05-05 14:59:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# assemble in the same order as linked
|
|
|
|
def assemble( io )
|
2014-05-18 11:34:53 +02:00
|
|
|
link_at( @position , nil) #second link in case of forward declarations
|
2014-05-05 14:59:29 +02:00
|
|
|
@entry.assemble( io )
|
|
|
|
@exit.assemble( io )
|
2014-06-11 20:36:22 +02:00
|
|
|
@main.assemble( io )
|
2014-05-31 13:35:33 +02:00
|
|
|
@classes.values.each do |clazz|
|
|
|
|
clazz.assemble(io)
|
2014-05-05 14:59:29 +02:00
|
|
|
end
|
|
|
|
@objects.each do |o|
|
|
|
|
o.assemble(io)
|
|
|
|
end
|
|
|
|
io
|
2014-05-05 08:35:40 +02:00
|
|
|
end
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
end
|