renamed program to boot_space, as in object_space at boot time. thats the way its going

This commit is contained in:
Torsten Ruger 2014-05-31 12:52:29 +03:00
parent c9c484f353
commit 3713d08748
16 changed files with 110 additions and 27 deletions

View File

@ -58,7 +58,7 @@ module Ast
def compile context , into def compile context , into
value = Vm::StringConstant.new(string) value = Vm::StringConstant.new(string)
context.program.add_object value context.object_space.add_object value
value value
end end
def attributes def attributes

View File

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

View File

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

View File

@ -15,6 +15,13 @@ module Ast
[:name , :expressions] [:name , :expressions]
end end
def compile context , into def compile context , into
# create the class or module
# 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) expression_value = expression.compile(context , into)
puts "compiled return expression #{expression_value.inspect}, now return in 7" puts "compiled return expression #{expression_value.inspect}, now return in 7"
# copied from function expression: TODO make function # copied from function expression: TODO make function

36
lib/core/array.rb Normal file
View File

@ -0,0 +1,36 @@
# this is not a "normal" ruby file, ie it is not required by crystal
# instead it is parsed by crystal to define part of the crystal that runs
class BaseObject
def _set_instance_variable(name , value)
end
def _get_instance_variable( name )
end
def _get_singleton_method(name )
end
def _add_singleton_method(method)
end
def initialize
end
end
class Array < BaseObject
def initialize size
end
def at(index)
end
def set(index , value)
end
end

View File

@ -35,10 +35,10 @@ module Core
def putint context , arg = Vm::Integer def putint context , arg = Vm::Integer
putint_function = Vm::Function.new(:putint , [arg] , arg ) putint_function = Vm::Function.new(:putint , [arg] , arg )
buffer = Vm::StringConstant.new(" ") # create a buffer buffer = Vm::StringConstant.new(" ") # create a buffer
context.program.add_object buffer # and save it (function local variable: a no no) context.object_space.add_object buffer # and save it (function local variable: a no no)
int = putint_function.args.first int = putint_function.args.first
moved_int = putint_function.new_local moved_int = putint_function.new_local
utoa = context.program.get_or_create_function(:utoa) utoa = context.object_space.get_or_create_function(:utoa)
b = putint_function.body b = putint_function.body
b.mov( moved_int , int ) #move arg up b.mov( moved_int , int ) #move arg up
#b.a buffer => int # string to write to #b.a buffer => int # string to write to

View File

@ -4,6 +4,6 @@ require "elf/object_writer"
require 'parser/crystal' require 'parser/crystal'
require 'parser/transform' require 'parser/transform'
require "vm/register_machine" require "vm/register_machine"
require "vm/program" require "vm/boot_space"
require "stream_reader" require "stream_reader"
require "core/kernel" require "core/kernel"

View File

@ -8,7 +8,7 @@ module Elf
class ObjectWriter class ObjectWriter
def initialize(program , target) def initialize(program , target)
@object = Elf::ObjectFile.new(target) @object = Elf::ObjectFile.new(target)
@program = program @object_space = program
sym_strtab = Elf::StringTableSection.new(".strtab") sym_strtab = Elf::StringTableSection.new(".strtab")
@object.add_section sym_strtab @object.add_section sym_strtab
@symbol_table = Elf::SymbolTableSection.new(".symtab", sym_strtab) @symbol_table = Elf::SymbolTableSection.new(".symtab", sym_strtab)

5
lib/vm/boot_class.rb Normal file
View File

@ -0,0 +1,5 @@
module Vm
class BootClass
end
end

0
lib/vm/boot_memory.rb Normal file
View File

View File

@ -5,8 +5,8 @@ require "arm/arm_machine"
require "core/kernel" require "core/kernel"
module Vm module Vm
# A Program represents an executable that we want to build # The BootSpace is contains all objects for a program. In functional terms it is a program, but on oo
# it has a list of functions and (global) objects # 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 # 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 # There is a start and exit block that call main, which receives an array of strings
@ -18,7 +18,7 @@ module Vm
# throwing in a context for unspecified use (well one is to pass the programm/globals around) # throwing in a context for unspecified use (well one is to pass the programm/globals around)
class Program < Code class BootSpace < Code
# Initialize with a string for cpu. Naming conventions are: for Machine XXX there exists a module XXX # 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 Vm::RegisterMachine # with a XXXMachine in it that derives from Vm::RegisterMachine
@ -44,6 +44,7 @@ module Vm
def add_object o def add_object o
return if @objects.include? o return if @objects.include? o
raise "must be derived from Code #{o.inspect}" unless o.is_a? Code
@objects << o # TODO check type , no basic values allowed (must be wrapped) @objects << o # TODO check type , no basic values allowed (must be wrapped)
end end

View File

@ -1,14 +1,14 @@
require "support/hash_attributes" require "support/hash_attributes"
module Vm module Vm
#currently just holding the program in here so we can have global access #currently just holding the object_space in here so we can have global access
class Context class Context
# Make hash attributes to object attributes # Make hash attributes to object attributes
include Support::HashAttributes include Support::HashAttributes
def initialize program def initialize object_space
@attributes = {} @attributes = {}
@attributes[:program] = program @attributes[:object_space] = object_space
end end
attr_reader :attributes attr_reader :attributes
end end

View File

