a lot of work to get one more test to pass

This commit is contained in:
Torsten Ruger 2014-07-12 21:59:17 +03:00
parent 842c4e3044
commit dc6cb2bb52
16 changed files with 89 additions and 153 deletions

View File

@ -4,25 +4,25 @@ module Ast
class IntegerExpression < Expression class IntegerExpression < Expression
# attr_reader :value # attr_reader :value
def compile frame def compile frame , method
Virtual::IntegerConstant.new value Virtual::IntegerConstant.new value
end end
end end
class TrueExpression class TrueExpression
def compile frame def compile frame , method
Virtual::TrueValue.new Virtual::TrueValue.new
end end
end end
class FalseExpression class FalseExpression
def compile frame def compile frame , method
Virtual::FalseValue.new Virtual::FalseValue.new
end end
end end
class NilExpression class NilExpression
def compile frame def compile frame , method
Virtual::NilValue.new Virtual::NilValue.new
end end
end end
@ -44,7 +44,7 @@ module Ast
class ModuleName < NameExpression class ModuleName < NameExpression
def compile context def compile frame , method
clazz = context.object_space.get_or_create_class name clazz = context.object_space.get_or_create_class name
raise "uups #{clazz}.#{name}" unless clazz raise "uups #{clazz}.#{name}" unless clazz
#class qualifier, means call from metaclass #class qualifier, means call from metaclass
@ -56,9 +56,9 @@ module Ast
class StringExpression < Expression class StringExpression < Expression
# attr_reader :string # attr_reader :string
def compile context def compile frame , method
value = Virtual::StringConstant.new(string) value = Virtual::StringConstant.new(string)
context.object_space.add_object value ::Virtual::Object.space.add_object value
value value
end end
end end

View File

@ -4,7 +4,7 @@ module Ast
class CallSiteExpression < Expression class CallSiteExpression < Expression
# attr_reader :name, :args , :receiver # attr_reader :name, :args , :receiver
@@counter = 0 @@counter = 0
def compile context def compile frame , method
into = context.function into = context.function
params = args.collect{ |a| a.compile(context) } params = args.collect{ |a| a.compile(context) }
puts "compiling receiver #{receiver} (call #{name})" puts "compiling receiver #{receiver} (call #{name})"

View File

@ -1,73 +1,51 @@
require_relative "meta_class" require_relative "meta_class"
module Boot module Boot
# class is mainly a list of functions with a name (for now) # class is mainly a list of methods with a name (for now)
# layout of object is seperated into Layout # layout of object is seperated into Layout
class BootClass < Virtual::ObjectConstant class BootClass < Virtual::ObjectConstant
def initialize name , context , super_class = :Object def initialize name , scope , super_class = :Object
super() super()
@context = context @scope = scope
# class functions # class methods
@functions = [] @methods = []
@name = name.to_sym @name = name.to_sym
@super_class = super_class @super_class = super_class
@meta_class = MetaClass.new(self) @meta_class = MetaClass.new(self)
end end
attr_reader :name , :functions , :meta_class , :context , :super_class attr_reader :name , :methods , :meta_class , :context , :super_class
def add_function function def add_method method
raise "not a function #{function}" unless function.is_a? Virtual::Function raise "not a method #{method}" unless method.is_a? Virtual::Method
raise "syserr " unless function.name.is_a? Symbol raise "syserr " unless method.name.is_a? Symbol
@functions << function @methods << method
end end
def get_function fname def get_method fname
fname = fname.to_sym fname = fname.to_sym
f = @functions.detect{ |f| f.name == fname } f = @methods.detect{ |f| f.name == fname }
names = @functions.collect{|f| f.name } names = @methods.collect{|f| f.name }
f f
end end
# get the function and if not found, try superclasses. raise error if not found # get the method and if not found, try superclasses. raise error if not found
def resolve_function name def resolve_method name
fun = get_function name fun = get_method name
unless fun or name == :Object unless fun or name == :Object
supr = @context.object_space.get_or_create_class(@super_class) supr = @context.object_space.get_or_create_class(@super_class)
fun = supr.get_function name fun = supr.get_method name
puts "#{supr.functions.collect(&:name)} for #{name} GOT #{fun.class}" if name == :index_of puts "#{supr.methods.collect(&:name)} for #{name} GOT #{fun.class}" if name == :index_of
end end
raise "Method not found :#{name}, for #{inspect}" unless fun raise "Method not found :#{name}, for #{inspect}" unless fun
fun fun
end end
def inspect def inspect
"BootClass #{@name} < #{@super_class}:#{@functions.collect(&:name)}" "BootClass #{@name} < #{@super_class}:#{@methods.collect(&:name)}"
end end
def to_s def to_s
inspect inspect
end 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
end end

