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|
obj.instance_type.add_method Builtin::Integer.operator_method(op)
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)
end
end

View File

@ -19,6 +19,25 @@ module Risc
compiler.add_mom( Mom::ReturnSequence.new)
return compiler.method
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)
compiler = compiler_for(:Integer,:putint ,{})
compiler.add_mom( Mom::ReturnSequence.new)

View File

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