CIRCT  19.0.0git
MooreTypes.h
Go to the documentation of this file.
1 //===- MooreTypes.h - Declare Moore dialect types ----------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the types for the Moore dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef CIRCT_DIALECT_MOORE_MOORETYPES_H
14 #define CIRCT_DIALECT_MOORE_MOORETYPES_H
15 
16 #include "circt/Support/LLVM.h"
17 #include "mlir/IR/Attributes.h"
18 #include "mlir/IR/BuiltinAttributes.h"
19 #include "mlir/IR/Location.h"
20 #include "mlir/IR/Types.h"
21 #include "mlir/Interfaces/MemorySlotInterfaces.h"
22 #include <variant>
23 
24 namespace circt {
25 namespace moore {
26 
27 class ArrayType;
28 class AssocArrayType;
29 class ChandleType;
30 class EventType;
31 class IntType;
32 class OpenArrayType;
33 class OpenUnpackedArrayType;
34 class PackedType;
35 class QueueType;
36 class RealType;
37 class StringType;
38 class StructType;
39 class UnionType;
40 class UnpackedType;
41 class UnpackedArrayType;
42 class UnpackedStructType;
43 class UnpackedUnionType;
44 class VoidType;
45 
46 /// The number of values each bit of a type can assume.
47 enum class Domain {
48  /// Two-valued types such as `bit` or `int`.
49  TwoValued,
50  /// Four-valued types such as `logic` or `integer`.
51  FourValued,
52 };
53 
54 //===----------------------------------------------------------------------===//
55 // Unpacked Type
56 //===----------------------------------------------------------------------===//
57 
58 /// An unpacked SystemVerilog type.
59 ///
60 /// Unpacked types are a second level of types in SystemVerilog. They extend a
61 /// core unpacked type with a variety of unpacked dimensions, depending on which
62 /// syntactic construct generated the type (variable or otherwise). The core
63 /// unpacked types are:
64 ///
65 /// - Packed types
66 /// - Non-integer types: `shortreal`, `real`, `realtime`
67 /// - Unpacked structs and unions
68 /// - `string`, `chandle`, `event`
69 /// - Virtual interfaces
70 /// - Class types
71 /// - Covergroups
72 /// - Unpacked named types
73 /// - Unpacked type references
74 ///
75 /// The unpacked dimensions are:
76 ///
77 /// - Unsized (`[]`)
78 /// - Arrays (`[x]`)
79 /// - Ranges (`[x:y]`)
80 /// - Associative (`[T]` or `[*]`)
81 /// - Queues (`[$]` or `[$:x]`)
82 class UnpackedType : public Type {
83 public:
84  static bool classof(Type type) {
85  return llvm::isa<PackedType, StringType, ChandleType, EventType, RealType,
86  UnpackedArrayType, OpenUnpackedArrayType, AssocArrayType,
87  QueueType, UnpackedStructType, UnpackedUnionType>(type);
88  }
89 
90  /// Get the value domain of this type.
91  Domain getDomain() const;
92 
93  /// Get the size of this type in bits.
94  ///
95  /// Returns `None` if any of the type's dimensions is unsized, associative, or
96  /// a queue, or the core type itself has no known size.
97  std::optional<unsigned> getBitSize() const;
98 
99  // Support parsing and printing of unpacked types in their prefix-stripped
100  // form.
101  static Type parse(mlir::AsmParser &odsParser);
102  void print(mlir::AsmPrinter &odsPrinter) const;
103 
104 protected:
105  using Type::Type;
106 };
107 
108 //===----------------------------------------------------------------------===//
109 // Packed Type
110 //===----------------------------------------------------------------------===//
111 
112 /// A packed SystemVerilog type.
113 ///
114 /// Packed types are the core types of SystemVerilog. They combine a core packed
115 /// type with an optional sign and zero or more packed dimensions. The core
116 /// packed types are:
117 ///
118 /// - Integer vector types: `bit`, `logic`, `reg`
119 /// - Integer atom types: `byte`, `shortint`, `int`, `longint`, `integer`,
120 /// `time`
121 /// - Packed structs and unions
122 /// - Enums
123 /// - Packed named types
124 /// - Packed type references
125 ///
126 /// The packed dimensions can be:
127 ///
128 /// - Unsized (`[]`)
129 /// - Ranges (`[x:y]`)
130 ///
131 /// Note that every packed type is also a valid unpacked type. But unpacked
132 /// types are *not* valid packed types.
133 class PackedType : public UnpackedType {
134 public:
135  static bool classof(Type type) {
136  return llvm::isa<VoidType, IntType, ArrayType, OpenArrayType, StructType,
137  UnionType>(type);
138  }
139 
140  /// Get the value domain of this type.
141  Domain getDomain() const;
142 
143  /// Get the size of this type in bits.
144  ///
145  /// Returns `None` if any of the type's dimensions is unsized.
146  std::optional<unsigned> getBitSize() const;
147 
148 protected:
149  using UnpackedType::UnpackedType;
150 };
151 
152 //===----------------------------------------------------------------------===//
153 // Struct Members
154 //===----------------------------------------------------------------------===//
155 
156 /// A member of a struct.
158  /// The name of this member.
159  StringAttr name;
160  /// The type of this member.
162 
163  bool operator==(const StructLikeMember &other) const {
164  return name == other.name && type == other.type;
165  }
166 };
167 
168 // NOLINTNEXTLINE(readability-identifier-naming)
169 inline llvm::hash_code hash_value(const StructLikeMember &x) {
170  return llvm::hash_combine(x.name, x.type);
171 }
172 
173 } // namespace moore
174 } // namespace circt
175 
176 // Include generated types.
177 #define GET_TYPEDEF_CLASSES
178 #include "circt/Dialect/Moore/MooreTypes.h.inc"
179 
180 #endif // CIRCT_DIALECT_MOORE_MOORETYPES_H
A packed SystemVerilog type.
Definition: MooreTypes.h:133
std::optional< unsigned > getBitSize() const
Get the size of this type in bits.
Definition: MooreTypes.cpp:114
Domain getDomain() const
Get the value domain of this type.
Definition: MooreTypes.cpp:100
static bool classof(Type type)
Definition: MooreTypes.h:135
An unpacked SystemVerilog type.
Definition: MooreTypes.h:82
std::optional< unsigned > getBitSize() const
Get the size of this type in bits.
Definition: MooreTypes.cpp:51
Domain getDomain() const
Get the value domain of this type.
Definition: MooreTypes.cpp:36
static bool classof(Type type)
Definition: MooreTypes.h:84
static Type parse(mlir::AsmParser &odsParser)
Definition: MooreTypes.cpp:85
void print(mlir::AsmPrinter &odsPrinter) const
Definition: MooreTypes.cpp:92
llvm::hash_code hash_value(const StructLikeMember &x)
Definition: MooreTypes.h:169
Domain
The number of values each bit of a type can assume.
Definition: MooreTypes.h:47
@ FourValued
Four-valued types such as logic or integer.
@ TwoValued
Two-valued types such as bit or int.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
A member of a struct.
Definition: MooreTypes.h:157
UnpackedType type
The type of this member.
Definition: MooreTypes.h:161
StringAttr name
The name of this member.
Definition: MooreTypes.h:159
bool operator==(const StructLikeMember &other) const
Definition: MooreTypes.h:163