Skip to content

Conversation

jserv
Copy link
Collaborator

@jserv jserv commented Aug 19, 2025

This improve shecc's robustness and partial C99 compatibility while maintaining the critical self-hosting property.

  • Array literals in expressions: {1, 2, 3} syntax now works in expression contexts
  • Global struct array initialization: Fixed runtime access and initialization of global struct arrays
  • Arm backend control flow: Fixed premature exit after global initialization
  • Lexer modernization: Cleaner token mapping using array compound literals

While this improves compound literal support, full C99 compound literals remain unsupported due to architectural constraints:

  • Cast syntax: (Type){...}
  • Struct compound literals: (struct Point){10, 20}
  • Typed array literals: (int[]){1, 2, 3}

Summary by Bito

This pull request enhances the shecc compiler's support for compound literals, improving robustness and partial C99 compatibility. Key updates include better support for array literals, global struct array initialization, and modernization of the lexer for cleaner token mapping. However, full C99 support remains limited due to architectural constraints.

jserv added 6 commits August 19, 2025 10:35
This commit consolidates compound literal improvements:
- Support global struct/array initialization with compound literals
- Implement GP-relative stores for global array initializers
- Enhanced global struct variable declaration support
- Support nested compound literals in arrays

What still doesn't work:
- True C99 compound literals with cast syntax: (Type){...}
- Struct compound literals: (struct Point){10, 20}
- Array compound literals with type: (int[]){1, 2, 3}
This commit addresses two critical issues with global struct arrays:
1. Global struct arrays were missing the is_global flag, causing them to
   be treated as local variables during code generation. This led to
   segmentation faults when accessing them at runtime since they were
   incorrectly loaded from the stack pointer (sp) instead of the global
   pointer (gp).
2. Global struct array initialization values were being parsed but
   ignored. The parser was only consuming the tokens without generating
   the necessary OP_load_constant instructions to initialize the values
   at runtime.

This fixes the test case where `struct point gpts1[] = { {3, 4} }` would
return 0 instead of the expected 7 when accessing gpts1[0].x+gpts1[0].y.
Apply control flow fix from dev-compound-literal branch to ensure proper
execution order after global variable initialization.
- Adjust syscall trampoline offset to 48 (was 44)
- Adjust global init offset to 84 (was 80)
- Add branch instruction after global init to skip early exit code
- Clarify that r0 already contains main's return value at exit
Adapt changes from commit 191c6ac to the compound-literal branch. This replaces
verbose index-based array initialization with cleaner compound literal arrays:
- Convert directive initialization to use array compound literal
- Convert keyword initialization to use array compound literal
Critical safety improvements:
- Add comprehensive null pointer checks to all ELF generation functions
- Prevent crashes from uninitialized ELF buffers
- Fix array compound literal crash: {1, 2, 3} now works in expressions
- Add support for empty arrays and trailing commas
- Proper type inference from first element
Extract parse_array_init and read_body_statement into reusable helper
functions, reducing nesting levels and improve code readability.
@jserv jserv force-pushed the compound-literal branch from e352238 to e179f4b Compare August 19, 2025 02:49
jserv added 2 commits August 19, 2025 17:04
This commit improves compound literal support by implementing C99-style
compound literals with both standard and non-standard extensions.
- Array compound literals: (int[]){10, 20, 30}
- Scalar compound literals: (int){42}
- Character compound literals: (char){'A'}
- Mixed expressions: (int){10} + (int[]){20}
- Function argument support: func((int){5}, (int[]){10})
- Non-standard scalar assignment: int x = (int[]){100} assigns 100
@jserv jserv merged commit 94f345c into master Aug 21, 2025
12 checks passed
@jserv jserv deleted the compound-literal branch August 21, 2025 01:54
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