View File

@ -1,7 +1,5 @@
require "vm/context"
require "boot/boot_class" require "boot/boot_class"
require "vm/call_site" #require "vm/call_site"
require "arm/arm_machine"
require "kernel/all" require "kernel/all"
require "boot/object" require "boot/object"
require "boot/string" require "boot/string"
@ -16,30 +14,18 @@ module Boot
# While data "ususally" would live in a .data section, we may also "inline" it into the code # 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 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
class BootSpace < Virtual::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 Virtual::RegisterMachine # with a XXXMachine in it that derives from Virtual::RegisterMachine
def initialize machine = nil def initialize machine = nil
super() 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 = {} @classes = {}
@context = Virtual::Context.new(self) @main = Virtual::Method.new("main")
@context.current_class = get_or_create_class :Object
@main = Virtual::Function.new("main")
@context.function = @main
#global objects (data) #global objects (data)
@objects = [] @objects = []
@entry = Virtual::RegisterMachine.instance.main_start @context
#main gets executed between entry and exit
@exit = Virtual::RegisterMachine.instance.main_exit @context
boot_classes boot_classes
@passes = [ Virtual::MoveMoveReduction.new , Virtual::LogicMoveReduction.new, Virtual::NoopReduction.new, Virtual::SaveLocals.new ] @passes = [ ]
end end
attr_reader :context , :main , :classes , :entry , :exit attr_reader :context , :main , :classes , :entry , :exit
@ -65,27 +51,27 @@ module Boot
obj = get_or_create_class :Object obj = get_or_create_class :Object
[:index_of , :_get_instance_variable , :_set_instance_variable].each do |f| [:index_of , :_get_instance_variable , :_set_instance_variable].each do |f|
#puts "Boot Object::#{f}" #puts "Boot Object::#{f}"
obj.add_function Boot::Object.send(f , @context) obj.add_method Boot::Object.send(f , @context)
end end
[:utoa, :putstring,:putint,:fibo,:exit].each do |f| [:putstring,:putint,:fibo,:exit].each do |f|
#puts "Boot Kernel::#{f}" #puts "Boot Kernel::#{f}"
obj.add_function Crystal::Kernel.send(f , @context) obj.add_method Crystal::Kernel.send(f , @context)
end end
obj = get_or_create_class :String obj = get_or_create_class :String
[:get , :set].each do |f| [:get , :set].each do |f|
#puts "Boot String::#{f}" #puts "Boot String::#{f}"
obj.add_function Boot::String.send(f , @context) obj.add_method Boot::String.send(f , @context)
end end
end end
# Objects are data and get assembled after functions # Objects are data and get assembled after functions
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? Virtual::Code # 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) @objects << o # TODO check type , no basic values allowed (must be wrapped)
end end
# this is the way to instantiate classes (not BootBlass.new) # this is the way to instantiate classes (not BootClass.new)
# so we get and keep exactly one per name # so we get and keep exactly one per name
def get_or_create_class name def get_or_create_class name
raise "uups #{name}.#{name.class}" unless name.is_a? Symbol raise "uups #{name}.#{name.class}" unless name.is_a? Symbol
@ -96,39 +82,5 @@ module Boot
end end
c c
end 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
end end

View File

@ -6,12 +6,12 @@ module Boot
# set/get instance variable use it. # 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. # 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 def index_of context , name = Virtual::Integer
index_function = Virtual::Function.new(:index_of , Virtual::Reference , [Virtual::Reference] , Virtual::Integer ) index_function = Virtual::Method.new(:index_of , Virtual::Reference , [Virtual::Reference] , Virtual::Integer )
return index_function return index_function
end end
def self.layout def self.layout
layout_function = Virtual::Function.new(:layout , Virtual::Reference , [ ] , Virtual::Reference ) layout_function = Virtual::Function.new(:layout , [ ] , Virtual::Reference , Virtual::Reference )
layout_function.at_index 2 layout_function.at_index 2
layout_function layout_function
end end
@ -23,7 +23,8 @@ module Boot
# end # 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 # 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 def _get_instance_variable context , name = Virtual::Integer
get_function = Virtual::Function.new(:_get_instance_variable , Virtual::Reference , [ Virtual::Reference ] , Virtual::Mystery ) get_function = Virtual::Method.new(:_get_instance_variable , [ Virtual::Reference ] , Virtual::Reference ,Virtual::Mystery )
return get_function
me = get_function.receiver me = get_function.receiver
var_name = get_function.args.first var_name = get_function.args.first
return_to = get_function.return_type return_to = get_function.return_type
@ -43,7 +44,9 @@ module Boot
end end
def _set_instance_variable(context , name = Virtual::Integer , value = Virtual::Integer ) 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 ) set_function = Virtual::Method.new(:_set_instance_variable ,[Virtual::Reference ,Virtual::Reference], Virtual::Reference ,Virtual::Mystery )
return set_function
receiver set_function
me = set_function.receiver me = set_function.receiver
var_name = set_function.args.first var_name = set_function.args.first
return_to = set_function.return_type return_to = set_function.return_type

View File

@ -2,11 +2,11 @@ module Boot
class String class String
module ClassMethods module ClassMethods
def get context , index = Virtual::Integer def get context , index = Virtual::Integer
get_function = Virtual::Function.new(:get , Virtual::Integer , [ Virtual::Integer] , Virtual::Integer ) get_function = Virtual::Method.new(:get , [ Virtual::Integer] , Virtual::Integer , Virtual::Integer )
return get_function return get_function
end end
def set context , index = Virtual::Integer , char = Virtual::Integer def set context , index = Virtual::Integer , char = Virtual::Integer
set_function = Virtual::Function.new(:set , Virtual::Integer ,[Virtual::Integer, Virtual::Integer] , Virtual::Integer ) set_function = Virtual::Method.new(:set , [Virtual::Integer, Virtual::Integer] , Virtual::Integer ,Virtual::Integer )
return set_function return set_function
end end
end end

View File

@ -7,7 +7,8 @@ module Crystal
# As we write before we recurse (save a push) we write the number backwards # As we write before we recurse (save a push) we write the number backwards
# arguments: string address , integer # arguments: string address , integer
def self.utoa context def self.utoa context
utoa_function = Virtual::Function.new(:utoa , Virtual::Integer , [ Virtual::Integer ] , Virtual::Integer ) utoa_function = Virtual::Method.new(:utoa , [ Virtual::Integer ] , Virtual::Integer ,Virtual::Integer )
return utoa_function
str_addr = utoa_function.receiver str_addr = utoa_function.receiver
number = utoa_function.args.first number = utoa_function.args.first
remainder = utoa_function.new_local remainder = utoa_function.new_local
@ -24,7 +25,8 @@ module Crystal
end end
def self.putint context def self.putint context
putint_function = Virtual::Function.new(:putint , Virtual::Integer , [] , Virtual::Integer ) putint_function = Virtual::Method.new(:putint , [] , Virtual::Integer ,Virtual::Integer )
return putint_function
buffer = Virtual::StringConstant.new(" ") # create a buffer buffer = Virtual::StringConstant.new(" ") # create a buffer
context.object_space.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.receiver int = putint_function.receiver
@ -50,7 +52,8 @@ module Crystal
# a hand coded version of the fibonachi numbers # a hand coded version of the fibonachi numbers
# not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html # not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html
def self.fibo context def self.fibo context
fibo_function = Virtual::Function.new(:fibo , Virtual::Integer , [] , Virtual::Integer ) fibo_function = Virtual::Method.new(:fibo , [] , Virtual::Integer ,Virtual::Integer )
return fibo_function
result = fibo_function.return_type result = fibo_function.return_type
int = fibo_function.receiver int = fibo_function.receiver

View File

