Commit 4d4e646a authored by Adam R. Nelson's avatar Adam R. Nelson

Made const naming more flexible

Same updates as for type naming; also changed "const" to "constDef" to prevent reserved name collision in Java.
parent deef38d9
...@@ -6,21 +6,22 @@ import scala.collection.{JavaConversions, mutable} ...@@ -6,21 +6,22 @@ import scala.collection.{JavaConversions, mutable}
import scala.collection.JavaConversions._ import scala.collection.JavaConversions._
class BundleBuilder(baseName: String) { class BundleBuilder(baseName: String) {
val typeDefs = mutable.AnyRefMap.empty[TypeName, TypeCtor] protected val typeDefs = mutable.AnyRefMap.empty[TypeName, TypeCtor]
val funcSigDefs = mutable.AnyRefMap.empty[FuncSigName, FuncSig] protected val funcSigDefs = mutable.AnyRefMap.empty[FuncSigName, FuncSig]
val constDefs = mutable.AnyRefMap.empty[GlobalVarName, Const] protected val constDefs = mutable.AnyRefMap.empty[GlobalVarName, Const]
val globalCellDefs = mutable.AnyRefMap.empty[GlobalVarName, TypeName] protected val globalCellDefs = mutable.AnyRefMap.empty[GlobalVarName, TypeName]
val funcDecls = mutable.AnyRefMap.empty[GlobalVarName, FuncSigName] protected val funcDecls = mutable.AnyRefMap.empty[GlobalVarName, FuncSigName]
val funcVers = mutable.AnyRefMap.empty[FuncVerName, FunctionBuilder] protected val funcVers = mutable.AnyRefMap.empty[FuncVerName, FunctionBuilder]
val funcExpDefs = mutable.AnyRefMap.empty[GlobalVarName, Expose] protected val funcExpDefs = mutable.AnyRefMap.empty[GlobalVarName, Expose]
val comments = mutable.AnyRefMap.empty[GlobalName, String] protected val comments = mutable.AnyRefMap.empty[GlobalName, String]
val usedNames = mutable.Set.empty[String] protected val usedNames = mutable.Set.empty[String]
private var nextTypeNumber = 0 private var nextTypeNumber = 0
private var nextSigNumber = 0 private var nextSigNumber = 0
private var nextConstNumber = 0 private var nextConstNumber = 0
private var nextGlobalCellNumber = 0 private var nextGlobalCellNumber = 0
private var typePrefix = baseName private var typePrefix = baseName
private var constPrefix = baseName
// Name generation // Name generation
// ------------------------------------------------------------ // ------------------------------------------------------------
...@@ -41,7 +42,9 @@ class BundleBuilder(baseName: String) { ...@@ -41,7 +42,9 @@ class BundleBuilder(baseName: String) {
newVarName(nextName) newVarName(nextName)
} }
def newVarName(name: String) = GlobalVarName(generateName(baseName, name)) def newVarName(name: String) = newVarName(name, baseName)
def newVarName(name: String, prefix: String) = GlobalVarName(generateName(prefix, name))
def newTypeName(): TypeName = { def newTypeName(): TypeName = {
var nextName: String = null var nextName: String = null
...@@ -75,6 +78,8 @@ class BundleBuilder(baseName: String) { ...@@ -75,6 +78,8 @@ class BundleBuilder(baseName: String) {
def setTypePrefix(newPrefix: String): Unit = typePrefix = newPrefix def setTypePrefix(newPrefix: String): Unit = typePrefix = newPrefix
def setConstPrefix(newPrefix: String): Unit = constPrefix = newPrefix
// Code generation // Code generation
// ------------------------------------------------------------ // ------------------------------------------------------------
...@@ -119,17 +124,17 @@ class BundleBuilder(baseName: String) { ...@@ -119,17 +124,17 @@ class BundleBuilder(baseName: String) {
def funcSig(name: FuncSigName, ret: TypeName, args: util.List[TypeName]): FuncSigName = def funcSig(name: FuncSigName, ret: TypeName, args: util.List[TypeName]): FuncSigName =
funcSig(name, ret, JavaConversions.asScalaBuffer(args)) funcSig(name, ret, JavaConversions.asScalaBuffer(args))
def const(c: Const): GlobalVarName = def constDef(c: Const): GlobalVarName =
constDefs collectFirst { case (name, existing) if c == existing => name } getOrElse { constDefs collectFirst { case (name, existing) if c == existing => name } getOrElse {
var nextName: String = null var nextName: String = null
do { do {
nextName = "const" + nextConstNumber nextName = "const" + nextConstNumber
nextConstNumber += 1 nextConstNumber += 1
} while (usedNames contains nextName) } while (usedNames contains nextName)
const(newVarName(nextName), c) constDef(newVarName(nextName, constPrefix), c)
} }
def const(name: GlobalVarName, c: Const): GlobalVarName = { def constDef(name: GlobalVarName, c: Const): GlobalVarName = {
constDefs.put(name, c) constDefs.put(name, c)
name name
} }
...@@ -242,6 +247,7 @@ trait FunctionBuilder { ...@@ -242,6 +247,7 @@ trait FunctionBuilder {
def newLabelName(): LabelName def newLabelName(): LabelName
def newLabelName(name: String): LabelName def newLabelName(name: String): LabelName
def typeDef(ctor: TypeCtor): TypeName def typeDef(ctor: TypeCtor): TypeName
def constDef(c: Const): VarName
def currentBlockLabel: LabelName def currentBlockLabel: LabelName
def startNewBlock(): LabelName def startNewBlock(): LabelName
def startNewBlock(label: LabelName): LabelName def startNewBlock(label: LabelName): LabelName
...@@ -260,8 +266,8 @@ private[text] class FunctionBuilderImpl( ...@@ -260,8 +266,8 @@ private[text] class FunctionBuilderImpl(
type Block = (LabelName, mutable.ArrayBuffer[NameableInst]) type Block = (LabelName, mutable.ArrayBuffer[NameableInst])
val blocks = mutable.ArrayBuffer.empty[Block] protected val blocks = mutable.ArrayBuffer.empty[Block]
val usedNames = mutable.Set.empty[String] protected val usedNames = mutable.Set.empty[String]
private var nextVarNumber = 0 private var nextVarNumber = 0
private var nextLabelNumber = 0 private var nextLabelNumber = 0
private var currentBlock: Block = createBlock(newLabelName("entry")) private var currentBlock: Block = createBlock(newLabelName("entry"))
...@@ -301,6 +307,8 @@ private[text] class FunctionBuilderImpl( ...@@ -301,6 +307,8 @@ private[text] class FunctionBuilderImpl(
override def typeDef(ctor: TypeCtor): TypeName = bundleBuilder.typeDef(ctor) override def typeDef(ctor: TypeCtor): TypeName = bundleBuilder.typeDef(ctor)
override def constDef(c: Const): VarName = bundleBuilder.constDef(c)
private def createBlock(name: LabelName): Block = { private def createBlock(name: LabelName): Block = {
val newBlock = (name, mutable.ArrayBuffer.empty[NameableInst]) val newBlock = (name, mutable.ArrayBuffer.empty[NameableInst])
blocks add newBlock blocks add newBlock
......
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