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);
155 return mlir::emitError(loc)
156 <<
"unsupported format specifier `" << fullSpecifier <<
"`";
161 LogicalResult emitInteger(
const slang::ast::Expression &arg,
162 const FormatOptions &options, IntFormat format) {
170 bool isSigned = arg.type->isSigned() && format == IntFormat::Decimal;
181 if (
auto realTy = dyn_cast<moore::RealType>(rVal.getType())) {
182 if (realTy.getWidth() == moore::RealWidth::f32) {
185 intTy = moore::IntType::getInt(context.
getContext(), 129);
186 }
else if (realTy.getWidth() == moore::RealWidth::f64) {
189 intTy = moore::IntType::getInt(context.
getContext(), 1025);
193 val = moore::RealToIntOp::create(builder, loc, intTy, rVal);
203 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
205 format == IntFormat::Decimal ? IntPadding::Space : IntPadding::Zero;
206 IntegerAttr widthAttr =
nullptr;
208 widthAttr = builder.getI32IntegerAttr(*options.width);
211 fragments.push_back(moore::FormatIntOp::create(
212 builder, loc, value, format, alignment, padding, widthAttr, isSigned));
216 LogicalResult emitReal(
const slang::ast::Expression &arg,
217 const FormatOptions &options, RealFormat format) {
222 arg, moore::RealType::get(context.
getContext(), moore::RealWidth::f64));
224 IntegerAttr widthAttr =
nullptr;
226 widthAttr = builder.getI32IntegerAttr(*options.width);
229 IntegerAttr precisionAttr =
nullptr;
230 if (options.precision) {
231 if (*options.precision)
232 precisionAttr = builder.getI32IntegerAttr(*options.precision);
235 precisionAttr = builder.getI32IntegerAttr(1);
238 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
242 fragments.push_back(moore::FormatRealOp::create(
243 builder, loc, value, format, alignment, widthAttr, precisionAttr));
253 LogicalResult emitTime(
const slang::ast::Expression &arg,
254 const FormatOptions &options) {
257 arg, moore::IntType::getInt(context.
getContext(), 64));
264 width = *options.width;
265 auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
266 auto padding = options.zeroPad ? IntPadding::Zero : IntPadding::Space;
267 fragments.push_back(moore::FormatIntOp::create(
268 builder, loc, value, IntFormat::Decimal, alignment, padding,
269 builder.getI32IntegerAttr(width)));
273 LogicalResult emitString(
const slang::ast::Expression &arg,
274 const FormatOptions &options) {
276 return mlir::emitError(loc)
277 <<
"string format specifier with width not supported";
280 if (
auto *
lit = arg.as_if<slang::ast::StringLiteral>()) {
281 emitLiteral(
lit->getValue());
287 arg, builder.getType<moore::FormatStringType>())) {
288 fragments.push_back(value);
293 <<
"expression cannot be formatted as string";
297 LogicalResult emitDefault(
const slang::ast::Expression &expr) {
298 FormatOptions options;
299 return emitInteger(expr, options, defaultFormat);
304FailureOr<Value> Context::convertFormatString(
305 std::span<const slang::ast::Expression *const> arguments, Location loc,
306 IntFormat defaultFormat,
bool appendNewline) {
307 FormatStringParser parser(*
this, ArrayRef(arguments.data(), arguments.size()),
309 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.