Fix div10 without method_missing

but reanimate infer_type to auto create the needed regsiters
also some helpers
This commit is contained in:
Torsten 2020-03-04 12:39:52 +02:00
parent 9a5e0f15cd
commit 9c5d17a3bb
6 changed files with 47 additions and 20 deletions

View File

@ -24,6 +24,20 @@ module Risc
@source_used = false
end
# especially for the macros (where register allocation is often manual)
# register need to be created. And since the code is "ported" we create
# them with the old names, which used the infer_type to infer the type
#
# Return the RegisterValue with given name and inferred type, compiler set
def register( name )
RegisterValue.new(name , infer_type(name) ).set_compiler(compiler)
end
# create an add a RegisterTransfer instruction with to and from
def transfer(to , from)
add_code Risc.transfer(@source, to , from)
end
# Infer the type from a symbol. In the simplest case the symbol is the class name.
# But in building, sometimes variations are needed, so next_message or caller work
# too (and both return "Message")
@ -90,8 +104,8 @@ module Risc
return ins
end
def load_object(object)
@compiler.load_object(object)
def load_object(object , into = nil)
@compiler.load_object(object , into)
end
# for some methods that return an integer it is beneficial to pre allocate the

View File

@ -93,8 +93,9 @@ module Risc
# Load a constant, meaning create a LoadConstant instruction for the constant
# add the instruction to the code and return the register_value that was created
# for further use
def load_object( object )
ins = Risc.load_constant("load to #{object}" , object)
# register may be passed in (epecially in mcro building) as second arg
def load_object( object , into = nil)
ins = Risc.load_constant("load to #{object}" , object , into)
ins.register.set_compiler(self)
add_code ins
# todo for constants (not objects)

View File

@ -32,8 +32,10 @@ module Risc
end
end
end
def self.load_constant( source , constant )
def self.load_constant( source , constant , register = nil)
value = constant
unless register
case constant
when Parfait::Object
type = constant.get_type
@ -44,6 +46,7 @@ module Risc
value = constant.value
end
register = RegisterValue.new( "id_#{value.object_id}".to_sym , type )
end
LoadConstant.new( source , constant , register )
end
end

View File

@ -4,13 +4,16 @@ module SlotMachine
s = "div_10 "
builder = compiler.builder(compiler.source)
integer_tmp = builder.allocate_int
integer_1 = builder.register( :integer_1 )
integer_reg = builder.register( :integer_reg )
integer_const = builder.register( :integer_const )
builder.build do
integer_self! << message[:receiver]
integer_self.reduce_int
integer_1! << integer_self
integer_reg! << integer_self
integer_self = message[:receiver].to_reg.reduce_int
transfer( integer_self , integer_1)
transfer( integer_self , integer_reg)
load_object( Parfait::Integer.new(1) , integer_const)
integer_const! << 1
integer_1.op :>> , integer_const
integer_const << 2

View File

@ -10,6 +10,12 @@ module Risc
@compiler = Risc::MethodCompiler.new( method, SlotMachine::Label.new( "source_name", "return_label") )
@builder = @compiler.builder(method)
end
def test_register_creation
assert_equal RegisterValue , @builder.register(:name).class
end
def test_register_creation_compiler
assert @builder.register(:name).compiler
end
def test_list
assert_equal :List , @builder.infer_type(:list).class_name
end

View File

@ -14,7 +14,7 @@ module SlotMachine
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 70 , @method.to_risc.risc_instructions.length
assert_equal 69 , @method.to_risc.risc_instructions.length
end
end
end