CIRCT 23.0.0git
Loading...
Searching...
No Matches
FormatStrings.cpp
Go to the documentation of this file.
1//===- FormatStrings.cpp - Verilog format string 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/ast/SFormat.h"
11
12using namespace mlir;
13using namespace circt;
14using namespace ImportVerilog;
15using moore::IntAlign;
16using moore::IntFormat;
17using moore::IntPadding;
18using moore::RealFormat;
19using slang::ast::SFormat::FormatOptions;
20
21namespace {
22struct FormatStringParser {
23 Context &context;
24 OpBuilder &builder;
25 /// The remaining arguments to be parsed.
26 ArrayRef<const slang::ast::Expression *> arguments;
27 /// The current location to use for ops and diagnostics.
28 Location loc;
29 /// The default format for integer arguments not covered by a format string
30 /// literal.
31 IntFormat defaultFormat;
32 /// The interpolated string fragments that will be concatenated using a
33 /// `moore.fmt.concat` op.
34 SmallVector<Value> fragments;
35
36 FormatStringParser(Context &context,
37 ArrayRef<const slang::ast::Expression *> arguments,
38 Location loc, IntFormat defaultFormat)
39 : context(context), builder(context.builder), arguments(arguments),
40 loc(loc), defaultFormat(defaultFormat) {}
41
42 /// Entry point to the format string parser.
43 FailureOr<Value> parse(bool appendNewline) {
44 while (!arguments.empty()) {
45 const auto &arg = *arguments[0];
46 arguments = arguments.drop_front();
47 if (arg.kind == slang::ast::ExpressionKind::EmptyArgument)
48 continue;
49 loc = context.convertLocation(arg.sourceRange);
50 if (auto *lit = arg.as_if<slang::ast::StringLiteral>()) {
51 if (failed(parseFormat(lit->getValue())))
52 return failure();
53 } else {
54 if (failed(emitDefault(arg)))
55 return failure();
56 }
57 }
58
59 // Append the optional newline.
60 if (appendNewline)
61 emitLiteral("\n");
62
63 // Concatenate all string fragments into one formatted string, or return an
64 // empty literal if no fragments were generated.
65 if (fragments.empty())
66 return Value{};
67 if (fragments.size() == 1)
68 return fragments[0];
69 return moore::FormatConcatOp::create(builder, loc, fragments).getResult();
70 }
71
72 /// Parse a format string literal and consume and format the arguments
73 /// corresponding to the format specifiers it contains.
74 LogicalResult parseFormat(StringRef format) {
75 bool anyFailure = false;
76 auto onText = [&](auto text) {
77 if (anyFailure)
78 return;
79 emitLiteral(text);
80 };
81 auto onArg = [&](auto specifier, auto offset, auto len,
82 const auto &options) {
83 if (anyFailure)
84 return;
85 if (failed(emitArgument(specifier, format.substr(offset, len), options)))
86 anyFailure = true;
87 };
88 auto onError = [&](auto, auto, auto, auto) {
89 assert(false && "Slang should have already reported all errors");
90 };
91 slang::ast::SFormat::parse(format, onText, onArg, onError);
92 return failure(anyFailure);
93 }
94
95 /// Emit a string literal that requires no additional formatting.
96 void emitLiteral(StringRef literal) {
97 fragments.push_back(moore::FormatLiteralOp::create(builder, loc, literal));
98 }
99
100 /// Consume the next argument from the list and emit it according to the given
101 /// format specifier.
102 LogicalResult emitArgument(char specifier, StringRef fullSpecifier,
103 const FormatOptions &options) {
104 auto specifierLower = std::tolower(specifier);
105
106 // Special handling for format specifiers that consume no argument.
107 // %m/%M prints the hierarchical path of the module instance.
108 if (specifierLower == 'm') {
109 bool useEscapes = std::isupper(specifier);
110 fragments.push_back(
111 moore::FormatHierPathOp::create(builder, loc, useEscapes));
112 return success();
113 }
114
115 // %l prints the library and cell name of the scope; not yet supported.
116 if (specifierLower == 'l')
117 return mlir::emitError(loc)
118 << "unsupported format specifier `" << fullSpecifier << "`";
119
120 // Consume the next argument, which will provide the value to be
121 // formatted.
122 assert(!arguments.empty() && "Slang guarantees correct arg count");
123 const auto &arg = *arguments[0];
124 arguments = arguments.drop_front();
125
126 // Handle the different formatting options.
127 // See IEEE 1800-2017 § 21.2.1.2 "Format specifications".
128 switch (specifierLower) {
129 case 'b':
130 return emitInteger(arg, options, IntFormat::Binary);
131 case 'o':
132 return emitInteger(arg, options, IntFormat::Octal);
133 case 'd':
134 return emitInteger(arg, options, IntFormat::Decimal);
135 case 'h':
136 case 'x':
137 return emitInteger(arg, options,
138 std::isupper(specifier) ? IntFormat::HexUpper
139 : IntFormat::HexLower);
140
141 case 'e':
142 return emitReal(arg, options, RealFormat::Exponential);
143 case 'g':
144 return emitReal(arg, options, RealFormat::General);
145 case 'f':
146 return emitReal(arg, options, RealFormat::Float);
147
148 case 't':
149 return emitTime(arg, options);
150
151 case 's':
152 return emitString(arg, options);
153 case 'c':
154 return emitChar(arg, options);
155
156 default:
157 return mlir::emitError(loc)
158 << "unsupported format specifier `" << fullSpecifier << "`";
159 }
160 }
161
162 /// Emit an integer value with the given format.
163 LogicalResult emitInteger(const slang::ast::Expression &arg,
164 const FormatOptions &options, IntFormat format) {
165
166 Type intTy = {};
167 Value val;
168 auto rVal = context.convertRvalueExpression(arg);
169 // To infer whether or not the value is signed while printing as a decimal
170 // Since it only matters if it's a decimal, we add `format ==
171 // IntFormat::Decimal`
172 bool isSigned = arg.type->isSigned() && format == IntFormat::Decimal;
173 if (!rVal)
174 return failure();
175
176 // An IEEE 754 float number is represented using a sign bit s, n mantissa,
177 // and m exponent bits, representing (-1)**s * 1.fraction * 2**(E-bias).
178 // This means that the largest finite value is (2-2**(-n) * 2**(2**m-1)),
179 // just slightly less than ((2**(2**(m)))-1).
180 // Since we need signed value representation, we need integers that can
181 // represent values between [-(2**(2**(m))) ... (2**(2**(m)))-1], which
182 // requires an m+1 bit signed integer.
183 if (auto realTy = dyn_cast<moore::RealType>(rVal.getType())) {
184 if (realTy.getWidth() == moore::RealWidth::f32) {
185 // A 32 Bit IEEE 754 float number needs at most 129 integer bits
186 // (signed).
187 intTy = moore::IntType::getInt(context.getContext(), 129);
188 } else if (realTy.getWidth() == moore::RealWidth::f64) {
189 // A 64 Bit IEEE 754 float number needs at most 1025 integer bits
190 // (signed).
191 intTy = moore::IntType::getInt(context.getContext(), 1025);
192 } else
193 return failure();
194
195 val = moore::RealToIntOp::create(builder, loc, intTy, rVal);
196 } else {
197 val = rVal;
198 }
199
200 auto value = context.convertToSimpleBitVector(val);
201 if (!value)
202 return failure();
203
204 // Determine the alignment and padding.
205 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
206 auto padding =
207 format == IntFormat::Decimal ? IntPadding::Space : IntPadding::Zero;
208 IntegerAttr widthAttr = nullptr;
209 if (options.width) {
210 widthAttr = builder.getI32IntegerAttr(*options.width);
211 }
212
213 fragments.push_back(moore::FormatIntOp::create(
214 builder, loc, value, format, alignment, padding, widthAttr, isSigned));
215 return success();
216 }
217
218 LogicalResult emitReal(const slang::ast::Expression &arg,
219 const FormatOptions &options, RealFormat format) {
220
221 // Ensures that the given value is moore.real
222 // i.e. $display("%f", 4) -> 4.000000, but 4 is not necessarily of real type
223 auto value = context.convertRvalueExpression(
224 arg, moore::RealType::get(context.getContext(), moore::RealWidth::f64));
225
226 IntegerAttr widthAttr = nullptr;
227 if (options.width) {
228 widthAttr = builder.getI32IntegerAttr(*options.width);
229 }
230
231 IntegerAttr precisionAttr = nullptr;
232 if (options.precision) {
233 if (*options.precision)
234 precisionAttr = builder.getI32IntegerAttr(*options.precision);
235 else
236 // If precision is 0, we set it to 1 instead
237 precisionAttr = builder.getI32IntegerAttr(1);
238 }
239
240 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
241 if (!value)
242 return failure();
243
244 fragments.push_back(moore::FormatRealOp::create(
245 builder, loc, value, format, alignment, widthAttr, precisionAttr));
246
247 return success();
248 }
249
250 // Format an integer with the %t specifier according to IEEE 1800-2023
251 // § 20.4.3 "$timeformat". We currently don't support user-defined time
252 // formats. Instead, we just convert the time to an integer and print it. This
253 // applies the local timeunit/timescale and seem to be inline with what
254 // Verilator does.
255 LogicalResult emitTime(const slang::ast::Expression &arg,
256 const FormatOptions &options) {
257 // Handle the time argument and convert it to a 64 bit integer.
258 auto value = context.convertRvalueExpression(
259 arg, moore::IntType::getInt(context.getContext(), 64));
260 if (!value)
261 return failure();
262
263 // Create an integer formatting fragment.
264 uint32_t width = 20; // default $timeformat field width
265 if (options.width)
266 width = *options.width;
267 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
268 auto padding = options.zeroPad ? IntPadding::Zero : IntPadding::Space;
269 fragments.push_back(moore::FormatIntOp::create(
270 builder, loc, value, IntFormat::Decimal, alignment, padding,
271 builder.getI32IntegerAttr(width)));
272 return success();
273 }
274
275 LogicalResult emitString(const slang::ast::Expression &arg,
276 const FormatOptions &options) {
277 if (options.width)
278 return mlir::emitError(loc)
279 << "string format specifier with width not supported";
280
281 // Simplified handling for literals.
282 if (auto *lit = arg.as_if<slang::ast::StringLiteral>()) {
283 emitLiteral(lit->getValue());
284 return success();
285 }
286
287 // Handle expressions
288 if (auto value = context.convertRvalueExpression(
289 arg, builder.getType<moore::FormatStringType>())) {
290 fragments.push_back(value);
291 return success();
292 }
293
294 return mlir::emitError(context.convertLocation(arg.sourceRange))
295 << "expression cannot be formatted as string";
296 }
297
298 LogicalResult emitChar(const slang::ast::Expression &arg,
299 const FormatOptions &options) {
300 if (options.width)
301 return mlir::emitError(loc)
302 << "character format specifier with width not supported";
303
304 auto value = context.convertRvalueExpression(arg);
305 if (!value)
306 return failure();
307
308 auto bitValue = context.convertToSimpleBitVector(value);
309 if (!bitValue)
310 return failure();
311
312 fragments.push_back(moore::FormatCharOp::create(builder, loc, bitValue));
313 return success();
314 }
315
316 /// Emit an expression argument with the appropriate default formatting.
317 LogicalResult emitDefault(const slang::ast::Expression &expr) {
318 FormatOptions options;
319 // Without an explicit format string, default formatting is not limited to
320 // integers, the string-typed arguments also be concerned.
321 if (expr.type->isString())
322 return emitString(expr, options);
323 return emitInteger(expr, options, defaultFormat);
324 }
325};
326} // namespace
327
328FailureOr<Value> Context::convertFormatString(
329 std::span<const slang::ast::Expression *const> arguments, Location loc,
330 IntFormat defaultFormat, bool appendNewline) {
331 FormatStringParser parser(*this, ArrayRef(arguments.data(), arguments.size()),
332 loc, defaultFormat);
333 return parser.parse(appendNewline);
334}
assert(baseType &&"element must be base type")
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
A helper class to facilitate the conversion from a Slang AST to MLIR operations.
Value convertRvalueExpression(const slang::ast::Expression &expr, Type requiredType={})
Value convertToSimpleBitVector(Value value)
Helper function to convert a value to its simple bit vector representation, if it has one.
MLIRContext * getContext()
Return the MLIR context.
Location convertLocation(slang::SourceLocation loc)
Convert a slang SourceLocation into an MLIR Location.