-
Notifications
You must be signed in to change notification settings - Fork 404
[ImportVerilog][Moore] Add support for %t format specifier, introduce moore.fmt.time
#8979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
8f72551 to
7b45def
Compare
7b45def to
ae2423b
Compare
This PR adds support for using the %t format specifier in format strings by mapping them to the newly introduced moore.fmt.time / moore::TimeFormatOp in the ImportVerilog string formatter. According to the system verilog spec IEEE 1800-2023 § 20.4.3 "$timeformat", the exact formatting for any time value is dictated by the last $timeformat system task invoked within the scope or parent scopes of the format string, or alternatively, by a mix of constant default values and The smallest time precision argument of all the `timescale compiler directives in the source description. Because of the formatting relying on context (who came up with this anyway? seems like a bad idea), and use of $timeformat is permitted in conditional settings, meaning it can't always be determined statically, I decided to not add a format attribute to moore::FormatTimeOp. I only added the width attribute, which may be defined in the formatter expression, e.g. %4t. Instead the semantics for implementing moore.fmt.time should rely on querying some global state that models the configuration set by the latest $timeformat system call within the scope. What's not fully clear to me is whether the System Verilog spec allows non-"time typed" values to be formatted with %t; I didn't find an explicit description of allowed values, but all examples in the spec I could find explicitly use the return value of a time-typed system task (e.g. $time) or a time-type parameter (e.g. parameter time _time = 100ns;). While it seems reasonable to suppose that also integer values are allowed (per the spec $time returns a 64-bit integer representing time), some examples in the doc also allow the use of $realtime (which returns a real value) with %t, so potentially any numeric value is allowed? In either case I didn't add this in the current implementation and instead only allow TimeType values, which in the context of Moore looked like the sanest option to me. Maybe someone has a clearer view on this.
ae2423b to
5893656
Compare
moore.fmt.timemoore.fmt.time
fabianschuiki
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool! I love the idea of keeping this simple and just work on TimeType. That will allow us to handle any $timeformat related business later on somewhere else.
| // Only handle `TimeType` values. | ||
| auto value = context.convertRvalueExpression( | ||
| arg, moore::TimeType::get(context.getContext())); | ||
| if (!value) | ||
| return failure(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I think this will already handle integers properly, too. The conversion will insert casts from integers to the time type 🚀
fabianschuiki
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor tweaks for easy commit.
This PR adds support for using the
%tformat specifier in format strings by mapping them to the newly introducedmoore.fmt.time / moore::TimeFormatOpin the ImportVerilog string formatter.According to the system verilog spec IEEE 1800-2023 § 20.4.3 "$timeformat", the exact formatting for any time value is dictated by the last
$timeformatsystem task invoked within the scope or parent scopes of the format string, or alternatively, by a mix of constant default values andBecause of the formatting relying on context (who came up with this anyway? seems like a bad idea), and use of
$timeformatis permitted in conditional settings, meaning it can't always be determined statically, I decided to not add a format attribute tomoore::FormatTimeOp. I only added thewidthattribute, which may be defined in the formatter expression, e.g.%4t. Instead the semantics for implementingmoore.fmt.timeshould rely on querying some global state that models the configuration set by the latest$timeformatsystem call within the scope.What's not fully clear to me is whether the System Verilog spec allows non-"time typed" values to be formatted with
%t; I didn't find an explicit description of allowed values, but all examples in the spec I could find explicitly use the return value of a time-typed system task (e.g.$time) or a time-type parameter (e.g.parameter time _time = 100ns;).While it seems reasonable to suppose that also integer values are allowed (per the spec
$timereturns a 64-bit integer representing time), some examples in the doc also allow the use of$realtime(which returns arealvalue) with%t, so potentially any numeric value is allowed? In either case I didn't add this in the current implementation and instead only allowTimeTypevalues, which in the context of Moore looked like the sanest option to me. Maybe someone has a clearer view on this.