copied old boot for later reference
This commit is contained in:
parent
c505db490f
commit
842c4e3044
73
lib/old_boot/boot_class.rb
Normal file
73
lib/old_boot/boot_class.rb
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
require_relative "meta_class"
|
||||||
|
|
||||||
|
module Boot
|
||||||
|
# class is mainly a list of functions with a name (for now)
|
||||||
|
# layout of object is seperated into Layout
|
||||||
|
class BootClass < Virtual::ObjectConstant
|
||||||
|
def initialize name , context , super_class = :Object
|
||||||
|
super()
|
||||||
|
@context = context
|
||||||
|
# class functions
|
||||||
|
@functions = []
|
||||||
|
@name = name.to_sym
|
||||||
|
@super_class = super_class
|
||||||
|
@meta_class = MetaClass.new(self)
|
||||||
|
end
|
||||||
|
attr_reader :name , :functions , :meta_class , :context , :super_class
|
||||||
|
|
||||||
|
def add_function function
|
||||||
|
raise "not a function #{function}" unless function.is_a? Virtual::Function
|
||||||
|
raise "syserr " unless function.name.is_a? Symbol
|
||||||
|
@functions << function
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_function fname
|
||||||
|
fname = fname.to_sym
|
||||||
|
f = @functions.detect{ |f| f.name == fname }
|
||||||
|
names = @functions.collect{|f| f.name }
|
||||||
|
f
|
||||||
|
end
|
||||||
|
|
||||||
|
# get the function and if not found, try superclasses. raise error if not found
|
||||||
|
def resolve_function name
|
||||||
|
fun = get_function name
|
||||||
|
unless fun or name == :Object
|
||||||
|
supr = @context.object_space.get_or_create_class(@super_class)
|
||||||
|
fun = supr.get_function name
|
||||||
|
puts "#{supr.functions.collect(&:name)} for #{name} GOT #{fun.class}" if name == :index_of
|
||||||
|
end
|
||||||
|
raise "Method not found :#{name}, for #{inspect}" unless fun
|
||||||
|
fun
|
||||||
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"BootClass #{@name} < #{@super_class}:#{@functions.collect(&:name)}"
|
||||||
|
end
|
||||||
|
def to_s
|
||||||
|
inspect
|
||||||
|
end
|
||||||
|
# Code interface follows. Note position is inheitted as is from Code
|
||||||
|
|
||||||
|
# length of the class is the length of it's functions
|
||||||
|
def length
|
||||||
|
@functions.inject(0) {| sum , item | sum + item.length}
|
||||||
|
end
|
||||||
|
|
||||||
|
# linking functions
|
||||||
|
def link_at( start , context)
|
||||||
|
super
|
||||||
|
@functions.each do |function|
|
||||||
|
function.link_at(start , context)
|
||||||
|
start += function.length
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# assemble functions
|
||||||
|
def assemble( io )
|
||||||
|
@functions.each do |function|
|
||||||
|
function.assemble(io)
|
||||||
|
end
|
||||||
|
io
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
0
lib/old_boot/boot_memory.rb
Normal file
0
lib/old_boot/boot_memory.rb
Normal file
134
lib/old_boot/boot_space.rb
Normal file
134
lib/old_boot/boot_space.rb
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
require "vm/context"
|
||||||
|
require "boot/boot_class"
|
||||||
|
require "vm/call_site"
|
||||||
|
require "arm/arm_machine"
|
||||||
|
require "kernel/all"
|
||||||
|
require "boot/object"
|
||||||
|
require "boot/string"
|
||||||
|
|
||||||
|
module Boot
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
class BootSpace < Virtual::Code
|
||||||
|
|
||||||
|
# Initialize with a string for cpu. Naming conventions are: for Machine XXX there exists a module XXX
|
||||||
|
# with a XXXMachine in it that derives from Virtual::RegisterMachine
|
||||||
|
def initialize machine = nil
|
||||||
|
super()
|
||||||
|
machine = RbConfig::CONFIG["host_cpu"] unless machine
|
||||||
|
machine = "intel" if machine == "x86_64"
|
||||||
|
machine = machine.capitalize
|
||||||
|
Virtual::RegisterMachine.instance = eval("#{machine}::#{machine}Machine").new
|
||||||
|
@classes = {}
|
||||||
|
@context = Virtual::Context.new(self)
|
||||||
|
@context.current_class = get_or_create_class :Object
|
||||||
|
@main = Virtual::Function.new("main")
|
||||||
|
@context.function = @main
|
||||||
|
#global objects (data)
|
||||||
|
@objects = []
|
||||||
|
@entry = Virtual::RegisterMachine.instance.main_start @context
|
||||||
|
#main gets executed between entry and exit
|
||||||
|
@exit = Virtual::RegisterMachine.instance.main_exit @context
|
||||||
|
boot_classes
|
||||||
|
@passes = [ Virtual::MoveMoveReduction.new , Virtual::LogicMoveReduction.new, Virtual::NoopReduction.new, Virtual::SaveLocals.new ]
|
||||||
|
end
|
||||||
|
attr_reader :context , :main , :classes , :entry , :exit
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
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 , :_set_instance_variable].each do |f|
|
||||||
|
#puts "Boot Object::#{f}"
|
||||||
|
obj.add_function Boot::Object.send(f , @context)
|
||||||
|
end
|
||||||
|
[:utoa, :putstring,:putint,:fibo,:exit].each do |f|
|
||||||
|
#puts "Boot Kernel::#{f}"
|
||||||
|
obj.add_function Crystal::Kernel.send(f , @context)
|
||||||
|
end
|
||||||
|
obj = get_or_create_class :String
|
||||||
|
[:get , :set].each do |f|
|
||||||
|
#puts "Boot String::#{f}"
|
||||||
|
obj.add_function Boot::String.send(f , @context)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Objects are data and get assembled after functions
|
||||||
|
def add_object o
|
||||||
|
return if @objects.include? o
|
||||||
|
raise "must be derived from Code #{o.inspect}" unless o.is_a? Virtual::Code
|
||||||
|
@objects << o # TODO check type , no basic values allowed (must be wrapped)
|
||||||
|
end
|
||||||
|
|
||||||
|
# this is the way to instantiate classes (not BootBlass.new)
|
||||||
|
# so we get and keep exactly one per name
|
||||||
|
def get_or_create_class name
|
||||||
|
raise "uups #{name}.#{name.class}" unless name.is_a? Symbol
|
||||||
|
c = @classes[name]
|
||||||
|
unless c
|
||||||
|
c = BootClass.new(name,@context)
|
||||||
|
@classes[name] = c
|
||||||
|
end
|
||||||
|
c
|
||||||
|
end
|
||||||
|
|
||||||
|
# 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
|
||||||
|
end
|
||||||
|
end
|
55
lib/old_boot/meta_class.rb
Normal file
55
lib/old_boot/meta_class.rb
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
module Boot
|
||||||
|
# class that acts like a class, but is really the object
|
||||||
|
|
||||||
|
# described in the ruby language book as the eigenclass, what you get with
|
||||||
|
# class MyClass
|
||||||
|
# class << self <--- this is called the eigenclass, or metaclass, and really is just the class object
|
||||||
|
# .... but gives us the ability to use the syntax as if it were a class
|
||||||
|
# PS: can't say i fancy the << self syntax and am considerernig adding a keyword for it, like meta
|
||||||
|
# In effect it is a very similar construct to def self.function(...)
|
||||||
|
# So one could write def meta.function(...) and thus define on the meta-class
|
||||||
|
class MetaClass < Virtual::ObjectConstant
|
||||||
|
# no name, nor nothing. as this is just the object really
|
||||||
|
|
||||||
|
def initialize(object)
|
||||||
|
super()
|
||||||
|
@functions = []
|
||||||
|
@me_self = object
|
||||||
|
end
|
||||||
|
|
||||||
|
# in a non-booting version this should map to _add_singleton_method
|
||||||
|
def add_function function
|
||||||
|
raise "not a function #{function}" unless function.is_a? Virtual::Function
|
||||||
|
raise "syserr " unless function.name.is_a? Symbol
|
||||||
|
@functions << function
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_function name
|
||||||
|
name = name.to_sym
|
||||||
|
f = @functions.detect{ |f| f.name == name }
|
||||||
|
return f if f
|
||||||
|
if( @me_self == :Object )
|
||||||
|
puts "no function for :#{name} in Meta #{@me_self.inspect}"
|
||||||
|
return nil
|
||||||
|
else #recurse up class hierachy unless we're at Object
|
||||||
|
return @me_self.context.object_space.get_or_create_class(@me_self.super_class).get_function name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# get the function and if not found, try superclasses. raise error if not found
|
||||||
|
def resolve_function name
|
||||||
|
fun = get_function name
|
||||||
|
# TODO THE BOOK says is class A derives from B , then the metaclass of A derives from the metaclass of B
|
||||||
|
# just get to it ! (and stop whimpering)
|
||||||
|
raise "Method not found #{name} , for #{inspect}" unless fun
|
||||||
|
fun
|
||||||
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"MetaClass on #{@me_self}, #{@functions.length} functions"
|
||||||
|
end
|
||||||
|
def to_s
|
||||||
|
inspect
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
74
lib/old_boot/object.rb
Normal file
74
lib/old_boot/object.rb
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
module Boot
|
||||||
|
class Object
|
||||||
|
module ClassMethods
|
||||||
|
|
||||||
|
# return the index of the variable. Now "normal" code can't really do anything with that, but
|
||||||
|
# set/get instance variable use it.
|
||||||
|
# This is just a placeholder, as we code this in ruby, but the instance methods need the definition before.
|
||||||
|
def index_of context , name = Virtual::Integer
|
||||||
|
index_function = Virtual::Function.new(:index_of , Virtual::Reference , [Virtual::Reference] , Virtual::Integer )
|
||||||
|
return index_function
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.layout
|
||||||
|
layout_function = Virtual::Function.new(:layout , Virtual::Reference , [ ] , Virtual::Reference )
|
||||||
|
layout_function.at_index 2
|
||||||
|
layout_function
|
||||||
|
end
|
||||||
|
|
||||||
|
# in ruby, how this goes is
|
||||||
|
# def _get_instance_variable var
|
||||||
|
# i = self.index_of(var)
|
||||||
|
# return at_index(i)
|
||||||
|
# end
|
||||||
|
# The at_index is just "below" the api, something we need but don't want to expose, so we can't code the above in ruby
|
||||||
|
def _get_instance_variable context , name = Virtual::Integer
|
||||||
|
get_function = Virtual::Function.new(:_get_instance_variable , Virtual::Reference , [ Virtual::Reference ] , Virtual::Mystery )
|
||||||
|
me = get_function.receiver
|
||||||
|
var_name = get_function.args.first
|
||||||
|
return_to = get_function.return_type
|
||||||
|
|
||||||
|
index_function = context.object_space.get_or_create_class(:Object).resolve_function(:index_of)
|
||||||
|
get_function.push( [me] )
|
||||||
|
index = get_function.call( index_function )
|
||||||
|
|
||||||
|
after_body = get_function.new_block("after_index")
|
||||||
|
get_function.insert_at after_body
|
||||||
|
|
||||||
|
get_function.pop([me])
|
||||||
|
return_to.at_index( get_function , me , return_to )
|
||||||
|
|
||||||
|
get_function.set_return return_to
|
||||||
|
return get_function
|
||||||
|
end
|
||||||
|
|
||||||
|
def _set_instance_variable(context , name = Virtual::Integer , value = Virtual::Integer )
|
||||||
|
set_function = Virtual::Function.new(:_set_instance_variable , Virtual::Reference ,[Virtual::Reference ,Virtual::Reference], Virtual::Mystery )
|
||||||
|
me = set_function.receiver
|
||||||
|
var_name = set_function.args.first
|
||||||
|
return_to = set_function.return_type
|
||||||
|
index_function = context.object_space.get_or_create_class(:Object).resolve_function(:index_of)
|
||||||
|
set_function.push( [me] )
|
||||||
|
set_function.call( index_function )
|
||||||
|
after_body = set_function.new_block("after_index")
|
||||||
|
|
||||||
|
set_function.insert_at after_body
|
||||||
|
set_function.pop([me])
|
||||||
|
return_to.at_index( set_function , me , return_to )
|
||||||
|
set_function.set_return return_to
|
||||||
|
return set_function
|
||||||
|
end
|
||||||
|
|
||||||
|
def _get_singleton_method(context , name )
|
||||||
|
raise name
|
||||||
|
end
|
||||||
|
def _add_singleton_method(context, method)
|
||||||
|
raise "4"
|
||||||
|
end
|
||||||
|
def initialize(context)
|
||||||
|
raise "4"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
extend ClassMethods
|
||||||
|
end
|
||||||
|
end
|
15
lib/old_boot/string.rb
Normal file
15
lib/old_boot/string.rb
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
module Boot
|
||||||
|
class String
|
||||||
|
module ClassMethods
|
||||||
|
def get context , index = Virtual::Integer
|
||||||
|
get_function = Virtual::Function.new(:get , Virtual::Integer , [ Virtual::Integer] , Virtual::Integer )
|
||||||
|
return get_function
|
||||||
|
end
|
||||||
|
def set context , index = Virtual::Integer , char = Virtual::Integer
|
||||||
|
set_function = Virtual::Function.new(:set , Virtual::Integer ,[Virtual::Integer, Virtual::Integer] , Virtual::Integer )
|
||||||
|
return set_function
|
||||||
|
end
|
||||||
|
end
|
||||||
|
extend ClassMethods
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user