rubyx/lib/risc/instructions/load_constant.rb

47 lines
1.3 KiB
Ruby
Raw Normal View History

module Risc
# load a constant into a register
#
# first is the actual constant, either immediate register or object reference (from the space)
# second argument is the register the constant is loaded into
class LoadConstant < Instruction
def initialize( source , constant , register)
super(source)
@register = register
@constant = constant
raise "Not Constant #{constant}" if constant.is_a?(SlotMachine::Slot)
raise "Not register #{register}" unless register.is_a?(RegisterValue)
end
attr_accessor :register , :constant
2015-07-24 12:23:56 +02:00
def to_s
2018-03-22 17:38:19 +01:00
class_source "#{register} <- #{constant_str}"
2015-07-24 12:23:56 +02:00
end
2015-08-08 23:52:47 +02:00
private
def constant_str
2015-11-21 13:17:54 +01:00
case @constant
when String , Symbol
2015-11-21 13:17:54 +01:00
@constant.to_s
else
if( @constant.respond_to? :rxf_reference_name )
constant.rxf_reference_name
2015-08-08 23:52:47 +02:00
else
2015-11-21 13:17:54 +01:00
constant.class.name.to_s
2015-08-08 23:52:47 +02:00
end
2015-11-21 13:17:54 +01:00
end
2015-08-08 23:52:47 +02:00
end
end
def self.load_constant( source , constant )
if(constant.is_a?(Parfait::Object))
type = constant.get_type
value = constant
else
type = constant.ct_type
value = constant.value
end
register = RegisterValue.new( "id_#{value.object_id}".to_sym , type )
LoadConstant.new( source , constant , register )
2015-11-21 13:17:54 +01:00
end
end