CIRCT 23.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 std::vector<std::string> enableLayers;
56 std::vector<std::string> disableLayers;
57 std::optional<LayerSpecialization> defaultLayerSpecialization;
58 std::vector<std::string> selectInstanceChoice;
59};
60
62 mlir::MLIRContext *context,
63 mlir::TimingScope &ts,
64 FIRParserOptions options = {});
65
66// Decode a source locator string `spelling`, returning a pair indicating that
67// the `spelling` was correct and an optional location attribute. The
68// `skipParsing` option can be used to short-circuit parsing and just do
69// validation of the `spelling`. This require both an Identifier and a
70// FileLineColLoc to use for caching purposes and context as the cache may be
71// updated with a new identifier.
72//
73// This utility exists because source locators can exist outside of normal
74// "parsing". E.g., these can show up in annotations or in Object Model 2.0
75// JSON.
76//
77// TODO: This API is super wacky and should be streamlined to hide the
78// caching.
79std::pair<bool, std::optional<mlir::LocationAttr>>
80maybeStringToLocation(llvm::StringRef spelling, bool skipParsing,
81 mlir::StringAttr &locatorFilenameCache,
82 FileLineColLoc &fileLineColLocCache,
83 MLIRContext *context);
84
86
87/// The FIRRTL specification version.
88struct FIRVersion {
89 constexpr FIRVersion(uint16_t major, uint16_t minor, uint16_t patch)
91
92 explicit constexpr operator uint64_t() const {
93 return uint64_t(major) << 32 | uint64_t(minor) << 16 | uint64_t(patch);
94 }
95
96 constexpr bool operator<(FIRVersion rhs) const {
97 return uint64_t(*this) < uint64_t(rhs);
98 }
99
100 constexpr bool operator>(FIRVersion rhs) const {
101 return uint64_t(*this) > uint64_t(rhs);
102 }
103
104 constexpr bool operator<=(FIRVersion rhs) const {
105 return uint64_t(*this) <= uint64_t(rhs);
106 }
107
108 constexpr bool operator>=(FIRVersion rhs) const {
109 return uint64_t(*this) >= uint64_t(rhs);
110 }
111
112 /// Parse a version string of the form "major.minor.patch". Returns
113 /// std::nullopt if the string is malformed or values overflow uint16_t.
114 static std::optional<FIRVersion> fromString(StringRef str) {
115 uint16_t major, minor, patch;
116 if (str.consumeInteger(10, major) || !str.consume_front(".") ||
117 str.consumeInteger(10, minor) || !str.consume_front(".") ||
118 str.consumeInteger(10, patch) || !str.empty())
119 return std::nullopt;
120 return FIRVersion(major, minor, patch);
121 }
122
123 uint16_t major;
124 uint16_t minor;
125 uint16_t patch;
126};
127
128/// The current minimum version of FIRRTL that the parser supports.
129constexpr FIRVersion minimumFIRVersion(2, 0, 0);
130
131/// The next version of FIRRTL that is not yet released.
132///
133/// Features use this version if they have been landed on the main branch of
134/// `chipsalliance/firrtl-spec`, but have not been part of a release yet. Once a
135/// new version of the spec is released, all uses of `nextFIRVersion` in the
136/// parser are replaced with the concrete version `{x, y, z}`, and this
137/// declaration here is bumped to the next probable version number.
138constexpr FIRVersion nextFIRVersion(5, 1, 0);
139
140/// A marker for parser features that are currently missing from the spec.
141///
142/// Features use this version if they have _not_ been added to the documentation
143/// in the `chipsalliance/firrtl-spec` repository. This allows us to distinguish
144/// features that are released in the next version of the spec and features that
145/// are still missing from the spec.
147
148/// The version of FIRRTL that the exporter produces. This is always the next
149/// version, since it contains any new developments.
151
152template <typename T>
153T &operator<<(T &os, FIRVersion version) {
154 return os << version.major << "." << version.minor << "." << version.patch;
155}
156
157} // namespace firrtl
158} // namespace circt
159
160#endif // CIRCT_DIALECT_FIRRTL_FIRPARSER_H
static std::unique_ptr< Context > context
void registerFromFIRFileTranslation()
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:150
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 nextFIRVersion(5, 1, 0)
The next version of FIRRTL that is not yet released.
constexpr FIRVersion missingSpecFIRVersion
A marker for parser features that are currently missing from the spec.
Definition FIRParser.h:146
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:58
std::optional< LayerSpecialization > defaultLayerSpecialization
Definition FIRParser.h:57
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:55
std::vector< std::string > disableLayers
Definition FIRParser.h:56
The FIRRTL specification version.
Definition FIRParser.h:88
constexpr bool operator<(FIRVersion rhs) const
Definition FIRParser.h:96
static std::optional< FIRVersion > fromString(StringRef str)
Parse a version string of the form "major.minor.patch".
Definition FIRParser.h:114
constexpr bool operator>(FIRVersion rhs) const
Definition FIRParser.h:100
constexpr bool operator>=(FIRVersion rhs) const
Definition FIRParser.h:108
constexpr FIRVersion(uint16_t major, uint16_t minor, uint16_t patch)
Definition FIRParser.h:89
constexpr bool operator<=(FIRVersion rhs) const
Definition FIRParser.h:104