-
Notifications
You must be signed in to change notification settings - Fork 143
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
Description
shecc lacks support for goto
statements and labels, which are part of the C standard. While goto is often discouraged, it's essential for certain patterns like error handling in systems code and is required for full C compliance.
Current Behavior
void func() {
goto cleanup; // Error: "goto statement not supported"
cleanup: // Error: labels not recognized
return;
}
Expected Behavior
Full support for labels and goto statements:
- Forward and backward jumps
- Labels in any statement context
- Proper scope handling
- Integration with control flow graph
Implementation Requirements
1. Parser Changes
- Recognize label syntax (
identifier:
) - Parse
goto identifier;
statements - Track label definitions and references
- Handle forward references to undefined labels
2. Symbol Table
- Add label namespace separate from variables
- Store label locations in IR
- Validate all gotos reference defined labels
3. Code Generation
- Map labels to basic block boundaries
- Generate unconditional branch instructions
- Ensure proper control flow in CFG
4. Semantic Checks
- Detect duplicate label definitions
- Error on undefined labels after parsing
- Scope validation (labels are function-scoped)
Test Cases
// Basic goto
int basic() {
int x = 0;
goto skip;
x = 1;
skip:
return x; // Should return 0
}
// Error handling pattern
int error_handling() {
void *p1 = malloc(100);
if (!p1) goto cleanup;
void *p2 = malloc(200);
if (!p2) goto cleanup_p1;
// Use resources
free(p2);
cleanup_p1:
free(p1);
cleanup:
return -1;
}
// Forward reference
int forward() {
goto end; // Forward reference
return 1;
end:
return 0;
}
// Loop with goto
int loop() {
int i = 0;
start:
if (i++ < 10)
goto start;
return i;
}
Complexity
MEDIUM - Requires parser changes and CFG integration
Benefits
- Full C standard compliance
- Enables certain system programming patterns
- Required for porting legacy code
- Useful for generated code patterns
Notes
- Consider computed goto (GNU extension) as future enhancement
- Ensure goto doesn't cross variable initialization
- Document interaction with other control flow
tobiichi3227
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request