Commit b39d6f9e authored by Kunshan Wang's avatar Kunshan Wang

Testing IR builder...

parent d7a76d9e
......@@ -13,6 +13,13 @@ class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNod
val idNodeMap = nodeList.map(n => (n.id, n)).toMap
def findNode[T <: IRBuilderNode](id: MuID): T = {
idNodeMap.getOrElse(id, {
val maybeName = idNameMap.get(id)
throw new NodeIDNotFoundException(id, maybeName)
}).asInstanceOf[T]
}
val bundle = new TrantientBundle()
def setIDAndMaybeName[T <: IdentifiedSettable](obj: T, id: MuID): T = {
......@@ -24,7 +31,8 @@ class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNod
def cascadeLookup[T <: Identified](id: MuID, ns1: Namespace[T], ns2: Namespace[T]): T = {
ns1.get(id).getOrElse {
ns2.get(id).getOrElse {
throw new UnknownIDException(id)
val maybeName = idNameMap.get(id)
throw new UnknownIDException(id, maybeName)
}
}
}
......@@ -51,7 +59,7 @@ class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNod
val t = resTy(id)
if (!(expectedType.isAssignableFrom(t.getClass))) {
throw new IRBuilderException("Type id:%d name:%s does not match expected type. Expected %s, actually %s.".format(
id, idNameMap.getOrElse(id, "(no name)"), n, t))
id, idNameMap.getOrElse(id, "(no name)"), n, t))
}
t.asInstanceOf[E]
}
......@@ -203,8 +211,10 @@ class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNod
}
def defineFunction(irNode: NodeFuncVer, muFuncVer: FuncVer): Unit = {
muFuncVer.bbs = new ArrayBuffer()
muFuncVer.bbNs = bundle.allNs.makeSubSpace("basic block")
val irBBmuBBs = irNode.bbs.map { bbID =>
val irBB = idNodeMap(bbID).asInstanceOf[NodeBB]
val irBB = findNode[NodeBB](bbID)
val muBB = new BasicBlock(muFuncVer)
setIDAndMaybeName(muBB, bbID)
muFuncVer.bbs += muBB
......@@ -213,6 +223,11 @@ class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNod
}
for ((irBB, muBB) <- irBBmuBBs) {
muBB.norParams = new ArrayBuffer()
muBB.excParam = None
muBB.insts = new ArrayBuffer()
muBB.localVarNs = bundle.allNs.makeSubSpace("local variable")
muBB.localInstNs = bundle.allNs.makeSubSpace("instruction")
for ((norParamID, norParamTyID) <- irBB.norParamIDs zip irBB.norParamTys) {
val muType = resTy(norParamTyID)
val muNorParam = NorParam(muType)
......@@ -229,7 +244,7 @@ class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNod
}
for (instID <- irBB.insts) {
val irInst = idNodeMap(instID).asInstanceOf[IRInstNode]
val irInst = findNode[IRInstNode](instID)
val muInst = defineInst(muFuncVer, muBB, irInst)
setIDAndMaybeName(muInst, instID)
muBB.insts += muInst
......@@ -250,7 +265,7 @@ class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNod
cascadeLookup(id, bundle.globalVarNs, globalBundle.globalVarNs)
}
}
implicit def resVars(ids: Seq[MuVarNode]): Seq[SSAVariable] = {
ids.map(resVar)
}
......@@ -258,8 +273,9 @@ class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNod
// used by keepalives
def resLocalVar(id: MuVarNode): LocalVariable = {
localVarNs.get(id).getOrElse {
throw new IRBuilderException("Local variable id:%d name:%s not found".format(id,
idNameMap.getOrElse(id, "(no name)")))
val vars = localVarNs.all.map(" " + _.repr).mkString("\n")
throw new IRBuilderException("Local variable id:%d name:%s not found.\nFYI: Current local variables are:\n%s".format(
id, idNameMap.getOrElse(id, "(no name)"), vars))
}
}
......@@ -270,37 +286,37 @@ class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNod
@inline
implicit def resDestClause(id: MuDestClause): DestClause = {
val NodeDestClause(_, dest, vars) = idNodeMap(id).asInstanceOf[NodeDestClause]
val NodeDestClause(_, dest, vars) = findNode[NodeDestClause](id)
DestClause(dest, vars.map(resVar))
}
@inline
implicit def resExcClause(maybeID: Option[MuExcClause]): Option[ExcClause] = maybeID map { id =>
val irExcClause = idNodeMap(id).asInstanceOf[NodeExcClause]
val irExcClause = findNode[NodeExcClause](id)
ExcClause(irExcClause.nor, irExcClause.exc)
}
@inline
implicit def resKeepalives(maybeID: Option[MuKeepaliveClause]): Seq[LocalVariable] = maybeID map { id =>
val irKeepaliveClause = idNodeMap(id).asInstanceOf[NodeKeepaliveClause]
val irKeepaliveClause = findNode[NodeKeepaliveClause](id)
irKeepaliveClause.vars.map(resLocalVar)
} getOrElse(Seq())
} getOrElse (Seq())
@inline
implicit def resCurStackClause(id: MuCurStackClause): CurStackAction = {
val irCurStackClause = idNodeMap(id).asInstanceOf[IRCurStackClauseNode]
val irCurStackClause = findNode[IRCurStackClauseNode](id)
irCurStackClause match {
case NodeCscRetWith(_, tys) => RetWith(tys)
case NodeCscKillOld(_) => KillOld()
case NodeCscKillOld(_) => KillOld()
}
}
@inline
implicit def resNewStackClause(id: MuNewStackClause): NewStackAction = {
val irNewStackClause = idNodeMap(id).asInstanceOf[IRNewStackClauseNode]
val irNewStackClause = findNode[IRNewStackClauseNode](id)
irNewStackClause match {
case NodeNscPassValues(_, tys, vars) => PassValues(tys, vars)
case NodeNscThrowExc(_, exc) => ThrowExc(exc)
case NodeNscThrowExc(_, exc) => ThrowExc(exc)
}
}
......@@ -321,7 +337,7 @@ class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNod
val caseDests = for ((c, d) <- cases zip dests) yield {
(resConst(c), resDestClause(d))
}
InstSwitch(opndTy, opnd, defaultDest, ArrayBuffer(caseDests:_*))
InstSwitch(opndTy, opnd, defaultDest, ArrayBuffer(caseDests: _*))
}
case NodeCall(id, resultIDs, sig, callee, args, excClause, keepaliveClause) =>
InstCall(sig, callee, args, excClause, keepaliveClause) --> resultIDs
......@@ -384,11 +400,12 @@ class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNod
case NodeCommInst(id, resultIDs, opcode, flags, tys, sigs, args, excClause, keepaliveClause) =>
InstCommInst(opcode, flags, tys, sigs.map(resSig), args, excClause, keepaliveClause) --> resultIDs
}
muInst.bb = muBB
muInst
} catch {
case e: Exception => {
throw new IRBuilderException("Exception thrown when building instruction. Func: %s, ver: %s, bb: %s, inst: %d:%s".format(
muFuncVer.func.repr, muFuncVer.repr, muBB.repr, irInst.id, idNameMap.getOrElse(irInst.id, "(no name)")), e)
muFuncVer.func.repr, muFuncVer.repr, muBB.repr, irInst.id, idNameMap.getOrElse(irInst.id, "(no name)")), e)
}
}
}
......@@ -397,7 +414,7 @@ private object BundleConstructorImplicitMagics {
implicit class AddResultsSeq[T <: Instruction](val inst: T) extends AnyVal {
@inline
def -->(resultIDs: Seq[MuID])(implicit self: BundleConstructor, localVarNs: Namespace[LocalVariable]): T = {
inst-->(resultIDs:_*)
inst --> (resultIDs: _*)
}
}
implicit class AddResultsVarArg[T <: Instruction](val inst: T) extends AnyVal {
......
......@@ -96,6 +96,10 @@ class IRBuilder(val id: MuInternalID,
id
}
// Convenient functions for Scala
def genSym(): MuID = genSym(None)
def genSym(name: String): MuID = genSym(Some(name))
// The following functions are generated from irBuilderNodes.scala by /migrate_scripts/irbuildernodestoirbuildermethods.py
// GEN:BEGIN:IRBUILDERNODE_CONSTRUCTORS
......
......@@ -2,6 +2,7 @@ package uvm.ir.irbuilder
import uvm.UvmException
import uvm.MuID
import uvm.MuName
/** Base class for exceptions thrown by the IR builder. */
class IRBuilderException(message: String = null, cause: Throwable = null) extends UvmException(message, cause)
......@@ -17,9 +18,20 @@ class IllegalNameException(message: String = null, cause: Throwable = null) exte
class IllegalSymbolException(message: String = null, cause: Throwable = null) extends IRBuilderException(message, cause)
/**
* Thrown if an ID cannot be resolved
* Thrown if an ID does not correspond to any IR nodes
*/
class UnknownIDException(id: MuID, cause: Throwable = null) extends {
private val msg = "ID %d not found".format(id)
class NodeIDNotFoundException(id: MuID, name: Option[MuName], cause: Throwable = null) extends {
private val msg = ("Node id:%d, name:%s not found. " +
"Chance is that you created this ID using gen_sym, but did not define the actual node.").format(
id, name.getOrElse("(no name)"))
} with IRBuilderException(msg, cause)
/**
* Thrown if a MuID cannot be resolved
*/
class UnknownIDException(id: MuID, name: Option[MuName], cause: Throwable = null) extends {
private val msg = ("MuID %d (name: %s) is not defined" +
"If your Mu IR bundle refers to previously loaded entities, make sure the IDs are correct. " +
"If it refers to entities in the current bundle, make sure they have the appropriate types.").format(
id, name.getOrElse("(no name)"))
} with IRBuilderException(msg, cause)
......@@ -50,7 +50,7 @@ case class InstResult(inst: Instruction, index: Int) extends LocalVariable
abstract class Instruction extends ChildNode {
var bb: BasicBlock = null
var results: IndexedSeq[InstResult] = null
var results: IndexedSeq[InstResult] = IndexedSeq()
override def toString = "[(%s) = %s %s]".format(this.results.map(_.repr).mkString(" "), this.repr, this.getClass.getSimpleName)
}
......
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