From c60949fe2438943c47a321a3a1a7cf7634401bb0 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 28 Dec 2016 21:10:14 +0200 Subject: [PATCH] small refactor on arg loading --- lib/register/instructions/branch.rb | 2 +- lib/register/instructions/getter.rb | 2 +- lib/register/instructions/label.rb | 2 +- lib/register/instructions/setter.rb | 2 +- lib/typed/method_compiler/call_site.rb | 24 ++++++++------ lib/typed/method_compiler/name_expression.rb | 34 +++++++++++--------- 6 files changed, 36 insertions(+), 30 deletions(-) diff --git a/lib/register/instructions/branch.rb b/lib/register/instructions/branch.rb index 27dce008..b3372633 100644 --- a/lib/register/instructions/branch.rb +++ b/lib/register/instructions/branch.rb @@ -10,7 +10,7 @@ module Register attr_reader :label def to_s - "#{self.class.name}: #{label ? label.name : ''}" + "#{self.class.name.split("::").last}: #{label ? label.name : ''}" end alias :inspect :to_s diff --git a/lib/register/instructions/getter.rb b/lib/register/instructions/getter.rb index 2e04f40d..de1397ad 100644 --- a/lib/register/instructions/getter.rb +++ b/lib/register/instructions/getter.rb @@ -30,7 +30,7 @@ module Register attr_accessor :array , :index , :register def to_s - "#{self.class.name}: #{array}[#{index}] -> #{register}" + "#{self.class.name.split("::").last}: #{array}[#{index}] -> #{register}" end end diff --git a/lib/register/instructions/label.rb b/lib/register/instructions/label.rb index f37944fe..b541290b 100644 --- a/lib/register/instructions/label.rb +++ b/lib/register/instructions/label.rb @@ -15,7 +15,7 @@ module Register attr_reader :name def to_s - "Label: #{@name} (#{self.next.class})" + "Label: #{@name} (#{self.next.class.name.split("::").last})" end def sof_reference_name @name diff --git a/lib/register/instructions/setter.rb b/lib/register/instructions/setter.rb index e7935e07..1f57c698 100644 --- a/lib/register/instructions/setter.rb +++ b/lib/register/instructions/setter.rb @@ -28,7 +28,7 @@ module Register attr_accessor :register , :array , :index def to_s - "#{self.class.name}: #{register} -> #{array}[#{index}]" + "#{self.class.name.split("::").last}: #{register} -> #{array}[#{index}]" end end diff --git a/lib/typed/method_compiler/call_site.rb b/lib/typed/method_compiler/call_site.rb index 6997dfb1..0157d4a6 100644 --- a/lib/typed/method_compiler/call_site.rb +++ b/lib/typed/method_compiler/call_site.rb @@ -82,17 +82,21 @@ module Typed message = "Arg number mismatch, method=#{arg_type.instance_length - 1} , call=#{arguments.length}" raise message if (arg_type.instance_length - 1 ) != arguments.length arguments.each_with_index do |arg , i | - reset_regs - # processing should return the register with the value - val = process( arg) - raise "Not register #{val}" unless val.is_a?(Register::RegisterValue) - #FIXME definately needs some tests - raise "TypeMismatch calling with #{val.type} , instead of #{arg_type.type_at(i + 2)}" if val.type != arg_type.type_at(i + 2) - list_reg = use_reg(:NamedList , arguments ) - add_slot_to_reg( "Set arg #{i}:#{arg}" , :new_message , :arguments , list_reg ) - # which we load int the new_message at the argument's index - add_reg_to_slot( arg , val , list_reg , i + 2 ) #one for type and one for ruby + store_arg_no(arguments , arg_type , arg , i + 1) #+1 for ruby(0 based) end end + + def store_arg_no(arguments , arg_type , arg , i ) + reset_regs + i = i + 1 # disregarding type field + val = process( arg) # processing should return the register with the value + raise "Not register #{val}" unless val.is_a?(Register::RegisterValue) + #FIXME definately needs some tests + raise "TypeMismatch calling with #{val.type} , instead of #{arg_type.type_at(i)}" if val.type != arg_type.type_at(i) + list_reg = use_reg(:NamedList , arguments ) + add_slot_to_reg( "Set arg #{i}:#{arg}" , :new_message , :arguments , list_reg ) + # which we load int the new_message at the argument's index + add_reg_to_slot( arg , val , list_reg , i ) #one for type and one for ruby + end end end diff --git a/lib/typed/method_compiler/name_expression.rb b/lib/typed/method_compiler/name_expression.rb index c3f452f7..9f566d34 100644 --- a/lib/typed/method_compiler/name_expression.rb +++ b/lib/typed/method_compiler/name_expression.rb @@ -8,24 +8,26 @@ module Typed def on_NameExpression statement name = statement.name [:self , :space , :message].each do |special| - return send(:"handle_special_#{special}" , statement ) if name == special + return send(:"load_special_#{special}" , statement ) if name == special end - # either an argument, so it's stored in message - if( index = @method.has_arg(name)) - named_list = use_reg :NamedList - ret = use_reg @method.argument_type(index) - #puts "For #{name} at #{index} got #{@method.arguments.inspect}" - add_slot_to_reg("#{statement} load args" , :message , :arguments, named_list ) - add_slot_to_reg("#{statement} load #{name}" , named_list , index + 1, ret ) - return ret - end - # or a local so it is in the named_list - handle_local(statement) + return load_argument(statement) if( @method.has_arg(name)) + load_local(statement) end private - def handle_local( statement ) + def load_argument(statement) + name = statement.name + index = @method.has_arg(name) + named_list = use_reg :NamedList + ret = use_reg @method.argument_type(index) + #puts "For #{name} at #{index} got #{@method.arguments.inspect}" + add_slot_to_reg("#{statement} load args" , :message , :arguments, named_list ) + add_slot_to_reg("#{statement} load #{name}" , named_list , index + 1, ret ) + return ret + end + + def load_local( statement ) name = statement.name index = @method.has_local( name ) raise "must define variable '#{name}' before using it" unless index @@ -36,20 +38,20 @@ module Typed return ret end - def handle_special_self(statement) + def load_special_self(statement) ret = use_reg @type add_slot_to_reg("#{statement} load self" , :message , :receiver , ret ) return ret end - def handle_special_space(statement) + def load_special_space(statement) space = Parfait::Space.object_space reg = use_reg :Space , space add_load_constant( "#{statement} load space", space , reg ) return reg end - def handle_special_message(statement) + def load_special_message(statement) reg = use_reg :Message add_transfer( "#{statement} load message", Register.message_reg , reg ) return reg