Commit 096d0e87 authored by Kunshan Wang's avatar Kunshan Wang

Reader (partial)

parent 81bcbf72
......@@ -9,5 +9,5 @@ class Bundle {
val declConstNs = new SimpleNamespace[DeclaredConstant]()
val globalDataNS = new SimpleNamespace[GlobalData]()
val funcNs = new SimpleNamespace[Function]()
val globalValueNS = new SimpleNamespace[Constant]()
val globalValueNS = new SimpleNamespace[GlobalValue]()
}
......@@ -2,6 +2,4 @@ package uvm
import uvm.types._
class GlobalData extends IdentifiedSettable {
var ty: Type = null
}
\ No newline at end of file
case class GlobalData(var ty: Type) extends IdentifiedSettable
......@@ -22,7 +22,7 @@ class CFG {
var params: Seq[Parameter] = null
var bbNs: Namespace[BasicBlock] = null // Consider using one global bb ns
var instNs: Namespace[Instruction] = null // Consider using one global value ns
var lvNs: Namespace[LocalValue] = null // Consider using one global value ns
}
class BasicBlock extends IdentifiedSettable {
......
......@@ -39,37 +39,45 @@ object UvmIRParser extends RegexParsers {
def topLevel: Parser[TopLevel] = typeDef | funcSigDef | constDef | globalDataDef | funcDecl | funcDef
def t = typeExpr
def s = funcSigExpr
def v = valueExpr
def l = lid
def g = gid
def typeDef: Parser[TypeDef] = ".typedef" ~> gid ~< "=" ~ typeCons ^^^^ TypeDef
def funcSigDef: Parser[FuncSigDef] = ".funcsig" ~> gid ~< "=" ~ funcSigCons ^^^^ FuncSigDef
def constDef: Parser[ConstDef] = ".const" ~> gid ~ ang(typeExpr) ~< "=" ~ constCons ^^^^ ConstDef
def globalDataDef: Parser[GlobalDataDef] = ".global" ~> gid ~ ang(typeExpr) ^^^^ GlobalDataDef
def funcDecl: Parser[FuncDecl] = ".funcdecl" ~> gid ~ ang(funcSigExpr) ^^^^ FuncDecl
def funcDef: Parser[FuncDef] = ".funcdef" ~> gid ~ ang(funcSigExpr) ~ paren(rep(lid)) ~< "=" ~ funcBodyDef ^^^^ FuncDef
def constDef: Parser[ConstDef] = ".const" ~> gid ~ ang(t) ~< "=" ~ constCons ^^^^ ConstDef
def globalDataDef: Parser[GlobalDataDef] = ".global" ~> gid ~ ang(t) ^^^^ GlobalDataDef
def funcDecl: Parser[FuncDecl] = ".funcdecl" ~> gid ~ ang(s) ^^^^ FuncDecl
def funcDef: Parser[FuncDef] = ".funcdef" ~> gid ~ ang(s) ~ paren(rep(l)) ~ funcBodyDef ^^^^ FuncDef
def typeExpr: Parser[TypeExpr] = gid ^^ TypeRef | typeCons
def typeExpr: Parser[TypeExpr] = gid ^^ ReferredType | typeCons
def typeCons: Parser[TypeCons] =
"int" ~> ang(intLit.asInt) ^^ IntCons |
"float" ^^^ FloatCons |
"double" ^^^ DoubleCons |
"ref" ~> ang(typeExpr) ^^ RefCons |
"iref" ~> ang(typeExpr) ^^ IRefCons |
"weakref" ~> ang(typeExpr) ^^ WeakRefCons |
"struct" ~> ang(rep(typeExpr)) ^^ StructCons |
"array" ~> ang(typeExpr ~ intLit.asLong) ^^^^ ArrayCons |
"ref" ~> ang(t) ^^ RefCons |
"iref" ~> ang(t) ^^ IRefCons |
"weakref" ~> ang(t) ^^ WeakRefCons |
"struct" ~> ang(rep(t)) ^^ StructCons |
"array" ~> ang(t ~ intLit.asLong) ^^^^ ArrayCons |
"hybrid" ~> ang(t ~ t) ^^^^ HybridCons |
"void" ^^^ VoidCons |
"func" ~> ang(funcSigExpr) ^^ FuncCons |
"func" ~> ang(s) ^^ FuncCons |
"thread" ^^^ ThreadCons |
"stack" ^^^ StackCons |
"tagref64" ^^^ TagRef64Cons
def funcSigExpr: Parser[FuncSigExpr] = gid ^^ FuncSigRef | funcSigCons
def funcSigCons: Parser[FuncSigCons] = typeExpr ~ paren(rep(typeExpr)) ^^^^ FuncSigCons
def funcSigExpr: Parser[FuncSigExpr] = funcSigCons | g ^^ ReferredFuncSig
def funcSigCons: Parser[FuncSigCons] = t ~ paren(rep(t)) ^^^^ FuncSigCons
def constExpr: Parser[ConstExpr] = gid ^^ ConstRef | constCons
def constExpr: Parser[ConstExpr] = g ^^ ReferredConst | constCons
def constCons: Parser[ConstCons] =
intLit ^^ IntConstCons |
floatLit ^^ FloatConstCons |
floatLit ^^ FloatConstCons |
doubleLit ^^ DoubleConstCons |
intLit ^^ IntConstCons |
brace(rep(constExpr)) ^^ StructConstCons |
"NULL" ^^^ NullConstCons
def funcBodyDef: Parser[FuncBodyDef] = brace(entryBlock ~ rep(regularBlock)) ^^^^ FuncBodyDef
......@@ -77,15 +85,9 @@ object UvmIRParser extends RegexParsers {
def entryBlock: Parser[BasicBlockDef] = opt(label) ~ rep1(inst) ^^^^ BasicBlockDef
def regularBlock: Parser[BasicBlockDef] = (label ^^ { Some(_) }) ~ rep1(inst) ^^^^ BasicBlockDef
def label: Parser[LID] = lid <~ ":"
def label: Parser[LID] = l <~ ":"
def inst: Parser[InstDef] = opt(lid <~ "=") ~ instCons ^^^^ InstDef
def t = typeExpr
def s = funcSigExpr
def v = valueExpr
def l = lid
def g = gid
def inst: Parser[InstDef] = opt(l <~ "=") ~ instCons ^^^^ InstDef
def instCons: Parser[InstCons] =
binOptr ~ ang(t) ~ v ~ v ^^^^ BinOpCons |
......@@ -94,8 +96,8 @@ object UvmIRParser extends RegexParsers {
"SELECT" ~> ang(t) ~ v ~ v ~ v ^^^^ SelectCons |
"BRANCH" ~> l ^^ BranchCons |
"BRANCH2" ~> v ~ l ~ l ^^^^ Branch2Cons |
"SWITCH" ~> ang(t) ~ v ~ l ~ vbMap ^^^^ SwitchCons |
"PHI" ~> ang(t) ~ bvMap ^^^^ PhiCons |
"SWITCH" ~> ang(t) ~ v ~ l ~ brace(vbMap) ^^^^ SwitchCons |
"PHI" ~> ang(t) ~ brace(bvMap) ^^^^ PhiCons |
"CALL" ~> ang(s) ~ v ~ args ~ maybeKeepAlive ^^^^ CallCons |
"INVOKE" ~> ang(s) ~ v ~ args ~ l ~ l ~ maybeKeepAlive ^^^^ InvokeCons |
"TAILCALL" ~> ang(s) ~ v ~ args ^^^^ TailCallCons |
......@@ -112,6 +114,7 @@ object UvmIRParser extends RegexParsers {
"GETIREF" ~> ang(t) ~ v ^^^^ GetIRefCons |
"GETFIELDIREF" ~> ang(t ~ intLit.asInt) ~ v ^^^^ GetFieldIRefCons |
"GETELEMIREF" ~> ang(t) ~ v ~ v ^^^^ GetElemIRefCons |
"SHIFTIREF" ~> ang(t) ~ v ~ v ^^^^ ShiftIRefCons |
"GETFIXEDPARTIREF" ~> ang(t) ~ v ^^^^ GetFixedPartIRefCons |
"GETVARPARTIREF" ~> ang(t) ~ v ^^^^ GetVarPartIRefCons |
"LOAD" ~> maybeMemOrd ~ ang(t) ~ v ^^^^ LoadCons |
......@@ -130,14 +133,14 @@ object UvmIRParser extends RegexParsers {
def bvMap: Parser[Seq[(LID, ValueExpr)]] = rep(lid ~< ":" ~ valueExpr ~< ";" ^^ { case a ~ b => (a, b) })
def args: Parser[Seq[ValueExpr]] = paren(rep(v))
def keepAlive: Parser[Seq[ValueExpr]] = "KEEPALIVE" ~> args
def maybeKeepAlive: Parser[Seq[ValueExpr]] = opt(keepAlive) ^^ { _.getOrElse(Nil) }
def keepAlive: Parser[Seq[LID]] = "KEEPALIVE" ~> paren(rep(l))
def maybeKeepAlive: Parser[Seq[LID]] = opt(keepAlive) ^^ { _.getOrElse(Nil) }
def valueExpr: Parser[ValueExpr] = id ^^ RefValue | constCons ^^ InlineValue
def binOptr: Parser[String] = "ADD|SUB|MUL|UDIV|SDIV|UREM|SREM|SHL|LSHR|ASHR|AND|OR|XOR".r
def binOptr: Parser[String] = "ADD|SUB|MUL|UDIV|SDIV|UREM|SREM|SHL|LSHR|ASHR|AND|OR|XOR|FADD|FSUB|FMUL|FDIV|FREM".r
def cmpOptr: Parser[String] = "EQ|NE|ULT|ULE|UGT|UGE|SLT|SLE|SGT|SGE|FTRUE|FFALSE|FEQ|FNE|FORD|FOEQ|FONE|FOLT|FOLE|FOGT|FOGE|FUNO|FUEQ|FUNE|FULT|FULE|FUGT|FUGE".r
def convOptr: Parser[String] = "TRUNC|ZEXT|SEXT|FPTRUNC|FPEXT|FPTOUI|FPTOSI|UPTOFP|SITPFO|BITCAST|REFCAST|IREFCAST|FUNCCAST".r
def convOptr: Parser[String] = "TRUNC|ZEXT|SEXT|FPTRUNC|FPEXT|FPTOUI|FPTOSI|UITOFP|SITOFP|BITCAST|REFCAST|IREFCAST|FUNCCAST".r
def memOrd: Parser[String] = "NOT_ATOMIC|UNORDERED|MONOTONIC|ACQUIRE|RELEASE|ACQ_REL|SEQ_CST".r
def atomicRMWOp: Parser[String] = "XCHG|ADD|SUB|AND|NAND|OR|XOR|MIN|MAX|UMIN|UMAX".r
def callConv: Parser[String] = "DEFAULT".r
......@@ -162,21 +165,21 @@ object UvmIRParser extends RegexParsers {
}
def intLit: Parser[BigInt] =
sign ~ """[1-9][0-9]*""" ^^ parseInt(10) |
sign ~< "0" ~ """[0-9]*""" ^^ parseInt(8) |
sign ~< "0x" ~ """[0-9a-fA-F]*""" ^^ parseInt(16)
sign ~< "0x" ~ """[0-9a-fA-F]+""".r ^^ parseInt(16) |
sign ~< "0" ~ """[0-9]+""".r ^^ parseInt(8) |
sign ~ """([1-9][0-9]*)|0""".r ^^ parseInt(10)
def floatLit: Parser[Float] =
"""[+-]?[0-9]+\.[0-9]+(e[+-]?[0-9]+)?""".r <~ "f" ^^ { _.toFloat } |
"nan" <~ "f" ^^^ Float.NaN |
"inf" <~ "f" ^^^ Float.PositiveInfinity |
"+inf" <~ "f" ^^^ Float.PositiveInfinity |
"-inf" <~ "f" ^^^ Float.NegativeInfinity |
"bitsf" ~> paren(intLit.asInt) ^^ { java.lang.Float.intBitsToFloat(_) }
def doubleLit: Parser[Double] =
"""[+-]?[0-9]+\.[0-9]+(e[+-]?[0-9]+)?""".r <~ "d" ^^ { _.toDouble } |
"nan" <~ "d" ^^^ Double.NaN |
"inf" <~ "d" ^^^ Double.PositiveInfinity |
"+inf" <~ "d" ^^^ Double.PositiveInfinity |
"-inf" <~ "d" ^^^ Double.NegativeInfinity |
"bitsd" ~> paren(intLit.asLong) ^^ { java.lang.Double.longBitsToDouble(_) }
......@@ -184,8 +187,9 @@ object UvmIRParser extends RegexParsers {
def apply(input: java.io.Reader): IR = handleNoSuccess(parseAll(ir, input))
private def handleNoSuccess(pr: ParseResult[IR]): IR = pr match {
case Success(irNode, _) => irNode
case NoSuccess(msg, _) => throw new TextIRParsingException(msg)
case Success(irNode, next) => irNode
case NoSuccess(msg, next) => throw new TextIRParsingException(
"line %d, col %d: %s".format(next.pos.line, next.pos.column, msg))
}
}
......@@ -197,7 +201,7 @@ object UvmIRAST {
case class TypeDef(name: GID, cons: TypeCons) extends TopLevel
abstract class TypeExpr
case class TypeRef(id: GID) extends TypeExpr
case class ReferredType(id: GID) extends TypeExpr
abstract class TypeCons extends TypeExpr
case class IntCons(length: Int) extends TypeCons
......@@ -218,13 +222,13 @@ object UvmIRAST {
case class FuncSigDef(name: GID, cons: FuncSigCons) extends TopLevel
abstract class FuncSigExpr
case class FuncSigRef(id: GID) extends FuncSigExpr
case class ReferredFuncSig(id: GID) extends FuncSigExpr
case class FuncSigCons(retTy: TypeExpr, paramTy: Seq[TypeExpr]) extends FuncSigExpr
case class ConstDef(name: GID, ty: TypeExpr, cons: ConstCons) extends TopLevel
abstract class ConstExpr
case class ConstRef(id: GID) extends ConstExpr
case class ReferredConst(id: GID) extends ConstExpr
abstract class ConstCons extends ConstExpr
case class IntConstCons(num: BigInt) extends ConstCons
......@@ -259,8 +263,8 @@ object UvmIRAST {
case class Branch2Cons(cond: ValueExpr, ifTrue: LID, ifFalse: LID) extends InstCons
case class SwitchCons(opndTy: TypeExpr, opnd: ValueExpr, defDest: LID, cases: Seq[(ValueExpr, LID)]) extends InstCons
case class PhiCons(opndTy: TypeExpr, cases: Seq[(LID, ValueExpr)]) extends InstCons
case class CallCons(sig: FuncSigExpr, callee: ValueExpr, args: Seq[ValueExpr], keepAlives: Seq[ValueExpr]) extends InstCons
case class InvokeCons(sig: FuncSigExpr, callee: ValueExpr, args: Seq[ValueExpr], nor: LID, exc: LID, keepAlives: Seq[ValueExpr]) extends InstCons
case class CallCons(sig: FuncSigExpr, callee: ValueExpr, args: Seq[ValueExpr], keepAlives: Seq[LID]) extends InstCons
case class InvokeCons(sig: FuncSigExpr, callee: ValueExpr, args: Seq[ValueExpr], nor: LID, exc: LID, keepAlives: Seq[LID]) extends InstCons
case class TailCallCons(sig: FuncSigExpr, callee: ValueExpr, args: Seq[ValueExpr]) extends InstCons
case class RetCons(retTy: TypeExpr, retVal: ValueExpr) extends InstCons
case object RetVoidCons extends InstCons
......@@ -284,12 +288,12 @@ object UvmIRAST {
loc: ValueExpr, expected: ValueExpr, desired: ValueExpr) extends InstCons
case class AtomicRMWCons(ord: String, op: String, referentTy: TypeExpr, loc: ValueExpr, newVal: ValueExpr) extends InstCons
case class FenceCons(ord: String) extends InstCons
case class TrapCons(retTy: TypeExpr, nor: LID, exc: LID, keepAlives: Seq[ValueExpr]) extends InstCons
case class WatchpointCons(wpID: Int, retTy: TypeExpr, dis: LID, nor: LID, exc: LID, keepAlives: Seq[ValueExpr]) extends InstCons
case class TrapCons(retTy: TypeExpr, nor: LID, exc: LID, keepAlives: Seq[LID]) extends InstCons
case class WatchpointCons(wpID: Int, retTy: TypeExpr, dis: LID, nor: LID, exc: LID, keepAlives: Seq[LID]) extends InstCons
case class CCallCons(callConv: String, sig: FuncSigExpr, callee: ValueExpr, args: Seq[ValueExpr]) extends InstCons
case class NewStackCons(sig: FuncSigExpr, callee: ValueExpr, args: Seq[ValueExpr]) extends InstCons
case class ICallCons(iFunc: GID, args: Seq[ValueExpr], keepAlives: Seq[ValueExpr]) extends InstCons
case class IInvokeCons(iFunc: GID, args: Seq[ValueExpr], nor: LID, exc: LID, keepAlives: Seq[ValueExpr]) extends InstCons
case class ICallCons(iFunc: GID, args: Seq[ValueExpr], keepAlives: Seq[LID]) extends InstCons
case class IInvokeCons(iFunc: GID, args: Seq[ValueExpr], nor: LID, exc: LID, keepAlives: Seq[LID]) extends InstCons
abstract class ID(name: String)
case class GID(name: String) extends ID(name)
......
......@@ -6,18 +6,340 @@ import uvm.ssavalues._
object UvmIRReader {
import UvmIRAST._
def read(ir: CharSequence): Bundle = {
val ast = UvmIRParser(ir)
read(ast)
}
def read(ir: java.io.Reader): Bundle = {
val ast = UvmIRParser(ir)
read(ast)
}
def read(ast: IR): Bundle = {
null
object IDFactory {
private var id: Int = 65536
def getID(): Int = {
val myID = id
id = id + 1
return myID
}
}
class Later {
var jobs: List[() => Unit] = Nil
def apply(job: () => Unit) {
jobs = job :: jobs
}
def doAll() {
while (!jobs.isEmpty) {
val job = jobs.head
jobs = jobs.tail
job()
}
}
def isEmpty: Boolean = jobs.isEmpty
}
implicit class Laterable[T](val anything: T) {
def later(lat: Later)(job: T => Unit): T = {
lat(() => job(anything))
anything
}
}
def read(ir: IR): Bundle = {
val bundle = new Bundle()
val phase1 = new Later()
def resTy(te: TypeExpr): Type = te match {
case ReferredType(g) => bundle.typeNs(g.name)
case tc: TypeCons => mkType(tc)
}
def mkType(tc: TypeCons): Type = {
val ty = tc match {
case IntCons(l) => TypeInt(l)
case FloatCons => TypeFloat()
case DoubleCons => TypeDouble()
case RefCons(t) => TypeRef(null).later(phase1) { _.ty = resTy(t) }
case IRefCons(t) => TypeIRef(null).later(phase1) { _.ty = resTy(t) }
case WeakRefCons(t) => TypeWeakRef(null).later(phase1) { _.ty = resTy(t) }
case StructCons(fs) => TypeStruct(null).later(phase1) { _.fieldTy = fs.map(resTy) }
case ArrayCons(et, l) => TypeArray(null, l).later(phase1) { _.elemTy = resTy(et) }
case VoidCons => TypeVoid()
case FuncCons(s) => TypeFunc(null).later(phase1) { _.sig = resSig(s) }
case ThreadCons => TypeThread()
case StackCons => TypeStack()
case TagRef64Cons => TypeTagRef64()
case _ => throw new RuntimeException("foo")
}
ty.id = IDFactory.getID()
return ty
}
def resSig(se: FuncSigExpr): FuncSig = se match {
case ReferredFuncSig(g) => bundle.funcSigNs(g.name)
case FuncSigCons(r, ps) => mkSig(r, ps)
}
def mkSig(r: TypeExpr, ps: Seq[TypeExpr]): FuncSig = {
val sig = FuncSig(null, null).later(phase1) { sig =>
sig.retTy = resTy(r)
sig.paramTy = ps.map(resTy)
}
sig.id = IDFactory.getID()
return sig
}
ir.topLevels.foreach {
case TypeDef(n, c) => {
val ty = mkType(c)
ty.name = Some(n.name)
bundle.typeNs.add(ty)
}
case FuncSigDef(n, FuncSigCons(r, ps)) => {
val sig = mkSig(r, ps)
sig.name = Some(n.name)
bundle.funcSigNs.add(sig)
}
}
phase1.doAll()
val phase2 = new Later()
def resGV(t: Type, ce: ConstExpr): GlobalValue = ce match {
case ReferredConst(g) => bundle.globalValueNS(g.name)
case c: ConstCons => mkConst(t, c)
}
def mkConst(t: Type, c: ConstCons): DeclaredConstant = {
val con = c match {
case IntConstCons(num) => ConstInt(t, num)
case FloatConstCons(num) => ConstFloat(t, num)
case DoubleConstCons(num) => ConstDouble(t, num)
case StructConstCons(fs) => ConstStruct(t, null).later(phase2) {
_.fields = for (f <- fs) yield resGV(t, f)
}
case NullConstCons => ConstNull(t)
}
con.id = IDFactory.getID()
return con
}
def mkGlobalData(t: Type): GlobalData = {
val gd = GlobalData(t)
gd.id = IDFactory.getID()
return gd
}
def mkGlobalDataConst(gd: GlobalData): ConstGlobalData = {
val gdc = ConstGlobalData(gd)
gdc.id = IDFactory.getID()
return gdc
}
def mkFunc(sig: FuncSig): Function = {
val func = new Function()
func.sig = sig
return func
}
def mkFuncConst(func: Function): ConstFunc = {
val fc = ConstFunc(func)
fc.id = IDFactory.getID()
return fc
}
def declFunc(n: GID, s: FuncSigExpr): Function = {
val sig = resSig(s)
val func = mkFunc(sig)
func.name = Some(n.name)
bundle.funcNs.add(func)
val fc = mkFuncConst(func)
fc.name = Some(n.name)
bundle.globalValueNS.add(fc)
return func
}
var funcDefs: List[(Function, Seq[LID], FuncBodyDef)] = Nil
ir.topLevels.foreach {
case ConstDef(n, t, c) => {
val ty = resTy(t)
val con = mkConst(ty, c)
con.name = Some(n.name)
bundle.declConstNs.add(con)
bundle.globalValueNS.add(con)
}
case GlobalDataDef(n, t) => {
val ty = resTy(t)
val gd = mkGlobalData(ty)
gd.name = Some(n.name)
bundle.globalDataNS.add(gd)
val gdc = mkGlobalDataConst(gd)
gdc.name = Some(n.name)
bundle.globalValueNS.add(gdc)
}
case FuncDecl(n, s) => {
declFunc(n, s)
}
case FuncDef(n, s, ps, body) => {
val func = declFunc(n, s)
funcDefs = (func, ps, body) :: funcDefs
}
}
phase2.doAll()
def defFunc(func: Function, ps: Seq[LID], body: FuncBodyDef) {
val cfg = new CFG()
cfg.func = func
cfg.params = ps.zipWithIndex.map {
case (n, i) =>
val param = Parameter(func.sig, i)
param.id = IDFactory.getID()
param.name = Some(n.name)
cfg.lvNs.add(param)
param
}
val phase3 = new Later()
val phase4 = new Later()
def makeBB(bbd: BasicBlockDef): BasicBlock = {
val bb = new BasicBlock()
bb.id = IDFactory.getID()
bb.name = bbd.name.map(_.name)
cfg.bbNs.add(bb)
phase3 { () =>
bb.insts = for (instDef <- bbd.insts)
yield mkInst(instDef)
}
return bb
}
def resBB(n: LID): BasicBlock = cfg.bbNs(n.name)
def resVal(ty: Option[Type], ve: ValueExpr): Value = ve match {
case RefValue(id) => id match {
case GID(n) => bundle.globalValueNS(n)
case LID(n) => cfg.lvNs(n)
}
case InlineValue(cc) => ty match {
case None => throw new TextIRParsingException(
"Cannot use inline constant value when its type is not a user-defined type.")
case Some(t) => mkConst(t, cc)
}
}
def vc(ty: Type, ve: ValueExpr): Value = resVal(Some(ty), ve)
def vnc(ve: ValueExpr): Value = resVal(None, ve)
def resArgs(s: FuncSig, a: Seq[ValueExpr]): Seq[Value] = s.paramTy.zip(a).map {
case (t, v) => vc(t, v)
}
def resKA(ka: Seq[LID]): Seq[Value] = ka.map(n => cfg.lvNs(n.name))
def mkInst(instDef: InstDef): Instruction = {
val inst: Instruction = instDef.cons match {
case BinOpCons(optr, t, op1, op2) =>
InstBinOp(BinOptr.withName(optr), resTy(t), null, null).later(phase4) { i =>
i.op1 = vc(i.opndTy, op1); i.op2 = vc(i.opndTy, op2)
}
case CmpCons(optr, t, op1, op2) =>
InstCmp(CmpOptr.withName(optr), resTy(t), null, null).later(phase4) { i =>
i.op1 = vc(i.opndTy, op1); i.op2 = vc(i.opndTy, op2)
}
case ConvCons(optr, t1, t2, op) =>
InstConv(ConvOptr.withName(optr), resTy(t1), resTy(t2), null).later(phase4) { i =>
i.opnd = vc(i.fromTy, op)
}
case SelectCons(t, c, tr, fa) =>
InstSelect(resTy(t), null, null, null).later(phase4) { i =>
i.cond = vnc(c); i.ifTrue = vc(i.opndTy, tr); i.ifFalse = vc(i.opndTy, fa)
}
case BranchCons(d) =>
InstBranch(resBB(d))
case Branch2Cons(c, tr, fa) =>
InstBranch2(null, resBB(tr), resBB(fa)).later(phase4) { i =>
i.cond = vnc(c)
}
case SwitchCons(t, o, d, cs) =>
InstSwitch(resTy(t), null, resBB(d), null).later(phase4) { i =>
i.opnd = vc(i.opndTy, o)
i.cases = cs.map {
case (v, b) =>
(vc(i.opndTy, v), resBB(b))
}
}
case PhiCons(t, cs) =>
InstPhi(resTy(t), null).later(phase4) { i =>
i.cases = cs.map {
case (b, v) =>
(resBB(b), vc(i.opndTy, v))
}
}
case CallCons(s, f, a, ka) =>
InstCall(resSig(s), null, null, null).later(phase4) { i =>
i.callee = vnc(f); i.args = resArgs(i.sig, a); i.keepAlives = resKA(ka)
}
case InvokeCons(s, f, a, n, e, ka) =>
InstInvoke(resSig(s), null, null, resBB(n), resBB(e), null).later(phase4) { i =>
i.callee = vnc(f); i.args = resArgs(i.sig, a); i.keepAlives = resKA(ka)
}
case TailCallCons(s, f, a) =>
InstTailCall(resSig(s), null, null).later(phase4) { i =>
i.callee = vnc(f); i.args = resArgs(i.sig, a)
}
case RetCons(t, v) =>
InstRet(resTy(t), null).later(phase4) { i =>
i.retVal = vc(i.retTy, v)
}
case RetVoidCons =>
InstRetVoid()
case ThrowCons(v) =>
InstThrow(null).later(phase4) { i =>
i.excVal = vnc(v)
}
case LandingpadCons =>
InstLandingpad()
}
return inst
}
val entry = makeBB(body.entry)
val rest = body.bbs.map(makeBB).toList
val bbs = entry :: rest
cfg.bbs = bbs
cfg.entry = entry
phase3.doAll()
phase4.doAll()
}
for ((func, ps, body) <- funcDefs) {
defFunc(func, ps, body)
}
return bundle
}
}
\ No newline at end of file
......@@ -13,9 +13,9 @@ abstract class Value extends IdentifiedSettable {
// Constants
abstract class Constant extends Value
abstract class GlobalValue extends Value
abstract class DeclaredConstant extends Constant {
abstract class DeclaredConstant extends GlobalValue {
var constTy: Type
def ty = constTy
}
......@@ -23,26 +23,28 @@ abstract class DeclaredConstant extends Constant {
case class ConstInt(var constTy: Type, var num: BigInt) extends DeclaredConstant
case class ConstFloat(var constTy: Type, var num: Float) extends DeclaredConstant
case class ConstDouble(var constTy: Type, var num: Double) extends DeclaredConstant
case class ConstStruct(var constTy: Type, var fields: Seq[Constant]) extends DeclaredConstant
case class ConstStruct(var constTy: Type, var fields: Seq[GlobalValue]) extends DeclaredConstant
case class ConstNull(var constTy: Type) extends DeclaredConstant
case class ConstFunc(var func: Function) extends Constant {
case class ConstFunc(var func: Function) extends GlobalValue {
def ty = func.sig.retTy
}
case class ConstGlobalData(var gd: GlobalData) extends Constant {
case class ConstGlobalData(var gd: GlobalData) extends GlobalValue {
def ty = gd.ty
}
// Parameter
// Local values: Parameter and Instructions
case class Parameter(var sig: FuncSig, var index: Int) extends Value {
abstract class LocalValue extends Value
case class Parameter(var sig: FuncSig, var index: Int) extends LocalValue {
def ty = sig.retTy
}
// Instructions
abstract class Instruction extends Value
abstract class Instruction extends LocalValue
/// enumerations
......
package uvm.ir.textinput
import collection.mutable.Stack
import org.scalatest._
class UvmIRParserSpec extends FlatSpec with Matchers {
def parseFile(fileName: String) {
val ir = UvmIRParser(new java.io.FileReader(fileName))
println(ir)
}
"The IRParser" should "parse simple type definitions" in {
parseFile("tests/uvm-parsing-test/types.uir")
}
it should "parse simple constant definitions" in {
parseFile("tests/uvm-parsing-test/constants.uir")
}
it should "parse simple function definitions" in {
parseFile("tests/uvm-parsing-test/functions.uir")
}
it should "parse simple instruction definitions" in {
parseFile("tests/uvm-parsing-test/instructions.uir")
}
}
\ No newline at end of file