Skip to content

lang0: split Conv into multiple operators - #160

Merged
saem merged 6 commits into
nim-works:mainfrom
zerbina:il-split-conv
May 3, 2025
Merged

lang0: split Conv into multiple operators#160
saem merged 6 commits into
nim-works:mainfrom
zerbina:il-split-conv

Conversation

@zerbina

@zerbina zerbina commented May 3, 2025

Copy link
Copy Markdown
Collaborator

Summary

Add the Zext, Sext, Trunc, Promote, and Demote operators.
Conv still exists, but is now only used for int-to-float and float-
to-int conversions.

Details

The previous Conv operator was modeled after the conversion operator
of NimSkull's MIR, making it fairly high-level and leaving too much
behaviour-related decision making to pass0.

It's split into multiple, lower-level operators, making the final
translation easier and also providing earlier transformation stages with
more precise conversion operators.

skully is changed to:

  • use the new operators instead of Conv (where applicable)
  • translate pointer-to-pointer conversions to IL no ops (as all
    pointers are translated to the same IL type)

Finally, tests for the new L0 operators are added, except for Conv,
whose semantics aren't very well-defined at the moment.


Notes For Reviewers

A preparation for #146, but the new operators should be helpful in general, especially once the source language starts to support numeric conversions.

zerbina added 6 commits May 3, 2025 21:59
Instead of overloading `Conv` with many different meanings, it only
performs float <-> int conversion now, with the other meanings taken
over by new operators.
The code is largely taken from the previous `Conv` implementation.
`Conv` only does int-to-float and float-to-int conversion now, and the
implementation is changed accordingly.
Only the aggregate parameter lowering has code that needs to handle the
new operators. Since they have the same shape as `Conv`, they're simply
appended to the `of` branches already existing for `Conv`.
* replace usage of `Conv` with the new operators (where applicable)
* don't emit conversions for pointers types
* use the new operators for implementing MIR cast
... except for `Conv`, as its semantics aren't very well defined at the
moment.
@zerbina zerbina added the enhancement New feature or request label May 3, 2025

@saem saem left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer source then destination (reading things as "from to") as the order, but if there is a good reason not to do that, I get it.

@zerbina

zerbina commented May 3, 2025

Copy link
Copy Markdown
Collaborator Author

I personally prefer source then destination (reading things as "from to") as the order, but if there is a good reason not to do that, I get it.

I chose the current order for reasons of regularity.

For most other operators that return a value (e.g., Add, Sub, BitAnd, etc.), the type of the result comes as the first operand (they don't have a second type operand), so when designing the initial Conv operator, I opted for having the destination type come first, so that child(n, 0) for a Conv tree also returns the type operand representing the type of the operation.

There are some exceptions to the "first type operand is result type" rule, however, namely the comparison operators, Not, and Call.

@saem
saem merged commit bbc7fb0 into nim-works:main May 3, 2025
@zerbina
zerbina deleted the il-split-conv branch May 4, 2025 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants