2018-03-11 12:32:42 +01:00
|
|
|
module Risc
|
2016-12-09 12:56:13 +01:00
|
|
|
|
2018-03-11 12:32:42 +01:00
|
|
|
# MethodCompiler (old name) is used to generate risc instructions for methods
|
|
|
|
# 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
|
|
|
|
2016-12-18 13:15:19 +01:00
|
|
|
class MethodCompiler
|
2015-05-08 14:10:30 +02:00
|
|
|
|
2017-01-17 20:23:58 +01:00
|
|
|
def initialize( method )
|
2015-10-10 10:05:55 +02:00
|
|
|
@regs = []
|
2017-01-17 20:23:58 +01:00
|
|
|
if method == :main
|
2016-12-30 13:10:49 +01:00
|
|
|
@type = Parfait.object_space.get_type()
|
2016-12-30 12:33:07 +01:00
|
|
|
@method = @type.get_method( :main )
|
|
|
|
@method = @type.create_method( :main ,{}) unless @method
|
2017-01-17 20:23:58 +01:00
|
|
|
else
|
|
|
|
@method = method
|
|
|
|
@type = method.for_type
|
2016-12-16 23:17:35 +01:00
|
|
|
end
|
2018-03-25 18:37:51 +02:00
|
|
|
@current = @method.risc_instructions
|
2015-09-19 17:56:18 +02:00
|
|
|
end
|
2016-12-14 12:24:42 +01:00
|
|
|
attr_reader :type , :method
|
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
|
2018-03-18 17:38:35 +01:00
|
|
|
def self.create_method( 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-03-18 17:38:35 +01:00
|
|
|
create_method_for( clazz.instance_type , method_name , args , frame)
|
2015-10-28 20:36:41 +01:00
|
|
|
end
|
|
|
|
|
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
|
2015-10-28 20:36:41 +01:00
|
|
|
# return the compiler (for chaining)
|
2018-03-18 17:38:35 +01:00
|
|
|
def self.create_method_for( 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-03-11 12:32:42 +01:00
|
|
|
def add_known(name)
|
|
|
|
case name
|
2018-03-14 15:56:13 +01:00
|
|
|
when :receiver
|
2018-03-11 12:32:42 +01:00
|
|
|
ret = use_reg @type
|
|
|
|
add_slot_to_reg(" load self" , :message , :receiver , ret )
|
|
|
|
return ret
|
|
|
|
when :space
|
|
|
|
space = Parfait.object_space
|
|
|
|
reg = use_reg :Space , space
|
|
|
|
add_load_constant( "load space", space , reg )
|
|
|
|
return reg
|
|
|
|
when :message
|
|
|
|
reg = use_reg :Message
|
|
|
|
add_transfer( "load message", Risc.message_reg , reg )
|
|
|
|
return reg
|
|
|
|
else
|
|
|
|
raise "Unknow expression #{name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-28 20:36:41 +01:00
|
|
|
# set the insertion point (where code is added with add_code)
|
|
|
|
def set_current c
|
|
|
|
@current = c
|
|
|
|
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-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
|
|
|
|
# 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
|
|
|
|
2018-03-14 13:09:04 +01:00
|
|
|
# for computationally building code (ie writing assembler) these short cuts
|
|
|
|
# help to instantiate risc instructions and add them immediately
|
2018-03-31 12:25:59 +02:00
|
|
|
[:label, :reg_to_slot , :slot_to_reg , :load_constant, :load_data,
|
2018-03-31 11:38:30 +02:00
|
|
|
:function_return , :function_call,
|
2016-12-28 19:37:54 +01:00
|
|
|
:transfer , :reg_to_slot , :byte_to_reg , :reg_to_byte].each do |method|
|
|
|
|
define_method("add_#{method}".to_sym) do |*args|
|
2017-01-19 08:02:29 +01:00
|
|
|
add_code Risc.send( method , *args )
|
2016-12-28 19:37:54 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-10 10:05:55 +02:00
|
|
|
# require a (temporary) register. code must give this back with release_reg
|
2016-12-28 18:20:16 +01:00
|
|
|
def use_reg( type , value = nil )
|
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?
|
2017-01-19 08:02:29 +01:00
|
|
|
reg = Risc.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
|
|
|
|
|
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-01 13:01:17 +02:00
|
|
|
# move a machine int from register "from" to a Parfait::Integer in register "to"
|
|
|
|
# have to grab an integer from space and stick it in the "to" register first.
|
|
|
|
def add_new_int( from, to )
|
2018-03-31 19:21:27 +02:00
|
|
|
source = "add_new_int "
|
|
|
|
space = use_reg(:Space)
|
|
|
|
int = use_reg(:Integer)
|
|
|
|
space_i = Risc.resolve_to_index(:Space, :next_integer)
|
|
|
|
add_load_constant( source + "space" , Parfait.object_space , space )
|
2018-04-01 13:09:30 +02:00
|
|
|
add_slot_to_reg( source + "next_i1" , space , space_i , to)
|
2018-04-01 13:01:17 +02:00
|
|
|
add_slot_to_reg( source + "next_i2" , to , Risc.resolve_to_index(:Integer, :next_integer) , int)
|
2018-03-31 19:21:27 +02:00
|
|
|
add_reg_to_slot( source + "store link" , int , space , space_i )
|
2018-04-01 13:01:17 +02:00
|
|
|
add_reg_to_slot( source + "store value" , from , to , Parfait::Integer.integer_index)
|
2018-03-31 19:21:27 +02:00
|
|
|
end
|
2018-03-31 12:25:59 +02:00
|
|
|
def add_constant(const)
|
|
|
|
Risc.machine.add_constant(const)
|
|
|
|
end
|
2018-04-01 13:09:30 +02:00
|
|
|
|
|
|
|
# load receiver and the first argument (int)
|
|
|
|
# return both registers
|
|
|
|
def self_and_int_arg( source )
|
|
|
|
me = add_known( :receiver )
|
|
|
|
int_arg = load_int_arg_at(source , 1 )
|
|
|
|
return me , int_arg
|
|
|
|
end
|
|
|
|
|
|
|
|
# Load the first argument, assumed to be integer
|
|
|
|
def load_int_arg_at( source , at)
|
|
|
|
int_arg = use_reg :Integer
|
|
|
|
add_slot_to_reg(source , :message , :arguments , int_arg )
|
|
|
|
add_slot_to_reg(source , int_arg , at + 1, int_arg ) #1 for type
|
|
|
|
return int_arg
|
|
|
|
end
|
|
|
|
|
2018-04-01 13:50:13 +02:00
|
|
|
# assumed Integer in given register is replaced by the fixnum that it is holding
|
|
|
|
def reduce_int( source , register )
|
|
|
|
add_slot_to_reg( source + "int -> fix" , register , Parfait::Integer.integer_index , register)
|
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
|
|
|
end
|