Skip to content

Commit 543a099

Browse files
author
Oron Port
committed
resources context change wip
1 parent 23c23f3 commit 543a099

File tree

9 files changed

+86
-7
lines changed

9 files changed

+86
-7
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dfhdl.core
2+
import dfhdl.internals.*
3+
import scala.annotation.Annotation
4+
5+
class ResourceContainer extends OnCreateEvents, HasDFC, HasClsMetaArgs:
6+
final lazy val dfc: DFC = __dfc
7+
protected def __dfc: DFC =
8+
println("Severe error: missing DFHDL context!\nMake sure you enable the DFHDL compiler plugin.")
9+
sys.exit(1)
10+
protected def setClsNamePos(
11+
name: String,
12+
position: Position,
13+
docOpt: Option[String],
14+
annotations: List[Annotation]
15+
): Unit = {}
16+
end ResourceContainer

core/src/main/scala/dfhdl/hw/annotation.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ end annotation
6363
object constraints:
6464
sealed abstract class Constraint extends annotation.HWAnnotation:
6565
val isActive: Boolean = true
66+
val asIR: ir.constraints.Constraint
6667
sealed abstract class SigConstraint extends Constraint:
6768
val bitIdx: ir.ConfigN[Int]
6869
final case class device(name: String, properties: (String, String)*) extends Constraint:

internals/src/main/scala/dfhdl/internals/helpers.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,33 @@ object AnnotatedWith:
460460
end annotWithMacro
461461
end AnnotatedWith
462462

463+
// getting annotations both from the class and the object
464+
// E.g.:
465+
// @annot1
466+
// class Foo
467+
// @annot2
468+
// val foo = new Foo
469+
// the annotations should be @annot1 and @annot2
470+
trait CTAnnotations:
471+
def annotations: List[StaticAnnotation]
472+
object CTAnnotations:
473+
inline given CTAnnotations = ${ macroImpl }
474+
def macroImpl(using Quotes): Expr[CTAnnotations] =
475+
import quotes.reflect.*
476+
val owner = Symbol.spliceOwner.owner
477+
val allAnnotations =
478+
owner.termRef.classSymbol.map(_.annotations).getOrElse(Nil) ++ owner.annotations
479+
val annotationsExpr =
480+
Expr.ofList(allAnnotations.filter(
481+
_.tpe <:< TypeRepr.of[StaticAnnotation]
482+
).map(_.asExprOf[StaticAnnotation]))
483+
'{
484+
new CTAnnotations:
485+
def annotations: List[StaticAnnotation] = $annotationsExpr
486+
}
487+
end macroImpl
488+
end CTAnnotations
489+
463490
lazy val osIsWindows: Boolean = sys.props("os.name").toLowerCase.contains("windows")
464491
lazy val osIsLinux: Boolean = sys.props("os.name").toLowerCase.contains("linux")
465492
lazy val osIsWSL: Boolean =

lib/src/main/scala/dfhdl/platforms/resources/IO.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import dfhdl.compiler.ir.constraints
44

55
trait IO extends Resource
66
object IO:
7-
given [T <: IO, R <: IO]: CanConnect[T, R] = new CanConnect[T, R] {}
7+
given [T <: IO, R <: IO]: CanConnect[T, R] = (resource1: T, resource2: R) =>
8+
resource1.connect(resource2)
9+
resource2.connect(resource1)
810

911
trait HasIOConstraints extends IO:
1012
val ioc: constraints.IO

lib/src/main/scala/dfhdl/platforms/resources/RCtx.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@ package dfhdl.platforms.resources
22
import dfhdl.internals.CTName
33
import dfhdl.internals.PrintType
44
import scala.annotation.implicitNotFound
5+
import dfhdl.internals.CTAnnotations
6+
import dfhdl.compiler.ir.constraints.Constraint
57

68
//Resource Context
79
@implicitNotFound("Missing RCtx context")
810
trait RCtx:
911
val id: String
1012
val owner: ResourceOwner
13+
val constraints: List[Constraint]
1114

