Skip to content

Commit 48cef76

Browse files
author
Oron Port
committed
refactor annotations and split between IR and FE
1 parent 320aa25 commit 48cef76

File tree

20 files changed

+199
-124
lines changed

20 files changed

+199
-124
lines changed

compiler/ir/src/main/scala/dfhdl/compiler/ir/DFMember.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dfhdl.compiler
22
package ir
33
import dfhdl.internals.*
44
import upickle.default.*
5-
import annotation.tailrec
5+
import scala.annotation.tailrec
66
import scala.collection.immutable.ListMap
77
import scala.reflect.{ClassTag, classTag}
88

@@ -1480,9 +1480,9 @@ final case class DomainBlock(
14801480
tags: DFTags
14811481
) extends DFBlock,
14821482
DFDomainOwner derives ReadWriter:
1483-
def flattenMode: dfhdl.hw.annotation.flattenMode = meta.annotations.collectFirst {
1484-
case fm: dfhdl.hw.annotation.flattenMode => fm
1485-
}.getOrElse(dfhdl.hw.annotation.flattenMode.defaultPrefixUnderscore)
1483+
def flattenMode: annotation.FlattenMode = meta.annotations.collectFirst {
1484+
case fm: annotation.FlattenMode => fm
1485+
}.getOrElse(annotation.FlattenMode.defaultPrefixUnderscore)
14861486
protected def `prot_=~`(that: DFMember)(using MemberGetSet): Boolean = that match
14871487
case that: DomainBlock =>
14881488
this.domainType =~ that.domainType &&

compiler/ir/src/main/scala/dfhdl/compiler/ir/Meta.scala

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package dfhdl.compiler.ir
22
import dfhdl.internals.*
3-
import dfhdl.hw.annotation.{HWAnnotation, getActiveHWAnnotations}
43
import upickle.default.*
5-
import scala.annotation.Annotation
4+
import annotation.HWAnnotation
65

76
final case class Meta(
87
nameOpt: Option[String],
98
position: Position,
109
docOpt: Option[String],
1110
annotations: List[HWAnnotation]
12-
) derives CanEqual,
13-
ReadWriter:
11+
) derives CanEqual, ReadWriter:
1412
val isAnonymous: Boolean = nameOpt.isEmpty
1513
val name: String =
1614
nameOpt.getOrElse(s"anon${this.hashString}")
@@ -30,9 +28,3 @@ end Meta
3028
object Meta:
3129
given ReadWriter[Position] = macroRW
3230
def empty: Meta = Meta(None, Position.unknown, None, Nil)
33-
def gen(
34-
nameOpt: Option[String],
35-
position: Position,
36-
docOpt: Option[String],
37-
annotations: List[Annotation]
38-
): Meta = Meta(nameOpt, position, docOpt, annotations.getActiveHWAnnotations)
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,57 @@
1-
/** this package contains all the dfhdl hardware annotations
2-
*/
3-
package dfhdl.hw
4-
1+
package dfhdl.compiler.ir
52
import dfhdl.compiler.printing.{Printer, HasCodeString}
6-
import scala.annotation.StaticAnnotation
73
import dfhdl.internals.*
8-
import scala.annotation.Annotation
94
import upickle.default.*
105
import dfhdl.internals.StableEnum
11-
import dfhdl.compiler.ir.ConfigN
12-
import dfhdl.compiler.ir.{RateNumber, FreqNumber, TimeNumber}
136

147
object annotation:
15-
sealed abstract class HWAnnotation extends StaticAnnotation, Product, Serializable, HasCodeString
16-
derives CanEqual:
17-
val isActive: Boolean
8+
sealed abstract class HWAnnotation extends Product, Serializable, HasCodeString derives CanEqual
189

