Skip to content

Consolidate duplicated GetLdcValue into ILInstruction#492

Open
Copilot wants to merge 3 commits intomainfrom
copilot/remove-duplicate-getldcvalue-and-getstlocindex
Open

Consolidate duplicated GetLdcValue into ILInstruction#492
Copilot wants to merge 3 commits intomainfrom
copilot/remove-duplicate-getldcvalue-and-getstlocindex

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 1, 2026

GetLdcValue was duplicated across IL2NESWriter.Helpers.cs and Transpiler.LocalFrameAllocation.cs with subtly different case ordering, risking silent divergence if one copy gets a bug fix or new opcode support.

  • Added GetLdcValue() as a public instance method on ILInstruction, matching the existing GetStlocIndex() pattern
  • Replaced both static copies with thin forwarding calls to preserve source compatibility at all call sites
// ILInstruction.cs — single source of truth
public int? GetLdcValue() => OpCode switch
{
    ILOpCode.Ldc_i4_m1 => -1,
    ILOpCode.Ldc_i4_0 => 0,
    // ...
    ILOpCode.Ldc_i4_s or ILOpCode.Ldc_i4 => Integer,
    _ => null
};

// IL2NESWriter.Helpers.cs / Transpiler.LocalFrameAllocation.cs — forwarding
static int? GetLdcValue(ILInstruction instr) => instr.GetLdcValue();

Move GetLdcValue to ILInstruction as a public instance method (matching
existing GetStlocIndex pattern). Replace duplicated static methods in
IL2NESWriter.Helpers.cs and Transpiler.LocalFrameAllocation.cs with thin
forwarding calls to maintain source compatibility.

Agent-Logs-Url: https://github.com/jonathanpeppers/dotnes/sessions/706765a7-5df8-4d40-aca4-2b0081ff6af0

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor GetLdcValue and GetStlocIndex to remove duplicates Consolidate duplicated GetLdcValue into ILInstruction May 1, 2026
Copilot AI requested a review from jonathanpeppers May 1, 2026 23:31
@jonathanpeppers jonathanpeppers marked this pull request as ready for review May 2, 2026 00:06
Copilot AI review requested due to automatic review settings May 2, 2026 00:06
@jonathanpeppers
Copy link
Copy Markdown
Owner

/review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 2, 2026

dotnes PR Reviewer completed successfully!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR removes duplicated GetLdcValue switch logic by making GetLdcValue() a single-source-of-truth instance method on ILInstruction, then updates existing call sites to forward to it (preserving the prior helper signatures).

Changes:

  • Added ILInstruction.GetLdcValue() alongside the existing GetStlocIndex() helper.
  • Replaced duplicated GetLdcValue(ILInstruction) implementations with thin forwarding wrappers in the transpiler and writer helpers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/dotnes.tasks/Utilities/ILInstruction.cs Introduces GetLdcValue() on ILInstruction to centralize ldc constant decoding.
src/dotnes.tasks/Utilities/IL2NESWriter.Helpers.cs Replaces the local GetLdcValue switch with a forwarder to ILInstruction.GetLdcValue().
src/dotnes.tasks/Utilities/Transpiler.LocalFrameAllocation.cs Replaces the local GetLdcValue switch with a forwarder to ILInstruction.GetLdcValue().

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

LGTM — Clean, correct deduplication.

Summary: This PR consolidates two copies of GetLdcValue (from IL2NESWriter.Helpers.cs and Transpiler.LocalFrameAllocation.cs) into a single instance method on ILInstruction, matching the existing GetStlocIndex() pattern. The old call sites become thin forwarding stubs.

Verified:

  • The two old copies had different case ordering (Ldc_i4_m1 first vs. last) — functionally identical but a divergence risk. Now eliminated.
  • The new canonical implementation covers all Ldc_i4_* opcodes correctly.
  • All ~30 callers go through the static forwarding methods, so no call-site changes needed.
  • Build ✅, macOS smoke test ✅, Windows smoke test in progress.
  • No snapshot or behavioral changes — pure refactoring.
Severity Count
❌ Error 0
⚠️ Warning 0
💡 Suggestion 2

Generated by dotnes PR Reviewer for issue #492 · ● 1.3M

/// <summary>
/// Gets the constant value for a Ldc_i4 opcode, or null if not a Ldc_i4.
/// </summary>
public int? GetLdcValue() => OpCode switch
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤖 💡 Code organization — Nice consolidation. The pattern mirrors GetStlocIndex() above and unifies the two divergent case orderings (the Transpiler copy had Ldc_i4_m1 as the last case instead of first). Functionally identical but eliminates the divergence risk.

Follow-up thought: GetLdlocIndex in IL2NESWriter.Helpers.cs (lines 18-26) follows the same "static helper wrapping a switch on OpCode" pattern. It could similarly move here as an instance method on ILInstruction in a future PR.

ILOpCode.Ldc_i4 => instr.Integer,
_ => null
};
static int? GetLdcValue(ILInstruction instr) => instr.GetLdcValue();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤖 💡 YAGNI — Keeping this forwarding method avoids touching ~30 callers, which is a pragmatic choice for this PR. If a future cleanup pass removes these forwarding stubs and updates callers to use instr.GetLdcValue() directly, the call sites will actually read a bit more naturally (since it's an instance method on the object they already have).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GetLdcValue and GetStlocIndex duplicated across partial classes

3 participants