@ -9,12 +9,12 @@ require_relative 'helper'
class TestSmallProg < MiniTest::Test class TestSmallProg < MiniTest::Test
# need a code generator, for arm # need a code generator, for arm
def setup def setup
@program = Vm::Program.new "Arm" @object_space = Vm::BootSpace.new "Arm"
end end
def test_loop def test_loop
r0 = Vm::Integer.new(0) r0 = Vm::Integer.new(0)
m = @program.main.scope binding m = @object_space.main.scope binding
m.r0 = 5 #1 m.r0 = 5 #1
s = m.new_block("loop").scope binding s = m.new_block("loop").scope binding
@ -26,13 +26,13 @@ class TestSmallProg < MiniTest::Test
def test_hello def test_hello
hello = Vm::StringConstant.new "Hello Raisa\n" hello = Vm::StringConstant.new "Hello Raisa\n"
@program.add_object hello @object_space.add_object hello
# these are only here because it's a test program, usually all coding happens with values # these are only here because it's a test program, usually all coding happens with values
r0 = Vm::Integer.new(0) r0 = Vm::Integer.new(0)
r1 = Vm::Integer.new(1) r1 = Vm::Integer.new(1)
r2 = Vm::Integer.new(2) r2 = Vm::Integer.new(2)
r7 = Vm::Integer.new(7) r7 = Vm::Integer.new(7)
b = @program.main.scope binding b = @object_space.main.scope binding
b.r7 = 4 # 4 == write b.r7 = 4 # 4 == write
b.r0 = 1 # stdout b.r0 = 1 # stdout
b.r1 = hello # address of "hello Raisa" b.r1 = hello # address of "hello Raisa"
@ -46,20 +46,20 @@ class TestSmallProg < MiniTest::Test
# not my hand off course, found in the net from a basic introduction # not my hand off course, found in the net from a basic introduction
def test_fibo def test_fibo
int = Vm::Integer.new(1) int = Vm::Integer.new(1)
fibo = @program.get_or_create_function(:fibo) fibo = @object_space.get_or_create_function(:fibo)
main = @program.main.scope binding main = @object_space.main.scope binding
main.int = 10 main.int = 10
ret = main.call( fibo ) ret = main.call( fibo )
main.mov( :r1 , :r7 ) main.mov( :r1 , :r7 )
putint = @program.get_or_create_function(:putint) putint = @object_space.get_or_create_function(:putint)
@program.main.call( 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] @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" write "fibo"
end end
#helper to write the file #helper to write the file
def write name def write name
writer = Elf::ObjectWriter.new(@program , Elf::Constants::TARGET_ARM) writer = Elf::ObjectWriter.new(@object_space , Elf::Constants::TARGET_ARM)
assembly = writer.text assembly = writer.text
# use this for getting the bytes to compare to : # use this for getting the bytes to compare to :
#puts assembly #puts assembly

View File

@ -13,7 +13,7 @@ require 'parslet/convenience'
module Fragments module Fragments
# need a code generator, for arm # need a code generator, for arm
def setup def setup
@program = Vm::Program.new "Arm" @object_space = Vm::BootSpace.new "Arm"
end end
def parse def parse
@ -24,9 +24,9 @@ module Fragments
# and the last is wrapped as a main # and the last is wrapped as a main
parts.each_with_index do |part,index| parts.each_with_index do |part,index|
if index == (parts.length - 1) if index == (parts.length - 1)
expr = part.compile( @program.context , @program.main ) expr = part.compile( @object_space.context , @object_space.main )
else else
expr = part.compile( @program.context , nil ) expr = part.compile( @object_space.context , nil )
raise "should be function definition for now" unless expr.is_a? Vm::Function raise "should be function definition for now" unless expr.is_a? Vm::Function
end end
end end
@ -34,7 +34,7 @@ module Fragments
# helper to write the file # helper to write the file
def write name def write name
writer = Elf::ObjectWriter.new(@program , Elf::Constants::TARGET_ARM) writer = Elf::ObjectWriter.new(@object_space , Elf::Constants::TARGET_ARM)
assembly = writer.text assembly = writer.text
# use this for getting the bytes to compare to : # use this for getting the bytes to compare to :
#puts assembly #puts assembly

View File

@ -0,0 +1,34 @@
require_relative 'helper'
class TestClass < MiniTest::Test
include Fragments
def test_base_class
@string_input = <<HERE
class BaseObject
def _set_instance_variable(name , value)
4
end
def _get_instance_variable( name )
4
end
def _get_singleton_method(name )
4
end
def _add_singleton_method(method)
4
end
def initialize()
4
end
end
HERE
@should = [0x0,0xb0,0xa0,0xe3,0x2a,0x10,0xa0,0xe3,0x13,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,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]
@output = " 42 "
parse
write "putint"
end
end

View File

@ -20,7 +20,7 @@ class TestRunner < MiniTest::Test
def execute file def execute file
string = File.read(file) string = File.read(file)
parser = Parser::Crystal.new parser = Parser::Crystal.new
program = Vm::Program.new "Arm" object_space = Vm::Program.new "Arm"
syntax = parser.parse_with_debug(string) syntax = parser.parse_with_debug(string)
assert syntax assert syntax
parts = Parser::Transform.new.apply(syntax) parts = Parser::Transform.new.apply(syntax)