Commit 5cc0978b authored by Kunshan Wang's avatar Kunshan Wang

fixed writeIDNameMap

parent 85e4a1bb
......@@ -10,7 +10,7 @@ import uvm.ssavariables._
import java.time.temporal.IsoFields.Unit
import uvm.ssavariables.NewStackAction
object UnnamedEntityUtils {
object EntityUtils {
def getName(obj: Identified): String = {
obj.name.getOrElse("@uvm.unnamed" + obj.id.toString())
}
......@@ -18,29 +18,22 @@ object UnnamedEntityUtils {
def getNames(objs: Seq[Identified]): String = {
objs.map(getName).mkString(" ")
}
}
class BundleSerializer(val bundle: GlobalBundle, val writeList: Option[Set[Int]]) {
import UnnamedEntityUtils._
import BundleSerializer._
def isMetaEntity(id: Int): Boolean = {
id < 65536
}
def filter(id: Int): Boolean = {
if (isMetaEntity(id)) {
false
} else {
writeList match {
case None => true
case Some(s) => s.contains(id)
}
}
def isMetaEntity(obj: Identified): Boolean = {
isMetaEntity(obj.id)
}
}
class BundleSerializer(val bundle: GlobalBundle, val whiteList: Set[TopLevel]) {
import EntityUtils._
import BundleSerializer._
def filter(obj: Identified): Boolean = {
filter(obj.id)
def filter(obj: TopLevel): Boolean = {
!isMetaEntity(obj) && whiteList.contains(obj)
}
def writeIDNameMap(output: Writer): Unit = {
......@@ -51,11 +44,31 @@ class BundleSerializer(val bundle: GlobalBundle, val writeList: Option[Set[Int]]
}
def writeIDNameMap(output: StringBuilder): Unit = {
for (obj <- bundle.allNs.all if filter(obj)) {
val name = getName(obj)
output.append("%s %d\n".format(name, obj.id))
for (obj <- whiteList if !isMetaEntity(obj)) {
writeIDNamePair(obj, output)
obj match {
case func: Function => {
bundle.funcToVers(func).headOption match {
case Some(fv) => {
fv.bbs foreach { bb =>
writeIDNamePair(bb, output)
bb.localVarNs.all foreach { lv => writeIDNamePair(lv, output) }
bb.insts foreach { inst => writeIDNamePair(inst, output) }
}
}
case None => // ignore
}
}
case _ => // ignore
}
}
}
private def writeIDNamePair(obj: Identified, output: StringBuilder): Unit = {
val id = obj.id
val name = getName(obj)
output.append("%d,%s\n".format(id, name))
}
def writeUIR(output: Writer): Unit = {
val sb = new StringBuilder()
......@@ -212,7 +225,7 @@ class BundleSerializer(val bundle: GlobalBundle, val writeList: Option[Set[Int]]
}
object BundleSerializer {
import UnnamedEntityUtils._
import EntityUtils._
def destToString(dest: DestClause): String = {
s"${dest.bb.n}(${dest.args.ns})"
......
......@@ -163,7 +163,7 @@ class MicroVM(vmConf: VMConf) {
*
*/
def debugPrintGlobalBundle(w: Writer): Unit = {
val bs = new BundleSerializer(globalBundle, None)
val bs = new BundleSerializer(globalBundle, globalBundle.allTopLevels().toSet)
bs.writeUIR(w)
}
......
......@@ -20,7 +20,7 @@ import uvm.refimpl.nat.NativeSupport
import uvm.refimpl.nat.PlatformConstants.Word
import uvm.types._
import uvm.utils.WithUtils.tryWithResource
import uvm.ir.textoutput.UnnamedEntityUtils
import uvm.ir.textoutput.EntityUtils
import uvm.ir.textoutput.BundleSerializer
class BootImageBuilder(implicit microVM: MicroVM) {
......@@ -91,6 +91,8 @@ object BootImageWriter {
class BootImageWriter(tcb: TransitiveClosureBuilder, outputFile: String)(implicit microVM: MicroVM) {
import BootImageWriter._
val bundleSerializer = new BundleSerializer(microVM.globalBundle, tcb.tls.set)
val tempDir = Files.createTempDirectory("mu-bootimg")
logger.info("Writing bootimg files to temp dir: %s".format(tempDir.toAbsolutePath().toString()))
......@@ -121,7 +123,6 @@ class BootImageWriter(tcb: TransitiveClosureBuilder, outputFile: String)(implici
writeAllocRecs(heapGroup)
writeIDNameMap()
writeUIRBundle()
}
......@@ -155,23 +156,20 @@ class BootImageWriter(tcb: TransitiveClosureBuilder, outputFile: String)(implici
private def writeAllocRecs(group: FileGroup): Unit = {
tryWithResource(Files.newBufferedWriter(group.allocFile, StandardCharsets.UTF_8)) { writer =>
for (ar <- group.allocRecs) {
writer.write("%d,%d,%d,%d,%s\n".format(ar.num, ar.fileOffset, ar.ty.id, ar.varLen, UnnamedEntityUtils.getName(ar.ty)))
writer.write("%d,%d,%d,%d,%s\n".format(ar.num, ar.fileOffset, ar.ty.id, ar.varLen, EntityUtils.getName(ar.ty)))
}
}
}
private def writeIDNameMap(): Unit = {
tryWithResource(Files.newBufferedWriter(idNameMapPath, StandardCharsets.UTF_8)) { writer =>
for (obj <- tcb.tls.set) {
writer.write("%d,%s\n".format(obj.id,UnnamedEntityUtils.getName(obj)))
}
bundleSerializer.writeIDNameMap(writer)
}
}
private def writeUIRBundle(): Unit = {
val bs = new BundleSerializer(microVM.globalBundle, Some(tcb.tls.set.map(_.id)))
tryWithResource(Files.newBufferedWriter(uirBundlePath, StandardCharsets.UTF_8)) { writer =>
bs.writeUIR(writer)
bundleSerializer.writeUIR(writer)
}
}
}
......
......@@ -44,7 +44,7 @@ object DumpLoadTimer extends App {
logger.info("Dumping loaded bundle...")
val sw = new StringWriter()
val bs = new BundleSerializer(gb, None)
val bs = new BundleSerializer(gb, gb.allTopLevels().toSet)
bs.writeUIR(sw)
val bTxt = sw.toString()
......
......@@ -39,7 +39,7 @@ class BundleSerializerSmokeTest extends UvmTestBase with TestingBundlesValidator
logger.info("Dumping loaded bundle...")
val sw = new StringWriter()
val bs = new BundleSerializer(gb, None)
val bs = new BundleSerializer(gb, gb.allTopLevels().toSet)
bs.writeUIR(sw)
val bTxt = sw.toString()
......
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