CIRCT  19.0.0git
ImportVerilogInternals.h
Go to the documentation of this file.
1 //===- ImportVerilogInternals.h - Internal implementation details ---------===//
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 // NOLINTNEXTLINE(llvm-header-guard)
10 #ifndef CONVERSION_IMPORTVERILOG_IMPORTVERILOGINTERNALS_H
11 #define CONVERSION_IMPORTVERILOG_IMPORTVERILOGINTERNALS_H
12 
14 #include "circt/Dialect/HW/HWOps.h"
16 #include "mlir/Dialect/SCF/IR/SCF.h"
17 #include "slang/ast/ASTVisitor.h"
18 #include "llvm/ADT/ScopedHashTable.h"
19 #include "llvm/Support/Debug.h"
20 #include <map>
21 #include <queue>
22 
23 #define DEBUG_TYPE "import-verilog"
24 
25 namespace circt {
26 namespace ImportVerilog {
27 
28 /// A helper class to facilitate the conversion from a Slang AST to MLIR
29 /// operations. Keeps track of the destination MLIR module, builders, and
30 /// various worklists and utilities needed for conversion.
31 struct Context {
32  Context(mlir::ModuleOp intoModuleOp,
33  const slang::SourceManager &sourceManager,
37  builder(OpBuilder::atBlockEnd(intoModuleOp.getBody())),
39  Context(const Context &) = delete;
40 
41  /// Return the MLIR context.
42  MLIRContext *getContext() { return intoModuleOp.getContext(); }
43 
44  /// Convert a slang `SourceLocation` into an MLIR `Location`.
45  Location convertLocation(slang::SourceLocation loc);
46  /// Convert a slang `SourceRange` into an MLIR `Location`.
47  Location convertLocation(slang::SourceRange range);
48 
49  /// Convert a slang type into an MLIR type. Returns null on failure. Uses the
50  /// provided location for error reporting, or tries to guess one from the
51  /// given type. Types tend to have unreliable location information, so it's
52  /// generally a good idea to pass in a location.
53  Type convertType(const slang::ast::Type &type, LocationAttr loc = {});
54  Type convertType(const slang::ast::DeclaredType &type);
55 
56  /// Convert hierarchy and structure AST nodes to MLIR ops.
57  LogicalResult convertCompilation(slang::ast::Compilation &compilation);
58  moore::SVModuleOp
59  convertModuleHeader(const slang::ast::InstanceBodySymbol *module);
60  LogicalResult convertModuleBody(const slang::ast::InstanceBodySymbol *module);
61 
62  // Convert a statement AST node to MLIR ops.
63  LogicalResult convertStatement(const slang::ast::Statement &stmt);
64 
65  // Convert an expression AST node to MLIR ops.
66  Value convertExpression(const slang::ast::Expression &expr);
67 
68  mlir::ModuleOp intoModuleOp;
69  const slang::SourceManager &sourceManager;
71 
72  /// The builder used to create IR operations.
73  OpBuilder builder;
74  /// A symbol table of the MLIR module we are emitting into.
75  SymbolTable symbolTable;
76 
77  /// The top-level operations ordered by their Slang source location. This is
78  /// used to produce IR that follows the source file order.
79  std::map<slang::SourceLocation, Operation *> orderedRootOps;
80  /// How we have lowered modules to MLIR.
81  DenseMap<const slang::ast::InstanceBodySymbol *, moore::SVModuleOp> moduleOps;
82  /// A list of modules for which the header has been created, but the body has
83  /// not been converted yet.
84  std::queue<const slang::ast::InstanceBodySymbol *> moduleWorklist;
85 
86  /// A table of defined values, such as variables, that may be referred to by
87  /// name in expressions. The expressions use this table to lookup the MLIR
88  /// value that was created for a given declaration in the Slang AST node.
89  using ValueSymbols =
90  llvm::ScopedHashTable<const slang::ast::ValueSymbol *, Value>;
91  using ValueSymbolScope = ValueSymbols::ScopeTy;
93 
94  /// A stack of assignment left-hand side values. Each assignment will push its
95  /// lowered left-hand side onto this stack before lowering its right-hand
96  /// side. This allows expressions to resolve the opaque
97  /// `LValueReferenceExpression`s in the AST.
98  SmallVector<Value> lvalueStack;
99 };
100 
101 } // namespace ImportVerilog
102 } // namespace circt
103 
104 #endif // CONVERSION_IMPORTVERILOG_IMPORTVERILOGINTERNALS_H
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
A helper class to facilitate the conversion from a Slang AST to MLIR operations.
LogicalResult convertModuleBody(const slang::ast::InstanceBodySymbol *module)
Convert a module's body to the corresponding IR ops.
Definition: Structure.cpp:307
OpBuilder builder
The builder used to create IR operations.
std::queue< const slang::ast::InstanceBodySymbol * > moduleWorklist
A list of modules for which the header has been created, but the body has not been converted yet.
LogicalResult convertCompilation(slang::ast::Compilation &compilation)
Convert hierarchy and structure AST nodes to MLIR ops.
Definition: Structure.cpp:221
Value convertExpression(const slang::ast::Expression &expr)
Context(const Context &)=delete
std::map< slang::SourceLocation, Operation * > orderedRootOps
The top-level operations ordered by their Slang source location.
Type convertType(const slang::ast::Type &type, LocationAttr loc={})
Convert a slang type into an MLIR type.
Definition: Types.cpp:164
SmallDenseMap< slang::BufferID, StringRef > & bufferFilePaths
DenseMap< const slang::ast::InstanceBodySymbol *, moore::SVModuleOp > moduleOps
How we have lowered modules to MLIR.
moore::SVModuleOp convertModuleHeader(const slang::ast::InstanceBodySymbol *module)
Convert a module and its ports to an empty module op in the IR.
Definition: Structure.cpp:257
ValueSymbols::ScopeTy ValueSymbolScope
Context(mlir::ModuleOp intoModuleOp, const slang::SourceManager &sourceManager, SmallDenseMap< slang::BufferID, StringRef > &bufferFilePaths)
llvm::ScopedHashTable< const slang::ast::ValueSymbol *, Value > ValueSymbols
A table of defined values, such as variables, that may be referred to by name in expressions.
const slang::SourceManager & sourceManager
SymbolTable symbolTable
A symbol table of the MLIR module we are emitting into.
MLIRContext * getContext()
Return the MLIR context.
LogicalResult convertStatement(const slang::ast::Statement &stmt)
Definition: Statements.cpp:325
SmallVector< Value > lvalueStack
A stack of assignment left-hand side values.
Location convertLocation(slang::SourceLocation loc)
Convert a slang SourceLocation into an MLIR Location.