10#include "slang/ast/SFormat.h"
14using namespace ImportVerilog;
16using moore::IntFormat;
17using moore::IntPadding;
18using moore::RealFormat;
19using slang::ast::SFormat::FormatOptions;
22struct FormatStringParser {
26 ArrayRef<const slang::ast::Expression *> arguments;
31 IntFormat defaultFormat;
34 SmallVector<Value> fragments;
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) {}
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)
50 if (
auto *
lit = arg.as_if<slang::ast::StringLiteral>()) {
51 if (failed(parseFormat(
lit->getValue())))
54 if (failed(emitDefault(arg)))
65 if (fragments.empty())
67 if (fragments.size() == 1)
69 return moore::FormatConcatOp::create(builder, loc, fragments).getResult();
74 LogicalResult parseFormat(StringRef format) {
75 bool anyFailure =
false;
76 auto onText = [&](
auto text) {
81 auto onArg = [&](
auto specifier,
auto offset,
auto len,
82 const auto &options) {
85 if (failed(emitArgument(specifier, format.substr(offset, len), options)))
88 auto onError = [&](
auto,
auto,
auto,
auto) {
89 assert(
false &&
"Slang should have already reported all errors");
91 slang::ast::SFormat::parse(format, onText, onArg, onError);
92 return failure(anyFailure);
96 void emitLiteral(StringRef literal) {
97 fragments.push_back(moore::FormatLiteralOp::create(builder, loc, literal));
102 LogicalResult emitArgument(
char specifier, StringRef fullSpecifier,
103 const FormatOptions &options) {
104 auto specifierLower = std::tolower(specifier);
108 if (specifierLower ==
'm') {
109 bool useEscapes = std::isupper(specifier);
111 moore::FormatHierPathOp::create(builder, loc, useEscapes));
116 if (specifierLower ==
'l')
117 return mlir::emitError(loc)
118 <<
"unsupported format specifier `" << fullSpecifier <<
"`";
122 assert(!arguments.empty() &&
"Slang guarantees correct arg count");
123 const auto &arg = *arguments[0];
124 arguments = arguments.drop_front();
128 switch (specifierLower) {
130 return emitInteger(arg, options, IntFormat::Binary);
132 return emitInteger(arg, options, IntFormat::Octal);
134 return emitInteger(arg, options, IntFormat::Decimal);
137 return emitInteger(arg, options,
138 std::isupper(specifier) ? IntFormat::HexUpper
139 : IntFormat::HexLower);
142 return emitReal(arg, options, RealFormat::Exponential);
144 return emitReal(arg, options, RealFormat::General);
146 return emitReal(arg, options, RealFormat::Float);
149 return emitTime(arg, options);
152 return emitString(arg, options);
154 return emitChar(arg, options);
157 return mlir::emitError(loc)
158 <<
"unsupported format specifier `" << fullSpecifier <<
"`";
163 LogicalResult emitInteger(
const slang::ast::Expression &arg,
164 const FormatOptions &options, IntFormat format) {
172 bool isSigned = arg.type->isSigned() && format == IntFormat::Decimal;
183 if (
auto realTy = dyn_cast<moore::RealType>(rVal.getType())) {
184 if (realTy.getWidth() == moore::RealWidth::f32) {
187 intTy = moore::IntType::getInt(context.
getContext(), 129);
188 }
else if (realTy.getWidth() == moore::RealWidth::f64) {
191 intTy = moore::IntType::getInt(context.
getContext(), 1025);
195 val = moore::RealToIntOp::create(builder, loc, intTy, rVal);
205 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
207 format == IntFormat::Decimal ? IntPadding::Space : IntPadding::Zero;
208 IntegerAttr widthAttr =
nullptr;
210 widthAttr = builder.getI32IntegerAttr(*options.width);
213 fragments.push_back(moore::FormatIntOp::create(
214 builder, loc, value, format, alignment, padding, widthAttr, isSigned));
218 LogicalResult emitReal(
const slang::ast::Expression &arg,
219 const FormatOptions &options, RealFormat format) {
224 arg, moore::RealType::get(context.
getContext(), moore::RealWidth::f64));
226 IntegerAttr widthAttr =
nullptr;
228 widthAttr = builder.getI32IntegerAttr(*options.width);
231 IntegerAttr precisionAttr =
nullptr;
232 if (options.precision) {
233 if (*options.precision)
234 precisionAttr = builder.getI32IntegerAttr(*options.precision);
237 precisionAttr = builder.getI32IntegerAttr(1);
240 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
244 fragments.push_back(moore::FormatRealOp::create(
245 builder, loc, value, format, alignment, widthAttr, precisionAttr));
255 LogicalResult emitTime(
const slang::ast::Expression &arg,
256 const FormatOptions &options) {
259 arg, moore::IntType::getInt(context.
getContext(), 64));
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)));
275 LogicalResult emitString(
const slang::ast::Expression &arg,
276 const FormatOptions &options) {
278 return mlir::emitError(loc)
279 <<
"string format specifier with width not supported";
282 if (
auto *
lit = arg.as_if<slang::ast::StringLiteral>()) {
283 emitLiteral(
lit->getValue());
289 arg, builder.getType<moore::FormatStringType>())) {
290 fragments.push_back(value);
295 <<
"expression cannot be formatted as string";
298 LogicalResult emitChar(
const slang::ast::Expression &arg,
299 const FormatOptions &options) {
301 return mlir::emitError(loc)
302 <<
"character format specifier with width not supported";
312 fragments.push_back(moore::FormatCharOp::create(builder, loc, bitValue));
317 LogicalResult emitDefault(
const slang::ast::Expression &expr) {
318 FormatOptions options;
321 if (expr.type->isString())
322 return emitString(expr, options);
323 return emitInteger(expr, options, defaultFormat);
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()),
333 return parser.parse(appendNewline);
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.