rubyx/lib/vm/method_compiler/name_expression.rb

59 lines
2.0 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_KnownName 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
raise "Unknow 'known' expression #{name}"
2016-12-09 11:13:33 +01:00
end
def on_ArgumentName(statement)
2016-12-28 20:10:14 +01:00
name = statement.name
index = @method.has_argument(name)
raise "no arg #{name}" unless index
2016-12-28 20:10:14 +01:00
named_list = use_reg :NamedList
ret = use_reg @method.arguments_type(index)
2016-12-28 20:10:14 +01:00
#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 on_LocalName( statement )
name = statement.name
index = @method.has_local( name )
raise "must define local '#{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", Risc.message_reg , reg )
2016-12-09 11:13:33 +01:00
return reg
end
end #module
end