GitLab will be upgraded to the 12.10.14-ce.0 on 28 Sept 2020 at 2.00pm (AEDT) to 2.30pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

Commit cca8f8f8 authored by Kunshan Wang's avatar Kunshan Wang

WIP: Text IR builder for client.

parent c82ab820
package uvm.clientsupport.text
import java.util._
import scala.beans._
import DefaultTypes._
/**
* NameFactory creates non-duplicated names.
* <p>
* This tool uses the scheme @prefix_num or @prefix_num_hint. In this way, as long as two NameFactory instances
* have two different prefixes and one is not a prefix of the other, they are guaranteed to give a unique name
* each time nextName is called. The "hint" argument provides a human-readable suffix to a name.
* <p>
* The global name prefix "@" is assumed. It is not recommended to use local names in generated code, since they
* strictly aliases of @funcvername.localname.
* <p>
* Example:
* <pre>
* val instNF = new NameFactory("@inst")
* val name1 = instNF.nextName() // @inst_0000
* val name2 = instNf.nextName("add") // @inst_0001_add
* </pre>
*/
class NameFactory(prefix: String) {
private var num = 0
def nextName(hint: String = null): MuName = {
val myNum = num
num += 1
if (hint == null) {
"@%s_%04d".format(prefix, myNum)
} else {
"@%s_%04d_%s".format(prefix, myNum, hint)
}
}
}
/**
* BundleBuilder helps the client build a Mu bundle.
* A helper class for building function definitions.
* <p>
* An entry block is
*/
class BundleBuilder {
class FuncDefBuilder(name: MuName, version: MuName, sig: MuName) {
@BeanProperty
val funcDef = new FuncDef()
funcDef.name = name
funcDef.version = version
funcDef.sig = sig
// Automatic name generation
private val paramNamer = new NameFactory(version + ".param")
private val bbNamer = new NameFactory(version + ".bb")
private val instNamer = new NameFactory(version + ".inst")
/**
* Add a parameter.
*
* @param hint A human-readable suffix of the parameter, or null if not needed.
* @return The generated name of the parameter.
*/
def addParam(hint: String = null): MuName = {
val paramName = paramNamer.nextName(hint)
funcDef.params.add(paramName)
paramName
}
/** The entry block. */
@BeanProperty
val entry = new BasicBlock()
entry.name = "entry"
@BeanProperty
var curBB = entry
/**
* Create a new basic block and add to the funcDef.
* @param hint: A human-readable suffix of the basic block name, or null if not needed.
* @return The basic block.
*/
def newBB(hint: String = null): BasicBlock = {
val bb = new BasicBlock()
bb.name = bbNamer.nextName(hint)
funcDef.bbs.add(bb)
bb
}
/**
* Add the instruction into the current basic block.
* @param inst: The instruction to add
*/
def emit(inst: Instruction) {
curBB.insts.add(inst)
}
}
\ No newline at end of file
......@@ -130,10 +130,39 @@ class FuncSigDef() extends TopLevelDefinition {
}
/** A constant. ".const" */
abstract class ConstDef() extends TopLevelDefinition {
abstract class ConstDef extends TopLevelDefinition {
@BeanProperty var ty: MuName = _
}
/** An int constant. */
class ConstInt() extends ConstDef {
@BeanProperty var num: Long = _
}
/** A float constant. */
class ConstFloat() extends ConstDef {
@BeanProperty var num: Float = _
}
/** A double constant. */
class ConstDouble() extends ConstDef {
@BeanProperty var num: Double = _
}
/** A struct constant. */
class ConstStruct() extends ConstDef {
@BeanProperty var fields: List[MuName] = makeList()
}
/** A NULL constant. */
class ConstNull() extends ConstDef {
}
/** A pointer constant. */
class ConstPointer() extends ConstDef {
@BeanProperty var addr: Long = _
}
/** A global cell definition. ".global" */
class GlobalCellDef() extends TopLevelDefinition {
@BeanProperty var ty: MuName = _
......
package uvm.clientsupport.text
import scala.collection.JavaConversions._
object TextIRWriter {
def bundleToText(bundle: Bundle): String = {
val sb = new StringBuilder()
sb.toString()
}
def typeToText(typeDef: TypeDef): String = {
val name = typeDef.name
val ctor = typeDef match {
case t: TypeInt => "int<%d>".format(t.len)
case t: TypeFloat => "float"
case t: TypeDouble => "double"
case t: TypeRef => "ref<%s>".format(t.ty)
case t: TypeIRef => "iref<%s>".format(t.ty)
case t: TypeWeakRef => "weakref<%s>".format(t.ty)
case t: TypeStruct => {
val sb = new StringBuilder("struct<")
for (ty <- t.fieldTy) sb ++= " " ++= ty
sb ++= " >"
sb.toString
}
case t: TypeArray => "array<%s %d>".format(t.elemTy, t.len)
case t: TypeHybrid => "hybrid<%s %s>".format(t.fixedTy, t.varTy)
case t: TypeVoid => "void"
case t: TypeFunc => "func<%s>".format(t.sig)
case t: TypeThread => "thread"
case t: TypeStack => "stack"
case t: TypeTagRef64 => "tagref64"
case t: TypePtr => "ptr<%s>".format(t.ty)
case t: TypeFuncPtr => "funcptr<%s>".format(t.sig)
}
".typedef %s = %s".format(name, ctor)
}
def funcSigToText(funcSigDef: FuncSigDef): String = {
val paramTys = {
val sb = new StringBuilder(" ")
for (ty <- funcSigDef.paramTy) sb ++= ty ++= " "
sb.toString
}
".funcsig %s = %s (%s)".format(funcSigDef.name, funcSigDef.retTy, funcSigDef.paramTy)
}
def constToText(constDef: ConstDef): String = {
val name = constDef.name
val ty = constDef.ty
val ctor: String = constDef match {
case c: ConstInt => c.num.toString
case c: ConstFloat => "bitsf(0x%x)".format(java.lang.Float.floatToRawIntBits(c.num))
case c: ConstDouble => "bitsd(0x%x)".format(java.lang.Double.doubleToRawLongBits(c.num))
case c: ConstStruct => {
val sb = new StringBuilder("{ ")
for (f <- c.fields) sb ++= f ++= " "
sb ++= "}"
sb.toString
}
case c: ConstNull => "NULL"
case c: ConstPointer => c.addr.toString
}
".const %s <%s> = %s".format(name, ty, ctor)
}
def globalToText(globalCellDef: GlobalCellDef): String = {
".global %s <%s>".format(globalCellDef.name, globalCellDef.ty)
}
def funcDeclToText(funcDecl: FuncDecl): String = {
".funcdecl %s <%s>".format(funcDecl.name, funcDecl.sig)
}
def funcExpToText(funcExp: FuncExpDef): String = {
".expose %s = %s %s %s".format(funcExp.name, funcExp.func, funcExp.callConv, funcExp.cookie)
}
def funcDefToText(funcDef: FuncDef): String = {
???
}
}
\ No newline at end of file
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