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);
133 return emitReal(arg, options, RealFormat::Exponential);
135 return emitReal(arg, options, RealFormat::General);
137 return emitReal(arg, options, RealFormat::Float);
140 return emitTime(arg, options);
143 return emitString(arg, options);
146 return mlir::emitError(loc)
147 <<
"unsupported format specifier `" << fullSpecifier <<
"`";
152 LogicalResult emitInteger(
const slang::ast::Expression &arg,
153 const FormatOptions &options, IntFormat format) {
161 bool isSigned = arg.type->isSigned() && format == IntFormat::Decimal;
172 if (
auto realTy = dyn_cast<moore::RealType>(rVal.getType())) {
173 if (realTy.getWidth() == moore::RealWidth::f32) {
176 intTy = moore::IntType::getInt(context.
getContext(), 129);
177 }
else if (realTy.getWidth() == moore::RealWidth::f64) {
180 intTy = moore::IntType::getInt(context.
getContext(), 1025);
184 val = moore::RealToIntOp::create(builder, loc, intTy, rVal);
194 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
196 format == IntFormat::Decimal ? IntPadding::Space : IntPadding::Zero;
197 IntegerAttr widthAttr =
nullptr;
199 widthAttr = builder.getI32IntegerAttr(*options.width);
202 fragments.push_back(moore::FormatIntOp::create(
203 builder, loc, value, format, alignment, padding, widthAttr, isSigned));
207 LogicalResult emitReal(
const slang::ast::Expression &arg,
208 const FormatOptions &options, RealFormat format) {
213 arg, moore::RealType::get(context.
getContext(), moore::RealWidth::f64));
215 IntegerAttr widthAttr =
nullptr;
217 widthAttr = builder.getI32IntegerAttr(*options.width);
220 IntegerAttr precisionAttr =
nullptr;
221 if (options.precision) {
222 if (*options.precision)
223 precisionAttr = builder.getI32IntegerAttr(*options.precision);
226 precisionAttr = builder.getI32IntegerAttr(1);
229 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
233 fragments.push_back(moore::FormatRealOp::create(
234 builder, loc, value, format, alignment, widthAttr, precisionAttr));
244 LogicalResult emitTime(
const slang::ast::Expression &arg,
245 const FormatOptions &options) {
248 arg, moore::IntType::getInt(context.
getContext(), 64));
255 width = *options.width;
256 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
257 auto padding = options.zeroPad ? IntPadding::Zero : IntPadding::Space;
258 fragments.push_back(moore::FormatIntOp::create(
259 builder, loc, value, IntFormat::Decimal, alignment, padding,
260 builder.getI32IntegerAttr(width)));
264 LogicalResult emitString(
const slang::ast::Expression &arg,
265 const FormatOptions &options) {
267 return mlir::emitError(loc)
268 <<
"string format specifier with width not supported";
271 if (
auto *lit = arg.as_if<slang::ast::StringLiteral>()) {
272 emitLiteral(lit->getValue());
278 arg, builder.getType<moore::FormatStringType>())) {
279 fragments.push_back(value);
284 <<
"expression cannot be formatted as string";
288 LogicalResult emitDefault(
const slang::ast::Expression &expr) {
289 FormatOptions options;
290 return emitInteger(expr, options, defaultFormat);
295FailureOr<Value> Context::convertFormatString(
296 std::span<const slang::ast::Expression *const> arguments, Location loc,
297 IntFormat defaultFormat,
bool appendNewline) {
298 FormatStringParser parser(*
this, ArrayRef(arguments.data(), arguments.size()),
300 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.