clientInterface.scala 17.8 KB
Newer Older
1 2 3 4 5 6
package uvm.refimpl

import uvm.types._
import uvm.refimpl.itpr._
import java.io.Reader
import scala.collection.mutable.HashSet
7
import uvm.refimpl.mem.TypeSizes._
8 9
import uvm.ssavariables.MemoryOrder._
import uvm.ssavariables.AtomicRMWOptr._
10
import uvm.refimpl.mem._
Kunshan Wang's avatar
Kunshan Wang committed
11
import uvm.ssavariables.HasKeepAliveClause
Kunshan Wang's avatar
Kunshan Wang committed
12
import scala.collection.mutable.ArrayBuffer
13
import uvm.ssavariables.Flag
14 15 16

case class Handle(ty: Type, vb: ValueBox)

Kunshan Wang's avatar
Kunshan Wang committed
17 18
abstract class TrapHandlerResult
case class TrapExit() extends TrapHandlerResult
Kunshan Wang's avatar
Kunshan Wang committed
19
case class TrapRebindPassValues(newStack: Handle, values: Seq[Handle]) extends TrapHandlerResult
Kunshan Wang's avatar
Kunshan Wang committed
20 21 22 23 24 25 26 27 28 29
case class TrapRebindThrowExc(newStack: Handle, exc: Handle) extends TrapHandlerResult

trait TrapHandler {
  def handleTrap(ca: ClientAgent, thread: Handle, stack: Handle, watchPointID: Int): TrapHandlerResult
}

trait UndefinedFunctionHandler {
  def handleUndefinedFunction(functionID: Int): Unit
}

