first tests comping through after ast/compile change

This commit is contained in:
Torsten Ruger 2015-05-04 23:03:52 +03:00
parent ff22c17784
commit e4c799ecb6
17 changed files with 72 additions and 75 deletions

View File

@ -1,5 +0,0 @@
lib/**/*.rb
bin/*
-
features/**/*.feature
LICENSE.txt

View File

@ -1,24 +1,24 @@
require_relative "instruction"
module Arm
# A Machines main responsibility in the framework is to instantiate Instruction
# A Machines main responsibility in the framework is to instantiate Instructions
# Value functions are mapped to machines by concatenating the values class name + the methd name
# Example: IntegerValue.plus( value ) -> Machine.signed_plus (value )
# Also, shortcuts are created to easily instantiate Instruction objects.
# Example: pop -> StackInstruction.new( {:opcode => :pop}.merge(options) )
# Instructions work with options, so you can pass anything in, and the only thing the functions does
# is save you typing the clazz.new. It passes the function name as the :opcode
class ArmMachine
# conditions specify all the possibilities for branches. Branches are b + condition
# Example: beq means brach if equal.
# Example: beq means brach if equal.
# :al means always, so bal is an unconditional branch (but b() also works)
CONDITIONS = [ :al , :eq , :ne , :lt , :le, :ge, :gt , :cs , :mi , :hi , :cc , :pl, :ls , :vc , :vs ]
# here we create the shortcuts for the "standard" instructions, see above
# Derived machines may use own instructions and define functions for them if so desired
def self.init
@ -40,7 +40,7 @@ module Arm
[:b, :call , :swi].each do |inst|
define_instruction_one(inst , CallInstruction)
end
# create all possible brach instructions, but the CallInstruction demangles the
# create all possible brach instructions, but the CallInstruction demangles the
# code, and has opcode set to :b and :condition_code set to the condition
CONDITIONS.each do |suffix|
define_instruction_one("b#{suffix}".to_sym , CallInstruction)
@ -53,7 +53,6 @@ module Arm
end
def self.class_for clazz
c_name = clazz.name
my_module = self.class.name.split("::").first
clazz_name = clazz.name.split("::").last
if(my_module != Register )
@ -65,13 +64,13 @@ module Arm
#defining the instruction (opcode, symbol) as an given class.
# the class is a Register::Instruction derived base class and to create machine specific function
# an actual machine must create derived classes (from this base class)
# an actual machine must create derived classes (from this base class)
# These instruction classes must follow a naming pattern and take a hash in the contructor
# Example, a mov() opcode instantiates a Register::MoveInstruction
# for an Arm machine, a class Arm::MoveInstruction < Register::MoveInstruction exists, and it will
# be used to define the mov on an arm machine.
# This methods picks up that derived class and calls a define_instruction methods that can
# be overriden in subclasses
# be used to define the mov on an arm machine.
# This methods picks up that derived class and calls a define_instruction methods that can
# be overriden in subclasses
def self.define_instruction_one(inst , clazz , defaults = {} )
clazz = class_for(clazz)
create_method(inst) do |first , options = nil|

View File

@ -1,9 +1,9 @@
module Compiler
def self.compile expression , method , message
exp_name = expression.class.split("::").last.sub("Expression","").downcase
exp_name = expression.class.name.split("::").last.sub("Expression","").downcase
puts "Expression #{exp_name}"
self.send exp_name.to_sym , method , message
self.send "compile_#{exp_name}".to_sym , expression, method , message
end
end

View File

