Skip to content

Commit bbcc405

Browse files
[Scala3] Scala2 compatibility changes (#4483)
1 parent eb31402 commit bbcc405

File tree

21 files changed

+236
-152
lines changed

21 files changed

+236
-152
lines changed

core/src/main/scala-2/chisel3/Aggregate.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ sealed class Vec[T <: Data] private[chisel3] (gen: => T, length: Int)
7474
): T = _reduceTreeImpl(redOp, layerOp)
7575
}
7676

77+
object Vec extends VecFactory
78+
7779
object VecInit extends VecInitImpl with SourceInfoDoc {
7880

7981
/** Creates a new [[Vec]] composed of elements of the input Seq of [[Data]]

core/src/main/scala-2/chisel3/Bits.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ sealed abstract class Bits(private[chisel3] val width: Width) extends BitsImpl w
282282
def do_##(that: Bits)(implicit sourceInfo: SourceInfo): UInt = _impl_##(that)
283283
}
284284

285+
object Bits extends UIntFactory
286+
285287
/** A data type for unsigned integers, represented as a binary bitvector. Defines arithmetic operations between other
286288
* integer types.
287289
*
@@ -553,6 +555,8 @@ sealed class UInt private[chisel3] (width: Width) extends Bits(width) with UIntI
553555
override def do_asSInt(implicit sourceInfo: SourceInfo): SInt = _asSIntImpl
554556
}
555557

558+
object UInt extends UIntFactory
559+
556560
/** A data type for signed integers, represented as a binary bitvector. Defines arithmetic operations between other
557561
* integer types.
558562
*
@@ -734,6 +738,8 @@ sealed class SInt private[chisel3] (width: Width) extends Bits(width) with SIntI
734738
override def do_asSInt(implicit sourceInfo: SourceInfo): SInt = _asSIntImpl
735739
}
736740

741+
object SInt extends SIntFactory
742+
737743
sealed trait Reset extends ResetImpl with ToBoolable {
738744

739745
/** Casts this $coll to an [[AsyncReset]] */
@@ -876,3 +882,5 @@ sealed class Bool() extends UInt(1.W) with BoolImpl with Reset {
876882
/** @group SourceInfoTransformMacro */
877883
def do_asAsyncReset(implicit sourceInfo: SourceInfo): AsyncReset = _asAsyncResetImpl
878884
}
885+
886+
object Bool extends BoolFactory

core/src/main/scala/chisel3/Printf.scala renamed to core/src/main/scala-2/chisel3/Printf.scala

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,11 @@ import scala.language.experimental.macros
1313
*
1414
* See apply methods for use
1515
*/
16-
object printf {
16+
object printf extends PrintfImpl {
1717

1818
/** Named class for [[printf]]s. */
1919
final class Printf private[chisel3] (val pable: Printable) extends VerificationStatement
2020

21-
/** Helper for packing escape characters */
22-
private[chisel3] def format(formatIn: String): String = {
23-
require(formatIn.forall(c => c.toInt > 0 && c.toInt < 128), "format strings must comprise non-null ASCII values")
24-
def escaped(x: Char) = {
25-
require(x.toInt >= 0, s"char ${x} to Int ${x.toInt} must be >= 0")
26-
if (x == '"' || x == '\\') {
27-
s"\\${x}"
28-
} else if (x == '\n') {
29-
"\\n"
30-
} else if (x == '\t') {
31-
"\\t"
32-
} else {
33-
require(
34-
x.toInt >= 32,
35-
s"char ${x} to Int ${x.toInt} must be >= 32"
36-
) // TODO \xNN once FIRRTL issue #59 is resolved
37-
x
38-
}
39-
}
40-
formatIn.map(escaped).mkString("")
41-
}
42-
4321
/** Prints a message in simulation
4422
*
4523
* Prints a message every cycle. If defined within the scope of a [[when]] block, the message

core/src/main/scala/chisel3/VerificationStatement.scala renamed to core/src/main/scala-2/chisel3/VerificationStatement.scala

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,9 @@
33
package chisel3
44

55
import chisel3.experimental.{BaseModule, SourceInfo}
6-
import chisel3.internal._
7-
import chisel3.internal.Builder.pushCommand
8-
import chisel3.internal.firrtl.ir._
9-
import chisel3.layer.block
106

117
import scala.language.experimental.macros
128

13-
/** Base class for all verification statements: Assert, Assume, Cover, Stop and Printf. */
14-
abstract class VerificationStatement extends NamedComponent {
15-
_parent.foreach(_.addId(this))
16-
}
17-
189
/** Scaladoc information for internal verification statement macros
1910
* that are used in objects assert, assume and cover.
2011
*
@@ -29,7 +20,7 @@ abstract class VerificationStatement extends NamedComponent {
2920
*/
3021
trait VerifPrintMacrosDoc
3122

32-
object assert extends VerifPrintMacrosDoc {
23+
object assert extends assertImpl with VerifPrintMacrosDoc {
3324

3425
/** Named class for assertions. */
3526
final class Assert private[chisel3] () extends VerificationStatement
@@ -79,15 +70,9 @@ object assert extends VerifPrintMacrosDoc {
7970

8071
def apply(cond: Bool)(implicit sourceInfo: SourceInfo): Assert =
8172
macro VerifStmtMacrosCompat.assert._applyMacroWithNoMessage
82-
83-
/** An elaboration-time assertion. Calls the built-in Scala assert function. */
84-
def apply(cond: Boolean, message: => String): Unit = Predef.assert(cond, message)
85-
86-
/** An elaboration-time assertion. Calls the built-in Scala assert function. */
87-
def apply(cond: Boolean): Unit = Predef.assert(cond, "")
8873
}
8974

90-
object assume extends VerifPrintMacrosDoc {
75+
object assume extends assumeImpl with VerifPrintMacrosDoc {
9176

9277
/** Named class for assumptions. */
9378
final class Assume private[chisel3] () extends VerificationStatement
@@ -141,12 +126,6 @@ object assume extends VerifPrintMacrosDoc {
141126

142127
def apply(cond: Bool)(implicit sourceInfo: SourceInfo): Assume =
143128
macro VerifStmtMacrosCompat.assume._applyMacroWithNoMessage
144-
145-
/** An elaboration-time assumption. Calls the built-in Scala assume function. */
146-
def apply(cond: Boolean, message: => String): Unit = Predef.assume(cond, message)
147-
148-
/** An elaboration-time assumption. Calls the built-in Scala assume function. */
149-
def apply(cond: Boolean): Unit = Predef.assume(cond, "")
150129
}
151130

152131
object cover extends VerifPrintMacrosDoc {
@@ -171,37 +150,3 @@ object cover extends VerifPrintMacrosDoc {
171150
def apply(cond: Bool)(implicit sourceInfo: SourceInfo): Cover =
172151
macro VerifStmtMacrosCompat.cover._applyMacroWithNoMessage
173152
}
174-
175-
object stop {
176-
177-
/** Terminate execution, indicating success and printing a message.
178-
*
179-
* @param message a message describing why simulation was stopped
180-
*/
181-
def apply(message: String)(implicit sourceInfo: SourceInfo): Stop = buildStopCommand(Some(PString(message)))
182-
183-
/** Terminate execution, indicating success and printing a message.
184-
*
185-
* @param message a printable describing why simulation was stopped
186-
*/
187-
def apply(message: Printable)(implicit sourceInfo: SourceInfo): Stop = buildStopCommand(Some(message))
188-
189-
/** Terminate execution, indicating success.
190-
*/
191-
def apply()(implicit sourceInfo: SourceInfo): Stop = buildStopCommand(None)
192-
193-
/** Named class for [[stop]]s. */
194-
final class Stop private[chisel3] () extends VerificationStatement
195-
196-
private def buildStopCommand(message: Option[Printable])(implicit sourceInfo: SourceInfo): Stop = {
197-
val stopId = new Stop()
198-
block(layers.Verification, skipIfAlreadyInBlock = true, skipIfLayersEnabled = true) {
199-
message.foreach(Printable.checkScope(_))
200-
when(!Module.reset.asBool) {
201-
message.foreach(PrintfMacrosCompat.printfWithoutReset(_))
202-
pushCommand(Stop(stopId, sourceInfo, Builder.forcedClock.ref, 0))
203-
}
204-
}
205-
stopId
206-
}
207-
}

core/src/main/scala-2/chisel3/experimental/dataview/ChiselSubtypeOf.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import scala.reflect.macros.blackbox.Context
2828
sealed trait ChiselSubtypeOf[A, B]
2929

3030
object ChiselSubtypeOf {
31+
// TODO return an empty tree here instead of a quasiquote for scala3 compatibility
3132
def genChiselSubtypeOf[A: c.WeakTypeTag, B: c.WeakTypeTag](c: Context): c.Tree = {
3233
import c.universe._
3334

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package chisel3.experimental.dataview
4+
5+
import chisel3._
6+
import scala.reflect.runtime.universe.WeakTypeTag
7+
8+
private[chisel3] trait InvertibleDataView {
9+
def swapArgs[A, B, C, D](f: (A, B) => Iterable[(C, D)]): (B, A) => Iterable[(D, C)] = {
10+
case (b, a) => f(a, b).map(_.swap)
11+
}
12+
13+
/** Provides `invert` for invertible [[DataView]]s
14+
*
15+
* This must be done as an extension method because it applies an addition constraint on the `Target`
16+
* type parameter, namely that it must be a subtype of [[Data]].
17+
*
18+
* @note [[PartialDataView]]s are **not** invertible and will result in an elaboration time exception
19+
*/
20+
implicit class InvertibleDataView[T <: Data: WeakTypeTag, V <: Data: WeakTypeTag](view: DataView[T, V]) {
21+
def invert(mkView: V => T): DataView[V, T] = {
22+
// It would've been nice to make this a compiler error, but it's unclear how to make that work.
23+
// We tried having separate TotalDataView and PartialDataView and only defining inversion for
24+
// TotalDataView. For some reason, implicit resolution wouldn't invert TotalDataViews. This is
25+
// probably because it was looking for the super-type DataView and since invertDataView was
26+
// only defined on TotalDataView, it wasn't included in implicit resolution. Thus we end up
27+
// with a runtime check.
28+
if (!view.total) {
29+
val tt = implicitly[WeakTypeTag[T]].tpe
30+
val vv = implicitly[WeakTypeTag[V]].tpe
31+
val msg = s"Cannot invert '$view' as it is non-total.\n Try providing a DataView[$vv, $tt]." +
32+
s"\n Please see https://www.chisel-lang.org/chisel3/docs/explanations/dataview."
33+
throw InvalidViewException(msg)
34+
}
35+
implicit val sourceInfo = view.sourceInfo
36+
new DataView[V, T](mkView, swapArgs(view.mapping), view.total)
37+
}
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package chisel3.experimental
2+
3+
/** Classes or traits which will be used with the [[Definition]] + [[Instance]] api should be marked
4+
* with the [[instantiable]] annotation at the class/trait definition.
5+
*
6+
* @example {{{
7+
* @instantiable
8+
* class MyModule extends Module {
9+
* ...
10+
* }
11+
*
12+
* val d = Definition(new MyModule)
13+
* val i0 = Instance(d)
14+
* val i1 = Instance(d)
15+
* }}}
16+
*/
17+
class instantiable extends chisel3.internal.instantiable
18+
19+
/** Classes marked with [[instantiable]] can have their vals marked with the [[public]] annotation to
20+
* enable accessing these values from a [[Definition]] or [[Instance]] of the class.
21+
*
22+
* Only vals of the the following types can be marked [[public]]:
23+
* 1. IsInstantiable
24+
* 2. IsLookupable
25+
* 3. Data
26+
* 4. BaseModule
27+
* 5. Iterable/Option containing a type that meets these requirements
28+
* 6. Basic type like String, Int, BigInt etc.
29+
*
30+
* @example {{{
31+
* @instantiable
32+
* class MyModule extends Module {
33+
* @public val in = IO(Input(UInt(3.W)))
34+
* @public val out = IO(Output(UInt(3.W)))
35+
* ..
36+
* }
37+
*
38+
* val d = Definition(new MyModule)
39+
* val i0 = Instance(d)
40+
* val i1 = Instance(d)
41+
*
42+
* i1.in := i0.out
43+
* }}}
44+
*/
45+
class public extends chisel3.internal.public

core/src/main/scala-2/chisel3/experimental/hierarchy/Instantiate.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ object Instantiate extends InstantiateImpl {
5454
f: K => A
5555
)(
5656
implicit sourceInfo: SourceInfo
57-
): Instance[A] = _instanceImpl(args, f)
57+
): Instance[A] = _instanceImpl(args, f, implicitly[ru.WeakTypeTag[A]])
5858

5959
/** This is not part of the public API, do not call directly! */
6060
def _definition[K, A <: BaseModule: ru.WeakTypeTag](
6161
args: K,
6262
f: K => A
63-
): Definition[A] = _definitionImpl(args, f)
63+
): Definition[A] = _definitionImpl(args, f, implicitly[ru.WeakTypeTag[A]])
6464

6565
private object internal {
6666

core/src/main/scala-2/chisel3/internal/SourceInfoMacro.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package chisel3.internal.sourceinfo
55
import scala.language.experimental.macros
66
import scala.reflect.macros.blackbox.Context
77

8+
import chisel3.internal.sourceinfo.SourceInfoFileResolver
9+
810
/** Provides a macro that returns the source information at the invocation point.
911
*/
1012
@deprecated("Public APIs in chisel3.internal are deprecated", "Chisel 3.6")
11-
object SourceInfoMacro {
13+
private[chisel3] object SourceInfoMacro {
1214
def generate_source_info(c: Context): c.Tree = {
1315
import c.universe._
1416
val p = c.enclosingPosition

0 commit comments

Comments
 (0)