Skip to content

Commit

Permalink
Merge branch 'master' into unary-not-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
seldridge authored Jan 17, 2019
2 parents 75caf7e + 685790b commit abdc1c3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 43 deletions.
18 changes: 1 addition & 17 deletions chiselFrontend/src/main/scala/chisel3/core/Module.scala
Original file line number Diff line number Diff line change
Expand Up @@ -225,30 +225,14 @@ abstract class BaseModule extends HasId {
}
}

/** Recursively suggests names to supported "container" classes
* Arbitrary nestings of supported classes are allowed so long as the
* innermost element is of type HasId
* (Note: Map is Iterable[Tuple2[_,_]] and thus excluded)
*/
def nameRecursively(prefix: String, nameMe: Any): Unit =
nameMe match {
case (id: HasId) => name(id, prefix)
case Some(elt) => nameRecursively(prefix, elt)
case (iter: Iterable[_]) if iter.hasDefiniteSize =>
for ((elt, i) <- iter.zipWithIndex) {
nameRecursively(s"${prefix}_${i}", elt)
}
case _ => // Do nothing
}

/** Scala generates names like chisel3$util$Queue$$ram for private vals
* This extracts the part after $$ for names like this and leaves names
* without $$ unchanged
*/
def cleanName(name: String): String = name.split("""\$\$""").lastOption.getOrElse(name)

for (m <- getPublicFields(rootClass)) {
nameRecursively(cleanName(m.getName), m.invoke(this))
Builder.nameRecursively(cleanName(m.getName), m.invoke(this), name)
}

names
Expand Down
15 changes: 15 additions & 0 deletions chiselFrontend/src/main/scala/chisel3/internal/Builder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,21 @@ private[chisel3] object Builder {
lastStack
}

/** Recursively suggests names to supported "container" classes
* Arbitrary nestings of supported classes are allowed so long as the
* innermost element is of type HasId
* (Note: Map is Iterable[Tuple2[_,_]] and thus excluded)
*/
def nameRecursively(prefix: String, nameMe: Any, namer: (HasId, String) => Unit): Unit = nameMe match {
case (id: HasId) => namer(id, prefix)
case Some(elt) => nameRecursively(prefix, elt, namer)
case (iter: Iterable[_]) if iter.hasDefiniteSize =>
for ((elt, i) <- iter.zipWithIndex) {
nameRecursively(s"${prefix}_${i}", elt, namer)
}
case _ => // Do nothing
}

def errors: ErrorLog = dynamicContext.errors
def error(m: => String): Unit = if (dynamicContextVar.value.isDefined) errors.error(m)
def warning(m: => String): Unit = if (dynamicContextVar.value.isDefined) errors.warning(m)
Expand Down
5 changes: 1 addition & 4 deletions chiselFrontend/src/main/scala/chisel3/internal/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ class NamingContext {
closed = true
for ((ref, suffix) <- items) {
// First name the top-level object
ref match {
case nameable: chisel3.internal.HasId => nameable.suggestName(prefix + suffix)
case _ =>
}
chisel3.internal.Builder.nameRecursively(prefix + suffix, ref, (id, name) => id.suggestName(name))

// Then recurse into descendant contexts
if (descendants.containsKey(ref)) {
Expand Down
47 changes: 25 additions & 22 deletions src/test/resources/chisel3/VerilogVendingMachine.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,38 @@ module VerilogVendingMachine(
);
parameter sIdle = 3'd0, s5 = 3'd1, s10 = 3'd2, s15 = 3'd3, sOk = 3'd4;
reg [2:0] state;
wire [2:0] next_state;

assign dispense = (state == sOk) ? 1'd1 : 1'd0;

always @(*) begin
case (state)
sIdle: if (nickel) next_state <= s5;
else if (dime) next_state <= s10;
else next_state <= state;
s5: if (nickel) next_state <= s10;
else if (dime) next_state <= s15;
else next_state <= state;
s10: if (nickel) next_state <= s15;
else if (dime) next_state <= sOk;
else next_state <= state;
s15: if (nickel) next_state <= sOk;
else if (dime) next_state <= sOk;
else next_state <= state;
sOk: next_state <= sIdle;
endcase
end

// Go to next state
always @(posedge clock) begin
if (reset) begin
state <= sIdle;
end else begin
state <= next_state;
case (state)
sIdle: begin
if (nickel) state <= s5;
else if (dime) state <= s10;
else state <= state;
end
s5: begin
if (nickel) state <= s10;
else if (dime) state <= s15;
else state <= state;
end
s10: begin
if (nickel) state <= s15;
else if (dime) state <= sOk;
else state <= state;
end
s15: begin
if (nickel) state <= sOk;
else if (dime) state <= sOk;
else state <= state;
end
sOk: begin
state <= sIdle;
end
endcase
end
end
endmodule

5 changes: 5 additions & 0 deletions src/test/scala/chiselTests/NamingAnnotationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class NamedModule extends NamedModuleTester {
val myA = expectName(1.U + myNested, "test_myA")
val myB = expectName(myA +& 2.U, "test_myB")
val myC = expectName(myB +& 3.U, "test_myC")

val myD = Seq(myC +& 1.U, myC +& 2.U)
for ((d, i) <- myD.zipWithIndex)
expectName(d, s"test_myD_$i")

myC +& 4.U // named at enclosing scope
}

Expand Down

0 comments on commit abdc1c3

Please sign in to comment.