Commit ef89c4e5 authored by Kunshan Wang's avatar Kunshan Wang

Allow comparing between pointers

parent 886a718f
...@@ -73,6 +73,7 @@ trait InstructionExecutor extends InterpreterActions with CommInstExecutor { ...@@ -73,6 +73,7 @@ trait InstructionExecutor extends InterpreterActions with CommInstExecutor {
val (op2b, op2o) = b2.asIRef val (op2b, op2o) = b2.asIRef
PrimOpHelpers.irefCmp(op, op1b, op1o, op2b, op2o, ctx) PrimOpHelpers.irefCmp(op, op1b, op1o, op2b, op2o, ctx)
} }
case t:AbstractPointerType => PrimOpHelpers.intCmp(op, TypeSizes.WORD_SIZE_BITS.toInt, b1.asPtr, b2.asPtr, ctx)
case _ => throw new UvmRuntimeException(ctx + "Comparison not suitable for type %s".format(opndTy)) case _ => throw new UvmRuntimeException(ctx + "Comparison not suitable for type %s".format(opndTy))
} }
} }
......
...@@ -360,6 +360,66 @@ class UvmInterpreterSpec extends UvmBundleTesterBase { ...@@ -360,6 +360,66 @@ class UvmInterpreterSpec extends UvmBundleTesterBase {
ctx.closeContext() ctx.closeContext()
} }
"Comparing operations" should "work on pointer types" in {
val ctx = microVM.newContext()
val func = ctx.handleFromFunc("@cmpptr")
val a0 = ctx.handleFromPtr("@ptri64", 1)
val a1 = ctx.handleFromPtr("@ptri64", 2)
testFunc(ctx, func, Seq(a0, a1)) { (ctx, th, st, wp) =>
val Seq(eq, ne, ult, ule, ugt, uge, slt, sle, sgt, sge) = ctx.dumpKeepalives(st, 0)
eq.vb.asUInt(1) shouldEqual 0
ne.vb.asUInt(1) shouldEqual 1
ult.vb.asUInt(1) shouldEqual 1
ule.vb.asUInt(1) shouldEqual 1
ugt.vb.asUInt(1) shouldEqual 0
uge.vb.asUInt(1) shouldEqual 0
slt.vb.asUInt(1) shouldEqual 1
sle.vb.asUInt(1) shouldEqual 1
sgt.vb.asUInt(1) shouldEqual 0
sge.vb.asUInt(1) shouldEqual 0
Rebind(st, PassValues(Seq()))
}
testFunc(ctx, func, Seq(a0, a0)) { (ctx, th, st, wp) =>
val Seq(eq, ne, ult, ule, ugt, uge, slt, sle, sgt, sge) = ctx.dumpKeepalives(st, 0)
eq.vb.asUInt(1) shouldEqual 1
ne.vb.asUInt(1) shouldEqual 0
ult.vb.asUInt(1) shouldEqual 0
ule.vb.asUInt(1) shouldEqual 1
ugt.vb.asUInt(1) shouldEqual 0
uge.vb.asUInt(1) shouldEqual 1
slt.vb.asUInt(1) shouldEqual 0
sle.vb.asUInt(1) shouldEqual 1
sgt.vb.asUInt(1) shouldEqual 0
sge.vb.asUInt(1) shouldEqual 1
Rebind(st, PassValues(Seq()))
}
val a2 = ctx.handleFromPtr("@ptri64", -3)
testFunc(ctx, func, Seq(a0, a2)) { (ctx, th, st, wp) =>
val Seq(eq, ne, ult, ule, ugt, uge, slt, sle, sgt, sge) = ctx.dumpKeepalives(st, 0)
eq.vb.asUInt(1) shouldEqual 0
ne.vb.asUInt(1) shouldEqual 1
slt.vb.asUInt(1) shouldEqual 0
sle.vb.asUInt(1) shouldEqual 0
sgt.vb.asUInt(1) shouldEqual 1
sge.vb.asUInt(1) shouldEqual 1
Rebind(st, PassValues(Seq()))
}
ctx.closeContext()
}
"Comparing operations" should "work on float" in { "Comparing operations" should "work on float" in {
val ctx = microVM.newContext() val ctx = microVM.newContext()
......
...@@ -137,6 +137,26 @@ ...@@ -137,6 +137,26 @@
COMMINST @uvm.thread_exit COMMINST @uvm.thread_exit
} }
.funcsig @cmpptr_sig = (@ptri64 @ptri64) -> ()
.funcdef @cmpptr VERSION %v1 <@cmpptr_sig> {
%entry(<@ptri64> %p0 <@ptri64> %p1):
%eq = EQ <@ptri64> %p0 %p1
%ne = NE <@ptri64> %p0 %p1
%ult = ULT <@ptri64> %p0 %p1
%ule = ULE <@ptri64> %p0 %p1
%ugt = UGT <@ptri64> %p0 %p1
%uge = UGE <@ptri64> %p0 %p1
%slt = SLT <@ptri64> %p0 %p1
%sle = SLE <@ptri64> %p0 %p1
%sgt = SGT <@ptri64> %p0 %p1
%sge = SGE <@ptri64> %p0 %p1
[%trap] TRAP <> KEEPALIVE (
%eq %ne %ult %ule %ugt %uge %slt %sle %sgt %sge
)
COMMINST @uvm.thread_exit
}
.funcsig @cmp_f_sig = (@float @float) -> () .funcsig @cmp_f_sig = (@float @float) -> ()
.funcdef @cmp_f VERSION %v1 <@cmp_f_sig> { .funcdef @cmp_f VERSION %v1 <@cmp_f_sig> {
%entry(<@float> %p0 <@float> %p1): %entry(<@float> %p0 <@float> %p1):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment