Commit b028c4e5 authored by Kunshan Wang's avatar Kunshan Wang

Test and fix constant parsing

parent 2d453cba
...@@ -132,7 +132,7 @@ object UvmIRReader { ...@@ -132,7 +132,7 @@ object UvmIRReader {
case FloatConstCons(num) => ConstFloat(t, num) case FloatConstCons(num) => ConstFloat(t, num)
case DoubleConstCons(num) => ConstDouble(t, num) case DoubleConstCons(num) => ConstDouble(t, num)
case StructConstCons(fs) => ConstStruct(t, null).later(phase2) { case StructConstCons(fs) => ConstStruct(t, null).later(phase2) {
_.fields = for (f <- fs) yield resGV(t, f) _.fields = for ((ft,f) <- t.asInstanceOf[TypeStruct].fieldTy.zip(fs)) yield resGV(ft, f)
} }
case NullConstCons => ConstNull(t) case NullConstCons => ConstNull(t)
} }
......
...@@ -18,11 +18,14 @@ trait AbstractReaderSpec extends FlatSpec with Matchers ...@@ -18,11 +18,14 @@ trait AbstractReaderSpec extends FlatSpec with Matchers
} }
it should "read simple constant definitions" in { it should "read simple constant definitions" in {
val b = parseFile("tests/uvm-parsing-test/constants.uir") val b = parseFile("tests/uvm-parsing-test/constants.uir")
validateConstants(b)
} }
it should "read simple function definitions" in { it should "read simple function definitions" in {
val b = parseFile("tests/uvm-parsing-test/functions.uir") val b = parseFile("tests/uvm-parsing-test/functions.uir")
validateFunctions(b)
} }
it should "read simple instruction definitions" in { it should "read simple instruction definitions" in {
val b = parseFile("tests/uvm-parsing-test/instructions.uir") val b = parseFile("tests/uvm-parsing-test/instructions.uir")
validateInstructions(b)
} }
} }
\ No newline at end of file
...@@ -3,6 +3,11 @@ package uvm.ir.textinput ...@@ -3,6 +3,11 @@ package uvm.ir.textinput
import scala.reflect._ import scala.reflect._
import org.scalatest._ import org.scalatest._
import uvm._
import uvm.types._
import uvm.ssavalues._
import uvm.ifuncs._
trait ExtraMatchers extends Assertions with Matchers { trait ExtraMatchers extends Assertions with Matchers {
implicit class AnythingExtraMatchers(val thing: Any) { implicit class AnythingExtraMatchers(val thing: Any) {
def shouldBeA[T: ClassTag](f: T => Unit): Unit = { def shouldBeA[T: ClassTag](f: T => Unit): Unit = {
...@@ -12,11 +17,54 @@ trait ExtraMatchers extends Assertions with Matchers { ...@@ -12,11 +17,54 @@ trait ExtraMatchers extends Assertions with Matchers {
} }
f(thing.asInstanceOf[T]) f(thing.asInstanceOf[T])
} }
def shouldBeATypeIntOf(len: Int) {
thing shouldBeA[TypeInt] {_.length shouldEqual len}
}
def shouldBeAConstIntOf(len: Int, num: BigInt) {
thing shouldBeA[ConstInt] { its =>
its.ty shouldBeATypeIntOf(len)
its.num shouldEqual num
}
}
def shouldBeAConstFloatOf(something: Any) {
thing shouldBeA[ConstFloat] { its =>
its.ty shouldBeA[TypeFloat] thatsIt
something match {
case `nan` => assert(its.num.isNaN)
case ExactFloat(num) => its.num shouldEqual num
case num: Float => its.num shouldEqual (num +- 0.001F)
case _ => its.num shouldEqual something
}
}
}
def shouldBeAConstDoubleOf(something: Any) {
thing shouldBeA[ConstDouble] { its =>
its.ty shouldBeA[TypeDouble] thatsIt
something match {
case `nan` => assert(its.num.isNaN)
case ExactDouble(num) => its.num shouldEqual num
case num: Double => its.num shouldEqual (num +- 0.001F)
case _ => its.num shouldEqual something
}
}
}
} }
val thatsIt = { f: Any => } val thatsIt = { f: Any => }
case object nan
case class ExactFloat(num: Float)
case class ExactDouble(num: Double)
def exactly(num: Float) = ExactFloat(num)
def exactly(num: Double) = ExactDouble(num)
def bitsf(num: Int) = java.lang.Float.intBitsToFloat(num)
def bitsd(num: Long) = java.lang.Double.longBitsToDouble(num)
} }
object ExtraMatchers extends ExtraMatchers { object ExtraMatchers {
} }
\ No newline at end of file
...@@ -13,6 +13,7 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers { ...@@ -13,6 +13,7 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers {
def ty = b.typeNs def ty = b.typeNs
def const = b.globalValueNS def const = b.globalValueNS
def globalValue = b.globalValueNS def globalValue = b.globalValueNS
def globalData = b.globalDataNS
def sig = b.funcSigNs def sig = b.funcSigNs
def func = b.funcNs def func = b.funcNs
} }
...@@ -20,11 +21,11 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers { ...@@ -20,11 +21,11 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers {
def validateTypes(bundle: Bundle) { def validateTypes(bundle: Bundle) {
val our = bundle val our = bundle
our ty "@i1" shouldBeA[TypeInt] { _.length shouldEqual 1 } our ty "@i1" shouldBeATypeIntOf(1)
our ty "@i8" shouldBeA[TypeInt] { _.length shouldEqual 8 } our ty "@i8" shouldBeATypeIntOf(8)
our ty "@i16" shouldBeA[TypeInt] { _.length shouldEqual 16 } our ty "@i16" shouldBeATypeIntOf(16)
our ty "@i32" shouldBeA[TypeInt] { _.length shouldEqual 32 } our ty "@i32" shouldBeATypeIntOf(32)
our ty "@i64" shouldBeA[TypeInt] { _.length shouldEqual 64 } our ty "@i64" shouldBeATypeIntOf(64)
our ty "@f" shouldBeA[TypeFloat] thatsIt our ty "@f" shouldBeA[TypeFloat] thatsIt
our ty "@d" shouldBeA[TypeDouble] thatsIt our ty "@d" shouldBeA[TypeDouble] thatsIt
...@@ -33,37 +34,37 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers { ...@@ -33,37 +34,37 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers {
our ty "@irv" shouldBeA[TypeIRef] { _.ty shouldBeA[TypeVoid] thatsIt } our ty "@irv" shouldBeA[TypeIRef] { _.ty shouldBeA[TypeVoid] thatsIt }
our ty "@wrv" shouldBeA[TypeWeakRef] { _.ty shouldBeA[TypeVoid] thatsIt } our ty "@wrv" shouldBeA[TypeWeakRef] { _.ty shouldBeA[TypeVoid] thatsIt }
our ty "@ri16" shouldBeA[TypeRef] { _.ty shouldBeA[TypeInt] {_.length shouldEqual 16}} our ty "@ri16" shouldBeA[TypeRef] { _.ty shouldBeATypeIntOf(16)}
our ty "@ri16_2" shouldBeA[TypeRef] { _.ty shouldBeA[TypeInt] {_.length shouldEqual 16}} our ty "@ri16_2" shouldBeA[TypeRef] { _.ty shouldBeATypeIntOf(16)}
our ty "@s0" shouldBeA[TypeStruct] { _.fieldTy shouldBe empty } our ty "@s0" shouldBeA[TypeStruct] { _.fieldTy shouldBe empty }
our ty "@s1" shouldBeA[TypeStruct] { its => our ty "@s1" shouldBeA[TypeStruct] { its =>
its fieldTy 0 shouldBeA[TypeInt] { _.length shouldEqual 8 } its fieldTy 0 shouldBeATypeIntOf(8)
its fieldTy 1 shouldBeA[TypeInt] { _.length shouldEqual 16 } its fieldTy 1 shouldBeATypeIntOf(16)
its fieldTy 2 shouldBeA[TypeInt] { _.length shouldEqual 32 } its fieldTy 2 shouldBeATypeIntOf(32)
its fieldTy 3 shouldBeA[TypeInt] { _.length shouldEqual 64 } its fieldTy 3 shouldBeATypeIntOf(64)
its fieldTy 4 shouldBeA[TypeFloat] thatsIt its fieldTy 4 shouldBeA[TypeFloat] thatsIt
its fieldTy 5 shouldBeA[TypeDouble] thatsIt its fieldTy 5 shouldBeA[TypeDouble] thatsIt
its fieldTy 6 shouldBeA[TypeRef] { _.ty shouldBeA[TypeVoid] thatsIt } its fieldTy 6 shouldBeA[TypeRef] { _.ty shouldBeA[TypeVoid] thatsIt }
its fieldTy 7 shouldBeA[TypeIRef] { _.ty shouldBeA[TypeVoid] thatsIt } its fieldTy 7 shouldBeA[TypeIRef] { _.ty shouldBeA[TypeVoid] thatsIt }
its fieldTy 8 shouldBeA[TypeWeakRef] { _.ty shouldBeA[TypeVoid] thatsIt } its fieldTy 8 shouldBeA[TypeWeakRef] { _.ty shouldBeA[TypeVoid] thatsIt }
its fieldTy 9 shouldBeA[TypeRef] { _.ty shouldBeA[TypeInt] {_.length shouldEqual 16}} its fieldTy 9 shouldBeA[TypeRef] { _.ty shouldBeATypeIntOf(16)}
its fieldTy 10 shouldBeA[TypeRef] { _.ty shouldBeA[TypeInt] {_.length shouldEqual 16}} its fieldTy 10 shouldBeA[TypeRef] { _.ty shouldBeATypeIntOf(16)}
} }
our ty "@cons" shouldBeA[TypeStruct] { its => our ty "@cons" shouldBeA[TypeStruct] { its =>
its fieldTy 0 shouldBeA[TypeInt] { _.length shouldEqual 64 } its fieldTy 0 shouldBeATypeIntOf(64)
its fieldTy 1 shouldBeA[TypeRef] { _.ty shouldBe (our ty "@cons")} its fieldTy 1 shouldBeA[TypeRef] { _.ty shouldBe (our ty "@cons")}
} }
our ty "@a0" shouldBeA[TypeArray] { its => our ty "@a0" shouldBeA[TypeArray] { its =>
its.elemTy shouldBeA[TypeInt] { _.length shouldEqual 8} its.elemTy shouldBeATypeIntOf(8)
its.len shouldEqual 100 its.len shouldEqual 100
} }
our ty "@a1" shouldBeA[TypeArray] { its => our ty "@a1" shouldBeA[TypeArray] { its =>
its.elemTy shouldBeA[TypeStruct] { whose => its.elemTy shouldBeA[TypeStruct] { whose =>
whose fieldTy 0 shouldBeA[TypeDouble] thatsIt whose fieldTy 0 shouldBeA[TypeDouble] thatsIt
whose fieldTy 1 shouldBeA[TypeInt] { _.length shouldEqual 64} whose fieldTy 1 shouldBeATypeIntOf(64)
} }
its.len shouldEqual 10 its.len shouldEqual 10
} }
...@@ -74,7 +75,7 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers { ...@@ -74,7 +75,7 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers {
our ty "@h0" shouldBeA[TypeHybrid] { its => our ty "@h0" shouldBeA[TypeHybrid] { its =>
its.fixedPart shouldBeA[TypeVoid] thatsIt its.fixedPart shouldBeA[TypeVoid] thatsIt
its.varPart shouldBeA[TypeInt] {_.length shouldEqual 8} its.varPart shouldBeATypeIntOf(8)
} }
our ty "@h1" shouldBeA[TypeHybrid] { its => our ty "@h1" shouldBeA[TypeHybrid] { its =>
its.fixedPart shouldBeA[TypeStruct] { whose => its.fixedPart shouldBeA[TypeStruct] { whose =>
...@@ -82,7 +83,7 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers { ...@@ -82,7 +83,7 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers {
whose fieldTy 1 shouldBe (our ty "@i32") whose fieldTy 1 shouldBe (our ty "@i32")
whose fieldTy 2 shouldBeA[TypeFloat] thatsIt whose fieldTy 2 shouldBeA[TypeFloat] thatsIt
} }
its.varPart shouldBeA[TypeInt] {_.length shouldEqual 64} its.varPart shouldBeATypeIntOf(64)
} }
our ty "@v" shouldBeA[TypeVoid] thatsIt our ty "@v" shouldBeA[TypeVoid] thatsIt
...@@ -92,10 +93,10 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers { ...@@ -92,10 +93,10 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers {
its.sig.paramTy shouldBe empty its.sig.paramTy shouldBe empty
} }
our ty "@f1" shouldBeA[TypeFunc] { its => our ty "@f1" shouldBeA[TypeFunc] { its =>
its.sig.retTy shouldBeA[TypeInt] {_.length shouldEqual 32} its.sig.retTy shouldBeATypeIntOf(32)
its.sig.paramTy(0) shouldBeA[TypeInt] {_.length shouldEqual 32} its.sig paramTy 0 shouldBeATypeIntOf(32)
its.sig.paramTy(1) shouldBeA[TypeIRef] { _.ty shouldBeA[TypeIRef] { its.sig.paramTy(1) shouldBeA[TypeIRef] { _.ty shouldBeA[TypeIRef] {
_.ty shouldBeA[TypeInt] {_.length shouldEqual 8}} _.ty shouldBeATypeIntOf(8)}
} }
} }
...@@ -103,4 +104,90 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers { ...@@ -103,4 +104,90 @@ trait TestingBundlesValidators extends Matchers with ExtraMatchers {
our ty "@st" shouldBeA[TypeStack] thatsIt our ty "@st" shouldBeA[TypeStack] thatsIt
our ty "@tr64" shouldBeA[TypeTagRef64] thatsIt our ty "@tr64" shouldBeA[TypeTagRef64] thatsIt
} }
def validateConstants(bundle: Bundle) {
val our = bundle
our const "@ci8" shouldBeAConstIntOf (8, 127)
our const "@ci16" shouldBeAConstIntOf (16, 32767)
our const "@ci32" shouldBeAConstIntOf (32, 2147483647)
our const "@ci64" shouldBeAConstIntOf (64, 9223372036854775807L)
our const "@ci64neg" shouldBeAConstIntOf (64, BigInt(-42L) & 0xffffffffffffffffL)
our const "@cio64" shouldBeAConstIntOf (64, BigInt("777", 8))
our const "@cix64" shouldBeAConstIntOf (64, BigInt("123456789abcdef0", 16))
our const "@cixovf" shouldBeAConstIntOf (64, BigInt("ffffffffffffffff", 16))
our const "@cixovf2" shouldBeAConstIntOf (64, BigInt("8000000000000000", 16))
our const "@cf" shouldBeAConstFloatOf 3.14F
our const "@cfnan" shouldBeAConstFloatOf nan
our const "@cfninf" shouldBeAConstFloatOf Float.NegativeInfinity
our const "@cfpinf" shouldBeAConstFloatOf Float.PositiveInfinity
our const "@cfbits" shouldBeAConstFloatOf exactly(bitsf(0x12345678))
our const "@cd" shouldBeAConstDoubleOf 6.28D
our const "@cdnan" shouldBeAConstDoubleOf nan
our const "@cdninf" shouldBeAConstDoubleOf Double.NegativeInfinity
our const "@cdpinf" shouldBeAConstDoubleOf Double.PositiveInfinity
our const "@cdbits" shouldBeAConstDoubleOf exactly(bitsd(0xfedcba9876543210L))
our const "@cs1" shouldBeA[ConstStruct] { its=>
its.constTy shouldBeA[TypeStruct] { whose =>
whose fieldTy 0 shouldBeATypeIntOf(64)
whose fieldTy 1 shouldBeA[TypeDouble] thatsIt
}
its fields 0 shouldBeAConstIntOf (64, 100)
its fields 1 shouldBeAConstDoubleOf 200.0D
}
our const "@cs2" shouldBeA[ConstStruct] { its=>
its.constTy shouldBeA[TypeStruct] { whose =>
whose fieldTy 0 shouldBeA[TypeDouble] thatsIt
whose fieldTy 1 shouldBeA[TypeStruct] { where =>
where fieldTy 0 shouldBeA[TypeFloat] thatsIt
where fieldTy 1 shouldBeATypeIntOf(64)
}
whose fieldTy 2 shouldBeATypeIntOf(32)
}
its fields 0 shouldBeAConstDoubleOf 1.0D
its fields 1 shouldBeA[ConstStruct] { whose =>
whose fields 0 shouldBeAConstFloatOf 2.0F
whose fields 1 shouldBeAConstIntOf (64, 3)
}
its fields 2 shouldBeAConstIntOf (32, 4)
}
our const "@cons" shouldBeA[ConstStruct] { its =>
its.constTy shouldBe (our ty "@Cons")
its fields 0 shouldBeAConstIntOf (64, 42)
its fields 1 shouldBeA[ConstNull] { _.ty shouldBeA[TypeRef] { _.ty shouldBe (our ty "@Cons") }}
}
our const "@cr" shouldBeA[ConstNull] { _.ty shouldBeA[TypeRef] { _.ty shouldBeATypeIntOf(64) }}
our const "@cir" shouldBeA[ConstNull] { _.ty shouldBeA[TypeIRef] { _.ty shouldBeA[TypeFloat] thatsIt }}
our const "@cwr" shouldBeA[ConstNull] { _.ty shouldBeA[TypeWeakRef] { _.ty shouldBe (our ty "@Cons") }}
our const "@cfu" shouldBeA[ConstNull] { _.ty shouldBeA[TypeFunc] { _.sig shouldBeA[FuncSig] { its =>
its.retTy shouldBeA[TypeVoid] thatsIt
its.paramTy shouldBe empty
}}}
our const "@cth" shouldBeA[ConstNull] { _.ty shouldBeA[TypeThread] thatsIt }
our const "@cst" shouldBeA[ConstNull] { _.ty shouldBeA[TypeStack] thatsIt }
our globalData "@gi64" shouldBeA[GlobalData] { _.ty shouldBeATypeIntOf(64) }
our func "@fdummy" shouldBeA[Function] { _.sig shouldBeA[FuncSig] { its =>
its.retTy shouldBeA[TypeVoid] thatsIt
its.paramTy shouldBe empty
}}
}
def validateFunctions(bundle: Bundle) {
val our = bundle
}
def validateInstructions(bundle: Bundle) {
val our = bundle
}
} }
\ No newline at end of file
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