stash old vm

moving on to getting mom to work and can’t have both
interpreter and elf broke, about 100 tests  went
This commit is contained in:
Torsten Ruger
2018-03-11 17:02:42 +05:30
parent f7aac1d1a4
commit 5fe0ba06ab
56 changed files with 80 additions and 143 deletions

View File

@ -7,10 +7,10 @@ end
require "risc/padding"
require "risc/positioned"
require "vm"
require "parfait"
require "risc/machine"
require "risc/method_compiler"
class Fixnum
def fits_u8?

View File

@ -4,14 +4,14 @@ module Risc
module CompileHelper
def self_and_int_arg(compiler , source)
me = compiler.process( Vm::Tree::KnownName.new( :self) )
me = compiler.add_known( :self )
int_arg = load_int_arg_at(compiler , source , 1 )
return me , int_arg
end
def compiler_for( type , method_name , extra_args = {})
args = {:index => :Integer}.merge( extra_args )
Vm::MethodCompiler.create_method(type , method_name , args ).init_method
Risc::MethodCompiler.create_method(type , method_name , args ).init_method
end
# Load the value

View File

@ -6,22 +6,23 @@ module Risc
include AST::Sexp
def mod4 context
compiler = Vm::MethodCompiler.create_method(:Integer,:mod4 ).init_method
compiler = Risc::MethodCompiler.create_method(:Integer,:mod4 ).init_method
return compiler.method
end
def putint context
compiler = Vm::MethodCompiler.create_method(:Integer,:putint ).init_method
compiler = Risc::MethodCompiler.create_method(:Integer,:putint ).init_method
return compiler.method
end
def div10 context
s = "div_10"
compiler = Vm::MethodCompiler.create_method(:Integer,:div10 ).init_method
me = compiler.process( Vm::Tree::KnownName.new( :self) )
tmp = compiler.process( Vm::Tree::KnownName.new( :self) )
q = compiler.process( Vm::Tree::KnownName.new( :self) )
const = compiler.process( Vm::Tree::IntegerExpression.new(1) )
compiler = Risc::MethodCompiler.create_method(:Integer,:div10 ).init_method
me = compiler.add_known( :self )
tmp = compiler.add_known( :self )
q = compiler.add_known( :self )
const = compiler.use_reg :Integer , 1
compiler.add_load_constant( s, 1 , const )
# int tmp = self >> 1
compiler.add_code Risc.op( s , ">>" , tmp , const)
# int q = self >> 2

View File

@ -6,7 +6,7 @@ module Risc
# it isn't really a function, ie it is jumped to (not called), exits and may not return
# so it is responsible for initial setup
def __init__ context
compiler = Vm::MethodCompiler.create_method(:Kernel,:__init__ )
compiler = Risc::MethodCompiler.create_method(:Kernel,:__init__ )
new_start = Risc.label("__init__ start" , "__init__" )
compiler.method.set_instructions( new_start)
compiler.set_current new_start
@ -29,7 +29,7 @@ module Risc
end
def exit context
compiler = Vm::MethodCompiler.create_method(:Kernel,:exit ).init_method
compiler = Risc::MethodCompiler.create_method(:Kernel,:exit ).init_method
emit_syscall( compiler , :exit )
return compiler.method
end

View File

@ -9,7 +9,7 @@ module Risc
# main entry point, ie __init__ calls this
# defined here as empty, to be redefined
def main context
compiler = Vm::MethodCompiler.create_method(:Space , :main ).init_method
compiler = Risc::MethodCompiler.create_method(:Space , :main ).init_method
return compiler.method
end

View File

@ -7,7 +7,7 @@ module Risc
include CompileHelper
def putstring context
compiler = Vm::MethodCompiler.create_method(:Word , :putstring ).init_method
compiler = Risc::MethodCompiler.create_method(:Word , :putstring ).init_method
compiler.add_slot_to_reg( "putstring" , :message , :receiver , :new_message )
index = Parfait::Word.get_length_index
reg = RiscValue.new(:r2 , :Integer)

View File

