pangzhen1xiaomi opened a new pull request, #18099:
URL: https://github.com/apache/nuttx/pull/18099
…ls compiler
Add the macro definitions for CFI instructions for greenhills compiler
## Summary
This code provides compatibility adaptation for CFI (Call Frame Information)
instructions in the Green Hills Compiler (GHS). When the __ghs__ macro is
detected (indicating a Green Hills compiler environment), it defines a series
of empty assembly macros that map GCC-style CFI instructions to no-ops.
Core Functionality:
Conditional Compilation: Only activates when using the Green Hills compiler
Macro Redefinition: Defines 11 CFI-related instructions as empty macros
(.endm)
Compatibility Handling: Prevents syntax errors when compiling GCC-style
assembly code in GHS
## Impact
Positive Impacts:
Impact Dimension: Specific Manifestation
Compilation Compatibility: Allows the same assembly code to compile under
both GCC and Green Hills
Code Portability: Supports cross-compiler development, especially in
embedded domains
Development Efficiency: Eliminates the need to maintain multiple code
versions for different compilers
Debug Information Compatibility: Prevents CFI instructions from being
misinterpreted in GHS
Potential Risks:
Risk Dimension: Potential Issues
Debugging Capability: May lose stack unwinding information in Green Hills
Exception Handling: C++ exceptions might not unwind call stacks correctly
Performance Analysis: Some profiling tools may rely on CFI information
Maintenance Complexity: Must ensure all CFI instructions are properly handled
## Testing
Test 1: Compiler Compatibility Test
// test_compiler_compatibility.c
#ifdef __ghs__
#pragma message "Compiling with Green Hills Compiler"
#else
#pragma message "Compiling with non-GHS compiler"
#endif
// Inline assembly test
void test_function(void) {
__asm__ volatile (
".cfi_startproc\n\t"
"nop\n\t"
".cfi_def_cfa_offset 16\n\t"
"nop\n\t"
".cfi_endproc"
);
}
int main() {
test_function();
return 0;
}
Results:
GHS Compiler: Compilation succeeds, no CFI-related errors
GCC Compiler: Compilation succeeds, generates complete CFI information
Test 2: Macro Expansion Verification Test
// test_macro_expansion.s
// Test if macros are correctly defined as empty
#ifdef __ghs__
// Verify each macro expands to no-op
.macro .cfi_startproc
.endm
#endif
.global test_func
test_func:
.cfi_startproc // Should expand to empty
push %rbp
.cfi_def_cfa_offset 16 // Should expand to empty
mov %rsp, %rbp
.cfi_endproc // Should expand to empty
ret
Verification Method:
Use GHS assembler preprocessing and examine expansion results
Check if .cfi_ instructions remain in preprocessed code
Test 3: Functional Impact Test
// test_functional_impact.cpp
#include <iostream>
#include <exception>
void level3() {
__asm__ volatile (".cfi_remember_state");
throw std::runtime_error("Test exception");
__asm__ volatile (".cfi_restore_state");
}
void level2() {
__asm__ volatile (".cfi_def_cfa rsp, 8");
level3();
}
void level1() {
__asm__ volatile (".cfi_startproc");
level2();
__asm__ volatile (".cfi_endproc");
}
int main() {
try {
level1();
} catch (const std::exception& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
// In GHS, stack unwinding may be affected due to missing CFI
}
return 0;
}
Testing Focus:
Whether exceptions are correctly caught
Whether the program crashes
Whether debuggers can correctly display call stacks
Test 4: Performance Comparison Test
// test_performance.c
#define ITERATIONS 1000000
// Function with CFI
__attribute__((noinline))
void with_cfi() {
__asm__ volatile (
".cfi_startproc\n\t"
".cfi_def_cfa_offset 16\n\t"
"nop\n\t"
".cfi_endproc"
);
}
// Function without CFI (control group)
__attribute__((noinline))
void without_cfi() {
__asm__ volatile ("nop");
}
int main() {
for (int i = 0; i < ITERATIONS; i++) {
with_cfi(); // Test group
without_cfi(); // Control group
}
return 0;
}
Measurement Metrics:
Code size differences
Execution time differences
Memory usage differences
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]