Fix div10 and test

fix and use load_data (similar to load_constant)
and integrate into load_object when appropriate (ie for integers)
This commit is contained in:
Torsten 2020-03-08 12:05:25 +02:00
parent bd02f69824
commit eed9ba082f
6 changed files with 85 additions and 45 deletions

View File

@ -90,12 +90,19 @@ module Risc
copied copied
end end
# Load a constant, meaning create a LoadConstant instruction for the constant # Load a constant, meaning create a LoadConstant or LoadData instruction for the
# given constant. Integers create LoadData (meaning the integer is encoded into
# the actual instruction), Parfait::Objects create LoadConstant, where a pointer
# to the object is loaded.
# add the instruction to the code and return the register_value that was created # add the instruction to the code and return the register_value that was created
# for further use # for further use
# register may be passed in (epecially in mcro building) as second arg # register may be passed in (epecially in mcro building) as second arg
def load_object( object , into = nil) def load_object( object , into = nil)
if(object.is_a? Integer)
ins = Risc.load_data("load data #{object}" , object , into)
else
ins = Risc.load_constant("load to #{object}" , object , into) ins = Risc.load_constant("load to #{object}" , object , into)
end
ins.register.set_compiler(self) ins.register.set_compiler(self)
add_code ins add_code ins
# todo for constants (not objects) # todo for constants (not objects)

View File

@ -22,7 +22,10 @@ module Risc
end end
end end
def self.load_data( source , constant , register ) def self.load_data( source , value , register = nil )
LoadData.new( source , constant , register) raise "can only load integers, not #{value}" unless value.is_a?(Integer)
type = Parfait.object_space.get_type_by_class_name(:Integer)
register = RegisterValue.new( "fix_#{value}".to_sym , type ) unless register
LoadData.new( source , value , register)
end end
end end

View File

@ -12,7 +12,7 @@ module SlotMachine
transfer( integer_self , integer_1) transfer( integer_self , integer_1)
transfer( integer_self , integer_reg) transfer( integer_self , integer_reg)
load_object( Parfait::Integer.new(1) , integer_const) load_object( 1 , integer_const)
integer_1.op :>> , integer_const integer_1.op :>> , integer_const

View File

@ -0,0 +1,24 @@
require_relative "../helper"
module Risc
class TestLoadData < MiniTest::Test
def setup
Parfait.boot!({})
end
def risc(i)
Risc.load_data("source" , 1)
end
def test_const
assert_equal LoadData , risc(1).class
end
def test_val
assert_equal 1 , risc(1).constant
end
def test_reg
assert_equal :fix_1 , risc(1).register.symbol
end
def test_reg_type
assert_equal "Integer_Type" , risc(1).register.type.name
end
end
end

View File

@ -31,5 +31,10 @@ module Risc
assert_equal LoadConstant , @compiler.current.class assert_equal LoadConstant , @compiler.current.class
assert_equal Parfait::Space , @compiler.current.constant.class assert_equal Parfait::Space , @compiler.current.constant.class
end end
def test_load_data
object = @compiler.load_object(1)
assert_equal LoadData , @compiler.current.class
assert_equal Integer , @compiler.current.constant.class
end
end end
end end

View File