@ -1,69 +1,10 @@
require_relative "method_compiler/assignment"
require_relative "method_compiler/basic_values"
require_relative "method_compiler/call_site"
require_relative "method_compiler/collections"
require_relative "method_compiler/field_access"
require_relative "method_compiler/if_statement"
require_relative "method_compiler/name_expression"
require_relative "method_compiler/operator_expression"
require_relative "method_compiler/return_statement"
require_relative "method_compiler/statement_list"
require_relative "method_compiler/while_statement"
module Risc
module Vm
CompilerModules = [ "assignment" , "basic_values" , "call_site",
"collections" , "field_access",
"if_statement" , "name_expression" ,
"operator_expression" , "return_statement", "statement_list",
"while_statement"]
CompilerModules.each do |mod|
# require_relative "method_compiler/" + mod
end
# Compiling is the conversion of the AST into 2 things:
# - code (ie sequences of Instructions inside Methods)
# - an object graph containing all the Methods, their classes and Constants
#
# Some compile methods just add code, some may add Instructions while
# others instantiate Class and TypedMethod objects
#
# Everything in ruby is an statement, ie returns a value. So the effect of every compile
# is that a value is put into the ReturnSlot of the current Message.
# The compile method (so every compile method) returns the value that it deposits.
#
# The process uses a visitor pattern (from AST::Processor) to dispatch according to the
# type the statement. So a s(:if xx) will become an on_if(node) call.
# This makes the dispatch extensible, ie Expressions may be added by external code,
# as long as matching compile methods are supplied too.
#
# A compiler can also be used to generate code for a method without AST nodes. In the same way
# compile methods do, ie adding Instructions etc. In this way code may be generated that
# has no code equivalent.
#
# The Compiler also keeps a list of used registers, from which one may take to use and return to
# when done. The list may be reset.
#
# The Compiler also carries method and class instance variables. The method is where code is
# added to (with add_code). To be more precise, the @current instruction is where code is added
# to, and that may be changed with set_current
# All Statements reset the registers and return nil.
# Expressions use registers and return the register where their value is stored.
# Helper function to create a new compiler and compie the statement(s)
# Statement must be and AST::Node as generated by s expressions
def self.compile_ast( statement )
compiler = MethodCompiler.new(:main)
code = Vm.ast_to_code statement
compiler.process code
end
# MethodCompiler (old name) is used to generate risc instructions for methods
# and to instantiate the methods correctly. Most of the init is typed layer stuff,
# but there is some logic too.
class MethodCompiler
CompilerModules.each do |mod|
include Vm.const_get( mod.camelize )
end
def initialize( method )
@regs = []
@ -79,31 +20,6 @@ module Vm
end
attr_reader :type , :method
# Dispatches `code` according to it's class name, for class NameExpression
# a method named `on_NameExpression` is invoked with one argument, the `code`
#
# @param [Vm::Code, nil] code
def process(code)
name = code.class.name.split("::").last
# Invoke a specific handler
on_handler = :"on_#{name}"
if respond_to? on_handler
return send on_handler, code
else
raise "No handler on_#{name}(code) #{code.inspect}"
end
end
# {#process}es each code from `codes` and returns an array of
# results.
#
def process_all(codes)
codes.to_a.map do |code|
process code
end
end
# create the method, do some checks and set it as the current method to be added to
# class_name and method_name are pretty clear, args are given as a ruby array
def self.create_method( class_name , method_name , args = {})
@ -148,6 +64,26 @@ module Vm
self
end
def add_known(name)
case name
when :self
ret = use_reg @type
add_slot_to_reg(" load self" , :message , :receiver , ret )
return ret
when :space
space = Parfait.object_space
reg = use_reg :Space , space
add_load_constant( "load space", space , reg )
return reg
when :message
reg = use_reg :Message
add_transfer( "load message", Risc.message_reg , reg )
return reg
else
raise "Unknow expression #{name}"
end
end
# set the insertion point (where code is added with add_code)
def set_current c
@current = c

View File

