Skip to content

Add codegen support for goto#163

Merged
nunoplopes merged 4 commits into
Cpp2Rust:masterfrom
lucic71:goto-codegen
Jun 3, 2026
Merged

Add codegen support for goto#163
nunoplopes merged 4 commits into
Cpp2Rust:masterfrom
lucic71:goto-codegen

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented Jun 1, 2026

Depends on #161. See #161 for a more detailed description of how goto is implemented.

On the codegen part, this PR emits the goto_block and goto macros when it hits functions that use labels and gotos internally. It also hoists local variables at the start of the function so that all goto_block arms can see and use the local variables.

@lucic71
Copy link
Copy Markdown
Contributor Author

lucic71 commented Jun 1, 2026

One other thing worth mentioning: initially I considered not hoisting the variables at the beginning of the functionin the codegen. Instead I tried to emit the variables as they were declared in the program, and let goto_block do the hoisting inside the macro, however this turned out to be a bit more complicated because I needed to duplicate the GetDefaultAsString logic inside libcc2rs-macro.

@nunoplopes
Copy link
Copy Markdown
Contributor

What happens with VLAs declare inside goto?
e.g.

lbl:
 int a[n];
 ...
  ++n;
  goto lbl;

@lucic71
Copy link
Copy Markdown
Contributor Author

lucic71 commented Jun 2, 2026

What happens with VLAs declare inside goto? e.g.

lbl:
 int a[n];
 ...
  ++n;
  goto lbl;

I did not think about that. We don't currently translate VLAs so I skipped this case. Should we add support for that in this PR or do we leave it for later?

@nunoplopes nunoplopes merged commit db0d23f into Cpp2Rust:master Jun 3, 2026
9 checks passed
lucic71 added a commit to lucic71/cpp2rust that referenced this pull request Jun 4, 2026
Depends on Cpp2Rust#161. See Cpp2Rust#161 for a more detailed description of how goto is
implemented.

On the codegen part, this PR emits the goto_block and goto macros when
it hits functions that use labels and gotos internally. It also hoists
local variables at the start of the function so that all goto_block arms
can see and use the local variables.
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