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);
107 if (specifierLower ==
'm' || specifierLower ==
'l')
108 return mlir::emitError(loc)
109 <<
"unsupported format specifier `" << fullSpecifier <<
"`";
113 assert(!arguments.empty() &&
"Slang guarantees correct arg count");
114 const auto &arg = *arguments[0];
115 arguments = arguments.drop_front();
120 switch (specifierLower) {
122 return emitInteger(arg, options, IntFormat::Binary);
124 return emitInteger(arg, options, IntFormat::Octal);
126 return emitInteger(arg, options, IntFormat::Decimal);
129 return emitInteger(arg, options,
130 std::isupper(specifier) ? IntFormat::HexUpper
131 : IntFormat::HexLower);
136 return emitReal(arg, options, RealFormat::Float);
140 if (
auto *lit = arg.as_if<slang::ast::StringLiteral>()) {
142 return mlir::emitError(loc)
143 <<
"string format specifier with width not supported";
144 emitLiteral(lit->getValue());
147 return mlir::emitError(argLoc)
148 <<
"expression cannot be formatted as string";
151 return mlir::emitError(loc)
152 <<
"unsupported format specifier `" << fullSpecifier <<
"`";
157 LogicalResult emitInteger(
const slang::ast::Expression &arg,
158 const FormatOptions &options, IntFormat format) {
167 width = *options.width;
169 width = cast<moore::IntType>(value.getType()).getWidth();
170 if (format == IntFormat::Octal)
172 width = (width + 2) / 3;
173 else if (format == IntFormat::HexLower || format == IntFormat::HexUpper)
175 width = (width + 3) / 4;
176 else if (format == IntFormat::Decimal)
178 width = std::ceil(width * std::log(2) / std::log(10));
182 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
184 format == IntFormat::Decimal ? IntPadding::Space : IntPadding::Zero;
186 fragments.push_back(moore::FormatIntOp::create(builder, loc, value, format,
187 width, alignment, padding));
191 LogicalResult emitReal(
const slang::ast::Expression &arg,
192 const FormatOptions &options, RealFormat format) {
197 arg, moore::RealType::get(context.
getContext()));
205 moore::FormatRealOp::create(builder, loc, value, format));
211 LogicalResult emitDefault(
const slang::ast::Expression &expr) {
212 FormatOptions options;
213 return emitInteger(expr, options, defaultFormat);
218FailureOr<Value> Context::convertFormatString(
219 std::span<const slang::ast::Expression *const> arguments, Location loc,
220 IntFormat defaultFormat,
bool appendNewline) {
221 FormatStringParser parser(*
this, ArrayRef(arguments.data(), arguments.size()),
223 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.