Kunshan Wang's avatar
Kunshan Wang committed
30 31
class ClientAgent(mutator: Mutator)(
    implicit microVM: MicroVM, memorySupport: MemorySupport) extends ObjectPinner {
32 33
  val handles = new HashSet[Handle]()

Kunshan Wang's avatar
Kunshan Wang committed
34
  val pinSet = new ArrayBuffer[Word]
35

36 37 38 39 40 41 42 43 44 45
  /**
   * Given a name, get the ID of an identified entity.
   */
  def idOf(name: String): Int = microVM.idOf(name)

  /**
   * Given an ID, get the name of an identified entity.
   */
  def nameOf(id: Int): String = microVM.nameOf(id)

46 47
  def close(): Unit = {
    handles.clear()
48
    mutator.close()
Kunshan Wang's avatar
Kunshan Wang committed
49
    microVM.clientAgents.remove(this)
50 51 52 53 54 55
  }

  def loadBundle(r: Reader): Unit = {
    val bundle = microVM.irReader.read(r, microVM.globalBundle)
    microVM.addBundle(bundle)
  }
Kunshan Wang's avatar
Kunshan Wang committed
56

57 58 59 60
  def loadBundle(s: String): Unit = {
    val bundle = microVM.irReader.read(s, microVM.globalBundle)
    microVM.addBundle(bundle)
  }
61 62 63 64 65 66 67 68 69

  private def newHandle(t: Type, vb: ValueBox): Handle = {
    val handle = Handle(t, vb)
    handles.add(handle)
    return handle
  }

  def putInt(typeID: Int, v: BigInt): Handle = {
    val t = microVM.globalBundle.typeNs(typeID).asInstanceOf[TypeInt]
70
    val preparedV = OpHelper.unprepare(v, t.length)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    newHandle(t, BoxInt(preparedV))
  }

  def putFloat(typeID: Int, v: Float): Handle = {
    val t = microVM.globalBundle.typeNs(typeID).asInstanceOf[TypeFloat]
    newHandle(t, BoxFloat(v))
  }

  def putDouble(typeID: Int, v: Double): Handle = {
    val t = microVM.globalBundle.typeNs(typeID).asInstanceOf[TypeDouble]
    newHandle(t, BoxDouble(v))
  }

  def putIntVec(typeID: Int, vs: Seq[BigInt]): Handle = {
    val t = microVM.globalBundle.typeNs(typeID).asInstanceOf[TypeVector]
    val et = t.elemTy.asInstanceOf[TypeInt]
    val preparedVs = for (v <- vs) yield OpHelper.trunc(v, et.length)
Kunshan Wang's avatar
Kunshan Wang committed
88
    newHandle(t, BoxSeq(preparedVs.map(BoxInt)))
89 90 91 92 93
  }

  def putFloatVec(typeID: Int, vs: Seq[Float]): Handle = {
    val t = microVM.globalBundle.typeNs(typeID).asInstanceOf[TypeVector]
    val et = t.elemTy.asInstanceOf[TypeFloat]
Kunshan Wang's avatar
Kunshan Wang committed
94
    newHandle(t, BoxSeq(vs.map(BoxFloat)))
95 96 97 98 99
  }

  def putDoubleVec(typeID: Int, vs: Seq[Double]): Handle = {
    val t = microVM.globalBundle.typeNs(typeID).asInstanceOf[TypeVector]
    val et = t.elemTy.asInstanceOf[TypeDouble]
Kunshan Wang's avatar
Kunshan Wang committed
100
    newHandle(t, BoxSeq(vs.map(BoxDouble)))
101
  }
Kunshan Wang's avatar
Kunshan Wang committed
102

103 104 105 106
  def putPointer(typeID: Int, v: Word): Handle = {
    val t = microVM.globalBundle.typeNs(typeID)
    newHandle(t, BoxPointer(v))
  }
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

  def putConstant(id: Int): Handle = {
    val c = microVM.globalBundle.constantNs(id)
    val t = c.constTy
    val box = microVM.constantPool.getGlobalVarBox(c)
    newHandle(t, box)
  }

  def putGlobal(id: Int): Handle = {
    val g = microVM.globalBundle.globalCellNs(id)
    val t = InternalTypePool.irefOf(g.cellTy)
    val a = microVM.memoryManager.globalMemory.addrForGlobalCell(g)
    val box = BoxIRef(0L, a)
    newHandle(t, box)
  }

  def putFunction(id: Int): Handle = {
    val f = microVM.globalBundle.funcNs(id)
    val t = InternalTypePool.funcOf(f.sig)
    val box = BoxFunc(Some(f))
    newHandle(t, box)
  }

130 131 132
  def putExpFunc(id: Int): Handle = {
    val ef = microVM.globalBundle.expFuncNs(id)
    val t = InternalTypePool.funcPtrOf(ef.func.sig)
133
    val box = BoxPointer(microVM.nativeCallHelper.getStaticExpFuncAddr(ef))
134 135 136
    newHandle(t, box)
  }

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  def deleteHandle(h: Handle): Unit = {
    handles.remove(h)
  }

  def toInt(h: Handle, signExt: Boolean = false): BigInt = {
    val t = h.ty.asInstanceOf[TypeInt]
    val ib = h.vb.asInstanceOf[BoxInt]
    if (signExt) OpHelper.prepareSigned(ib.value, t.length) else OpHelper.prepareUnsigned(ib.value, t.length)
  }

  def toFloat(h: Handle): Float = {
    h.vb.asInstanceOf[BoxFloat].value
  }

  def toDouble(h: Handle): Double = {
    h.vb.asInstanceOf[BoxDouble].value
  }

  def toIntVec(h: Handle, signExt: Boolean = false): Seq[BigInt] = {
    val t = h.ty.asInstanceOf[TypeVector]
    val et = t.elemTy.asInstanceOf[TypeInt]
Kunshan Wang's avatar
Kunshan Wang committed
158
    val bv = h.vb.asInstanceOf[BoxSeq]
159 160 161 162 163 164 165
    for (b <- bv.values) yield {
      val ib = b.asInstanceOf[BoxInt]
      if (signExt) OpHelper.prepareSigned(ib.value, et.length) else OpHelper.prepareUnsigned(ib.value, et.length)
    }
  }

  def toFloatVec(h: Handle): Seq[Float] = {
Kunshan Wang's avatar
Kunshan Wang committed
166
    h.vb.asInstanceOf[BoxSeq].values.map(b => b.asInstanceOf[BoxFloat].value)
167 168 169
  }

  def toDoubleVec(h: Handle): Seq[Double] = {
Kunshan Wang's avatar
Kunshan Wang committed
170
    h.vb.asInstanceOf[BoxSeq].values.map(b => b.asInstanceOf[BoxDouble].value)
171
  }
Kunshan Wang's avatar
Kunshan Wang committed
172

173 174 175
  def toPointer(h: Handle): Word = {
    h.vb.asInstanceOf[BoxPointer].addr
  }
176 177 178 179

  def extractValue(str: Handle, index: Int): Handle = {
    val st = str.ty.asInstanceOf[TypeStruct]
    val sb = str.vb.asInstanceOf[BoxStruct]
Kunshan Wang's avatar
Kunshan Wang committed
180
    val et = st.fieldTys(index)
181 182 183 184 185 186 187 188 189 190
    val eb = sb.values(index)
    newHandle(et, eb)
  }

  def insertValue(str: Handle, index: Int, newVal: Handle): Handle = {
    val st = str.ty.asInstanceOf[TypeStruct]
    val sb = str.vb.asInstanceOf[BoxStruct]
    val nsb = BoxStruct(for ((b, i) <- sb.values.zipWithIndex) yield if (i == index) newVal.vb else b)
    newHandle(st, nsb)
  }
191 192

  def newFixed(tid: Int): Handle = {
193 194 195 196 197
    val t = microVM.globalBundle.typeNs(tid)
    val objRef = mutator.newScalar(t)
    val b = BoxRef(objRef)
    val rt = InternalTypePool.refOf(t)
    newHandle(rt, b)
198 199 200
  }

  def newHybrid(tid: Int, length: Handle): Handle = {
201 202 203 204 205 206
    val t = microVM.globalBundle.typeNs(tid).asInstanceOf[TypeHybrid]
    val len = toInt(length).longValue
    val objRef = mutator.newHybrid(t, len)
    val b = BoxRef(objRef)
    val rt = InternalTypePool.refOf(t)
    newHandle(rt, b)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  }

  def refCast(handle: Handle, newType: Int): Handle = {
    val t = microVM.globalBundle.typeNs(newType)
    newHandle(t, handle.vb)
  }

  def getIRef(handle: Handle): Handle = {
    val t = handle.ty.asInstanceOf[TypeRef]
    val nt = InternalTypePool.irefOf(t.ty)
    val ob = handle.vb.asInstanceOf[BoxRef]
    val nb = BoxIRef(ob.objRef, 0L)
    newHandle(nt, nb)
  }

  def getFieldIRef(handle: Handle, index: Int): Handle = {
    val t = handle.ty.asInstanceOf[TypeIRef]
    val st = t.ty.asInstanceOf[TypeStruct]
Kunshan Wang's avatar
Kunshan Wang committed
225
    val ft = st.fieldTys(index)
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    val nt = InternalTypePool.irefOf(ft)
    val ob = handle.vb.asInstanceOf[BoxIRef]
    val nb = BoxIRef(ob.objRef, ob.offset + TypeSizes.fieldOffsetOf(st, index))
    newHandle(nt, nb)
  }

  def getElemIRef(handle: Handle, index: Handle): Handle = {
    val t = handle.ty.asInstanceOf[TypeIRef]
    val st = t.ty.asInstanceOf[AbstractSeqType]
    val et = st.elemTy
    val nt = InternalTypePool.irefOf(et)
    val ob = handle.vb.asInstanceOf[BoxIRef]
    val i = toInt(index, signExt = true).longValue
    val nb = BoxIRef(ob.objRef, ob.offset + TypeSizes.elemOffsetOf(st, i))
    newHandle(nt, nb)
  }

  def shiftIRef(handle: Handle, index: Handle): Handle = {
    val t = handle.ty.asInstanceOf[TypeIRef]
    val rt = t.ty
    val nt = InternalTypePool.irefOf(rt)
    val ob = handle.vb.asInstanceOf[BoxIRef]
    val i = toInt(index, signExt = true).longValue
    val nb = BoxIRef(ob.objRef, ob.offset + TypeSizes.shiftOffsetOf(rt, i))
    newHandle(nt, nb)
  }

  def getVarPartIRef(handle: Handle): Handle = {
    val t = handle.ty.asInstanceOf[TypeIRef]
    val ht = t.ty.asInstanceOf[TypeHybrid]
    val vt = ht.varTy
    val nt = InternalTypePool.irefOf(vt)
    val ob = handle.vb.asInstanceOf[BoxIRef]
    val nb = BoxIRef(ob.objRef, ob.offset + TypeSizes.varPartOffsetOf(ht))
    newHandle(nt, nb)
  }
Kunshan Wang's avatar
Kunshan Wang committed
262

263
  def load(ord: MemoryOrder, loc: Handle): Handle = {
264 265
    val (ptr, ty) = loc.ty match {
      case TypeIRef(t) => (false, t)
Kunshan Wang's avatar
Kunshan Wang committed
266
      case TypeUPtr(t) => (true, t)
267
    }
268
    val uty = InternalTypePool.unmarkedOf(ty)
269
    val addr = MemoryOperations.addressOf(ptr, loc.vb)
Kunshan Wang's avatar
Kunshan Wang committed
270 271
    val nb = ValueBox.makeBoxForType(uty)

272
    MemoryOperations.load(ptr, uty, addr, nb)
273

274
    newHandle(uty, nb)
275 276 277
  }

  def store(ord: MemoryOrder, loc: Handle, newVal: Handle): Unit = {
278 279
    val (ptr, ty) = loc.ty match {
      case TypeIRef(t) => (false, t)
Kunshan Wang's avatar
Kunshan Wang committed
280
      case TypeUPtr(t) => (true, t)
281
    }
282
    val uty = InternalTypePool.unmarkedOf(ty)
283
    val addr = MemoryOperations.addressOf(ptr, loc.vb)
Kunshan Wang's avatar
Kunshan Wang committed
284
    val nvb = newVal.vb
Kunshan Wang's avatar
Kunshan Wang committed
285 286
    
    MemoryOperations.store(ptr, uty, addr, nvb)
287 288 289
  }

  def cmpXchg(ordSucc: MemoryOrder, ordFail: MemoryOrder, weak: Boolean, loc: Handle, expected: Handle, desired: Handle): (Boolean, Handle) = {
290 291
    val (ptr, ty) = loc.ty match {
      case TypeIRef(t) => (false, t)
Kunshan Wang's avatar
Kunshan Wang committed
292
      case TypeUPtr(t) => (true, t)
293
    }
294
    val uty = InternalTypePool.unmarkedOf(ty)
295
    val addr = MemoryOperations.addressOf(ptr, loc.vb)
296 297
    val eb = expected.vb
    val db = desired.vb
298
    val br = ValueBox.makeBoxForType(uty)
299
    val succ = MemoryOperations.cmpXchg(ptr, uty, addr, eb, db, br)
300
    (succ, newHandle(uty, br))
301 302 303
  }

  def atomicRMW(ord: MemoryOrder, op: AtomicRMWOptr, loc: Handle, opnd: Handle): Handle = {
304 305
    val (ptr, ty) = loc.ty match {
      case TypeIRef(t) => (false, t)
Kunshan Wang's avatar
Kunshan Wang committed
306
      case TypeUPtr(t) => (true, t)
307
    }
308
    val uty = InternalTypePool.unmarkedOf(ty)
309
    val addr = MemoryOperations.addressOf(ptr, loc.vb)
310
    val ob = opnd.vb
311
    val br = ValueBox.makeBoxForType(uty)
312
    MemoryOperations.atomicRMW(ptr, uty, op, addr, ob, br)
313
    newHandle(uty, br)
314
  }
Kunshan Wang's avatar
Kunshan Wang committed
315

316 317 318
  def fence(ord: MemoryOrder): Unit = {
  }

Kunshan Wang's avatar
Kunshan Wang committed
319
  def newStack(func: Handle): Handle = {
Kunshan Wang's avatar
Kunshan Wang committed
320 321
    val funcVal = func.vb.asInstanceOf[BoxFunc].func.getOrElse {
      throw new UvmRuntimeException("Stack-bottom function must not be NULL")
Kunshan Wang's avatar
Kunshan Wang committed
322
    }
Kunshan Wang's avatar
Kunshan Wang committed
323

Kunshan Wang's avatar
Kunshan Wang committed
324
    val sta = microVM.threadStackManager.newStack(funcVal, mutator)
Kunshan Wang's avatar
Kunshan Wang committed
325 326 327

    val nb = BoxStack(Some(sta))
    newHandle(InternalTypes.STACK, nb)
328 329
  }

Kunshan Wang's avatar
Kunshan Wang committed
330 331
  private def getStackNotNull(stack: Handle): InterpreterStack = {
    stack.vb.asInstanceOf[BoxStack].stack match {
Kunshan Wang's avatar
Kunshan Wang committed
332
      case None    => throw new UvmRuntimeException("Stack argument cannot be a NULL micro VM stack value.")
Kunshan Wang's avatar
Kunshan Wang committed
333 334 335 336
      case Some(v) => v
    }
  }

337
  def newThread(stack: Handle): Handle = {
Kunshan Wang's avatar
Kunshan Wang committed
338 339 340 341 342
    val sv = getStackNotNull(stack)
    val thr = microVM.threadStackManager.newThread(sv)

    val nb = BoxThread(Some(thr))
    newHandle(InternalTypes.THREAD, nb)
343
  }
Kunshan Wang's avatar
Kunshan Wang committed
344 345

  def killStack(stack: Handle): Unit = {
Kunshan Wang's avatar
Kunshan Wang committed
346 347
    val sv = getStackNotNull(stack)

348
    sv.kill()
Kunshan Wang's avatar
Kunshan Wang committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
  }

  private def nthFrame(stack: InterpreterStack, n: Int): InterpreterFrame = {
    val it = stack.frames
    for (i <- (0 until n)) {
      if (it.hasNext) {
        it.next()
      } else {
        throw new UvmRuntimeException("The stack only has %d frames, but the %d-th frame is requested.".format(i, n))
      }
    }
    if (it.hasNext) {
      it.next()
    } else {
      throw new UvmRuntimeException("The stack only has %d frames, but the %d-th frame is requested.".format(n, n))
    }

Kunshan Wang's avatar
Kunshan Wang committed
366 367 368
  }

  def currentFuncVer(stack: Handle, frame: Int): Int = {
Kunshan Wang's avatar
Kunshan Wang committed
369 370
    val sv = getStackNotNull(stack)
    val fr = nthFrame(sv, frame)
371
    fr match {
Kunshan Wang's avatar
Kunshan Wang committed
372 373 374
      case f: NativeFrame      => 0
      case f: UndefinedMuFrame => 0
      case f: DefinedMuFrame   => f.funcVer.id
375
    }
Kunshan Wang's avatar
Kunshan Wang committed
376 377 378
  }

  def currentInstruction(stack: Handle, frame: Int): Int = {
Kunshan Wang's avatar
Kunshan Wang committed
379 380
    val sv = getStackNotNull(stack)
    val fr = nthFrame(sv, frame)
381
    fr match {
Kunshan Wang's avatar
Kunshan Wang committed
382 383 384
      case f: NativeFrame      => 0
      case f: UndefinedMuFrame => 0
      case f: DefinedMuFrame   => f.curInst.id
385
    }
Kunshan Wang's avatar
Kunshan Wang committed
386 387 388
  }

  def dumpKeepalives(stack: Handle, frame: Int): Seq[Handle] = {
Kunshan Wang's avatar
Kunshan Wang committed
389 390
    val sv = getStackNotNull(stack)
    val fr = nthFrame(sv, frame)
391 392 393
    fr match {
      case f: NativeFrame => {
        throw new UvmRefImplException("Attempt to dump keepalive variables for a native frame for native funciton 0x%x".format(f.func))
Kunshan Wang's avatar
Kunshan Wang committed
394
      }
Kunshan Wang's avatar
Kunshan Wang committed
395 396 397 398 399 400
      case f: UndefinedMuFrame => {
        for ((ty,box) <- f.func.sig.paramTys zip f.boxes) yield {
          newHandle(ty, box)
        }
      }
      case f: DefinedMuFrame => {
401 402 403 404 405 406 407 408 409 410 411 412 413 414
        val i = f.curInst
        i match {
          case hkac: HasKeepAliveClause => {
            val kas = hkac.keepAlives
            for (ka <- kas) yield {
              val box = f.boxes(ka)
              val ty = TypeInferer.inferType(ka)
              newHandle(ty, box)
            }
          }
          case _ => {
            throw new UvmRuntimeException("The current instruction %s does not have keep-alive clause.".format(i.repr))
          }
        }
Kunshan Wang's avatar
Kunshan Wang committed
415 416
      }
    }
Kunshan Wang's avatar
Kunshan Wang committed
417 418 419
  }

  def popFrame(stack: Handle): Unit = {
Kunshan Wang's avatar
Kunshan Wang committed
420 421
    val st = getStackNotNull(stack)
    val top = st.top
422 423 424 425 426 427
    top match {
      case f: NativeFrame => throw new UvmRuntimeException("Attempting to pop a native frame. It has implementation-defined behaviour and this refimpl does not allow it.")
      case f: MuFrame => f.prev match {
        case None       => throw new UvmRuntimeException("Attempting to pop the last frame of a stack.")
        case Some(prev) => st.popFrame()
      }
Kunshan Wang's avatar
Kunshan Wang committed
428
    }
Kunshan Wang's avatar
Kunshan Wang committed
429 430
  }

Kunshan Wang's avatar
Kunshan Wang committed
431
  def pushFrame(stack: Handle, func: Handle): Unit = {
Kunshan Wang's avatar
Kunshan Wang committed
432 433 434 435 436 437 438
    val sta = stack.vb.asInstanceOf[BoxStack].stack.getOrElse {
      throw new UvmRuntimeException("Stack must not be NULL")
    }

    val funcVal = func.vb.asInstanceOf[BoxFunc].func.getOrElse {
      throw new UvmRuntimeException("Stack-bottom function must not be NULL")
    }
Kunshan Wang's avatar
Kunshan Wang committed
439

Kunshan Wang's avatar
Kunshan Wang committed
440
    sta.pushFrame(funcVal)
Kunshan Wang's avatar
Kunshan Wang committed
441
  }
Kunshan Wang's avatar
Kunshan Wang committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477

  def tr64IsFp(handle: Handle): Boolean = {
    OpHelper.tr64IsFp(handle.vb.asInstanceOf[BoxTagRef64].raw)
  }

  def tr64IsInt(handle: Handle): Boolean = {
    OpHelper.tr64IsInt(handle.vb.asInstanceOf[BoxTagRef64].raw)
  }

  def tr64IsRef(handle: Handle): Boolean = {
    OpHelper.tr64IsRef(handle.vb.asInstanceOf[BoxTagRef64].raw)
  }

  def tr64ToFp(handle: Handle): Handle = {
    val raw = handle.vb.asInstanceOf[BoxTagRef64].raw
    val box = new BoxDouble(OpHelper.tr64ToFp(raw))
    newHandle(InternalTypes.DOUBLE, box)
  }

  def tr64ToInt(handle: Handle): Handle = {
    val raw = handle.vb.asInstanceOf[BoxTagRef64].raw
    val box = new BoxInt(OpHelper.tr64ToInt(raw))
    newHandle(InternalTypes.I52, box)
  }

  def tr64ToRef(handle: Handle): Handle = {
    val raw = handle.vb.asInstanceOf[BoxTagRef64].raw
    val box = new BoxRef(OpHelper.tr64ToRef(raw))
    newHandle(InternalTypes.REF_VOID, box)
  }

  def tr64ToTag(handle: Handle): Handle = {
    val raw = handle.vb.asInstanceOf[BoxTagRef64].raw
    val box = new BoxInt(OpHelper.tr64ToTag(raw))
    newHandle(InternalTypes.I6, box)
  }
478

Kunshan Wang's avatar
Kunshan Wang committed
479 480 481 482 483
  def tr64FromFp(handle: Handle): Handle = {
    val fp = handle.vb.asInstanceOf[BoxDouble].value
    val box = new BoxTagRef64(OpHelper.fpToTr64(fp))
    newHandle(InternalTypes.TAGREF64, box)
  }
484

Kunshan Wang's avatar
Kunshan Wang committed
485 486 487 488 489 490 491 492 493 494 495 496
  def tr64FromInt(handle: Handle): Handle = {
    val i = handle.vb.asInstanceOf[BoxInt].value
    val box = new BoxTagRef64(OpHelper.intToTr64(i.longValue))
    newHandle(InternalTypes.TAGREF64, box)
  }

  def tr64FromRef(ref: Handle, tag: Handle): Handle = {
    val refv = ref.vb.asInstanceOf[BoxRef].objRef
    val tagv = tag.vb.asInstanceOf[BoxInt].value
    val box = new BoxTagRef64(OpHelper.refToTr64(refv, tagv.longValue))
    newHandle(InternalTypes.TAGREF64, box)
  }
Kunshan Wang's avatar
Kunshan Wang committed
497

498 499 500 501 502 503 504
  def enableWatchPoint(wpID: Int): Unit = {
    microVM.trapManager.enableWatchPoint(wpID)
  }

  def disableWatchPoint(wpID: Int): Unit = {
    microVM.trapManager.disableWatchPoint(wpID)
  }
Kunshan Wang's avatar
Kunshan Wang committed
505

Kunshan Wang's avatar
Kunshan Wang committed
506 507 508 509 510 511 512 513 514 515
  def ptrcast(handle: Handle, newType: Type): Handle = {
    require(handle.ty.isInstanceOf[AbstractPointerType] || handle.ty.isInstanceOf[TypeInt], "handle must have type int, ptr or funcptr. %s found".format(handle.ty.repr))
    require(newType.isInstanceOf[AbstractPointerType] || newType.isInstanceOf[TypeInt], "can only convert to int, ptr or funcptr. %s found".format(newType.repr))

    val addr = handle.ty match {
      case TypeInt(n)             => OpHelper.trunc(handle.vb.asInstanceOf[BoxInt].value, 64).toLong
      case _: AbstractPointerType => handle.vb.asInstanceOf[BoxPointer].addr
    }

    val box = newType match {
Kunshan Wang's avatar
Kunshan Wang committed
516
      case TypeInt(n)             => new BoxInt(OpHelper.trunc(BigInt(addr), Math.min(n, 64)))
Kunshan Wang's avatar
Kunshan Wang committed
517 518 519 520 521 522 523
      case _: AbstractPointerType => new BoxPointer(addr)
    }

    newHandle(newType, box)
  }

  def pin(handle: Handle): Handle = {
Kunshan Wang's avatar
Kunshan Wang committed
524 525 526
    val (objTy, (objRef, offset)) = handle.ty match {
      case TypeRef(t)  => (t, (handle.vb.asInstanceOf[BoxRef].objRef, 0L))
      case TypeIRef(t) => (t, handle.vb.asInstanceOf[BoxIRef].oo)
Kunshan Wang's avatar
Kunshan Wang committed
527 528 529
    }
    pin(objRef)
    val ptrTy = InternalTypePool.ptrOf(objTy)
Kunshan Wang's avatar
Kunshan Wang committed
530
    val box = new BoxPointer(objRef + offset)
Kunshan Wang's avatar
Kunshan Wang committed
531 532 533 534 535 536 537 538 539 540
    newHandle(ptrTy, box)
  }

  def unpin(handle: Handle): Unit = {
    val (objTy, objRef) = handle.ty match {
      case TypeRef(t)  => (t, handle.vb.asInstanceOf[BoxRef].objRef)
      case TypeIRef(t) => (t, handle.vb.asInstanceOf[BoxIRef].objRef)
    }
    unpin(objRef)
  }
Kunshan Wang's avatar
Kunshan Wang committed
541

542
  def expose(func: Handle, callConv: Flag, cookie: Handle): Handle = {
Kunshan Wang's avatar
Kunshan Wang committed
543
    val TypeFuncRef(sig) = func.ty
544 545 546
    val f = func.vb.asInstanceOf[BoxFunc].func.getOrElse {
      throw new UvmRuntimeException("Attempt to expose NULL Mu function")
    }
Kunshan Wang's avatar
Kunshan Wang committed
547

548
    val c = cookie.vb.asInstanceOf[BoxInt].value.toLong
Kunshan Wang's avatar
Kunshan Wang committed
549 550

    val addr = microVM.nativeCallHelper.exposeFuncDynamic(f, c)
551 552
    newHandle(InternalTypePool.funcPtrOf(sig), BoxPointer(addr))
  }
Kunshan Wang's avatar
Kunshan Wang committed
553

554 555 556 557
  def unexpose(callConv: Flag, addr: Handle): Unit = {
    val a = addr.vb.asInstanceOf[BoxPointer].addr
    microVM.nativeCallHelper.unexposeFunc(a)
  }
Kunshan Wang's avatar
Kunshan Wang committed
558

Kunshan Wang's avatar
Kunshan Wang committed
559
  // Internal methods for the micro VM
560 561 562 563 564 565

  def putThread(thr: Option[InterpreterThread]): Handle = {
    val t = InternalTypes.THREAD
    val box = BoxThread(thr)
    newHandle(t, box)
  }
Kunshan Wang's avatar
Kunshan Wang committed
566

567 568 569 570 571 572
  def putStack(sta: Option[InterpreterStack]): Handle = {
    val t = InternalTypes.STACK
    val box = BoxStack(sta)
    newHandle(t, box)
  }

573
}