Commit 7a1a6da7 authored by Kunshan Wang's avatar Kunshan Wang

IRReader now passes names to IDFactory

parent 1aaa1356
......@@ -22,7 +22,7 @@ import com.typesafe.scalalogging.Logger
import uvm.ir.textinput.gen.UIRParser.StringLiteralContext
import uvm.ir.textinput.gen.UIRParser.StringLiteralContext
class UIRTextReader(val idFactory: IDFactory, val recordSourceInfo: Boolean = true) {
class UIRTextReader(val defaultIDFactory: IDFactory, val recordSourceInfo: Boolean = true) {
import UIRTextReader._
def read(ir: java.io.Reader, globalBundle: GlobalBundle): TrantientBundle = {
......@@ -60,6 +60,10 @@ class UIRTextReader(val idFactory: IDFactory, val recordSourceInfo: Boolean = tr
}
def read(source: String, ais: ANTLRInputStream, globalBundle: GlobalBundle): TrantientBundle = {
read(source, ais, globalBundle, defaultIDFactory)
}
def read(source: String, ais: ANTLRInputStream, globalBundle: GlobalBundle, idFactory: IDFactory): TrantientBundle = {
val ast = parse(source, ais)
logger.debug("Antlr parsed. Reading UIR AST into a Bundle...")
val instanceReader = new InstanceUIRTextReader(idFactory, source, ast, globalBundle, recordSourceInfo)
......@@ -207,15 +211,17 @@ private[textinput] class InstanceUIRTextReader(idFactory: IDFactory, source: Str
ir.topLevelDef.map(_.getChild(0)).foreach {
case td: TypeDefContext => {
val ty = mkType(td.typeConstructor)
ty.id = idFactory.getID()
ty.name = Some(td.nam)
addTy(ty, toSourceInfo(td.nam))
val name = td.nam
ty.id = idFactory.getID(name)
ty.name = Some(name)
addTy(ty, toSourceInfo(name))
}
case fsd: FuncSigDefContext => {
val sig = mkSig(fsd.funcSigConstructor)
sig.id = idFactory.getID()
sig.name = Some(fsd.nam)
addSig(sig, toSourceInfo(fsd.nam))
val name = fsd.nam
sig.id = idFactory.getID(name)
sig.name = Some(name)
addSig(sig, toSourceInfo(name))
}
case _ =>
}
......@@ -263,7 +269,7 @@ private[textinput] class InstanceUIRTextReader(idFactory: IDFactory, source: Str
def declFunc(n: String, s: FuncSigContext, sourceInfo: SourceInfo): Function = {
val sig = resSig(s)
val func = mkFunc(sig)
func.id = idFactory.getID()
func.id = idFactory.getID(n)
func.name = Some(n)
addFunc(func, sourceInfo)
......@@ -277,16 +283,18 @@ private[textinput] class InstanceUIRTextReader(idFactory: IDFactory, source: Str
case cdctx: ConstDefContext => {
val ty = resTy(cdctx.ty)
val con = mkConst(ty, cdctx.constConstructor)
con.id = idFactory.getID()
con.name = Some(cdctx.nam)
addConst(con, toSourceInfo(cdctx.nam))
val name = cdctx.nam
con.id = idFactory.getID(name)
con.name = Some(name)
addConst(con, toSourceInfo(name))
}
case gdctx: GlobalDefContext => {
val ty = resTy(gdctx.ty)
val gc = mkGlobalCell(ty)
gc.id = idFactory.getID()
gc.name = Some(gdctx.nam)
addGlobalCell(gc, toSourceInfo(gdctx.nam))
val name = gdctx.nam
gc.id = idFactory.getID(name)
gc.name = Some(name)
addGlobalCell(gc, toSourceInfo(name))
}
case fdecl: FuncDeclContext => {
val name = globalNameToString(fdecl.nam)
......@@ -303,9 +311,10 @@ private[textinput] class InstanceUIRTextReader(idFactory: IDFactory, source: Str
}
case edef: FuncExpDefContext => {
val efun = mkExpo(edef)
efun.id = idFactory.getID()
efun.name = Some(edef.nam)
addExpFunc(efun, toSourceInfo(edef.nam))
val name = edef.nam
efun.id = idFactory.getID(name)
efun.name = Some(name)
addExpFunc(efun, toSourceInfo(name))
}
case _ => {}
}
......@@ -318,7 +327,7 @@ private[textinput] class InstanceUIRTextReader(idFactory: IDFactory, source: Str
logger.trace("Visiting function %s".format(func.name))
val ver = new FuncVer(func)
val verName = globalize(fDefCtx.ver, func.name.get)
ver.id = idFactory.getID()
ver.id = idFactory.getID(verName)
ver.name = Some(verName)
addFuncVer(ver, toSourceInfo(fDefCtx.ver))
......@@ -344,7 +353,7 @@ private[textinput] class InstanceUIRTextReader(idFactory: IDFactory, source: Str
val label = bbCtx.label()
val bbName = globalize(label.name(), verName)
val bb = new BasicBlock(ver)
bb.id = idFactory.getID()
bb.id = idFactory.getID(bbName)
bb.name = Some(bbName)
addBB(bb, toSourceInfo(bbCtx))
......@@ -355,14 +364,14 @@ private[textinput] class InstanceUIRTextReader(idFactory: IDFactory, source: Str
def mkNorParam(ty: Type, name: String): NorParam = {
val param = NorParam(ty)
param.id = idFactory.getID()
param.id = idFactory.getID(name)
param.name = Some(globalize(name, bbName))
param
}
def mkExcParam(name: String): ExcParam = {
val param = ExcParam()
param.id = idFactory.getID()
param.id = idFactory.getID(name)
param.name = Some(globalize(name, bbName))
param
}
......@@ -608,8 +617,13 @@ private[textinput] class InstanceUIRTextReader(idFactory: IDFactory, source: Str
}
}
inst.id = idFactory.getID()
inst.name = Option(instDef.name).map(n => globalize(n.getText, bbName))
val maybeInstName = Option(instDef.name).map(n => globalize(n.getText, bbName))
// When reading from regular text IR, instructions may or may not have names, but the IDFactory at this time
// is SequentialIDFactory. It ignores the name. But when reading from the boot image, everything must be
// named and must have a corresponding ID. The "poison" name below will let MappedIDFactory throw
// NoSuchElementException.
inst.id = idFactory.getID(maybeInstName.getOrElse("@uvm.this_instruction_does_not_have_a_name"))
inst.name = maybeInstName
addInst(inst, toSourceInfo(instDef))
......@@ -619,7 +633,7 @@ private[textinput] class InstanceUIRTextReader(idFactory: IDFactory, source: Str
val resName = globalize(instResDef.getText, bbName)
val instRes = InstResult(inst, index)
instRes.id = idFactory.getID()
instRes.id = idFactory.getID(resName)
instRes.name = Some(resName)
addLocalVar(instRes, toSourceInfo(instResDef))
......
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