@ -1,57 +0,0 @@
### Compiling
The typed syntax tree is created by the ruby compiler.
The code in this directory compiles the typed tree to the register machine code, and
Parfait object structure.
If this were an interpreter, we would just walk the tree and do what it says.
Since it's not things are a little more difficult, especially in time.
When compiling we deal with two times, compile-time and run-time.
All the headache comes from mixing those two up.*
Similarly, the result of compiling is two-fold: a static and a dynamic part.
- the static part are objects like the constants, but also defined classes and their methods
- the dynamic part is the code, which is stored as streams of instructions in the MethodSource
Too make things a little simpler, we create a very high level instruction stream at first and then
run transformation and optimization passes on the stream to improve it.
The compiler has a method for each class of typed tree, named along on_xxx with xxx as the type
#### Compiler holds scope
The Compiler instance can hold arbitrary scope needed during the compilation.
A class statement sets the current @type scope , a method definition the @method.
If either are not set when needed compile errors will follow. So easy, so nice.
All code is encoded as a stream of Instructions in the MethodSource.
Instructions are stored as a list of Blocks, and Blocks are the smallest unit of code,
which is always linear.
Code is added to the method (using add_code), rather than working with the actual instructions.
This is so each compiling method can just do it's bit and be unaware of the larger structure
that is being created.
The general structure of the instructions is a graph
(with if's and whiles and breaks and what), but we build it to have one start and *one* end (return).
#### Messages and frames
Since the machine is oo we define it in objects.
Also it is important to define how instructions operate, which is is in a physical machine would
be by changing the contents of registers or some stack.
Our machine is not a register machine, but an object machine: it operates directly on objects and
also has no separate stack, only objects. There is only one object which is accessible,
basically meaning pinned to a register, the Message.
One can think of the Message as an oo replacement of the stack.
When a TypedMethod needs to make a call, it creates a NewMessage object.
Messages contain return addresses (yes, plural) and arguments.
The important thing here is that Messages and Frames are normal objects.

View File

@ -1,56 +0,0 @@
module Vm
module Assignment
def on_IvarAssignment( statement )
value = assignment_value(statement)
name = check_name(statement.name.name)
index = @method.for_type.variable_index( name)
raise "No such ivar #{name} #{@method.for_type}" unless index
value_type = @method.for_type.type_at( index )
raise "Argument Type mismatch #{value.type}!=#{value_type}" unless value.type == value_type
value_reg = use_reg(:value_type)
add_slot_to_reg(statement , :message , :receiver , value_reg )
add_reg_to_slot(statement , value , value_reg , index + 1 ) # one for type
end
def on_LocalAssignment( statement )
do_assignment_for( statement , :local )
end
def on_ArgAssignment( statement )
do_assignment_for( statement , :argument )
end
private
def do_assignment_for( statement , type )
value = assignment_value(statement)
name = check_name(statement.name.name)
index = @method.send( "has_#{type}" , name)
raise "No such #{type} #{name} #{@method.inspect}" unless index
value_type = @method.send("#{type}s_type" , index )
raise "Argument Type mismatch #{value.type}!=#{value_type}" unless value.type == value_type
move_reg(statement , "#{type}s".to_sym , value , index)
end
def move_reg(statement , type , value , index)
named_list = use_reg(:NamedList)
add_slot_to_reg(statement , :message , type , named_list )
add_reg_to_slot(statement , value , named_list , index + 1 ) # one for type
end
def assignment_value(statement)
reset_regs # statements reset registers, ie have all at their disposal
value = process(statement.value)
raise "Not register #{v}" unless value.is_a?(Risc::RiscValue)
value
end
# ensure the name given is not space and raise exception otherwise
# return the name
def check_name( name )
raise "space is a reserved name" if name == :space
name
end
end
end

View File

@ -1,52 +0,0 @@
module Vm
# collection of the simple ones, int and strings and such
module BasicValues
# Constant expressions can by definition be evaluated at compile time.
# But that does not solve their storage, ie they need to be accessible at runtime from _somewhere_
# So expressions move the data into a Risc.
# All expressions return registers
# But in the future (in the one that holds great things) we optimize those unneccesay moves away
def on_IntegerExpression expression
int = expression.value
reg = use_reg :Integer , int
add_load_constant( expression, int , reg )
return reg
end
def on_TrueExpression expression
reg = use_reg :Boolean
add_load_constant( expression, true , reg )
return reg
end
def on_FalseExpression expression
reg = use_reg :Boolean
add_load_constant( expression, false , reg )
return reg
end
def on_NilExpression expression
reg = use_reg :NilClass
add_load_constant( expression, nil , reg )
return reg
end
def on_StringExpression expression
value = Parfait.new_word expression.value.to_sym
reg = use_reg :Word
Risc.machine.constants << value
add_load_constant( expression, value , reg )
return reg
end
def on_ClassExpression expression
name = expression.value
raise "No meta class #{name}"
end
end
end

View File

@ -1,99 +0,0 @@
module Vm
module CallSite
def on_CallSite( statement )
raise "not inside method " unless @method
reset_regs
#move the new message (that we need to populate to make a call) to std register
load_new_message(statement)
me = get_me( statement )
type = get_my_type(me)
method = type.get_method(statement.name)
raise "Method not implemented for me:#{me} #{type.inspect}.#{statement.name}" unless method
# move our receiver there
add_reg_to_slot( statement , me , :new_message , :receiver)
set_message_details(method , statement , statement.arguments)
set_arguments(method , statement.arguments)
ret = use_reg( :Object ) #FIXME real return type
Risc.issue_call( self , method )
# the effect of the method is that the NewMessage Return slot will be filled, return it
# but move it into a register too
add_slot_to_reg(statement, :new_message , :return_value , ret )
ret
end
private
def load_new_message(statement)
new_message = Risc.resolve_to_register(:new_message)
add_slot_to_reg(statement, :message , :next_message , new_message )
new_message
end
def get_me( statement )
if statement.receiver
me = process( statement.receiver )
else
me = use_reg @method.for_type
add_slot_to_reg(statement, :message , :receiver , me )
end
me
end
def get_my_type( me )
# now we have to resolve the method name (+ receiver) into a callable method
case me.type
when Parfait::Type
type = me.type
when Symbol
type = Parfait.object_space.get_class_by_name(me.type).instance_type
else
raise me.inspect
end
raise "Not type #{type}" unless type.is_a? Parfait::Type
type
end
# load method name and set to new message (for exceptions/debug)
def set_message_details( method , name_s , arguments )
name = name_s.name
name_tmp = use_reg(:Word)
add_load_constant("#{name} load method name", name , name_tmp)
add_reg_to_slot( "#{name} store method name" , name_tmp , :new_message , :name)
# next arg type
args_reg = use_reg(:Type , method.arguments )
list_reg = use_reg(:NamedList , arguments )
add_load_constant("#{name} load arguments type", method.arguments , args_reg)
add_slot_to_reg( "#{name} get args from method" , :new_message , :arguments , list_reg )
add_reg_to_slot( "#{name} store args type in args" , args_reg , list_reg , 1 )
end
def set_arguments( method , arguments )
# reset tmp regs for each and load result into new_message
arg_type = method.arguments
message = "Arg number mismatch, method=#{arg_type.instance_length - 1} , call=#{arguments.length}"
raise message if (arg_type.instance_length - 1 ) != arguments.length
arguments.each_with_index do |arg , i |
store_arg_no(arguments , arg_type , arg , i + 1) #+1 for ruby(0 based)
end
end
def store_arg_no(arguments , arg_type , arg , i )
reset_regs
i = i + 1 # disregarding type field
val = process( arg) # processing should return the register with the value
raise "Not register #{val}" unless val.is_a?(Risc::RiscValue)
#FIXME definately needs some tests
raise "TypeMismatch calling with #{val.type} , instead of #{arg_type.type_at(i)}" if val.type != arg_type.type_at(i)
list_reg = use_reg(:NamedList , arguments )
add_slot_to_reg( "Set arg #{i}:#{arg}" , :new_message , :arguments , list_reg )
# which we load int the new_message at the argument's index
add_reg_to_slot( arg , val , list_reg , i ) #one for type and one for ruby
end
end
end

View File

@ -1,14 +0,0 @@
module Vm
module Collections
# attr_reader :values
def on_array statement, context
end
# attr_reader :key , :value
def on_association context
end
def on_hash context
end
end
end

View File

@ -1,27 +0,0 @@
module Vm
module FieldAccess
def on_FieldAccess statement
# receiver_ast , field_ast = *statement
receiver = process(statement.receiver)
type = receiver.type
if(type.is_a?(Symbol))
type = Parfait.object_space.get_class_by_name(type).instance_type
end
field_name = statement.field.name
index = type.variable_index(field_name)
raise "no such field:#{field_name} for class #{type.inspect}" unless index
value = use_reg(type.type_at(index))
add_slot_to_reg(statement , receiver , index, value)
value
end
def on_receiver expression
process expression.first
end
end
end

View File

@ -1,41 +0,0 @@
module Vm
module IfStatement
# an if evaluates the condition and jumps to the true block if true
# so the else block is automatically after that.
# But then the else needs to jump over the true block unconditionally.
def on_IfStatement( statement )
# branch_type , condition , if_true , if_false = *statement
true_block = compile_if_condition( statement )
merge = compile_if_false( statement )
add_code true_block
compile_if_true(statement)
add_code merge
nil # statements don't return anything
end
private
def compile_if_condition( statement )
reset_regs
process(statement.condition)
branch_class = Object.const_get "Risc::Is#{statement.branch_type.capitalize}"
true_block = Risc.label(statement, "if_true")
add_code branch_class.new( statement.condition , true_block )
return true_block
end
def compile_if_true( statement )
reset_regs
process(statement.if_true)
end
def compile_if_false( statement )
reset_regs
process(statement.if_false) if statement.if_false.statements
merge = Risc.label(statement , "if_merge")
add_code Risc::Branch.new(statement.if_false, merge )
merge
end
end
end

View File

@ -1,58 +0,0 @@
module Vm
module NameExpression
# attr_reader :name
# compiling name needs to check if it's a local variable
# or an argument
# whichever way this goes the result is stored in the return slot (as all compiles)
def on_KnownName statement
name = statement.name
[:self , :space , :message].each do |special|
return send(:"load_special_#{special}" , statement ) if name == special
end
raise "Unknow 'known' expression #{name}"
end
def on_ArgumentName(statement)
name = statement.name
index = @method.has_argument(name)
raise "no arg #{name}" unless index
named_list = use_reg :NamedList
ret = use_reg @method.arguments_type(index)
#puts "For #{name} at #{index} got #{@method.arguments.inspect}"
add_slot_to_reg("#{statement} load args" , :message , :arguments, named_list )
add_slot_to_reg("#{statement} load #{name}" , named_list , index + 1, ret )
return ret
end
def on_LocalName( statement )
name = statement.name
index = @method.has_local( name )
raise "must define local '#{name}' before using it" unless index
named_list = use_reg :NamedList
add_slot_to_reg("#{name} load locals" , :message , :locals , named_list )
ret = use_reg @method.locals_type( index )
add_slot_to_reg("#{name} load from locals" , named_list , index + 1, ret )
return ret
end
def load_special_self(statement)
ret = use_reg @type
add_slot_to_reg("#{statement} load self" , :message , :receiver , ret )
return ret
end
def load_special_space(statement)
space = Parfait.object_space
reg = use_reg :Space , space
add_load_constant( "#{statement} load space", space , reg )
return reg
end
def load_special_message(statement)
reg = use_reg :Message
add_transfer( "#{statement} load message", Risc.message_reg , reg )
return reg
end
end #module
end

View File

@ -1,15 +0,0 @@
module Vm
module OperatorExpression
def on_OperatorExpression statement
# operator , left_e , right_e = *statement
# left and right must be expressions. Expressions return a register when compiled
left_reg = process(statement.left_expression)
right_reg = process(statement.right_expression)
raise "Not register #{left_reg}" unless left_reg.is_a?(Risc::RiscValue)
raise "Not register #{right_reg}" unless right_reg.is_a?(Risc::RiscValue)
add_code Risc::OperatorInstruction.new(statement,statement.operator,left_reg,right_reg)
return left_reg # though this has wrong value attached
end
end
end

View File

@ -1,10 +0,0 @@
module Vm
module ReturnStatement
def on_ReturnStatement statement
reg = process(statement.return_value)
add_reg_to_slot( statement, reg , :message , :return_value)
nil # statements don't return
end
end
end

View File

@ -1,7 +0,0 @@
module Vm
module StatementList
def on_Statements statement
process_all( statement.statements )
end
end
end

View File

@ -1,42 +0,0 @@
module Vm
module WhileStatement
def on_WhileStatement statement
#branch_type , condition , statements = *statement
condition_label = compile_while_preamble( statement ) #jump there
start = compile_while_body( statement )
# This is where the loop starts, though in subsequent iterations it's in the middle
add_code condition_label
compile_while_condition( statement )
branch_class = Object.const_get "Risc::Is#{statement.branch_type.capitalize}"
# this is where the while ends and both branches meet
add_code branch_class.new( statement.condition , start )
nil # statements don't return anything
end
private
def compile_while_preamble( statement )
condition_label = Risc.label(statement.condition , "condition_label")
# unconditionally branch to the condition upon entering the loop
add_code Risc::Branch.new(statement.condition , condition_label)
condition_label
end
def compile_while_body( statement )
start = Risc.label(statement , "while_start" )
add_code start
reset_regs
process(statement.statements)
start
end
def compile_while_condition( statement )
reset_regs
process(statement.condition)
end
end
end

View File

@ -1,48 +0,0 @@
# Base class for Expresssion and Statement
module Vm
class Code ; end
class Statement < Code ; end
class Expression < Code ; end
module ValuePrinter
def to_s
@value.to_s
end
end
end
require_relative "tree/while_statement"
require_relative "tree/if_statement"
require_relative "tree/return_statement"
require_relative "tree/statements"
require_relative "tree/operator_expression"
require_relative "tree/field_access"
require_relative "tree/call_site"
require_relative "tree/basic_values"
require_relative "tree/assignment"
require_relative "tree/to_code"
AST::Node.class_eval do
# def [](name)
# #puts self.inspect
# children.each do |child|
# if child.is_a?(AST::Node)
# #puts child.type
# if (child.type == name)
# return child.children
# end
# else
# #puts child.class
# end
# end
# nil
# end
#
# def first_from( node_name )
# from = self[node_name]
# return nil unless from
# from.first
# end
end

View File

@ -1,30 +0,0 @@
module Vm
module Tree
class Assignment < Statement
attr_accessor :name , :value
def initialize(name , value = nil )
@name , @value = name , value
end
def to_s
"#{name} = #{value}\n"
end
end
class IvarAssignment < Assignment
def to_s
"@#{name} = #{value}\n"
end
end
class ArgAssignment < Assignment
end
class LocalAssignment < Assignment
end
end
end

View File

@ -1,66 +0,0 @@
module Vm
module Tree
class IntegerExpression < Expression
include ValuePrinter
attr_accessor :value
def initialize(value)
@value = value
end
end
class FloatExpression < Expression
include ValuePrinter
attr_accessor :value
def initialize(value)
@value = value
end
end
class TrueExpression < Expression
def to_s
"true"
end
end
class FalseExpression < Expression
def to_s
"false"
end
end
class NilExpression < Expression
def to_s
"nil"
end
end
class StringExpression < Expression
include ValuePrinter
attr_accessor :value
def initialize(value)
@value = value
end
end
class NameExpression < Expression
include ValuePrinter
attr_accessor :value
alias :name :value
def initialize(value)
@value = value
end
end
class LocalName < NameExpression
end
class ArgumentName < NameExpression
end
class InstanceName < NameExpression
end
class KnownName < NameExpression
end
class ClassExpression < Expression
include ValuePrinter
attr_accessor :value
def initialize(value)
@value = value
end
end
end
end

View File

@ -1,12 +0,0 @@
module Vm
module Tree
class CallSite < Expression
attr_accessor :name , :receiver , :arguments
def to_s
str = receiver ? "#{receiver}.#{name}" : name.to_s
str + arguments.collect{|a| a.to_s }.join(",")
end
end
end
end

View File

@ -1,10 +0,0 @@
module Vm
module Tree
class FieldAccess < Expression
attr_accessor :receiver , :field
def to_s
"#{receiver}.#{field}"
end
end
end
end

View File

@ -1,12 +0,0 @@
module Vm
module Tree
class IfStatement < Statement
attr_accessor :branch_type , :condition , :if_true , :if_false
def to_s
str = "if_#{branch_type}(#{condition}) \n #{if_true}\n"
str += "else\n #{if_false}\n" if if_false
str + "end\n"
end
end
end
end

View File

@ -1,10 +0,0 @@
module Vm
module Tree
class OperatorExpression < Expression
attr_accessor :operator , :left_expression , :right_expression
def to_s
"#{left_expression} #{operator} #{right_expression}"
end
end
end
end

View File

@ -1,11 +0,0 @@
module Vm
module Tree
class ReturnStatement < Statement
attr_accessor :return_value
def to_s
"return #{return_value}"
end
end
end
end

View File

@ -1,9 +0,0 @@
module Vm
class Statements < Statement
attr_accessor :statements
def to_s
return "" unless statements
statements.collect() { |s| s.to_s }.join
end
end
end

View File

@ -1,162 +0,0 @@
module Vm
def self.ast_to_code statement
compiler = ToCode.new
compiler.process statement
end
# ToCode converts an ast (from the ast gem) into the vm code expressions
# Code is the base class of the tree that is transformed to and
# Expression and Statement the next two subclasses.
# While it is an ast, it is NOT a ruby parser generated ast. Instead the ast is generated
# with s-expressions (also from the ast gem), mostly in tests, but also a little in
# the generation of functions (Builtin)
#
class ToCode < AST::Processor
def handler_missing node
raise "No handler on_#{node.type}(node)"
end
def on_parameters statement
params = {}
statement.children.each do |param , type , name|
type , name = *param
params[name] = type
end
params
end
def on_while_statement statement
branch_type , condition , statements = *statement
whil = Tree::WhileStatement.new()
whil.branch_type = branch_type
whil.condition = process(condition)
whil.statements = process(statements)
whil
end
def on_if_statement statement
branch_type , condition , if_true , if_false = *statement
iff = Tree::IfStatement.new()
iff.branch_type = branch_type
iff.condition = process(condition)
iff.if_true = process(if_true)
iff.if_false = process(if_false)
iff
end
def process_first code
raise "Too many children #{code.inspect}" if code.children.length != 1
process code.children.first
end
alias :on_conditional :process_first
alias :on_condition :process_first
alias :on_field :process_first
def on_statements( statement )
list = Statements.new()
kids = statement.children
return list unless kids
return list unless kids.first
list.statements = process_all(kids)
list
end
alias :on_true_statements :on_statements
alias :on_false_statements :on_statements
def on_return statement
ret = Tree::ReturnStatement.new()
ret.return_value = process(statement.children.first)
ret
end
def on_operator_value statement
operator , left_e , right_e = *statement
op = Tree::OperatorExpression.new()
op.operator = operator
op.left_expression = process(left_e)
op.right_expression = process(right_e)
op
end
def on_field_access statement
receiver_ast , field_ast = *statement
field = Tree::FieldAccess.new()
field.receiver = process(receiver_ast)
field.field = process(field_ast)
field
end
def on_receiver expression
process expression.children.first
end
def on_call statement
name , arguments , receiver = *statement
call = Tree::CallSite.new()
call.name = name
call.arguments = process_all(arguments)
call.receiver = process(receiver)
call
end
def on_int expression
Tree::IntegerExpression.new(expression.children.first)
end
def on_true _expression
Tree::TrueExpression.new
end
def on_false _expression
Tree::FalseExpression.new
end
def on_nil _expression
Tree::NilExpression.new
end
def on_arg statement
Tree::ArgumentName.new(statement.children.first)
end
def on_local statement
Tree::LocalName.new(statement.children.first)
end
def on_ivar statement
Tree::InstanceName.new(statement.children.first)
end
def on_known statement
Tree::KnownName.new(statement.children.first)
end
def on_string expressions
Tree::StringExpression.new(expressions.children.first)
end
def on_class_name expression
Tree::ClassExpression.new(expression.children.first)
end
def on_i_assignment statement
assignment_for( statement, Vm::Tree::IvarAssignment)
end
def on_a_assignment statement
assignment_for( statement, Vm::Tree::ArgAssignment)
end
def on_l_assignment( statement )
assignment_for( statement, Vm::Tree::LocalAssignment)
end
def assignment_for( statement , clazz)
name , value = *statement
p_name = process name
p_value = process(value)
clazz.new(p_name , p_value)
end
end
end

View File

@ -1,11 +0,0 @@
module Vm
module Tree
class WhileStatement < Statement
attr_accessor :branch_type , :condition , :statements
def to_s
str = "while_#{branch_type}(#{condition}) do\n"
str + statements.to_s + "\nend\n"
end
end
end
end

View File

@ -39,7 +39,7 @@ module Vool
methods.each do |method|
code = Passes::MethodCompiler.new(method).get_code
typed_method = method.create_parfait_method(clazz.instance_type)
Vm::MethodCompiler.new( typed_method ).init_method.process( code )
Risc::MethodCompiler.new( typed_method ).init_method.process( code )
end
end