2015-10-23 13:22:55 +02:00
|
|
|
module Soml
|
2015-10-28 20:36:41 +01:00
|
|
|
# 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 Method 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)
|
|
|
|
def self.compile statement
|
|
|
|
compiler = Compiler.new
|
|
|
|
compiler.process statement
|
|
|
|
end
|
|
|
|
|
2015-09-19 17:56:18 +02:00
|
|
|
class Compiler < AST::Processor
|
2015-05-08 14:10:30 +02:00
|
|
|
|
2015-10-28 20:36:41 +01:00
|
|
|
def initialize( method = nil )
|
2015-10-10 10:05:55 +02:00
|
|
|
@regs = []
|
2015-10-28 20:36:41 +01:00
|
|
|
return unless method
|
|
|
|
@method = method
|
|
|
|
@clazz = method.for_class
|
|
|
|
@current = method.instructions
|
2015-09-19 17:56:18 +02:00
|
|
|
end
|
2015-10-28 20:36:41 +01:00
|
|
|
attr_reader :clazz , :method
|
|
|
|
|
2015-09-19 17:56:18 +02:00
|
|
|
def handler_missing node
|
|
|
|
raise "No handler on_#{node.type}(node)"
|
|
|
|
end
|
2015-10-28 20:36:41 +01:00
|
|
|
|
|
|
|
# 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
|
2015-11-11 19:41:02 +01:00
|
|
|
def create_method( class_name , method_name , args = {})
|
2015-10-28 20:36:41 +01:00
|
|
|
raise "create_method #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol
|
|
|
|
clazz = Register.machine.space.get_class_by_name class_name
|
|
|
|
raise "No such class #{class_name}" unless clazz
|
|
|
|
create_method_for( clazz , method_name , args)
|
|
|
|
end
|
|
|
|
|
|
|
|
# create a method for the given class ( Parfait class object)
|
|
|
|
# method_name is a Symbol
|
|
|
|
# args a ruby array
|
|
|
|
# the created method is set as the current and the given class too
|
|
|
|
# return the compiler (for chaining)
|
|
|
|
def create_method_for clazz , method_name , args
|
|
|
|
@clazz = clazz
|
|
|
|
raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol
|
|
|
|
arguments = []
|
2015-11-19 09:10:13 +01:00
|
|
|
if( args.is_a? Array)
|
|
|
|
arguments = args
|
|
|
|
else
|
|
|
|
args.each do | name , type |
|
|
|
|
arguments << Parfait::Variable.new( type , name.to_sym)
|
2015-10-28 20:36:41 +01:00
|
|
|
end
|
|
|
|
end
|
2015-11-18 10:55:29 +01:00
|
|
|
@method = clazz.create_instance_method( method_name , Parfait.new_list(arguments))
|
2015-10-28 20:36:41 +01:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# add method entry and exit code. Mainly save_return for the enter and
|
|
|
|
# message shuffle and FunctionReturn for the return
|
|
|
|
# return self for chaining
|
|
|
|
def init_method
|
2015-11-01 18:13:40 +01:00
|
|
|
source = "_init_method"
|
2015-11-14 21:53:01 +01:00
|
|
|
name = "#{method.for_class.name}.#{method.name}"
|
|
|
|
@method.instructions = Register::Label.new(source, name)
|
2015-11-02 19:11:12 +01:00
|
|
|
@current = enter = method.instructions
|
2015-11-14 21:53:01 +01:00
|
|
|
add_code Register::Label.new( source, "return #{name}")
|
2015-10-28 20:36:41 +01:00
|
|
|
#load the return address into pc, affecting return. (other cpus have commands for this, but not arm)
|
2015-11-07 20:59:39 +01:00
|
|
|
add_code Register::FunctionReturn.new( source , Register.message_reg , Register.resolve_index(:message , :return_address) )
|
2015-10-28 20:36:41 +01:00
|
|
|
@current = enter
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# set the insertion point (where code is added with add_code)
|
|
|
|
def set_current c
|
|
|
|
@current = c
|
|
|
|
end
|
|
|
|
|
|
|
|
# add an instruction after the current (insertion point)
|
|
|
|
# the added instruction will become the new insertion point
|
|
|
|
def add_code instruction
|
|
|
|
unless instruction.is_a?(Register::Instruction)
|
|
|
|
raise instruction.to_s
|
|
|
|
end
|
|
|
|
@current.insert(instruction) #insert after current
|
|
|
|
@current = instruction
|
|
|
|
self
|
2015-10-23 13:08:12 +02:00
|
|
|
end
|
2015-10-28 20:36:41 +01:00
|
|
|
|
2015-10-10 10:05:55 +02:00
|
|
|
# require a (temporary) register. code must give this back with release_reg
|
2015-10-15 08:07:47 +02:00
|
|
|
def use_reg type , value = nil
|
2015-10-10 10:05:55 +02:00
|
|
|
if @regs.empty?
|
2015-10-15 08:07:47 +02:00
|
|
|
reg = Register.tmp_reg(type , value)
|
2015-10-10 10:05:55 +02:00
|
|
|
else
|
2015-10-15 08:07:47 +02:00
|
|
|
reg = @regs.last.next_reg_use(type , value)
|
2015-10-10 10:05:55 +02:00
|
|
|
end
|
|
|
|
@regs << reg
|
|
|
|
return reg
|
|
|
|
end
|
|
|
|
|
2015-11-13 19:47:08 +01:00
|
|
|
def copy reg , source
|
|
|
|
copied = use_reg reg.type
|
|
|
|
add_code Reister.transfer source , reg , copied
|
|
|
|
copied
|
|
|
|
end
|
|
|
|
|
2015-10-14 12:48:21 +02:00
|
|
|
# releasing a register (accuired by use_reg) makes it available for use again
|
|
|
|
# thus avoiding possibly using too many registers
|
2015-10-10 10:05:55 +02:00
|
|
|
def release_reg reg
|
|
|
|
last = @regs.pop
|
|
|
|
raise "released register in wrong order, expect #{last} but was #{reg}" if reg != last
|
|
|
|
end
|
2015-10-14 12:48:21 +02:00
|
|
|
|
|
|
|
# reset the registers to be used. Start at r4 for next usage.
|
|
|
|
# Every statement starts with this, meaning each statement may use all registers, but none
|
|
|
|
# get saved. Statements have affect on objects.
|
|
|
|
def reset_regs
|
|
|
|
@regs.clear
|
|
|
|
end
|
2015-11-07 11:19:04 +01:00
|
|
|
|
|
|
|
# ensure the name given is not space and raise exception otherwise
|
|
|
|
# return the name for chaining
|
|
|
|
def no_space name
|
|
|
|
raise "space is a reserved name" if name == :space
|
|
|
|
name
|
|
|
|
end
|
2015-11-07 16:38:27 +01:00
|
|
|
|
|
|
|
def self.load_parfait
|
2015-11-08 17:35:10 +01:00
|
|
|
each_parfait do |parts|
|
|
|
|
self.new.process( parts )
|
|
|
|
end
|
|
|
|
end
|
2015-11-11 19:41:02 +01:00
|
|
|
|
2015-11-08 17:35:10 +01:00
|
|
|
def self.each_parfait
|
2016-02-25 20:50:10 +01:00
|
|
|
["word","class","type","message" ,"integer", "object"].each do |o|
|
2015-11-07 16:38:27 +01:00
|
|
|
str = File.open(File.expand_path("parfait/#{o}.soml", File.dirname(__FILE__))).read
|
2015-11-11 19:41:02 +01:00
|
|
|
syntax = Parser::Salama.new.parse_with_debug(str, reporter: Parslet::ErrorReporter::Deepest.new)
|
2015-11-07 16:38:27 +01:00
|
|
|
parts = Parser::Transform.new.apply(syntax)
|
2015-11-08 17:35:10 +01:00
|
|
|
yield parts
|
2015-11-07 16:38:27 +01:00
|
|
|
end
|
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-27 10:26:53 +02:00
|
|
|
require_relative "ast_helper"
|
2015-10-15 12:08:53 +02:00
|
|
|
require_relative "compiler/assignment"
|
2015-10-09 16:51:14 +02:00
|
|
|
require_relative "compiler/basic_values"
|
|
|
|
require_relative "compiler/call_site"
|
2015-09-27 11:59:26 +02:00
|
|
|
require_relative "compiler/class_field"
|
2015-10-26 21:22:38 +01:00
|
|
|
require_relative "compiler/class_statement"
|
2015-10-09 16:51:14 +02:00
|
|
|
require_relative "compiler/collections"
|
2015-09-20 15:30:07 +02:00
|
|
|
require_relative "compiler/field_def"
|
2015-09-27 10:26:53 +02:00
|
|
|
require_relative "compiler/field_access"
|
2015-10-09 16:51:14 +02:00
|
|
|
require_relative "compiler/function_definition"
|
|
|
|
require_relative "compiler/if_statement"
|
2015-09-20 15:30:07 +02:00
|
|
|
require_relative "compiler/name_expression"
|
2015-10-09 16:51:14 +02:00
|
|
|
require_relative "compiler/operator_value"
|
|
|
|
require_relative "compiler/return_statement"
|
2015-10-26 21:22:38 +01:00
|
|
|
require_relative "compiler/statement_list"
|
2015-10-09 16:51:14 +02:00
|
|
|
require_relative "compiler/while_statement"
|