@ -11,28 +11,28 @@ module Compiler
# But in the future (in the one that holds great things) we optimize those unneccesay moves away
# attr_reader :value
def compile_integer expession , method , message
def self.compile_integer expession , method , message
int = Virtual::IntegerConstant.new(value)
to = Virtual::NewReturn.new(Virtual::Integer , int)
method.add_code Virtual::Set.new( to , int)
to
end
def compile_true expession , method , message
def self.compile_true expession , method , message
value = Virtual::TrueConstant.new
to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value )
to
end
def compile_false expession , method , message
def self.compile_false expession , method , message
value = Virtual::FalseConstant.new
to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value )
to
end
def compile_nil expession , method , message
def self.compile_nil expession , method , message
value = Virtual::NilConstant.new
to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value )
@ -43,7 +43,7 @@ module Compiler
# compiling name needs to check if it's a variable and if so resolve it
# otherwise it's a method without args and a send is ussued.
# this makes the namespace static, ie when eval and co are implemented method needs recompilation
def compile_name expession , method , message
def self.compile_name expession , method , message
return Virtual::Self.new( Virtual::Mystery ) if expession.name == :self
if method.has_var(expession.name)
message.compile_get(method , expession.name )
@ -54,7 +54,7 @@ module Compiler
end
def compile_module expession , method , message
def self.compile_module expession , method , message
clazz = Virtual::BootSpace.space.get_or_create_class name
raise "uups #{clazz}.#{name}" unless clazz
to = Virtual::Return.new(Virtual::Reference , clazz )
@ -63,7 +63,7 @@ module Compiler
end
# attr_reader :string
def compile_string expession , method , message
def self.compile_string expession , method , message
value = Virtual::StringConstant.new(expession.string)
to = Virtual::Return.new(Virtual::Reference , value)
Virtual::BootSpace.space.add_object value
@ -72,14 +72,14 @@ module Compiler
end
#attr_reader :left, :right
def compile_assignment expession , method , message
def self.compile_assignment expession , method , message
raise "must assign to NameExpression , not #{expession.left}" unless expession.left.instance_of? NameExpression
r = right.compile(method,message)
raise "oh noo, nil from where #{expession.right.inspect}" unless r
message.compile_set( method , expession.left.name , r )
end
def compile_variable expession, method , message
def self.compile_variable expession, method , message
method.add_code Virtual::InstanceGet.new(expession.name)
Virtual::NewReturn.new( Virtual::Mystery )
end

View File

@ -3,7 +3,7 @@ module Compiler
# call_site - attr_reader :name, :args , :receiver
def compile_call_site expession , method , message
def self.compile_call_site expession , method , message
me = expession.receiver.compile( method, message )
method.add_code Virtual::NewMessage.new
method.add_code Virtual::Set.new(Virtual::NewSelf.new(me.type), me)

View File

@ -1,14 +1,14 @@
module Compiler
# attr_reader :values
def compile_array expession, context
def self.compile_array expession, context
to.do
end
# attr_reader :key , :value
def compile_association context
def self.compile_association context
to.do
end
def compile_hash context
def self.compile_hash context
to.do
end
end

View File

@ -1,6 +1,8 @@
module Compiler
# list - attr_reader :expressions
def compile_list expession , method , message
expession.expressions.collect { |part| part.compile( method, message ) }
def self.compile_list expession , method , message
expession.expressions.collect do |part|
Compiler.compile( part , method, message )
end
end
end

View File

@ -1,6 +1,6 @@
module Compiler
# function attr_reader :name, :params, :body , :receiver
def compile_function expression, method , message
def self.compile_function expression, method , message
args = expession.params.collect do |p|
raise "error, argument must be a identifier, not #{p}" unless p.is_a? NameExpression
p.name

View File

@ -1,7 +1,7 @@
module Compiler
# if - attr_reader :cond, :if_true, :if_false
def compile_if expression , method , message
def self.compile_if expression , method , message
# to execute the logic as the if states it, the blocks are the other way around
# so we can the jump over the else if true ,and the else joins unconditionally after the true_block
merge_block = method.new_block "if_merge" # last one, created first

View File

@ -1,10 +1,10 @@
module Compiler
# module attr_reader :name ,:expressions
def compile_module expression , context
def self.compile_module expression , context
return clazz
end
def compile_class expression , method , message
def self.compile_class expression , method , message
clazz = ::Virtual::BootSpace.space.get_or_create_class name
puts "Created class #{clazz.name.inspect}"
expression.expressions.each do |expr|

View File