19-
extension (annotList: List[Annotation])
20-
def getActiveHWAnnotations: List[HWAnnotation] = annotList.collect {
21-
case annot: HWAnnotation if annot.isActive => annot
22-
}
2310
object HWAnnotation:
2411
given ReadWriter[HWAnnotation] = ReadWriter.merge(
25-
summon[ReadWriter[unused]],
26-
summon[ReadWriter[pure]],
27-
summon[ReadWriter[flattenMode]],
12+
summon[ReadWriter[Unused]],
13+
summon[ReadWriter[Pure.type]],
14+
summon[ReadWriter[FlattenMode]],
2815
summon[ReadWriter[constraints.Constraint]]
2916
)
3017

31-
sealed trait unused extends HWAnnotation derives ReadWriter
32-
object unused:
33-
/** `quiet` suppresses the unused warning for the tagged value.
34-
*/
35-
final case class quiet(isActive: Boolean) extends unused:
36-
def this() = this(true)
37-
def codeString(using Printer): String = "@hw.annotation.unused.quiet"
38-
39-
/** `keep` suppresses the unused warning, and also attempts to keep the tagged value.
40-
*/
41-
final case class keep(isActive: Boolean) extends unused:
42-
def this() = this(true)
43-
def codeString(using Printer): String = "@hw.annotation.unused.keep"
44-
45-
/** `prune` removes all the redundant paths until and including the tagged value.
46-
*/
47-
final case class prune(isActive: Boolean) extends unused:
48-
def this() = this(true)
49-
def codeString(using Printer): String = "@hw.annotation.unused.prune"
50-
end unused
18+
enum Unused extends HWAnnotation derives ReadWriter:
19+
case Quiet, Keep, Prune
20+
def codeString(using Printer): String =
21+
this match
22+
case Quiet => "@hw.annotation.unused.quiet"
23+
case Keep => "@hw.annotation.unused.keep"
24+
case Prune => "@hw.annotation.unused.prune"
5125

52-
final case class pure(isActive: Boolean) extends HWAnnotation derives ReadWriter:
53-
def this() = this(true)
26+
case object Pure extends HWAnnotation:
27+
given ReadWriter[Pure.type] = macroRW
5428
def codeString(using Printer): String = "@hw.annotation.pure"
5529

5630
/** Flattening Mode:
5731
* - transparent: $memberName
5832
* - prefix: $ownerName$sep$memberName
5933
* - suffix: $memberName$sep$ownerName
6034
*/
61-
enum flattenMode extends HWAnnotation, StableEnum derives CanEqual, ReadWriter:
62-
case transparent()
63-
case prefix(sep: String)
64-
case suffix(sep: String)
65-
val isActive: Boolean = true
35+
enum FlattenMode extends HWAnnotation, StableEnum derives ReadWriter:
36+
case Transparent
37+
case Prefix(sep: String)
38+
case Suffix(sep: String)
6639
def codeString(using Printer): String =
6740
"@hw.annotation.flattenMode." + (
6841
this match
69-
case transparent() => "transparent()"
70-
case prefix(sep) => s"""prefix("$sep")"""
71-
case suffix(sep) => s"""suffix("$sep")"""
42+
case Transparent => "transparent()"
43+
case Prefix(sep) => s"""prefix("$sep")"""
44+
case Suffix(sep) => s"""suffix("$sep")"""
7245
)
73-
object flattenMode:
74-
val defaultPrefixUnderscore = flattenMode.prefix("_")
46+
object FlattenMode:
47+
val defaultPrefixUnderscore = FlattenMode.Prefix("_")
7548
end annotation
7649

7750
object constraints:
78-
sealed abstract class Constraint extends annotation.HWAnnotation derives ReadWriter:
79-
val isActive: Boolean = true
51+
sealed abstract class Constraint extends annotation.HWAnnotation derives ReadWriter
8052
sealed abstract class SigConstraint extends Constraint derives ReadWriter:
8153
val bitIdx: ConfigN[Int]
82-
final case class device(name: String, properties: Map[String, String])
83-
extends Constraint
54+
final case class Device(name: String, properties: Map[String, String]) extends Constraint
8455
derives CanEqual, ReadWriter:
8556
def this(name: String, properties: (String, String)*) =
8657
this(name, properties.toMap)
@@ -97,13 +68,13 @@ object constraints:
9768
case num: TimeNumber => s"$name = ${printer.csDFTimeData(num)}"
9869
case _ => s"$name = ${value}"
9970

