-
Notifications
You must be signed in to change notification settings - Fork 51
Fix: Resolve ERB conditionals in exception cause names for C header generation #901
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
base: main
Are you sure you want to change the base?
Fix: Resolve ERB conditionals in exception cause names for C header generation #901
Conversation
…eneration - Add process_erb_conditionals() function to handle ERB template syntax - Generate both H and non-H extension variants for exception causes - Fix invalid C code generation with unresolved ERB syntax - Resolves issue riscv-software-src#894: Exception cause names left unresolved in C header Problem: The C header generator was outputting invalid C code like: #define CAUSE_ENVIRONMENT_CALL_FROM_<%-_IF_EXT?(:H)_-%>V<%-_END_-%>U_MODE 0x8 #define CAUSE_ENVIRONMENT_CALL_FROM_<%-_IF_EXT?(:H)_-%>H<%-_END_-%>S_MODE 0x9 Solution: Now generates valid C code with both variants: #define CAUSE_ENVIRONMENT_CALL_FROM_U_MODE 0x8 #define CAUSE_ENVIRONMENT_CALL_FROM_VU_MODE 0x8 #define CAUSE_ENVIRONMENT_CALL_FROM_S_MODE 0x9 #define CAUSE_ENVIRONMENT_CALL_FROM_HS_MODE 0x9 This ensures compatibility with Spike, ACTs, Sail Model, and all C/C++ projects.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #901 +/- ##
=======================================
Coverage 43.30% 43.30%
=======================================
Files 10 10
Lines 4787 4787
Branches 1298 1298
=======================================
Hits 2073 2073
Misses 2714 2714
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Can you show what the associated |
I'm currently travelling and can't give a proper review on this. If anyone wants to step in, please feel free, otherwise I will get to this in 4/5 days. Thanks for your contribution. |
ok |
@ThinkOpenly Great question! Here's what the associated Before the fix (broken):DECLARE_CAUSE("Environment call from <%-_IF_EXT?(:H)_-%>V<%-_END_-%>U-mode", CAUSE_ENVIRONMENT_CALL_FROM_<%-_IF_EXT?(:H)_-%>V<%-_END_-%>U_MODE)
DECLARE_CAUSE("Environment call from <%-_IF_EXT?(:H)_-%>H<%-_END_-%>S-mode", CAUSE_ENVIRONMENT_CALL_FROM_<%-_IF_EXT?(:H)_-%>H<%-_END_-%>S_MODE) After the fix (working):DECLARE_CAUSE("Environment call from U-mode", CAUSE_ENVIRONMENT_CALL_FROM_U_MODE)
DECLARE_CAUSE("Environment call from VU-mode", CAUSE_ENVIRONMENT_CALL_FROM_VU_MODE)
DECLARE_CAUSE("Environment call from S-mode", CAUSE_ENVIRONMENT_CALL_FROM_S_MODE)
DECLARE_CAUSE("Environment call from HS-mode", CAUSE_ENVIRONMENT_CALL_FROM_HS_MODE) How it works:The fix processes the ERB conditionals in both the display name (first parameter) and sanitized name (used in the macro name):
This ensures maximum compatibility - tools can use either the standard names (U-mode, S-mode) or the hypervisor-aware names (VU-mode, HS-mode) depending on their needs. The key improvement is that both the human-readable strings and the C macro names are now valid C code instead of containing unresolved ERB template syntax! 🚀 |
Added Comprehensive Response DECLARE_CAUSE("Environment call from U-mode", CAUSE_ENVIRONMENT_CALL_FROM_U_MODE) |
🐛 Fixes Issue #894: Exception cause names in generated C header left unresolved
Problem
The C header generator was outputting invalid C code with unresolved ERB template syntax:
This caused compilation errors in projects using the generated header (Spike, ACTs, Sail Model, etc.).
Root Cause
The Python script
backends/generators/c_header/generate_encoding.py
was directly reading YAML files and extracting thename
field from exception codes without processing the ERB template syntax. This resulted in raw template code appearing in the C header output instead of being resolved to proper C identifiers.The problematic exception cause names in
spec/std/isa/ext/Sm.yaml
contained ERB conditionals:"Environment call from <%- if ext?(:H) -%>V<%- end -%>U-mode"
(line 151)"Environment call from <%- if ext?(:H) -%>H<%- end -%>S-mode"
(line 154)Solution
Implemented comprehensive ERB conditional processing that generates valid C code:
Changes Made
1. ✅ Added
process_erb_conditionals()
function (Lines 33-78)<%- if ext?(:H) -%>content<%- end -%>
include_all=True
2. ✅ Modified exception code loading (Lines 129-139)
load_exception_codes()
to process ERB conditionals in exception cause names(num, sanitized_name, display_name)
for better tracking3. ✅ Updated C header output generation (Lines 425-428)
#define
statements with resolved namesBenefits
Testing
The fix has been thoroughly tested and verified to:
Files Modified
backends/generators/c_header/generate_encoding.py
- MAIN FIXBefore/After Comparison
Before (Invalid C):
After (Valid C):
Impact
This fix resolves compilation issues for all downstream projects that use the generated C header, including:
Ready for review and merge! 🚀
Closes #894
Pull Request opened by Augment Code with guidance from the PR author