Post Reply 
Unary minus precedence preference
06-01-2024, 08:47 PM (This post was last modified: 06-01-2024 08:51 PM by Thomas Okken.)
Post: #34
RE: Unary minus precedence preference
(06-01-2024 08:12 PM)mfleming Wrote:  In support, few processors that I'm aware of store numerics as strictly positive values and require a negation operation in a calculation, though there may be a remote chance that one exists* Wink

That's not really an issue, though. For example, in Plus42, at one point I added explicit support for negative numbers, simply by adding a special case for the code generation logic for unary minus, where it checked whether the argument was a literal, and transformed that literal into its negative, before emitting the executable code. (I never released that optimization because it made the correspondence between source code and generated code in TRACE mode too confusing for my taste, and the performance benefit was too tiny to be worth it.)

So, for example, -2 would be parsed as unary_minus(literal(2)) and then optimized to literal(-2). Doing the optimization after parsing was what made it safe: -2^2 is parsed as unary_minus(pow(literal(2), literal(2))), so now the unary_minus(literal(X)) to literal(-X) won't be performed, because the pattern doesn't match.

Separating optimizations like this from the language that the user writes in is part of what compilers are supposed to do.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Unary minus precedence preference - Thomas Okken - 06-01-2024 08:47 PM



User(s) browsing this thread: 3 Guest(s)