Skip to content

Commit

Permalink
early try at repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
calebmkim committed Apr 1, 2024
1 parent 9ec9a58 commit 7041748
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
8 changes: 7 additions & 1 deletion src/main/scala/backends/calyx/Ast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,15 @@ object Calyx:
else
space <> text("else") <+> scope(falseBr.doc)
)
case While(port, cond, body) =>
case While(port, cond, body) => {
text("while") <+> port.doc() <+> text("with") <+>
cond.doc() <+>
scope(body.doc(meta))
}
case While(count, body) => {
text("repeat") <+> count.toString <+>
scope(body.doc(meta))
}
case e @ Enable(id) => {
emitPos(e.pos, e.span) <> id.doc() <> semi
}
Expand Down Expand Up @@ -340,6 +345,7 @@ object Calyx:
case class If(port: Port, cond: CompVar, trueBr: Control, falseBr: Control)
extends Control
case class While(port: Port, cond: CompVar, body: Control) extends Control
case class Repeat(count: Int, body: Control) extends Control
case class Enable(id: CompVar) extends Control with Syntax.PositionalWithSpan
case class Invoke(
id: CompVar,
Expand Down
38 changes: 22 additions & 16 deletions src/main/scala/backends/calyx/Backend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -936,23 +936,29 @@ private class CalyxBackendHelper {
}
case CEmpty => (List(), Empty, store)
case wh @ CWhile(cond, _, body) => {
val condOut = emitExpr(cond)
val groupName = genName("cond")
assertOrThrow(
!condOut.done.isDefined,
BackendError("Loop condition is non-combinational")
)
val (condGroup, condDefs) =
Group.fromStructure(
groupName,
condOut.structure,
condOut.delay,
true
if wh.attributes.contains("bound") then {
val (bodyStruct, bodyCon, st) = emitCmd(body)
val control = Repeat(10, bodyCon)
(bodyStruct, control, st)
} else {
val condOut = emitExpr(cond)
val groupName = genName("cond")
assertOrThrow(
!condOut.done.isDefined,
BackendError("Loop condition is non-combinational")
)
val (bodyStruct, bodyCon, st) = emitCmd(body)
val control = While(condOut.port, condGroup.id, bodyCon)
control.attributes = wh.attributes
(condGroup :: bodyStruct ++ condDefs, control, st)
val (condGroup, condDefs) =
Group.fromStructure(
groupName,
condOut.structure,
condOut.delay,
true
)
val (bodyStruct, bodyCon, st) = emitCmd(body)
val control = While(condOut.port, condGroup.id, bodyCon)
control.attributes = wh.attributes
(condGroup :: bodyStruct ++ condDefs, control, st)
}
}
case _: CFor =>
throw BackendError(
Expand Down

0 comments on commit 7041748

Please sign in to comment.