CIRCT 22.0.0git
Loading...
Searching...
No Matches
Types.cpp
Go to the documentation of this file.
1//===- Types.cpp - Slang type conversion ----------------------------------===//
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
10#include "slang/syntax/AllSyntax.h"
11
12using namespace circt;
13using namespace ImportVerilog;
14using moore::Domain;
15
16namespace {
17struct TypeVisitor {
18 Context &context;
19 Location loc;
20 TypeVisitor(Context &context, Location loc) : context(context), loc(loc) {}
21
22 // Handle simple bit vector types such as `bit`, `int`, or `bit [41:0]`.
23 Type getSimpleBitVectorType(const slang::ast::IntegralType &type) {
24 return moore::IntType::get(context.getContext(), type.bitWidth,
25 type.isFourState ? Domain::FourValued
26 : Domain::TwoValued);
27 }
28
29 // NOLINTBEGIN(misc-no-recursion)
30 Type visit(const slang::ast::VoidType &type) {
31 return moore::VoidType::get(context.getContext());
32 }
33
34 Type visit(const slang::ast::ScalarType &type) {
35 return getSimpleBitVectorType(type);
36 }
37
38 Type visit(const slang::ast::FloatingType &type) {
39 if (type.floatKind == slang::ast::FloatingType::Kind::RealTime)
40 return moore::TimeType::get(context.getContext());
41 if (type.floatKind == slang::ast::FloatingType::Kind::Real)
42 return moore::RealType::get(context.getContext(), moore::RealWidth::f64);
43 return moore::RealType::get(context.getContext(), moore::RealWidth::f32);
44 }
45
46 Type visit(const slang::ast::PredefinedIntegerType &type) {
47 if (type.integerKind == slang::ast::PredefinedIntegerType::Kind::Time)
48 return moore::TimeType::get(context.getContext());
49 return getSimpleBitVectorType(type);
50 }
51
52 Type visit(const slang::ast::PackedArrayType &type) {
53 // Handle simple bit vector types of the form `bit [41:0]`.
54 if (type.elementType.as_if<slang::ast::ScalarType>())
55 return getSimpleBitVectorType(type);
56
57 // Handle all other packed arrays.
58 auto innerType = type.elementType.visit(*this);
59 if (!innerType)
60 return {};
61 // The Slang frontend guarantees the inner type to be packed.
62 return moore::ArrayType::get(type.range.width(),
63 cast<moore::PackedType>(innerType));
64 }
65
66 Type visit(const slang::ast::QueueType &type) {
67 auto innerType = type.elementType.visit(*this);
68 if (!innerType)
69 return {};
70 return moore::QueueType::get(cast<moore::UnpackedType>(innerType),
71 type.maxBound);
72 }
73
74 Type visit(const slang::ast::AssociativeArrayType &type) {
75 auto innerType = type.elementType.visit(*this);
76 if (!innerType)
77 return {};
78 if (!type.indexType) {
79 mlir::emitError(
80 loc, "unsupported type: associative arrays with wildcard index");
81 return {};
82 }
83 auto indexType = type.indexType->visit(*this);
84 if (!indexType)
85 return {};
86 return moore::AssocArrayType::get(cast<moore::UnpackedType>(innerType),
87 cast<moore::UnpackedType>(indexType));
88 }
89
90 Type visit(const slang::ast::FixedSizeUnpackedArrayType &type) {
91 auto innerType = type.elementType.visit(*this);
92 if (!innerType)
93 return {};
94 return moore::UnpackedArrayType::get(type.range.width(),
95 cast<moore::UnpackedType>(innerType));
96 }
97
98 Type visit(const slang::ast::DynamicArrayType &type) {
99 auto innerType = type.elementType.visit(*this);
100 if (!innerType)
101 return {};
102 return moore::OpenUnpackedArrayType::get(
103 cast<moore::UnpackedType>(innerType));
104 }
105
106 // Handle type defs.
107 Type visit(const slang::ast::TypeAliasType &type) {
108 // Simply return the underlying type.
109 return type.targetType.getType().visit(*this);
110 }
111
112 // Handle enums.
113 Type visit(const slang::ast::EnumType &type) {
114 // Simply return the underlying type.
115 return type.baseType.visit(*this);
116 }
117
118 // Collect the members in a struct or union.
119 LogicalResult
120 collectMembers(const slang::ast::Scope &structType,
121 SmallVectorImpl<moore::StructLikeMember> &members) {
122 for (auto &field : structType.membersOfType<slang::ast::FieldSymbol>()) {
123 auto name = StringAttr::get(context.getContext(), field.name);
124 auto innerType = context.convertType(*field.getDeclaredType());
125 if (!innerType)
126 return failure();
127 members.push_back({name, cast<moore::UnpackedType>(innerType)});
128 }
129 return success();
130 }
131
132 // Handle packed and unpacked structs.
133 Type visit(const slang::ast::PackedStructType &type) {
134 SmallVector<moore::StructLikeMember> members;
135 if (failed(collectMembers(type, members)))
136 return {};
137 return moore::StructType::get(context.getContext(), members);
138 }
139
140 Type visit(const slang::ast::UnpackedStructType &type) {
141 SmallVector<moore::StructLikeMember> members;
142 if (failed(collectMembers(type, members)))
143 return {};
144 return moore::UnpackedStructType::get(context.getContext(), members);
145 }
146
147 Type visit(const slang::ast::PackedUnionType &type) {
148 SmallVector<moore::StructLikeMember> members;
149 if (failed(collectMembers(type, members)))
150 return {};
151 return moore::UnionType::get(context.getContext(), members);
152 }
153
154 Type visit(const slang::ast::UnpackedUnionType &type) {
155 SmallVector<moore::StructLikeMember> members;
156 if (failed(collectMembers(type, members)))
157 return {};
158 return moore::UnpackedUnionType::get(context.getContext(), members);
159 }
160
161 Type visit(const slang::ast::StringType &type) {
162 return moore::StringType::get(context.getContext());
163 }
164
165 /// Emit an error for all other types.
166 template <typename T>
167 Type visit(T &&node) {
168 auto d = mlir::emitError(loc, "unsupported type: ")
169 << slang::ast::toString(node.kind);
170 d.attachNote() << node.template as<slang::ast::Type>().toString();
171 return {};
172 }
173 // NOLINTEND(misc-no-recursion)
174};
175} // namespace
176
177// NOLINTBEGIN(misc-no-recursion)
178Type Context::convertType(const slang::ast::Type &type, LocationAttr loc) {
179 if (!loc)
180 loc = convertLocation(type.location);
181 return type.visit(TypeVisitor(*this, loc));
182}
183
184Type Context::convertType(const slang::ast::DeclaredType &type) {
185 LocationAttr loc;
186 if (auto *ts = type.getTypeSyntax())
187 loc = convertLocation(ts->sourceRange().start());
188 return convertType(type.getType(), loc);
189}
190// NOLINTEND(misc-no-recursion)
mlir::Type innerType(mlir::Type type)
Definition ESITypes.cpp:227
Domain
The number of values each bit of a type can assume.
Definition MooreTypes.h:48
@ TwoValued
Two-valued types such as bit or int.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
A helper class to facilitate the conversion from a Slang AST to MLIR operations.
Type convertType(const slang::ast::Type &type, LocationAttr loc={})
Convert a slang type into an MLIR type.
Definition Types.cpp:178
MLIRContext * getContext()
Return the MLIR context.
Location convertLocation(slang::SourceLocation loc)
Convert a slang SourceLocation into an MLIR Location.