Commit 77f486e
authored
Add goto support in libcc2rs-macros (#161)
This PR adds support for writing goto and goto_block in the following
form:
```rs
let mut ret: i32 = 0;
goto_block!({
'__entry: {
if n < 0 {
ret = -1;
goto!('out);
}
ret = 100;
}
'out: {
return ret;
}
});
```
This is equivalent to the following C code:
```c
int f() {
int ret;
if (n < 0) {
ret = -1;
goto out;
}
ret = 100;
out:
return ret;
}
```
goto_block is already used by the switch-with-fallthrough macro
internally. I modified its syntax from a list of `label = { ... }` to
`label: { ... }` so that rustfmt can format it properly.
Then, I added GotoRewriter to rewrite each `goto!('label)` into `{ __s =
<target index>; continue '__sm; }`. Whenever GotoStateMachine hits a
goto macro, it calls GotoRewriter to generate the following state
machine:
```rs
{
let mut __s: u32 = 0;
'__sm: loop {
match __s {
0u32 => {
if n < 0 {
ret = -1;
__s = 1; continue '__sm; // <- was goto!('out)
}
ret = 100;
__s = 1; continue '__sm; // fall-through to next arm
}
1u32 => {
return ret;
break '__sm; // last arm: exit
}
_ => break '__sm, // match exhaustiveness
}
}
}
```
Finally, to support nested goto_blocks, for example a switch inside a
goto_block, I added StateMachineNames which gives a unique name to each
state machine so that each goto targets the correct state machine.
After this PR is merged, I will add the codegen counterpart in the
`cpp2rust/` dir.
A few limitations about how goto will work:
* local variables of a function that contains goto will be hoisted
outside the goto_block so that all arms of the goto_block macro can see
them. c2rust also does this. All hoisted variables will be default
initialized.
* the rust compiler cannot prove that all paths of the code inside the
loop of the state machine return. I reality the loop always returns
because the C behavior is preserved. To suppress the rustc error, I
added a panic at the end of the function: `panic!("ub: non-void function
does not return a value");`1 parent 4a32f50 commit 77f486e
5 files changed
Lines changed: 320 additions & 46 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | | - | |
| 7 | + | |
7 | 8 | | |
8 | | - | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
| 38 | + | |
36 | 39 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
45 | 61 | | |
46 | 62 | | |
47 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
40 | | - | |
| 39 | + | |
| 40 | + | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
4 | 7 | | |
5 | 8 | | |
6 | 9 | | |
7 | | - | |
| 10 | + | |
8 | 11 | | |
9 | 12 | | |
10 | 13 | | |
| |||
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
24 | | - | |
25 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
26 | 45 | | |
27 | 46 | | |
28 | 47 | | |
29 | 48 | | |
| 49 | + | |
30 | 50 | | |
31 | 51 | | |
32 | 52 | | |
| |||
87 | 107 | | |
88 | 108 | | |
89 | 109 | | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
94 | 116 | | |
95 | 117 | | |
96 | 118 | | |
| |||
101 | 123 | | |
102 | 124 | | |
103 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
104 | 137 | | |
105 | 138 | | |
106 | 139 | | |
| |||
131 | 164 | | |
132 | 165 | | |
133 | 166 | | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
134 | 219 | | |
135 | 220 | | |
136 | 221 | | |
| |||
186 | 271 | | |
187 | 272 | | |
188 | 273 | | |
189 | | - | |
190 | | - | |
| 274 | + | |
191 | 275 | | |
192 | | - | |
193 | | - | |
| 276 | + | |
| 277 | + | |
194 | 278 | | |
195 | 279 | | |
196 | 280 | | |
197 | 281 | | |
198 | 282 | | |
199 | | - | |
| 283 | + | |
200 | 284 | | |
201 | 285 | | |
202 | 286 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
24 | 26 | | |
25 | 27 | | |
26 | 28 | | |
27 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
28 | 33 | | |
29 | 34 | | |
30 | 35 | | |
| |||
0 commit comments