CIRCT 24.0.0git
Loading...
Searching...
No Matches
FIRParser.h
Go to the documentation of this file.
1//===- FIRParser.h - .fir to FIRRTL dialect parser --------------*- C++ -*-===//
2//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// Defines the interface to the .fir file parser.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CIRCT_DIALECT_FIRRTL_FIRPARSER_H
15#define CIRCT_DIALECT_FIRRTL_FIRPARSER_H
16
18#include "circt/Support/LLVM.h"
19#include <optional>
20#include <string>
21#include <vector>
22
23namespace llvm {
24class SourceMgr;
25} // namespace llvm
26
27namespace mlir {
28class LocationAttr;
29class TimingScope;
30} // namespace mlir
31
32namespace circt {
33namespace firrtl {
34
36 /// Specify how @info locators should be handled.
37 enum class InfoLocHandling {
38 /// If this is set to true, the @info locators are ignored, and the
39 /// locations are set to the location in the .fir file.
41 /// Prefer @info locators, fallback to .fir locations.
43 /// Attach both @info locators (when present) and .fir locations.
45 };
46
48
49 /// The number of annotation files that were specified on the command line.
50 /// This, provides structure to the buffers in the source manager.
54 bool scalarizeExtModules = false;
55 bool warnOnTruncation = false;
56 std::vector<std::string> enableLayers;
57 std::vector<std::string> disableLayers;
58 std::optional<LayerSpecialization> defaultLayerSpecialization;
59 std::vector<std::string> selectInstanceChoice;
60};
61
63 mlir::MLIRContext *context,
64 mlir::TimingScope &ts,
65 FIRParserOptions options = {});
66
67// Decode a source locator string `spelling`, returning a pair indicating that
68// the `spelling` was correct and an optional location attribute. The
69// `skipParsing` option can be used to short-circuit parsing and just do
70// validation of the `spelling`. This require both an Identifier and a
71// FileLineColLoc to use for caching purposes and context as the cache may be
72// updated with a new identifier.
73//
74// This utility exists because source locators can exist outside of normal
75// "parsing". E.g., these can show up in annotations or in Object Model 2.0
76// JSON.
77//
78// TODO: This API is super wacky and should be streamlined to hide the
79// caching.
80std::pair<bool, std::optional<mlir::LocationAttr>>
81maybeStringToLocation(llvm::StringRef spelling, bool skipParsing,
82 mlir::StringAttr &locatorFilenameCache,
83 FileLineColLoc &fileLineColLocCache,
84 MLIRContext *context);
85
87
88/// The FIRRTL specification version.
89struct FIRVersion {
90 constexpr FIRVersion(uint16_t major, uint16_t minor, uint16_t patch)
92
93 explicit constexpr operator uint64_t() const {
94 return uint64_t(major) << 32 | uint64_t(minor) << 16 | uint64_t(patch);
95 }
96
97 constexpr bool operator<(FIRVersion rhs) const {
98 return uint64_t(*this) < uint64_t(rhs);
99 }
100
101 constexpr bool operator>(FIRVersion rhs) const {
102 return uint64_t(*this) > uint64_t(rhs);
103 }
104
105 constexpr bool operator<=(FIRVersion rhs) const {
106 return uint64_t(*this) <= uint64_t(rhs);
107 }
108
109 constexpr bool operator>=(FIRVersion rhs) const {
110 return uint64_t(*this) >= uint64_t(rhs);
111 }
112
113 /// Parse a version string of the form "major.minor.patch". Returns
114 /// std::nullopt if the string is malformed or values overflow uint16_t.
115 static std::optional<FIRVersion> fromString(StringRef str) {
116 uint16_t major, minor, patch;
117 if (str.consumeInteger(10, major) || !str.consume_front(".") ||
118 str.consumeInteger(10, minor) || !str.consume_front(".") ||
119 str.consumeInteger(10, patch) || !str.empty())
120 return std::nullopt;
121 return FIRVersion(major, minor, patch);
122 }
123
124 uint16_t major;
125 uint16_t minor;
126 uint16_t patch;
127};
128
129/// The current minimum version of FIRRTL that the parser supports.
130constexpr FIRVersion minimumFIRVersion(2, 0, 0);
131
132/// The next version of FIRRTL that is not yet released.
133///
134/// Features use this version if they have been landed on the main branch of
135/// `chipsalliance/firrtl-spec`, but have not been part of a release yet. Once a
136/// new version of the spec is released, all uses of `nextFIRVersion` in the
137/// parser are replaced with the concrete version `{x, y, z}`, and this
138/// declaration here is bumped to the next probable version number.
139constexpr FIRVersion nextFIRVersion(7, 0, 0);
140
141/// A marker for parser features that are currently missing from the spec.
142///
143/// Features use this version if they have _not_ been added to the documentation
144/// in the `chipsalliance/firrtl-spec` repository. This allows us to distinguish
145/// features that are released in the next version of the spec and features that
146/// are still missing from the spec.
148
149/// The version of FIRRTL that the exporter produces. This is always the next
150/// version, since it contains any new developments.
152
153template <typename T>
154T &operator<<(T &os, FIRVersion version) {
155 return os << version.major << "." << version.minor << "." << version.patch;
156}
157
158} // namespace firrtl
159} // namespace circt
160
161#endif // CIRCT_DIALECT_FIRRTL_FIRPARSER_H
static std::unique_ptr< Context > context
void registerFromFIRFileTranslation()
constexpr FIRVersion nextFIRVersion(7, 0, 0)
The next version of FIRRTL that is not yet released.
std::pair< bool, std::optional< mlir::LocationAttr > > maybeStringToLocation(llvm::StringRef spelling, bool skipParsing, mlir::StringAttr &locatorFilenameCache, FileLineColLoc &fileLineColLocCache, MLIRContext *context)
constexpr FIRVersion exportFIRVersion
The version of FIRRTL that the exporter produces.
Definition FIRParser.h:151
llvm::raw_ostream & operator<<(llvm::raw_ostream &os, const InstanceInfo::LatticeValue &value)
mlir::OwningOpRef< mlir::ModuleOp > importFIRFile(llvm::SourceMgr &sourceMgr, mlir::MLIRContext *context, mlir::TimingScope &ts, FIRParserOptions options={})
constexpr FIRVersion missingSpecFIRVersion
A marker for parser features that are currently missing from the spec.
Definition FIRParser.h:147
constexpr FIRVersion minimumFIRVersion(2, 0, 0)
The current minimum version of FIRRTL that the parser supports.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
unsigned numAnnotationFiles
The number of annotation files that were specified on the command line.
Definition FIRParser.h:51
InfoLocHandling infoLocatorHandling
Definition FIRParser.h:47
std::vector< std::string > selectInstanceChoice
Definition FIRParser.h:59
std::optional< LayerSpecialization > defaultLayerSpecialization
Definition FIRParser.h:58
InfoLocHandling
Specify how @info locators should be handled.
Definition FIRParser.h:37
@ PreferInfo
Prefer @info locators, fallback to .fir locations.
@ FusedInfo
Attach both @info locators (when present) and .fir locations.
@ IgnoreInfo
If this is set to true, the @info locators are ignored, and the locations are set to the location in ...
std::vector< std::string > enableLayers
Definition FIRParser.h:56
std::vector< std::string > disableLayers
Definition FIRParser.h:57
The FIRRTL specification version.
Definition FIRParser.h:89
constexpr bool operator<(FIRVersion rhs) const
Definition FIRParser.h:97
static std::optional< FIRVersion > fromString(StringRef str)
Parse a version string of the form "major.minor.patch".
Definition FIRParser.h:115
constexpr bool operator>(FIRVersion rhs) const
Definition FIRParser.h:101
constexpr bool operator>=(FIRVersion rhs) const
Definition FIRParser.h:109
constexpr FIRVersion(uint16_t major, uint16_t minor, uint16_t patch)
Definition FIRParser.h:90
constexpr bool operator<=(FIRVersion rhs) const
Definition FIRParser.h:105