Starting to rework slot instructions that create risc
have to go through all and all macros and all thems tests. What did the wise man say: one step at a time
This commit is contained in:
parent
4643be0ae6
commit
4888b3b6db
@ -19,10 +19,11 @@ module Risc
|
|||||||
# a new regsister will be created as the result, ie the reg part for slot_to_reg
|
# a new regsister will be created as the result, ie the reg part for slot_to_reg
|
||||||
def self.slot_to_reg( source , array , index )
|
def self.slot_to_reg( source , array , index )
|
||||||
raise "Register #{array}" if RegisterValue.look_like_reg(array.symbol)
|
raise "Register #{array}" if RegisterValue.look_like_reg(array.symbol)
|
||||||
|
new_name = "#{array.symbol}.#{index.to_s.downcase}".to_sym
|
||||||
index = array.resolve_index(index) if index.is_a?(Symbol)
|
index = array.resolve_index(index) if index.is_a?(Symbol)
|
||||||
type = array.type_at(index)
|
type = array.type_at(index)
|
||||||
#puts "Slot for #{array.symbol}@ index #{index} is #{type}"
|
#puts "Slot for #{array.symbol}@ index #{index} is #{type}"
|
||||||
to = RegisterValue.new( "#{array.symbol}.#{type.to_s.downcase}".to_sym , type )
|
to = RegisterValue.new( new_name , type )
|
||||||
SlotToReg.new( source , array , index , to)
|
SlotToReg.new( source , array , index , to)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -25,17 +25,25 @@ module Risc
|
|||||||
def <<( reg )
|
def <<( reg )
|
||||||
case reg
|
case reg
|
||||||
when RegisterValue
|
when RegisterValue
|
||||||
puts reg.symbol
|
|
||||||
to_mem("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg)
|
to_mem("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg)
|
||||||
when RegisterSlot
|
when RegisterSlot
|
||||||
puts reg.register.symbol
|
|
||||||
reg = to_reg("reduce #{@register.symbol}[@index]")
|
reg = to_reg("reduce #{@register.symbol}[@index]")
|
||||||
to_mem("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg)
|
to_mem("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg)
|
||||||
else
|
else
|
||||||
raise "not reg value or slot #{reg}"
|
raise "not reg value or slot #{reg}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# for chaining the array operator is defined here too.
|
||||||
|
# It basically reduces the slot to a register and applies the [] on that reg.
|
||||||
|
# thus returning a new RegisterSlot.
|
||||||
|
# Example: message[:caller][:next_message]
|
||||||
|
# message[:caller] returns a RegisterSlot, which would be self for this example
|
||||||
|
# to evaluate self[:next_message] we reduce self to a register with to_reg
|
||||||
|
def [](index)
|
||||||
|
reg = to_reg("reduce #{@register.symbol}[@index]")
|
||||||
|
reg[index]
|
||||||
|
end
|
||||||
# push the given register into the slot that self represents
|
# push the given register into the slot that self represents
|
||||||
# ie create a slot_to_reg instruction and add to the compiler
|
# ie create a slot_to_reg instruction and add to the compiler
|
||||||
# the register represents and "array", and the content of the
|
# the register represents and "array", and the content of the
|
||||||
@ -47,7 +55,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
|
|
||||||
# load the conntent of the slot that self descibes into a a new register.
|
# load the conntent of the slot that self descibes into a a new register.
|
||||||
# the regsiter is created, and the slot_to_reg instruction added to the
|
# the register is created, and the slot_to_reg instruction added to the
|
||||||
# compiler. the return is a bit like @register[@index]
|
# compiler. the return is a bit like @register[@index]
|
||||||
def to_reg(source )
|
def to_reg(source )
|
||||||
slot_to_reg = Risc.slot_to_reg(source , register, index)
|
slot_to_reg = Risc.slot_to_reg(source , register, index)
|
||||||
|
@ -82,7 +82,6 @@ module Risc
|
|||||||
new_type , extra = compiler.slot_type(slot , type) unless new_type
|
new_type , extra = compiler.slot_type(slot , type) unless new_type
|
||||||
new_name = "#{@symbol}.#{slot}"
|
new_name = "#{@symbol}.#{slot}"
|
||||||
raise "no #{self}" if RegisterValue.look_like_reg(@symbol)
|
raise "no #{self}" if RegisterValue.look_like_reg(@symbol)
|
||||||
puts "New name #{new_name}"
|
|
||||||
new_left = RegisterValue.new( new_name.to_sym , new_type , extra)
|
new_left = RegisterValue.new( new_name.to_sym , new_type , extra)
|
||||||
new_left
|
new_left
|
||||||
end
|
end
|
||||||
|
@ -38,9 +38,9 @@ module SlotMachine
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
require_relative "basic_values"
|
||||||
require_relative "instruction/label"
|
require_relative "instruction/label"
|
||||||
require_relative "instruction/check"
|
require_relative "instruction/check"
|
||||||
require_relative "instruction/basic_values"
|
|
||||||
require_relative "instruction/simple_call"
|
require_relative "instruction/simple_call"
|
||||||
require_relative "instruction/dynamic_call"
|
require_relative "instruction/dynamic_call"
|
||||||
require_relative "instruction/block_yield"
|
require_relative "instruction/block_yield"
|
||||||
|
@ -40,11 +40,9 @@ module SlotMachine
|
|||||||
#TODO transfer the Number of arguments to :arguments_given (to be checked on entry)
|
#TODO transfer the Number of arguments to :arguments_given (to be checked on entry)
|
||||||
arg_target = [:message , :next_message ]
|
arg_target = [:message , :next_message ]
|
||||||
@arguments.each_with_index do |arg , index| # +1 because of type
|
@arguments.each_with_index do |arg , index| # +1 because of type
|
||||||
compiler.reset_regs
|
|
||||||
load = SlotMachine::SlotLoad.new(self.source, arg_target + ["arg#{index+1}".to_sym] , arg)
|
load = SlotMachine::SlotLoad.new(self.source, arg_target + ["arg#{index+1}".to_sym] , arg)
|
||||||
load.to_risc(compiler)
|
load.to_risc(compiler)
|
||||||
end
|
end
|
||||||
compiler.reset_regs
|
|
||||||
transfer
|
transfer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -48,7 +48,6 @@ module SlotMachine
|
|||||||
def to_risc(compiler)
|
def to_risc(compiler)
|
||||||
const_reg = @right.to_register(compiler , original_source)
|
const_reg = @right.to_register(compiler , original_source)
|
||||||
@left.reduce_and_load(const_reg , compiler , original_source )
|
@left.reduce_and_load(const_reg , compiler , original_source )
|
||||||
compiler.reset_regs
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -42,8 +42,9 @@ module SlotMachine
|
|||||||
# most likely be united
|
# most likely be united
|
||||||
def reduce_and_load(const_reg , compiler , original_source )
|
def reduce_and_load(const_reg , compiler , original_source )
|
||||||
raise "only cache" unless known_object.is_a?( Parfait::CacheEntry)
|
raise "only cache" unless known_object.is_a?( Parfait::CacheEntry)
|
||||||
left = compiler.use_reg( :CacheEntry )
|
load = Risc.load_constant(original_source, known_object )
|
||||||
compiler.add_code Risc.load_constant(original_source, known_object , left)
|
left = load.register
|
||||||
|
compiler.add_code load
|
||||||
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, slots.name)
|
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, slots.name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -15,10 +15,11 @@ module Risc
|
|||||||
end
|
end
|
||||||
def test_reg_to_slot_inst
|
def test_reg_to_slot_inst
|
||||||
@r0[:next_message] << @r0
|
@r0[:next_message] << @r0
|
||||||
|
inst = @compiler.current
|
||||||
assert_equal RegToSlot , @compiler.current.class
|
assert_equal RegToSlot , @compiler.current.class
|
||||||
assert_equal @r0 , @compiler.current.register
|
assert_equal @r0 , inst.register
|
||||||
assert_equal 1 , @compiler.current.index
|
assert_equal 1 , inst.index
|
||||||
assert_equal :message , @compiler.current.array.symbol
|
assert_equal :message , inst.array.symbol
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
class TestRegisterSlot2 < MiniTest::Test
|
class TestRegisterSlot2 < MiniTest::Test
|
||||||
@ -30,14 +31,14 @@ module Risc
|
|||||||
def test_reg_to_slot_reg
|
def test_reg_to_slot_reg
|
||||||
reg = @r0[:next_message] << @r0[:next_message]
|
reg = @r0[:next_message] << @r0[:next_message]
|
||||||
assert_equal RegisterValue , reg.class
|
assert_equal RegisterValue , reg.class
|
||||||
assert_equal :"message.message" , reg.symbol
|
assert_equal :"message.next_message" , reg.symbol
|
||||||
assert_equal "Message_Type" , reg.type.name
|
assert_equal "Message_Type" , reg.type.name
|
||||||
end
|
end
|
||||||
def test_reg_to_slot_inst1
|
def test_reg_to_slot_inst1
|
||||||
@r0[:next_message] << @r0[:next_message]
|
@r0[:next_message] << @r0[:next_message]
|
||||||
inst = @compiler.risc_instructions.next
|
inst = @compiler.risc_instructions.next
|
||||||
assert_equal SlotToReg , inst.class
|
assert_equal SlotToReg , inst.class
|
||||||
assert_equal :"message.message" , inst.register.symbol
|
assert_equal :"message.next_message" , inst.register.symbol
|
||||||
assert_equal 1 , inst.index
|
assert_equal 1 , inst.index
|
||||||
assert_equal :message , inst.array.symbol
|
assert_equal :message , inst.array.symbol
|
||||||
end
|
end
|
||||||
@ -45,7 +46,28 @@ module Risc
|
|||||||
@r0[:next_message] << @r0[:next_message]
|
@r0[:next_message] << @r0[:next_message]
|
||||||
inst = @compiler.current
|
inst = @compiler.current
|
||||||
assert_equal RegToSlot , inst.class
|
assert_equal RegToSlot , inst.class
|
||||||
assert_equal :"message.message" , inst.register.symbol
|
assert_equal :"message.next_message" , inst.register.symbol
|
||||||
|
assert_equal 1 , inst.index
|
||||||
|
assert_equal :message , inst.array.symbol
|
||||||
|
end
|
||||||
|
end
|
||||||
|
class TestRegisterSlot3 < MiniTest::Test
|
||||||
|
def setup
|
||||||
|
Parfait.boot!(Parfait.default_test_options)
|
||||||
|
@compiler = Risc.test_compiler
|
||||||
|
@r0 = RegisterValue.new(:message , :Message).set_compiler(@compiler)
|
||||||
|
end
|
||||||
|
def test_arr_reg
|
||||||
|
slot = @r0[:next_message][:type]
|
||||||
|
assert_equal RegisterSlot , slot.class
|
||||||
|
assert_equal :"message.next_message" , slot.register.symbol
|
||||||
|
assert_equal "Message_Type" , slot.register.type.name
|
||||||
|
end
|
||||||
|
def test_arr_inst
|
||||||
|
slot = @r0[:next_message][:type]
|
||||||
|
inst = @compiler.current
|
||||||
|
assert_equal SlotToReg , inst.class
|
||||||
|
assert_equal :"message.next_message" , inst.register.symbol
|
||||||
assert_equal 1 , inst.index
|
assert_equal 1 , inst.index
|
||||||
assert_equal :message , inst.array.symbol
|
assert_equal :message , inst.array.symbol
|
||||||
end
|
end
|
||||||
|
@ -11,25 +11,25 @@ module SlotMachine
|
|||||||
assert_equal 8 , all.length , all_str
|
assert_equal 8 , all.length , all_str
|
||||||
end
|
end
|
||||||
def test_1_slot
|
def test_1_slot
|
||||||
assert_slot_to_reg risc(1) ,:r0 , 9 , :r2
|
assert_slot_to_reg risc(1) ,:message , 9 , :"message.arg1"
|
||||||
end
|
end
|
||||||
def test_2_slot
|
def test_2_slot
|
||||||
assert_slot_to_reg risc(2) ,:r0 , 1 , :r3
|
assert_slot_to_reg risc(2) ,:message , 1 , :"message.next_message"
|
||||||
end
|
end
|
||||||
def test_3_reg
|
def test_3_reg
|
||||||
assert_reg_to_slot risc(3) , :r2 , :r3 , 2
|
assert_reg_to_slot risc(3) , :"message.arg1" , :"message.next_message" , 2
|
||||||
end
|
end
|
||||||
def test_4_slot
|
def test_4_slot
|
||||||
assert_slot_to_reg risc(4) ,:r0 , 2 , :r2
|
assert_slot_to_reg risc(4) ,:message , 2 , :"message.receiver"
|
||||||
end
|
end
|
||||||
def test_5
|
def test_5
|
||||||
assert_slot_to_reg risc(5) ,:r2 , 0 , :r2
|
assert_slot_to_reg risc(5) ,:"message.receiver" , 0 , :"message.receiver.type"
|
||||||
end
|
end
|
||||||
def test_6
|
def test_6
|
||||||
assert_slot_to_reg risc(6) ,:r0 , 1 , :r3
|
assert_slot_to_reg risc(6) ,:message , 1 , :"message.next_message"
|
||||||
end
|
end
|
||||||
def test_7
|
def test_7
|
||||||
assert_reg_to_slot risc(7) , :r2 , :r3 , 9
|
assert_reg_to_slot risc(7) , :"message.receiver.type" , :"message.next_message" , 9
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,19 +4,20 @@ module SlotMachine
|
|||||||
class TestNotSameCheck < SlotMachineInstructionTest
|
class TestNotSameCheck < SlotMachineInstructionTest
|
||||||
def instruction
|
def instruction
|
||||||
target = Slotted.for(:message , [:caller])
|
target = Slotted.for(:message , [:caller])
|
||||||
NotSameCheck.new(target , target , Label.new("ok" , "target"))
|
target2 = Slotted.for(:message , [:next_message])
|
||||||
|
NotSameCheck.new(target , target2 , Label.new("ok" , "target"))
|
||||||
end
|
end
|
||||||
def test_len
|
def test_len
|
||||||
assert_equal 5 , all.length , all_str
|
assert_equal 5 , all.length , all_str
|
||||||
end
|
end
|
||||||
def test_1_slot
|
def test_1_slot
|
||||||
assert_slot_to_reg risc(1) ,:r0 , 6 , :r2
|
assert_slot_to_reg risc(1) ,:message , 6 , :"message.caller"
|
||||||
end
|
end
|
||||||
def test_2_slot
|
def test_2_slot
|
||||||
assert_slot_to_reg risc(2) ,:r0 , 6 , :r4
|
assert_slot_to_reg risc(2) ,:message , 1 , :"message.next_message"
|
||||||
end
|
end
|
||||||
def test_3_op
|
def test_3_op
|
||||||
assert_operator risc(3) , :-, :r2 , :r4
|
assert_operator risc(3) , :-, :"message.caller" , :"message.next_message"
|
||||||
end
|
end
|
||||||
def test_4_zero
|
def test_4_zero
|
||||||
assert_zero risc(4) , "target"
|
assert_zero risc(4) , "target"
|
||||||
|
@ -18,19 +18,19 @@ module SlotMachine
|
|||||||
assert_equal Risc::RegToSlot , @instructions.next.class
|
assert_equal Risc::RegToSlot , @instructions.next.class
|
||||||
end
|
end
|
||||||
def test_ins_arr
|
def test_ins_arr
|
||||||
assert_equal :r0 , @instructions.array.symbol
|
assert_equal :message , @instructions.array.symbol
|
||||||
end
|
end
|
||||||
def test_ins_reg
|
def test_ins_reg
|
||||||
assert_equal :r2 , @instructions.register.symbol
|
assert_equal :"message.type" , @instructions.register.symbol
|
||||||
end
|
end
|
||||||
def test_ins_index
|
def test_ins_index
|
||||||
assert_equal 0 , @instructions.index
|
assert_equal 0 , @instructions.index
|
||||||
end
|
end
|
||||||
def test_ins_next_reg
|
def test_ins_next_reg
|
||||||
assert_equal :r2 , @instructions.next.register.symbol
|
assert_equal :"message.type" , @instructions.next.register.symbol
|
||||||
end
|
end
|
||||||
def test_ins_next_arr
|
def test_ins_next_arr
|
||||||
assert_equal :r0 , @instructions.next.array.symbol
|
assert_equal :message , @instructions.next.array.symbol
|
||||||
end
|
end
|
||||||
def test_ins_next_index
|
def test_ins_next_index
|
||||||
assert_equal 6 , @instructions.next.index
|
assert_equal 6 , @instructions.next.index
|
||||||
|
@ -21,28 +21,28 @@ module SlotMachine
|
|||||||
assert_equal NilClass , @instructions.next(4).class
|
assert_equal NilClass , @instructions.next(4).class
|
||||||
end
|
end
|
||||||
def test_ins_next_reg
|
def test_ins_next_reg
|
||||||
assert_equal :r2 , @instructions.next.register.symbol
|
assert_equal :"message.caller.type" , @instructions.next.register.symbol
|
||||||
end
|
end
|
||||||
def test_ins_next_arr
|
def test_ins_next_arr
|
||||||
assert_equal :r2 , @instructions.next.array.symbol
|
assert_equal :"message.caller" , @instructions.next.array.symbol
|
||||||
end
|
end
|
||||||
def test_ins_next_index
|
def test_ins_next_index
|
||||||
assert_equal 0 , @instructions.next.index
|
assert_equal 0 , @instructions.next.index
|
||||||
end
|
end
|
||||||
def test_ins_next_2_reg
|
def test_ins_next_2_reg
|
||||||
assert_equal :r3 , @instructions.next(2).register.symbol
|
assert_equal :"message.caller" , @instructions.next(2).register.symbol
|
||||||
end
|
end
|
||||||
def test_ins_next_2_arr
|
def test_ins_next_2_arr
|
||||||
assert_equal :r0 , @instructions.next(2).array.symbol
|
assert_equal :"message" , @instructions.next(2).array.symbol
|
||||||
end
|
end
|
||||||
def test_ins_next_2_index
|
def test_ins_next_2_index
|
||||||
assert_equal 6 , @instructions.next(2).index
|
assert_equal 6 , @instructions.next(2).index
|
||||||
end
|
end
|
||||||
def test_ins_next_3_reg
|
def test_ins_next_3_reg
|
||||||
assert_equal :r2 , @instructions.next(3).register.symbol
|
assert_equal :"message.caller.type" , @instructions.next(3).register.symbol
|
||||||
end
|
end
|
||||||
def test_ins_next_3_arr
|
def test_ins_next_3_arr
|
||||||
assert_equal :r3 , @instructions.next(3).array.symbol
|
assert_equal :"message.caller" , @instructions.next(3).array.symbol
|
||||||
end
|
end
|
||||||
def test_ins_next_3_index
|
def test_ins_next_3_index
|
||||||
assert_equal 0 , @instructions.next(3).index
|
assert_equal 0 , @instructions.next(3).index
|
||||||
|
@ -23,25 +23,25 @@ module SlotMachine
|
|||||||
assert_equal NilClass , @instructions.next(3).class
|
assert_equal NilClass , @instructions.next(3).class
|
||||||
end
|
end
|
||||||
def test_ins_load
|
def test_ins_load
|
||||||
assert_equal :r3 , @instructions.next.register.symbol
|
assert @instructions.next.register.is_object?
|
||||||
assert_equal Parfait::CacheEntry , @instructions.next.constant.class
|
assert_equal Parfait::CacheEntry , @instructions.next.constant.class
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_ins_next_reg
|
def test_ins_next_reg
|
||||||
assert_equal :r2 , @instructions.register.symbol
|
assert_equal :"message.type" , @instructions.register.symbol
|
||||||
end
|
end
|
||||||
def test_ins_next_arr
|
def test_ins_next_arr
|
||||||
assert_equal :r0 , @instructions.array.symbol
|
assert_equal :message , @instructions.array.symbol
|
||||||
end
|
end
|
||||||
def test_ins_next_index
|
def test_ins_next_index
|
||||||
assert_equal 0 , @instructions.index
|
assert_equal 0 , @instructions.index
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_ins_next_2_reg
|
def test_ins_next_2_reg
|
||||||
assert_equal :r2 , @instructions.next(2).register.symbol
|
assert_equal :"message.type" , @instructions.next(2).register.symbol
|
||||||
end
|
end
|
||||||
def test_ins_next_2_arr
|
def test_ins_next_2_arr
|
||||||
assert_equal :r3 , @instructions.next(2).array.symbol
|
assert @instructions.next(2).array.is_object?
|
||||||
end
|
end
|
||||||
def test_ins_next_2_index
|
def test_ins_next_2_index
|
||||||
assert_equal 1 , @instructions.next(2).index
|
assert_equal 1 , @instructions.next(2).index
|
||||||
|
Loading…
x
Reference in New Issue
Block a user