100-
final case class io(
71+
final case class IO(
10172
bitIdx: ConfigN[Int] = None,
10273
loc: ConfigN[String] = None,
103-
standard: ConfigN[io.Standard] = None,
104-
slewRate: ConfigN[io.SlewRate] = None,
74+
standard: ConfigN[IO.Standard] = None,
75+
slewRate: ConfigN[IO.SlewRate] = None,
10576
driveStrength: ConfigN[Int] = None,
106-
pullMode: ConfigN[io.PullMode] = None
77+
pullMode: ConfigN[IO.PullMode] = None
10778
) extends SigConstraint derives CanEqual, ReadWriter:
10879
def codeString(using Printer): String =
10980
val params = List(
@@ -116,8 +87,8 @@ object constraints:
11687
).filter(_.nonEmpty).mkString(", ")
11788
s"""@io($params)"""
11889
end codeString
119-
end io
120-
object io:
90+
end IO
91+
object IO:
12192
enum Standard extends StableEnum, HasCodeString derives CanEqual, ReadWriter:
12293
case LVCMOS33, LVCMOS25, LVCMOS18
12394
def codeString(using Printer): String = "io.Standard." + this.toString
@@ -128,8 +99,8 @@ object constraints:
12899
case UP, DOWN
129100
def codeString(using Printer): String = "io.PullMode." + this.toString
130101

131-
object timing:
132-
final case class ignore(
102+
object Timing:
103+
final case class Ignore(
133104
bitIdx: ConfigN[Int] = None,
134105
maxFreqMinPeriod: ConfigN[RateNumber] = None
135106
) extends SigConstraint
@@ -141,10 +112,10 @@ object constraints:
141112
).filter(_.nonEmpty).mkString(", ")
142113
s"""@timing.ignore($params)"""
143114

144-
final case class clock(
115+
final case class Clock(
145116
rate: RateNumber
146117
) extends Constraint derives CanEqual, ReadWriter:
147118
def codeString(using Printer): String =
148119
s"""@timing.clock(${csParam("rate", rate)})"""
149-
end timing
120+
end Timing
150121
end constraints

compiler/stages/src/main/scala/dfhdl/compiler/analysis/DBAnalysis.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ extension (designDB: DB)
77
designDB.members.flatMap:
88
case dfVal: DFVal if !dfVal.isAnonymous =>
99
val isUnused = dfVal.meta.annotations.exists {
10-
case u: dfhdl.hw.annotation.unused => u.isActive
11-
case _ => false
10+
case u: annotation.Unused => true
11+
case _ => false
1212
}
1313
if (isUnused) Some(dfVal)
1414
else None

compiler/stages/src/main/scala/dfhdl/compiler/stages/AddClkRst.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ case object AddClkRst extends Stage:
108108
val clkRstSimGen = new EDDomain:
109109
override protected def __dfc: DFC =
110110
selfDFC.setName("clkRstSimGen")
111-
.setAnnotations(List(dfhdl.hw.annotation.flattenMode.transparent()))
111+
.setAnnotations(List(annotation.FlattenMode.Transparent))
112112
val dfcAnon = selfDFC.anonymize.setAnnotations(Nil)
113113
locally {
114114
given DFC = selfDFC.anonymize.setAnnotations(Nil)

compiler/stages/src/main/scala/dfhdl/compiler/stages/DropDomains.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import dfhdl.compiler.analysis.*
44
import dfhdl.compiler.ir.*
55
import dfhdl.compiler.patching.*
66
import dfhdl.options.CompilerOptions
7-
import dfhdl.hw.annotation.flattenMode
7+
import annotation.FlattenMode
88

99
/** This stage flattens the domains by removing them and changing their named members according to
1010
* the flattening mode.
@@ -30,10 +30,10 @@ case object DropDomains extends Stage:
3030
// looping through domain composition until reaching a non-domain and applying the name flattening
3131
while (inDomain)
3232
currentDomain.flattenMode match
33-
case flattenMode.transparent() => // no change
34-
case flattenMode.prefix(sep) =>
33+
case FlattenMode.Transparent => // no change
34+
case FlattenMode.Prefix(sep) =>
3535
currentName = s"${currentDomain.getName}$sep$currentName"
36-
case flattenMode.suffix(sep) =>
36+
case FlattenMode.Suffix(sep) =>
3737
currentName = s"${currentName}$sep${currentDomain.getName}"
3838
currentDomain.getOwner match
3939
case domain: DomainBlock => currentDomain = domain

core/src/main/scala/dfhdl/core/DFC.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package dfhdl.core
22
import dfhdl.internals.*
33
import dfhdl.compiler.ir
44
import dfhdl.options.ElaborationOptions
5-
import dfhdl.hw.annotation.{HWAnnotation, getActiveHWAnnotations}
5+
import dfhdl.hw.annotation.getActiveHWAnnotations
66
import scala.reflect.ClassTag
77
import collection.mutable
88
import scala.annotation.Annotation
99
import scala.annotation.implicitNotFound
10+
import ir.annotation.HWAnnotation
1011

1112
final case class DFC(
1213
nameOpt: Option[String],

core/src/main/scala/dfhdl/core/Design.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ trait Design extends Container, HasClsMetaArgs:
3333
setOwner(
3434
getSet.replace(designBlock)(
3535
designBlock.copy(
36-
dclMeta = ir.Meta.gen(Some(name), position, docOpt, annotations),
36+
dclMeta = r__For_Plugin.metaGen(Some(name), position, docOpt, annotations),
3737
instMode = mkInstMode
3838
)
3939
).asFE

core/src/main/scala/dfhdl/core/MutableDB.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import dfhdl.compiler.ir.{
1818
SourceFile,
1919
MemberView,
2020
RTDomainCfg,
21-
DFTags
21+
DFTags,
22+
annotation
2223
}
2324
import dfhdl.compiler.analysis.filterPublicMembers
2425

@@ -228,8 +229,8 @@ final class MutableDB():
228229
current.defInputs = inputs
229230
val currentDesign = OwnershipContext.currentDesign
230231
val isPure = currentDesign.dclMeta.annotations.exists {
231-
case hw.annotation.pure(true) => true
232-
case _ => false
232+
case annotation.Pure => true
233+
case _ => false
233234
}
234235
if (isPure)
235236
val key = (currentDesign.dclMeta.position, inputs.map(_.dfType.asIR))

core/src/main/scala/dfhdl/core/r__For_Plugin.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ package dfhdl.core
22
import dfhdl.compiler.ir
33
import DFVal.Func.Op as FuncOp
44
import ir.DFConditional.DFCaseBlock.Pattern
5-
5+
import dfhdl.internals.Position
66
import collection.immutable.ListMap
77
import dfhdl.internals.metaContextIgnore
88
import dfhdl.internals.metaContextForward
99
import dfhdl.compiler.ir.DFConditional
10+
import scala.annotation.Annotation
11+
import dfhdl.hw.annotation.getActiveHWAnnotations
12+
1013
object r__For_Plugin:
14+
def metaGen(
15+
nameOpt: Option[String],
16+
position: Position,
17+
docOpt: Option[String],
18+
annotations: List[Annotation]
19+
): ir.Meta = ir.Meta(nameOpt, position, docOpt, annotations.getActiveHWAnnotations)
1120
def toFunc1[R](block: => R): () => R = () => block
1221
def toTuple2[T1, T2](t1: T1, t2: T2): (T1, T2) = (t1, t2)
1322
def toTuple3[T1, T2, T3](t1: T1, t2: T2, t3: T3): (T1, T2, T3) = (t1, t2, t3)

0 commit comments

Comments
 (0)