-
Notifications
You must be signed in to change notification settings - Fork 143
Parser: fix for struct compound literal initialization (#299) #302
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: master
Are you sure you want to change the base?
Conversation
Investigated the bug where compound literals like (struct point){10, 20, 30} produced shifted field values (e.g., x got garbage, y=10, z=20). Added a temporary fix by adjusting the source address +4 bytes in parser.c. Tested with simple cases: x=10, y=20, z=30. This is an experimental patch; needs review for design correctness. Refs: sysprog21#299
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Consolidate git commit messages by enforcing the rules specified by How to Write a Git Commit Message carefully
- Don't leave dead code
- Don't mention your name (loretta) in code
Previously, array compound literals such as (int[]){100, 200, 300} failed with an "Unexpected token" error. Struct compound literals worked correctly, but array syntax [] was unhandled in the parser. This change adds proper array compound literal handling, including scalar and pointer contexts. In scalar context, the literal returns its first element (e.g. int x = (int[]){100,200}; yields 100). In pointer context, it allocates and initializes backing storage and returns the address (e.g. int *p = (int[]){100,200};). Arithmetic expressions using literals (e.g. 50 + (int[]){100}) also evaluate correctly. Additional fixes include: - Consume missing '{' token for int/char compound literals - Add return statements to prevent control flow fall-through - Prevent segfaults in pointer assignments by allocating memory As a result, shecc now supports array compound literals alongside existing struct compound literals, improving C99 compatibility and preserving self-hosting capability. Known limitations remain: designated initializers and complex expression combinations are still unsupported.
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0, NULL); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid unnecessary blank lines.
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do this.
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto. Don't do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebase the latest master
branch.
This PR is a work-in-progress for issue #299.
adjusting the source address by +4 bytes in parser.c.
Refs: #299
Summary by cubic
Fix struct compound literal initialization in the parser so fields are assigned correctly instead of being shifted. Adds explicit brace-initializer emission and corrects assignment handling, addressing issue #299.