Skip to content

Conversation

KushalMeghani1644
Copy link

Improve documentation and code quality for RISC-V assembly instructions

Summary

  • This PR enhances the documentation and code quality of RISC-V assembly instruction wrappers in asm.rs while maintaining full backward compatibility.

Changes Made:

Documentation Improvements:

  • Enhanced safety documentation: Added comprehensive safety sections explaining when and why instructions like ebreak and ecall are unsafe, including specific warnings about exception handling and stack pointer management.

  • Expanded behavioral descriptions: Provided detailed explanations of instruction behavior, including privilege mode effects for ecall and power management implications for wfi.

  • Added practical use cases: Documented appropriate use cases for fence operations, including multiprocessor considerations and performance implications.

  • Improved function parameter documentation: Enhanced documentation for sfence_vma() with clear parameter descriptions and usage examples.

Code Quality Enhancements:

  • Removed redundant code: Eliminated unnecessary unimplemented!() calls on non-RISC-V targets since functions are already properly gated with cfg attributes.

  • Added missing assembly options: Included preserves_flags option for instructions that do not modify processor flags (nop, wfi, ebreak, ecall).

  • Fixed assembly template syntax: Updated sfence_vma() to use idiomatic {} placeholder syntax instead of explicit numbering.

  • Improved code organization: Restructured the delay() function implementation for better readability.

Enhanced Warning Documentation:

  • Strengthened timing accuracy warnings: Added comprehensive warnings about the delay() function's limitations, including interrupt interference and architecture dependencies, with strong recommendations to use proper timer peripherals for precise timing requirements.

  • Added performance guidance: Documented the performance implications of fence operations and recommended using specific sfence_vma() over sfence_vma_all() when possible.

Testing:

  • All existing functionality remains unchanged. The modifications only affect documentation and non-functional code improvements.

Compatibility

  • This PR maintains full backward compatibility. All existing APIs and behaviors are preserved.

- Remove redundant unimplemented!() calls on non-RISC-V targets since
  functions are already properly gated with cfg attributes
- Add comprehensive safety documentation explaining when and why
  instructions are unsafe (ebreak, ecall)
- Enhance behavior descriptions with practical use cases and performance
  considerations for fence operations
- Add preserves_flags option to instructions that don't modify flags
  (nop, wfi, ebreak, ecall)
- Fix sfence_vma() assembly template to use idiomatic {} syntax
- Strengthen delay() function warnings about timing accuracy limitations
  and recommend proper timer peripherals for precise delays
- Add examples for sfence_vma() showing ASID and address targeting
- Improve multiprocessor considerations documentation for fence_i
- Standardize documentation format with consistent Safety, Behavior,
  and Use Cases sections

These changes maintain full backward compatibility while significantly
improving developer experience and preventing common usage mistakes.
@KushalMeghani1644 KushalMeghani1644 requested a review from a team as a code owner September 8, 2025 12:11
Copy link
Contributor

@romancardenas romancardenas left a comment

Choose a reason for hiding this comment

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

Thank you for your contributions! Please, check my comments. A second round of review would be highly appreciated in case I'm missing something @MabezDev @rmsyn

Copy link
Contributor

@romancardenas romancardenas left a comment

Choose a reason for hiding this comment

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

Hi! I noticed that the git history is quite dirty (9 commits), and you included a few unnecessary/wrong indentation/space changes.

Please, start from a fresh, clean fork of this repo, include only the changes in the documentation and preserves_flags, and open a new PR with only the changes needed. Otherwise, it is very difficult to review, as there are changes everywhere and I cannot easily keep track of what has actually changed and what has not.

@@ -0,0 +1,5 @@
## [Unreleased]
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, remove this file completely

@@ -1,282 +1,4 @@
# Change Log
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like your branch is not up to date with the master branch. Please, rebase from master to get the latest changes. Also, note that the idea is not removing anything from the changelog, but progressively adding and documenting changes across versions. The newest changes (for example, the ones on your PR) go on top of the file under the [Ureleased] section. Please, take a look at http://keepachangelog.com/ for more information about how we record changes in the repo.

Comment on lines 125 to 126
// Debug Mode Registers
pub mod dcsr;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not add registers in this PR. It is better to leave these changes to #342

@KushalMeghani1644
Copy link
Author

@romancardenas Thanks for putting time to my PR! I have cleanly rebased this entire branch with fixed indentations, and the CHANGELOG.md is completely fixed, aswell.

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.

2 participants