let the BootClasses carry functions, ie one level squeezed between object_space and funtion, the class is coming to life

This commit is contained in:
Torsten Ruger 2014-05-31 14:35:33 +03:00
parent 3713d08748
commit a6f02d6be3
10 changed files with 99 additions and 65 deletions

View File

@ -8,7 +8,7 @@ module Ast
end
def compile context , into
params = args.collect{ |a| a.compile(context, into) }
function = context.object_space.get_or_create_function(name)
function = context.current_class.get_or_create_function(name)
raise "Forward declaration not implemented (#{name}) #{inspect}" if function == nil
call = Vm::CallSite.new( name , params , function)
current_function = context.function

View File

@ -27,7 +27,7 @@ module Ast
args << arg_value
end
function = Vm::Function.new(name , args )
context.object_space.add_function function
context.current_class.add_function function
parent_locals = context.locals
parent_function = context.function

View File

@ -15,25 +15,18 @@ module Ast
[:name , :expressions]
end
def compile context , into
# create the class or module
clazz = context.object_space.get_or_create_class name
puts "Class #{clazz.inspect}"
context.class = clazz
expressions.each do |expression|
# check if it'sa function definition and add
# if not, execute it, but that does means we should be in crystal (executable), not ruby. ie throw an error for now
clazz = context.object_space.get_or_create_class name
expression_value = expression.compile(context , into)
raise "only functions for now #{expression.inspect}" unless expression.is_a? Ast::FunctionExpression
expression_value = expression.compile(context , nil )
puts "compiled return expression #{expression_value.inspect}, now return in 7"
# copied from function expression: TODO make function
return_reg = Vm::Integer.new(7)
if expression_value.is_a?(Vm::IntegerConstant) or expression_value.is_a?(Vm::StringConstant)
return_reg.load into , expression_value if expression_value.register != return_reg.register
else
return_reg.move( into, expression_value ) if expression_value.register != return_reg.register
end
#function.set_return return_reg
return return_reg
return nil
end
end

View File

@ -38,7 +38,7 @@ module Core
context.object_space.add_object buffer # and save it (function local variable: a no no)
int = putint_function.args.first
moved_int = putint_function.new_local
utoa = context.object_space.get_or_create_function(:utoa)
utoa = context.current_class.get_or_create_function(:utoa)
b = putint_function.body
b.mov( moved_int , int ) #move arg up
#b.a buffer => int # string to write to

View File

@ -4,6 +4,11 @@ require "elf/object_writer"
require 'parser/crystal'
require 'parser/transform'
require "vm/register_machine"
require "vm/code"
require "vm/values"
require "vm/block"
require "vm/function"
require "vm/boot_class"
require "vm/boot_space"
require "stream_reader"
require "core/kernel"

View File

@ -22,7 +22,9 @@ module Elf
binary = program.assemble(StringIO.new )
blocks = []
program.functions.each {|f| blocks += f.blocks }
program.classes.values.each do |clazz|
clazz.functions.each {|f| blocks += f.blocks }
end
blocks += [program.entry , program.exit , program.main]
blocks.flatten.each do |b|
add_symbol b.name.to_s , b.position

View File

@ -1,5 +1,59 @@
module Vm
class BootClass
# class is mainly a list of functions with a name (for now)
# layout of object is seperated into Layout
class BootClass < Code
def initialize name , context
@context = context
# class functions
@functions = []
@name = name.to_sym
end
attr_reader :name , :functions
def add_function function
raise "not a function #{function}" unless function.is_a? Function
raise "syserr " unless function.name.is_a? Symbol
@functions << function
end
def get_function name
name = name.to_sym
@functions.detect{ |f| f.name == name }
end
# preferred way of creating new functions (also forward declarations, will flag unresolved later)
def get_or_create_function name
fun = get_function name
unless fun
fun = Core::Kernel.send(name , @context)
raise "no such function '#{name}'" if fun == nil
@functions << fun
end
fun
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

View File