@ -14,53 +14,54 @@ module SlotMachine
assert_equal Risc::MethodCompiler , @method.to_risc.class assert_equal Risc::MethodCompiler , @method.to_risc.class
end end
def test_risc_length def test_risc_length
assert_equal 69 , @method.to_risc.risc_instructions.length assert_equal 68 , @method.to_risc.risc_instructions.length
end end
def test_allocate def test_allocate
assert_allocate assert_allocate
end end
def test_return def test_return
assert_return(61) assert_return(60)
end end
def test_all def test_all
assert_slot_to_reg risc(23) , :r0 , 2 , :r2 assert_slot_to_reg 23 , "message" , 2 , "message.receiver"
assert_slot_to_reg risc(24) , :r2 , 2 , :r2 assert_slot_to_reg 24 , "message.receiver" , 2 , "message.receiver.data_1"
assert_transfer risc(25) , :r2 , :r3 assert_transfer 25 , "message.receiver.data_1" , "integer_1"
assert_transfer risc(26) , :r2 , :r4 assert_transfer 26 , "message.receiver.data_1" , "integer_reg"
assert_data risc(27) , 1 assert_data 27 , 1
assert_operator risc(28) , :>> , :r3 , :r5 assert_operator 28 , :>> , :integer_1 , :integer_const
assert_data risc(29) , 2 assert_data 29 , 2
assert_operator risc(30) , :>> , :r4 , :r5 assert_operator 30 , :>> , :integer_reg , :integer_const
assert_operator risc(31) , :+ , :r4 , :r3 assert_operator 31 , :+ , :integer_reg , :integer_1
assert_data risc(32) , 4 assert_data 32 , 4
assert_transfer risc(33) , :r4 , :r3 assert_transfer 33 , :integer_reg , :integer_1
assert_operator risc(34) , :>> , :r4 , :r3 assert_operator 34 , :>> , :integer_reg , :integer_1
assert_operator risc(35) , :+ , :r4 , :r3 assert_operator 35 , :+ , :integer_reg , :integer_1
assert_data risc(36) , 8 assert_data 36 , 8
assert_transfer risc(37) , :r4 , :r3 assert_transfer 37 , :integer_reg , :integer_1
assert_operator risc(38) , :>> , :r3 , :r5 assert_operator 38 , :>> , :integer_1 , :integer_const
assert_operator risc(39) , :+ , :r4 , :r3 assert_operator 39 , :+ , :integer_reg , :integer_1
assert_data risc(40) , 16 assert_data 40 , 16
assert_transfer risc(41) , :r4 , :r3 assert_transfer 41 , :integer_reg , :integer_1
assert_operator risc(42) , :>> , :r3 , :r5 assert_operator 42 , :>> , :integer_1 , :integer_const
assert_operator risc(43) , :+ , :r4 , :r3 assert_operator 43 , :+ , :integer_reg , :integer_1
assert_data risc(44) , 3 assert_data 44 , 3
assert_operator risc(45) , :>> , :r4 , :r5 assert_operator 45 , :>> , :integer_reg , :integer_const
assert_data risc(46) , 10 assert_data 46 , 10
assert_transfer risc(47) , :r4 , :r3 assert_transfer 47 , :integer_reg , :integer_1
assert_operator risc(48) , :* , :r3 , :r5 assert_operator 48 , :* , :integer_1 , :integer_const
assert_operator risc(49) , :- , :r2 , :r3 assert_transfer 49 , "message.receiver.data_1" , :integer_1
assert_transfer risc(50) , :r2 , :r3 # assert_operator 50 , :- , :r2 , :integer_1
assert_data risc(51) , 6 assert_data 50 , 6
assert_operator risc(52) , :+ , :r3 , :r5 assert_operator 51 , :+ , :integer_1 , :integer_const
assert_data risc(53) , 4 assert_data 52 , 4
assert_operator risc(54) , :>> , :r3 , :r5 assert_operator 53 , :>> , :integer_1 , :integer_const
assert_operator risc(55) , :+ , :r4 , :r3 assert_operator 54 , :+ , :integer_reg , :integer_1
assert_reg_to_slot risc(56) , :r4 , :r1 , 2 assert_reg_to_slot 55 , :integer_reg , "id_factory_.next_object" , 2
assert_reg_to_slot risc(57) , :r1 , :r0 , 5 assert_reg_to_slot 56 , "id_factory_.next_object" , :message , 5
assert_slot_to_reg risc(58),:r0 , 5 , :r2 assert_slot_to_reg 57 ,:message , 5 , "message.return_value"
assert_reg_to_slot risc(59) , :r2 , :r0 , 5 assert_reg_to_slot 58 , "message.return_value" , :message , 5
assert_branch risc(60) , "return_label" assert_branch 59 , "return_label"
assert_label 60 , "return_label"
end end
end end
end end