@ -1,6 +1,6 @@
module Compiler
# operator attr_reader :operator, :left, :right
def compile_operator expression, method , message
def self.compile_operator expression, method , message
call = CallSiteExpression.new( operator , [right] , left )
call.compile(method,message)
end

View File

@ -1,7 +1,7 @@
module Compiler
# return attr_reader :expression
def compile_return expression, scope ,method
def self.compile_return expression, scope ,method
Virtual::Reference.new
end
def old

View File

@ -1,7 +1,7 @@
module Compiler
# while- attr_reader :condition, :body
def compile_while expression, method , message
def self.compile_while expression, method , message
start = Virtual::Label.new("while_start")
method.add_code start
is = expression.condition.compile(method,message)

View File

@ -1,9 +1,9 @@
module Builtin
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.
# 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::CompiledMethod.new(:index_of , Virtual::Reference , [Virtual::Reference] , Virtual::Integer )
@ -16,15 +16,15 @@ module Builtin
layout_function
end
# in ruby, how this goes is
# in ruby, how this goes is
# def _get_instance_variable var
# i = self.index_of(var)
# return at_index(i)
# 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
def _get_instance_variable context , name = Virtual::Integer
get_function = Virtual::CompiledMethod.new(:_get_instance_variable , [ Virtual::Reference ] , Virtual::Reference ,Virtual::Mystery )
# return get_function
return get_function
me = get_function.receiver
var_name = get_function.args.first
return_to = get_function.return_type
@ -32,13 +32,13 @@ module Builtin
index_function = ::Virtual::BootSpace.space.get_or_create_class(:Object).resolve_method(:index_of)
# get_function.push( [me] )
# index = get_function.call( index_function )
after_body = get_function.new_block("after_index")
get_function.current 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
@ -54,14 +54,14 @@ module Builtin
set_function.push( [me] )
set_function.call( index_function )
after_body = set_function.new_block("after_index")
set_function.current 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

View File

@ -14,20 +14,20 @@ module Virtual
# Methods also have arguments and a return. These are typed by subclass instances of Value
# They also have local variables.
# They also have local variables.
# Code-wise Methods are made up from a list of Blocks, in a similar way blocks are made up of Instructions
# The function starts with one block, and that has a start and end (return)
# Blocks can be linked in two ways:
# -linear: flow continues from one to the next as they are sequential both logically and
# "physically" use the block set_next for this.
# "physically" use the block set_next for this.
# This "straight line", there must be a continuous sequence from body to return
# Linear blocks may be created from an existing block with new_block
# - branched: You create new blocks using function.new_block which gets added "after" return
# These (eg if/while) blocks may themselves have linear blocks ,but the last of these
# These (eg if/while) blocks may themselves have linear blocks ,but the last of these
# MUST have an uncoditional branch. And remember, all roads lead to return.
class CompiledMethod < Virtual::Object
#return the main function (the top level) into which code is compiled
def CompiledMethod.main
@ -63,10 +63,10 @@ module Virtual
def locals_at l_block
used =[]
# call assigns the return register, but as it is in l_block, it is not asked.
assigned = [ RegisterReference.new(Register::RegisterMachine.instance.return_register) ]
assigned = [ RegisterReference.new(Virtual::RegisterMachine.instance.return_register) ]
l_block.reachable.each do |b|
b.uses.each {|u|
(used << u) unless assigned.include?(u)
(used << u) unless assigned.include?(u)
}
assigned += b.assigns
end
@ -77,15 +77,15 @@ module Virtual
# So when creating a new block (with new_block), it is only added to the list, but instructions
# still go to the current one
# With this function one can change the current block, to actually code it.
# This juggling is (unfortunately) neccessary, as all compile functions just keep puring their code into the
# This juggling is (unfortunately) neccessary, as all compile functions just keep puring their code into the
# method and don't care what other compiles (like if's) do.
# Example: while, needs 2 extra blocks
# 1 condition code, must be its own blockas we jump back to it
# - the body, can actually be after the condition as we don't need to jump there
# 2 after while block. Condition jumps here
# 2 after while block. Condition jumps here
# After block 2, the function is linear again and the calling code does not need to know what happened
# But subsequent statements are still using the original block (self) to add code to
# So the while expression creates the extra blocks, adds them and the code and then "moves" the insertion point along
def current block
@ -93,8 +93,8 @@ module Virtual
self
end
# create a new linear block after the current insertion block.
# Linear means there is no brach needed from that one to the new one.
# create a new linear block after the current insertion block.
# Linear means there is no brach needed from that one to the new one.
# Usually the new one just serves as jump address for a control statement
# In code generation , the new_block is written after this one, ie zero runtime cost
# This does _not_ change the insertion point, that has do be done with insert_at(block)
@ -109,7 +109,7 @@ module Virtual
# determine whether this method has a variable by the given name
# variables are locals and and arguments
# used to determine if a send must be issued
# used to determine if a send must be issued
def has_var name
name = name.to_sym
var = @arg_names.find {|a| a.name == name }
@ -151,23 +151,23 @@ module Virtual
def layout
Virtual::Object.layout
end
# sugar to create instructions easily.
# sugar to create instructions easily.
# any method will be passed on to the RegisterMachine and the result added to the insertion block
# With this trick we can write what looks like assembler,
# With this trick we can write what looks like assembler,
# Example func.instance_eval
# mov( r1 , r2 )
# add( r1 , r2 , 4)
# end
# mov and add will be called on Machine and generate Inststuction that are then added
# mov and add will be called on Machine and generate Instructions that are then added
# to the current block
# also symbols are supported and wrapped as register usages (for bare metal programming)
def method_missing(meth, *arg_names, &block)
add_code ::Register::RegisterMachine.instance.send(meth , *arg_names)
add_code ::Arm::ArmMachine.send(meth , *arg_names)
end
def mem_length
l = @blocks.inject(0) { |c , block| c += block.mem_length }
padded(l)
padded(l)
end
# position of the function is the position of the entry block, is where we call
def set_position at

