Skip to content

Fix load and store field IL instructions for value types in stack #3186

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

Merged

Conversation

josesimoes
Copy link
Member

@josesimoes josesimoes commented Jun 23, 2025

Description

  • Now dealing properly with value types for structs assigned from stack and not from heap.
  • Fixes in store and load field.

Motivation and Context

How Has This Been Tested?

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • Dependencies/declarations (update dependencies or assembly declarations and changes associated, has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of field load and store operations for by-reference value types, ensuring correct processing of special types like DateTime and TimeSpan. This enhances reliability when working with these types in various scenarios.

- Now dealing properly with value types for structs assigned from stack and not from heap.
- Fixes in store and load field.
@josesimoes josesimoes added the Area: Interpreter Everything related with the interpreter, execution engine and such label Jun 23, 2025
Copy link

coderabbitai bot commented Jun 23, 2025

Walkthrough

The changes update the handling of field load (ldfld) and field store (stfld) instructions in the interpreter. The new logic introduces special handling for by-reference types, including structs and specific cases like DateTime and TimeSpan, ensuring correct dereferencing and type management during field access operations.

Changes

File(s) Change Summary
src/CLR/Core/Interpreter.cpp Refactored ldfld and stfld handling to add conditional logic for by-reference and special types.

Sequence Diagram(s)

sequenceDiagram
    participant Thread as CLR_RT_Thread
    participant Stack as Evaluation Stack
    participant TypeDesc as CLR_RT_TypeDescriptor

    Note over Thread: ldfld or stfld instruction

    Thread->>Stack: Pop object reference
    alt By-reference type
        Stack->>Stack: Dereference object
        alt Special case (DateTime/TimeSpan)
            Thread->>TypeDesc: ExtractObjectAndDataType(dereferenced object)
        else Other struct
            Stack->>Stack: Treat as raw struct, set type to VALUETYPE
            Stack->>Stack: Assert non-null
        end
    else Not by-reference
        Stack->>Stack: Assert non-null
        Thread->>TypeDesc: ExtractObjectAndDataType(object)
    end
    Note over Thread: Proceed with field load/store
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

nfbot and others added 2 commits June 23, 2025 15:36
Automated fixes for code style.
…d71d-e808-4447-aa01-cfb322a45f6c

Code style fixes for nanoframework/nf-interpreter PR#3186
@josesimoes josesimoes force-pushed the fix-field-access-stack-objects branch from 941c314 to aa84a32 Compare June 23, 2025 15:48
@josesimoes
Copy link
Member Author

@coderabbitai review

Copy link

coderabbitai bot commented Jun 23, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/CLR/Core/Interpreter.cpp (1)

696-722: Consistent implementation for field stores mirrors the load logic.

The store field implementation correctly applies the same by-reference detection and handling logic as the load operation, ensuring symmetric behavior. The special case handling for DateTime and TimeSpan is consistently applied.

One minor observation: there's code duplication between the load and store implementations that could potentially be extracted into a helper function for better maintainability, though this is not critical given the relatively small scope.

Consider extracting the by-reference detection and object resolution logic into a helper function to reduce duplication:

static HRESULT ResolveFieldAccessObject(CLR_RT_HeapBlock*& obj, NanoCLRDataType& dt) {
    bool instanceIsByRef = (obj->DataType() == DATATYPE_BYREF) || (obj->DataType() == DATATYPE_ARRAY_BYREF);
    
    if (instanceIsByRef) {
        if (obj->Dereference()->DataType() == DATATYPE_DATETIME ||
            obj->Dereference()->DataType() == DATATYPE_TIMESPAN) {
            NANOCLR_CHECK_HRESULT(CLR_RT_TypeDescriptor::ExtractObjectAndDataType(obj, dt));
        } else {
            dt = DATATYPE_VALUETYPE;
            obj = obj->Dereference();
            FAULT_ON_NULL(obj);
        }
    } else {
        FAULT_ON_NULL(obj);
        NANOCLR_CHECK_HRESULT(CLR_RT_TypeDescriptor::ExtractObjectAndDataType(obj, dt));
    }
    return S_OK;
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 927f73d and aa84a32.

📒 Files selected for processing (1)
  • src/CLR/Core/Interpreter.cpp (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: nf-interpreter (Nightly build) (Check_Build_Options)
  • GitHub Check: nf-interpreter (Check_Build_Options)
🔇 Additional comments (1)
src/CLR/Core/Interpreter.cpp (1)

572-598: Well-implemented by-reference type handling for field loads.

The logic correctly distinguishes between by-reference struct instances and regular objects, with appropriate special case handling for DateTime and TimeSpan types. The error handling with FAULT_ON_NULL after dereferencing is good defensive programming.

The implementation addresses the core issue described in the PR objectives by ensuring value types assigned from the stack are handled correctly.

@josesimoes josesimoes merged commit 4854f6b into nanoframework:develop Jun 23, 2025
24 checks passed
@josesimoes josesimoes deleted the fix-field-access-stack-objects branch June 23, 2025 16:41
josesimoes added a commit to josesimoes/nf-interpreter that referenced this pull request Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Interpreter Everything related with the interpreter, execution engine and such Type: bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants