Skip to content

Commit e9dafef

Browse files
committed
add control flow details to Rationale.md
1 parent bdcc63a commit e9dafef

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

AstSemantics.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,9 @@ Branches may only reference labels defined by an outer *enclosing construct*.
252252
This means that, for example, references to a `block`'s label can only occur
253253
within the `block`'s body.
254254

255-
In practice, outer `block`s can be used to place labels for any given branching
256-
pattern, except for one restriction: one can't branch into the middle of a loop
255+
In practice, outer `block`s can be used to place labels for
256+
[any given branching pattern](Rational.md#control-flow),
257+
except for one restriction: one can't branch into the middle of a loop
257258
from outside it. This restriction ensures all control flow graphs are well-structured
258259
in the exact sense as in high-level languages like Java and JavaScript. To
259260
further see the parallel, note that a `br` to a `block`'s label is functionally

Rationale.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,34 @@ control flow with the
130130
[Relooper](https://github.com/kripken/emscripten/raw/master/docs/paper.pdf)
131131
[algorithm](http://dl.acm.org/citation.cfm?id=2048224&CFID=670868333&CFTOKEN=46181900),
132132
with guaranteed low code size overhead, and typically minimal throughput
133-
overhead (except for pathological cases of irreducible control
133+
overhead (due to using a label threading variable for irreducible control
134134
flow). Alternative approaches can generate reducible control flow via node
135135
splitting, which can reduce throughput overhead, at the cost of increasing
136136
code size (potentially very significantly in pathological cases).
137-
Also,
138-
[more expressive control flow constructs](FutureFeatures.md#more-expressive-control-flow)
137+
138+
More specifically, control flow in WebAssembly has the same restrictions as in
139+
high-level languages such as Java and JavaScript, which do not have arbitrary
140+
`goto`s, but do have labeled `break`: WebAssembly's `br` control flow construct is
141+
in fact
142+
[functionally equivalent to a labeled `break`](AstSemantics.md#branches-and-nesting).
143+
The main limitation that such control flow has over completely arbitrary control
144+
flow is that it is not possible to
145+
[jump into the middle of loops](https://en.wikipedia.org/wiki/Duff's_device)
146+
(unless you introduce a label threading variable, of course, which allows any
147+
CFG to be represented, as in the Relooper algorithm). And the relationship
148+
is bidirectional, as it is in fact possible to represent any control flow having
149+
that limitation by using just `block`s, `loop`s, and labeled `break`s: create a
150+
`loop`s where necessary, and then you are left with sections in which control
151+
flow can only branch forward, in which you can sort and create nested `blocks`.
152+
Branches forward are then simply labeled `break`s, which can go as forward as
153+
necessary in that section.
154+
155+
In summary, we know that structured control flow can be represented in the
156+
WebAssembly AST in a guaranteed manner. Arbitrary control flow can also be
157+
represented there, with overhead that experience has shown is acceptable
158+
especially given the rarity of irreducible control flow.
159+
160+
[More expressive control flow constructs](FutureFeatures.md#more-expressive-control-flow)
139161
may be added in the future.
140162

141163

0 commit comments

Comments
 (0)