View File

@ -50,7 +50,8 @@ module Virtual
# read all the files needed for a minimal system at compile
classes = ["object"]
classes.each do |clazz|
bytes = File.read(File.join( File.dirname( __FILE__ ) , ".." , "parfait" , "#{clazz}.rb") )
bytes = File.read(File.join( File.dirname( __FILE__ ) , ".." , "parfait" , "#{clazz}.rb") )
bytes = 0 #shuts up my atom linter
# expression = compile_main(bytes)
end
end
@ -59,7 +60,7 @@ module Virtual
syntax = @parser.parse_with_debug(bytes)
parts = Parser::Transform.new.apply(syntax)
main = Virtual::CompiledMethod.main
parts.compile( main , self.message )
Compiler.compile( parts , main , self.message )
end
end

View File

@ -30,12 +30,12 @@ HERE
# a hand coded version of the fibonachi numbers (moved to kernel to be able to call it)
# not my hand off course, found in the net from a basic introduction
def test_kernel_fibo
int = Register::Integer.new(Register::RegisterMachine.instance.receiver_register)
int = Register::Integer.new(Virtual::RegisterMachine.instance.receiver_register)
fibo = @object_space.get_or_create_class(:Object).resolve_method(:fibo)
main = @object_space.main
main.mov int , 10
main.call( fibo )
main.mov( Register::RegisterMachine.instance.receiver_register , Register::RegisterMachine.instance.return_register )
main.mov( Virtual::RegisterMachine.instance.receiver_register , Virtual::RegisterMachine.instance.return_register )
putint = @object_space.get_or_create_class(:Object).resolve_method(:putint)
main.call( putint )
@should = [0x0,0x40,0x2d,0xe9,0x1,0x0,0x52,0xe3,0x2,0x0,0xa0,0xd1,0x7,0x0,0x0,0xda,0x1,0x30,0xa0,0xe3,0x0,0x40,0xa0,0xe3,0x4,0x30,0x83,0xe0,0x4,0x40,0x43,0xe0,0x1,0x20,0x42,0xe2,0x1,0x0,0x52,0xe3,0xfa,0xff,0xff,0x1a,0x3,0x0,0xa0,0xe1,0x0,0x80,0xbd,0xe8]