Skip to content

Conversation

@pangzhen1xiaomi
Copy link
Contributor

@pangzhen1xiaomi pangzhen1xiaomi commented Jan 22, 2026

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
#include

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

@github-actions github-actions bot added Arch: arm Issues related to ARM (32-bit) architecture Size: S The size of the change in this PR is small labels Jan 22, 2026
@pangzhen1xiaomi pangzhen1xiaomi marked this pull request as ready for review January 23, 2026 02:39
…ls compiler

Add the macro definitions for CFI instructions for greenhills compiler

Signed-off-by: pangzhen1 <pangzhen1@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants