Commit 3dd5c9cd authored by Kunshan Wang's avatar Kunshan Wang

Some instructions.

parent 4fcad221
......@@ -482,7 +482,17 @@ class ClientAgent(microVM: MicroVM) {
}
def newStack(func: Handle, args: Seq[Handle]): Handle = {
throw new UvmRefImplException("Not Implemented")
val fv = func.vb.asInstanceOf[BoxFunc].func match {
case None => throw new UvmRuntimeException("Stack-bottom function must not be NULL")
case Some(v) => v
}
val argBoxes = args.map(_.vb)
val sta = microVM.threadStackManager.newStack(fv, argBoxes, mutator)
val nb = BoxStack(Some(sta))
newHandle(InternalTypes.STACK, nb)
}
def newThread(stack: Handle): Handle = {
......
......@@ -10,3 +10,13 @@ class UvmOutOfMemoryException(message: String = null, cause: Throwable = null) e
/** Thrown when an action not required by the specification and not implemented by this refimpl is performed. */
class UnimplementedOprationException(message: String = null, cause: Throwable = null) extends UvmRefImplException(message, cause)
/**
* Thrown when a dynamic error (errors that cannot be found at compile time) happens. This refimpl may sometimes throw
* exceptions on static errors rather than checking before running because the µVM has undefined behaviour on static
* errors. (It has undefined behaviour on dynamic errors, too.)
*/
class UvmRuntimeException(message: String = null, cause: Throwable = null) extends UvmRefImplException(message, cause)
/** Thrown when a division by zero is executed and the exception claues is not present. */
class UvmDivisionByZeroException(message: String = null, cause: Throwable = null) extends UvmRuntimeException(message, cause)
\ No newline at end of file
package uvm.refimpl.itpr
object OpHelper {
val ONE = BigInt(1)
def mask(n: Int): BigInt = {
(ONE << n) - ONE
}
def truncFromBigInt(n: BigInt, len: Int): BigInt = n & mask(len)
def zextToBigInt(n: BigInt, len: Int): BigInt = n & mask(len)
def sextToBigInt(n: BigInt, len: Int): BigInt = {
val bit = n.testBit(len - 1)
if (bit) {
n | (~mask(len - 1))
} else {
n & (mask(len - 1))
}
}
def prepareUnsigned(n: BigInt, len: Int): BigInt = truncFromBigInt(n, len)
def prepareSigned(n: BigInt, len: Int): BigInt = {
sextToBigInt(truncFromBigInt(n, len), len)
}
def unprepare(n: BigInt, len: Int): BigInt = truncFromBigInt(n, len)
def trunc(n: BigInt, toLen: Int): BigInt = truncFromBigInt(n, toLen)
def zext(n: BigInt, fromLen: Int, toLen: Int): BigInt = truncFromBigInt(n, fromLen)
def sext(n: BigInt, fromLen: Int, toLen: Int): BigInt = {
truncFromBigInt(sextToBigInt(n, fromLen), toLen)
}
def tr64IsInt(opnd: Long): Boolean = {
(opnd & 0x7ff0000000000001L) == 0x7ff0000000000001L
}
def tr64IsFp(opnd: Long): Boolean = {
(opnd & 0x7ff0000000000001L) != 0x7ff0000000000001L &&
(opnd & 0x7ff0000000000003L) != 0x7ff0000000000002L
}
def tr64IsRef(opnd: Long): Boolean = {
(opnd & 0x7ff0000000000003L) == 0x7ff0000000000002L
}
def intToTr64(opnd: Long): Long = {
(0x7ff0000000000001L | ((opnd & 0x7ffffffffffffL) << 1) |
((opnd & 0x8000000000000L) << 12))
}
def fpToTr64(opnd: Double): Long = {
var bits = java.lang.Double.doubleToRawLongBits(opnd)
if (java.lang.Double.isNaN(opnd)) {
bits = bits & 0xfff8000000000000L | 0x0000000000000008L
}
bits
}
def refToTr64(opnd: Long, tag: Long): Long = {
(0x7ff0000000000002L | (opnd & 0x7ffffffffff8L) | ((opnd & 0x800000000000L) << 16) |
((tag & 0x3eL) << 46) |
((tag & 0x1) << 2))
}
def tr64ToInt(opnd: Long): Long = {
(((opnd & 0xffffffffffffeL) >> 1) | ((opnd & 0x8000000000000000L) >> 12) & (1L << 51))
}
def tr64ToFp(opnd: Long): Double = java.lang.Double.longBitsToDouble(opnd)
def tr64ToRef(opnd: Long): Long = {
((opnd & 0x7ffffffffff8L) |
(((~(((opnd & 0x8000000000000000L) << 1) - 1)) >> 17) &
0xffff800000000000L))
}
def tr64ToTag(opnd: Long): Long = {
(((opnd & 0x000f800000000000L) >> 46) | ((opnd & 0x4) >> 2))
}
}
......@@ -48,7 +48,6 @@ class ThreadStackManager(microVM: MicroVM) {
val id = makeThreadID()
val thr = new InterpreterThread(id, microVM, stack, mutator)
threadRegistry.put(id, thr)
thr.start()
thr
}
......
......@@ -5,7 +5,9 @@ import uvm.types._
import uvm.refimpl._
import uvm.refimpl.mem.TypeSizes.Word
abstract class ValueBox
abstract class ValueBox {
def copyFrom(other: ValueBox): Unit
}
abstract class HasObjRef extends ValueBox {
def hasObjRef(): Boolean
......@@ -13,28 +15,60 @@ abstract class HasObjRef extends ValueBox {
def setObjRef(newObjRef: Word): Unit
}
abstract class ObjectBox[T](obj: Option[T]) extends ValueBox
abstract class ObjectBox[T] extends ValueBox {
def obj: Option[T]
def obj_=(o: Option[T]): Unit
def copyFrom(other: ValueBox): Unit = { this.obj = other.asInstanceOf[ObjectBox[T]].obj }
}
case class BoxInt(var value: BigInt) extends ValueBox
case class BoxFloat(var value: Float) extends ValueBox
case class BoxDouble(var value: Double) extends ValueBox
case class BoxVector(var values: Seq[ValueBox]) extends ValueBox
case class BoxInt(var value: BigInt) extends ValueBox {
def copyFrom(other: ValueBox): Unit = { this.value = other.asInstanceOf[BoxInt].value }
}
case class BoxFloat(var value: Float) extends ValueBox {
def copyFrom(other: ValueBox): Unit = { this.value = other.asInstanceOf[BoxFloat].value }
}
case class BoxDouble(var value: Double) extends ValueBox {
def copyFrom(other: ValueBox): Unit = { this.value = other.asInstanceOf[BoxDouble].value }
}
case class BoxVector(var values: Seq[ValueBox]) extends ValueBox {
def copyFrom(other: ValueBox): Unit = { for ((t, o) <- this.values.zip(other.asInstanceOf[BoxVector].values)) t.copyFrom(o) }
}
case class BoxRef(var objRef: Word) extends HasObjRef {
def copyFrom(other: ValueBox): Unit = { this.objRef = other.asInstanceOf[BoxRef].objRef }
def hasObjRef() = true
def getObjRef() = objRef
def setObjRef(newObjRef: Word): Unit = { objRef = newObjRef }
}
case class BoxIRef(var objRef: Word, var offset: Word) extends HasObjRef {
def copyFrom(other: ValueBox): Unit = {
val that = other.asInstanceOf[BoxIRef]
this.objRef = that.objRef; this.offset = that.offset
}
def hasObjRef() = objRef != 0
def getObjRef() = objRef
def setObjRef(newObjRef: Word): Unit = { objRef = newObjRef }
}
case class BoxStruct(var values: Seq[ValueBox]) extends ValueBox
case class BoxVoid() extends ValueBox
case class BoxFunc(var func: Option[Function]) extends ObjectBox[Function](func)
case class BoxThread(var thread: Option[InterpreterThread]) extends ObjectBox[InterpreterThread](thread)
case class BoxStack(var stack: Option[InterpreterStack]) extends ObjectBox[InterpreterStack](stack)
case class BoxStruct(var values: Seq[ValueBox]) extends ValueBox {
def copyFrom(other: ValueBox): Unit = { for ((t, o) <- this.values.zip(other.asInstanceOf[BoxStruct].values)) t.copyFrom(o) }
}
case class BoxVoid() extends ValueBox {
def copyFrom(other: ValueBox): Unit = {}
}
case class BoxFunc(var func: Option[Function]) extends ObjectBox[Function] {
def obj = func
def obj_=(other: Option[Function]): Unit = { func = other }
}
case class BoxThread(var thread: Option[InterpreterThread]) extends ObjectBox[InterpreterThread] {
def obj = thread
def obj_=(other: Option[InterpreterThread]): Unit = { thread = other }
}
case class BoxStack(var stack: Option[InterpreterStack]) extends ObjectBox[InterpreterStack] {
def obj = stack
def obj_=(other: Option[InterpreterStack]): Unit = { stack = other }
}
case class BoxTagRef64(var raw: Long) extends HasObjRef {
def copyFrom(other: ValueBox): Unit = { this.raw = other.asInstanceOf[BoxTagRef64].raw }
def hasObjRef() = OpHelper.tr64IsRef(raw)
def getObjRef() = OpHelper.tr64ToRef(raw)
def setObjRef(newObjRef: Word) = {
......
This diff is collapsed.
......@@ -21,7 +21,7 @@ class InterpreterStack(val id: Int, val stackMemory: StackMemory, stackBottomFun
var state: StackState = StackState.Ready(InternalTypes.VOID) // Initial state is READY<void>
var top: InterpreterFrame = new InterpreterFrame(stackBottomFunc, None) // Bottom frame
var top: InterpreterFrame = InterpreterFrame.frameForCall(stackBottomFunc, args, None)
def frames: Iterator[InterpreterFrame] = new AbstractIterator[InterpreterFrame] {
var curFrame: Option[InterpreterFrame] = Some(top)
......@@ -80,3 +80,15 @@ class InterpreterFrame(val funcVer: FuncVer, val prev: Option[InterpreterFrame])
}
}
}
object InterpreterFrame {
def frameForCall(funcVer: FuncVer, args: Seq[ValueBox], prev: Option[InterpreterFrame]): InterpreterFrame = {
val frm = new InterpreterFrame(funcVer, prev) // Bottom frame
for ((p, a) <- (funcVer.params zip args)) {
frm.boxes(p).copyFrom(a)
}
frm
}
}
\ No newline at end of file
This diff is collapsed.
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