Commit 938280e1 authored by Kunshan Wang's avatar Kunshan Wang

WIP: refactoring

parent fc3528e0
package uvm
trait Identified {
def id: Int
def name: Option[String]
import Identified._
def id: MuID
def name: Option[MuName]
def repr: String = "[%d:%s]".format(id, name.getOrElse("_"))
......@@ -19,20 +20,23 @@ object Identified {
val INVALID_ID = 0
val FIRST_INTERNAL_ID = 32768
val FIRST_CLIENT_USABLE_ID = 65536
type MuID = Int
type MuName = String
}
trait IdentifiedSettable extends Identified {
var id: Int = 0
var name: Option[String] = None
var id: MuID = 0
var name: Option[MuName] = None
}
object RichIdentifiedSettable {
implicit class RichIdentifiedSettable[T <: IdentifiedSettable](val is: T) extends AnyVal {
def :=(p: Int): T = {
def :=(p: MuID): T = {
is.id = p
is
}
def :=(p: (Int, String)): T = {
def :=(p: (MuID, MuName)): T = {
is.id = p._1
is.name = Some(p._2)
is
......
......@@ -2,11 +2,11 @@ package uvm.comminsts
import uvm._
case class CommInst(val id: Int, val name: Option[String], val isTerminator: Boolean) extends Identified
case class CommInst(val id: MuID, val name: Option[MuName], val isTerminator: Boolean) extends Identified
object CommInsts extends SimpleNamespace[CommInst] {
private def commInst(id: Int, name: String, isTerminator: Boolean = false) {
private def commInst(id: MuID, name: MuName, isTerminator: Boolean = false) {
val ci = CommInst(id, Some(name), isTerminator)
add(ci)
}
......
......@@ -9,11 +9,7 @@ import uvm.types._
import uvm.utils.IDFactory
import uvm.staticanalysis.InstructionResultInferer
object DestKind extends Enumeration {
val NORMAL, EXCEPT, TRUE, FALSE, DEFAULT, DISABLED, ENABLED = Value
}
private[irbuilder] object IRBuilder {
object IRBuilder {
val validChars = {
val table = new Array[Boolean](256)
......@@ -52,6 +48,8 @@ private[irbuilder] object IRBuilder {
sym
}
type MuTypeNode = MuID
}
class IRBuilder(globalBundle: GlobalBundle, idFactory: IDFactory) {
......
......@@ -18,7 +18,7 @@ object EntityUtils {
objs.map(getName).mkString(" ")
}
def isMetaEntity(id: Int): Boolean = {
def isMetaEntity(id: MuID): Boolean = {
id < 65536
}
......
package uvm
abstract class Namespace[T <: Identified] {
def apply(id: Int): T
def apply(name: String): T
def apply(id: MuID): T
def apply(name: MuName): T
def get(id: Int): Option[T]
def get(name: String): Option[T]
def get(id: MuID): Option[T]
def get(name: MuName): Option[T]
def add(obj: T): Unit
......@@ -20,24 +20,24 @@ abstract class Namespace[T <: Identified] {
class SimpleNamespace[T <: Identified](val kind: String = SimpleNamespace.DEFAULT_KIND) extends Namespace[T] {
private type MapType[K, V] = collection.mutable.HashMap[K, V]
private val idMap = new MapType[Int, T]()
private val nameMap = new MapType[String, T]()
private val idMap = new MapType[MuID, T]()
private val nameMap = new MapType[MuName, T]()
@throws(classOf[NoSuchElementException])
def apply(id: Int): T = try {
def apply(id: MuID): T = try {
idMap(id)
} catch {
case e: NoSuchElementException => throw new NoSuchElementException("No %s has ID %d".format(kind, id))
}
@throws(classOf[NoSuchElementException])
def apply(name: String): T = try {
def apply(name: MuName): T = try {
nameMap(name)
} catch {
case e: NoSuchElementException => throw new NoSuchElementException("No %s has name '%s'".format(kind, name))
}
def get(id: Int): Option[T] = idMap.get(id)
def get(name: String): Option[T] = nameMap.get(name)
def get(id: MuID): Option[T] = idMap.get(id)
def get(name: MuName): Option[T] = nameMap.get(name)
def add(obj: T) {
for (obj2 <- get(obj.id)) {
......
package object uvm {
type MuID = Identified.MuID
type MuName = Identified.MuName
}
\ No newline at end of file
......@@ -135,12 +135,12 @@ class MicroVM(val vmConf: VMConf) {
/**
* Given a name, get the ID of an identified entity.
*/
def idOf(name: String): Int = globalBundle.allNs(name).id
def idOf(name: MuName): MuID = globalBundle.allNs(name).id
/**
* Given an ID, get the name of an identified entity.
*/
def nameOf(id: Int): String = globalBundle.allNs(id).name.get
def nameOf(id: MuID): MuName = globalBundle.allNs(id).name.get
/**
* Set the trap handler.
......@@ -152,7 +152,7 @@ class MicroVM(val vmConf: VMConf) {
/**
* Make boot image.
*/
def makeBootImage(whiteList: Seq[Int], outputFile: String): Unit = {
def makeBootImage(whiteList: Seq[MuID], outputFile: String): Unit = {
val whiteListObjs = whiteList.map { id =>
val obj = globalBundle.allNs(id)
obj match {
......
......@@ -19,6 +19,7 @@ import uvm.ssavariables.AtomicRMWOptr._
import uvm.ssavariables.HasKeepaliveClause
import uvm.ssavariables.MemoryOrder._
import uvm.types._
import uvm.ir.irbuilder.IRBuilder
object MuCtx {
val logger = Logger(LoggerFactory.getLogger(getClass.getName))
......@@ -32,7 +33,7 @@ object MuCtx {
*/
class MuCtx(val ctxID: Int, _mutator: Mutator)(
implicit protected val microVM: MicroVM, memorySupport: MemorySupport)
extends ObjectPinner with MuCtxIRBuilderPart with AutoCloseable {
extends ObjectPinner with AutoCloseable {
import MuCtx._
implicit def mutator = _mutator
......@@ -42,10 +43,10 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
val pinSet = new ArrayBuffer[Word]
/** Given a name, get the ID of an identified entity. */
def idOf(name: String): Int = microVM.idOf(name)
def idOf(name: MuName): MuID = microVM.idOf(name)
/** Given an ID, get the name of an identified entity. */
def nameOf(id: Int): String = microVM.nameOf(id)
def nameOf(id: MuID): MuName = microVM.nameOf(id)
/** Close the context. */
def closeContext(): Unit = {
......@@ -102,13 +103,13 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
}
/** Convert pointer (Long) to a handle. */
def handleFromPtr(muType: Int, v: Word): MuUPtrValue = {
def handleFromPtr(muType: MuID, v: Word): MuUPtrValue = {
val t = microVM.globalBundle.typeNs(muType).asInstanceOf[TypeUPtr]
addHandle(MuUPtrValue(t, BoxPointer(v)))
}
/** Convert function pointer (Long) to a handle. */
def handleFromFP(muType: Int, v: Word): MuUFPValue = {
def handleFromFP(muType: MuID, v: Word): MuUFPValue = {
val t = microVM.globalBundle.typeNs(muType).asInstanceOf[TypeUFuncPtr]
addHandle(MuUFPValue(t, BoxPointer(v)))
}
......@@ -147,7 +148,7 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
}
/** Make a handle for a constant. */
def handleFromConst(id: Int): MuValue = {
def handleFromConst(id: MuID): MuValue = {
val c = microVM.globalBundle.constantNs(id)
val t = c.constTy
val box = microVM.constantPool.getGlobalVarBox(c)
......@@ -155,7 +156,7 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
}
/** Make a handle for a global cell (its iref). */
def handleFromGlobal(id: Int): MuIRefValue = {
def handleFromGlobal(id: MuID): MuIRefValue = {
val g = microVM.globalBundle.globalCellNs(id)
val t = InternalTypePool.irefOf(g.cellTy)
val a = microVM.memoryManager.globalMemory.addrForGlobalCell(g)
......@@ -164,7 +165,7 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
}
/** Make a handle for a function (funcref). */
def handleFromFunc(id: Int): MuFuncRefValue = {
def handleFromFunc(id: MuID): MuFuncRefValue = {
val f = microVM.globalBundle.funcNs(id)
val t = InternalTypePool.funcOf(f.sig)
val box = BoxFunc(Some(f))
......@@ -175,7 +176,7 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
* Make a handle for an exposed function. In this implementation, the type is ufuncptr, but other implementations
* may be different.
*/
def handleFromExpose(id: Int): MuUFPValue = {
def handleFromExpose(id: MuID): MuUFPValue = {
val ef = microVM.globalBundle.expFuncNs(id)
val t = InternalTypePool.funcPtrOf(ef.func.sig)
val box = BoxPointer(microVM.nativeCallHelper.getStaticExpFuncAddr(ef))
......@@ -249,7 +250,7 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
}
/** Allocate a fixed object in the heap. */
def newFixed(muType: Int): MuRefValue = {
def newFixed(muType: MuID): MuRefValue = {
val t = microVM.globalBundle.typeNs(muType)
val objRef = mutator.newScalar(t)
val b = BoxRef(objRef)
......@@ -258,7 +259,7 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
}
/** Allocate a hybrid object in the heap. */
def newHybrid(tid: Int, length: MuIntValue): MuRefValue = {
def newHybrid(tid: MuID, length: MuIntValue): MuRefValue = {
val t = microVM.globalBundle.typeNs(tid).asInstanceOf[TypeHybrid]
val len = handleToUInt(length).longValue
val objRef = mutator.newHybrid(t, len)
......@@ -268,7 +269,7 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
}
/** Cast between two refs, two irefs or two funcrefs */
def refcast[T <: MuGenRefValue](opnd: T, newType: Int): T = {
def refcast[T <: MuGenRefValue](opnd: T, newType: MuID): T = {
val nt = microVM.globalBundle.typeNs(newType)
val nh = (opnd, nt) match {
......@@ -506,7 +507,7 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
}
/** Get the ID of the current function of a frame. Return 0 for native frames. */
def curFunc(cursor: MuFCRefValue): Int = {
def curFunc(cursor: MuFCRefValue): MuID = {
val c = getCursorNotNull(cursor)
c.frame.curFuncID
}
......@@ -515,7 +516,7 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
* Get the ID of the current function version of a frame. Return 0 for native frames
* or Mu frames of undefined functions
*/
def curFuncVer(cursor: MuFCRefValue): Int = {
def curFuncVer(cursor: MuFCRefValue): MuID = {
val c = getCursorNotNull(cursor)
c.frame.curFuncVerID
}
......@@ -524,7 +525,7 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
* Get the ID of the current instruction of a frame. Return 0 for native frames, Mu frames for undefined
* functions, or if the frame is just created by newStack or pushFrame.
*/
def curInst(cursor: MuFCRefValue): Int = {
def curInst(cursor: MuFCRefValue): MuID = {
val c = getCursorNotNull(cursor)
c.frame.curInstID
}
......@@ -646,11 +647,11 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
addHandle(MuTagRef64Value(InternalTypes.TAGREF64, box))
}
def enableWatchPoint(wpID: Int): Unit = {
def enableWatchPoint(wpID: MuID): Unit = {
microVM.trapManager.enableWatchPoint(wpID)
}
def disableWatchPoint(wpID: Int): Unit = {
def disableWatchPoint(wpID: MuID): Unit = {
microVM.trapManager.disableWatchPoint(wpID)
}
......@@ -695,6 +696,10 @@ class MuCtx(val ctxID: Int, _mutator: Mutator)(
val a = addr.vb.addr
microVM.nativeCallHelper.unexposeFunc(a)
}
def newIRBuilder(): IRBuilder = {
new IRBuilder(microVM.globalBundle, microVM.idFactory)
}
// Internal methods for the micro VM
......
This diff is collapsed.
......@@ -78,34 +78,6 @@ case class MuUPtrValue(ty: TypeUPtr, vb: BoxPointer) extends MuValue
case class MuUFPValue(ty: TypeUFuncPtr, vb: BoxPointer) extends MuValue
case class MuFCRefValue(ty: TypeFrameCursorRef, vb: BoxFrameCursor) extends MuSpecialEntityRefValue[FrameCursor]
case class MuIRNode[+T <: IRNode](ty: TypeIRNodeRef, vb: BoxIRNode) extends MuSpecialEntityRefValue[T] {
// The underlying BoxIRNode is weakly typed. Conversion needed
def maybeNode: Option[T] = vb.obj.asInstanceOf[Option[T]]
def node: T = vb.obj.get.asInstanceOf[T]
}
object MuIRNodeAliases {
type MuBundleNode = MuIRNode[TrantientBundle]
type MuChildNode = MuIRNode[ChildNode]
type MuVarNode = MuIRNode[SSAVariable]
type MuGlobalVarNode = MuIRNode[GlobalVariable]
type MuLocalVarNode = MuIRNode[LocalVariable]
type MuTypeNode = MuIRNode[Type]
type MuFuncSigNode = MuIRNode[FuncSig]
type MuConstNode = MuIRNode[Constant]
type MuGlobalNode = MuIRNode [GlobalCell]
type MuFuncNode = MuIRNode[Function]
type MuExpFuncNode = MuIRNode[ExposedFunc]
type MuFuncVerNode = MuIRNode[FuncVer]
type MuBBNode = MuIRNode[BasicBlock]
type MuNorParamNode = MuIRNode[NorParam]
type MuExcParamNode = MuIRNode[ExcParam]
type MuInstResNode = MuIRNode[InstResult]
type MuInstNode = MuIRNode[Instruction]
}
abstract class TrapHandlerResult
object TrapHandlerResult {
case class ThreadExit() extends TrapHandlerResult
......@@ -119,9 +91,9 @@ object HowToResume {
}
trait TrapHandler {
def handleTrap(ctx: MuCtx, thread: MuThreadRefValue, stack: MuStackRefValue, watchPointID: Int): TrapHandlerResult
def handleTrap(ctx: MuCtx, thread: MuThreadRefValue, stack: MuStackRefValue, watchPointID: MuID): TrapHandlerResult
}
trait UndefinedFunctionHandler {
def handleUndefinedFunction(functionID: Int): Unit
def handleUndefinedFunction(functionID: MuID): Unit
}
......@@ -12,7 +12,7 @@ object InternalTypes {
val internalIDFactory = IDFactory.newInternalIDFactory() // IDs from 32768-65535 are for implementation internal use.
def internal(name: String): (Int, String) = {
def internal(name: MuName): (MuID, MuName) = {
val id = internalIDFactory.getID()
val n = "@uvm.internal.types." + name
(id, n)
......
......@@ -13,7 +13,6 @@ import uvm.ir.irbuilder.DestKind
import uvm.refimpl.MicroVM
object CDefs {
import MuIRNodeAliases._
import CDefsHelperFunctions._
import NativeClientSupport._
......
......@@ -23,7 +23,7 @@ import com.kenai.jffi.CallContext
import com.kenai.jffi.HeapInvocationBuffer
import NativeClientSupport._
import uvm.ir.irbuilder.DestKind
import uvm.ir.irbuilder.IRBuilder
object NativeTrapHandler {
val jffiInvoker = Invoker.getInstance
......@@ -245,6 +245,7 @@ object NativeClientSupport {
// Give exposed objects a random "memory address" so native programs can pass them back to Mu as parameters.
val microVMs = jnrRuntime.newObjectReferenceManager[MicroVM]()
val muCtxs = jnrRuntime.newObjectReferenceManager[MuCtx]()
val muIRBuilders = jnrRuntime.newObjectReferenceManager[IRBuilder]()
val muValues = jnrRuntime.newObjectReferenceManager[MuValue]()
/** Map each MuCtx to all of its current MuValues. This is needed when closing a MuCtx. */
......@@ -299,6 +300,8 @@ object NativeClientSupport {
val muVMExposer = new ClientAccessibleClassExposer(CDefs.stubsOfMuVM)
logger.debug("Creating Mu Ctx Exposer...")
val muCtxExposer = new ClientAccessibleClassExposer(CDefs.stubsOfMuCtx)
logger.debug("Creating Mu IRBuilder Exposer...")
val muIRBuilderExposer = new ClientAccessibleClassExposer(CDefs.stubsOfMuIRBuilder)
// Expose and unexpose objects
......
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