@ -1,5 +1,5 @@
require_relative "context"
require_relative "function"
require_relative "boot_class"
require_relative "call_site"
require "arm/arm_machine"
require "core/kernel"
@ -14,8 +14,6 @@ module Vm
# 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
# in terms of variables and their visibility, things are simple. They are either local or global
# throwing in a context for unspecified use (well one is to pass the programm/globals around)
class BootSpace < Code
@ -28,19 +26,17 @@ module Vm
machine = "intel" if machine == "x86_64"
machine = machine.capitalize
RegisterMachine.instance = eval("#{machine}::#{machine}Machine").new
@classes = {}
@context = Context.new(self)
@context.current_class = get_or_create_class :Object
#global objects (data)
@objects = []
# global functions
@functions = []
@classes = []
@entry = Core::Kernel::main_start Vm::Block.new("main_entry",nil)
#main gets executed between entry and exit
@main = Block.new("main",nil)
@exit = Core::Kernel::main_exit Vm::Block.new("main_exit",nil)
end
attr_reader :context , :main , :functions , :entry , :exit
attr_reader :context , :main , :classes , :entry , :exit
def add_object o
return if @objects.include? o
@ -48,45 +44,27 @@ module Vm
@objects << o # TODO check type , no basic values allowed (must be wrapped)
end
def add_function function
raise "not a function #{function}" unless function.is_a? Function
raise "syserr " unless function.name.is_a? Symbol
@functions << function
end
def get_function name
name = name.to_sym
@functions.detect{ |f| f.name == name }
end
# preferred way of creating new functions (also forward declarations, will flag unresolved later)
def get_or_create_function name
fun = get_function name
unless fun
fun = Core::Kernel.send(name , context)
raise "no such function '#{name}'" if fun == nil
@functions << fun
end
fun
end
def get_or_create_class name
c = @classes[name]
unless c
c = BootClass.new(name,@context)
@classes[name] = c
end
c
end
# linking entry , main , exit
# functions , objects
# linking entry , main , exit , classes , objects
def link_at( start , context)
@position = start
super
@entry.link_at( start , context )
start += @entry.length
@main.link_at( start , context )
start += @main.length
@exit.link_at( start , context)
start += @exit.length
@functions.each do |function|
function.link_at(start , context)
start += function.length
@classes.values.each do |clazz|
clazz.link_at(start , context)
start += clazz.length
end
@objects.each do |o|
o.link_at(start , context)
@ -100,8 +78,8 @@ module Vm
@entry.assemble( io )
@main.assemble( io )
@exit.assemble( io )
@functions.each do |function|
function.assemble(io)
@classes.values.each do |clazz|
clazz.assemble(io)
end
@objects.each do |o|
o.assemble(io)

View File

@ -11,5 +11,7 @@ module Vm
@attributes[:object_space] = object_space
end
attr_reader :attributes
attr_accessor :current_class
end
end

View File

@ -46,12 +46,12 @@ class TestSmallProg < MiniTest::Test
# not my hand off course, found in the net from a basic introduction
def test_fibo
int = Vm::Integer.new(1)
fibo = @object_space.get_or_create_function(:fibo)
fibo = @object_space.context.current_class.get_or_create_function(:fibo)
main = @object_space.main.scope binding
main.int = 10
ret = main.call( fibo )
main.mov( :r1 , :r7 )
putint = @object_space.get_or_create_function(:putint)
putint = @object_space.context.current_class.get_or_create_function(:putint)
@object_space.main.call( putint )
@should = [0x0,0xb0,0xa0,0xe3,0xa,0x10,0xa0,0xe3,0x4,0x0,0x0,0xeb,0x7,0x10,0xa0,0xe1,0x22,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0x1,0x0,0x51,0xe3,0x1,0x70,0xa0,0xd1,0xe,0xf0,0xa0,0xd1,0x1c,0x40,0x2d,0xe9,0x1,0x30,0xa0,0xe3,0x0,0x40,0xa0,0xe3,0x2,0x20,0x41,0xe2,0x4,0x30,0x83,0xe0,0x4,0x40,0x43,0xe0,0x1,0x20,0x52,0xe2,0xfb,0xff,0xff,0x5a,0x3,0x70,0xa0,0xe1,0x1c,0x80,0xbd,0xe8,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0xa,0x30,0x42,0xe2,0x22,0x21,0x42,0xe0,0x22,0x22,0x82,0xe0,0x22,0x24,0x82,0xe0,0x22,0x28,0x82,0xe0,0xa2,0x21,0xa0,0xe1,0x2,0x41,0x82,0xe0,0x84,0x30,0x53,0xe0,0x1,0x20,0x82,0x52,0xa,0x30,0x83,0x42,0x30,0x30,0x83,0xe2,0x0,0x30,0xc1,0xe5,0x1,0x10,0x41,0xe2,0x0,0x0,0x52,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20]
write "fibo"