2018-03-11 12:32:42 +01:00
|
|
|
module Risc
|
2016-12-09 12:56:13 +01:00
|
|
|
|
2018-06-30 22:26:28 +02:00
|
|
|
# MethodCompiler (old name) is used to generate risc instructions for methods
|
2018-03-11 12:32:42 +01:00
|
|
|
# and to instantiate the methods correctly. Most of the init is typed layer stuff,
|
|
|
|
# but there is some logic too.
|
2015-10-28 20:36:41 +01:00
|
|
|
|
2018-07-10 21:02:11 +02:00
|
|
|
class MethodCompiler < CallableCompiler
|
2015-05-08 14:10:30 +02:00
|
|
|
|
2017-01-17 20:23:58 +01:00
|
|
|
def initialize( method )
|
2018-07-30 09:26:11 +02:00
|
|
|
super(method)
|
2015-09-19 17:56:18 +02:00
|
|
|
end
|
2018-06-29 12:03:19 +02:00
|
|
|
|
2018-08-01 15:27:34 +02:00
|
|
|
#include block_compilers constants
|
|
|
|
def constants
|
|
|
|
block_compilers.inject(@constants.dup){|all, compiler| all += compiler.constants}
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:02:11 +02:00
|
|
|
def source_name
|
2018-07-30 09:26:11 +02:00
|
|
|
"#{@callable.self_type.name}.#{@callable.name}"
|
2018-07-10 21:02:11 +02:00
|
|
|
end
|
2018-07-30 09:26:11 +02:00
|
|
|
|
2018-07-09 18:32:17 +02:00
|
|
|
def get_method
|
2018-07-30 09:26:11 +02:00
|
|
|
@callable
|
2018-07-09 18:32:17 +02:00
|
|
|
end
|
2018-07-30 09:26:11 +02:00
|
|
|
|
2018-07-09 18:32:17 +02:00
|
|
|
# sometimes the method is used as source (tb reviewed)
|
|
|
|
def source
|
2018-07-30 09:26:11 +02:00
|
|
|
@callable
|
2018-07-09 18:32:17 +02:00
|
|
|
end
|
2018-07-30 09:26:11 +02:00
|
|
|
|
2018-06-29 12:03:19 +02:00
|
|
|
# helper method for builtin mainly
|
|
|
|
# the class_name is a symbol, which is resolved to the instance_type of that class
|
|
|
|
#
|
2018-07-09 15:48:23 +02:00
|
|
|
# return compiler_for_type with the resolved type
|
2018-06-29 12:03:19 +02:00
|
|
|
#
|
|
|
|
def self.compiler_for_class( class_name , method_name , args , frame )
|
2015-10-28 20:36:41 +01:00
|
|
|
raise "create_method #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol
|
2016-12-30 13:10:49 +01:00
|
|
|
clazz = Parfait.object_space.get_class_by_name! class_name
|
2018-07-09 15:48:23 +02:00
|
|
|
compiler_for_type( clazz.instance_type , method_name , args , frame)
|
2015-10-28 20:36:41 +01:00
|
|
|
end
|
|
|
|
|
2018-07-09 18:32:17 +02:00
|
|
|
def add_method_to( target )
|
2018-07-30 09:26:11 +02:00
|
|
|
target.add_method( @callable )
|
2018-07-09 18:32:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_block(arg_type , frame_type)
|
2018-07-30 09:26:11 +02:00
|
|
|
@callable.create_block(arg_type ,frame_type)
|
2018-07-09 18:32:17 +02:00
|
|
|
end
|
2018-07-30 09:26:11 +02:00
|
|
|
|
2016-12-14 12:24:42 +01:00
|
|
|
# create a method for the given type ( Parfait type object)
|
2015-10-28 20:36:41 +01:00
|
|
|
# method_name is a Symbol
|
2016-12-14 12:24:42 +01:00
|
|
|
# args a hash that will be converted to a type
|
|
|
|
# the created method is set as the current and the given type too
|
2018-06-29 12:03:19 +02:00
|
|
|
# return the compiler
|
2018-07-09 15:48:23 +02:00
|
|
|
def self.compiler_for_type( type , method_name , args , frame)
|
2016-12-14 12:24:42 +01:00
|
|
|
raise "create_method #{type.inspect} is not a Type" unless type.is_a? Parfait::Type
|
2018-03-18 17:38:35 +01:00
|
|
|
raise "Args must be Type #{args}" unless args.is_a?(Parfait::Type)
|
2015-10-28 20:36:41 +01:00
|
|
|
raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol
|
2018-03-18 17:38:35 +01:00
|
|
|
method = type.create_method( method_name , args , frame)
|
2017-01-17 20:23:58 +01:00
|
|
|
self.new(method)
|
2015-10-28 20:36:41 +01:00
|
|
|
end
|
2018-07-09 15:48:23 +02:00
|
|
|
|
2018-07-09 16:53:56 +02:00
|
|
|
# determine how given name need to be accsessed.
|
|
|
|
# For methods the options are args or frame
|
|
|
|
def slot_type_for(name)
|
2018-07-30 09:26:11 +02:00
|
|
|
if @callable.arguments_type.variable_index(name)
|
2018-07-09 16:53:56 +02:00
|
|
|
type = :arguments
|
|
|
|
else
|
|
|
|
type = :frame
|
|
|
|
end
|
|
|
|
[type , name]
|
|
|
|
end
|
|
|
|
|
2018-07-09 15:48:23 +02:00
|
|
|
def add_block_compiler(compiler)
|
|
|
|
@block_compilers << compiler
|
|
|
|
end
|
|
|
|
|
2018-07-09 16:53:56 +02:00
|
|
|
# return true or false if the given name is in scope (arg/local)
|
|
|
|
def in_scope?(name)
|
2018-07-30 09:26:11 +02:00
|
|
|
ret = true if @callable.arguments_type.variable_index(name)
|
|
|
|
ret = @callable.frame_type.variable_index(name) unless ret
|
2018-07-09 16:53:56 +02:00
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2018-07-16 11:03:40 +02:00
|
|
|
|
|
|
|
# return the frame type, ie the method frame type
|
|
|
|
def frame_type
|
2018-07-30 09:26:11 +02:00
|
|
|
@callable.frame_type
|
2018-07-16 11:03:40 +02:00
|
|
|
end
|
|
|
|
# return the frame type, ie the method arguments type
|
|
|
|
def arg_type
|
2018-07-30 09:26:11 +02:00
|
|
|
@callable.arguments_type
|
2018-07-16 11:03:40 +02:00
|
|
|
end
|
|
|
|
# return the frame type, ie the method self_type
|
|
|
|
def receiver_type
|
2018-07-30 09:26:11 +02:00
|
|
|
@callable.self_type
|
2018-07-09 17:16:51 +02:00
|
|
|
end
|
|
|
|
|
2018-03-14 13:09:04 +01:00
|
|
|
# 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 )
|
2018-03-19 08:49:42 +01:00
|
|
|
raise "whats this a #{instruction}" unless instruction.is_a?(Mom::Instruction)
|
|
|
|
#puts "adding mom #{instruction.to_s}:#{instruction.next.to_s}"
|
2018-03-14 13:09:04 +01:00
|
|
|
risc = instruction.to_risc( self )
|
|
|
|
add_code(risc)
|
2018-04-08 21:29:08 +02:00
|
|
|
reset_regs
|
2018-03-19 08:49:42 +01:00
|
|
|
#puts "adding risc #{risc.to_s}:#{risc.next.to_s}"
|
2018-03-14 13:09:04 +01:00
|
|
|
instruction = instruction.next
|
|
|
|
end
|
|
|
|
end
|
2018-04-07 21:35:40 +02:00
|
|
|
|
2018-03-14 13:09:04 +01:00
|
|
|
# add a risc instruction after the current (insertion point)
|
2015-10-28 20:36:41 +01:00
|
|
|
# the added instruction will become the new insertion point
|
2018-03-14 13:09:04 +01:00
|
|
|
def add_code( instruction )
|
2018-03-17 06:43:44 +01:00
|
|
|
raise "Not an instruction:#{instruction.to_s}" unless instruction.is_a?(Risc::Instruction)
|
2016-12-28 18:20:16 +01:00
|
|
|
raise instruction.to_s if( instruction.class.name.split("::").first == "Arm")
|
2018-03-19 08:49:42 +01:00
|
|
|
new_current = instruction.last #after insertion this point is lost
|
2015-10-28 20:36:41 +01:00
|
|
|
@current.insert(instruction) #insert after current
|
2018-03-19 08:49:42 +01:00
|
|
|
@current = new_current
|
2015-10-28 20:36:41 +01:00
|
|
|
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
|
2018-07-16 10:23:09 +02:00
|
|
|
def use_reg( type , extra = {} )
|
2016-12-14 12:24:42 +01:00
|
|
|
raise "Not type #{type.inspect}" unless type.is_a?(Symbol) or type.is_a?(Parfait::Type)
|
2015-10-10 10:05:55 +02:00
|
|
|
if @regs.empty?
|
2018-07-16 10:23:09 +02:00
|
|
|
reg = Risc.tmp_reg(type , extra)
|
2015-10-10 10:05:55 +02:00
|
|
|
else
|
2018-07-16 10:23:09 +02:00
|
|
|
reg = @regs.last.next_reg_use(type , extra)
|
2015-10-10 10:05:55 +02:00
|
|
|
end
|
|
|
|
@regs << reg
|
|
|
|
return reg
|
|
|
|
end
|
|
|
|
|
2016-12-28 18:20:16 +01:00
|
|
|
def copy( reg , source )
|
2015-11-13 19:47:08 +01:00
|
|
|
copied = use_reg reg.type
|
2018-03-14 13:09:04 +01:00
|
|
|
add_code Register.transfer( source , reg , copied )
|
2015-11-13 19:47:08 +01:00
|
|
|
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
|
2018-03-14 13:09:04 +01:00
|
|
|
def release_reg( reg )
|
2015-10-10 10:05:55 +02:00
|
|
|
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
|
|
|
|
2018-04-07 21:35:40 +02:00
|
|
|
# Build with builder (see there), adding the created instructions
|
|
|
|
def build(&block)
|
2018-04-07 22:07:44 +02:00
|
|
|
builder.build(&block)
|
2018-04-07 21:35:40 +02:00
|
|
|
end
|
|
|
|
|
2018-06-29 12:27:57 +02:00
|
|
|
# return a new code builder that uses this compiler
|
|
|
|
# CodeBuilder returns code after building
|
|
|
|
def code_builder( source)
|
|
|
|
CodeBuilder.new(self , source)
|
|
|
|
end
|
|
|
|
|
|
|
|
# return a CompilerBuilder
|
|
|
|
# CompilerBuilder adds the generated code to the compiler
|
|
|
|
def compiler_builder( source)
|
|
|
|
CompilerBuilder.new(self , source)
|
2018-04-07 21:35:40 +02:00
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
|
|
|
end
|