2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2014-06-14 09:59:25 +02:00
|
|
|
|
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-06 13:21:38 +02:00
|
|
|
def initialize(compiler)
|
|
|
|
@compiler = compiler
|
2018-04-06 19:55:21 +02:00
|
|
|
@names = {}
|
2015-06-01 07:34:17 +02:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
else
|
|
|
|
type = Risc.resolve_type(name , @compiler) #checking
|
|
|
|
reg = @compiler.use_reg( type.object_class.name )
|
|
|
|
end
|
2018-04-06 19:55:21 +02:00
|
|
|
@names[name] = reg
|
2018-04-06 13:54:24 +02:00
|
|
|
reg.builder = self
|
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-06 13:54:24 +02:00
|
|
|
def build(&block)
|
|
|
|
instance_eval(&block)
|
|
|
|
return built
|
|
|
|
end
|
|
|
|
def add_instruction(ins)
|
2018-04-06 15:08:35 +02:00
|
|
|
if(@built)
|
|
|
|
@built << ins
|
2018-04-06 13:54:24 +02:00
|
|
|
else
|
|
|
|
@built = ins
|
|
|
|
end
|
|
|
|
end
|
2014-06-14 09:59:25 +02:00
|
|
|
end
|
|
|
|
|
2018-04-06 13:21:38 +02:00
|
|
|
def self.build(compiler, &block)
|
2018-04-06 13:54:24 +02:00
|
|
|
Builder.new(compiler).build( &block )
|
2015-06-29 09:55:22 +02:00
|
|
|
end
|
2015-06-30 08:38:32 +02:00
|
|
|
|
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 )
|
|
|
|
return reference if reference.is_a?(RiscValue)
|
|
|
|
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-04-05 19:10:00 +02:00
|
|
|
object = object.type if object.is_a?(RiscValue)
|
|
|
|
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-06 23:14:02 +02:00
|
|
|
object = object.to_s.camelize.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)
|
|
|
|
return variable_name if variable_name.is_a?(Integer) or variable_name.is_a?(RiscValue)
|
|
|
|
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
|
|
|
|
|
2015-08-07 15:46:55 +02:00
|
|
|
|
2014-06-14 09:59:25 +02:00
|
|
|
end
|