aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/altIR/FirrtlGraphNode.scala
blob: 7b8bcc145f981c09e9d5900ae5a412f943fdd19f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package firrtl.altIR
import firrtl.ir._
import scala.collection.mutable

/**
 * Base class for the graph nodes in the graph representation of firrtl AST tree.
 */
abstract class FirrtlGraphNode {
  def neighbors: Seq[FirrtlGraphNode]
}

/**
 * Graph node equivalent of Statement AST tree nodes in IR.scala.
 */
abstract class StatementGraphNode extends FirrtlGraphNode

/**
 * Graph node representation of nodes that can be refered to by name in the tree form of the IR
 */
abstract class NamedGraphNode extends StatementGraphNode {
  val name: String
  val references = mutable.ArrayBuffer.empty[ReferenceGraphNode]
  def addReference(ref: ReferenceGraphNode): Unit
}

/**
 * Graph node representation of nodes that can be assigned to by connect statements in the tree form
 * of firrtl.
 */
abstract class AssignableGraphNode extends NamedGraphNode {
  def neighbors = references
  def addReference(ref: ReferenceGraphNode): Unit = {
    references += ref
    ref.namedNode = Some(this)
  }
}

/**
 * Graph node equivalent to Expression nodes in the tree form of the IR.
 */
abstract class ExpressionGraphNode extends FirrtlGraphNode {
  val tpe: Type
  var parent: Option[FirrtlGraphNode]
  def addParent(node: ExpressionGraphNode): Unit = {
    parent = Some(node)
    node match {
      case subField: SubFieldGraphNode =>
        subField.expr = Some(this)
      case subIndex: SubIndexGraphNode =>
        subIndex.expr = Some(this)
    }
  }
}

/**
 * Graph node equivalent to Reference nodes in the tree form of the IR
 */
class ReferenceGraphNode(
  val name: String, val tpe: Type
) extends ExpressionGraphNode {
  var namedNode: Option[NamedGraphNode] = None
  var parent: Option[FirrtlGraphNode] = None
  def neighbors = namedNode.toList ++ parent.toList
}

/**
 * Graph node equivalent to SubField nodes in the tree form of the IR
 */
class SubFieldGraphNode(
  val name: String, val tpe: Type
) extends ExpressionGraphNode {
  var expr: Option[ExpressionGraphNode] = None
  var parent: Option[FirrtlGraphNode] = None
  def neighbors = expr.toList ++ parent.toList
}

/**
 * Graph node equivalent to SubIndex nodes in the tree form of the IR
 */
class SubIndexGraphNode(
  val value: Int, val tpe: Type
) extends ExpressionGraphNode {
  var expr: Option[ExpressionGraphNode] = None
  var parent: Option[FirrtlGraphNode] = None
  def neighbors = expr.toList ++ parent.toList
}

/**
 * Graph node equivalent to DefWire nodes in the tree form of the IR
 */
class DefWireGraphNode(
  val info: Info,
  val name: String,
  val tpe: Type
) extends AssignableGraphNode

/**
 * Graph node equivalent to DefInstance nodes in the tree form of the IR
 */
class DefInstanceGraphNode(
  val info: Info,
  val name: String,
  val module: String
) extends AssignableGraphNode

/**
 * Graph nodes that represent the IO ports of a module not present in the tree form of the IR.
 */
class PortGraphNode(
  val info: Info,
  val name: String,
  val direction: Direction,
  val tpe: Type
) extends AssignableGraphNode

/**
 * Graph node equivalent to Connect nodes in the tree form of the IR
 */
class ConnectGraphNode(val info: Info) extends StatementGraphNode {
  var loc: Option[ExpressionGraphNode] = None
  var expr: Option[ExpressionGraphNode] = None
  def neighbors = loc.toList ++ expr.toList
  def addLoc(node: ExpressionGraphNode): Unit = {
    loc = Some(node)
    node.parent = Some(this)
  }
  def addExpr(node: ExpressionGraphNode): Unit = {
    expr = Some(node)
    node.parent = Some(this)
  }
}

/**
 * Graph node equivalent to IsInvalid nodes in the tree form of the IR
 */
class IsInvalidGraphNode(val info: Info) extends StatementGraphNode {
  var expr: Option[ExpressionGraphNode] = None
  def neighbors = expr.toList
}

object getGraphNode {
  def apply[T](pointer: Option[T]): T = {
    pointer.getOrElse(
      throw new Exception(
        s"InsertWrapperModules pass encountered unexpectedly unconnected "
        + s"graph node pointer"
      )
    )
  }
}