UvmBundleTesterBase.scala 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package uvm.refimpl

import org.scalatest._
import java.io.FileReader
import uvm._
import uvm.types._
import uvm.ssavariables._
import uvm.refimpl._
import uvm.refimpl.itpr._
import uvm.ssavariables.MemoryOrder._
import uvm.ssavariables.AtomicRMWOptr._
import uvm.refimpl.mem.TypeSizes.Word
import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory
import ch.qos.logback.classic.{ Logger => LLogger }
import ch.qos.logback.classic.Level
17 18
import uvm.refimpl.TrapHandlerResult.Rebind
import uvm.refimpl.HowToResume.PassValues
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

object UvmBundleTesterBase {
  val logger = Logger(LoggerFactory.getLogger(getClass.getName))
}

abstract class UvmBundleTesterBase extends FlatSpec with Matchers {
  val ROOT_LOGGER_NAME = org.slf4j.Logger.ROOT_LOGGER_NAME

  def setLogLevels(settings: (String, Level)*): Unit = { // Configure logger
    import org.slf4j.LoggerFactory
    import org.slf4j.{ Logger => SLogger }
    import ch.qos.logback.classic.{ Logger => LLogger, Level }
    import ch.qos.logback.classic.Level._

    def setLevel(name: String, level: Level): Unit = {
      LoggerFactory.getLogger(name).asInstanceOf[LLogger].setLevel(level)
    }

    for ((name, lvl) <- settings) {
      setLevel(name, lvl)
    }
  }

  def makeMicroVM(): MicroVM = new MicroVM()

  val microVM = makeMicroVM()

  implicit def idOf(name: String): Int = microVM.idOf(name)
  implicit def nameOf(id: Int): String = microVM.nameOf(id)

  def preloadBundles(fileNames: String*): Unit = {
Kunshan Wang's avatar
Kunshan Wang committed
50
    val ctx = microVM.newContext()
51 52 53

    for (fn <- fileNames) {
      val r = new FileReader(fn)
Kunshan Wang's avatar
Kunshan Wang committed
54
      ctx.loadBundle(r)
55 56 57
      r.close()
    }

Kunshan Wang's avatar
Kunshan Wang committed
58
    ctx.closeContext()
59 60
  }

Kunshan Wang's avatar
Kunshan Wang committed
61
  type TrapHandlerFunction = (MuCtx, MuThreadRefValue, MuStackRefValue, Int) => TrapHandlerResult
62 63

  class MockTrapHandler(thf: TrapHandlerFunction) extends TrapHandler {
Kunshan Wang's avatar
Kunshan Wang committed
64 65
    def handleTrap(ctx: MuCtx, thread: MuThreadRefValue, stack: MuStackRefValue, watchPointID: Int): TrapHandlerResult = {
      thf(ctx, thread, stack, watchPointID)
66 67 68
    }
  }

Kunshan Wang's avatar
Kunshan Wang committed
69
  def testFunc(ctx: MuCtx, func: MuFuncRefValue, args: Seq[MuValue])(handler: TrapHandlerFunction): Unit = {
70
    microVM.trapManager.trapHandler = new MockTrapHandler(handler)
Kunshan Wang's avatar
Kunshan Wang committed
71 72
    val hStack = ctx.newStack(func)
    val hThread = ctx.newThread(hStack, HowToResume.PassValues(args))
Kunshan Wang's avatar
Kunshan Wang committed
73
    microVM.execute()
74
  }
Kunshan Wang's avatar
Kunshan Wang committed
75 76
  
  implicit def magicalMuValue(mv: MuValue): MagicalBox = MagicalBox(mv.vb)
77 78 79 80 81 82 83 84 85 86

  implicit class MagicalBox(vb: ValueBox) {
    def asInt: BigInt = vb.asInstanceOf[BoxInt].value
    def asSInt(l: Int): BigInt = OpHelper.prepareSigned(vb.asInstanceOf[BoxInt].value, l)
    def asUInt(l: Int): BigInt = OpHelper.prepareUnsigned(vb.asInstanceOf[BoxInt].value, l)
    def asFloat: Float = vb.asInstanceOf[BoxFloat].value
    def asDouble: Double = vb.asInstanceOf[BoxDouble].value
    def asRef: Word = vb.asInstanceOf[BoxRef].objRef
    def asIRef: (Word, Word) = { val b = vb.asInstanceOf[BoxIRef]; (b.objRef, b.offset) }
    def asIRefAddr: Word = { val b = vb.asInstanceOf[BoxIRef]; b.objRef + b.offset }
Kunshan Wang's avatar
Kunshan Wang committed
87
    def asStruct: Seq[ValueBox] = vb.asInstanceOf[BoxSeq].values
88 89 90 91 92
    def asFunc: Option[Function] = vb.asInstanceOf[BoxFunc].func
    def asThread: Option[InterpreterThread] = vb.asInstanceOf[BoxThread].thread
    def asStack: Option[InterpreterStack] = vb.asInstanceOf[BoxStack].stack
    def asTR64Box: BoxTagRef64 = vb.asInstanceOf[BoxTagRef64]
    def asTR64Raw: Long = vb.asInstanceOf[BoxTagRef64].raw
Kunshan Wang's avatar
Kunshan Wang committed
93 94
    def asSeq: Seq[ValueBox] = vb.asInstanceOf[BoxSeq].values
    def asVec: Seq[ValueBox] = vb.asInstanceOf[BoxSeq].values
95
    def asPointer: Word = vb.asInstanceOf[BoxPointer].addr
96
  }
Kunshan Wang's avatar
Kunshan Wang committed
97 98 99 100 101 102
  
  implicit class RichMuCtx(ctx: MuCtx) {
    def i32(num: BigInt) = ctx.handleFromInt(num, 32)
    def i64(num: BigInt) = ctx.handleFromInt(num, 64)
    def func(id: Int) = ctx.handleFromFunc(id)
  }
103 104
  
  def returnFromTrap(st: MuStackRefValue) = Rebind(st, PassValues(Seq()))
105
}