diff --git a/lib/risc/builder.rb b/lib/risc/builder.rb index 24e83c6b..ad5839af 100644 --- a/lib/risc/builder.rb +++ b/lib/risc/builder.rb @@ -171,7 +171,11 @@ module Risc end end - # The first arg is a class name (possibly lowercase) and the second an instance variable name. + # resolve a symbol to a type. In the simplest case the sybbol is the class name + # But in building sometimes variations are needed, so next_message or caller work + # too (and return Message) + # Also objects work, in which case the instance_type of their class is returned + # An error is raised if the symbol/object can not be resolved def self.resolve_type( object , compiler ) object = object.type if object.is_a?(RegisterValue) case object @@ -205,7 +209,8 @@ module Risc # 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?(RegisterValue) - type = resolve_type(object , compiler) + type = compiler.resolve_type( object) if compiler + type = resolve_type(object , compiler) unless type index = type.variable_index(variable_name) raise "Index not found for #{variable_name} in #{object} of type #{type}" unless index return index diff --git a/lib/risc/method_compiler.rb b/lib/risc/method_compiler.rb index 1e60c1fd..522b8e28 100644 --- a/lib/risc/method_compiler.rb +++ b/lib/risc/method_compiler.rb @@ -69,6 +69,21 @@ module Risc ret end + # resolve a symbol to a type. Allowed symbols are :frame , :receiver and arguments + # which return the respective types, otherwise nil + def resolve_type( name ) + case name + when :frame + return @method.frame_type + when :arguments + return @method.arguments_type + when :receiver + return @method.self_type + else + return nil + 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) diff --git a/test/mom/instruction/helper.rb b/test/mom/instruction/helper.rb index e03adb27..968115db 100644 --- a/test/mom/instruction/helper.rb +++ b/test/mom/instruction/helper.rb @@ -2,6 +2,11 @@ require_relative '../helper' module Mom class CompilerMock + # resolve a symbol to a type. Allowed symbols are :frame , :receiver and arguments + # which return the respective types, otherwise nil + def resolve_type( name ) + return nil + end def use_reg( type ) Risc.tmp_reg(type , nil) end