Copy risc compiler stuff to mom
Start to separate the layers. wip, just checkin in to see the following changes better
This commit is contained in:
53
lib/mom/block_compiler.rb
Normal file
53
lib/mom/block_compiler.rb
Normal file
@ -0,0 +1,53 @@
|
||||
module Mom
|
||||
|
||||
# A BlockCompiler is much like a MehtodCompiler, exept for blocks
|
||||
#
|
||||
class BlockCompiler < CallableCompiler
|
||||
|
||||
attr_reader :block , :risc_instructions , :constants
|
||||
alias :block :callable
|
||||
|
||||
def initialize( block , method)
|
||||
@method = method
|
||||
super(block)
|
||||
end
|
||||
|
||||
def source_name
|
||||
"#{@method.self_type.name}.init"
|
||||
end
|
||||
|
||||
# resolve the type of the slot, by inferring from it's name, using the type
|
||||
# scope related slots are resolved by the compiler by method/block
|
||||
#
|
||||
# This mainly calls super, and only for :caller adds extra info
|
||||
# Using the info, means assuming that the block is not passed around (FIXME in 2020)
|
||||
def slot_type( slot , type)
|
||||
new_type = super
|
||||
if slot == :caller
|
||||
extra_info = { type_frame: @method.frame_type ,
|
||||
type_arguments: @method.arguments_type ,
|
||||
type_self: @method.self_type}
|
||||
end
|
||||
return new_type , extra_info
|
||||
end
|
||||
# determine how given name need to be accsessed.
|
||||
# For blocks the options are args or frame
|
||||
# or then the methods arg or frame
|
||||
def slot_type_for(name)
|
||||
if @callable.arguments_type.variable_index(name)
|
||||
slot_def = [:arguments]
|
||||
elsif @callable.frame_type.variable_index(name)
|
||||
slot_def = [:frame]
|
||||
elsif @method.arguments_type.variable_index(name)
|
||||
slot_def = [:caller , :caller ,:arguments ]
|
||||
elsif @method.frame_type.variable_index(name)
|
||||
slot_def = [:caller ,:caller , :frame ]
|
||||
elsif
|
||||
raise "no variable #{name} , need to resolve at runtime"
|
||||
end
|
||||
slot_def << name
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
@ -1,8 +1,10 @@
|
||||
module Mom
|
||||
|
||||
# CallableCompiler is used to generate Mom instructions.
|
||||
# CallableCompiler is used to generate mom instructions. It is an abstact base
|
||||
# class shared by BlockCompiler and MethodCompiler
|
||||
|
||||
# - mom_instructions: The sequence of mom level instructions that vool was compiled to
|
||||
# - mom_instructions: The sequence of mom level instructions that mom was compiled to
|
||||
# Instructions derive from class Instruction and form a linked list
|
||||
|
||||
class CallableCompiler
|
||||
|
||||
@ -10,11 +12,11 @@ module Mom
|
||||
@callable = callable
|
||||
@constants = []
|
||||
@block_compilers = []
|
||||
@mom_instructions = Risc.label(source_name, source_name)
|
||||
@mom_instructions = Label.new(source_name, source_name)
|
||||
@current = start = @risc_instructions
|
||||
add_code Risc.label( source_name, "return_label")
|
||||
Mom::ReturnSequence.new.to_risc(self)
|
||||
add_code Risc.label( source_name, "unreachable")
|
||||
add_code Label.new( source_name, "return_label")
|
||||
add_code Mom::ReturnSequence.new
|
||||
add_code Label.new( source_name, "unreachable")
|
||||
@current = start
|
||||
end
|
||||
attr_reader :risc_instructions , :constants , :block_compilers , :callable , :current
|
||||
@ -26,20 +28,6 @@ module Mom
|
||||
end
|
||||
end
|
||||
|
||||
# convert the given mom instruction to_risc and then add it (see add_code)
|
||||
# continue down the instruction chain unti depleted
|
||||
# (adding moves the insertion point so the whole mom chain is added as a risc chain)
|
||||
def add_mom( instruction )
|
||||
while( instruction )
|
||||
raise "whats this a #{instruction}" unless instruction.is_a?(Mom::Instruction)
|
||||
#puts "adding mom #{instruction.to_s}:#{instruction.next.to_s}"
|
||||
instruction.to_risc( self )
|
||||
reset_regs
|
||||
#puts "adding risc #{risc.to_s}:#{risc.next.to_s}"
|
||||
instruction = instruction.next
|
||||
end
|
||||
end
|
||||
|
||||
# add a constant (which get created during compilation and need to be linked)
|
||||
def add_constant(const)
|
||||
raise "Must be Parfait #{const}" unless const.is_a?(Parfait::Object)
|
||||
@ -49,18 +37,38 @@ module Mom
|
||||
# add a risc instruction after the current (insertion point)
|
||||
# the added instruction will become the new insertion point
|
||||
def add_code( instruction )
|
||||
raise "Not an instruction:#{instruction.to_s}:#{instruction.class.name}" unless instruction.is_a?(Risc::Instruction)
|
||||
raise instruction.to_s if( instruction.class.name.split("::").first == "Arm")
|
||||
raise "Not an instruction:#{instruction.to_s}:#{instruction.class.name}" unless instruction.is_a?(Mom::Instruction)
|
||||
new_current = instruction.last #after insertion this point is lost
|
||||
@current.insert(instruction) #insert after current
|
||||
@current = new_current
|
||||
self
|
||||
end
|
||||
|
||||
# resolve the type of the slot, by inferring from it's name, using the type
|
||||
# scope related slots are resolved by the compiler by method/block
|
||||
def slot_type( slot , type)
|
||||
case slot
|
||||
when :frame
|
||||
new_type = self.frame_type
|
||||
when :arguments
|
||||
new_type = self.arg_type
|
||||
when :receiver
|
||||
new_type = self.receiver_type
|
||||
when Symbol
|
||||
new_type = type.type_for(slot)
|
||||
raise "Not found object #{slot}: in #{type}" unless new_type
|
||||
else
|
||||
raise "Not implemented object #{slot}:#{slot.class}"
|
||||
end
|
||||
#puts "RESOLVE in #{@type.class_name} #{slot}->#{type}"
|
||||
return new_type
|
||||
end
|
||||
|
||||
# return the frame type, ie the blocks frame type
|
||||
def frame_type
|
||||
@callable.frame_type
|
||||
end
|
||||
|
||||
# return the frame type, ie the blocks arguments type
|
||||
def arg_type
|
||||
@callable.arguments_type
|
||||
|
85
lib/mom/method_compiler.rb
Normal file
85
lib/mom/method_compiler.rb
Normal file
@ -0,0 +1,85 @@
|
||||
module Mom
|
||||
|
||||
# MethodCompiler is used to generate Mom instructions for methods
|
||||
# and to instantiate the methods correctly.
|
||||
|
||||
class MethodCompiler < CallableCompiler
|
||||
|
||||
def initialize( method )
|
||||
super(method)
|
||||
end
|
||||
|
||||
#include block_compilers constants
|
||||
def constants
|
||||
block_compilers.inject(@constants.dup){|all, compiler| all += compiler.constants}
|
||||
end
|
||||
|
||||
def source_name
|
||||
"#{@callable.self_type.name}.#{@callable.name}"
|
||||
end
|
||||
|
||||
def get_method
|
||||
@callable
|
||||
end
|
||||
|
||||
# sometimes the method is used as source (tb reviewed)
|
||||
def source
|
||||
@callable
|
||||
end
|
||||
|
||||
# helper method for builtin mainly
|
||||
# the class_name is a symbol, which is resolved to the instance_type of that class
|
||||
#
|
||||
# return compiler_for_type with the resolved type
|
||||
#
|
||||
def self.compiler_for_class( class_name , method_name , args , frame )
|
||||
raise "create_method #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol
|
||||
clazz = Parfait.object_space.get_class_by_name! class_name
|
||||
compiler_for_type( clazz.instance_type , method_name , args , frame)
|
||||
end
|
||||
|
||||
def add_method_to( target )
|
||||
target.add_method( @callable )
|
||||
end
|
||||
|
||||
def create_block(arg_type , frame_type)
|
||||
@callable.create_block(arg_type ,frame_type)
|
||||
end
|
||||
|
||||
# create a method for the given type ( Parfait type object)
|
||||
# method_name is a Symbol
|
||||
# args a hash that will be converted to a type
|
||||
# the created method is set as the current and the given type too
|
||||
# return the compiler
|
||||
def self.compiler_for_type( type , method_name , args , frame)
|
||||
raise "create_method #{type.inspect} is not a Type" unless type.is_a? Parfait::Type
|
||||
raise "Args must be Type #{args}" unless args.is_a?(Parfait::Type)
|
||||
raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol
|
||||
method = type.create_method( method_name , args , frame)
|
||||
self.new(method)
|
||||
end
|
||||
|
||||
# determine how given name need to be accsessed.
|
||||
# For methods the options are args or frame
|
||||
def slot_type_for(name)
|
||||
if @callable.arguments_type.variable_index(name)
|
||||
type = :arguments
|
||||
else
|
||||
type = :frame
|
||||
end
|
||||
[type , name]
|
||||
end
|
||||
|
||||
def add_block_compiler(compiler)
|
||||
@block_compilers << compiler
|
||||
end
|
||||
|
||||
# return true or false if the given name is in scope (arg/local)
|
||||
def in_scope?(name)
|
||||
ret = true if @callable.arguments_type.variable_index(name)
|
||||
ret = @callable.frame_type.variable_index(name) unless ret
|
||||
ret
|
||||
end
|
||||
|
||||
end
|
||||
end
|
86
lib/mom/mom_collection.rb
Normal file
86
lib/mom/mom_collection.rb
Normal file
@ -0,0 +1,86 @@
|
||||
module Mom
|
||||
# The Compiler/Collection for the Mom level is a collection of Mom level Method
|
||||
# compilers These will transform to Risc MethodCompilers on the way down.
|
||||
#
|
||||
# As RubyCompiler pools source at the vool level, when several classes are compiled
|
||||
# from vool to mom, several MomCompilers get instantiated. They must be merged before
|
||||
# proceeding with translate. Thus we have a append method.
|
||||
#
|
||||
class MomCollection
|
||||
attr_reader :method_compilers
|
||||
|
||||
# Initialize with an array of risc MethodCompilers
|
||||
def initialize(compilers = [])
|
||||
@method_compilers = compilers
|
||||
end
|
||||
|
||||
# lazily instantiate the compilers for boot functions
|
||||
# (in the hope of only booting the functions once)
|
||||
def boot_compilers
|
||||
@boot_compilers ||= Risc::Builtin.boot_functions
|
||||
end
|
||||
|
||||
# Return all compilers, namely the MethodCompilers passed in, plus the
|
||||
# boot_function's compilers (boot_compilers)
|
||||
def compilers
|
||||
@method_compilers #+ boot_compilers
|
||||
end
|
||||
|
||||
# collects constants from all compilers into one array
|
||||
def constants
|
||||
compilers.inject([]){|sum ,comp| sum + comp.constants }
|
||||
end
|
||||
|
||||
# Append another MomCompilers method_compilers to this one.
|
||||
def append(mom_compiler)
|
||||
@method_compilers += mom_compiler.method_compilers
|
||||
self
|
||||
end
|
||||
|
||||
# Translate code to whatever cpu is specified.
|
||||
# Currently only :arm and :interpret
|
||||
#
|
||||
# Translating means translating the initial jump
|
||||
# and then translating all methods
|
||||
def translate( platform_sym )
|
||||
platform_sym = platform_sym.to_s.capitalize
|
||||
platform = Risc::Platform.for(platform_sym)
|
||||
assemblers = translate_methods( platform.translator )
|
||||
Risc::Linker.new(platform , assemblers , constants)
|
||||
end
|
||||
|
||||
# go through all methods and translate them to cpu, given the translator
|
||||
def translate_methods(translator)
|
||||
compilers.collect do |compiler|
|
||||
#log.debug "Translate method #{compiler.method.name}"
|
||||
translate_method(compiler , translator)
|
||||
end.flatten
|
||||
end
|
||||
|
||||
# translate one method, which means the method itself and all blocks inside it
|
||||
# returns an array of assemblers
|
||||
def translate_method( method_compiler , translator)
|
||||
all = []
|
||||
all << translate_cpu( method_compiler , translator )
|
||||
method_compiler.block_compilers.each do |block_compiler|
|
||||
all << translate_cpu(block_compiler , translator)
|
||||
end
|
||||
all
|
||||
end
|
||||
|
||||
# compile the callable (method or block) to cpu
|
||||
# return an Assembler that will then translate to binary
|
||||
def translate_cpu(compiler , translator)
|
||||
risc = compiler.risc_instructions
|
||||
cpu_instructions = risc.to_cpu(translator)
|
||||
nekst = risc.next
|
||||
while(nekst)
|
||||
cpu = nekst.to_cpu(translator) # returning nil means no replace
|
||||
cpu_instructions << cpu if cpu
|
||||
nekst = nekst.next
|
||||
end
|
||||
Risc::Assembler.new(compiler.callable , cpu_instructions )
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user