implement smaller than comparison

which is NOT an operator in the risc sense
rather a minus and a check for sign
(which _could be more efficient in arm, with conditional execution)
This commit is contained in:
Torsten Ruger 2018-04-19 22:41:40 +03:00
parent 9e21719aeb
commit be3d125b82
4 changed files with 22 additions and 3 deletions

View File

@ -184,7 +184,7 @@ module Risc
Risc.operators.each do |op| Risc.operators.each do |op|
obj.instance_type.add_method Builtin::Integer.operator_method(op) obj.instance_type.add_method Builtin::Integer.operator_method(op)
end end
[:putint, :div4, :div10].each do |f| #div4 is just a forward declaration [:putint, :div4, :div10 , :<].each do |f| #div4 is just a forward declaration
obj.instance_type.add_method Builtin::Integer.send(f , nil) obj.instance_type.add_method Builtin::Integer.send(f , nil)
end end
end end

View File

@ -114,7 +114,7 @@ module Risc
add_slot_to_reg(source , int_arg , at + 1, int_arg ) #1 for type add_slot_to_reg(source , int_arg , at + 1, int_arg ) #1 for type
return int_arg return int_arg
end end
# assumed Integer in given register is replaced by the fixnum that it is holding # assumed Integer in given register is replaced by the fixnum that it is holding
def reduce_int( source , register ) def reduce_int( source , register )
add_slot_to_reg( source + "int -> fix" , register , Parfait::Integer.integer_index , register) add_slot_to_reg( source + "int -> fix" , register , Parfait::Integer.integer_index , register)

View File

@ -19,6 +19,25 @@ module Risc
compiler.add_mom( Mom::ReturnSequence.new) compiler.add_mom( Mom::ReturnSequence.new)
return compiler.method return compiler.method
end end
def <( context )
compiler = compiler_for(:Integer, :< ,{other: :Integer})
builder = compiler.builder(true, compiler.method)
me , other = builder.self_and_int_arg("< load receiver and arg")
false_label = Risc.label(compiler.method , "false_label_#{builder.object_id}")
merge_label = Risc.label(compiler.method , "merge_label_#{builder.object_id}")
builder.reduce_int( "< fix me", me )
builder.reduce_int( "< fix arg", other )
builder.add_code Risc.op( "< operator", :- , me , other)
builder.add_code Risc::IsPlus.new( "< if", false_label)
builder.add_load_constant("< new int", Parfait.object_space.true_object , other)
builder.add_code Risc::Branch.new("jump over false", merge_label)
builder.add_code false_label
builder.add_load_constant("< new int", Parfait.object_space.false_object , other)
builder.add_code merge_label
builder.add_reg_to_slot( "< save ret" , other , :message , :return_value)
compiler.add_mom( Mom::ReturnSequence.new)
return compiler.method
end
def putint(context) def putint(context)
compiler = compiler_for(:Integer,:putint ,{}) compiler = compiler_for(:Integer,:putint ,{})
compiler.add_mom( Mom::ReturnSequence.new) compiler.add_mom( Mom::ReturnSequence.new)

View File

@ -31,7 +31,7 @@ module Risc
end end
def test_smaller def test_smaller
run_all "4 < 5" run_all "4 < 5"
assert_equal 16 , get_return.value assert_equal Parfait::TrueClass , get_return.class
end end
end end
end end