Skip to content

translate-c: support goto statements - #19146

Closed
rpkak wants to merge 17 commits into
ziglang:masterfrom
rpkak:translate-c-goto
Closed

translate-c: support goto statements#19146
rpkak wants to merge 17 commits into
ziglang:masterfrom
rpkak:translate-c-goto

Conversation

@rpkak

@rpkakrpkak commented Mar 2, 2024

Copy link
Copy Markdown
Contributor

Goto support works by creating a boolean variable per label, which is false by default. A goto changes this variable to true and breaks to the highest common scope of the goto and the label. Now, if and while statements, which are controlled by the boolean variable, jump over the execution part of the code to get to the label.

closesziglang/translate-c#148

@rpkak
rpkakforce-pushed the translate-c-goto branch 2 times, most recently from d0bd7fd to 5a6574eCompareMarch 8, 2024 06:13
@rpkak
rpkakforce-pushed the translate-c-goto branch 3 times, most recently from 4509616 to 0e5b47cCompareMarch 14, 2024 20:33
@rpkak
rpkakforce-pushed the translate-c-goto branch from dec5d86 to af875a7CompareApril 4, 2024 19:29
@rpkak

rpkak commented Apr 4, 2024

Copy link
Copy Markdown
ContributorAuthor

I think I completed this PR now. It lacks the ability to use goto to jump out of or within a statement expression, but I don't think there is much need for this.
As I stated in a87c470 this PR also fixes ziglang/translate-c#133.
This PR inserts while loops which exist not as while loops in the c code. These loops have the same issues as descripted in ziglang/translate-c#132/#16790.

@rpkak
rpkak marked this pull request as ready for review April 4, 2024 19:31
@mnemnion

Copy link
Copy Markdown

Shouldn't a feature like this wait on #19812? That will be a more performant and general way to translate gotos into Zig, without adding extra conditionals to the code.

@rpkak
rpkakforce-pushed the translate-c-goto branch from 714ec76 to 008e88bCompareMay 6, 2024 04:53
@rpkak
rpkakforce-pushed the translate-c-goto branch from 008e88b to aef621cCompareMay 15, 2024 17:17
@rpkak

Copy link
Copy Markdown
ContributorAuthor

Shouldn't a feature like this wait on #19812? That will be a more performant and general way to translate gotos into Zig, without adding extra conditionals to the code.

@mnemnion I don't see how #19812 will make this more performant or general.
I have not seen any c code snippet, where llvm does not optimize away the goto-variable generated by this PR.

For the translation of c-switch statements, I argree. Translate-c outputs zig code multiple times, if the code is accessible by multiple cases. This PR makes translate-c also output case code once more, if the case is accessed by a goto. But this rewrite would be translate-c-switch specific and not translate-c-goto specific.

@rpkak
rpkakforce-pushed the translate-c-goto branch from aef621c to 2de160bCompareMay 25, 2024 10:07
@mnemnion

Copy link
Copy Markdown

This PR makes translate-c also output case code once more, if the case is accessed by a goto. But this rewrite would be translate-c-switch specific and not translate-c-goto specific.

Thanks for the reply. My main intention was to bring that branch to your attention, if you weren't aware of it, I apologize if it came across as second-guessing.

Many non-trivial uses of goto are in complex case statements, particularly those implementing a VM dispatch loop, a case (heh) that the labeled continue branch is specifically designed to handle. You say that would constitute a distinct branch? I believe you. Cheers.

@rpkak
rpkakforce-pushed the translate-c-goto branch from 2de160b to c2930a9CompareJune 6, 2024 13:49
@rpkak
rpkakforce-pushed the translate-c-goto branch from c2930a9 to f3a119eCompareJune 16, 2024 12:21
@rpkak
rpkakforce-pushed the translate-c-goto branch 2 times, most recently from a02a560 to 4ff0e2fCompareJuly 22, 2024 20:18
@rpkak

rpkak commented Dec 14, 2024

Copy link
Copy Markdown
ContributorAuthor

I closed this PR because it has not been merged yet, and new features in Zig allow for better code for these changes and the generated code.

@rpkakrpkak closed this Dec 14, 2024
@rpkak
rpkak deleted the translate-c-goto branch December 14, 2024 15:18
Sign up for freeto 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.

translate-c: support goto

2 participants

@rpkak@mnemnion