To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

Commit 7a05f9e1 authored by Kunshan Wang's avatar Kunshan Wang
Browse files

Updating parser...

parent ed7707c2
...@@ -56,14 +56,14 @@ typeConstructor ...@@ -56,14 +56,14 @@ typeConstructor
; ;
funcSigConstructor funcSigConstructor
: retTy=type '(' (paramTy=type*) ')' : retTy=type '(' (paramTy+=type*) ')'
; ;
constConstructor constConstructor
: intLiteral # ConstInt : intLiteral # ConstInt
| floatLiteral # ConstFloat | floatLiteral # ConstFloat
| doubleLiteral # ConstDouble | doubleLiteral # ConstDouble
| '{' constant* '}' # ConstStruct | '{' GLOBAL_NAME* '}' # ConstStruct
| 'NULL' # ConstNull | 'NULL' # ConstNull
| 'VEC' '{' constant* '}' # ConstVector | 'VEC' '{' constant* '}' # ConstVector
; ;
...@@ -245,9 +245,9 @@ callconv ...@@ -245,9 +245,9 @@ callconv
; ;
intLiteral intLiteral
: INT_DEC # DecIntLiteral : INT_DEC
| INT_OCT # OctIntLiteral | INT_OCT
| INT_HEX # HexIntLiteral | INT_HEX
; ;
floatLiteral floatLiteral
...@@ -276,7 +276,7 @@ INT_DEC ...@@ -276,7 +276,7 @@ INT_DEC
; ;
INT_OCT INT_OCT
: ('+'|'-')? '0' DIGIT* : ('+'|'-')? '0' OCT_DIGIT*
; ;
INT_HEX INT_HEX
...@@ -313,6 +313,11 @@ DIGIT_NON_ZERO ...@@ -313,6 +313,11 @@ DIGIT_NON_ZERO
: [1-9] : [1-9]
; ;
fragment
OCT_DIGIT
: [0-7]
;
fragment fragment
HEX_DIGIT HEX_DIGIT
: [0-9a-fA-F] : [0-9a-fA-F]
......
...@@ -4,12 +4,22 @@ import uvm.types._ ...@@ -4,12 +4,22 @@ import uvm.types._
import uvm.ssavariables._ import uvm.ssavariables._
class Bundle { class Bundle {
/**
* Namespace of all SSA variables, global or local.
*/
val varNs = new SimpleNamespace[SSAVariable]()
/**
* All global SSA varaibles.
*/
val globalVarNs = new SimpleNamespace[GlobalVariable]()
val typeNs = new SimpleNamespace[Type]() val typeNs = new SimpleNamespace[Type]()
val funcSigNs = new SimpleNamespace[FuncSig]() val funcSigNs = new SimpleNamespace[FuncSig]()
val declConstNs = new SimpleNamespace[DeclaredConstant]() val constantNs = new SimpleNamespace[Constant]()
val globalDataNs = new SimpleNamespace[GlobalData]() val globalCellNs = new SimpleNamespace[GlobalCell]()
val funcNs = new SimpleNamespace[Function]() val funcNs = new SimpleNamespace[Function]()
val globalValueNs = new SimpleNamespace[GlobalValue]()
private def simpleMerge[T <: Identified](oldNs: Namespace[T], newNs: Namespace[T]) { private def simpleMerge[T <: Identified](oldNs: Namespace[T], newNs: Namespace[T]) {
for (cand <- newNs.all) { for (cand <- newNs.all) {
......
...@@ -12,19 +12,20 @@ object FuncSig { ...@@ -12,19 +12,20 @@ object FuncSig {
class Function extends GlobalVariable { class Function extends GlobalVariable {
var sig: FuncSig = null var sig: FuncSig = null
var versions: Seq[FuncVersion] = Nil var versions: List[FuncVer] = Nil
} }
/** /**
* A version of a function. Also known as a "control flow graph". * A version of a function. Also known as a "control flow graph".
*/ */
class FuncVersion extends IdentifiedSettable { class FuncVer extends IdentifiedSettable {
var func: Function = null var func: Function = null
var bbs: Seq[BasicBlock] = null var bbs: Seq[BasicBlock] = null
var entry: BasicBlock = null var entry: BasicBlock = null
var params: Seq[Parameter] = null var params: Seq[Parameter] = null
val bbNs: Namespace[BasicBlock] = new SimpleNamespace[BasicBlock] val bbNs: Namespace[BasicBlock] = new SimpleNamespace[BasicBlock]()
val localVarNs: Namespace[LocalVariable] = new SimpleNamespace[LocalVariable]()
} }
class BasicBlock extends IdentifiedSettable { class BasicBlock extends IdentifiedSettable {
......
package uvm.ir.textinput
class IDFactory(val initialID: Int = 65536) {
private var id: Int = initialID
def getID(): Int = {
val myID = id
id = id + 1
return myID
}
}
\ No newline at end of file
package uvm.ir.textinput package uvm.ir.textinput
import scala.collection.JavaConversions._
import uvm._
import uvm.types._
import uvm.ssavariables._
import uvm.comminsts._
import uvm.ir.textinput.gen._
import org.antlr.v4.runtime._ import org.antlr.v4.runtime._
import org.antlr.v4.runtime.tree.TerminalNode import org.antlr.v4.runtime.tree.TerminalNode
import uIRParser._ import uvm._
import uvm.ir.textinput.gen.UIRParser._
import uvm.ir.textinput.gen._
import uvm.ssavariables._
import uvm.types._
import scala.collection.JavaConversions._
object AntlrUvmIRReader { class UIRTextReader(val idFactory: IDFactory) {
import Later.Laterable import uvm.ir.textinput.Later.Laterable
def read(ir: String, globalBundle: Bundle): Bundle = { def read(ir: String, globalBundle: Bundle): Bundle = {
val input = new ANTLRInputStream(ir) val input = new ANTLRInputStream(ir)
val lexer = new uIRLexer(input) read(input, globalBundle)
val tokens = new CommonTokenStream(lexer)
val parser = new uIRParser(tokens)
val ast = parser.ir();
read(ast, globalBundle)
} }
def read(ir: java.io.Reader, globalBundle: Bundle): Bundle = { def read(ir: java.io.Reader, globalBundle: Bundle): Bundle = {
val input = new ANTLRInputStream(ir) val input = new ANTLRInputStream(ir)
val lexer = new uIRLexer(input) read(input, globalBundle)
val tokens = new CommonTokenStream(lexer)
val parser = new uIRParser(tokens)
val ast = parser.ir();
read(ast, globalBundle)
} }
object IDFactory { def read(ir: ANTLRInputStream, globalBundle: Bundle): Bundle = {
private var id: Int = 65536 val lexer = new UIRLexer(ir)
def getID(): Int = { val tokens = new CommonTokenStream(lexer)
val myID = id val parser = new UIRParser(tokens)
id = id + 1 val ast = parser.ir()
return myID read(ast, globalBundle)
}
} }
implicit def terminalToString(tn: TerminalNode): String = tn.getText() implicit def terminalToString(tn: TerminalNode): String = tn.getText()
implicit def nameToString(name: NameContext): String = name.getText()
implicit def tokenToString(tok: Token): String = tok.getText()
val IntRe = """([+-]?)(0x|0|)([0-9a-fA-F]*)""".r
implicit def IntLiteralToBigInt(il: IntLiteralContext): BigInt = { implicit def IntLiteralToBigInt(il: IntLiteralContext): BigInt = {
val txt = il.getText() val txt = il.getText()
val (neg, beg) = txt(0) match {
case '+' => (false, 1) txt match {
case '-' => (true, 1) case IntRe(sign, prefix, nums) => {
case _ => (false, 0) val neg = sign match {
case "+" => false
case "-" => true
case "" => true
} }
val abs = il match { val abs = prefix match {
case dec: DecIntLiteralContext => BigInt(txt.substring(beg), 10) case "0x" => BigInt(nums, 16)
case oct: OctIntLiteralContext => BigInt(txt.substring(beg), 8) case "0" => BigInt(nums, 8)
case hex: HexIntLiteralContext => BigInt(txt.substring(beg + 2), 16) case "" => BigInt(nums, 10)
} }
return if (neg) -abs else abs return if (neg) -abs else abs
} }
}
}
implicit def floatLiteralToFloat(fl: FloatLiteralContext): Float = fl match { implicit def floatLiteralToFloat(fl: FloatLiteralContext): Float = fl match {
case num: FloatNumberContext => num.FP_NUM.getText.toFloat case num: FloatNumberContext => num.FP_NUM.getText.toFloat
...@@ -80,62 +79,69 @@ object AntlrUvmIRReader { ...@@ -80,62 +79,69 @@ object AntlrUvmIRReader {
case bits: DoubleBitsContext => java.lang.Double.longBitsToDouble(bits.intLiteral().longValue()) case bits: DoubleBitsContext => java.lang.Double.longBitsToDouble(bits.intLiteral().longValue())
} }
def read(ir: IrContext, globalBundle: Bundle): Bundle = { def cascadeLookup[T](name:String, ns1: Namespace[T], ns2: Namespace[T]): T =
ns1.get(name).getOrElse(ns2(name))
def read(ir: IrContext, globalBundle: Bundle): Bundle = {
val bundle = new Bundle() val bundle = new Bundle()
val phase1 = new Later() val phase1 = new Later()
def resTy(te: TypeContext): Type = te match { def resTy(ctx: TypeContext): Type = resTy(ctx.getText)
case rt: ReferencedTypeContext => bundle.typeNs.get(rt.GLOBAL_ID()).getOrElse(globalBundle.typeNs(rt.GLOBAL_ID())) def resTy(name: String): Type = cascadeLookup(name, bundle.typeNs, globalBundle.typeNs)
case it: InLineTypeContext => mkType(it.typeConstructor())
} def resSig(ctx: FuncSigContext): FuncSig = resSig(ctx.getText)
def resSig(name: String): FuncSig = cascadeLookup(name, bundle.funcSigNs, globalBundle.funcSigNs)
def resConst(ctx: ConstantContext): Constant = resConst(ctx.getText)
def resConst(name: String): Constant = cascadeLookup(name, bundle.constantNs, globalBundle.constantNs)
def resVar(ctx: ValueContext): Type = resTy(ctx.getText)
def resVar(name: String): SSAVariable = cascadeLookup(name, bundle.varNs, globalBundle.varNs)
def resGlobalVar(name: String): GlobalVariable = cascadeLookup(name, bundle.globalVarNs, globalBundle.globalVarNs)
def mkType(tc: TypeConstructorContext): Type = { def mkType(tc: TypeConstructorContext): Type = {
val ty = tc match { val ty = tc match {
case it: IntTypeContext => TypeInt(it.intLiteral().intValue()) case t: TypeIntContext => TypeInt(t.length.intValue())
case ft: FloatTypeContext => TypeFloat() case t: TypeFloatContext => TypeFloat()
case dt: DoubleTypeContext => TypeDouble() case t: TypeDoubleContext => TypeDouble()
case rt: RefTypeContext => TypeRef(null).later(phase1) { _.ty = resTy(rt.`type`()) } case t: TypeRefContext => TypeRef(null).later(phase1) { _.ty = resTy(t.`type`()) }
case irt: IRefTypeContext => TypeIRef(null).later(phase1) { _.ty = resTy(irt.`type`()) } case t: TypeIRefContext => TypeIRef(null).later(phase1) { _.ty = resTy(t.`type`()) }
case wrt: WeakRefTypeContext => TypeWeakRef(null).later(phase1) { _.ty = resTy(wrt.`type`()) } case t: TypeWeakRefContext => TypeWeakRef(null).later(phase1) { _.ty = resTy(t.`type`()) }
case st: StructTypeContext => TypeStruct(null).later(phase1) { _.fieldTy = st.`type`().map(resTy) } case t: TypeStructContext => TypeStruct(null).later(phase1) { _.fieldTy = t.`type`().map(resTy) }
case at: ArrayTypeContext => TypeArray(null, at.intLiteral().longValue()).later(phase1) { _.elemTy = resTy(at.`type`()) } case t: TypeArrayContext => TypeArray(null, t.length.longValue()).later(phase1) { _.elemTy = resTy(t.`type`()) }
case ht: HybridTypeContext => TypeHybrid(null, null).later(phase1) { t => t.fixedPart = resTy(ht.`type`(0)); t.varPart = resTy(ht.`type`(1)) } case t: TypeHybridContext => TypeHybrid(null, null).later(phase1) { tt => tt.fixedTy = resTy(t.fixedTy); tt.varTy = resTy(t.varTy) }
case vt: VoidTypeContext => TypeVoid() case t: TypeVoidContext => TypeVoid()
case ft: FuncTypeContext => TypeFunc(null).later(phase1) { _.sig = resSig(ft.funcSig()) } case t: TypeFuncContext => TypeFunc(null).later(phase1) { _.sig = resSig(t.funcSig()) }
case thr: ThreadTypeContext => TypeThread() case t: TypeThreadContext => TypeThread()
case sta: StackTypeContext => TypeStack() case t: TypeStackContext => TypeStack()
case tr64: TagRef64TypeContext => TypeTagRef64() case t: TypeTagRef64Context => TypeTagRef64()
case t: TypeVectorContext => TypeVector(null, t.length.longValue()).later(phase1) { _.elemTy = resTy(t.`type`()) }
case _ => throw new TextIRParsingException("foo") case _ => throw new TextIRParsingException("foo")
} }
ty.id = IDFactory.getID()
return ty return ty
} }
def resSig(fs: FuncSigContext): FuncSig = fs match {
case rfs: ReferencedFuncSigContext => bundle.funcSigNs.get(rfs.GLOBAL_ID()).getOrElse(globalBundle.funcSigNs(rfs.GLOBAL_ID()))
case ilfs: InLineFuncSigContext => mkSig(ilfs.funcSigConstructor())
}
def mkSig(fsc: FuncSigConstructorContext): FuncSig = { def mkSig(fsc: FuncSigConstructorContext): FuncSig = {
val sig = FuncSig(null, null).later(phase1) { sig => val sig = FuncSig(null, null).later(phase1) { sig =>
sig.retTy = resTy(fsc.`type`().head) sig.retTy = resTy(fsc.retTy)
sig.paramTy = fsc.`type`().tail.map(resTy) sig.paramTy = for (t<-fsc.paramTy) yield resTy(t)
} }
sig.id = IDFactory.getID()
return sig return sig
} }
ir.metaData.map(_.getChild(0)).foreach { ir.topLevelDef.map(_.getChild(0)).foreach {
case td: TypeDefContext => { case td: TypeDefContext => {
val ty = mkType(td.typeConstructor) val ty = mkType(td.typeConstructor)
ty.name = Some(td.GLOBAL_ID) ty.id = idFactory.getID()
ty.name = Some(td.nam)
bundle.typeNs.add(ty) bundle.typeNs.add(ty)
} }
case fsd: FuncSigDefContext => { case fsd: FuncSigDefContext => {
val sig = mkSig(fsd.funcSigConstructor) val sig = mkSig(fsd.funcSigConstructor)
sig.name = Some(fsd.GLOBAL_ID) sig.id = idFactory.getID()
sig.name = Some(fsd.nam)
bundle.funcSigNs.add(sig) bundle.funcSigNs.add(sig)
} }
case _ => case _ =>
...@@ -145,35 +151,23 @@ object AntlrUvmIRReader { ...@@ -145,35 +151,23 @@ object AntlrUvmIRReader {
val phase2 = new Later() val phase2 = new Later()
def resGV(t: Type, ce: ConstantContext): GlobalValue = ce match { def mkConst(t: Type, c: ConstConstructorContext): Constant = {
case rcc: ReferencedConstContext => bundle.globalValueNs.get(rcc.GLOBAL_ID).getOrElse(globalBundle.globalValueNs(rcc.GLOBAL_ID))
case icc: InLineConstContext => mkConst(t, icc.constExpr)
}
def mkConst(t: Type, c: ConstExprContext): DeclaredConstant = {
val con = c match { val con = c match {
case icc: IntConstContext => ConstInt(t, icc.intLiteral) case cc: ConstIntContext => ConstInt(t, cc.intLiteral)
case fcc: FloatConstContext => ConstFloat(t, fcc.floatLiteral) case cc: ConstFloatContext => ConstFloat(t, cc.floatLiteral)
case dcc: DoubleConstContext => ConstDouble(t, dcc.doubleLiteral) case cc: ConstDoubleContext => ConstDouble(t, cc.doubleLiteral)
case scc: StructConstContext => ConstStruct(t, null).later(phase2) { case cc: ConstStructContext => ConstStruct(t, null).later(phase2) {
_.fields = for ((ft, f) <- t.asInstanceOf[TypeStruct].fieldTy.zip(scc.constant)) yield resGV(ft, f) _.fields = for (gn <- cc.GLOBAL_NAME()) yield resGlobalVar(gn)
} }
case _: NullConstContext => ConstNull(t) case _: ConstNullContext => ConstNull(t)
case cc: ConstVectorContext => ConstVector(t, null).later(phase2) {
_.elems = for (c <- cc.constant()) yield resConst(c)
} }
con.id = IDFactory.getID()
return con
} }
return con
def mkGlobalData(t: Type): GlobalData = {
val gd = GlobalData(t)
gd.id = IDFactory.getID()
return gd
} }
def mkGlobalDataConst(gd: GlobalData): ConstGlobalData = { def mkGlobalCell(t: Type): GlobalCell = GlobalCell(t)
val gdc = ConstGlobalData(gd)
return gdc
}
def mkFunc(sig: FuncSig): Function = { def mkFunc(sig: FuncSig): Function = {
val func = new Function() val func = new Function()
...@@ -181,11 +175,6 @@ object AntlrUvmIRReader { ...@@ -181,11 +175,6 @@ object AntlrUvmIRReader {
return func return func
} }
def mkFuncConst(func: Function): ConstFunc = {
val fc = ConstFunc(func)
return fc
}
def tryReuseFuncID(name: String): Option[Int] = { def tryReuseFuncID(name: String): Option[Int] = {
globalBundle.funcNs.get(name).map(_.id) globalBundle.funcNs.get(name).map(_.id)
} }
...@@ -194,43 +183,39 @@ object AntlrUvmIRReader { ...@@ -194,43 +183,39 @@ object AntlrUvmIRReader {
val sig = resSig(s) val sig = resSig(s)
val func = mkFunc(sig) val func = mkFunc(sig)
val maybeOldID = tryReuseFuncID(n) val maybeOldID = tryReuseFuncID(n)
func.id = maybeOldID.getOrElse(IDFactory.getID()) func.id = maybeOldID.getOrElse(idFactory.getID())
func.name = Some(n) func.name = Some(n)
bundle.funcNs.add(func) bundle.funcNs.add(func)
if (maybeOldID == None) {
val fc = mkFuncConst(func)
bundle.globalValueNs.add(fc)
}
return func return func
} }
var funcDefs: List[(Function, Seq[String], FuncBodyContext)] = Nil var funcDefs: List[(Function, Seq[String], FuncBodyContext)] = Nil
ir.metaData.map(_.getChild(0)).foreach { ir.topLevelDef().map(_.getChild(0)).foreach {
case cdctx: ConstDefContext => { case cdctx: ConstDefContext => {
val ty = resTy(cdctx.`type`) val ty = resTy(cdctx.`type`)
val con = mkConst(ty, cdctx.constExpr) val con = mkConst(ty, cdctx.constConstructor)
con.name = Some(cdctx.GLOBAL_ID) con.id = idFactory.getID()
bundle.declConstNs.add(con) con.name = Some(cdctx.nam)
bundle.globalValueNs.add(con) bundle.constantNs.add(con)
bundle.globalVarNs.add(con)
bundle.varNs.add(con)
} }
case gdctx: GlobalDefContext => { case gdctx: GlobalDefContext => {
val ty = resTy(gdctx.`type`) val ty = resTy(gdctx.`type`)
val gd = mkGlobalData(ty) val gc = mkGlobalCell(ty)
gd.name = Some(gdctx.GLOBAL_ID) gc.name = Some(gdctx.nam)
bundle.globalDataNs.add(gd) bundle.globalCellNs.add(gc)
bundle.globalVarNs.add(gc)
val gdc = mkGlobalDataConst(gd) bundle.varNs.add(gc)
bundle.globalValueNs.add(gdc)
} }
case fdecl: FuncDeclContext => { case fdecl: FuncDeclContext => {
declFunc(fdecl.GLOBAL_ID, fdecl.funcSig) declFunc(fdecl.nam, fdecl.funcSig)
} }
case fdef: FuncDefContext => { case fdef: FuncDefContext => {
val func = declFunc(fdef.GLOBAL_ID, fdef.funcSig) val func = declFunc(fdef.nam, fdef.funcSig)
funcDefs = (func, fdef.paramList.LOCAL_ID.map(_.getText), fdef.funcBody) :: funcDefs funcDefs = (func, fdef.paramList.name().map(_.getText), fdef.funcBody) :: funcDefs
} }
case _ => {} case _ => {}
} }
...@@ -238,16 +223,18 @@ object AntlrUvmIRReader { ...@@ -238,16 +223,18 @@ object AntlrUvmIRReader {
phase2.doAll() phase2.doAll()
def defFunc(func: Function, ps: Seq[String], body: FuncBodyContext) { def defFunc(func: Function, ps: Seq[String], body: FuncBodyContext) {
val cfg = new CFG() val ver = new FuncVer()
cfg.func = func ver.func = func
func.cfg = Some(cfg) func.versions = ver :: func.versions
def globalize(name: String): String = if(name(0)=="@") name else (ver.name.get + name.substring(1))
cfg.params = ps.zipWithIndex.map { ver.params = ps.zipWithIndex.map {
case (n, i) => case (n, i) =>
val param = Parameter(func.sig, i) val param = Parameter(ver, i)
param.id = IDFactory.getID() param.id = idFactory.getID()
param.name = Some(n) param.name = Some(globalize(n))
cfg.lvNs.add(param) ver.localVarNs.add(param)
param param
} }
...@@ -259,12 +246,12 @@ object AntlrUvmIRReader { ...@@ -259,12 +246,12 @@ object AntlrUvmIRReader {
val bb = new BasicBlock() val bb = new BasicBlock()
bb.id = IDFactory.getID() bb.id = IDFactory.getID()
bb.name = name bb.name = name
cfg.bbNs.add(bb)