diff --git a/lib/risc/boot.rb b/lib/risc/boot.rb index e0193128..60b72581 100644 --- a/lib/risc/boot.rb +++ b/lib/risc/boot.rb @@ -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 diff --git a/lib/risc/builder.rb b/lib/risc/builder.rb index 533f9da6..3d8a3d71 100644 --- a/lib/risc/builder.rb +++ b/lib/risc/builder.rb @@ -114,7 +114,7 @@ module Risc add_slot_to_reg(source , int_arg , at + 1, int_arg ) #1 for type return int_arg end - + # assumed Integer in given register is replaced by the fixnum that it is holding def reduce_int( source , register ) add_slot_to_reg( source + "int -> fix" , register , Parfait::Integer.integer_index , register) diff --git a/lib/risc/builtin/integer.rb b/lib/risc/builtin/integer.rb index 25e3cb09..d232d1ef 100644 --- a/lib/risc/builtin/integer.rb +++ b/lib/risc/builtin/integer.rb @@ -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) diff --git a/test/risc/builtin/test_simple_int.rb b/test/risc/builtin/test_simple_int.rb index 6201cb50..96f7db46 100644 --- a/test/risc/builtin/test_simple_int.rb +++ b/test/risc/builtin/test_simple_int.rb @@ -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