Repository navigation
feat: Better constants in generated code - #2363
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Constant emission regresses narrow-width support and can incorrectly type smaller UINT32 literals as signed integers.
Review effort: Balanced
Findings: 2
Open (2)
What changed in this PR
Simplifies generated C# constants by removing redundant casts while preserving typed conversion semantics.
Changes:
- Adds compact constant rendering and constant-folded conversions.
- Adds tests for literal formatting, masks, casts, and unary precedence.
- Regenerates generated-code golden files.
| File | Description |
|---|---|
GeneratedCode/sub.spec.cs.txt |
Updates subtraction golden. |
GeneratedCode/strings.spec.cs.txt |
Updates string-operation golden. |
GeneratedCode/stipending.pit.spec.cs.txt |
Updates interrupt timing golden. |
GeneratedCode/sticli.spec.cs.txt |
Updates STI/CLI golden. |
GeneratedCode/speculative_smc_guard.spec.cs.txt |
Updates speculative SMC golden. |
GeneratedCode/speculative_mixed_block.spec.cs.txt |
Updates mixed-block golden. |
GeneratedCode/speculative_invalid_opcode.spec.cs.txt |
Updates invalid-opcode golden. |
GeneratedCode/speculative_discard.spec.cs.txt |
Updates speculative-discard golden. |
GeneratedCode/speculative_convergence.spec.cs.txt |
Updates convergence golden. |
GeneratedCode/speculative_closure.spec.cs.txt |
Updates closure golden. |
GeneratedCode/speculative_call_entry.spec.cs.txt |
Updates call-entry golden. |
GeneratedCode/speculative_branch.spec.cs.txt |
Updates branch golden. |
GeneratedCode/shifts.spec.cs.txt |
Updates shift-operation golden. |
GeneratedCode/selfmodifyvalue.spec.cs.txt |
Updates self-modifying-value golden. |
GeneratedCode/selfmodifyterminator.spec.cs.txt |
Updates terminator golden. |
GeneratedCode/selfmodifyje.spec.cs.txt |
Updates conditional-jump golden. |
GeneratedCode/selfmodifyinstructions.spec.cs.txt |
Updates self-modifying instructions golden. |
GeneratedCode/selfmodifycalltarget.spec.cs.txt |
Updates call-target golden. |
GeneratedCode/selfmodifycall.spec.cs.txt |
Updates self-modifying call golden. |
GeneratedCode/segpr.spec.cs.txt |
Updates segment/pointer golden. |
GeneratedCode/returnedterminator.spec.cs.txt |
Updates returned-terminator golden. |
GeneratedCode/pushcs_callnear_retfar.spec.cs.txt |
Updates call/return golden. |
GeneratedCode/partition_shared_tail.spec.cs.txt |
Updates shared-tail golden. |
GeneratedCode/partition_mutual_recursion_unwind.spec.cs.txt |
Updates recursion golden. |
GeneratedCode/partition_multi_entry_irreducible_shared.spec.cs.txt |
Updates irreducible CFG golden. |
GeneratedCode/partition_multi_entry_dominated_shared.spec.cs.txt |
Updates dominated CFG golden. |
GeneratedCode/partition_mixed_activation_cycle.spec.cs.txt |
Updates activation-cycle golden. |
GeneratedCode/partition_jump_into_function_middle.spec.cs.txt |
Updates mid-function jump golden. |
GeneratedCode/partition_indirect_call_jump.spec.cs.txt |
Updates indirect-transfer golden. |
GeneratedCode/partition_cross_function_loop.spec.cs.txt |
Updates cross-function loop golden. |
GeneratedCode/multimisalignedcall.spec.cs.txt |
Updates misaligned-call golden. |
GeneratedCode/lockprefix.spec.cs.txt |
Updates LOCK-prefix golden. |
GeneratedCode/linearsamesegmenteddifferent.a20.spec.cs.txt |
Updates A20-addressing golden. |
GeneratedCode/jump2.spec.cs.txt |
Updates jump golden. |
GeneratedCode/jump1.spec.cs.txt |
Updates jump golden. |
GeneratedCode/jmpmov.spec.cs.txt |
Updates computed-jump golden. |
GeneratedCode/interrupt.spec.cs.txt |
Updates interrupt golden. |
GeneratedCode/intchain.ivt.spec.cs.txt |
Updates interrupt-chain golden. |
GeneratedCode/externalint.pit.spec.cs.txt |
Updates external-interrupt golden. |
GeneratedCode/divfaultloop.spec.cs.txt |
Updates division-fault golden. |
GeneratedCode/div2.spec.cs.txt |
Updates division golden. |
GeneratedCode/datatrnf.spec.cs.txt |
Updates data-transfer golden. |
GeneratedCode/control.spec.cs.txt |
Updates control-flow golden. |
GeneratedCode/cmpneg.spec.cs.txt |
Updates comparison/negation golden. |
ConstantNodeTest.cs |
Tests casts and bit masks. |
CSharpAstEmitterFragmentTest.cs |
Updates fragment expectations. |
CSharpAstEmitterConstantTest.cs |
Tests literal rendering. |
CSharpFragment.cs |
Tracks source constants in fragments. |
CSharpAstEmitter.cs |
Implements compact constant emission. |
ConstantNode.cs |
Adds masks and C#-style conversion. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+754
to
+760
| int hexDigits = width switch { | ||
| BitWidth.BYTE_8 => 2, | ||
| BitWidth.WORD_16 => 4, | ||
| BitWidth.DWORD_32 or BitWidth.BOOL_1 => 8, | ||
| BitWidth.QWORD_64 => 16, | ||
| _ => throw new NotSupportedException($"Unsupported bit width {width}") | ||
| }; |
Comment on lines
+768
to
+770
| if (!dataType.Signed && dataType.BitWidth == BitWidth.DWORD_32 && value > int.MaxValue) { | ||
| return "u"; | ||
| } |
maximilien-noal
approved these changes
Sep 29, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

No description provided.