2015-10-07 14:22:47 +02:00
|
|
|
module Phisol
|
2015-09-19 17:56:18 +02:00
|
|
|
class Compiler < AST::Processor
|
2015-05-08 14:10:30 +02:00
|
|
|
|
2015-10-05 23:27:13 +02:00
|
|
|
def initialize()
|
2015-10-10 10:05:55 +02:00
|
|
|
@regs = []
|
2015-09-19 17:56:18 +02:00
|
|
|
end
|
|
|
|
def handler_missing node
|
|
|
|
raise "No handler on_#{node.type}(node)"
|
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
# Compiling is the conversion of the AST into 2 things:
|
2015-07-03 19:13:03 +02:00
|
|
|
# - code (ie sequences of Instructions inside Blocks) carried by MethodSource
|
2015-05-08 14:10:30 +02:00
|
|
|
# - an object graph containing all the Methods, their classes and Constants
|
|
|
|
#
|
|
|
|
# Some compile methods just add code, some may add structure (ie Blocks) while
|
|
|
|
# others instantiate Class and Method objects
|
|
|
|
#
|
2015-10-09 16:51:14 +02:00
|
|
|
# Everything in ruby is an statement, ie returns a value. So the effect of every compile
|
2015-05-08 14:10:30 +02:00
|
|
|
# is that a value is put into the ReturnSlot of the current Message.
|
2015-09-27 13:07:02 +02:00
|
|
|
# The compile method (so every compile method) returns the value that it deposits.
|
2015-05-08 14:10:30 +02:00
|
|
|
#
|
2015-09-19 17:56:18 +02:00
|
|
|
# The process uses a visitor pattern (from AST::Processor) to dispatch according to the
|
2015-10-09 16:51:14 +02:00
|
|
|
# type the statement. So a s(:if xx) will become an on_if(node) call.
|
2015-05-08 14:10:30 +02:00
|
|
|
# This makes the dispatch extensible, ie Expressions may be added by external code,
|
|
|
|
# as long as matching compile methods are supplied too.
|
|
|
|
#
|
2015-10-09 16:51:14 +02:00
|
|
|
def self.compile statement
|
2015-10-05 23:27:13 +02:00
|
|
|
compiler = Compiler.new
|
2015-10-09 16:51:14 +02:00
|
|
|
compiler.process statement
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
|
|
|
|
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-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-05-08 14:10:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-27 10:26:53 +02:00
|
|
|
require_relative "ast_helper"
|
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-09 16:51:14 +02:00
|
|
|
require_relative "compiler/collections"
|
|
|
|
require_relative "compiler/statement_list"
|
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"
|
|
|
|
require_relative "compiler/class_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"
|
|
|
|
require_relative "compiler/while_statement"
|