1215
object RCtx:
1316
type NonEmptyName[T] = util.NotGiven[T =:= ""]
1417
given RCtx(using
1518
ctName: CTName,
19+
ctAnnotations: CTAnnotations,
1620
owner: ResourceOwner = NoResourceOwner
1721
)(using NonEmptyName[ctName.Out]): RCtx = new RCtx:
1822
val id = ctName.value
1923
val owner = owner
24+
val constraints = ctAnnotations.annotations.collect {
25+
case c: dfhdl.hw.constraints.Constraint => c.asIR
26+
}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
package dfhdl.platforms.resources
22
import scala.annotation.implicitNotFound
3+
import scala.collection.mutable.ListBuffer
34

45
trait Resource(using ctx: RCtx):
56
final val id: String = ctx.id
67
final val owner: ResourceOwner = ctx.owner
8+
private val connections = ListBuffer[Resource]()
9+
protected[resources] def connect(that: Resource): Unit =
10+
connections += that
711
owner.add(this)
812

913
private trait ResourceLP:
1014
import Resource.CanConnect
11-
given [T <: Resource, R <: Resource](using CanConnect[R, T]): CanConnect[T, R] =
12-
new CanConnect[T, R] {}
15+
// connection is commutative, so can connect T to R if can connect R to T
16+
given [T <: Resource, R <: Resource](using cc: CanConnect[R, T]): CanConnect[T, R] =
17+
(resource1: T, resource2: R) =>
18+
cc.connect(resource2, resource1)
1319

1420
object Resource extends ResourceLP:
1521
@implicitNotFound("Cannot connect the resources ${T} and ${R}")
16-
trait CanConnect[T <: Resource, R <: Resource]
17-
given [T <: Resource, R <: Resource](using T =:= R): CanConnect[T, R] = new CanConnect[T, R] {}
22+
trait CanConnect[T <: Resource, R <: Resource]:
23+
def connect(resource1: T, resource2: R): Unit
24+
given [T <: Resource, R <: Resource](using T =:= R): CanConnect[T, R] =
25+
(resource1: T, resource2: R) =>
26+
resource1.connect(resource2)
27+
resource2.connect(resource1)
1828

1929
extension [T <: Resource](self: T)
20-
def <>[R <: Resource](that: R)(using CanConnect[T, R]): Unit = {}
30+
def <>[R <: Resource](that: R)(using cc: CanConnect[T, R]): Unit =
31+
cc.connect(self, that)

lib/src/main/scala/dfhdl/platforms/resources/ResourceDeps.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ package dfhdl.platforms.resources
22

33
trait ResourceDeps(using RCtx) extends Resource:
44
protected def deps: List[Resource]
5+
override protected[resources] def connect(that: Resource): Unit =
6+
deps.lazyZip(that.asInstanceOf[ResourceDeps].deps).foreach {
7+
case (r1, r2) => r1.connect(r2)
8+
}
9+
super.connect(that)
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
package dfhdl.platforms.resources
22

3-
trait ResourceGroup(using RCtx) extends Resource, ResourceOwner
3+
trait ResourceGroup(using RCtx) extends Resource, ResourceOwner:
4+
override protected[resources] def connect(that: Resource): Unit =
5+
getResources.lazyZip(that.asInstanceOf[ResourceGroup].getResources).foreach {
6+
// skipping ResourceDeps in the group, since their dependencies are already connected
7+
// since they are also children of the same group.
8+
// (only when directly connecting two ResourceDeps, their dependencies are connected)
9+
case (r1: ResourceDeps, r2: ResourceDeps) => // do nothing
10+
case (r1, r2) => r1.connect(r2)
11+
}
12+
super.connect(that)

lib/src/main/scala/dfhdl/platforms/resources/ResourceOwner.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import scala.collection.mutable.ListBuffer
33
trait ResourceOwner:
44
protected given ResourceOwner = this
55
private val resources = ListBuffer[Resource]()
6+
def getResources: List[Resource] = resources.toList
67
def add(resource: Resource): Unit = resources += resource
78

89
sealed trait NoResourceOwner extends ResourceOwner

0 commit comments

Comments
 (0)