Skip to content

Commit e645c20

Browse files
committed
[www] revisit the explanation of generating conditional binary operators
1 parent fb978e6 commit e645c20

File tree

1 file changed

+26
-45
lines changed

1 file changed

+26
-45
lines changed

www/operators.html

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,66 +1138,46 @@ <h2>Conditional Binary Operators</h2>
11381138
together. Consider the following example and how it's block
11391139
structure should look like.
11401140
</p>
1141-
<pre><code>x || y && z || w
1141+
<pre><code>x || y || z || w
11421142
┌───┐
11431143
│ x │──F──┐
11441144
└───┘ │
11451145
│ ┌───┐
1146-
│ │ y │──T──┐
1146+
│ │ y │──F──┐
11471147
│ └───┘ │
11481148
│ │ ┌───┐
11491149
T │ │ z │──F──┐
1150-
F └───┘ │
1150+
T └───┘ │
11511151
│ │ │ ┌───┐
11521152
│ │ T │ w │
11531153
│ │ │ └───┘
11541154
│ ┌─────────┐ │
11551155
└──────│ merge │──────┘
11561156
└─────────┘</code></pre>
11571157
<p>
1158-
Before deciding how to generate something like this, it is
1159-
worth taking a quick look at some examples and where each
1160-
operand should lead.
1161-
</p>
1162-
<pre><code>x || y
1163-
┌────┐ ┌────┐
1164-
┌──│ || │──┐ │ || │
1165-
│ └────┘ │ └────┘
1166-
┌───┐ ┌───┐ ┌───┐ ┌───┐
1167-
│ x │ │ y │ │ x │─────>│ y │
1168-
└───┘ └───┘ └───┘ └───┘
1169-
│ │</code></pre>
1170-
<pre><code>x || y || z
1171-
┌────┐ ┌────┐
1172-
┌──│ || │──┐ │ || │
1173-
│ └────┘ │ └────┘
1174-
┌────┐ ┌───┐ ┌────┐ ┌───┐
1175-
┌──│ || │──┐ │ z │ │ || │ │ z │
1176-
│ └────┘ │ └───┘ └────┘ └───┘
1177-
┌───┐ ┌───┐ ┌───┐ ┌───┐ ^
1178-
│ x │ │ y │ │ x │─────>│ y │───┘
1179-
└───┘ └───┘ └───┘ └───┘
1180-
│ │</code></pre>
1158+
Before deciding how to generate something like this, let's
1159+
take a quick look at one more example and which AST node
1160+
each operand should lead to.
1161+
</p>
11811162
<pre><code>x || y && z || w
11821163
┌────┐ ┌────┐
11831164
┌──│ || │──┐ │ || │
11841165
│ └────┘ │ └────┘
11851166
┌────┐ ┌───┐ ┌────┐ ┌───┐
1186-
┌──│ || │──┐ │ w │ │ || │ │ w │
1187-
│ └────┘ │ └───┘ └────┘ └───┘
1188-
┌───┐ ┌────┐ ┌───┐ ┌────┐ ^
1189-
│ x │ ┌──│ && │──┐ │ x │───┐ │ && │ │
1190-
└───┘ │ └────┘ │ └───┘ V └────┘ │
1191-
┌───┐ ┌───┐ │ ┌───┐ ┌───┐
1192-
│ y │ │ z │ │ │ y │─────>│ z │
1193-
└───┘ └───┘ │ └───┘ └───┘
1194-
│ │ │
1195-
</code></pre>
1196-
<p>
1197-
The observation is that from the basic block of each
1198-
operand, one edge should lead to the merge block and the
1199-
other one should lead to the block containing the next
1200-
operand on the right in the AST.
1167+
┌──│ || │──┐ │ w │ │ || │ │ w │<
1168+
│ └────┘ │ └───┘ └────┘ └───┘ │
1169+
┌───┐ ┌────┐ ┌───┐ ┌────┐ ^ │
1170+
│ x │ ┌──│ && │──┐ │ x │───┐ │ && │ │ │
1171+
└───┘ │ └────┘ │ └───┘ V └────┘ │ │
1172+
┌───┐ ┌───┐ │ ┌───┐ ┌───┐ │
1173+
│ y │ │ z │ │ │ y │─────>│ z │ │
1174+
└───┘ └───┘ │ └───┘ └───┘ │
1175+
│ └───────── │ ──┘</code></pre>
1176+
<p>
1177+
This latest example shows that when operators are combined,
1178+
the resulting execution paths can end up being quite
1179+
complicated, so there is no need to look for patterns, just
1180+
stick to a good old recursive solution.
12011181
</p>
12021182
<p>
12031183
The idea is to create a function that traverses the AST of a
@@ -1389,8 +1369,8 @@ <h2>Conditional Binary Operators</h2>
13891369
<p>
13901370
Since <code>generateExpr()</code> can change the current
13911371
block, <code>rhsBB</code> needs to be retrieved once again
1392-
(consider <code>x || (y && z)</code> where the insertion
1393-
point after the function is run is the
1372+
(consider <code>x || y && z</code> where the insertion point
1373+
after the function is run is the
13941374
<code>and.merge</code> block). Then the insertion point is
13951375
set to the merge block and a phi node is created.
13961376
</p>
@@ -1471,8 +1451,9 @@ <h2>Conditional Binary Operators</h2>
14711451
When <code>bz</code> is reached, <code>$x</code> and
14721452
<code>$y</code> are guaranteed to be false. Since the
14731453
results are <code>||</code>-d together and
1474-
<code>false || false || z == z</code>, the result only
1475-
depends on the value of <code>$z</code>.
1454+
<code>false || false || z</code> can be simplified to
1455+
<code>z</code>, the result only depends on the value of
1456+
<code>$z</code>.
14761457
</p>
14771458
<p>
14781459
These observations are what

0 commit comments

Comments
 (0)