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();
119 switch (specifierLower) {
121 return emitInteger(arg, options, IntFormat::Binary);
123 return emitInteger(arg, options, IntFormat::Octal);
125 return emitInteger(arg, options, IntFormat::Decimal);
128 return emitInteger(arg, options,
129 std::isupper(specifier) ? IntFormat::HexUpper
130 : IntFormat::HexLower);
135 return emitReal(arg, options, RealFormat::Float);
138 return emitTime(arg, options);
141 return emitString(arg, options);
144 return mlir::emitError(loc)
145 <<
"unsupported format specifier `" << fullSpecifier <<
"`";
150 LogicalResult emitInteger(
const slang::ast::Expression &arg,
151 const FormatOptions &options, IntFormat format) {
160 width = *options.width;
162 width = cast<moore::IntType>(value.getType()).getWidth();
163 if (format == IntFormat::Octal)
165 width = (width + 2) / 3;
166 else if (format == IntFormat::HexLower || format == IntFormat::HexUpper)
168 width = (width + 3) / 4;
169 else if (format == IntFormat::Decimal)
171 width = std::ceil(width * std::log(2) / std::log(10));
175 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
177 format == IntFormat::Decimal ? IntPadding::Space : IntPadding::Zero;
179 fragments.push_back(moore::FormatIntOp::create(builder, loc, value, format,
180 width, alignment, padding));
184 LogicalResult emitReal(
const slang::ast::Expression &arg,
185 const FormatOptions &options, RealFormat format) {
190 arg, moore::RealType::get(context.
getContext(), moore::RealWidth::f64));
198 moore::FormatRealOp::create(builder, loc, value, format));
205 LogicalResult emitTime(
const slang::ast::Expression &arg,
206 const FormatOptions &options) {
210 arg, moore::TimeType::get(context.
getContext()));
214 mlir::IntegerAttr width =
nullptr;
217 mlir::IntegerType::get(context.
getContext(), 32);
218 width = mlir::IntegerAttr::get(i32Ty, options.width.value());
225 moore::FormatTimeOp::create(builder, loc, value, width));
227 fragments.push_back(moore::FormatTimeOp::create(builder, loc, value));
233 LogicalResult emitString(
const slang::ast::Expression &arg,
234 const FormatOptions &options) {
236 return mlir::emitError(loc)
237 <<
"string format specifier with width not supported";
240 if (
auto *lit = arg.as_if<slang::ast::StringLiteral>()) {
241 emitLiteral(lit->getValue());
247 arg, builder.getType<moore::FormatStringType>())) {
248 fragments.push_back(value);
253 <<
"expression cannot be formatted as string";
257 LogicalResult emitDefault(
const slang::ast::Expression &expr) {
258 FormatOptions options;
259 return emitInteger(expr, options, defaultFormat);
264FailureOr<Value> Context::convertFormatString(
265 std::span<const slang::ast::Expression *const> arguments, Location loc,
266 IntFormat defaultFormat,
bool appendNewline) {
267 FormatStringParser parser(*
this, ArrayRef(arguments.data(), arguments.size()),
269 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.