Skip to content

Commit

Permalink
final fall commit?
Browse files Browse the repository at this point in the history
things are in a relatively stable state, old features work, and bit width
addition also works
the file generic_funcs_pass.pdl in the root directory is one that i've been
using for scratch testing, so i'd like to keep it around for now.
  • Loading branch information
Charles Sherk committed Dec 16, 2021
1 parent dbabd98 commit 11c29e3
Show file tree
Hide file tree
Showing 13 changed files with 355 additions and 139 deletions.
5 changes: 4 additions & 1 deletion docs/inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ have a multiplication of constants, one of them will need to be annotated

You can parameterize both external modules as well as functions on named
types. You can write down expressions in type variables, though for now the only
supported operation is addition.
supported operation is addition. To do this, the type variables you want to use
are placed in java style <> before the arguments. These type variables
necessarily are always bit widths, and can be referenced in the function body in
other types. They will soon be allowed to be used in bit indexing expressions.
97 changes: 52 additions & 45 deletions generic_funcs_pass.pdl
Original file line number Diff line number Diff line change
@@ -1,48 +1,55 @@
def error(inpt :int<10>) :bool
{
shamt = cast(inpt{4:0}, uint<5>);
return true;
}


def iden<U>(a :int<U>) :int<U>
{
return a;
}


def adder<T>(x :int<T>, y :int<T>) :int<T>
{
return x + iden(y);
}

def my_concat<W, X, Z>(a :int<W>, b :int<X>, c :int<Z>) :int<W + X + Z>
{
tmp = a ++ b ++ c;
other = b ++ a ++ c;
return tmp + other;
}

def indexing<G, H>(x :int<G>, y :int<H>) :int<5>
{
tmp = (x ++ y){4:0};
return (x ++ y){4:0};
}

def in_scope<I>(x :int<I>) :int<1>
{
return x{I - 1:I - 1};
}


def infty(x :int<5>, y :int<5>) :int<10>
{
return x ++ y;
}

