Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Sherk committed Mar 14, 2022
1 parent c31c456 commit 263fed8
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 15 deletions.
11 changes: 8 additions & 3 deletions except-new.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ exn-pipe cpu(pc :uint<16>)[imem :uint<8>[16]<a,a>, acc :uint<4>[0]<c,s>] :uint<4
{
acc[] <- acc_val - imm;
}
case: (ex)
{
except();
}
}
if (done)
{
Expand All @@ -32,11 +36,12 @@ exn-pipe cpu(pc :uint<16>)[imem :uint<8>[16]<a,a>, acc :uint<4>[0]<c,s>] :uint<4
call cpu(pc + 1);
}
end(acc);
}
commit:
{}
print("no exn here!");
except:
{}
---
print("exception!");
}


circuit {
Expand Down
10 changes: 7 additions & 3 deletions src/main/scala/pipedsl/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ object Main {
rfLockImpl: Option[String] = None): Unit = {
val outputName = FilenameUtils.getBaseName(inputFile.getName) + ".interpret"
val outputFile = new File(Paths.get(outDir.getPath, outputName).toString)
val prog = parse(debug = false, printOutput = false, inputFile, outDir)
val ex_prog = parse(debug = false, printOutput = false, inputFile, outDir)
val prog = ExceptingToNormal.run(ex_prog)
val i: Interpreter = new Interpreter(maxIterations)
i.interp_prog(RemoveTimingPass.run(prog), MemoryInputParser.parse(memoryInputs), outputFile)
}
Expand All @@ -70,9 +71,12 @@ object Main {
val outputName = FilenameUtils.getBaseName(inputFile.getName) + ".typecheck"
val outputFile = new File(Paths.get(outDir.getPath, outputName).toString)

val prog = parse(debug = false, printOutput = false, inputFile, outDir, rfLockImpl = rfLockImpl)
val pinfo = new ProgInfo(prog)
val ex_prog = parse(debug = false, printOutput = false, inputFile, outDir, rfLockImpl = rfLockImpl)

try {
val prog = ExceptingToNormal.run(ex_prog)
new PrettyPrinter(None).printProgram(prog)
val pinfo = new ProgInfo(prog)
MarkNonRecursiveModulePass.run(prog)
//First: add lock regions + checkpoints, then do other things
val inferredProg = new LockRegionInferencePass().run(prog)
Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/pipedsl/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class Parser(rflockImpl: String) extends RegexParsers with PackratParsers {
typ.? ~ lhs ~ "<-" ~ expr ^^ { case t ~ l ~ _ ~ r => l.typ = t; CRecv(l, r) } |
check |
resolveSpec |
"except()" ^^ {_ => CExcept()} |
"start" ~> parens(iden) ^^ { i => CLockStart(i) } |
"end" ~> parens(iden) ^^ { i => CLockEnd(i) } |
"acquire" ~> parens(lockArg ~ ("," ~> lockType).?) ^^ { case i ~ t => CSeq(CLockOp(i, Reserved, t, List(), None), CLockOp(i, Acquired, t, List(), None)) } |
Expand Down Expand Up @@ -376,8 +377,8 @@ class Parser(rflockImpl: String) extends RegexParsers with PackratParsers {
"except:" ~> cmd ^^ (i => i)
}

lazy val exn_body: P[(Command, Command, Command)] =
cmd ~ commital ~ except ^^ {case bod ~ com ~ ex => (bod, com, ex)}
lazy val exn_body: P[(Command, Command, Command)] = dlog(
cmd ~ commital ~ except ^^ {case bod ~ com ~ ex => (bod, com, ex)})("Exception body")

lazy val bitWidthAtom :P[TBitWidth] = iden ^^ {id => TBitWidthVar(Id(generic_type_prefix + id.v))} |
posint ^^ {i => TBitWidthLen(i)}
Expand Down
8 changes: 8 additions & 0 deletions src/main/scala/pipedsl/common/Errors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,12 @@ object Errors {
case class BadConstraintsAtCall(app :EApp) extends RuntimeException(
withPos(s"Constraints for $app not satisfied", app.pos)
)

case class ReleaseInExnBlock(p :Position) extends RuntimeException(
withPos(s"You may not release locks in the except block!", p)
)

case class ReleaseWhenMaybeExcepting(p :Position) extends RuntimeException(
withPos(s"No no no! No releasing when you might be excepting!!", p)
)
}
6 changes: 4 additions & 2 deletions src/main/scala/pipedsl/common/PrettyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class PrettyPrinter(output: Option[File]) {
def printExceptingModule(m :ExceptingModule) :Unit = {
pline("exn-pipe " + m.name.v + "(" + m.inputs.map(printParamToString).mkString(",") +
")[" + m.modules.map(printParamToString).mkString(",") + "] {\n" +
printCmdToString(m.body, 2) + "\n}\ncommit:\n" +
printCmdToString(m.commit_block, 2) + "\n}\nexcept:\n" +
printCmdToString(m.body, 2) + "\ncommit:\n" +
printCmdToString(m.commit_block, 2) + "\nexcept:\n" +
printCmdToString(m.exn_block, 2) + "\n}")
}

Expand Down Expand Up @@ -127,6 +127,7 @@ class PrettyPrinter(output: Option[File]) {
printExprToString(originalSpec) + " = update(" + specId + ", " + printExprToString(value) + ");"
case Syntax.ICheck(specId, value) => ins + "check(" + specId + ", " + printExprToString(value) + ");"
case Syntax.CCheckpoint(h, m) => ins + printExprToString(h) + " <- checkpoint(" + m.v + ");"
case Syntax.CExcept() => ins + "except()"
case _ => "TODO PRINTING COMMAND"
}
}
Expand All @@ -153,6 +154,7 @@ class PrettyPrinter(output: Option[File]) {
case Syntax.EApp(func, args) => func.v + "(" + args.map(a => printExprToString(a)).mkString(",") + ")"
case Syntax.ECall(id, name, args, isAtomic) => "call" + (if(isAtomic) "<atomic> " else " ") + id + "(" + args.map(a => printExprToString(a)).mkString(",") + ")"
case Syntax.EVar(id) => id.v
case Syntax.EString(v) => s""""${v}""""
case Syntax.ECast(ctyp, exp) => "cast(" + printExprToString(exp) + "," + printTypeToString(ctyp) + ")"
case expr: Syntax.CirExpr => expr match {
case CirMem(elemTyp, addrSize, numPorts) => "memory(" + printTypeToString(elemTyp) + "," + addrSize.toString + "," + numPorts.toString + ")"
Expand Down
4 changes: 3 additions & 1 deletion src/main/scala/pipedsl/common/Syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ object Syntax {
}
case class CSplit(cases: List[CaseObj], default: Command) extends Command
case class CEmpty() extends Command

case class CExcept() extends Command

sealed trait InternalCommand extends Command

Expand Down Expand Up @@ -734,6 +734,8 @@ object Syntax {

case class ExternDef(name: Id, typParams: List[Type], methods: List[MethodDef]) extends Definition with TypeAnnotation

val is_excepting_var: Id =
Id("__excepting").setType(TBool())

case class ExceptableProg(exts: List[ExternDef],
fdefs: List[FuncDef], moddefs: List[ModuleTrait], circ: Circuit) extends Positional
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/pipedsl/common/Utilities.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ object Utilities {
case ICondCommand(cond, cs) => getUsedVars(cond) ++ cs.foldLeft(Set[Id]())((s, c) => getAllVarNames(c) ++ s)
case IUpdate(specId, value, originalSpec) => getUsedVars(value) ++ getUsedVars(originalSpec) + specId
case Syntax.CEmpty() => Set()
case CExcept() => Set()
case _ => throw UnexpectedCommand(c)
}

Expand Down
1 change: 1 addition & 0 deletions src/main/scala/pipedsl/passes/CanonicalizePass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class CanonicalizePass() extends CommandPass[Command] with ModulePass[ModuleDef]
case CLockEnd(_) => c
case CLockOp(_, _, _, _, _) => c
case CEmpty() => c
case CExcept() => c
case _: InternalCommand => c
}

Expand Down
36 changes: 35 additions & 1 deletion src/main/scala/pipedsl/passes/ExceptingToNormal.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pipedsl.passes

import pipedsl.common.Errors.{ReleaseInExnBlock, ReleaseWhenMaybeExcepting}
import pipedsl.common.Locks.Released
import pipedsl.common.Syntax._

object ExceptingToNormal
Expand All @@ -11,7 +13,39 @@ object ExceptingToNormal
private def translateModule(m :ModuleTrait) :ModuleDef = m match
{
case m:ModuleDef => m
case ExceptingModule(name, inputs, modules, ret, body, commit_block, exn_block) => ???
case m@ExceptingModule(name, inputs, modules, ret, body, commit_block, exn_block) =>
check_exn_block(exn_block)
check_body(body)
ModuleDef(
name, inputs, modules, ret,
ast_append(body, CIf(EVar(is_excepting_var), exn_block, commit_block))).copyMeta(m)
}

private def ast_append(original :Command, tail :Command) :Command = original match
{
case CTBar(c1, c2) => CTBar(c1, ast_append(c2, tail)).copyMeta(original)
case CEmpty() => tail
case _ => CSeq(original, tail)
}

private def check_body(c :Command) :Unit = c match
{
case CSeq(c1, c2) => check_body(c1); check_body(c2);
case CTBar(c1, c2) => check_body(c1); check_body(c2);
case CIf(_, cons, alt) => check_body(cons); check_body(alt)
case CSplit(cases, default) => cases.foreach(c => check_body(c.body))
case CLockOp(_, Released, _, _, _) => throw ReleaseWhenMaybeExcepting(c.pos)
case _ => ()
}

private def check_exn_block(c :Command): Unit = c match
{
case CSeq(c1, c2) => check_exn_block(c1); check_exn_block(c2)
case CTBar(c1, c2) => check_exn_block(c1); check_exn_block(c2);
case CIf(_, cons, alt) => check_exn_block(cons); check_exn_block(alt)
case CSplit(cases, default) => cases.foreach(c => check_exn_block(c.body)); check_exn_block(default)
case CLockOp(_, Released, _, _, _) => throw ReleaseInExnBlock(c.pos)
case _ => ()
}


Expand Down
1 change: 1 addition & 0 deletions src/main/scala/pipedsl/typechecker/BaseTypeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ object BaseTypeChecker extends TypeChecks[Id, Type] {
})
tenv
case CEmpty() => tenv
case CExcept() => tenv
case _ => throw UnexpectedCommand(c)
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/pipedsl/typechecker/TimingTypeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ object TimingTypeChecker extends TypeChecks[Id, Type] {
checkExpr(exp, vars)
(vars, nextVars)
case Syntax.CEmpty() => (vars, nextVars)
case CExcept() => (vars, nextVars)
case CPrint(args) =>
args.foreach(a => {
checkExpr(a, vars)
Expand Down Expand Up @@ -328,7 +329,7 @@ object TimingTypeChecker extends TypeChecks[Id, Type] {
//calling another pipe
case None => Asynchronous
}
case EVar(id) => if(!vars(id) && isRhs) {
case EVar(id) => if(!vars(id) && isRhs && !(id == is_excepting_var)) {
throw UnavailableArgUse(e.pos, id.toString) }
//TODO make this error message more clear about what's wrong when these are lock handle vars
else { Combinational }
Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/pipedsl/typechecker/TypeInferenceWrapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ object TypeInferenceWrapper
}



def checkProgram(p: Prog): Prog =
{
val extEnv = p.exts.foldLeft[Environment[Id, Type]](TypeEnv())((env, ext) => {
Expand All @@ -208,7 +207,8 @@ object TypeInferenceWrapper
val (nenv, nfunc) = checkFunc(f, env.asInstanceOf[TypeEnv])
(nenv, lst.prepended(nfunc))
})
val (modEnvs, newMods) = p.moddefs.foldLeft[(Environment[Id, Type], List[ModuleDef])]((funcEnvs, List.empty[ModuleDef]))((envNlst, m) =>
val (modEnvs, newMods) = p.moddefs.foldLeft[(Environment[Id, Type],
List[ModuleDef])]((funcEnvs.add(is_excepting_var, TBool()), List.empty[ModuleDef]))((envNlst, m) =>
{
val env = envNlst._1
val lst = envNlst._2
Expand Down Expand Up @@ -345,6 +345,7 @@ object TypeInferenceWrapper
case b => throw UnexpectedType(mem.id.pos, c.toString, "Memory or Module Type", b)
}
case CEmpty() => (c, env, sub)
case CExcept() => (c, env, sub)
case cr@CReturn(exp) =>
val (s, t, e, fixed) = infer(env, exp)
val tempSub = compose_subst(sub, s)
Expand Down

0 comments on commit 263fed8

Please sign in to comment.