@ -1,7 +1,8 @@
module Crystal module Crystal
module Kernel module Kernel
def self.putstring context def self.putstring context
function = Virtual::Function.new(:putstring , Virtual::Reference , [] ) function = Virtual::Method.new(:putstring , [] )
return function
ret = Virtual::RegisterMachine.instance.write_stdout(function) ret = Virtual::RegisterMachine.instance.write_stdout(function)
function.set_return ret function.set_return ret
function function

View File

@ -1,7 +1,8 @@
module Crystal module Crystal
module Kernel module Kernel
def self.exit context def self.exit context
function = Virtual::Function.new(:exit , Virtual::Integer , [] ) function = Virtual::Method.new(:exit , [] , Virtual::Integer)
return function
ret = Virtual::RegisterMachine.instance.exit(function) ret = Virtual::RegisterMachine.instance.exit(function)
function.set_return ret function.set_return ret
function function

View File

@ -28,28 +28,18 @@ module Virtual
class StringConstant < ObjectConstant class StringConstant < ObjectConstant
attr_reader :string attr_reader :string
# currently aligned to 4 (ie padded with 0) and off course 0 at the end def attributes
[:string]
end
def initialize str def initialize str
str = str.to_s if str.is_a? Symbol @string = str
length = str.length
# rounding up to the next 4 (always adding one for zero pad)
pad = ((length / 4 ) + 1 ) * 4 - length
raise "#{pad} #{self}" unless pad >= 1
@string = str + " " * pad
end end
def result= value def result= value
class_for(MoveInstruction).new(value , self , :opcode => :mov) class_for(MoveInstruction).new(value , self , :opcode => :mov)
end end
def inspect
# the strings length plus padding self.class.name + ".new('#{@string}')"
def length
string.length
end
# just writing the string
def assemble(io)
io << string
end end
end end

View File

@ -1,4 +1,6 @@
module Vm require_relative "value"
module Virtual
class Integer < Value class Integer < Value
def initialize def initialize

View File

@ -31,10 +31,6 @@ module Virtual
# that the the model is such that what is a variable in ruby, never ends up being just on the pysical stack. # that the the model is such that what is a variable in ruby, never ends up being just on the pysical stack.
# #
class Machine class Machine
# the main machine is the one that runs on the main thread, if it exists or receives an uncaught exception, that's it.
def self.main
@main || @main = Machine.new
end
def initialize def initialize
the_end = Halt.new the_end = Halt.new
@ -62,3 +58,4 @@ require_relative "value"
require_relative "mystery" require_relative "mystery"
require_relative "object" require_relative "object"
require_relative "constants" require_relative "constants"
require "boot/boot_space"

View File

@ -12,10 +12,12 @@ module Virtual
def Method.main def Method.main
Method.new(:main) Method.new(:main)
end end
def initialize name , args = [] def initialize name , args = [] , receiver = Virtual::Reference , return_type = Virtual::Reference
@name = name @name = name
@args = args @args = args
@locals = [] @locals = []
@receiver = receiver
@return_type = return_type
@start = MethodEnter.new @start = MethodEnter.new
@current = @start @current = @start
end end

View File

@ -1,3 +1,7 @@
module Boot
class BootSoace
end
end
module Virtual module Virtual
# our machine is made up of objects, some of which are code, some data # our machine is made up of objects, some of which are code, some data
# #
@ -21,6 +25,14 @@ module Virtual
end end
return true return true
end end
def self.space
if defined? @@space
@@space
else
@@space = ::Boot::BootSpace.new
end
end
end end
class Layout < Object class Layout < Object
@ -38,5 +50,7 @@ module Virtual
@super_class = sup @super_class = sup
end end
end end
end end
require_relative "integer"
require_relative "reference"

View File

@ -1,4 +1,6 @@
module Vm require_relative "value"
module Virtual
class Reference < Value class Reference < Value
def initialize clazz = nil def initialize clazz = nil

View File

@ -46,18 +46,9 @@ class TestBasic < MiniTest::Test
check check
end end
def test_comment
out = "# i am a comment \n"
@string_input = out.dup #NEEDS the return, which is what delimits the comment
@parse_output = out
@output = @parse_output #dont transform
check
end
def test_string def test_string
@string_input = "\"hello\"" @string_input = "\"hello\""
@parse_output = {:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]} @output = [Virtual::StringConstant.new('hello')]
@output = Ast::StringExpression.new('hello')
check check
end end