From 29363e7f726e2f961d0bea780730de3293519fba Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 16 Jul 2018 12:03:40 +0300 Subject: [PATCH] split compilers resolve_type into the three possibilities cleaner code, though temporary it shifts some dirt into the index method. up next --- lib/risc/block_compiler.rb | 26 ++++++++++++-------------- lib/risc/builder.rb | 17 ++++++++++++++--- lib/risc/builtin/object.rb | 2 +- lib/risc/callable_compiler.rb | 8 ++++++-- lib/risc/method_compiler.rb | 25 ++++++++++++------------- lib/vool/basic_values.rb | 2 +- lib/vool/send_statement.rb | 2 +- 7 files changed, 47 insertions(+), 35 deletions(-) diff --git a/lib/risc/block_compiler.rb b/lib/risc/block_compiler.rb index 86f0dfc4..236d6387 100644 --- a/lib/risc/block_compiler.rb +++ b/lib/risc/block_compiler.rb @@ -21,7 +21,7 @@ module Risc # or then the methods arg or frame def slot_type_for(name) if @block.arguments_type.variable_index(name) - slot_def = [ :arguments] + slot_def = [:arguments] elsif @block.frame_type.variable_index(name) slot_def = [:frame] elsif @method.arguments_type.variable_index(name) @@ -34,19 +34,17 @@ module Risc slot_def << name 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 @block.frame_type - when :arguments - return @block.arguments_type - when :receiver - return @block.self_type - else - return nil - end + # return the frame type, ie the blocks frame type + def frame_type + @block.frame_type + end + # return the frame type, ie the blocks arguments type + def arg_type + @block.arguments_type + end + # return the frame type, ie the blocks self_type + def receiver_type + @block.self_type end end diff --git a/lib/risc/builder.rb b/lib/risc/builder.rb index 05fc0a8c..711882d6 100644 --- a/lib/risc/builder.rb +++ b/lib/risc/builder.rb @@ -183,8 +183,12 @@ module Risc case object when :name type = Parfait.object_space.get_type_by_class_name( :Word ) - when :frame , :arguments , :receiver - type = compiler.resolve_type(object) + when :frame + type = compiler.frame_type + when :arguments + type = compiler.arg_type + when :receiver + type = compiler.receiver_type when :message , :next_message , :caller type = Parfait.object_space.get_type_by_class_name(:Message) when Parfait::Object @@ -207,7 +211,14 @@ 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 = compiler.resolve_type( object) if compiler + case object + when :frame + type = compiler.frame_type + when :arguments + type = compiler.arg_type + when :receiver + type = compiler.receiver_type + end if compiler type = resolve_type(object , compiler) unless type #puts "TYPE #{type} obj:#{object} var:#{variable_name} comp:#{compiler}" index = type.variable_index(variable_name) diff --git a/lib/risc/builtin/object.rb b/lib/risc/builtin/object.rb index fa93e6ab..c2accbfa 100644 --- a/lib/risc/builtin/object.rb +++ b/lib/risc/builtin/object.rb @@ -63,7 +63,7 @@ module Risc message[:receiver] << space end - exit_label = Risc.label(compiler.source , "#{compiler.resolve_type(:receiver).object_class.name}.#{compiler.source.name}" ) + exit_label = Risc.label(compiler.source , "#{compiler.receiver_type.object_class.name}.#{compiler.source.name}" ) ret_tmp = compiler.use_reg(:Label) builder.build do add_load_constant("__init__ load return", exit_label , ret_tmp) diff --git a/lib/risc/callable_compiler.rb b/lib/risc/callable_compiler.rb index 7462ae03..c060aeba 100644 --- a/lib/risc/callable_compiler.rb +++ b/lib/risc/callable_compiler.rb @@ -69,8 +69,12 @@ module Risc # scope related slots are resolved by the compiler by methood/block def slot_type( slot , type) case slot - when :frame , :arguments , :receiver - new_type = self.resolve_type(slot) + when :frame + new_type = self.frame_type + when :arguments + new_type = self.arg_type + when :receiver + new_type = self.receiver_type when Symbol new_type = type.type_for(slot) raise "Not found object #{slot}: in #{type}" unless new_type diff --git a/lib/risc/method_compiler.rb b/lib/risc/method_compiler.rb index 194895ab..6ded7a32 100644 --- a/lib/risc/method_compiler.rb +++ b/lib/risc/method_compiler.rb @@ -74,19 +74,18 @@ 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 + + # return the frame type, ie the method frame type + def frame_type + @method.frame_type + end + # return the frame type, ie the method arguments type + def arg_type + @method.arguments_type + end + # return the frame type, ie the method self_type + def receiver_type + @method.self_type end # convert the given mom instruction to_risc and then add it (see add_code) diff --git a/lib/vool/basic_values.rb b/lib/vool/basic_values.rb index ebe07aff..fff84829 100644 --- a/lib/vool/basic_values.rb +++ b/lib/vool/basic_values.rb @@ -70,7 +70,7 @@ module Vool @my_type = type end def slot_definition(compiler) - @my_type = compiler.resolve_type(:receiver) + @my_type = compiler.receiver_type Mom::SlotDefinition.new(:message , [:receiver]) end def ct_type diff --git a/lib/vool/send_statement.rb b/lib/vool/send_statement.rb index ea8497d8..b9629c53 100644 --- a/lib/vool/send_statement.rb +++ b/lib/vool/send_statement.rb @@ -72,7 +72,7 @@ module Vool # - Setting up the next message, with receiver, arguments, and (importantly) return address # - a CachedCall , or a SimpleCall, depending on wether the receiver type can be determined def to_mom( compiler ) - @receiver = SelfExpression.new(compiler.resolve_type(:receiver)) if @receiver.is_a?(SelfExpression) + @receiver = SelfExpression.new(compiler.receiver_type) if @receiver.is_a?(SelfExpression) if(@receiver.ct_type) simple_call(compiler) else