2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2014-06-14 09:59:25 +02:00
|
|
|
|
2018-04-08 17:51:20 +02:00
|
|
|
# A Builder is used to generate code, either by using it's api, or dsl
|
|
|
|
#
|
2018-06-29 12:27:57 +02:00
|
|
|
# There are two subclasses of Builder, depending of what one wants to do with the
|
|
|
|
# generated code.
|
|
|
|
#
|
|
|
|
# CompilerBuilder: The code is added to the method_compiler.
|
2018-04-08 17:51:20 +02:00
|
|
|
# This is used to generate the builtin methods.
|
2018-06-29 12:27:57 +02:00
|
|
|
#
|
|
|
|
# CodeBuilder: The code can be stored up and returned.
|
|
|
|
# This is used in Mom::to_risc methods
|
|
|
|
#
|
2018-04-06 13:21:38 +02:00
|
|
|
class Builder
|
2014-06-14 09:59:25 +02:00
|
|
|
|
2018-04-06 23:14:02 +02:00
|
|
|
attr_reader :built , :compiler
|
2018-04-06 13:54:24 +02:00
|
|
|
|
2018-04-07 21:35:40 +02:00
|
|
|
# pass a compiler, to which instruction are added (usually)
|
2018-04-08 17:51:20 +02:00
|
|
|
# second arg determines weather instructions are added (default true)
|
|
|
|
# call build with a block to build
|
2018-06-29 12:27:57 +02:00
|
|
|
def initialize(compiler, for_source)
|
2018-04-06 13:21:38 +02:00
|
|
|
@compiler = compiler
|
2018-04-18 18:12:30 +02:00
|
|
|
@source = for_source
|
|
|
|
@source_used = false
|
2018-04-06 19:55:21 +02:00
|
|
|
@names = {}
|
2015-06-01 07:34:17 +02:00
|
|
|
end
|
|
|
|
|
2018-04-07 21:35:40 +02:00
|
|
|
# make the magic: convert incoming names into registers that have the
|
|
|
|
# type set according to the name (using resolve_type)
|
|
|
|
# anmes are stored, so subsequent calls use the same register
|
2018-04-06 13:21:38 +02:00
|
|
|
def method_missing(*args)
|
|
|
|
super if args.length != 1
|
2018-04-06 19:55:21 +02:00
|
|
|
name = args[0]
|
|
|
|
return @names[name] if @names.has_key?(name)
|
2018-04-06 21:54:54 +02:00
|
|
|
if name == :message
|
|
|
|
reg = Risc.message_reg
|
2018-04-07 23:39:35 +02:00
|
|
|
reg.builder = self
|
|
|
|
elsif name.to_s.index("label")
|
2018-04-18 18:27:46 +02:00
|
|
|
reg = Risc.label( @source , "#{name}_#{object_id}")
|
|
|
|
@source_used = true
|
2018-04-06 21:54:54 +02:00
|
|
|
else
|
|
|
|
type = Risc.resolve_type(name , @compiler) #checking
|
|
|
|
reg = @compiler.use_reg( type.object_class.name )
|
2018-04-07 23:39:35 +02:00
|
|
|
reg.builder = self
|
2018-04-06 21:54:54 +02:00
|
|
|
end
|
2018-04-06 19:55:21 +02:00
|
|
|
@names[name] = reg
|
2018-04-06 13:21:38 +02:00
|
|
|
reg
|
2014-06-14 09:59:25 +02:00
|
|
|
end
|
2018-04-06 21:40:58 +02:00
|
|
|
|
2018-04-07 23:50:51 +02:00
|
|
|
def if_zero( label )
|
2018-04-18 18:27:46 +02:00
|
|
|
@source_used = true
|
|
|
|
add_code Risc::IsZero.new(@source , label)
|
2018-04-07 23:50:51 +02:00
|
|
|
end
|
|
|
|
def if_not_zero( label )
|
2018-04-18 18:27:46 +02:00
|
|
|
@source_used = true
|
|
|
|
add_code Risc::IsNotZero.new(@source , label)
|
2018-04-07 23:50:51 +02:00
|
|
|
end
|
|
|
|
def branch( label )
|
2018-04-18 18:27:46 +02:00
|
|
|
@source_used = true
|
|
|
|
add_code Risc::Branch.new(@source, label)
|
2018-04-07 23:50:51 +02:00
|
|
|
end
|
|
|
|
|
2018-04-07 21:35:40 +02:00
|
|
|
# build code using dsl (see __init__ or MessageSetup for examples)
|
|
|
|
# names (that ruby would resolve to a variable/method) are converted
|
|
|
|
# to registers. << means assignment and [] is supported both on
|
|
|
|
# L and R values (but only one at a time). R values may also be constants.
|
2018-06-29 12:27:57 +02:00
|
|
|
#
|
2018-04-07 21:35:40 +02:00
|
|
|
# Basically this allows to create LoadConstant, RegToSlot, SlotToReg and
|
|
|
|
# Transfer instructions with extremely readable code.
|
|
|
|
# example:
|
|
|
|
# space << Parfait.object_space # load constant
|
|
|
|
# message[:receiver] << space #make current message (r0) receiver the space
|
|
|
|
#
|
2018-04-08 17:51:20 +02:00
|
|
|
# build result is available as built, but also gets added to compiler, if the
|
|
|
|
# builder is created with default args
|
2018-06-29 12:27:57 +02:00
|
|
|
#
|
2018-04-06 13:54:24 +02:00
|
|
|
def build(&block)
|
|
|
|
instance_eval(&block)
|
2018-04-08 17:51:20 +02:00
|
|
|
@built
|
2018-04-06 13:54:24 +02:00
|
|
|
end
|
2018-04-06 23:35:54 +02:00
|
|
|
|
2018-04-08 17:51:20 +02:00
|
|
|
def add_code(ins)
|
2018-06-29 12:27:57 +02:00
|
|
|
raise "Must be implemented in subclass #{self}"
|
2018-04-06 13:54:24 +02:00
|
|
|
end
|
2018-04-08 17:51:20 +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( source , from, to )
|
|
|
|
source += "add_new_int "
|
|
|
|
space = compiler.use_reg(:Space)
|
|
|
|
int = compiler.use_reg(:Integer)
|
|
|
|
space_i = Risc.resolve_to_index(:Space, :next_integer)
|
|
|
|
add_load_constant( source + "space" , Parfait.object_space , space )
|
|
|
|
add_slot_to_reg( source + "next_i1" , space , space_i , to)
|
|
|
|
add_slot_to_reg( source + "next_i2" , to , Risc.resolve_to_index(:Integer, :next_integer) , int)
|
|
|
|
add_reg_to_slot( source + "store link" , int , space , space_i )
|
|
|
|
add_reg_to_slot( source + "store value" , from , to , Parfait::Integer.integer_index)
|
|
|
|
end
|
|
|
|
|
|
|
|
# load receiver and the first argument (int)
|
|
|
|
# return both registers
|
|
|
|
def self_and_int_arg( source )
|
|
|
|
me = add_known( :receiver )
|
2018-05-14 14:17:04 +02:00
|
|
|
int_arg = load_int_arg_at(source , 0 )
|
2018-04-08 17:51:20 +02:00
|
|
|
return me , int_arg
|
|
|
|
end
|
|
|
|
|
|
|
|
# Load the first argument, assumed to be integer
|
|
|
|
def load_int_arg_at( source , at)
|
|
|
|
int_arg = compiler.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-05-14 14:17:04 +02:00
|
|
|
|
2018-04-08 17:51:20 +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
|
|
|
|
|
|
|
|
# for computationally building code (ie writing assembler) these short cuts
|
|
|
|
# help to instantiate risc instructions and add them immediately
|
|
|
|
[:label, :reg_to_slot , :slot_to_reg , :load_constant, :load_data,
|
|
|
|
:function_return , :function_call, :op ,
|
|
|
|
:transfer , :reg_to_slot , :byte_to_reg , :reg_to_byte].each do |method|
|
|
|
|
define_method("add_#{method}".to_sym) do |*args|
|
2018-04-18 18:27:46 +02:00
|
|
|
if not @source_used
|
|
|
|
args[0] = @source
|
|
|
|
@source_used = true
|
|
|
|
end
|
2018-04-08 17:51:20 +02:00
|
|
|
add_code Risc.send( method , *args )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_known(name)
|
|
|
|
case name
|
|
|
|
when :receiver
|
2018-06-29 13:48:52 +02:00
|
|
|
ret = compiler.use_reg compiler.method.for_type
|
2018-04-08 17:51:20 +02:00
|
|
|
add_slot_to_reg(" load self" , :message , :receiver , ret )
|
|
|
|
return ret
|
|
|
|
when :space
|
|
|
|
space = Parfait.object_space
|
|
|
|
reg = compiler.use_reg :Space , space
|
|
|
|
add_load_constant( "load space", space , reg )
|
|
|
|
return reg
|
|
|
|
when :message
|
|
|
|
reg = compiler.use_reg :Message
|
|
|
|
add_transfer( "load message", Risc.message_reg , reg )
|
|
|
|
return reg
|
|
|
|
else
|
|
|
|
raise "Unknow expression #{name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-14 09:59:25 +02:00
|
|
|
end
|
|
|
|
|
2018-04-06 13:21:38 +02:00
|
|
|
# if a symbol is given, it may be the message or the new_message.
|
|
|
|
# These are mapped to register references.
|
|
|
|
# The valid symbols (:message,:new_message) are the same that are returned
|
|
|
|
# by the slots. All data (at any time) is in one of the instance variables of these two
|
|
|
|
# objects. Risc defines module methods with the same names (and _reg)
|
|
|
|
def self.resolve_to_register( reference )
|
2018-06-29 10:39:07 +02:00
|
|
|
return reference if reference.is_a?(RegisterValue)
|
2018-04-06 13:21:38 +02:00
|
|
|
case reference
|
|
|
|
when :message
|
|
|
|
return message_reg
|
|
|
|
when :new_message
|
|
|
|
return new_message_reg
|
|
|
|
else
|
|
|
|
raise "not recognized register reference #{reference} #{reference.class}"
|
|
|
|
end
|
2015-06-29 09:55:22 +02:00
|
|
|
end
|
|
|
|
|
2015-06-30 08:38:32 +02:00
|
|
|
# The first arg is a class name (possibly lowercase) and the second an instance variable name.
|
2018-04-06 13:21:38 +02:00
|
|
|
def self.resolve_type( object , compiler )
|
2018-06-29 10:39:07 +02:00
|
|
|
object = object.type if object.is_a?(RegisterValue)
|
2018-04-05 19:10:00 +02:00
|
|
|
case object
|
2018-04-06 21:40:58 +02:00
|
|
|
when :name
|
|
|
|
type = Parfait.object_space.get_class_by_name( :Word ).instance_type
|
2018-04-05 19:10:00 +02:00
|
|
|
when :frame
|
|
|
|
type = compiler.method.frame_type
|
|
|
|
when :message , :next_message , :caller
|
|
|
|
type = Parfait.object_space.get_class_by_name(:Message).instance_type
|
|
|
|
when :arguments
|
|
|
|
type = compiler.method.arguments_type
|
|
|
|
when :receiver
|
|
|
|
type = compiler.method.for_type
|
|
|
|
when Parfait::Object
|
|
|
|
type = Parfait.object_space.get_class_by_name( object.class.name.split("::").last.to_sym).instance_type
|
|
|
|
when Symbol
|
2018-04-16 20:24:27 +02:00
|
|
|
object = object.to_s.camelise.to_sym
|
2018-04-05 19:10:00 +02:00
|
|
|
clazz = Parfait.object_space.get_class_by_name(object)
|
|
|
|
raise "Not implemented/found object #{object}:#{object.class}" unless clazz
|
|
|
|
type = clazz.instance_type
|
|
|
|
else
|
|
|
|
raise "Not implemented/found object #{object}:#{object.class}"
|
|
|
|
end
|
2018-04-06 13:21:38 +02:00
|
|
|
return type
|
|
|
|
end
|
|
|
|
|
|
|
|
# The first arg is a class name (possibly lowercase) and the second an instance variable name.
|
|
|
|
# By looking up the class and the type for that class, we can resolve the instance
|
|
|
|
# variable name to an index.
|
|
|
|
# The class can be mapped to a register, and so we get a memory address (reg+index)
|
|
|
|
# Third arg, compiler, is only needed to resolve receiver/arguments/frame
|
|
|
|
def self.resolve_to_index(object , variable_name ,compiler = nil)
|
2018-06-29 10:39:07 +02:00
|
|
|
return variable_name if variable_name.is_a?(Integer) or variable_name.is_a?(RegisterValue)
|
2018-04-06 13:21:38 +02:00
|
|
|
type = resolve_type(object , compiler)
|
2018-04-05 19:10:00 +02:00
|
|
|
index = type.variable_index(variable_name)
|
|
|
|
raise "Index not found for #{variable_name} in #{object} of type #{type}" unless index
|
|
|
|
return index
|
2015-06-29 09:55:22 +02:00
|
|
|
end
|
|
|
|
|
2018-06-29 12:27:57 +02:00
|
|
|
class CodeBuilder < Builder
|
|
|
|
|
|
|
|
attr_reader :built
|
|
|
|
def initialize(compiler, for_source)
|
|
|
|
super
|
|
|
|
@built = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def build(&block)
|
|
|
|
super
|
|
|
|
@built
|
|
|
|
end
|
|
|
|
|
|
|
|
# CodeBuilder stores the code.
|
|
|
|
# The code can be access through the @built instance, and is returned
|
|
|
|
# from build method
|
|
|
|
def add_code(ins)
|
|
|
|
if(@built)
|
|
|
|
@built << ins
|
|
|
|
else
|
|
|
|
@built = ins
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-29 13:48:52 +02:00
|
|
|
# A CompilerBuilder adds the generated code to the RiscCompiler.
|
2018-06-29 12:27:57 +02:00
|
|
|
#
|
|
|
|
class CompilerBuilder < Builder
|
|
|
|
# add code straight to the compiler
|
|
|
|
def add_code(ins)
|
|
|
|
return @compiler.add_code(ins)
|
|
|
|
end
|
|
|
|
end
|
2015-08-07 15:46:55 +02:00
|
|
|
|
2014-06-14 09:59:25 +02:00
|
|
|
end
|