Skip to content

Conversation

tobiichi3227
Copy link
Contributor

@tobiichi3227 tobiichi3227 commented Oct 3, 2025

Add goto and label statement support

This PR adds full support for C goto statements and labels, integrating them with the parser, IR/CFG, and SSA passes. This enables forward/backward jumps, error-handling patterns, and improves C compatibility (#280).

Changes Made

Lexer/Parser:

  • Add T_goto; parse goto identifier; and identifier: labels.
  • Function-scoped label table with forward-reference backpatching.
  • Report unused label warning.

IR/CFG:

  • Introduce OP_label; update text dump and graph dump.
  • Lower goto via an if (1) wrapper to keep CFG well-formed (then: jmp label; else: fallthrough).
  • Map each label to a basic-block boundary and connect jumps accordingly.

SSA/Semantics:

  • Dominance-based check warns when goto crosses variable initialization (cross-initialization warning).

Notes:

  • Computed goto (GNU extension) is out of scope.

Testing

  • All tests pass on both Stage 0 and Stage 2

Summary by cubic

Adds C goto and label support across the lexer/parser, IR/CFG, and SSA to enable forward/backward jumps and improve C compatibility. Also adds warnings and errors around labels and unsafe jumps.

  • New Features
    • Lexer/Parser: add T_goto; parse "goto ;" and ":" labels; per-function label table with forward-reference backpatching.
    • IR/CFG: add OP_label and OP_jump; map labels to basic-block boundaries; lower goto via an if (1) wrapper to keep CFG well-formed; update text and graph dumps.
    • SSA: dominance-based check warns when a goto crosses a variable’s initialization.
    • Diagnostics: error on undefined labels and duplicate labels; warning on unused labels.
    • Tests: coverage for forward/backward jumps, label namespace, redefinition, and loop patterns.
    • Out of scope: computed goto (GNU extension).

Copy link
Collaborator

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check https://github.com/sysprog21/shecc/blob/master/CONTRIBUTING.md carefully, and you must use clang-format version 18 (not later) to indent.

@jserv jserv requested review from ChAoSUnItY and DrXiao October 3, 2025 04:08
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 6 files

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 issues and fix them.


<file name="src/lexer.c">

<violation number="1" location="src/lexer.c:88">
Adding this entry expands the keyword table to 18 elements, but NUM_KEYWORDS is still 17, so the loop stops early and the last keyword (&quot;const&quot;) is never inserted into KEYWORD_MAP. Update NUM_KEYWORDS so all entries are registered.</violation>
</file>


Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Ask questions if you need clarification on any suggestion

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

@jserv jserv requested a review from vacantron October 3, 2025 04:13
@jserv jserv changed the title Goto statement support Support goto statement Oct 3, 2025
Copy link
Collaborator

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read https://cbea.ms/git-commit/ and enforce the rules. In addition, append Close #280 at the end of git commit message.

@jserv
Copy link
Collaborator

jserv commented Oct 3, 2025

Update COMPLIANCE.md once you are confident that the goto statement is supported.

@tobiichi3227 tobiichi3227 force-pushed the goto-statement-support branch from f88cec8 to dc49f7a Compare October 3, 2025 05:44
@tobiichi3227 tobiichi3227 requested a review from jserv October 3, 2025 05:46
Copy link
Collaborator

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tobiichi3227 tobiichi3227 force-pushed the goto-statement-support branch 2 times, most recently from af2c003 to 79899e0 Compare October 3, 2025 06:26
@tobiichi3227 tobiichi3227 requested a review from jserv October 3, 2025 06:27
Copy link
Collaborator

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refine git commit messages into complete English sentences that clearly describe the rationale, while supporting the 'goto' keyword.

@tobiichi3227 tobiichi3227 force-pushed the goto-statement-support branch from 79899e0 to 9f7ec88 Compare October 3, 2025 06:40
@tobiichi3227 tobiichi3227 requested a review from jserv October 3, 2025 06:43
@tobiichi3227 tobiichi3227 force-pushed the goto-statement-support branch 2 times, most recently from c70a663 to b99b5d3 Compare October 3, 2025 06:55
@tobiichi3227 tobiichi3227 force-pushed the goto-statement-support branch 2 times, most recently from 07f313f to 0c21eae Compare October 3, 2025 07:10
Copy link
Collaborator

@visitorckw visitorckw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please resolve CI failure.
Again, please test it before pushing the remote branch for review.

@tobiichi3227 tobiichi3227 force-pushed the goto-statement-support branch from 0c21eae to 0a1f2f1 Compare October 3, 2025 07:31
@tobiichi3227 tobiichi3227 force-pushed the goto-statement-support branch from 0a1f2f1 to 26850c0 Compare October 3, 2025 09:07
@jserv jserv requested a review from visitorckw October 3, 2025 10:05
@tobiichi3227 tobiichi3227 force-pushed the goto-statement-support branch from 26850c0 to 0bea9f9 Compare October 3, 2025 14:26
@tobiichi3227 tobiichi3227 force-pushed the goto-statement-support branch from 0bea9f9 to 1b2ff97 Compare October 3, 2025 14:42

void add_label(char *name, basic_block_t *bb)
{
if (label_idx > MAX_LABELS - 1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would prefer:

if (label_idx >= MAX_LABELS)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I referred to this.

Enable parsing and analysis of C labels and goto so the compiler can
accept common patterns (for example error-handling cleanup) and be
more compatible with real-world C code.

Provide diagnostics for duplicate, undefined, and unused labels,
and warn when a goto can bypass a variable's initialization.

Close sysprog21#280
@tobiichi3227 tobiichi3227 force-pushed the goto-statement-support branch from 1b2ff97 to 772e540 Compare October 3, 2025 15:26
@jserv jserv merged commit 9062977 into sysprog21:master Oct 4, 2025
6 checks passed
@jserv
Copy link
Collaborator

jserv commented Oct 4, 2025

Thank @tobiichi3227 for contributing!

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.

4 participants