2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2014-10-03 10:07:18 +02:00
|
|
|
# load a constant into a register
|
|
|
|
#
|
2015-06-25 15:31:09 +02:00
|
|
|
# first is the actual constant, either immediate register or object reference (from the space)
|
|
|
|
# second argument is the register the constant is loaded into
|
2015-05-31 16:54:36 +02:00
|
|
|
|
2014-10-03 10:07:18 +02:00
|
|
|
class LoadConstant < Instruction
|
2018-05-24 18:20:06 +02:00
|
|
|
def initialize( source , constant , register)
|
2015-07-27 11:13:39 +02:00
|
|
|
super(source)
|
2015-06-25 15:31:09 +02:00
|
|
|
@register = register
|
2014-10-03 10:07:18 +02:00
|
|
|
@constant = constant
|
2020-02-11 10:19:52 +01:00
|
|
|
raise "Not Constant #{constant}" if constant.is_a?(SlotMachine::Slot)
|
2020-02-29 16:17:58 +01:00
|
|
|
raise "Not register #{register}" unless register.is_a?(RegisterValue)
|
2014-10-03 10:07:18 +02:00
|
|
|
end
|
2015-06-25 15:31:09 +02:00
|
|
|
attr_accessor :register , :constant
|
2015-07-24 12:23:56 +02:00
|
|
|
|
2020-03-18 16:49:23 +01:00
|
|
|
# return an array of names of registers that is used by the instruction
|
2020-03-20 12:58:40 +01:00
|
|
|
def register_attributes
|
|
|
|
[:register]
|
2020-03-18 16:49:23 +01:00
|
|
|
end
|
|
|
|
|
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
|
2018-03-31 11:38:30 +02:00
|
|
|
when String , Symbol
|
2015-11-21 13:17:54 +01:00
|
|
|
@constant.to_s
|
|
|
|
else
|
2018-05-14 11:38:44 +02:00
|
|
|
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
|
2014-10-03 10:07:18 +02:00
|
|
|
end
|
2020-03-04 11:39:52 +01:00
|
|
|
|
|
|
|
def self.load_constant( source , constant , register = nil)
|
2020-03-01 22:38:23 +01:00
|
|
|
value = constant
|
2020-03-04 11:39:52 +01:00
|
|
|
unless register
|
|
|
|
case constant
|
|
|
|
when Parfait::Object
|
|
|
|
type = constant.get_type
|
|
|
|
when Label
|
|
|
|
type = constant.address.get_type
|
|
|
|
else
|
|
|
|
type = constant.ct_type
|
|
|
|
value = constant.value
|
|
|
|
end
|
2020-03-07 18:25:07 +01:00
|
|
|
value_class = value.class.name.to_s.split("::").last.downcase
|
|
|
|
register = RegisterValue.new( "id_#{value_class}_#{value.object_id}".to_sym , type )
|
2020-02-29 16:17:58 +01:00
|
|
|
end
|
2018-05-24 18:20:06 +02:00
|
|
|
LoadConstant.new( source , constant , register )
|
2015-11-21 13:17:54 +01:00
|
|
|
end
|
2014-10-03 10:07:18 +02:00
|
|
|
end
|