more splitting of slot code

now constants, still wip
This commit is contained in:
Torsten Rüger 2020-02-10 18:58:48 +07:00
parent 24d7fe25da
commit 1da5cd16c3
8 changed files with 62 additions and 33 deletions

View File

@ -0,0 +1,49 @@
module SlotMachine
class ConstantDefinition < SlotDefinition
# get the right definition, depending on the object
def self.for(object , slots)
case object
when :message
MessageDefinition.new(slots)
when Constant
ConstantDefinition.new(object , slots)
else
SlotDefinition.new(object,slots)
end
end
def initialize( object , slots)
super(object, slots)
end
def known_name
known_object.class.short_name
end
# load the slots into a register
# the code is added to compiler
# the register returned
def to_register(compiler, source)
if known_object.respond_to?(:ct_type)
type = known_object.ct_type
else
type = :Object
end
right = compiler.use_reg( type )
case known_object
when Constant
parfait = known_object.to_parfait(compiler)
const = Risc.load_constant(source, parfait , right)
compiler.add_code const
if slots.length == 1
raise "only type allowed for constants, not #{slots[0]}" unless slots[0] == :type
compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right)
end
raise "Can't have slots into Constants #{slots}" if slots.length > 1
end
return const.register
end
end
end

View File

@ -6,7 +6,7 @@ module SlotMachine
end
def known_name
known_object
:message
end
# load the slots into a register

View File

@ -20,6 +20,8 @@ module SlotMachine
case object
when :message
MessageDefinition.new(slots)
when Constant
ConstantDefinition.new(object , slots)
else
SlotDefinition.new(object,slots)
end
@ -45,7 +47,7 @@ module SlotMachine
def known_name
case known_object
when Constant , Parfait::Object
when Parfait::Object
known_object.class.short_name
when Risc::Label
known_object.to_s
@ -58,24 +60,13 @@ module SlotMachine
# the code is added to compiler
# the register returned
def to_register(compiler, source)
if known_object.respond_to?(:ct_type)
type = known_object.ct_type
elsif(known_object.respond_to?(:get_type))
if(known_object.respond_to?(:get_type))
type = known_object.get_type
else
type = :Object
end
right = compiler.use_reg( type )
case known_object
when Constant
parfait = known_object.to_parfait(compiler)
const = Risc.load_constant(source, parfait , right)
compiler.add_code const
if slots.length == 1
raise "only type allowed for constants, not #{slots[0]}" unless slots[0] == :type
compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right)
end
raise "Can't have slots into Constants #{slots}" if slots.length > 1
when Parfait::Object , Risc::Label
const = Risc.load_constant(source, known_object , right)
compiler.add_code const

View File

@ -82,3 +82,4 @@ module SlotMachine
end
require_relative "slot_definition"
require_relative "message_definition"
require_relative "constant_definition"

16
out.txt
View File

@ -1,16 +0,0 @@
[/home/torsten/ruby-x/rubyx/test/slot_machine/instruction/test_slot_definition.rb:20
[/home/torsten/ruby-x/rubyx/test/slot_machine/instruction/test_slot_definition.rb:20
/home/torsten/ruby-x/rubyx/test/slot_machine/instruction/test_slot_definition.rb:20
test/slot_machine/instruction/test_slot_definition3.rb
/slot_machine/instruction/test_slot_definition3.rb
/slot_machine/instruction/test_slot_definition3.rb
test/slot_machine/instruction/test_slot_definition3.rb
test/slot_machine/instruction/test_slot_definition2.rb
home/torsten/ruby-x/rubyx/test/slot_machine/instruction/test_slot_definition.rb:20
home/torsten/ruby-x/rubyx/test/slot_machine/instruction/test_slot_definition.rb:20
home/torsten/ruby-x/rubyx/test/slot_machine/instruction/test_slot_definition.rb:20
/home/torsten/ruby-x/rubyx/test/slot_machine/instruction/test_slot_definition.rb:20
test/slot_machine/instruction/test_slot_definition3.rb
/home/torsten/ruby-x/rubyx/lib/sol/if_statement.rb:35
test/slot_machine/instruction/test_slot_definition.rb
/home/torsten/ruby-x/rubyx/test/slot_machine/instruction/test_slot_definition.rb:20

View File

@ -23,9 +23,6 @@ module Sol
def test_receiver_move_class
assert_equal ArgumentTransfer, @ins.next(1).class
end
def test_receiver_move
assert_equal SlotDefinition, @ins.next.receiver.class
end
def test_receiver
type , value = receiver
assert_equal type, @ins.next.receiver.known_object.class

View File

@ -13,6 +13,9 @@ module Sol
def test_call_has_right_method
assert_equal :div4, @ins.next(2).method.name
end
def test_receiver_move
assert_equal ConstantDefinition, @ins.next.receiver.class
end
end
end

View File

@ -23,5 +23,9 @@ module Sol
check_array [MessageSetup,ArgumentTransfer,SimpleCall, SlotLoad, ReturnJump,
Label, ReturnSequence , Label] , @ins
end
def test_receiver_move
assert_equal ConstantDefinition, @ins.next.receiver.class
end
end
end