-
Notifications
You must be signed in to change notification settings - Fork 39
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
support uint64
for case statement operands
#820
Merged
chore-runner
merged 7 commits into
nim-works:devel
from
zerbina:support-uint64-for-case-stmt
Jul 31, 2023
Merged
support uint64
for case statement operands
#820
chore-runner
merged 7 commits into
nim-works:devel
from
zerbina:support-uint64-for-case-stmt
Jul 31, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The `uint64` is considered an ordinal type by `isOrdinal`, and not supporting it here seems like an oversight.
Keep the `firstOrd` result as an `Int128`, fixing the compiler crashing with integer overflow defects when the `first` + element value is outside the `int64` range. In addition, use `let` where it makes sense, and replace appending to the `nkRange` node with setting the elements directly.
Move the logic into a standalone procedure, in preparation for the following fix. In addition, integer-literal nodes reaching the C code generator are now required to be typed.
For generating code for `of`-branches, don't operate with the signed `intVal` values, but turn them into `Int128` values first. When using C compilers that don't support case ranges, this fixes the compiler crashing with overflow defects.
* move code-generation for integer literals into a standalone procedure * same as with the C code generator, use `Int128` values when iterating over the range
Compiling it doesn't crash the compiler anymore, at least.
zerbina
added
bug
Something isn't working
enhancement
New feature or request
compiler/sem
Related to semantic-analysis system of the compiler
compiler/backend
Related to backend system of the compiler
labels
Jul 30, 2023
/merge |
Merge requested by: @saem Contents after the first section break of the PR description has been removed and preserved below:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
bug
Something isn't working
compiler/backend
Related to backend system of the compiler
compiler/sem
Related to semantic-analysis system of the compiler
enhancement
New feature or request
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Allow
uint64
values to be used ascase
-statement operands and fixmultiple compiler crashes with
uint
of
-branch labels outside theint64
range. Previously, trying to use auint64
value with casestatements resulted in a "selector must be of an ordinal type" error.
uint64
values can now be used ascase
-statement operands (outsideof objects)
of
-branches don't crash the compilerwhen the range of the element value crosses
high(int64)
..
ranges inof
-branches don'tcrash the compiler when the lower bound is less-than-or-equal to,
and the upper bound greater than
high(int64)
high(int64)
are not treated as-1 - high(uint64) - value
anymore
Details
uint64
being disallowed ascase
-statement operands seems to havebeen a leftover from when
uint64
was not considered an ordinal type.Since it is considered an ordinal type now, not allowing it there is
inconsistent.
The compiler crashes were all caused by signed integer overflow
defects. All integer values are stored as
BiggestInt
(signed) inPNode
, even unsigned values. Prior to working with them, they have tobe either casted to an unsigned integer (in case the node represents
an unsigned value) or turned into an
Int128
value viagetOrdValue
/getInt
(both which take care of properly reinterpreting the value).This was missing in
toTreeSet
, which is called when removingduplicate elements from set literals used as
of
-branch labels.Instead of turning the sets' lower bound (
firstOrd
) into a signedinteger, the value is now kept as an
Int128
, fixing the overflowdefect.
The other problems where with the C and JavaScript code generators,
both which were using the nodes
intVal
directly when iterating overnkRange
s -- they now useInt128
values. In order to not havingto create a temporary
PNode
, the integer-literal rendering logicfor both code generators is moved to standalone procedures that take
an
Int128
value directly (which for the JavaScript backend fixeslarge unsigned values being inverted).
Instead of inferring the integer type from the node kind,
cgen
nowrequires all integer literals to be typed, which is preparation for the
code-generator IR.
When using the VM backend,
of
-branches with ranges crossinghigh(int64)
still don't work correctly, as the VM uses signedintegers for the comparisons. Fixing this requires a larger rethinking
of the VM's case statement support, and it is thus left as is for now.