rubyx/lib/vm/method_compiler/name_expression.rb

61 lines
2.1 KiB
Ruby
Raw Normal View History

2017-01-14 18:28:44 +01:00
module Vm
2016-12-09 11:13:33 +01:00
module NameExpression
# attr_reader :name
# compiling name needs to check if it's a local variable
# or an argument
# whichever way this goes the result is stored in the return slot (as all compiles)
def on_NameExpression statement
name = statement.name
2016-12-09 11:13:33 +01:00
[:self , :space , :message].each do |special|
2016-12-28 20:10:14 +01:00
return send(:"load_special_#{special}" , statement ) if name == special
end
2016-12-28 20:10:14 +01:00
return load_argument(statement) if( @method.has_arg(name))
load_local(statement)
2016-12-09 11:13:33 +01:00
end
private
2016-12-28 20:10:14 +01:00
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
2016-12-21 17:51:22 +01:00
named_list = use_reg :NamedList
add_slot_to_reg("#{name} load locals" , :message , :locals , named_list )
ret = use_reg @method.locals_type( index )
add_slot_to_reg("#{name} load from locals" , named_list , index + 1, ret )
2016-12-09 11:13:33 +01:00
return ret
end
2016-12-28 20:10:14 +01:00
def load_special_self(statement)
2016-12-14 12:24:42 +01:00
ret = use_reg @type
add_slot_to_reg("#{statement} load self" , :message , :receiver , ret )
2016-12-09 11:13:33 +01:00
return ret
end
2016-12-28 20:10:14 +01:00
def load_special_space(statement)
space = Parfait.object_space
2016-12-09 11:13:33 +01:00
reg = use_reg :Space , space
add_load_constant( "#{statement} load space", space , reg )
2016-12-09 11:13:33 +01:00
return reg
end
2016-12-28 20:10:14 +01:00
def load_special_message(statement)
2016-12-09 11:13:33 +01:00
reg = use_reg :Message
add_transfer( "#{statement} load message", Register.message_reg , reg )
2016-12-09 11:13:33 +01:00
return reg
end
end #module
end