pipe test1(inpt: int<32>)[rf: int<32>[32]] :int<100>
{
output (adder(3, 4));
// def error(inpt :int<10>) :bool
// {
// shamt = cast(inpt{4:0}, uint<5>);
// return true;
// }

// def extract(inpt :uint<32>) :bool
// {
// uint<5> a = inpt{0:4};
// return true;
// }

// def iden<U>(a :int<U>) :int<U>
// {
// return a;
// }


// def adder<T>(x :int<T>, y :int<T>) :int<T>
// {
// return x + iden(y);
// }

// def my_concat<W, X, Z>(a :int<W>, b :int<X>, c :int<Z>) :int<W + X + Z>
// {
// tmp = a ++ b ++ c;
// other = b ++ a ++ c;
// return tmp + other;
// }

// def indexing<G, H>(x :int<G>, y :int<H>) :int<5>
// {
// tmp = (x ++ y){4:0};
// return (x ++ y){4:0};
// }

// // def in_scope<I, J>(x :int<I>, y :int<J>) :int<1>
// // {
// // return x{J:0};
// // }


// def infty(x :int<5>, y :int<5>) :int<10>
// {
// return x ++ y;
// }

pipe test1(inpt: uint<32>)[rf: int<32>[32]] :int<100>
{
uint<5> a = inpt{0:4};
//output (adder(3, 4));
output (4);
}


Expand Down
11 changes: 9 additions & 2 deletions src/main/scala/pipedsl/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class Interpreter(val maxIterations: Int) {
var moduleCalls: ModuleCalls = immutable.Queue[Unit => Environment]()
var iterations: Int = 1

def interp_index(idx: EIndex, env:this.Environment) :Int = idx match {
case EIndConst(c) => c
case EIndVar(id) => env(id).asInstanceOf[Int]
case EIndAdd(l, r) => interp_index(l, env) + interp_index(r, env)
case EIndSub(l, r) => interp_index(l, env) - interp_index(r, env)
}

def interp_expr(e: Expr, env:Environment): Any = e match {
case i: EInt => i.v
case b: EBool => b.v
Expand Down Expand Up @@ -54,8 +61,8 @@ class Interpreter(val maxIterations: Int) {
}
case be: EBitExtract => {
val n = interp_expr(be.num, env).asInstanceOf[Int]
val start = be.start
val end = be.end
val start = interp_index(be.start, env)
val end = interp_index(be.end, env)
if (start > end) throw Errors.InvalidBitExtraction(start, end)
val mask = (~0) << (31 - end) >>> (31 - (end-start)) << start
(mask & n) >>> start
Expand Down
35 changes: 28 additions & 7 deletions src/main/scala/pipedsl/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import common.Syntax._
import common.Locks._
import pipedsl.common.LockImplementation
import pipedsl.common.Syntax.Latency.Latency
import pipedsl.common.Utilities.generic_type_prefix
import pipedsl.common.Utilities.{generic_type_prefix, opt_func}

import scala.util.matching.Regex

Expand Down Expand Up @@ -126,8 +126,29 @@ class Parser(rflockImpl: String) extends RegexParsers with PackratParsers {
iden ~ angular("atomic" | "a").? ~ brackets(expr ~ ("," ~> expr).?) ^^ { case m ~ a ~ (i ~ n) => EMemAccess(m, i, n, None, None, a.isDefined) }
}


lazy val indexAtom :P[EIndex] = dlog(positioned
{
iden ^^ { id => EIndVar(id).setType(TInteger()) } |
posint ^^ { n => EIndConst(n).setType(TInteger()) }

})("index atom")


lazy val index :P[EIndex] = positioned(
indexAtom ~ rep(("+" | "-") ~ indexAtom) ^^ {case fst ~ lst =>
//1 + 2 - 3
//1
//(1 + 2)
//(1 + 2) - 3
lst.foldLeft(fst)((idx, op) =>
if (op._1 == "+") EIndAdd(idx, op._2).setType(TInteger())
else EIndSub(idx, op._2).setType(TInteger()))
}
)

lazy val bitAccess: P[Expr] = positioned {
expr ~ braces(posint ~ ":" ~ posint) ^^ { case n ~ (e ~ _ ~ s) => EBitExtract(n, s, e) }
expr ~ braces(index ~ ":" ~ index) ^^ { case n ~ (e ~ _ ~ s) => EBitExtract(n, s, e) }
}


Expand Down Expand Up @@ -231,9 +252,9 @@ class Parser(rflockImpl: String) extends RegexParsers with PackratParsers {

lazy val lhs: Parser[Expr] = memAccess | variable

lazy val simpleCmd: P[Command] = dlog(positioned {
lazy val simpleCmd: P[Command] = positioned {
speccall |
dlog(typ.? ~ variable ~ "=" ~ expr ^^ { case t ~ n ~ _ ~ r => n.typ = t; CAssign(n, r) })("assign") |
typ.? ~ variable ~ "=" ~ expr ^^ { case t ~ n ~ _ ~ r => n.typ = t; CAssign(n, r) } |
typ.? ~ lhs ~ "<-" ~ expr ^^ { case t ~ l ~ _ ~ r => l.typ = t; CRecv(l, r) } |
check |
resolveSpec |
Expand All @@ -247,7 +268,7 @@ class Parser(rflockImpl: String) extends RegexParsers with PackratParsers {
"return" ~> expr ^^ (e => CReturn(e)) |
"output" ~> expr ^^ (e => { COutput(e)}) |
expr ^^ (e => { CExpr(e)})
})("simple command")
}

lazy val lockArg: P[LockArg] = positioned {
iden ~ brackets(variable).? ^^ {case i ~ v => LockArg(i, v)}
Expand Down Expand Up @@ -333,8 +354,8 @@ class Parser(rflockImpl: String) extends RegexParsers with PackratParsers {
cmd <~ "---" ^^ {c => CTBar(c, CEmpty())} |
seqCmd } )

lazy val bitWidthAtom :P[TBitWidth] = dlog(iden ^^ {id => TBitWidthVar(Id(generic_type_prefix + id.v))} |
posint ^^ {i => TBitWidthLen(i)})("parsed bit width")
lazy val bitWidthAtom :P[TBitWidth] = iden ^^ {id => TBitWidthVar(Id(generic_type_prefix + id.v))} |
posint ^^ {i => TBitWidthLen(i)}

lazy val bitWidth :P[TBitWidth] =
repsep(bitWidthAtom, "+") ^^
Expand Down
9 changes: 8 additions & 1 deletion src/main/scala/pipedsl/codegen/bsv/BSVPrettyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ object BSVPrettyPrinter {
case _ => throw BaseError(base)
}

private def toBSVIndString(index: BIndex) :String = index match {
case BIndConst(n) => n.toString
case BIndVar(v) => v
case BIndSub(l, r) => s"(${toBSVIndString(l)} - ${toBSVIndString(r)})"
case BIndAdd(l, r) => s"(${toBSVIndString(l)} + ${toBSVIndString(r)})"
}

private def toBSVExprStr(expr: BExpr): String = expr match {
case BIsValid(exp) => mkExprString("isValid(",toBSVExprStr(exp), ")")
case BFromMaybe(d, exp) => mkExprString("fromMaybe(",toBSVExprStr(d), ",", toBSVExprStr(exp), ")")
Expand Down Expand Up @@ -107,7 +114,7 @@ object BSVPrettyPrinter {
//TODO incorporate bit types into the typesystem properly
//and then remove the custom pack/unpack operations
case BBitExtract(expr, start, end) => mkExprString(toBSVExprStr(expr),
"[", end.toString, ":", start.toString, "]"
"[", toBSVIndString(end), ":", toBSVIndString(start), "]"
)
case BConcat(first, rest) =>
val exprstr = rest.foldLeft[String](toBSVExprStr(first))((s, e) => {
Expand Down
20 changes: 17 additions & 3 deletions src/main/scala/pipedsl/codegen/bsv/BSVSyntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ object BSVSyntax {
toVar(v.id)
}

def toBSVIndex(index: EIndex) :BIndex = index match
{
case EIndConst(v) => BIndConst(v)
case EIndAdd(l, r) => BIndAdd(toBSVIndex(l), toBSVIndex(r))
case EIndSub(l, r) => BIndSub(toBSVIndex(l), toBSVIndex(r))
case EIndVar(id) => BIndVar(id.v)
}

def toExpr(e: Expr): BExpr = e match {
case EInt(v, base, bits) => BIntLit(v, base, bits)
case EBool(v) => BBoolLit(v)
Expand All @@ -150,8 +158,8 @@ object BSVSyntax {
val bnum = toExpr(num)
//remove nested pack/unpacks
bnum match {
case BUnpack(e) => BUnpack(BBitExtract(e, start, end))
case e => BUnpack(BBitExtract(BPack(e), start, end))
case BUnpack(e) => BUnpack(BBitExtract(e, toBSVIndex(start), toBSVIndex(end)))
case e => BUnpack(BBitExtract(BPack(e), toBSVIndex(start), toBSVIndex(end)))
}
case ETernary(cond, tval, fval) => BTernaryExpr(toExpr(cond), toExpr(tval), toExpr(fval))
case e@EVar(_) => toVar(e)
Expand Down Expand Up @@ -401,13 +409,19 @@ object BSVSyntax {
case class BVar(name: String, typ: BSVType) extends BExpr
case class BBOp(op: String, lhs: BExpr, rhs: BExpr, isInfix: Boolean = true) extends BExpr
case class BUOp(op: String, expr: BExpr) extends BExpr
case class BBitExtract(expr: BExpr, start: Int, end: Int) extends BExpr
case class BBitExtract(expr: BExpr, start: BIndex, end: BIndex) extends BExpr
case class BConcat(first: BExpr, rest: List[BExpr]) extends BExpr
case class BModule(name: String, args: List[BExpr] = List()) extends BExpr
case class BMethodInvoke(mod: BExpr, method: String, args: List[BExpr]) extends BExpr
case class BFuncCall(func: String, args: List[BExpr]) extends BExpr
case class BValueOf(s :String) extends BExpr

sealed trait BIndex
case class BIndConst(n :Int) extends BIndex
case class BIndVar(v :String) extends BIndex
case class BIndAdd(l :BIndex, r :BIndex) extends BIndex
case class BIndSub(l :BIndex, r :BIndex) extends BIndex

sealed trait BStatement {
var useLet: Boolean = false
def setUseLet(b: Boolean): BStatement = { this.useLet = b; this }
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/pipedsl/common/Errors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object Errors {
withPos(s"$i is not a valid lock implementation name", i.pos)
)

case class InvalidBitExtraction(s: Int, e: Int) extends RuntimeException(
case class InvalidBitExtraction(s: Any, e: Any) extends RuntimeException(
s"Start: $s must be less than or equal to End: $e"
)
case class AlreadySetException(id: Id) extends RuntimeException(
Expand Down
Loading

0 comments on commit 11c29e3

Please sign in to comment.