Skip to content

Commit

Permalink
Nextifying support in chisel3
Browse files Browse the repository at this point in the history
  • Loading branch information
aferr committed Aug 15, 2017
1 parent 51afc8d commit 9844676
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 2 deletions.
12 changes: 10 additions & 2 deletions chiselFrontend/src/main/scala/chisel3/core/Bits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package chisel3.core
import scala.language.experimental.macros

import chisel3.internal._
import chisel3.internal.Builder.{pushCommand, pushOp, pushDeclass, pushEndorse}
import chisel3.internal.Builder.{pushCommand, pushOp, pushDeclass, pushEndorse, pushNext}
import chisel3.internal.firrtl._
import chisel3.internal.sourceinfo.{SourceInfo, DeprecatedSourceInfo, SourceInfoTransform, SourceInfoWhiteboxTransform,
UIntTransform, MuxTransform, DeclassifyTransform, EndorseTransform}
UIntTransform, MuxTransform, DeclassifyTransform, EndorseTransform, NextTransform}
import chisel3.internal.firrtl.PrimOp._
// TODO: remove this once we have CompileOptions threaded through the macro system.
import chisel3.core.ExplicitCompileOptions.NotStrict
Expand Down Expand Up @@ -790,6 +790,14 @@ object Endorse {
}
}

object Next {
def apply[T <: Data](arg: T): T = macro NextTransform.apply[T]
def do_apply[T <: Data](arg: T)(implicit sourceInfo: SourceInfo): T = {
val dtype = arg.cloneType
pushNext(DefNext(sourceInfo, dtype, arg.ref))
}
}

object Mux {
/** Creates a mux, whose output is one of the inputs depending on the
* value of the condition.
Expand Down
6 changes: 6 additions & 0 deletions chiselFrontend/src/main/scala/chisel3/internal/Builder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ private[chisel3] object Builder {
pushCommand(cmd).id
}

def pushNext[ T <: Data](cmd: DefNext[T]): T = {
// Bind each element of the returned Data to being a Op
Binding.bind(cmd.id, OpBinder(forcedModule), "Error: During op creation, fresh result")
pushCommand(cmd).id
}

def errors: ErrorLog = dynamicContext.errors
def error(m: => String): Unit = errors.error(m)
def warning(m: => String): Unit = errors.warning(m)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ abstract class Definition extends Command {
case class DefPrim[T <: Data](sourceInfo: SourceInfo, id: T, op: PrimOp, args: Arg*) extends Definition
case class DefDeclass[T <: Data](sourceInfo: SourceInfo, id: T, arg: Arg, lbl: Label) extends Definition
case class DefEndorse[T <: Data](sourceInfo: SourceInfo, id: T, arg: Arg, lbl: Label) extends Definition
case class DefNext[T <: Data](sourceInfo: SourceInfo, id: T, arg: Arg) extends Definition
case class DefInvalid(sourceInfo: SourceInfo, arg: Arg) extends Command
case class DefWire(sourceInfo: SourceInfo, id: Data, lbl: Label) extends Definition
case class DefReg(sourceInfo: SourceInfo, id: Data, clock: Arg, lbl: Label) extends Definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ class EndorseTransform(val c: Context) extends SourceInfoTransformMacro {
}
}

class NextTransform(val c: Context) extends SourceInfoTransformMacro {
import c.universe._
def apply[T: c.WeakTypeTag](arg: c.Tree): c.Tree = {
val tpe = weakTypeOf[T]
q"$thisObj.do_apply[$tpe]($arg)($implicitSourceInfo)"
}
}

class MuxTransform(val c: Context) extends SourceInfoTransformMacro {
import c.universe._
def apply[T: c.WeakTypeTag](cond: c.Tree, con: c.Tree, alt: c.Tree): c.Tree = {
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/chisel3/compatibility.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package object Chisel { // scalastyle:ignore package.object.name
val MeetLabelComp = chisel3.core.MeetLabelComp
val Declassify = chisel3.core.Declassify
val Endorse = chisel3.core.Endorse
val Next = chisel3.core.Next


// Not originally part of compatibility.scala
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/chisel3/internal/firrtl/Emitter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ private class Emitter(circuit: Circuit, compileTopOnly: Boolean) {
case e: DefEndorse[_] =>
val lbl_s = e.lbl.fullName(ctx)
s"node ${e.name} ${lbl_s} = endorse(${e.arg.fullName(ctx)}, ${lbl_s})"
case e: DefNext[_] =>
s"node ${e.name} = next(${e.arg.fullName(ctx)})"
case e: DefWire => s"wire ${e.name} : ${e.lbl.fullName(ctx)}${e.id.toType(ctx)}"
case e: DefReg => s"reg ${e.name} : ${e.lbl.fullName(ctx)}${e.id.toType(ctx)}, ${e.clock.fullName(ctx)}"
case e: DefRegInit => s"reg ${e.name} : ${e.lbl.fullName(ctx)}${e.id.toType(ctx)}, ${e.clock.fullName(ctx)} with : (reset => (${e.reset.fullName(ctx)}, ${e.init.fullName(ctx)}))"
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/chisel3/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ package object chisel3 { // scalastyle:ignore package.object.name
val MeetLabelComp = chisel3.core.MeetLabelComp
val Declassify = chisel3.core.Declassify
val Endorse = chisel3.core.Endorse
val Next = chisel3.core.Next

// Components
val C = chisel3.core.C
Expand Down

0 comments on commit 9844676

Please sign in to comment.