summaryrefslogtreecommitdiff
path: root/chapter06
diff options
context:
space:
mode:
authorDibyendu Majumdar <dibyendumajumdar@users.noreply.github.com>2023-11-11 23:17:22 +0000
committerGitHub <noreply@github.com>2023-11-11 23:17:22 +0000
commit1a9cac97df063af82516ca35bde34fde62d24c56 (patch)
tree7bab31bac1cf890466b3f0884a0f59c784cc0027 /chapter06
parent077b25007cf8af3c3673d017625ea255bda87620 (diff)
downloadSimple-1a9cac97df063af82516ca35bde34fde62d24c56.tar.gz
Simple-1a9cac97df063af82516ca35bde34fde62d24c56.zip
Chapter 6 (#38)
* #37 Create chapter 6 skeleton * #37 Revise type system - TOP, BOTTOM, CONTROL are now directly implemented in Type, XCONTROL is a new type and that's implemented directly too. We have a _type field that is like an enum. * #37 Revise Type.meet() - this is now final. Subclasses can override xmeet() which is protected but now handles simple types in Type itself. * #37 Basic test for peephole of CFG * #37 Revise compute of if * #37 Revise compute of if * #37 WIP peephole of If * #37 WIP peephole of If * #37 WIP peephole of If * #37 WIP peephole of If * #37 WIP peephole of If * #37 WIP fix the chars inserted by IntelliJ * #37 WIP Fix compute type in IfNode * #37 WIP Null check region inputs * #37 WIP Fix where we choose great grand parent instead of grand parent Add new test * #37 WIP More fixes and tests * #37 WIP Some minor adjustments * Cleanup folding dead IF tests Remove isDeadCtrl & just test for the constant. Remove DEAD_CTRL, doesn't properly reset between tests. Move keep/unkeep to Node. PhiNode has a proper compute() call. ProjNode ideal is a lot simpler. Region doesn't ever get a null. Move the mergeScopes Region peep till after all Phis are added. Type TOP and XCONTROL count as constants. TypeTuple gets a toString * #37 WIP Upate README * #37 WIP Region node should check if one of its inputs is a region that needs to be peepholed * #37 WIP If node should check if its ctrl input is a Region that has only 1 live input so can be peepholed * #37 WIP Further update to README to make the peephole logic clearer. Also discuss the relationship between Region and Phi nodes * Region removes dead input paths Remove region-folding in Return, If. Add a Makefile. Reformat triple-quote lines to be emacs-friendly. * StopNode idealize Return type sharpens to include control input. * Remove !=, >=, >; use Not and <, <=, == Fix bug mixing rotating different ops. Generalize Add fold constants; apply to Mul, EQ. Fix Div.copy * Fix bug rotating e.g. < or <= Peep is only valid for associative ops * #37 WIP Doc updates * Work over the Ch6 Readme Major issue: Region inputs align 1-to-1 with Phi inputs, but a Region can have zero to many Phis. When we lose a Region input (dead path), we need to lose the same inputs to each Phi - but we don't lose the Phis themselves, unless we're folding away down to the last live input. * Update chapter06/README.md Co-authored-by: Yichen Yan <oraluben@outlook.com> * Update chapter06/README.md Co-authored-by: Yichen Yan <oraluben@outlook.com> * #37 Backport type system changes to chapter 2 * #37 Backport type system changes to chapter 3 * #37 Backport type system changes to chapter 4 * #37 Backport type system changes to chapter 5 * #37 Merge chapter 5 updates from main. Remove language grammar module as we describe the grammar incrementally in each chapter. * #37 Chapter 6 doc updates - added visuals to show effects of CFG peephole * #37 Sync some updates to chapter 2 * #37 Sync some updates to chapter 3 * #37 Sync some updates to chapter 4 --------- Co-authored-by: Cliff Click <cliffc@acm.org> Co-authored-by: Yichen Yan <oraluben@outlook.com>
Diffstat (limited to 'chapter06')
-rw-r--r--chapter06/Makefile92
-rw-r--r--chapter06/README.md154
-rw-r--r--chapter06/docs/06-grammar.md83
-rw-r--r--chapter06/docs/06-graph1-post.gv29
-rw-r--r--chapter06/docs/06-graph1-post.svg67
-rw-r--r--chapter06/docs/06-graph1-pre.gv52
-rw-r--r--chapter06/docs/06-graph1-pre.svg160
-rw-r--r--chapter06/docs/06-graph2-post.gv33
-rw-r--r--chapter06/docs/06-graph2-post.svg67
-rw-r--r--chapter06/docs/06-graph2-pre.gv55
-rw-r--r--chapter06/docs/06-graph2-pre.svg154
-rw-r--r--chapter06/docs/06-graph3-false.gv35
-rw-r--r--chapter06/docs/06-graph3-false.svg67
-rw-r--r--chapter06/docs/06-graph3-nonconst.gv86
-rw-r--r--chapter06/docs/06-graph3-nonconst.svg290
-rw-r--r--chapter06/docs/06-graph3-true.gv35
-rw-r--r--chapter06/docs/06-graph3-true.svg67
-rw-r--r--chapter06/docs/06-graph4-arg2.gv37
-rw-r--r--chapter06/docs/06-graph4-arg2.svg67
-rw-r--r--chapter06/docs/06-graph4-nonconst.gv122
-rw-r--r--chapter06/docs/06-graph4-nonconst.svg456
-rw-r--r--chapter06/docs/06-graph4-true.gv37
-rw-r--r--chapter06/docs/06-graph4-true.svg67
-rw-r--r--chapter06/docs/build-graphs.sh10
-rw-r--r--chapter06/pom.xml14
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/GraphVisualizer.java235
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/Parser.java528
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/AddNode.java154
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/BoolNode.java58
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/ConstantNode.java41
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/DivNode.java37
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/IfNode.java44
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/MinusNode.java28
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/MulNode.java56
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/MultiNode.java9
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/Node.java380
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/NotNode.java28
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/PhiNode.java98
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/ProjNode.java50
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/RegionNode.java56
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/ReturnNode.java44
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/ScopeNode.java138
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/StartNode.java37
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/StopNode.java51
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/node/SubNode.java41
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/type/Type.java77
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/type/TypeInteger.java63
-rw-r--r--chapter06/src/main/java/com/seaofnodes/simple/type/TypeTuple.java32
-rw-r--r--chapter06/src/test/java/com/seaofnodes/simple/Chapter06Test.java795
49 files changed, 5416 insertions, 0 deletions
diff --git a/chapter06/Makefile b/chapter06/Makefile
new file mode 100644
index 00000000..f2839f15
--- /dev/null
+++ b/chapter06/Makefile
@@ -0,0 +1,92 @@
+SHELL := /bin/bash
+.DELETE_ON_ERROR:
+
+# for printing variable values
+# usage: make print-VARIABLE
+# > VARIABLE = value_of_variable
+print-% : ; @echo $* = $($*)
+
+# literal space
+space := $() $()
+
+# Decide OS-specific questions
+# jar-file seperator
+ifeq ($(OS),Windows_NT)
+ SEP = ;
+else
+ SEP = :
+endif
+# Find a reasonable ctags.
+CTAGS = $(shell which ctags)
+# Hack for MacOS: /usr/bin/ctags is unfriendly, so look for ctags from brew
+ifeq ($(UNAME),Darwin)
+ CTAGS = $(shell brew list ctags 2> /dev/null | grep bin/ctags)
+endif
+
+# Fun Args to javac. Mostly limit to java8 source definitions, and fairly
+# aggressive lint warnings.
+JAVAC_ARGS = -g -source 17 -target 17
+
+# Source code
+SIMPLE := com/seaofnodes/simple
+SRC := src/main/java
+TST := src/test/java
+CLZDIR:= build/classes
+main_javas := $(wildcard $(SRC)/$(SIMPLE)/*java $(SRC)/$(SIMPLE)/*/*java)
+test_javas := $(wildcard $(TST)/$(SIMPLE)/*java $(TST)/$(SIMPLE)/*/*java)
+main_classes := $(patsubst $(SRC)/%java,$(CLZDIR)/main/%class,$(main_javas))
+test_classes := $(patsubst $(TST)/%java,$(CLZDIR)/test/%class,$(test_javas))
+test_cp := $(patsubst $(TST)/$(SIMPLE)/%.java,com.seaofnodes.simple.%,$(test_javas))
+classes = $(main_classes) $(test_classes)
+# All the libraries
+libs = $(wildcard lib/*jar)
+jars = $(subst $(space),$(SEP),$(libs))
+
+
+default_targets := $(main_classes) $(test_classes)
+# Optionally add ctags to the default target if a reasonable one was found.
+ifneq ($(CTAGS),)
+default_targets += tags
+endif
+
+default: $(default_targets)
+
+# Compile just the out-of-date files
+$(main_classes): build/classes/main/%class: $(SRC)/%java
+ @echo "compiling " $@ " because " $?
+ @[ -d $(CLZDIR)/main ] || mkdir -p $(CLZDIR)/main
+ @javac $(JAVAC_ARGS) -cp "$(CLZDIR)/main$(SEP)$(jars)" -sourcepath $(SRC) -d $(CLZDIR)/main $(main_javas)
+
+$(test_classes): $(CLZDIR)/test/%class: $(TST)/%java $(main_classes)
+ @echo "compiling " $@ " because " $?
+ @[ -d $(CLZDIR)/test ] || mkdir -p $(CLZDIR)/test
+ @javac $(JAVAC_ARGS) -cp "$(CLZDIR)/test$(SEP)$(CLZDIR)/main$(SEP)$(jars)" -sourcepath $(TST) -d $(CLZDIR)/test $(test_javas)
+
+# Base launch line for JVM tests
+JVM=nice java -ea -cp "build/classes/main${SEP}${jars}${SEP}$(CLZDIR)/test"
+
+tests: $(default_targets)
+ $(JVM) org.junit.runner.JUnitCore $(test_cp)
+
+.PHONY: clean
+clean:
+ rm -rf build
+ rm -f TAGS
+ (find . -name "*~" -exec rm {} \; 2>/dev/null; exit 0)
+
+# Download libs from maven
+lib: lib/junit-4.12.jar lib/hamcrest-core-1.3.jar
+
+# Jars
+lib/junit-4.12.jar:
+ @[ -d lib ] || mkdir -p lib
+ @(cd lib; wget https://repo1.maven.org/maven2/junit/junit/4.12/junit-4.12.jar)
+
+lib/hamcrest-core-1.3.jar:
+ @[ -d lib ] || mkdir -p lib
+ @(cd lib; wget https://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar)
+
+# Build emacs tags (part of a tasty emacs ide experience)
+tags: $(main_javas) $(test_javas)
+ @rm -f TAGS
+ @$(CTAGS) -e --recurse=yes --extra=+q --fields=+fksaiS $(SRC) $(TST)
diff --git a/chapter06/README.md b/chapter06/README.md
new file mode 100644
index 00000000..dd08aff5
--- /dev/null
+++ b/chapter06/README.md
@@ -0,0 +1,154 @@
+# Chapter 6
+
+In this chapter we do not add new language features. Our goal is to add peephole optimization to `if` statements.
+
+## Type System Revision
+
+We now need to extend the type system to introduce another lattice structure, in addition to the Integer lattice.
+As before we denote "top" by T and "bottom" by ⊥.
+
+* "ctrl" represents a live control, whereas "~ctrl" represents a dead control.
+* "INT" refers to the integer type.
+
+| | ⊥ | Ctrl | ~Ctrl | T | INT |
+|-------|---|------|-------|-------|-----|
+| T | ⊥ | Ctrl | ~Ctrl | T | INT |
+| Ctrl | ⊥ | Ctrl | Ctrl | Ctrl | ⊥ |
+| ~Ctrl | ⊥ | Ctrl | ~Ctrl | ~Ctrl | ⊥ |
+| ⊥ | ⊥ | ⊥ | ⊥ | ⊥ | ⊥ |
+
+* "top" meets any type is that type,
+* "bottom" meets any type is "bottom",
+* "top", "bottom", "ctrl", "~ctrl" are classed as simple types,
+* "ctrl" meets "~ctrl" is "ctrl"
+* Unless covered above, a simple type meets non-simple results in "bottom"
+
+## Peephole of `if`
+
+* Our general strategy is to continue parsing the entire `if` statement even if we know that one branch is dead.
+* This approach creates dead nodes which get cleaned up by dead code
+ elimination. The benefits are that we do not introduce special logic during
+ parsing, the normal graph building machinery takes care of cleaning up dead
+ nodes.
+* When we peephole an `If` node we test if the predicate is a constant. If so,
+ then one branch or the other dead, depending on if the constant is `0` or not.
+* When we add the `Proj` nodes, in this scenario, we already know one of the projections is dead. The peephole of the
+ relevant `Proj` node replaces the `Proj` with a `Constant` of type "~ctrl" indicating dead control.
+* At this point our dead code elimination would kill the `If` node since it has no uses
+ - but we cannot do that because we need to continue the parse. The
+ important observation is that *we __can__ kill the `If` node after __both__
+ the `Proj` nodes have been created*, because any subsequent control flow can
+ only see the `Proj` nodes.
+* To ensure this, we add a dummy user to `If` before creating the first `Proj` and remove it immediately after, allowing the
+ second `Proj` to trigger a dead code elimination of the `If`.
+* The live `Proj` gets replaced by the parent of `If`, whereas the dead `Proj` gets replaced by "~ctrl".
+* The parsing then continues as normal.
+* The other changes are when we reach the merge point with a dead control.
+* Again keeping with our strategy we merge as normal, including creating `Phi` nodes.
+* The `Region` may have have one of its inputs as "~ctrl". This will be seen
+ by the `Phi` which then optimizes itself. If the `Phi` has just one live
+ input, the `Phi` peephole replaces itself with the remaining input.
+* Finally, at the end, when the `Region` node is peepholed, we see that it has only one live input and no Phi uses
+ and can be replaced with its one live input.
+
+## Discussion of `Region` and `Phi`
+
+One of the invariants we maintain is that the for each control input to a
+`Region` every `Phi` has a 1-to-1 relationship with a data input. Thus, if a
+`Region` loses a control input, every `Phi`'s corresponding data input must be
+deleted. Conversely, we cannot collapse a Region until it has no dependent
+`Phi`s.
+
+When processing `If` we do not remove control inputs to a `Region`, instead the
+dead control input is simply set to `Constant(~ctrl)`. The peephole logic in
+a `Phi` notices this and replaces itself with the live input.
+
+## Examples
+
+We show a number of examples of peephole in action.
+
+### Example 1
+
+```java
+if( true ) return 2;
+return 1;
+```
+
+Before peephole:
+
+![Graph1-Pre](./docs/06-graph1-pre.svg)
+
+After peephole:
+
+![Graph1-Post](./docs/06-graph1-post.svg)
+
+### Example 2
+
+```java
+int a=1;
+if( true )
+ a=2;
+else
+ a=3;
+return a;
+```
+
+Before peephole:
+
+![Graph2-Pre](./docs/06-graph2-pre.svg)
+
+After peephole:
+
+![Graph2-Post](./docs/06-graph2-post.svg)
+
+### Example 3
+
+```java
+int a = 0;
+int b = 1;
+if( arg ) {
+ a = 2;
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b;
+```
+
+In this example, `arg` is defined externally. If `arg` is given a non-constant value, then the graph looks like this:
+
+![Graph3-NonConst](./docs/06-graph3-nonconst.svg)
+
+If we set `arg` to a non-zero constant such as `1` we get:
+
+![Graph3-True](./docs/06-graph3-true.svg)
+
+If we set `arg` to `0` which means false, then:
+
+![Graph3-False](./docs/06-graph3-false.svg)
+
+### Example 4
+
+```java
+int a = 0;
+int b = 1;
+int c = 0;
+if( arg ) {
+ a = 1;
+ if( arg==2 ) { c=2; } else { c=3; }
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b+c;
+```
+
+In this example, `arg` is defined externally. If `arg` is given a non-constant value, then the graph looks like this:
+
+![Graph4-NonConst](./docs/06-graph4-nonconst.svg)
+
+If we set `arg` to `1` we get:
+
+![Graph4-True](./docs/06-graph4-true.svg)
+
+If we set `arg` to `2`, then:
+
+![Graph4-Arg2](./docs/06-graph4-arg2.svg)
diff --git a/chapter06/docs/06-grammar.md b/chapter06/docs/06-grammar.md
new file mode 100644
index 00000000..ea779a5a
--- /dev/null
+++ b/chapter06/docs/06-grammar.md
@@ -0,0 +1,83 @@
+# Grammar for Chapter 6
+
+```antlrv4
+grammar SimpleLanguage;
+
+program
+ : statement+ EOF
+ ;
+
+statement
+ : returnStatement
+ | declStatement
+ | blockStatment
+ | expressionStatement
+ | ifStatement
+ | metaStatement
+ ;
+
+ifStatement
+ : 'if' '(' expression ')' statement ('else' statement)?
+ ;
+
+metaStatement
+ : '#showGraph' ';'
+ ;
+
+expressionStatement
+ : IDENTIFIER '=' expression ';'
+ ;
+
+blockStatement
+ : '{' statement+ '}'
+ ;
+
+declStatement
+ : 'int' IDENTIFIER '=' expression ';'
+ ;
+
+returnStatement
+ : 'return' expression ';'
+ ;
+
+expression
+ : comparisonExpression
+ ;
+
+comparisonExpression
+ : additiveExpression (('==' | '!='| '>'| '<'| '>='| '<=') additiveExpression)*
+ ;
+
+additiveExpression
+ : multiplicativeExpression (('+' | '-') multiplicativeExpression)*
+ ;
+
+multiplicativeExpression
+ : unaryExpression (('*' | '/') unaryExpression)*
+ ;
+
+unaryExpression
+ : ('-') unaryExpression
+ | primaryExpression
+ ;
+
+primaryExpression
+ : IDENTIFIER
+ | INTEGER_LITERAL
+ | 'true'
+ | 'false'
+ | '(' expression ')'
+ ;
+
+INTEGER_LITERAL
+ : [1-9][0-9]*
+ | [0]
+ ;
+
+IDENTIFIER
+ : NON_DIGIT (NON_DIGIT | DIGIT)*
+ ;
+
+NON_DIGIT: [a-zA-Z_];
+DEC_DIGIT: [0-9];
+``` \ No newline at end of file
diff --git a/chapter06/docs/06-graph1-post.gv b/chapter06/docs/06-graph1-post.gv
new file mode 100644
index 00000000..59214346
--- /dev/null
+++ b/chapter06/docs/06-graph1-post.gv
@@ -0,0 +1,29 @@
+digraph chapter06 {
+/*
+if( true ) return 2;
+return 1;
+
+*/
+ rankdir=BT;
+ ordering="in";
+ concentrate="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_12 [ label="#2" ];
+ Return13 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return13[taillabel=0 color=red];
+ Return13 -> Start2:p0[taillabel=0 color=red];
+ Return13 -> Con_12[taillabel=1];
+} \ No newline at end of file
diff --git a/chapter06/docs/06-graph1-post.svg b/chapter06/docs/06-graph1-post.svg
new file mode 100644
index 00000000..faabcd8c
--- /dev/null
+++ b/chapter06/docs/06-graph1-post.svg
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.43.0 (0)
+ -->
+<!-- Title: chapter06 Pages: 1 -->
+<svg width="178pt" height="248pt"
+ viewBox="0.00 0.00 178.00 248.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 244)">
+<title>chapter06</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-244 174,-244 174,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-232 162,-232 162,-8 8,-8"/>
+</g>
+<!-- Start2 -->
+<g id="node1" class="node">
+<title>Start2</title>
+<polygon fill="yellow" stroke="transparent" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<polygon fill="none" stroke="black" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<text text-anchor="start" x="30.5" y="-203.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="24,-164 24,-195 74,-195 74,-164 24,-164"/>
+<polygon fill="yellow" stroke="transparent" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<polygon fill="none" stroke="black" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<text text-anchor="start" x="32" y="-175.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+</g>
+<!-- Stop3 -->
+<g id="node2" class="node">
+<title>Stop3</title>
+<polygon fill="yellow" stroke="black" points="112,-52 58,-52 58,-16 112,-16 112,-52"/>
+<text text-anchor="middle" x="85" y="-30.3" font-family="Times,serif" font-size="14.00">Stop</text>
+</g>
+<!-- Return13 -->
+<g id="node4" class="node">
+<title>Return13</title>
+<polygon fill="yellow" stroke="black" points="118,-124 52,-124 52,-88 118,-88 118,-124"/>
+<text text-anchor="middle" x="85" y="-102.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Stop3&#45;&gt;Return13 -->
+<g id="edge1" class="edge">
+<title>Stop3&#45;&gt;Return13</title>
+<path fill="none" stroke="red" d="M85,-52.3C85,-60.02 85,-69.29 85,-77.89"/>
+<polygon fill="red" stroke="red" points="81.5,-77.9 85,-87.9 88.5,-77.9 81.5,-77.9"/>
+<text text-anchor="middle" x="82" y="-54.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Con_12 -->
+<g id="node3" class="node">
+<title>Con_12</title>
+<ellipse fill="none" stroke="black" cx="127" cy="-192" rx="27" ry="18"/>
+<text text-anchor="middle" x="127" y="-188.3" font-family="Times,serif" font-size="14.00">#2</text>
+</g>
+<!-- Return13&#45;&gt;Start2 -->
+<g id="edge2" class="edge">
+<title>Return13&#45;&gt;Start2:p0</title>
+<path fill="none" stroke="red" d="M68.26,-124.13C60.77,-133.26 52.99,-145.23 50.13,-158.06"/>
+<polygon fill="red" stroke="red" points="46.66,-157.67 49,-168 53.61,-158.46 46.66,-157.67"/>
+<text text-anchor="middle" x="65.26" y="-126.73" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Return13&#45;&gt;Con_12 -->
+<g id="edge3" class="edge">
+<title>Return13&#45;&gt;Con_12</title>
+<path fill="none" stroke="black" d="M93.7,-124.4C99.66,-136.31 107.65,-152.3 114.31,-165.62"/>
+<polygon fill="black" stroke="black" points="111.28,-167.38 118.88,-174.76 117.54,-164.25 111.28,-167.38"/>
+<text text-anchor="middle" x="90.7" y="-127" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+</g>
+</svg>
diff --git a/chapter06/docs/06-graph1-pre.gv b/chapter06/docs/06-graph1-pre.gv
new file mode 100644
index 00000000..b04fa2f9
--- /dev/null
+++ b/chapter06/docs/06-graph1-pre.gv
@@ -0,0 +1,52 @@
+digraph chapter06 {
+/*
+if( true ) return 2;
+return 1;
+
+*/
+ rankdir=BT;
+ ordering="in";
+ concentrate="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_6 [ label="#1" ];
+ If7 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Con_11 [ label="#2" ];
+ Return12 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ Con_13 [ label="#~CONTROL" ];
+ Region14 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Con_15 [ label="#1" ];
+ Return16 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ { rank=same; Region14;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return12[taillabel=0 color=red];
+ Stop3 -> Return16[taillabel=1 color=red];
+ If7 -> Start2:p0[taillabel=0 color=red];
+ If7 -> Con_6[taillabel=1];
+ Return12 -> If7:p0[taillabel=0 color=red];
+ Return12 -> Con_11[taillabel=1];
+ Region14 -> Con_13[taillabel=1];
+ Region14 -> If7:p1[taillabel=2 color=red];
+ Return16 -> Region14[taillabel=0 color=red];
+ Return16 -> Con_15[taillabel=1];
+} \ No newline at end of file
diff --git a/chapter06/docs/06-graph1-pre.svg b/chapter06/docs/06-graph1-pre.svg
new file mode 100644
index 00000000..f80edcfc
--- /dev/null
+++ b/chapter06/docs/06-graph1-pre.svg
@@ -0,0 +1,160 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.43.0 (0)
+ -->
+<!-- Title: chapter06 Pages: 1 -->
+<svg width="316pt" height="420pt"
+ viewBox="0.00 0.00 316.00 420.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 416)">
+<title>chapter06</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-416 312,-416 312,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-404 300,-404 300,-8 8,-8"/>
+</g>
+<!-- Start2 -->
+<g id="node1" class="node">
+<title>Start2</title>
+<polygon fill="yellow" stroke="transparent" points="35,-367 35,-392 85,-392 85,-367 35,-367"/>
+<polygon fill="none" stroke="black" points="35,-367 35,-392 85,-392 85,-367 35,-367"/>
+<text text-anchor="start" x="41.5" y="-375.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="35,-336 35,-367 85,-367 85,-336 35,-336"/>
+<polygon fill="yellow" stroke="transparent" points="40,-341 40,-362 80,-362 80,-341 40,-341"/>
+<polygon fill="none" stroke="black" points="40,-341 40,-362 80,-362 80,-341 40,-341"/>
+<text text-anchor="start" x="43" y="-347.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+</g>
+<!-- Stop3 -->
+<g id="node2" class="node">
+<title>Stop3</title>
+<polygon fill="yellow" stroke="black" points="150,-52 96,-52 96,-16 150,-16 150,-52"/>
+<text text-anchor="middle" x="123" y="-30.3" font-family="Times,serif" font-size="14.00">Stop</text>
+</g>
+<!-- Return12 -->
+<g id="node6" class="node">
+<title>Return12</title>
+<polygon fill="yellow" stroke="black" points="114,-124 48,-124 48,-88 114,-88 114,-124"/>
+<text text-anchor="middle" x="81" y="-102.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Stop3&#45;&gt;Return12 -->
+<g id="edge1" class="edge">
+<title>Stop3&#45;&gt;Return12</title>
+<path fill="none" stroke="red" d="M112.62,-52.3C107.74,-60.44 101.81,-70.31 96.42,-79.3"/>
+<polygon fill="red" stroke="red" points="93.41,-77.52 91.26,-87.9 99.41,-81.12 93.41,-77.52"/>
+<text text-anchor="middle" x="109.62" y="-54.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Return16 -->
+<g id="node10" class="node">
+<title>Return16</title>
+<polygon fill="yellow" stroke="black" points="198,-124 132,-124 132,-88 198,-88 198,-124"/>
+<text text-anchor="middle" x="165" y="-102.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Stop3&#45;&gt;Return16 -->
+<g id="edge2" class="edge">
+<title>Stop3&#45;&gt;Return16</title>
+<path fill="none" stroke="red" d="M133.38,-52.3C138.26,-60.44 144.19,-70.31 149.58,-79.3"/>
+<polygon fill="red" stroke="red" points="146.59,-81.12 154.74,-87.9 152.59,-77.52 146.59,-81.12"/>
+<text text-anchor="middle" x="130.38" y="-54.9" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Con_6 -->
+<g id="node3" class="node">
+<title>Con_6</title>
+<ellipse fill="none" stroke="black" cx="138" cy="-364" rx="27" ry="18"/>
+<text text-anchor="middle" x="138" y="-360.3" font-family="Times,serif" font-size="14.00">#1</text>
+</g>
+<!-- If7 -->
+<g id="node4" class="node">
+<title>If7</title>
+<polygon fill="yellow" stroke="transparent" points="24,-267 24,-292 118,-292 118,-267 24,-267"/>
+<polygon fill="none" stroke="black" points="24,-267 24,-292 118,-292 118,-267 24,-267"/>
+<text text-anchor="start" x="65.5" y="-275.8" font-family="Times,serif" font-size="14.00">If</text>
+<polygon fill="none" stroke="black" points="24,-236 24,-267 118,-267 118,-236 24,-236"/>
+<polygon fill="yellow" stroke="transparent" points="29,-241 29,-262 69,-262 69,-241 29,-241"/>
+<polygon fill="none" stroke="black" points="29,-241 29,-262 69,-262 69,-241 29,-241"/>
+<text text-anchor="start" x="32" y="-247.8" font-family="Times,serif" font-size="14.00">True</text>
+<polygon fill="yellow" stroke="transparent" points="69,-241 69,-262 113,-262 113,-241 69,-241"/>
+<polygon fill="none" stroke="black" points="69,-241 69,-262 113,-262 113,-241 69,-241"/>
+<text text-anchor="start" x="72" y="-247.8" font-family="Times,serif" font-size="14.00">False</text>
+</g>
+<!-- If7&#45;&gt;Start2 -->
+<g id="edge3" class="edge">
+<title>If7&#45;&gt;Start2:p0</title>
+<path fill="none" stroke="red" d="M64.35,-296.23C62.56,-306.6 60.93,-318.45 60.29,-329.99"/>
+<polygon fill="red" stroke="red" points="56.79,-329.9 60,-340 63.79,-330.11 56.79,-329.9"/>
+<text text-anchor="middle" x="61.35" y="-298.83" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- If7&#45;&gt;Con_6 -->
+<g id="edge4" class="edge">
+<title>If7&#45;&gt;Con_6</title>
+<path fill="none" stroke="black" d="M92.2,-296.01C101.67,-309.86 112.66,-325.94 121.5,-338.87"/>
+<polygon fill="black" stroke="black" points="118.7,-340.97 127.24,-347.25 124.48,-337.02 118.7,-340.97"/>
+<text text-anchor="middle" x="89.2" y="-298.61" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Con_11 -->
+<g id="node5" class="node">
+<title>Con_11</title>
+<ellipse fill="none" stroke="black" cx="43" cy="-178" rx="27" ry="18"/>
+<text text-anchor="middle" x="43" y="-174.3" font-family="Times,serif" font-size="14.00">#2</text>
+</g>
+<!-- Return12&#45;&gt;If7 -->
+<g id="edge5" class="edge">
+<title>Return12&#45;&gt;If7:p0</title>
+<path fill="none" stroke="red" d="M83.45,-124.21C85.4,-142.49 86.7,-172.01 79,-196 73.01,-214.66 56.37,-216.36 50.82,-229.83"/>
+<polygon fill="red" stroke="red" points="47.32,-229.54 49,-240 54.21,-230.77 47.32,-229.54"/>
+<text text-anchor="middle" x="80.45" y="-126.81" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Return12&#45;&gt;Con_11 -->
+<g id="edge6" class="edge">
+<title>Return12&#45;&gt;Con_11</title>
+<path fill="none" stroke="black" d="M71.61,-124.3C67.1,-132.6 61.63,-142.69 56.67,-151.81"/>
+<polygon fill="black" stroke="black" points="53.46,-150.4 51.76,-160.86 59.61,-153.74 53.46,-150.4"/>
+<text text-anchor="middle" x="68.61" y="-126.9" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Con_13 -->
+<g id="node7" class="node">
+<title>Con_13</title>
+<ellipse fill="none" stroke="black" cx="218" cy="-264" rx="74.19" ry="18"/>
+<text text-anchor="middle" x="218" y="-260.3" font-family="Times,serif" font-size="14.00">#~CONTROL</text>
+</g>
+<!-- Region14 -->
+<g id="node8" class="node">
+<title>Region14</title>
+<polygon fill="yellow" stroke="black" points="190,-196 124,-196 124,-160 190,-160 190,-196"/>
+<text text-anchor="middle" x="157" y="-174.3" font-family="Times,serif" font-size="14.00">Region</text>
+</g>
+<!-- Region14&#45;&gt;If7 -->
+<g id="edge8" class="edge">
+<title>Region14&#45;&gt;If7:p1</title>
+<path fill="none" stroke="red" d="M150.64,-196.06C144.08,-212.53 133.37,-236.39 122.96,-246.35"/>
+<polygon fill="red" stroke="red" points="121.27,-243.29 114,-251 124.49,-249.5 121.27,-243.29"/>
+<text text-anchor="middle" x="147.64" y="-198.66" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Region14&#45;&gt;Con_13 -->
+<g id="edge7" class="edge">
+<title>Region14&#45;&gt;Con_13</title>
+<path fill="none" stroke="black" d="M169.64,-196.4C178.34,-208.39 190.04,-224.5 199.75,-237.87"/>
+<polygon fill="black" stroke="black" points="197.09,-240.16 205.79,-246.19 202.75,-236.04 197.09,-240.16"/>
+<text text-anchor="middle" x="166.64" y="-199" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Con_15 -->
+<g id="node9" class="node">
+<title>Con_15</title>
+<ellipse fill="none" stroke="black" cx="235" cy="-178" rx="27" ry="18"/>
+<text text-anchor="middle" x="235" y="-174.3" font-family="Times,serif" font-size="14.00">#1</text>
+</g>
+<!-- Return16&#45;&gt;Region14 -->
+<g id="edge9" class="edge">
+<title>Return16&#45;&gt;Region14</title>
+<path fill="none" stroke="red" d="M163.02,-124.3C162.14,-132.02 161.08,-141.29 160.1,-149.89"/>
+<polygon fill="red" stroke="red" points="156.61,-149.56 158.95,-159.9 163.57,-150.36 156.61,-149.56"/>
+<text text-anchor="middle" x="160.02" y="-126.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Return16&#45;&gt;Con_15 -->
+<g id="edge10" class="edge">
+<title>Return16&#45;&gt;Con_15</title>
+<path fill="none" stroke="black" d="M182.3,-124.3C191.72,-133.72 203.47,-145.47 213.47,-155.47"/>
+<polygon fill="black" stroke="black" points="211.18,-158.12 220.72,-162.72 216.12,-153.18 211.18,-158.12"/>
+<text text-anchor="middle" x="179.3" y="-126.9" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+</g>
+</svg>
diff --git a/chapter06/docs/06-graph2-post.gv b/chapter06/docs/06-graph2-post.gv
new file mode 100644
index 00000000..dacd80fd
--- /dev/null
+++ b/chapter06/docs/06-graph2-post.gv
@@ -0,0 +1,33 @@
+digraph chapter06 {
+/*
+int a=1;
+if( true )
+ a=2;
+else
+ a=3;
+return a;
+
+*/
+ rankdir=BT;
+ ordering="in";
+ concentrate="true";
+ subgraph cluster_Nodes {
+ Return17 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_13 [ label="#2" ];
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Return17 -> Start2:p0[taillabel=0 color=red];
+ Return17 -> Con_13[taillabel=1];
+ Stop3 -> Return17[taillabel=0 color=red];
+} \ No newline at end of file
diff --git a/chapter06/docs/06-graph2-post.svg b/chapter06/docs/06-graph2-post.svg
new file mode 100644
index 00000000..269eb15c
--- /dev/null
+++ b/chapter06/docs/06-graph2-post.svg
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.43.0 (0)
+ -->
+<!-- Title: chapter06 Pages: 1 -->
+<svg width="178pt" height="248pt"
+ viewBox="0.00 0.00 178.00 248.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 244)">
+<title>chapter06</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-244 174,-244 174,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-232 162,-232 162,-8 8,-8"/>
+</g>
+<!-- Return17 -->
+<g id="node1" class="node">
+<title>Return17</title>
+<polygon fill="yellow" stroke="black" points="118,-124 52,-124 52,-88 118,-88 118,-124"/>
+<text text-anchor="middle" x="85" y="-102.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Start2 -->
+<g id="node2" class="node">
+<title>Start2</title>
+<polygon fill="yellow" stroke="transparent" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<polygon fill="none" stroke="black" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<text text-anchor="start" x="30.5" y="-203.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="24,-164 24,-195 74,-195 74,-164 24,-164"/>
+<polygon fill="yellow" stroke="transparent" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<polygon fill="none" stroke="black" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<text text-anchor="start" x="32" y="-175.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+</g>
+<!-- Return17&#45;&gt;Start2 -->
+<g id="edge1" class="edge">
+<title>Return17&#45;&gt;Start2:p0</title>
+<path fill="none" stroke="red" d="M68.26,-124.13C60.77,-133.26 52.99,-145.23 50.13,-158.06"/>
+<polygon fill="red" stroke="red" points="46.66,-157.67 49,-168 53.61,-158.46 46.66,-157.67"/>
+<text text-anchor="middle" x="65.26" y="-126.73" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Con_13 -->
+<g id="node4" class="node">
+<title>Con_13</title>
+<ellipse fill="none" stroke="black" cx="127" cy="-192" rx="27" ry="18"/>
+<text text-anchor="middle" x="127" y="-188.3" font-family="Times,serif" font-size="14.00">#2</text>
+</g>
+<!-- Return17&#45;&gt;Con_13 -->
+<g id="edge2" class="edge">
+<title>Return17&#45;&gt;Con_13</title>
+<path fill="none" stroke="black" d="M93.7,-124.4C99.66,-136.31 107.65,-152.3 114.31,-165.62"/>
+<polygon fill="black" stroke="black" points="111.28,-167.38 118.88,-174.76 117.54,-164.25 111.28,-167.38"/>
+<text text-anchor="middle" x="90.7" y="-127" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Stop3 -->
+<g id="node3" class="node">
+<title>Stop3</title>
+<polygon fill="yellow" stroke="black" points="112,-52 58,-52 58,-16 112,-16 112,-52"/>
+<text text-anchor="middle" x="85" y="-30.3" font-family="Times,serif" font-size="14.00">Stop</text>
+</g>
+<!-- Stop3&#45;&gt;Return17 -->
+<g id="edge3" class="edge">
+<title>Stop3&#45;&gt;Return17</title>
+<path fill="none" stroke="red" d="M85,-52.3C85,-60.02 85,-69.29 85,-77.89"/>
+<polygon fill="red" stroke="red" points="81.5,-77.9 85,-87.9 88.5,-77.9 81.5,-77.9"/>
+<text text-anchor="middle" x="82" y="-54.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+</g>
+</svg>
diff --git a/chapter06/docs/06-graph2-pre.gv b/chapter06/docs/06-graph2-pre.gv
new file mode 100644
index 00000000..d183b4c4
--- /dev/null
+++ b/chapter06/docs/06-graph2-pre.gv
@@ -0,0 +1,55 @@
+digraph chapter06 {
+/*
+int a=1;
+if( true )
+ a=2;
+else
+ a=3;
+return a;
+
+*/
+ rankdir=BT;
+ ordering="in";
+ concentrate="true";
+ subgraph cluster_Nodes {
+ Return16 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_7 [ label="#1" ];
+ If8 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Con_12 [ label="#2" ];
+ Con_13 [ label="#3" ];
+ Region14 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_a15 [ style=filled fillcolor=lightyellow label="&phi;_a" ];
+ { rank=same; Region14;Phi_a15;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Return16 -> Region14[taillabel=0 color=red];
+ Return16 -> Phi_a15[taillabel=1];
+ Stop3 -> Return16[taillabel=0 color=red];
+ If8 -> Start2:p0[taillabel=0 color=red];
+ If8 -> Con_7[taillabel=1];
+ Region14 -> If8:p0[taillabel=1 color=red];
+ Region14 -> If8:p1[taillabel=2 color=red];
+ Phi_a15 -> Region14 [style=dotted taillabel=0];
+ Phi_a15 -> Con_12[taillabel=1];
+ Phi_a15 -> Con_13[taillabel=2];
+}
diff --git a/chapter06/docs/06-graph2-pre.svg b/chapter06/docs/06-graph2-pre.svg
new file mode 100644
index 00000000..e9a4dd55
--- /dev/null
+++ b/chapter06/docs/06-graph2-pre.svg
@@ -0,0 +1,154 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.43.0 (0)
+ -->
+<!-- Title: chapter06 Pages: 1 -->
+<svg width="294pt" height="420pt"
+ viewBox="0.00 0.00 294.00 420.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 416)">
+<title>chapter06</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-416 290,-416 290,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-404 278,-404 278,-8 8,-8"/>
+</g>
+<!-- Return16 -->
+<g id="node1" class="node">
+<title>Return16</title>
+<polygon fill="yellow" stroke="black" points="187,-124 121,-124 121,-88 187,-88 187,-124"/>
+<text text-anchor="middle" x="154" y="-102.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Region14 -->
+<g id="node8" class="node">
+<title>Region14</title>
+<polygon fill="yellow" stroke="black" points="226,-196 160,-196 160,-160 226,-160 226,-196"/>
+<text text-anchor="middle" x="193" y="-174.3" font-family="Times,serif" font-size="14.00">Region</text>
+</g>
+<!-- Return16&#45;&gt;Region14 -->
+<g id="edge1" class="edge">
+<title>Return16&#45;&gt;Region14</title>
+<path fill="none" stroke="red" d="M163.64,-124.3C168.13,-132.36 173.56,-142.11 178.52,-151.02"/>
+<polygon fill="red" stroke="red" points="175.55,-152.86 183.47,-159.9 181.66,-149.46 175.55,-152.86"/>
+<text text-anchor="middle" x="160.64" y="-126.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Phi_a15 -->
+<g id="node9" class="node">
+<title>Phi_a15</title>
+<ellipse fill="lightyellow" stroke="black" cx="115" cy="-178" rx="27" ry="18"/>
+<text text-anchor="middle" x="115" y="-174.3" font-family="Times,serif" font-size="14.00">φ_a</text>
+</g>
+<!-- Return16&#45;&gt;Phi_a15 -->
+<g id="edge2" class="edge">
+<title>Return16&#45;&gt;Phi_a15</title>
+<path fill="none" stroke="black" d="M144.36,-124.3C139.69,-132.68 134,-142.9 128.87,-152.1"/>
+<polygon fill="black" stroke="black" points="125.8,-150.42 123.99,-160.86 131.92,-153.82 125.8,-150.42"/>
+<text text-anchor="middle" x="141.36" y="-126.9" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Start2 -->
+<g id="node2" class="node">
+<title>Start2</title>
+<polygon fill="yellow" stroke="transparent" points="126,-367 126,-392 176,-392 176,-367 126,-367"/>
+<polygon fill="none" stroke="black" points="126,-367 126,-392 176,-392 176,-367 126,-367"/>
+<text text-anchor="start" x="132.5" y="-375.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="126,-336 126,-367 176,-367 176,-336 126,-336"/>
+<polygon fill="yellow" stroke="transparent" points="131,-341 131,-362 171,-362 171,-341 131,-341"/>
+<polygon fill="none" stroke="black" points="131,-341 131,-362 171,-362 171,-341 131,-341"/>
+<text text-anchor="start" x="134" y="-347.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+</g>
+<!-- Stop3 -->
+<g id="node3" class="node">
+<title>Stop3</title>
+<polygon fill="yellow" stroke="black" points="181,-52 127,-52 127,-16 181,-16 181,-52"/>
+<text text-anchor="middle" x="154" y="-30.3" font-family="Times,serif" font-size="14.00">Stop</text>
+</g>
+<!-- Stop3&#45;&gt;Return16 -->
+<g id="edge3" class="edge">
+<title>Stop3&#45;&gt;Return16</title>
+<path fill="none" stroke="red" d="M154,-52.3C154,-60.02 154,-69.29 154,-77.89"/>
+<polygon fill="red" stroke="red" points="150.5,-77.9 154,-87.9 157.5,-77.9 150.5,-77.9"/>
+<text text-anchor="middle" x="151" y="-54.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Con_7 -->
+<g id="node4" class="node">
+<title>Con_7</title>
+<ellipse fill="none" stroke="black" cx="229" cy="-364" rx="27" ry="18"/>
+<text text-anchor="middle" x="229" y="-360.3" font-family="Times,serif" font-size="14.00">#1</text>
+</g>
+<!-- If8 -->
+<g id="node5" class="node">
+<title>If8</title>
+<polygon fill="yellow" stroke="transparent" points="168,-267 168,-292 262,-292 262,-267 168,-267"/>
+<polygon fill="none" stroke="black" points="168,-267 168,-292 262,-292 262,-267 168,-267"/>
+<text text-anchor="start" x="209.5" y="-275.8" font-family="Times,serif" font-size="14.00">If</text>
+<polygon fill="none" stroke="black" points="168,-236 168,-267 262,-267 262,-236 168,-236"/>
+<polygon fill="yellow" stroke="transparent" points="173,-241 173,-262 213,-262 213,-241 173,-241"/>
+<polygon fill="none" stroke="black" points="173,-241 173,-262 213,-262 213,-241 173,-241"/>
+<text text-anchor="start" x="176" y="-247.8" font-family="Times,serif" font-size="14.00">True</text>
+<polygon fill="yellow" stroke="transparent" points="213,-241 213,-262 257,-262 257,-241 213,-241"/>
+<polygon fill="none" stroke="black" points="213,-241 213,-262 257,-262 257,-241 213,-241"/>
+<text text-anchor="start" x="216" y="-247.8" font-family="Times,serif" font-size="14.00">False</text>
+</g>
+<!-- If8&#45;&gt;Start2 -->
+<g id="edge4" class="edge">
+<title>If8&#45;&gt;Start2:p0</title>
+<path fill="none" stroke="red" d="M205.76,-296.05C199.47,-314.72 190.61,-336.65 181.17,-346.18"/>
+<polygon fill="red" stroke="red" points="179.22,-343.25 172,-351 182.48,-349.44 179.22,-343.25"/>
+<text text-anchor="middle" x="202.76" y="-298.65" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- If8&#45;&gt;Con_7 -->
+<g id="edge5" class="edge">
+<title>If8&#45;&gt;Con_7</title>
+<path fill="none" stroke="black" d="M219.43,-296.01C221.25,-308.76 223.34,-323.4 225.1,-335.73"/>
+<polygon fill="black" stroke="black" points="221.67,-336.47 226.55,-345.87 228.6,-335.48 221.67,-336.47"/>
+<text text-anchor="middle" x="216.43" y="-298.61" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Con_12 -->
+<g id="node6" class="node">
+<title>Con_12</title>
+<ellipse fill="none" stroke="black" cx="43" cy="-264" rx="27" ry="18"/>
+<text text-anchor="middle" x="43" y="-260.3" font-family="Times,serif" font-size="14.00">#2</text>
+</g>
+<!-- Con_13 -->
+<g id="node7" class="node">
+<title>Con_13</title>
+<ellipse fill="none" stroke="black" cx="115" cy="-264" rx="27" ry="18"/>
+<text text-anchor="middle" x="115" y="-260.3" font-family="Times,serif" font-size="14.00">#3</text>
+</g>
+<!-- Region14&#45;&gt;If8 -->
+<g id="edge6" class="edge">
+<title>Region14&#45;&gt;If8:p0</title>
+<path fill="none" stroke="red" d="M193,-196.09C193,-205.55 193,-217.77 193,-229.69"/>
+<polygon fill="red" stroke="red" points="189.5,-230 193,-240 196.5,-230 189.5,-230"/>
+<text text-anchor="middle" x="190" y="-198.69" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Region14&#45;&gt;If8 -->
+<g id="edge7" class="edge">
+<title>Region14&#45;&gt;If8:p1</title>
+<path fill="none" stroke="red" d="M213.02,-196.12C221.64,-205.06 230.48,-216.84 233.71,-229.84"/>
+<polygon fill="red" stroke="red" points="230.27,-230.52 235,-240 237.22,-229.64 230.27,-230.52"/>
+<text text-anchor="middle" x="210.02" y="-198.72" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_a15&#45;&gt;Con_12 -->
+<g id="edge9" class="edge">
+<title>Phi_a15&#45;&gt;Con_12</title>
+<path fill="none" stroke="black" d="M102.13,-194.02C91.07,-206.92 74.96,-225.71 62.35,-240.42"/>
+<polygon fill="black" stroke="black" points="59.61,-238.24 55.76,-248.11 64.93,-242.8 59.61,-238.24"/>
+<text text-anchor="middle" x="99.13" y="-196.62" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Phi_a15&#45;&gt;Con_13 -->
+<g id="edge10" class="edge">
+<title>Phi_a15&#45;&gt;Con_13</title>
+<path fill="none" stroke="black" d="M115,-196.4C115,-207.74 115,-222.77 115,-235.68"/>
+<polygon fill="black" stroke="black" points="111.5,-235.9 115,-245.9 118.5,-235.9 111.5,-235.9"/>
+<text text-anchor="middle" x="112" y="-199" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_a15&#45;&gt;Region14 -->
+<g id="edge8" class="edge">
+<title>Phi_a15&#45;&gt;Region14</title>
+<path fill="none" stroke="black" stroke-dasharray="1,5" d="M142.12,-178C144.58,-178 147.04,-178 149.5,-178"/>
+<polygon fill="black" stroke="black" points="149.61,-181.5 159.61,-178 149.61,-174.5 149.61,-181.5"/>
+<text text-anchor="middle" x="145.12" y="-171.6" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+</g>
+</svg>
diff --git a/chapter06/docs/06-graph3-false.gv b/chapter06/docs/06-graph3-false.gv
new file mode 100644
index 00000000..28f741fa
--- /dev/null
+++ b/chapter06/docs/06-graph3-false.gv
@@ -0,0 +1,35 @@
+digraph chapter06 {
+/*
+int a = 0;
+int b = 1;
+if( arg ) {
+ a = 2;
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b;
+
+*/
+ rankdir=BT;
+ ordering="in";
+ concentrate="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_30 [ label="#1" ];
+ Return31 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return31[taillabel=0 color=red];
+ Return31 -> Start2:p0[taillabel=0 color=red];
+ Return31 -> Con_30[taillabel=1];
+}
diff --git a/chapter06/docs/06-graph3-false.svg b/chapter06/docs/06-graph3-false.svg
new file mode 100644
index 00000000..49b04f09
--- /dev/null
+++ b/chapter06/docs/06-graph3-false.svg
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.43.0 (0)
+ -->
+<!-- Title: chapter06 Pages: 1 -->
+<svg width="178pt" height="248pt"
+ viewBox="0.00 0.00 178.00 248.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 244)">
+<title>chapter06</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-244 174,-244 174,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-232 162,-232 162,-8 8,-8"/>
+</g>
+<!-- Start2 -->
+<g id="node1" class="node">
+<title>Start2</title>
+<polygon fill="yellow" stroke="transparent" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<polygon fill="none" stroke="black" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<text text-anchor="start" x="30.5" y="-203.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="24,-164 24,-195 74,-195 74,-164 24,-164"/>
+<polygon fill="yellow" stroke="transparent" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<polygon fill="none" stroke="black" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<text text-anchor="start" x="32" y="-175.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+</g>
+<!-- Stop3 -->
+<g id="node2" class="node">
+<title>Stop3</title>
+<polygon fill="yellow" stroke="black" points="112,-52 58,-52 58,-16 112,-16 112,-52"/>
+<text text-anchor="middle" x="85" y="-30.3" font-family="Times,serif" font-size="14.00">Stop</text>
+</g>
+<!-- Return31 -->
+<g id="node4" class="node">
+<title>Return31</title>
+<polygon fill="yellow" stroke="black" points="118,-124 52,-124 52,-88 118,-88 118,-124"/>
+<text text-anchor="middle" x="85" y="-102.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Stop3&#45;&gt;Return31 -->
+<g id="edge1" class="edge">
+<title>Stop3&#45;&gt;Return31</title>
+<path fill="none" stroke="red" d="M85,-52.3C85,-60.02 85,-69.29 85,-77.89"/>
+<polygon fill="red" stroke="red" points="81.5,-77.9 85,-87.9 88.5,-77.9 81.5,-77.9"/>
+<text text-anchor="middle" x="82" y="-54.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Con_30 -->
+<g id="node3" class="node">
+<title>Con_30</title>
+<ellipse fill="none" stroke="black" cx="127" cy="-192" rx="27" ry="18"/>
+<text text-anchor="middle" x="127" y="-188.3" font-family="Times,serif" font-size="14.00">#1</text>
+</g>
+<!-- Return31&#45;&gt;Start2 -->
+<g id="edge2" class="edge">
+<title>Return31&#45;&gt;Start2:p0</title>
+<path fill="none" stroke="red" d="M68.26,-124.13C60.77,-133.26 52.99,-145.23 50.13,-158.06"/>
+<polygon fill="red" stroke="red" points="46.66,-157.67 49,-168 53.61,-158.46 46.66,-157.67"/>
+<text text-anchor="middle" x="65.26" y="-126.73" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Return31&#45;&gt;Con_30 -->
+<g id="edge3" class="edge">
+<title>Return31&#45;&gt;Con_30</title>
+<path fill="none" stroke="black" d="M93.7,-124.4C99.66,-136.31 107.65,-152.3 114.31,-165.62"/>
+<polygon fill="black" stroke="black" points="111.28,-167.38 118.88,-174.76 117.54,-164.25 111.28,-167.38"/>
+<text text-anchor="middle" x="90.7" y="-127" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+</g>
+</svg>
diff --git a/chapter06/docs/06-graph3-nonconst.gv b/chapter06/docs/06-graph3-nonconst.gv
new file mode 100644
index 00000000..0746be85
--- /dev/null
+++ b/chapter06/docs/06-graph3-nonconst.gv
@@ -0,0 +1,86 @@
+digraph chapter06 {
+/*
+int a = 0;
+int b = 1;
+if( arg ) {
+ a = 2;
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b;
+
+*/
+ rankdir=BT;
+ ordering="in";
+ concentrate="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD><TD PORT="p1">arg</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_6 [ label="#0" ];
+ Con_7 [ label="#1" ];
+ If8 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Con_12 [ label="#2" ];
+ If13 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Con_17 [ label="#2" ];
+ Con_18 [ label="#3" ];
+ Region19 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_b20 [ style=filled fillcolor=lightyellow label="&phi;_b" ];
+ Region21 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_a22 [ style=filled fillcolor=lightyellow label="&phi;_a" ];
+ Phi_b23 [ style=filled fillcolor=lightyellow label="&phi;_b" ];
+ Add24 [ label="+" ];
+ Return25 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ { rank=same; Region19;Phi_b20;}
+ { rank=same; Region21;Phi_b23;Phi_a22;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return25[taillabel=0 color=red];
+ If8 -> Start2:p0[taillabel=0 color=red];
+ If8 -> Start2:p1[taillabel=1];
+ If13 -> If8:p0[taillabel=0 color=red];
+ If13 -> Start2:p1[taillabel=1];
+ Region19 -> If13:p0[taillabel=1 color=red];
+ Region19 -> If13:p1[taillabel=2 color=red];
+ Phi_b20 -> Region19 [style=dotted taillabel=0];
+ Phi_b20 -> Con_17[taillabel=1];
+ Phi_b20 -> Con_18[taillabel=2];
+ Region21 -> Region19[taillabel=1 color=red];
+ Region21 -> If8:p1[taillabel=2 color=red];
+ Phi_a22 -> Region21 [style=dotted taillabel=0];
+ Phi_a22 -> Con_12[taillabel=1];
+ Phi_a22 -> Con_6[taillabel=2];
+ Phi_b23 -> Region21 [style=dotted taillabel=0];
+ Phi_b23 -> Phi_b20[taillabel=1];
+ Phi_b23 -> Con_7[taillabel=2];
+ Add24 -> Phi_b23[taillabel=1];
+ Add24 -> Phi_a22[taillabel=2];
+ Return25 -> Region21[taillabel=0 color=red];
+ Return25 -> Add24[taillabel=1];
+} \ No newline at end of file
diff --git a/chapter06/docs/06-graph3-nonconst.svg b/chapter06/docs/06-graph3-nonconst.svg
new file mode 100644
index 00000000..dee852e4
--- /dev/null
+++ b/chapter06/docs/06-graph3-nonconst.svg
@@ -0,0 +1,290 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.43.0 (0)
+ -->
+<!-- Title: chapter06 Pages: 1 -->
+<svg width="414pt" height="664pt"
+ viewBox="0.00 0.00 414.00 664.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 660)">
+<title>chapter06</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-660 410,-660 410,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-648 398,-648 398,-8 8,-8"/>
+</g>
+<!-- Start2 -->
+<g id="node1" class="node">
+<title>Start2</title>
+<polygon fill="yellow" stroke="transparent" points="274,-611 274,-636 355,-636 355,-611 274,-611"/>
+<polygon fill="none" stroke="black" points="274,-611 274,-636 355,-636 355,-611 274,-611"/>
+<text text-anchor="start" x="296" y="-619.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="274,-580 274,-611 355,-611 355,-580 274,-580"/>
+<polygon fill="yellow" stroke="transparent" points="279,-585 279,-606 319,-606 319,-585 279,-585"/>
+<polygon fill="none" stroke="black" points="279,-585 279,-606 319,-606 319,-585 279,-585"/>
+<text text-anchor="start" x="282" y="-591.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+<polygon fill="none" stroke="black" points="319,-585 319,-606 350,-606 350,-585 319,-585"/>
+<text text-anchor="start" x="322" y="-591.8" font-family="Times,serif" font-size="14.00">arg</text>
+</g>
+<!-- Stop3 -->
+<g id="node2" class="node">
+<title>Stop3</title>
+<polygon fill="yellow" stroke="black" points="299,-52 245,-52 245,-16 299,-16 299,-52"/>
+<text text-anchor="middle" x="272" y="-30.3" font-family="Times,serif" font-size="14.00">Stop</text>
+</g>
+<!-- Return25 -->
+<g id="node16" class="node">
+<title>Return25</title>
+<polygon fill="yellow" stroke="black" points="305,-124 239,-124 239,-88 305,-88 305,-124"/>
+<text text-anchor="middle" x="272" y="-102.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Stop3&#45;&gt;Return25 -->
+<g id="edge1" class="edge">
+<title>Stop3&#45;&gt;Return25</title>
+<path fill="none" stroke="red" d="M272,-52.3C272,-60.02 272,-69.29 272,-77.89"/>
+<polygon fill="red" stroke="red" points="268.5,-77.9 272,-87.9 275.5,-77.9 268.5,-77.9"/>
+<text text-anchor="middle" x="269" y="-54.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Con_6 -->
+<g id="node3" class="node">
+<title>Con_6</title>
+<ellipse fill="none" stroke="black" cx="259" cy="-322" rx="27" ry="18"/>
+<text text-anchor="middle" x="259" y="-318.3" font-family="Times,serif" font-size="14.00">#0</text>
+</g>
+<!-- Con_7 -->
+<g id="node4" class="node">
+<title>Con_7</title>
+<ellipse fill="none" stroke="black" cx="115" cy="-322" rx="27" ry="18"/>
+<text text-anchor="middle" x="115" y="-318.3" font-family="Times,serif" font-size="14.00">#1</text>
+</g>
+<!-- If8 -->
+<g id="node5" class="node">
+<title>If8</title>
+<polygon fill="yellow" stroke="transparent" points="288,-511 288,-536 382,-536 382,-511 288,-511"/>
+<polygon fill="none" stroke="black" points="288,-511 288,-536 382,-536 382,-511 288,-511"/>
+<text text-anchor="start" x="329.5" y="-519.8" font-family="Times,serif" font-size="14.00">If</text>
+<polygon fill="none" stroke="black" points="288,-480 288,-511 382,-511 382,-480 288,-480"/>
+<polygon fill="yellow" stroke="transparent" points="293,-485 293,-506 333,-506 333,-485 293,-485"/>
+<polygon fill="none" stroke="black" points="293,-485 293,-506 333,-506 333,-485 293,-485"/>
+<text text-anchor="start" x="296" y="-491.8" font-family="Times,serif" font-size="14.00">True</text>
+<polygon fill="yellow" stroke="transparent" points="333,-485 333,-506 377,-506 377,-485 333,-485"/>
+<polygon fill="none" stroke="black" points="333,-485 333,-506 377,-506 377,-485 333,-485"/>
+<text text-anchor="start" x="336" y="-491.8" font-family="Times,serif" font-size="14.00">False</text>
+</g>
+<!-- If8&#45;&gt;Start2 -->
+<g id="edge2" class="edge">
+<title>If8&#45;&gt;Start2:p0</title>
+<path fill="none" stroke="red" d="M312.41,-540.3C306.75,-550.38 301.75,-562.04 299.83,-573.96"/>
+<polygon fill="red" stroke="red" points="296.34,-573.75 299,-584 303.31,-574.32 296.34,-573.75"/>
+<text text-anchor="middle" x="309.41" y="-542.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- If8&#45;&gt;Start2 -->
+<g id="edge3" class="edge">
+<title>If8&#45;&gt;Start2:p1</title>
+<path fill="none" stroke="black" d="M335,-540.02C335,-550.47 335,-562.42 335,-573.98"/>
+<polygon fill="black" stroke="black" points="331.5,-574 335,-584 338.5,-574 331.5,-574"/>
+<text text-anchor="middle" x="332" y="-542.62" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Con_12 -->
+<g id="node6" class="node">
+<title>Con_12</title>
+<ellipse fill="none" stroke="black" cx="187" cy="-322" rx="27" ry="18"/>
+<text text-anchor="middle" x="187" y="-318.3" font-family="Times,serif" font-size="14.00">#2</text>
+</g>
+<!-- If13 -->
+<g id="node7" class="node">
+<title>If13</title>
+<polygon fill="yellow" stroke="transparent" points="267,-411 267,-436 361,-436 361,-411 267,-411"/>
+<polygon fill="none" stroke="black" points="267,-411 267,-436 361,-436 361,-411 267,-411"/>
+<text text-anchor="start" x="308.5" y="-419.8" font-family="Times,serif" font-size="14.00">If</text>
+<polygon fill="none" stroke="black" points="267,-380 267,-411 361,-411 361,-380 267,-380"/>
+<polygon fill="yellow" stroke="transparent" points="272,-385 272,-406 312,-406 312,-385 272,-385"/>
+<polygon fill="none" stroke="black" points="272,-385 272,-406 312,-406 312,-385 272,-385"/>
+<text text-anchor="start" x="275" y="-391.8" font-family="Times,serif" font-size="14.00">True</text>
+<polygon fill="yellow" stroke="transparent" points="312,-385 312,-406 356,-406 356,-385 312,-385"/>
+<polygon fill="none" stroke="black" points="312,-385 312,-406 356,-406 356,-385 312,-385"/>
+<text text-anchor="start" x="315" y="-391.8" font-family="Times,serif" font-size="14.00">False</text>
+</g>
+<!-- If13&#45;&gt;Start2 -->
+<g id="edge5" class="edge">
+<title>If13&#45;&gt;Start2:p1</title>
+<path fill="none" stroke="black" d="M288.75,-440.22C281.57,-450.93 274.7,-463.4 271,-476 262.99,-503.29 257.76,-514.83 271,-540 285.19,-566.97 324.2,-553.71 333.15,-573.98"/>
+<polygon fill="black" stroke="black" points="329.75,-574.8 335,-584 336.63,-573.53 329.75,-574.8"/>
+<text text-anchor="middle" x="285.75" y="-442.82" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- If13&#45;&gt;If8 -->
+<g id="edge4" class="edge">
+<title>If13&#45;&gt;If8:p0</title>
+<path fill="none" stroke="red" d="M313.4,-440.02C313.24,-450.46 313.09,-462.42 313.03,-473.98"/>
+<polygon fill="red" stroke="red" points="309.53,-473.99 313,-484 316.53,-474.01 309.53,-473.99"/>
+<text text-anchor="middle" x="310.4" y="-442.62" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Con_17 -->
+<g id="node8" class="node">
+<title>Con_17</title>
+<ellipse fill="none" stroke="black" cx="43" cy="-408" rx="27" ry="18"/>
+<text text-anchor="middle" x="43" y="-404.3" font-family="Times,serif" font-size="14.00">#2</text>
+</g>
+<!-- Con_18 -->
+<g id="node9" class="node">
+<title>Con_18</title>
+<ellipse fill="none" stroke="black" cx="115" cy="-408" rx="27" ry="18"/>
+<text text-anchor="middle" x="115" y="-404.3" font-family="Times,serif" font-size="14.00">#3</text>
+</g>
+<!-- Region19 -->
+<g id="node10" class="node">
+<title>Region19</title>
+<polygon fill="yellow" stroke="black" points="370,-340 304,-340 304,-304 370,-304 370,-340"/>
+<text text-anchor="middle" x="337" y="-318.3" font-family="Times,serif" font-size="14.00">Region</text>
+</g>
+<!-- Region19&#45;&gt;If13 -->
+<g id="edge6" class="edge">
+<title>Region19&#45;&gt;If13:p0</title>
+<path fill="none" stroke="red" d="M315.29,-340.1C306.12,-348.94 296.78,-360.62 293.36,-373.72"/>
+<polygon fill="red" stroke="red" points="289.84,-373.63 292,-384 296.78,-374.54 289.84,-373.63"/>
+<text text-anchor="middle" x="312.29" y="-342.7" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Region19&#45;&gt;If13 -->
+<g id="edge7" class="edge">
+<title>Region19&#45;&gt;If13:p1</title>
+<path fill="none" stroke="red" d="M335.71,-340.08C335.08,-349.53 334.4,-361.76 334.13,-373.68"/>
+<polygon fill="red" stroke="red" points="330.62,-373.96 334,-384 337.62,-374.04 330.62,-373.96"/>
+<text text-anchor="middle" x="332.71" y="-342.68" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_b20 -->
+<g id="node11" class="node">
+<title>Phi_b20</title>
+<ellipse fill="lightyellow" stroke="black" cx="43" cy="-322" rx="27.1" ry="18"/>
+<text text-anchor="middle" x="43" y="-318.3" font-family="Times,serif" font-size="14.00">φ_b</text>
+</g>
+<!-- Phi_b20&#45;&gt;Con_17 -->
+<g id="edge9" class="edge">
+<title>Phi_b20&#45;&gt;Con_17</title>
+<path fill="none" stroke="black" d="M43,-340.4C43,-351.74 43,-366.77 43,-379.68"/>
+<polygon fill="black" stroke="black" points="39.5,-379.9 43,-389.9 46.5,-379.9 39.5,-379.9"/>
+<text text-anchor="middle" x="40" y="-343" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Phi_b20&#45;&gt;Con_18 -->
+<g id="edge10" class="edge">
+<title>Phi_b20&#45;&gt;Con_18</title>
+<path fill="none" stroke="black" d="M55.87,-338.02C66.93,-350.92 83.04,-369.71 95.65,-384.42"/>
+<polygon fill="black" stroke="black" points="93.07,-386.8 102.24,-392.11 98.39,-382.24 93.07,-386.8"/>
+<text text-anchor="middle" x="52.87" y="-340.62" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_b20&#45;&gt;Region19 -->
+<g id="edge8" class="edge">
+<title>Phi_b20&#45;&gt;Region19</title>
+<path fill="none" stroke="black" stroke-dasharray="1,5" d="M57.22,-306.43C65.37,-298.68 76.29,-290.11 88,-286 129.52,-271.44 244.06,-272.69 286,-286 293.99,-288.54 301.81,-292.83 308.83,-297.58"/>
+<polygon fill="black" stroke="black" points="307.11,-300.66 317.24,-303.75 311.25,-295.01 307.11,-300.66"/>
+<text text-anchor="middle" x="54.22" y="-300.03" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Region21 -->
+<g id="node12" class="node">
+<title>Region21</title>
+<polygon fill="yellow" stroke="black" points="352,-268 286,-268 286,-232 352,-232 352,-268"/>
+<text text-anchor="middle" x="319" y="-246.3" font-family="Times,serif" font-size="14.00">Region</text>
+</g>
+<!-- Region21&#45;&gt;If8 -->
+<g id="edge12" class="edge">
+<title>Region21&#45;&gt;If8:p1</title>
+<path fill="none" stroke="red" d="M347.26,-268.03C359.43,-277.04 372.37,-289.3 379,-304 385.58,-318.6 394.79,-373.07 378,-440 373.59,-457.56 360.94,-461.07 356.53,-473.94"/>
+<polygon fill="red" stroke="red" points="353.05,-473.59 355,-484 359.97,-474.64 353.05,-473.59"/>
+<text text-anchor="middle" x="344.26" y="-270.63" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Region21&#45;&gt;Region19 -->
+<g id="edge11" class="edge">
+<title>Region21&#45;&gt;Region19</title>
+<path fill="none" stroke="red" d="M323.45,-268.3C325.45,-276.1 327.87,-285.49 330.1,-294.17"/>
+<polygon fill="red" stroke="red" points="326.72,-295.08 332.6,-303.9 333.5,-293.34 326.72,-295.08"/>
+<text text-anchor="middle" x="320.45" y="-270.9" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Phi_a22 -->
+<g id="node13" class="node">
+<title>Phi_a22</title>
+<ellipse fill="lightyellow" stroke="black" cx="241" cy="-250" rx="27" ry="18"/>
+<text text-anchor="middle" x="241" y="-246.3" font-family="Times,serif" font-size="14.00">φ_a</text>
+</g>
+<!-- Phi_a22&#45;&gt;Con_6 -->
+<g id="edge15" class="edge">
+<title>Phi_a22&#45;&gt;Con_6</title>
+<path fill="none" stroke="black" d="M245.36,-267.95C247.39,-275.86 249.86,-285.46 252.14,-294.31"/>
+<polygon fill="black" stroke="black" points="248.8,-295.4 254.68,-304.21 255.58,-293.65 248.8,-295.4"/>
+<text text-anchor="middle" x="242.36" y="-270.55" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_a22&#45;&gt;Con_12 -->
+<g id="edge14" class="edge">
+<title>Phi_a22&#45;&gt;Con_12</title>
+<path fill="none" stroke="black" d="M229.02,-266.54C222.02,-275.6 213.06,-287.21 205.21,-297.39"/>
+<polygon fill="black" stroke="black" points="202.29,-295.45 198.95,-305.51 207.83,-299.73 202.29,-295.45"/>
+<text text-anchor="middle" x="226.02" y="-269.14" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Phi_a22&#45;&gt;Region21 -->
+<g id="edge13" class="edge">
+<title>Phi_a22&#45;&gt;Region21</title>
+<path fill="none" stroke="black" stroke-dasharray="1,5" d="M268.12,-250C270.58,-250 273.04,-250 275.5,-250"/>
+<polygon fill="black" stroke="black" points="275.61,-253.5 285.61,-250 275.61,-246.5 275.61,-253.5"/>
+<text text-anchor="middle" x="271.12" y="-243.6" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Phi_b23 -->
+<g id="node14" class="node">
+<title>Phi_b23</title>
+<ellipse fill="lightyellow" stroke="black" cx="133" cy="-250" rx="27.1" ry="18"/>
+<text text-anchor="middle" x="133" y="-246.3" font-family="Times,serif" font-size="14.00">φ_b</text>
+</g>
+<!-- Phi_b23&#45;&gt;Con_7 -->
+<g id="edge18" class="edge">
+<title>Phi_b23&#45;&gt;Con_7</title>
+<path fill="none" stroke="black" d="M128.64,-267.95C126.61,-275.86 124.14,-285.46 121.86,-294.31"/>
+<polygon fill="black" stroke="black" points="118.42,-293.65 119.32,-304.21 125.2,-295.4 118.42,-293.65"/>
+<text text-anchor="middle" x="125.64" y="-270.55" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_b23&#45;&gt;Phi_b20 -->
+<g id="edge17" class="edge">
+<title>Phi_b23&#45;&gt;Phi_b20</title>
+<path fill="none" stroke="black" d="M116.07,-264.17C102.58,-274.66 83.49,-289.51 68.2,-301.4"/>
+<polygon fill="black" stroke="black" points="65.9,-298.75 60.16,-307.65 70.2,-304.28 65.9,-298.75"/>
+<text text-anchor="middle" x="113.07" y="-266.77" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Phi_b23&#45;&gt;Region21 -->
+<g id="edge16" class="edge">
+<title>Phi_b23&#45;&gt;Region21</title>
+<path fill="none" stroke="black" stroke-dasharray="1,5" d="M152.84,-237.61C179.31,-222.7 227.54,-201.16 268,-214 275.99,-216.54 283.81,-220.83 290.83,-225.58"/>
+<polygon fill="black" stroke="black" points="289.11,-228.66 299.24,-231.75 293.25,-223.01 289.11,-228.66"/>
+<text text-anchor="middle" x="149.84" y="-231.21" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Add24 -->
+<g id="node15" class="node">
+<title>Add24</title>
+<ellipse fill="none" stroke="black" cx="241" cy="-178" rx="27" ry="18"/>
+<text text-anchor="middle" x="241" y="-174.3" font-family="Times,serif" font-size="14.00">+</text>
+</g>
+<!-- Add24&#45;&gt;Phi_a22 -->
+<g id="edge20" class="edge">
+<title>Add24&#45;&gt;Phi_a22</title>
+<path fill="none" stroke="black" d="M241,-196.3C241,-204.02 241,-213.29 241,-221.89"/>
+<polygon fill="black" stroke="black" points="237.5,-221.9 241,-231.9 244.5,-221.9 237.5,-221.9"/>
+<text text-anchor="middle" x="238" y="-198.9" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Add24&#45;&gt;Phi_b23 -->
+<g id="edge19" class="edge">
+<title>Add24&#45;&gt;Phi_b23</title>
+<path fill="none" stroke="black" d="M222.19,-191.19C205.1,-202.27 179.69,-218.74 160.36,-231.27"/>
+<polygon fill="black" stroke="black" points="158.45,-228.34 151.96,-236.71 162.25,-234.21 158.45,-228.34"/>
+<text text-anchor="middle" x="219.19" y="-193.79" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Return25&#45;&gt;Region21 -->
+<g id="edge21" class="edge">
+<title>Return25&#45;&gt;Region21</title>
+<path fill="none" stroke="red" d="M277.67,-124.13C285.71,-148.44 300.52,-193.18 310.06,-221.99"/>
+<polygon fill="red" stroke="red" points="306.85,-223.42 313.31,-231.81 313.49,-221.22 306.85,-223.42"/>
+<text text-anchor="middle" x="274.67" y="-126.73" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Return25&#45;&gt;Add24 -->
+<g id="edge22" class="edge">
+<title>Return25&#45;&gt;Add24</title>
+<path fill="none" stroke="black" d="M264.34,-124.3C260.76,-132.39 256.42,-142.19 252.46,-151.13"/>
+<polygon fill="black" stroke="black" points="249.21,-149.82 248.36,-160.38 255.61,-152.65 249.21,-149.82"/>
+<text text-anchor="middle" x="261.34" y="-126.9" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+</g>
+</svg>
diff --git a/chapter06/docs/06-graph3-true.gv b/chapter06/docs/06-graph3-true.gv
new file mode 100644
index 00000000..8fdcc5df
--- /dev/null
+++ b/chapter06/docs/06-graph3-true.gv
@@ -0,0 +1,35 @@
+digraph chapter06 {
+/*
+int a = 0;
+int b = 1;
+if( arg ) {
+ a = 2;
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b;
+
+*/
+ rankdir=BT;
+ ordering="in";
+ concentrate="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_28 [ label="#4" ];
+ Return29 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return29[taillabel=0 color=red];
+ Return29 -> Start2:p0[taillabel=0 color=red];
+ Return29 -> Con_28[taillabel=1];
+} \ No newline at end of file
diff --git a/chapter06/docs/06-graph3-true.svg b/chapter06/docs/06-graph3-true.svg
new file mode 100644
index 00000000..7e573cc6
--- /dev/null
+++ b/chapter06/docs/06-graph3-true.svg
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.43.0 (0)
+ -->
+<!-- Title: chapter06 Pages: 1 -->
+<svg width="178pt" height="248pt"
+ viewBox="0.00 0.00 178.00 248.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 244)">
+<title>chapter06</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-244 174,-244 174,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-232 162,-232 162,-8 8,-8"/>
+</g>
+<!-- Start2 -->
+<g id="node1" class="node">
+<title>Start2</title>
+<polygon fill="yellow" stroke="transparent" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<polygon fill="none" stroke="black" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<text text-anchor="start" x="30.5" y="-203.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="24,-164 24,-195 74,-195 74,-164 24,-164"/>
+<polygon fill="yellow" stroke="transparent" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<polygon fill="none" stroke="black" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<text text-anchor="start" x="32" y="-175.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+</g>
+<!-- Stop3 -->
+<g id="node2" class="node">
+<title>Stop3</title>
+<polygon fill="yellow" stroke="black" points="112,-52 58,-52 58,-16 112,-16 112,-52"/>
+<text text-anchor="middle" x="85" y="-30.3" font-family="Times,serif" font-size="14.00">Stop</text>
+</g>
+<!-- Return29 -->
+<g id="node4" class="node">
+<title>Return29</title>
+<polygon fill="yellow" stroke="black" points="118,-124 52,-124 52,-88 118,-88 118,-124"/>
+<text text-anchor="middle" x="85" y="-102.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Stop3&#45;&gt;Return29 -->
+<g id="edge1" class="edge">
+<title>Stop3&#45;&gt;Return29</title>
+<path fill="none" stroke="red" d="M85,-52.3C85,-60.02 85,-69.29 85,-77.89"/>
+<polygon fill="red" stroke="red" points="81.5,-77.9 85,-87.9 88.5,-77.9 81.5,-77.9"/>
+<text text-anchor="middle" x="82" y="-54.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Con_28 -->
+<g id="node3" class="node">
+<title>Con_28</title>
+<ellipse fill="none" stroke="black" cx="127" cy="-192" rx="27" ry="18"/>
+<text text-anchor="middle" x="127" y="-188.3" font-family="Times,serif" font-size="14.00">#4</text>
+</g>
+<!-- Return29&#45;&gt;Start2 -->
+<g id="edge2" class="edge">
+<title>Return29&#45;&gt;Start2:p0</title>
+<path fill="none" stroke="red" d="M68.26,-124.13C60.77,-133.26 52.99,-145.23 50.13,-158.06"/>
+<polygon fill="red" stroke="red" points="46.66,-157.67 49,-168 53.61,-158.46 46.66,-157.67"/>
+<text text-anchor="middle" x="65.26" y="-126.73" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Return29&#45;&gt;Con_28 -->
+<g id="edge3" class="edge">
+<title>Return29&#45;&gt;Con_28</title>
+<path fill="none" stroke="black" d="M93.7,-124.4C99.66,-136.31 107.65,-152.3 114.31,-165.62"/>
+<polygon fill="black" stroke="black" points="111.28,-167.38 118.88,-174.76 117.54,-164.25 111.28,-167.38"/>
+<text text-anchor="middle" x="90.7" y="-127" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+</g>
+</svg>
diff --git a/chapter06/docs/06-graph4-arg2.gv b/chapter06/docs/06-graph4-arg2.gv
new file mode 100644
index 00000000..7d6913e2
--- /dev/null
+++ b/chapter06/docs/06-graph4-arg2.gv
@@ -0,0 +1,37 @@
+digraph chapter06 {
+/*
+int a = 0;
+int b = 1;
+int c = 0;
+if( arg ) {
+ a = 1;
+ if( arg==2 ) { c=2; } else { c=3; }
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b+c;
+
+*/
+ rankdir=BT;
+ ordering="in";
+ concentrate="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_44 [ label="#5" ];
+ Return45 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return45[taillabel=0 color=red];
+ Return45 -> Start2:p0[taillabel=0 color=red];
+ Return45 -> Con_44[taillabel=1];
+}
diff --git a/chapter06/docs/06-graph4-arg2.svg b/chapter06/docs/06-graph4-arg2.svg
new file mode 100644
index 00000000..8ec80be4
--- /dev/null
+++ b/chapter06/docs/06-graph4-arg2.svg
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.43.0 (0)
+ -->
+<!-- Title: chapter06 Pages: 1 -->
+<svg width="178pt" height="248pt"
+ viewBox="0.00 0.00 178.00 248.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 244)">
+<title>chapter06</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-244 174,-244 174,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-232 162,-232 162,-8 8,-8"/>
+</g>
+<!-- Start2 -->
+<g id="node1" class="node">
+<title>Start2</title>
+<polygon fill="yellow" stroke="transparent" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<polygon fill="none" stroke="black" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<text text-anchor="start" x="30.5" y="-203.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="24,-164 24,-195 74,-195 74,-164 24,-164"/>
+<polygon fill="yellow" stroke="transparent" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<polygon fill="none" stroke="black" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<text text-anchor="start" x="32" y="-175.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+</g>
+<!-- Stop3 -->
+<g id="node2" class="node">
+<title>Stop3</title>
+<polygon fill="yellow" stroke="black" points="112,-52 58,-52 58,-16 112,-16 112,-52"/>
+<text text-anchor="middle" x="85" y="-30.3" font-family="Times,serif" font-size="14.00">Stop</text>
+</g>
+<!-- Return45 -->
+<g id="node4" class="node">
+<title>Return45</title>
+<polygon fill="yellow" stroke="black" points="118,-124 52,-124 52,-88 118,-88 118,-124"/>
+<text text-anchor="middle" x="85" y="-102.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Stop3&#45;&gt;Return45 -->
+<g id="edge1" class="edge">
+<title>Stop3&#45;&gt;Return45</title>
+<path fill="none" stroke="red" d="M85,-52.3C85,-60.02 85,-69.29 85,-77.89"/>
+<polygon fill="red" stroke="red" points="81.5,-77.9 85,-87.9 88.5,-77.9 81.5,-77.9"/>
+<text text-anchor="middle" x="82" y="-54.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Con_44 -->
+<g id="node3" class="node">
+<title>Con_44</title>
+<ellipse fill="none" stroke="black" cx="127" cy="-192" rx="27" ry="18"/>
+<text text-anchor="middle" x="127" y="-188.3" font-family="Times,serif" font-size="14.00">#5</text>
+</g>
+<!-- Return45&#45;&gt;Start2 -->
+<g id="edge2" class="edge">
+<title>Return45&#45;&gt;Start2:p0</title>
+<path fill="none" stroke="red" d="M68.26,-124.13C60.77,-133.26 52.99,-145.23 50.13,-158.06"/>
+<polygon fill="red" stroke="red" points="46.66,-157.67 49,-168 53.61,-158.46 46.66,-157.67"/>
+<text text-anchor="middle" x="65.26" y="-126.73" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Return45&#45;&gt;Con_44 -->
+<g id="edge3" class="edge">
+<title>Return45&#45;&gt;Con_44</title>
+<path fill="none" stroke="black" d="M93.7,-124.4C99.66,-136.31 107.65,-152.3 114.31,-165.62"/>
+<polygon fill="black" stroke="black" points="111.28,-167.38 118.88,-174.76 117.54,-164.25 111.28,-167.38"/>
+<text text-anchor="middle" x="90.7" y="-127" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+</g>
+</svg>
diff --git a/chapter06/docs/06-graph4-nonconst.gv b/chapter06/docs/06-graph4-nonconst.gv
new file mode 100644
index 00000000..b8899b48
--- /dev/null
+++ b/chapter06/docs/06-graph4-nonconst.gv
@@ -0,0 +1,122 @@
+digraph chapter06 {
+/*
+int a = 0;
+int b = 1;
+int c = 0;
+if( arg ) {
+ a = 1;
+ if( arg==2 ) { c=2; } else { c=3; }
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b+c;
+
+*/
+ rankdir=BT;
+ ordering="in";
+ concentrate="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD><TD PORT="p1">arg</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_6 [ label="#0" ];
+ Con_7 [ label="#1" ];
+ Con_8 [ label="#0" ];
+ If9 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Con_13 [ label="#1" ];
+ Con_14 [ label="#2" ];
+ EQNode15 [ label="==" ];
+ If16 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Con_20 [ label="#2" ];
+ Con_21 [ label="#3" ];
+ Region22 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_c23 [ style=filled fillcolor=lightyellow label="&phi;_c" ];
+ If24 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Con_28 [ label="#2" ];
+ Con_29 [ label="#3" ];
+ Region30 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_b31 [ style=filled fillcolor=lightyellow label="&phi;_b" ];
+ Region32 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_a33 [ style=filled fillcolor=lightyellow label="&phi;_a" ];
+ Phi_b34 [ style=filled fillcolor=lightyellow label="&phi;_b" ];
+ Phi_c35 [ style=filled fillcolor=lightyellow label="&phi;_c" ];
+ Add36 [ label="+" ];
+ Add37 [ label="+" ];
+ Return38 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ { rank=same; Region22;Phi_c23;}
+ { rank=same; Region30;Phi_b31;}
+ { rank=same; Region32;Phi_c35;Phi_a33;Phi_b34;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return38[taillabel=0 color=red];
+ If9 -> Start2:p0[taillabel=0 color=red];
+ If9 -> Start2:p1[taillabel=1];
+ EQNode15 -> Start2:p1[taillabel=1];
+ EQNode15 -> Con_14[taillabel=2];
+ If16 -> If9:p0[taillabel=0 color=red];
+ If16 -> EQNode15[taillabel=1];
+ Region22 -> If16:p0[taillabel=1 color=red];
+ Region22 -> If16:p1[taillabel=2 color=red];
+ Phi_c23 -> Region22 [style=dotted taillabel=0];
+ Phi_c23 -> Con_20[taillabel=1];
+ Phi_c23 -> Con_21[taillabel=2];
+ If24 -> Region22[taillabel=0 color=red];
+ If24 -> Start2:p1[taillabel=1];
+ Region30 -> If24:p0[taillabel=1 color=red];
+ Region30 -> If24:p1[taillabel=2 color=red];
+ Phi_b31 -> Region30 [style=dotted taillabel=0];
+ Phi_b31 -> Con_28[taillabel=1];
+ Phi_b31 -> Con_29[taillabel=2];
+ Region32 -> Region30[taillabel=1 color=red];
+ Region32 -> If9:p1[taillabel=2 color=red];
+ Phi_a33 -> Region32 [style=dotted taillabel=0];
+ Phi_a33 -> Con_13[taillabel=1];
+ Phi_a33 -> Con_6[taillabel=2];
+ Phi_b34 -> Region32 [style=dotted taillabel=0];
+ Phi_b34 -> Phi_b31[taillabel=1];
+ Phi_b34 -> Con_7[taillabel=2];
+ Phi_c35 -> Region32 [style=dotted taillabel=0];
+ Phi_c35 -> Phi_c23[taillabel=1];
+ Phi_c35 -> Con_8[taillabel=2];
+ Add36 -> Phi_c35[taillabel=1];
+ Add36 -> Phi_b34[taillabel=2];
+ Add37 -> Add36[taillabel=1];
+ Add37 -> Phi_a33[taillabel=2];
+ Return38 -> Region32[taillabel=0 color=red];
+ Return38 -> Add37[taillabel=1];
+}
diff --git a/chapter06/docs/06-graph4-nonconst.svg b/chapter06/docs/06-graph4-nonconst.svg
new file mode 100644
index 00000000..d38c3636
--- /dev/null
+++ b/chapter06/docs/06-graph4-nonconst.svg
@@ -0,0 +1,456 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.43.0 (0)
+ -->
+<!-- Title: chapter06 Pages: 1 -->
+<svg width="506pt" height="908pt"
+ viewBox="0.00 0.00 506.00 908.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 904)">
+<title>chapter06</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-904 502,-904 502,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-892 490,-892 490,-8 8,-8"/>
+</g>
+<!-- Start2 -->
+<g id="node1" class="node">
+<title>Start2</title>
+<polygon fill="yellow" stroke="transparent" points="266,-855 266,-880 347,-880 347,-855 266,-855"/>
+<polygon fill="none" stroke="black" points="266,-855 266,-880 347,-880 347,-855 266,-855"/>
+<text text-anchor="start" x="288" y="-863.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="266,-824 266,-855 347,-855 347,-824 266,-824"/>
+<polygon fill="yellow" stroke="transparent" points="271,-829 271,-850 311,-850 311,-829 271,-829"/>
+<polygon fill="none" stroke="black" points="271,-829 271,-850 311,-850 311,-829 271,-829"/>
+<text text-anchor="start" x="274" y="-835.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+<polygon fill="none" stroke="black" points="311,-829 311,-850 342,-850 342,-829 311,-829"/>
+<text text-anchor="start" x="314" y="-835.8" font-family="Times,serif" font-size="14.00">arg</text>
+</g>
+<!-- Stop3 -->
+<g id="node2" class="node">
+<title>Stop3</title>
+<polygon fill="yellow" stroke="black" points="329,-52 275,-52 275,-16 329,-16 329,-52"/>
+<text text-anchor="middle" x="302" y="-30.3" font-family="Times,serif" font-size="14.00">Stop</text>
+</g>
+<!-- Return38 -->
+<g id="node26" class="node">
+<title>Return38</title>
+<polygon fill="yellow" stroke="black" points="335,-124 269,-124 269,-88 335,-88 335,-124"/>
+<text text-anchor="middle" x="302" y="-102.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Stop3&#45;&gt;Return38 -->
+<g id="edge1" class="edge">
+<title>Stop3&#45;&gt;Return38</title>
+<path fill="none" stroke="red" d="M302,-52.3C302,-60.02 302,-69.29 302,-77.89"/>
+<polygon fill="red" stroke="red" points="298.5,-77.9 302,-87.9 305.5,-77.9 298.5,-77.9"/>
+<text text-anchor="middle" x="299" y="-54.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Con_6 -->
+<g id="node3" class="node">
+<title>Con_6</title>
+<ellipse fill="none" stroke="black" cx="351" cy="-394" rx="27" ry="18"/>
+<text text-anchor="middle" x="351" y="-390.3" font-family="Times,serif" font-size="14.00">#0</text>
+</g>
+<!-- Con_7 -->
+<g id="node4" class="node">
+<title>Con_7</title>
+<ellipse fill="none" stroke="black" cx="207" cy="-394" rx="27" ry="18"/>
+<text text-anchor="middle" x="207" y="-390.3" font-family="Times,serif" font-size="14.00">#1</text>
+</g>
+<!-- Con_8 -->
+<g id="node5" class="node">
+<title>Con_8</title>
+<ellipse fill="none" stroke="black" cx="43" cy="-394" rx="27" ry="18"/>
+<text text-anchor="middle" x="43" y="-390.3" font-family="Times,serif" font-size="14.00">#0</text>
+</g>
+<!-- If9 -->
+<g id="node6" class="node">
+<title>If9</title>
+<polygon fill="yellow" stroke="transparent" points="232,-755 232,-780 326,-780 326,-755 232,-755"/>
+<polygon fill="none" stroke="black" points="232,-755 232,-780 326,-780 326,-755 232,-755"/>
+<text text-anchor="start" x="273.5" y="-763.8" font-family="Times,serif" font-size="14.00">If</text>
+<polygon fill="none" stroke="black" points="232,-724 232,-755 326,-755 326,-724 232,-724"/>
+<polygon fill="yellow" stroke="transparent" points="237,-729 237,-750 277,-750 277,-729 237,-729"/>
+<polygon fill="none" stroke="black" points="237,-729 237,-750 277,-750 277,-729 237,-729"/>
+<text text-anchor="start" x="240" y="-735.8" font-family="Times,serif" font-size="14.00">True</text>
+<polygon fill="yellow" stroke="transparent" points="277,-729 277,-750 321,-750 321,-729 277,-729"/>
+<polygon fill="none" stroke="black" points="277,-729 277,-750 321,-750 321,-729 277,-729"/>
+<text text-anchor="start" x="280" y="-735.8" font-family="Times,serif" font-size="14.00">False</text>
+</g>
+<!-- If9&#45;&gt;Start2 -->
+<g id="edge2" class="edge">
+<title>If9&#45;&gt;Start2:p0</title>
+<path fill="none" stroke="red" d="M286.26,-784.2C288.21,-794.57 289.98,-806.42 290.68,-817.97"/>
+<polygon fill="red" stroke="red" points="287.18,-818.12 291,-828 294.18,-817.89 287.18,-818.12"/>
+<text text-anchor="middle" x="283.26" y="-786.8" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- If9&#45;&gt;Start2 -->
+<g id="edge3" class="edge">
+<title>If9&#45;&gt;Start2:p1</title>
+<path fill="none" stroke="black" d="M309.94,-784.28C317.27,-794.18 323.65,-805.76 326.02,-818.03"/>
+<polygon fill="black" stroke="black" points="322.54,-818.39 327,-828 329.51,-817.71 322.54,-818.39"/>
+<text text-anchor="middle" x="306.94" y="-786.88" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Con_13 -->
+<g id="node7" class="node">
+<title>Con_13</title>
+<ellipse fill="none" stroke="black" cx="279" cy="-394" rx="27" ry="18"/>
+<text text-anchor="middle" x="279" y="-390.3" font-family="Times,serif" font-size="14.00">#1</text>
+</g>
+<!-- Con_14 -->
+<g id="node8" class="node">
+<title>Con_14</title>
+<ellipse fill="none" stroke="black" cx="413" cy="-852" rx="27" ry="18"/>
+<text text-anchor="middle" x="413" y="-848.3" font-family="Times,serif" font-size="14.00">#2</text>
+</g>
+<!-- EQNode15 -->
+<g id="node9" class="node">
+<title>EQNode15</title>
+<ellipse fill="none" stroke="black" cx="413" cy="-752" rx="27" ry="18"/>
+<text text-anchor="middle" x="413" y="-748.3" font-family="Times,serif" font-size="14.00">==</text>
+</g>
+<!-- EQNode15&#45;&gt;Start2 -->
+<g id="edge4" class="edge">
+<title>EQNode15&#45;&gt;Start2:p1</title>
+<path fill="none" stroke="black" d="M403.94,-769.18C391.81,-790 369.82,-824.8 352.53,-835.66"/>
+<polygon fill="black" stroke="black" points="351.28,-832.39 343,-839 353.59,-838.99 351.28,-832.39"/>
+<text text-anchor="middle" x="400.94" y="-771.78" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- EQNode15&#45;&gt;Con_14 -->
+<g id="edge5" class="edge">
+<title>EQNode15&#45;&gt;Con_14</title>
+<path fill="none" stroke="black" d="M413,-770.07C413,-784.88 413,-806.57 413,-823.77"/>
+<polygon fill="black" stroke="black" points="409.5,-823.8 413,-833.8 416.5,-823.8 409.5,-823.8"/>
+<text text-anchor="middle" x="410" y="-772.67" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- If16 -->
+<g id="node10" class="node">
+<title>If16</title>
+<polygon fill="yellow" stroke="transparent" points="380,-655 380,-680 474,-680 474,-655 380,-655"/>
+<polygon fill="none" stroke="black" points="380,-655 380,-680 474,-680 474,-655 380,-655"/>
+<text text-anchor="start" x="421.5" y="-663.8" font-family="Times,serif" font-size="14.00">If</text>
+<polygon fill="none" stroke="black" points="380,-624 380,-655 474,-655 474,-624 380,-624"/>
+<polygon fill="yellow" stroke="transparent" points="385,-629 385,-650 425,-650 425,-629 385,-629"/>
+<polygon fill="none" stroke="black" points="385,-629 385,-650 425,-650 425,-629 385,-629"/>
+<text text-anchor="start" x="388" y="-635.8" font-family="Times,serif" font-size="14.00">True</text>
+<polygon fill="yellow" stroke="transparent" points="425,-629 425,-650 469,-650 469,-629 425,-629"/>
+<polygon fill="none" stroke="black" points="425,-629 425,-650 469,-650 469,-629 425,-629"/>
+<text text-anchor="start" x="428" y="-635.8" font-family="Times,serif" font-size="14.00">False</text>
+</g>
+<!-- If16&#45;&gt;If9 -->
+<g id="edge6" class="edge">
+<title>If16&#45;&gt;If9:p0</title>
+<path fill="none" stroke="red" d="M371.81,-671.79C331.47,-686.19 280.89,-705.86 263.34,-719.58"/>
+<polygon fill="red" stroke="red" points="260.22,-717.91 257,-728 265.81,-722.12 260.22,-717.91"/>
+<text text-anchor="middle" x="368.81" y="-665.39" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- If16&#45;&gt;EQNode15 -->
+<g id="edge7" class="edge">
+<title>If16&#45;&gt;EQNode15</title>
+<path fill="none" stroke="black" d="M422.57,-684.01C420.75,-696.76 418.66,-711.4 416.9,-723.73"/>
+<polygon fill="black" stroke="black" points="413.4,-723.48 415.45,-733.87 420.33,-724.47 413.4,-723.48"/>
+<text text-anchor="middle" x="419.57" y="-686.61" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Con_20 -->
+<g id="node11" class="node">
+<title>Con_20</title>
+<ellipse fill="none" stroke="black" cx="134" cy="-652" rx="27" ry="18"/>
+<text text-anchor="middle" x="134" y="-648.3" font-family="Times,serif" font-size="14.00">#2</text>
+</g>
+<!-- Con_21 -->
+<g id="node12" class="node">
+<title>Con_21</title>
+<ellipse fill="none" stroke="black" cx="206" cy="-652" rx="27" ry="18"/>
+<text text-anchor="middle" x="206" y="-648.3" font-family="Times,serif" font-size="14.00">#3</text>
+</g>
+<!-- Region22 -->
+<g id="node13" class="node">
+<title>Region22</title>
+<polygon fill="yellow" stroke="black" points="446,-584 380,-584 380,-548 446,-548 446,-584"/>
+<text text-anchor="middle" x="413" y="-562.3" font-family="Times,serif" font-size="14.00">Region</text>
+</g>
+<!-- Region22&#45;&gt;If16 -->
+<g id="edge8" class="edge">
+<title>Region22&#45;&gt;If16:p0</title>
+<path fill="none" stroke="red" d="M409.56,-584.01C407.87,-593.54 406.04,-605.9 405.32,-617.98"/>
+<polygon fill="red" stroke="red" points="401.82,-617.89 405,-628 408.81,-618.12 401.82,-617.89"/>
+<text text-anchor="middle" x="406.56" y="-586.61" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Region22&#45;&gt;If16 -->
+<g id="edge9" class="edge">
+<title>Region22&#45;&gt;If16:p1</title>
+<path fill="none" stroke="red" d="M428.61,-584.03C435.66,-593.13 443.03,-605.06 445.83,-617.72"/>
+<polygon fill="red" stroke="red" points="442.39,-618.46 447,-628 449.35,-617.67 442.39,-618.46"/>
+<text text-anchor="middle" x="425.61" y="-586.63" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_c23 -->
+<g id="node14" class="node">
+<title>Phi_c23</title>
+<ellipse fill="lightyellow" stroke="black" cx="170" cy="-566" rx="27" ry="18"/>
+<text text-anchor="middle" x="170" y="-562.3" font-family="Times,serif" font-size="14.00">φ_c</text>
+</g>
+<!-- Phi_c23&#45;&gt;Con_20 -->
+<g id="edge11" class="edge">
+<title>Phi_c23&#45;&gt;Con_20</title>
+<path fill="none" stroke="black" d="M162.89,-583.6C157.81,-595.45 150.88,-611.62 145.08,-625.15"/>
+<polygon fill="black" stroke="black" points="141.82,-623.87 141.1,-634.44 148.25,-626.63 141.82,-623.87"/>
+<text text-anchor="middle" x="159.89" y="-586.2" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Phi_c23&#45;&gt;Con_21 -->
+<g id="edge12" class="edge">
+<title>Phi_c23&#45;&gt;Con_21</title>
+<path fill="none" stroke="black" d="M177.11,-583.6C182.19,-595.45 189.12,-611.62 194.92,-625.15"/>
+<polygon fill="black" stroke="black" points="191.75,-626.63 198.9,-634.44 198.18,-623.87 191.75,-626.63"/>
+<text text-anchor="middle" x="174.11" y="-586.2" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_c23&#45;&gt;Region22 -->
+<g id="edge10" class="edge">
+<title>Phi_c23&#45;&gt;Region22</title>
+<path fill="none" stroke="black" stroke-dasharray="1,5" d="M197.05,-566C254.48,-566 311.91,-566 369.34,-566"/>
+<polygon fill="black" stroke="black" points="369.68,-569.5 379.68,-566 369.68,-562.5 369.68,-569.5"/>
+<text text-anchor="middle" x="200.05" y="-559.6" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- If24 -->
+<g id="node15" class="node">
+<title>If24</title>
+<polygon fill="yellow" stroke="transparent" points="375,-483 375,-508 469,-508 469,-483 375,-483"/>
+<polygon fill="none" stroke="black" points="375,-483 375,-508 469,-508 469,-483 375,-483"/>
+<text text-anchor="start" x="416.5" y="-491.8" font-family="Times,serif" font-size="14.00">If</text>
+<polygon fill="none" stroke="black" points="375,-452 375,-483 469,-483 469,-452 375,-452"/>
+<polygon fill="yellow" stroke="transparent" points="380,-457 380,-478 420,-478 420,-457 380,-457"/>
+<polygon fill="none" stroke="black" points="380,-457 380,-478 420,-478 420,-457 380,-457"/>
+<text text-anchor="start" x="383" y="-463.8" font-family="Times,serif" font-size="14.00">True</text>
+<polygon fill="yellow" stroke="transparent" points="420,-457 420,-478 464,-478 464,-457 420,-457"/>
+<polygon fill="none" stroke="black" points="420,-457 420,-478 464,-478 464,-457 420,-457"/>
+<text text-anchor="start" x="423" y="-463.8" font-family="Times,serif" font-size="14.00">False</text>
+</g>
+<!-- If24&#45;&gt;Start2 -->
+<g id="edge14" class="edge">
+<title>If24&#45;&gt;Start2:p1</title>
+<path fill="none" stroke="black" d="M392.77,-512.23C384.4,-522.84 376.15,-535.26 371,-548 331.39,-645.92 367.43,-681.24 343,-784 339.09,-800.45 330.97,-805.48 328.06,-817.95"/>
+<polygon fill="black" stroke="black" points="324.57,-817.69 327,-828 331.53,-818.42 324.57,-817.69"/>
+<text text-anchor="middle" x="389.77" y="-514.83" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- If24&#45;&gt;Region22 -->
+<g id="edge13" class="edge">
+<title>If24&#45;&gt;Region22</title>
+<path fill="none" stroke="red" d="M418.65,-512.28C417.76,-520.58 416.81,-529.48 415.94,-537.56"/>
+<polygon fill="red" stroke="red" points="412.45,-537.28 414.86,-547.6 419.41,-538.02 412.45,-537.28"/>
+<text text-anchor="middle" x="415.65" y="-514.88" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Con_28 -->
+<g id="node16" class="node">
+<title>Con_28</title>
+<ellipse fill="none" stroke="black" cx="135" cy="-480" rx="27" ry="18"/>
+<text text-anchor="middle" x="135" y="-476.3" font-family="Times,serif" font-size="14.00">#2</text>
+</g>
+<!-- Con_29 -->
+<g id="node17" class="node">
+<title>Con_29</title>
+<ellipse fill="none" stroke="black" cx="207" cy="-480" rx="27" ry="18"/>
+<text text-anchor="middle" x="207" y="-476.3" font-family="Times,serif" font-size="14.00">#3</text>
+</g>
+<!-- Region30 -->
+<g id="node18" class="node">
+<title>Region30</title>
+<polygon fill="yellow" stroke="black" points="482,-412 416,-412 416,-376 482,-376 482,-412"/>
+<text text-anchor="middle" x="449" y="-390.3" font-family="Times,serif" font-size="14.00">Region</text>
+</g>
+<!-- Region30&#45;&gt;If24 -->
+<g id="edge15" class="edge">
+<title>Region30&#45;&gt;If24:p0</title>
+<path fill="none" stroke="red" d="M424.79,-412.2C414.85,-420.99 404.86,-432.65 401.33,-446"/>
+<polygon fill="red" stroke="red" points="397.85,-445.63 400,-456 404.79,-446.55 397.85,-445.63"/>
+<text text-anchor="middle" x="421.79" y="-414.8" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Region30&#45;&gt;If24 -->
+<g id="edge16" class="edge">
+<title>Region30&#45;&gt;If24:p1</title>
+<path fill="none" stroke="red" d="M445.99,-412.03C444.51,-421.56 442.91,-433.93 442.28,-446"/>
+<polygon fill="red" stroke="red" points="438.78,-445.91 442,-456 445.78,-446.1 438.78,-445.91"/>
+<text text-anchor="middle" x="442.99" y="-414.63" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_b31 -->
+<g id="node19" class="node">
+<title>Phi_b31</title>
+<ellipse fill="lightyellow" stroke="black" cx="135" cy="-394" rx="27.1" ry="18"/>
+<text text-anchor="middle" x="135" y="-390.3" font-family="Times,serif" font-size="14.00">φ_b</text>
+</g>
+<!-- Phi_b31&#45;&gt;Con_28 -->
+<g id="edge18" class="edge">
+<title>Phi_b31&#45;&gt;Con_28</title>
+<path fill="none" stroke="black" d="M135,-412.4C135,-423.74 135,-438.77 135,-451.68"/>
+<polygon fill="black" stroke="black" points="131.5,-451.9 135,-461.9 138.5,-451.9 131.5,-451.9"/>
+<text text-anchor="middle" x="132" y="-415" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Phi_b31&#45;&gt;Con_29 -->
+<g id="edge19" class="edge">
+<title>Phi_b31&#45;&gt;Con_29</title>
+<path fill="none" stroke="black" d="M147.87,-410.02C158.93,-422.92 175.04,-441.71 187.65,-456.42"/>
+<polygon fill="black" stroke="black" points="185.07,-458.8 194.24,-464.11 190.39,-454.24 185.07,-458.8"/>
+<text text-anchor="middle" x="144.87" y="-412.62" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_b31&#45;&gt;Region30 -->
+<g id="edge17" class="edge">
+<title>Phi_b31&#45;&gt;Region30</title>
+<path fill="none" stroke="black" stroke-dasharray="1,5" d="M149.22,-378.43C157.37,-370.68 168.29,-362.11 180,-358 224.67,-342.33 347.58,-344.66 393,-358 401.94,-360.63 410.81,-365.17 418.76,-370.16"/>
+<polygon fill="black" stroke="black" points="417.05,-373.23 427.3,-375.91 420.96,-367.43 417.05,-373.23"/>
+<text text-anchor="middle" x="146.22" y="-372.03" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Region32 -->
+<g id="node20" class="node">
+<title>Region32</title>
+<polygon fill="yellow" stroke="black" points="390,-340 324,-340 324,-304 390,-304 390,-340"/>
+<text text-anchor="middle" x="357" y="-318.3" font-family="Times,serif" font-size="14.00">Region</text>
+</g>
+<!-- Region32&#45;&gt;If9 -->
+<g id="edge21" class="edge">
+<title>Region32&#45;&gt;If9:p1</title>
+<path fill="none" stroke="red" d="M369.73,-340.15C376.29,-350.08 383.61,-363.12 387,-376 391.08,-391.47 392.32,-396.91 387,-412 380.17,-431.38 366.81,-429.44 358,-448 304.99,-559.76 299.28,-598.69 299.01,-717.76"/>
+<polygon fill="red" stroke="red" points="295.51,-718 299,-728 302.51,-718 295.51,-718"/>
+<text text-anchor="middle" x="366.73" y="-342.75" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Region32&#45;&gt;Region30 -->
+<g id="edge20" class="edge">
+<title>Region32&#45;&gt;Region30</title>
+<path fill="none" stroke="red" d="M379.74,-340.3C391.44,-349.2 405.86,-360.18 418.51,-369.8"/>
+<polygon fill="red" stroke="red" points="416.44,-372.63 426.52,-375.9 420.68,-367.06 416.44,-372.63"/>
+<text text-anchor="middle" x="376.74" y="-342.9" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Phi_a33 -->
+<g id="node21" class="node">
+<title>Phi_a33</title>
+<ellipse fill="lightyellow" stroke="black" cx="279" cy="-322" rx="27" ry="18"/>
+<text text-anchor="middle" x="279" y="-318.3" font-family="Times,serif" font-size="14.00">φ_a</text>
+</g>
+<!-- Phi_a33&#45;&gt;Con_6 -->
+<g id="edge24" class="edge">
+<title>Phi_a33&#45;&gt;Con_6</title>
+<path fill="none" stroke="black" d="M293.57,-337.17C303.75,-347.06 317.52,-360.45 329.03,-371.64"/>
+<polygon fill="black" stroke="black" points="326.59,-374.15 336.2,-378.62 331.47,-369.13 326.59,-374.15"/>
+<text text-anchor="middle" x="290.57" y="-339.77" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_a33&#45;&gt;Con_13 -->
+<g id="edge23" class="edge">
+<title>Phi_a33&#45;&gt;Con_13</title>
+<path fill="none" stroke="black" d="M279,-340.3C279,-348.02 279,-357.29 279,-365.89"/>
+<polygon fill="black" stroke="black" points="275.5,-365.9 279,-375.9 282.5,-365.9 275.5,-365.9"/>
+<text text-anchor="middle" x="276" y="-342.9" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Phi_a33&#45;&gt;Region32 -->
+<g id="edge22" class="edge">
+<title>Phi_a33&#45;&gt;Region32</title>
+<path fill="none" stroke="black" stroke-dasharray="1,5" d="M306.12,-322C308.58,-322 311.04,-322 313.5,-322"/>
+<polygon fill="black" stroke="black" points="313.61,-325.5 323.61,-322 313.61,-318.5 313.61,-325.5"/>
+<text text-anchor="middle" x="309.12" y="-315.6" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Phi_b34 -->
+<g id="node22" class="node">
+<title>Phi_b34</title>
+<ellipse fill="lightyellow" stroke="black" cx="207" cy="-322" rx="27.1" ry="18"/>
+<text text-anchor="middle" x="207" y="-318.3" font-family="Times,serif" font-size="14.00">φ_b</text>
+</g>
+<!-- Phi_b34&#45;&gt;Con_7 -->
+<g id="edge27" class="edge">
+<title>Phi_b34&#45;&gt;Con_7</title>
+<path fill="none" stroke="black" d="M207,-340.3C207,-348.02 207,-357.29 207,-365.89"/>
+<polygon fill="black" stroke="black" points="203.5,-365.9 207,-375.9 210.5,-365.9 203.5,-365.9"/>
+<text text-anchor="middle" x="204" y="-342.9" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_b34&#45;&gt;Phi_b31 -->
+<g id="edge26" class="edge">
+<title>Phi_b34&#45;&gt;Phi_b31</title>
+<path fill="none" stroke="black" d="M192.08,-337.5C182.04,-347.27 168.62,-360.31 157.32,-371.3"/>
+<polygon fill="black" stroke="black" points="154.63,-369.03 149.9,-378.51 159.51,-374.05 154.63,-369.03"/>
+<text text-anchor="middle" x="189.08" y="-340.1" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Phi_b34&#45;&gt;Region32 -->
+<g id="edge25" class="edge">
+<title>Phi_b34&#45;&gt;Region32</title>
+<path fill="none" stroke="black" stroke-dasharray="1,5" d="M221.22,-306.43C229.37,-298.68 240.29,-290.11 252,-286 274.65,-278.06 283.12,-278.74 306,-286 313.99,-288.54 321.81,-292.83 328.83,-297.58"/>
+<polygon fill="black" stroke="black" points="327.11,-300.66 337.24,-303.75 331.25,-295.01 327.11,-300.66"/>
+<text text-anchor="middle" x="218.22" y="-300.03" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Phi_c35 -->
+<g id="node23" class="node">
+<title>Phi_c35</title>
+<ellipse fill="lightyellow" stroke="black" cx="112" cy="-322" rx="27" ry="18"/>
+<text text-anchor="middle" x="112" y="-318.3" font-family="Times,serif" font-size="14.00">φ_c</text>
+</g>
+<!-- Phi_c35&#45;&gt;Con_8 -->
+<g id="edge30" class="edge">
+<title>Phi_c35&#45;&gt;Con_8</title>
+<path fill="none" stroke="black" d="M97.7,-337.5C88.08,-347.27 75.22,-360.31 64.39,-371.3"/>
+<polygon fill="black" stroke="black" points="61.81,-368.94 57.28,-378.51 66.79,-373.85 61.81,-368.94"/>
+<text text-anchor="middle" x="94.7" y="-340.1" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Phi_c35&#45;&gt;Phi_c23 -->
+<g id="edge29" class="edge">
+<title>Phi_c35&#45;&gt;Phi_c23</title>
+<path fill="none" stroke="black" d="M105.53,-339.52C93.19,-373.79 70.03,-454.23 99,-512 107.39,-528.74 123.86,-541.54 138.82,-550.37"/>
+<polygon fill="black" stroke="black" points="137.41,-553.59 147.86,-555.35 140.79,-547.46 137.41,-553.59"/>
+<text text-anchor="middle" x="102.53" y="-342.12" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Phi_c35&#45;&gt;Region32 -->
+<g id="edge28" class="edge">
+<title>Phi_c35&#45;&gt;Region32</title>
+<path fill="none" stroke="black" stroke-dasharray="1,5" d="M130.62,-308.94C143.75,-300.56 162.11,-290.44 180,-286 234.35,-272.5 252.62,-269.06 306,-286 313.99,-288.54 321.81,-292.83 328.83,-297.58"/>
+<polygon fill="black" stroke="black" points="327.11,-300.66 337.24,-303.75 331.25,-295.01 327.11,-300.66"/>
+<text text-anchor="middle" x="127.62" y="-302.54" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Add36 -->
+<g id="node24" class="node">
+<title>Add36</title>
+<ellipse fill="none" stroke="black" cx="207" cy="-250" rx="27" ry="18"/>
+<text text-anchor="middle" x="207" y="-246.3" font-family="Times,serif" font-size="14.00">+</text>
+</g>
+<!-- Add36&#45;&gt;Phi_b34 -->
+<g id="edge32" class="edge">
+<title>Add36&#45;&gt;Phi_b34</title>
+<path fill="none" stroke="black" d="M207,-268.3C207,-276.02 207,-285.29 207,-293.89"/>
+<polygon fill="black" stroke="black" points="203.5,-293.9 207,-303.9 210.5,-293.9 203.5,-293.9"/>
+<text text-anchor="middle" x="204" y="-270.9" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Add36&#45;&gt;Phi_c35 -->
+<g id="edge31" class="edge">
+<title>Add36&#45;&gt;Phi_c35</title>
+<path fill="none" stroke="black" d="M189.58,-263.84C175.07,-274.53 154.21,-289.9 137.77,-302.01"/>
+<polygon fill="black" stroke="black" points="135.61,-299.26 129.64,-308.01 139.76,-304.89 135.61,-299.26"/>
+<text text-anchor="middle" x="186.58" y="-266.44" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Add37 -->
+<g id="node25" class="node">
+<title>Add37</title>
+<ellipse fill="none" stroke="black" cx="279" cy="-178" rx="27" ry="18"/>
+<text text-anchor="middle" x="279" y="-174.3" font-family="Times,serif" font-size="14.00">+</text>
+</g>
+<!-- Add37&#45;&gt;Phi_a33 -->
+<g id="edge34" class="edge">
+<title>Add37&#45;&gt;Phi_a33</title>
+<path fill="none" stroke="black" d="M279,-196.13C279,-220.33 279,-264.79 279,-293.61"/>
+<polygon fill="black" stroke="black" points="275.5,-293.81 279,-303.81 282.5,-293.81 275.5,-293.81"/>
+<text text-anchor="middle" x="276" y="-198.73" font-family="Helvetica,sans-Serif" font-size="8.00">2</text>
+</g>
+<!-- Add37&#45;&gt;Add36 -->
+<g id="edge33" class="edge">
+<title>Add37&#45;&gt;Add36</title>
+<path fill="none" stroke="black" d="M264.43,-193.17C254.25,-203.06 240.48,-216.45 228.97,-227.64"/>
+<polygon fill="black" stroke="black" points="226.53,-225.13 221.8,-234.62 231.41,-230.15 226.53,-225.13"/>
+<text text-anchor="middle" x="261.43" y="-195.77" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+<!-- Return38&#45;&gt;Region32 -->
+<g id="edge35" class="edge">
+<title>Return38&#45;&gt;Region32</title>
+<path fill="none" stroke="red" d="M306.41,-124.15C315.94,-161.25 338.54,-249.19 350.03,-293.9"/>
+<polygon fill="red" stroke="red" points="346.69,-294.95 352.57,-303.77 353.47,-293.21 346.69,-294.95"/>
+<text text-anchor="middle" x="303.41" y="-126.75" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Return38&#45;&gt;Add37 -->
+<g id="edge36" class="edge">
+<title>Return38&#45;&gt;Add37</title>
+<path fill="none" stroke="black" d="M296.31,-124.3C293.75,-132.1 290.67,-141.49 287.82,-150.17"/>
+<polygon fill="black" stroke="black" points="284.42,-149.3 284.62,-159.9 291.07,-151.49 284.42,-149.3"/>
+<text text-anchor="middle" x="293.31" y="-126.9" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+</g>
+</svg>
diff --git a/chapter06/docs/06-graph4-true.gv b/chapter06/docs/06-graph4-true.gv
new file mode 100644
index 00000000..6e48ee7d
--- /dev/null
+++ b/chapter06/docs/06-graph4-true.gv
@@ -0,0 +1,37 @@
+digraph chapter06 {
+/*
+int a = 0;
+int b = 1;
+int c = 0;
+if( arg ) {
+ a = 1;
+ if( arg==2 ) { c=2; } else { c=3; }
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b+c;
+
+*/
+ rankdir=BT;
+ ordering="in";
+ concentrate="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_44 [ label="#6" ];
+ Return45 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return45[taillabel=0 color=red];
+ Return45 -> Start2:p0[taillabel=0 color=red];
+ Return45 -> Con_44[taillabel=1];
+}
diff --git a/chapter06/docs/06-graph4-true.svg b/chapter06/docs/06-graph4-true.svg
new file mode 100644
index 00000000..a533ea21
--- /dev/null
+++ b/chapter06/docs/06-graph4-true.svg
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.43.0 (0)
+ -->
+<!-- Title: chapter06 Pages: 1 -->
+<svg width="178pt" height="248pt"
+ viewBox="0.00 0.00 178.00 248.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 244)">
+<title>chapter06</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-244 174,-244 174,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-232 162,-232 162,-8 8,-8"/>
+</g>
+<!-- Start2 -->
+<g id="node1" class="node">
+<title>Start2</title>
+<polygon fill="yellow" stroke="transparent" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<polygon fill="none" stroke="black" points="24,-195 24,-220 74,-220 74,-195 24,-195"/>
+<text text-anchor="start" x="30.5" y="-203.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="24,-164 24,-195 74,-195 74,-164 24,-164"/>
+<polygon fill="yellow" stroke="transparent" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<polygon fill="none" stroke="black" points="29,-169 29,-190 69,-190 69,-169 29,-169"/>
+<text text-anchor="start" x="32" y="-175.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+</g>
+<!-- Stop3 -->
+<g id="node2" class="node">
+<title>Stop3</title>
+<polygon fill="yellow" stroke="black" points="112,-52 58,-52 58,-16 112,-16 112,-52"/>
+<text text-anchor="middle" x="85" y="-30.3" font-family="Times,serif" font-size="14.00">Stop</text>
+</g>
+<!-- Return45 -->
+<g id="node4" class="node">
+<title>Return45</title>
+<polygon fill="yellow" stroke="black" points="118,-124 52,-124 52,-88 118,-88 118,-124"/>
+<text text-anchor="middle" x="85" y="-102.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Stop3&#45;&gt;Return45 -->
+<g id="edge1" class="edge">
+<title>Stop3&#45;&gt;Return45</title>
+<path fill="none" stroke="red" d="M85,-52.3C85,-60.02 85,-69.29 85,-77.89"/>
+<polygon fill="red" stroke="red" points="81.5,-77.9 85,-87.9 88.5,-77.9 81.5,-77.9"/>
+<text text-anchor="middle" x="82" y="-54.9" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Con_44 -->
+<g id="node3" class="node">
+<title>Con_44</title>
+<ellipse fill="none" stroke="black" cx="127" cy="-192" rx="27" ry="18"/>
+<text text-anchor="middle" x="127" y="-188.3" font-family="Times,serif" font-size="14.00">#6</text>
+</g>
+<!-- Return45&#45;&gt;Start2 -->
+<g id="edge2" class="edge">
+<title>Return45&#45;&gt;Start2:p0</title>
+<path fill="none" stroke="red" d="M68.26,-124.13C60.77,-133.26 52.99,-145.23 50.13,-158.06"/>
+<polygon fill="red" stroke="red" points="46.66,-157.67 49,-168 53.61,-158.46 46.66,-157.67"/>
+<text text-anchor="middle" x="65.26" y="-126.73" font-family="Helvetica,sans-Serif" font-size="8.00">0</text>
+</g>
+<!-- Return45&#45;&gt;Con_44 -->
+<g id="edge3" class="edge">
+<title>Return45&#45;&gt;Con_44</title>
+<path fill="none" stroke="black" d="M93.7,-124.4C99.66,-136.31 107.65,-152.3 114.31,-165.62"/>
+<polygon fill="black" stroke="black" points="111.28,-167.38 118.88,-174.76 117.54,-164.25 111.28,-167.38"/>
+<text text-anchor="middle" x="90.7" y="-127" font-family="Helvetica,sans-Serif" font-size="8.00">1</text>
+</g>
+</g>
+</svg>
diff --git a/chapter06/docs/build-graphs.sh b/chapter06/docs/build-graphs.sh
new file mode 100644
index 00000000..d65826f4
--- /dev/null
+++ b/chapter06/docs/build-graphs.sh
@@ -0,0 +1,10 @@
+dot -Tsvg 06-graph1-pre.gv > 06-graph1-pre.svg
+dot -Tsvg 06-graph1-post.gv > 06-graph1-post.svg
+dot -Tsvg 06-graph2-pre.gv > 06-graph2-pre.svg
+dot -Tsvg 06-graph2-post.gv > 06-graph2-post.svg
+dot -Tsvg 06-graph3-nonconst.gv > 06-graph3-nonconst.svg
+dot -Tsvg 06-graph3-true.gv > 06-graph3-true.svg
+dot -Tsvg 06-graph3-false.gv > 06-graph3-false.svg
+dot -Tsvg 06-graph4-nonconst.gv > 06-graph4-nonconst.svg
+dot -Tsvg 06-graph4-true.gv > 06-graph4-true.svg
+dot -Tsvg 06-graph4-arg2.gv > 06-graph4-arg2.svg
diff --git a/chapter06/pom.xml b/chapter06/pom.xml
new file mode 100644
index 00000000..7a9b466d
--- /dev/null
+++ b/chapter06/pom.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>com.seaofnodes</groupId>
+ <artifactId>simple</artifactId>
+ <version>1.0</version>
+ </parent>
+ <artifactId>chapter06</artifactId>
+ <packaging>jar</packaging>
+ <name>Chapter 6</name>
+</project> \ No newline at end of file
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/GraphVisualizer.java b/chapter06/src/main/java/com/seaofnodes/simple/GraphVisualizer.java
new file mode 100644
index 00000000..c1c1c4c9
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/GraphVisualizer.java
@@ -0,0 +1,235 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+
+import java.util.*;
+
+/**
+ * Simple visualizer that outputs GraphViz dot format.
+ * The dot output must be saved to a file and run manually via dot to generate the SVG output.
+ * Currently, this is done manually.
+ */
+public class GraphVisualizer {
+
+ public String generateDotOutput(Parser parser) {
+
+ // Since the graph has cycles, we need to create a flat list of all the
+ // nodes in the graph.
+ Collection<Node> all = findAll(parser);
+ StringBuilder sb = new StringBuilder();
+ sb.append("digraph chapter06 {\n");
+ sb.append("/*\n");
+ sb.append(parser.src());
+ sb.append("\n*/\n");
+
+ // To keep the Scopes below the graph and pointing up into the graph we
+ // need to group the Nodes in a subgraph cluster, and the scopes into a
+ // different subgraph cluster. THEN we can draw edges between the
+ // scopes and nodes. If we try to cross subgraph cluster borders while
+ // still making the subgraphs DOT gets confused.
+ sb.append("\trankdir=BT;\n"); // Force Nodes before Scopes
+
+ // Preserve node input order
+ sb.append("\tordering=\"in\";\n");
+
+ // Merge multiple edges hitting the same node. Makes common shared
+ // nodes much prettier to look at.
+ sb.append("\tconcentrate=\"true\";\n");
+
+ // Just the Nodes first, in a cluster no edges
+ nodes(sb, all);
+
+ // Now the scopes, in a cluster no edges
+ for (ScopeNode sn: parser._allScopes)
+ scopes(sb, sn);
+
+ // Walk the Node edges
+ nodeEdges(sb, all);
+
+ // Walk the Scope edges
+ for (ScopeNode sn: parser._allScopes)
+ scopeEdges(sb, sn);
+
+ sb.append("}\n");
+ return sb.toString();
+ }
+
+
+ private void nodes(StringBuilder sb, Collection<Node> all) {
+ // Just the Nodes first, in a cluster no edges
+ sb.append("\tsubgraph cluster_Nodes {\n"); // Magic "cluster_" in the subgraph name
+ for( Node n : all ) {
+ if( n instanceof ProjNode || n instanceof ScopeNode )
+ continue; // Do not emit, rolled into MultiNode or Scope cluster already
+ sb.append("\t\t").append(n.uniqueName()).append(" [ ");
+ String lab = n.glabel();
+ if( n instanceof MultiNode ) {
+ // Make a box with the MultiNode on top, and all the projections on the bottom
+ sb.append("shape=plaintext label=<\n");
+ sb.append("\t\t\t<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\">\n");
+ sb.append("\t\t\t<TR><TD BGCOLOR=\"yellow\">").append(lab).append("</TD></TR>\n");
+ sb.append("\t\t\t<TR>");
+ boolean doProjTable = false;
+ for( Node use : n._outputs ) {
+ if( use instanceof ProjNode proj ) {
+ if (!doProjTable) {
+ doProjTable = true;
+ sb.append("<TD>").append("\n");
+ sb.append("\t\t\t\t<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">").append("\n");
+ sb.append("\t\t\t\t<TR>");
+ }
+ sb.append("<TD PORT=\"p").append(proj._idx).append("\"");
+ if( proj.isCFG() ) sb.append(" BGCOLOR=\"yellow\"");
+ sb.append(">").append(proj.glabel()).append("</TD>");
+ }
+ }
+ if (doProjTable) {
+ sb.append("</TR>").append("\n");
+ sb.append("\t\t\t\t</TABLE>").append("\n");
+ sb.append("\t\t\t</TD>");
+ }
+ sb.append("</TR>\n");
+ sb.append("\t\t\t</TABLE>>\n\t\t");
+
+ } else {
+ // control nodes have box shape
+ // other nodes are ellipses, i.e. default shape
+ if( n.isCFG() )
+ sb.append("shape=box style=filled fillcolor=yellow ");
+ if( n instanceof PhiNode )
+ sb.append("style=filled fillcolor=lightyellow ");
+ sb.append("label=\"").append(lab).append("\" ");
+ }
+ sb.append("];\n");
+ }
+
+ // Force Region & Phis to line up
+ for( Node n : all ) {
+ if( n instanceof RegionNode region ) {
+ sb.append("\t\t{ rank=same; ");
+ sb.append(region).append(";") ;
+ for( Node phi : region._outputs )
+ if( phi instanceof PhiNode )
+ sb.append(phi.uniqueName()).append(";");
+ sb.append("}\n");
+ }
+ }
+
+ sb.append("\t}\n"); // End Node cluster
+ }
+
+ private void scopes( StringBuilder sb, ScopeNode scopenode) {
+ sb.append("\tnode [shape=plaintext];\n");
+ int level=0;
+ for( HashMap<String,Integer> scope : scopenode._scopes ) {
+ String scopeName = makeScopeName(scopenode, level);
+ sb.append("\tsubgraph cluster_").append(scopeName).append(" {\n"); // Magic "cluster_" in the subgraph name
+ sb.append("\t\t").append(scopeName).append(" [label=<\n");
+ sb.append("\t\t\t<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">\n");
+ // Add the scope level
+ sb.append("\t\t\t<TR><TD BGCOLOR=\"cyan\">").append(level).append("</TD>");
+ for( String name : scope.keySet() )
+ sb.append("<TD PORT=\"").append(makePortName(scopeName, name)).append("\">").append(name).append("</TD>");
+ sb.append("</TR>\n");
+ sb.append("\t\t\t</TABLE>>];\n");
+ level++;
+ }
+ // Scope clusters nest, so the graphics shows the nested scopes, so
+ // they are not closed as they are printed; so they just keep nesting.
+ // We close them all at once here.
+ sb.append( "\t}\n".repeat( level ) ); // End all Scope clusters
+ }
+
+ private String makeScopeName(ScopeNode sn, int level) { return sn.uniqueName() + "_" + level; }
+ private String makePortName(String scopeName, String varName) { return scopeName + "_" + varName; }
+
+ // Walk the node edges
+ private void nodeEdges(StringBuilder sb, Collection<Node> all) {
+ // All them edge labels
+ sb.append("\tedge [ fontname=Helvetica, fontsize=8 ];\n");
+ for( Node n : all ) {
+ // Do not display the Constant->Start edge;
+ // ProjNodes handled by Multi;
+ // ScopeNodes are done separately
+ if( n instanceof ConstantNode || n instanceof ProjNode || n instanceof ScopeNode )
+ continue;
+ int i=0;
+ for( Node def : n._inputs ) {
+ if( n instanceof PhiNode && def instanceof RegionNode ) {
+ // Draw a dotted use->def edge from Phi to Region.
+ sb.append('\t').append(n.uniqueName());
+ sb.append(" -> ");
+ sb.append(def.uniqueName());
+ sb.append(" [style=dotted taillabel=").append(i).append("];\n");
+ } else if( def != null ) {
+ // Most edges land here use->def
+ sb.append('\t').append(n.uniqueName()).append(" -> ");
+ if( def instanceof ProjNode proj ) {
+ String mname = proj.ctrl().uniqueName();
+ sb.append(mname).append(":p").append(proj._idx);
+ } else sb.append(def.uniqueName());
+ // Number edges, so we can see how they track
+ sb.append("[taillabel=").append(i);
+ // control edges are colored red
+ if( def.isCFG() )
+ sb.append(" color=red");
+ sb.append("];\n");
+ }
+ i++;
+ }
+ }
+ }
+
+ // Walk the scope edges
+ private void scopeEdges( StringBuilder sb, ScopeNode scopenode ) {
+ sb.append("\tedge [style=dashed color=cornflowerblue];\n");
+ int level=0;
+ for( HashMap<String,Integer> scope : scopenode._scopes ) {
+ String scopeName = makeScopeName(scopenode, level);
+ for( String name : scope.keySet() ) {
+ Node def = scopenode.in(scope.get(name));
+ if( def==null ) continue;
+ sb.append("\t")
+ .append(scopeName).append(":")
+ .append('"').append(makePortName(scopeName, name)).append('"') // wrap port name with quotes because $ctrl is not valid unquoted
+ .append(" -> ");
+ if( def instanceof ProjNode proj ) {
+ String mname = proj.ctrl().uniqueName();
+ sb.append(mname).append(":p").append(proj._idx);
+ } else sb.append(def.uniqueName());
+ sb.append(";\n");
+ }
+ level++;
+ }
+ }
+
+ /**
+ * Finds all nodes in the graph.
+ */
+ private Collection<Node> findAll(Parser parser) {
+ final StartNode start = Parser.START;
+ final HashMap<Integer, Node> all = new HashMap<>();
+ for( Node n : start._outputs )
+ walk(all, n);
+ // Scan symbol tables
+ for( HashMap<String,Integer> scope : parser._scope._scopes )
+ for (Integer i : scope.values())
+ walk(all, parser._scope.in(i));
+ return all.values();
+ }
+
+ /**
+ * Walk a subgraph and populate distinct nodes in the all list.
+ */
+ private void walk(HashMap<Integer, Node> all, Node n) {
+ if(n == null ) return;
+ if (all.get(n._nid) != null) return; // Been there, done that
+ all.put(n._nid, n);
+ for (Node c : n._inputs)
+ if (c != null)
+ walk(all, c);
+ for (Node c : n._outputs)
+ if (c != null)
+ walk(all, c);
+ }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/Parser.java b/chapter06/src/main/java/com/seaofnodes/simple/Parser.java
new file mode 100644
index 00000000..b08c3c56
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/Parser.java
@@ -0,0 +1,528 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.*;
+
+import java.util.*;
+
+/**
+ * The Parser converts a Simple source program to the Sea of Nodes intermediate
+ * representation directly in one pass. There is no intermediate Abstract
+ * Syntax Tree structure.
+ * <p>
+ * This is a simple recursive descent parser. All lexical analysis is done here as well.
+ */
+public class Parser {
+
+ /**
+ * The control is a name that binds to the currently active control
+ * node in the graph
+ */
+ public static final String CTRL = "$ctrl";
+
+ /**
+ * A Global Static, unique to each compilation. This is a public, so we
+ * can make constants everywhere without having to thread the StartNode
+ * through the entire parser and optimizer.
+ * <p>
+ * To make the compiler multithreaded, this field will have to move into a TLS.
+ */
+ public static StartNode START;
+
+ public StopNode STOP;
+
+ // The Lexer. Thin wrapper over a byte[] buffer with a cursor.
+ private final Lexer _lexer;
+
+ /**
+ * Current ScopeNode - ScopeNodes change as we parse code, but at any point of time
+ * there is one current ScopeNode. The reason the current ScopeNode can change is to do with how
+ * we handle branching. See {@link #parseIf()}.
+ * <p>
+ * Each ScopeNode contains a stack of lexical scopes, each scope is a symbol table that binds
+ * variable names to Nodes. The top of this stack represents current scope.
+ * <p>
+ * We keep a list of all ScopeNodes so that we can show them in graphs.
+ * @see #parseIf()
+ * @see #_allScopes
+ */
+ public ScopeNode _scope;
+
+ /**
+ * List of keywords disallowed as identifiers
+ */
+ private final HashSet<String> KEYWORDS = new HashSet<>(){{
+ add("else");
+ add("false");
+ add("if");
+ add("int");
+ add("return");
+ add("true");
+ }};
+
+
+ /**
+ * We clone ScopeNodes when control flows branch; it is useful to have
+ * a list of all active ScopeNodes for purposes of visualization of the SoN graph
+ */
+ public final Stack<ScopeNode> _allScopes = new Stack<>();
+
+ public Parser(String source, TypeInteger arg) {
+ _lexer = new Lexer(source);
+ Node.reset();
+ _scope = new ScopeNode();
+ START = new StartNode(new Type[]{ Type.CONTROL, arg });
+ START.peephole();
+ STOP = new StopNode();
+ }
+
+ public Parser(String source) {
+ this(source, TypeInteger.BOT);
+ }
+
+ String src() { return new String( _lexer._input ); }
+
+ private Node ctrl() { return _scope.ctrl(); }
+
+ private Node ctrl(Node n) { return _scope.ctrl(n); }
+
+ public StopNode parse() { return parse(false); }
+ public StopNode parse(boolean show) {
+ _allScopes.push(_scope);
+ _scope.push();
+ try {
+ _scope.define(CTRL , new ProjNode(START, 0, CTRL ).peephole());
+ _scope.define("arg", new ProjNode(START, 1, "arg").peephole());
+ parseBlock();
+ if (!_lexer.isEOF()) throw error("Syntax error, unexpected " + _lexer.getAnyNextToken());
+ return STOP;
+ }
+ finally {
+ _scope.pop();
+ _allScopes.pop();
+ STOP.peephole();
+ if( show ) showGraph();
+ }
+ }
+
+ /**
+ * Parses a block
+ *
+ * <pre>
+ * '{' statements '}'
+ * </pre>
+ * Does not parse the opening or closing '{}'
+ * @return a {@link Node} or {@code null}
+ */
+ private Node parseBlock() {
+ // Enter a new scope
+ _scope.push();
+ Node n = null;
+ while (!peek('}') && !_lexer.isEOF()) {
+ Node n0 = parseStatement();
+ if (n0 != null) n = n0; // Allow null returns from eg showGraph
+ };
+ // Exit scope
+ _scope.pop();
+ return n;
+ }
+
+ /**
+ * Parses a statement
+ *
+ * <pre>
+ * returnStatement | declStatement | blockStatement | ifStatement | expressionStatement
+ * </pre>
+ * @return a {@link Node} or {@code null}
+ */
+ private Node parseStatement() {
+ if (matchx("return") ) return parseReturn();
+ else if (matchx("int")) return parseDecl();
+ else if (match ("{" )) return require(parseBlock(),"}");
+ else if (matchx("if" )) return parseIf();
+ else if (matchx("#showGraph")) return require(showGraph(),";");
+ else return parseExpressionStatement();
+ }
+
+ /**
+ * Parses a statement
+ *
+ * <pre>
+ * if ( expression ) statement [else statement]
+ * </pre>
+ * @return a {@link Node}, never {@code null}
+ */
+ private Node parseIf() {
+ require("(");
+ // Parse predicate
+ var pred = require(parseExpression(), ")");
+ // IfNode takes current control and predicate
+ IfNode ifNode = (IfNode)new IfNode(ctrl(), pred).<IfNode>keep().peephole();
+ // Setup projection nodes
+ Node ifT = new ProjNode(ifNode, 0, "True" ).peephole();
+ ifNode.unkeep();
+ Node ifF = new ProjNode(ifNode, 1, "False").peephole();
+ // In if true branch, the ifT proj node becomes the ctrl
+ // But first clone the scope and set it as current
+ int ndefs = _scope.nIns();
+ ScopeNode fScope = _scope.dup(); // Duplicate current scope
+ _allScopes.push(fScope); // For graph visualization we need all scopes
+
+ // Parse the true side
+ ctrl(ifT); // set ctrl token to ifTrue projection
+ parseStatement(); // Parse true-side
+ ScopeNode tScope = _scope;
+
+ // Parse the false side
+ _scope = fScope; // Restore scope, then parse else block if any
+ ctrl(ifF); // Ctrl token is now set to ifFalse projection
+ if (matchx("else")) parseStatement();
+
+ if( tScope.nIns() != ndefs || fScope.nIns() != ndefs )
+ throw error("Cannot define a new name on one arm of an if");
+
+ // Merge results
+ _scope = tScope;
+ _allScopes.pop(); // Discard pushed from graph display
+
+ return ctrl(tScope.mergeScopes(fScope));
+ }
+
+
+ /**
+ * Parses a return statement; "return" already parsed.
+ * The $ctrl edge is killed.
+ *
+ * <pre>
+ * 'return' expr ;
+ * </pre>
+ * @return an expression {@link Node}, never {@code null}
+ */
+ private Node parseReturn() {
+ var expr = require(parseExpression(), ";");
+ Node ret = STOP.addReturn(new ReturnNode(ctrl(), expr).peephole());
+ ctrl(new ConstantNode(Type.XCONTROL).peephole()); // Kill control
+ return ret;
+ }
+
+ /**
+ * Dumps out the node graph
+ * @return {@code null}
+ */
+ private Node showGraph() {
+ System.out.println(new GraphVisualizer().generateDotOutput(this));
+ return null;
+ }
+
+ /**
+ * Parses an expression statement
+ *
+ * <pre>
+ * name '=' expression ';'
+ * </pre>
+ * @return an expression {@link Node}, never {@code null}
+ */
+ private Node parseExpressionStatement() {
+ var name = requireId();
+ require("=");
+ var expr = require(parseExpression(), ";");
+ if( _scope.update(name, expr)==null )
+ throw error("Undefined name '" + name + "'");
+ return expr;
+ }
+
+ /**
+ * Parses a declStatement
+ *
+ * <pre>
+ * 'int' name = expression ';'
+ * </pre>
+ * @return an expression {@link Node}, never {@code null}
+ */
+ private Node parseDecl() {
+ // Type is 'int' for now
+ var name = requireId();
+ require("=");
+ var expr = require(parseExpression(), ";");
+ if( _scope.define(name,expr) == null )
+ throw error("Redefining name '" + name + "'");
+ return expr;
+ }
+
+ /**
+ * Parse an expression of the form:
+ *
+ * <pre>
+ * expr : compareExpr
+ * </pre>
+ * @return an expression {@link Node}, never {@code null}
+ */
+ private Node parseExpression() { return parseComparison(); }
+
+ /**
+ * Parse an expression of the form:
+ *
+ * <pre>
+ * expr : additiveExpr op additiveExpr
+ * </pre>
+ * @return an comparator expression {@link Node}, never {@code null}
+ */
+ private Node parseComparison() {
+ var lhs = parseAddition();
+ if (match("==")) return new BoolNode.EQNode(lhs, parseComparison()).peephole();
+ if (match("!=")) return new NotNode(new BoolNode.EQNode(lhs, parseComparison()).peephole()).peephole();
+ if (match("<" )) return new BoolNode.LTNode(lhs, parseComparison()).peephole();
+ if (match("<=")) return new BoolNode.LENode(lhs, parseComparison()).peephole();
+ if (match(">" )) return new BoolNode.LTNode(parseComparison(), lhs).peephole();
+ if (match(">=")) return new BoolNode.LENode(parseComparison(), lhs).peephole();
+ return lhs;
+ }
+
+ /**
+ * Parse an additive expression
+ *
+ * <pre>
+ * additiveExpr : multiplicativeExpr (('+' | '-') multiplicativeExpr)*
+ * </pre>
+ * @return an add expression {@link Node}, never {@code null}
+ */
+ private Node parseAddition() {
+ var lhs = parseMultiplication();
+ if (match("+")) return new AddNode(lhs, parseAddition()).peephole();
+ if (match("-")) return new SubNode(lhs, parseAddition()).peephole();
+ return lhs;
+ }
+
+ /**
+ * Parse an multiplicativeExpr expression
+ *
+ * <pre>
+ * multiplicativeExpr : unaryExpr (('*' | '/') unaryExpr)*
+ * </pre>
+ * @return a multiply expression {@link Node}, never {@code null}
+ */
+ private Node parseMultiplication() {
+ var lhs = parseUnary();
+ if (match("*")) return new MulNode(lhs, parseMultiplication()).peephole();
+ if (match("/")) return new DivNode(lhs, parseMultiplication()).peephole();
+ return lhs;
+ }
+
+ /**
+ * Parse a unary minus expression.
+ *
+ * <pre>
+ * unaryExpr : ('-') unaryExpr | primaryExpr
+ * </pre>
+ * @return a unary expression {@link Node}, never {@code null}
+ */
+ private Node parseUnary() {
+ if (match("-")) return new MinusNode(parseUnary()).peephole();
+ return parsePrimary();
+ }
+
+ /**
+ * Parse a primary expression:
+ *
+ * <pre>
+ * primaryExpr : integerLiteral | Identifier | true | false | '(' expression ')'
+ * </pre>
+ * @return a primary {@link Node}, never {@code null}
+ */
+ private Node parsePrimary() {
+ if( _lexer.isNumber() ) return parseIntegerLiteral();
+ if( match("(") ) return require(parseExpression(), ")");
+ if( matchx("true" ) ) return new ConstantNode(TypeInteger.constant(1)).peephole();
+ if( matchx("false") ) return new ConstantNode(TypeInteger.constant(0)).peephole();
+ String name = _lexer.matchId();
+ if( name == null) throw errorSyntax("an identifier or expression");
+ Node n = _scope.lookup(name);
+ if( n!=null ) return n;
+ throw error("Undefined name '" + name + "'");
+ }
+
+ /**
+ * Parse integer literal
+ *
+ * <pre>
+ * integerLiteral: [1-9][0-9]* | [0]
+ * </pre>
+ */
+ private ConstantNode parseIntegerLiteral() {
+ return (ConstantNode) new ConstantNode(_lexer.parseNumber()).peephole();
+ }
+
+ //////////////////////////////////
+ // Utilities for lexical analysis
+
+ // Return true and skip if "syntax" is next in the stream.
+ private boolean match (String syntax) { return _lexer.match (syntax); }
+ // Match must be "exact", not be followed by more id letters
+ private boolean matchx(String syntax) { return _lexer.matchx(syntax); }
+ // Return true and do NOT skip if 'ch' is next
+ private boolean peek(char ch) { return _lexer.peek(ch); }
+
+ // Require and return an identifier
+ private String requireId() {
+ String id = _lexer.matchId();
+ if (id != null && !KEYWORDS.contains(id) ) return id;
+ throw error("Expected an identifier, found '"+id+"'");
+ }
+
+ // Require an exact match
+ private void require(String syntax) { require(null, syntax); }
+ private Node require(Node n, String syntax) {
+ if (match(syntax)) return n;
+ throw errorSyntax(syntax);
+ }
+
+ RuntimeException errorSyntax(String syntax) {
+ return error("Syntax error, expected " + syntax + ": " + _lexer.getAnyNextToken());
+ }
+
+ static RuntimeException error(String errorMessage) {
+ return new RuntimeException(errorMessage);
+ }
+
+ ////////////////////////////////////
+ // Lexer components
+
+ private static class Lexer {
+
+ // Input buffer; an array of text bytes read from a file or a string
+ private final byte[] _input;
+ // Tracks current position in input buffer
+ private int _position = 0;
+
+ /**
+ * Record the source text for lexing
+ */
+ public Lexer(String source) {
+ this(source.getBytes());
+ }
+
+ /**
+ * Direct from disk file source
+ */
+ public Lexer(byte[] buf) {
+ _input = buf;
+ }
+
+ // Very handy in the debugger, shows the unparsed program
+ @Override
+ public String toString() {
+ return new String(_input, _position, _input.length - _position);
+ }
+
+ // True if at EOF
+ private boolean isEOF() {
+ return _position >= _input.length;
+ }
+
+ // Peek next character, or report EOF
+ private char peek() {
+ return isEOF() ? Character.MAX_VALUE // Special value that causes parsing to terminate
+ : (char) _input[_position];
+ }
+
+ private char nextChar() {
+ char ch = peek();
+ _position++;
+ return ch;
+ }
+
+ // True if a white space
+ private boolean isWhiteSpace() {
+ return peek() <= ' '; // Includes all the use space, tab, newline, CR
+ }
+
+ /**
+ * Return the next non-white-space character
+ */
+ private void skipWhiteSpace() {
+ while (isWhiteSpace()) _position++;
+ }
+
+
+ // Return true, if we find "syntax" after skipping white space; also
+ // then advance the cursor past syntax.
+ // Return false otherwise, and do not advance the cursor.
+ boolean match(String syntax) {
+ skipWhiteSpace();
+ int len = syntax.length();
+ if (_position + len > _input.length) return false;
+ for (int i = 0; i < len; i++)
+ if ((char) _input[_position + i] != syntax.charAt(i))
+ return false;
+ _position += len;
+ return true;
+ }
+
+ boolean matchx(String syntax) {
+ if( !match(syntax) ) return false;
+ if( !isIdLetter(peek()) ) return true;
+ _position -= syntax.length();
+ return false;
+ }
+
+ private boolean peek(char ch) {
+ skipWhiteSpace();
+ return peek()==ch;
+ }
+
+ // Return an identifier or null
+ String matchId() {
+ skipWhiteSpace();
+ return isIdStart(peek()) ? parseId() : null;
+ }
+
+ // Used for errors
+ String getAnyNextToken() {
+ if (isEOF()) return "";
+ if (isIdStart(peek())) return parseId();
+ if (isPunctuation(peek())) return parsePunctuation();
+ return String.valueOf(peek());
+ }
+
+ boolean isNumber() {return isNumber(peek());}
+ boolean isNumber(char ch) {return Character.isDigit(ch);}
+
+ private Type parseNumber() {
+ int start = _position;
+ while (isNumber(nextChar())) ;
+ String snum = new String(_input, start, --_position - start);
+ if (snum.length() > 1 && snum.charAt(0) == '0')
+ throw error("Syntax error: integer values cannot start with '0'");
+ return TypeInteger.constant(Long.parseLong(snum));
+ }
+
+ // First letter of an identifier
+ private boolean isIdStart(char ch) {
+ return Character.isAlphabetic(ch) || ch == '_';
+ }
+
+ // All characters of an identifier, e.g. "_x123"
+ private boolean isIdLetter(char ch) {
+ return Character.isLetterOrDigit(ch) || ch == '_';
+ }
+
+ private String parseId() {
+ int start = _position;
+ while (isIdLetter(nextChar())) ;
+ return new String(_input, start, --_position - start);
+ }
+
+ //
+ private boolean isPunctuation(char ch) {
+ return "=;[]<>()+-/*".indexOf(ch) != -1;
+ }
+
+ private String parsePunctuation() {
+ int start = _position;
+ return new String(_input, start, 1);
+ }
+ }
+
+ public static RuntimeException TODO() { return TODO("Not yet implemented"); }
+ public static RuntimeException TODO(String msg) { return new RuntimeException(msg); }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/AddNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/AddNode.java
new file mode 100644
index 00000000..61359d64
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/AddNode.java
@@ -0,0 +1,154 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.*;
+
+public class AddNode extends Node {
+ public AddNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
+
+ @Override public String label() { return "Add"; }
+
+ @Override public String glabel() { return "+"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ in(1)._print0(sb.append("("));
+ in(2)._print0(sb.append("+"));
+ return sb.append(")");
+ }
+
+
+ @Override
+ public Type compute() {
+ if( in(1)._type instanceof TypeInteger i0 &&
+ in(2)._type instanceof TypeInteger i1 ) {
+ if (i0.isConstant() && i1.isConstant())
+ return TypeInteger.constant(i0.value()+i1.value());
+ return i0.meet(i1);
+ }
+ return Type.BOTTOM;
+ }
+
+ @Override
+ public Node idealize () {
+ Node lhs = in(1);
+ Node rhs = in(2);
+ Type t1 = lhs._type;
+ Type t2 = rhs._type;
+
+ // Already handled by peephole constant folding
+ assert !(t1.isConstant() && t2.isConstant());
+
+ // Add of 0. We do not check for (0+x) because this will already
+ // canonicalize to (x+0)
+ if( t2 instanceof TypeInteger i && i.value()==0 )
+ return lhs;
+
+ // Add of same to a multiply by 2
+ if( lhs==rhs )
+ return new MulNode(lhs,new ConstantNode(TypeInteger.constant(2)).peephole());
+
+ // Goal: a left-spine set of adds, with constants on the rhs (which then fold).
+
+ // Move non-adds to RHS
+ if( !(lhs instanceof AddNode) && rhs instanceof AddNode )
+ return swap12();
+
+ // Now we might see (add add non) or (add non non) or (add add add) but never (add non add)
+
+ // Do we have x + (y + z) ?
+ // Swap to (x + y) + z
+ // Rotate (add add add) to remove the add on RHS
+ if( rhs instanceof AddNode add )
+ return new AddNode(new AddNode(lhs,add.in(1)).peephole(), add.in(2));
+
+ // Now we might see (add add non) or (add non non) but never (add non add) nor (add add add)
+ if( !(lhs instanceof AddNode) )
+ return spline_cmp(lhs,rhs) ? swap12() : null;
+
+ // Now we only see (add add non)
+
+ // Do we have (x + con1) + con2?
+ // Replace with (x + (con1+con2) which then fold the constants
+ if( lhs.in(2)._type.isConstant() && t2.isConstant() )
+ return new AddNode(lhs.in(1),new AddNode(lhs.in(2),rhs).peephole());
+
+
+ // Do we have ((x + (phi cons)) + con) ?
+ // Do we have ((x + (phi cons)) + (phi cons)) ?
+ // Push constant up through the phi: x + (phi con0+con0 con1+con1...)
+ Node phicon = phiCon(this,true);
+ if( phicon!=null ) return phicon;
+
+ // Now we sort along the spline via rotates, to gather similar things together.
+
+ // Do we rotate (x + y) + z
+ // into (x + z) + y ?
+ if( spline_cmp(lhs.in(2),rhs) )
+ return new AddNode(new AddNode(lhs.in(1),rhs).peephole(),lhs.in(2));
+
+ return null;
+ }
+
+ // Rotation is only valid for associative ops, e.g. Add, Mul, And, Or.
+ // Do we have ((phi cons)|(x + (phi cons)) + con|(phi cons)) ?
+ // Push constant up through the phi: x + (phi con0+con0 con1+con1...)
+ static Node phiCon(Node op, boolean rotate) {
+ Node lhs = op.in(1);
+ Node rhs = op.in(2);
+ // LHS is either a Phi of constants, or another op with Phi of constants
+ PhiNode lphi = pcon(lhs);
+ if( rotate && lphi==null && lhs.nIns() > 2 ) {
+ // Only valid to rotate constants if both are same associative ops
+ if( lhs.getClass() != op.getClass() ) return null;
+ lphi = pcon(lhs.in(2)); // Will rotate with the Phi push
+ }
+ if( lphi==null ) return null;
+
+ // RHS is a constant or a Phi of constants
+ if( !(rhs instanceof ConstantNode con) && pcon(rhs)==null )
+ return null;
+
+ // If both are Phis, must be same Region
+ if( rhs instanceof PhiNode && lphi.in(0) != rhs.in(0) )
+ return null;
+
+ // Note that this is the exact reverse of Phi pulling a common op down
+ // to reduce total op-count. We don't get in an endless push-up
+ // push-down peephole cycle because the constants all fold first.
+ Node[] ns = new Node[lphi.nIns()];
+ ns[0] = lphi.in(0);
+ // Push constant up through the phi: x + (phi con0+con0 con1+con1...)
+ for( int i=1; i<ns.length; i++ )
+ ns[i] = op.copy(lphi.in(i), rhs instanceof PhiNode ? rhs.in(i) : rhs).peephole();
+ String label = lphi._label + (rhs instanceof PhiNode rphi ? rphi._label : "");
+ Node phi = new PhiNode(label,ns).peephole();
+ // Rotate needs another op, otherwise just the phi
+ return lhs==lphi ? phi : op.copy(lhs.in(1),phi);
+ }
+
+ static PhiNode pcon(Node op) {
+ return op instanceof PhiNode phi && phi.all_cons() ? phi : null;
+ }
+
+ // Compare two off-spline nodes and decide what order they should be in.
+ // Do we rotate ((x + hi) + lo) into ((x + lo) + hi) ?
+ // Generally constants always go right, then Phi-of-constants, then muls, then others.
+ // Ties with in a category sort by node ID.
+ // TRUE if swapping hi and lo.
+ static boolean spline_cmp( Node hi, Node lo ) {
+ if( lo._type.isConstant() ) return false;
+ if( hi._type.isConstant() ) return true ;
+
+ if( lo instanceof PhiNode && lo.all_cons() ) return false;
+ if( hi instanceof PhiNode && hi.all_cons() ) return true ;
+
+ if( lo instanceof PhiNode && !(hi instanceof PhiNode) ) return true;
+ if( hi instanceof PhiNode && !(lo instanceof PhiNode) ) return false;
+
+ // Same category of "others"
+ return lo._nid > hi._nid;
+ }
+
+ @Override Node copy(Node lhs, Node rhs) { return new AddNode(lhs,rhs); }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/BoolNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/BoolNode.java
new file mode 100644
index 00000000..bbcc742d
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/BoolNode.java
@@ -0,0 +1,58 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeInteger;
+
+abstract public class BoolNode extends Node {
+
+ public BoolNode(Node lhs, Node rhs) {
+ super(null, lhs, rhs);
+ }
+
+ abstract String op(); // String opcode name
+
+ @Override
+ public String label() { return getClass().getSimpleName(); }
+
+ @Override
+ public String glabel() { return op(); }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ in(1)._print0(sb.append("("));
+ in(2)._print0(sb.append(op()));
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ if( in(1)._type instanceof TypeInteger i0 &&
+ in(2)._type instanceof TypeInteger i1 ) {
+ if (i0.isConstant() && i1.isConstant())
+ return TypeInteger.constant(doOp(i0.value(), i1.value()) ? 1 : 0);
+ return i0.meet(i1);
+ }
+ return Type.BOTTOM;
+ }
+
+ abstract boolean doOp(long lhs, long rhs);
+
+ @Override
+ public Node idealize() {
+ // Compare of same
+ if( in(1)==in(2) )
+ return new ConstantNode(TypeInteger.constant(doOp(3,3)?1:0));
+
+ // Do we have ((x * (phi cons)) * con) ?
+ // Do we have ((x * (phi cons)) * (phi cons)) ?
+ // Push constant up through the phi: x * (phi con0*con0 con1*con1...)
+ Node phicon = AddNode.phiCon(this,false);
+ if( phicon!=null ) return phicon;
+
+ return null;
+ }
+
+ public static class EQNode extends BoolNode { public EQNode(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "=="; } boolean doOp(long lhs, long rhs) { return lhs == rhs; } Node copy(Node lhs, Node rhs) { return new EQNode(lhs,rhs); } }
+ public static class LTNode extends BoolNode { public LTNode(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "<" ; } boolean doOp(long lhs, long rhs) { return lhs < rhs; } Node copy(Node lhs, Node rhs) { return new LTNode(lhs,rhs); } }
+ public static class LENode extends BoolNode { public LENode(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "<="; } boolean doOp(long lhs, long rhs) { return lhs <= rhs; } Node copy(Node lhs, Node rhs) { return new LENode(lhs,rhs); } }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/ConstantNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
new file mode 100644
index 00000000..bc92b540
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
@@ -0,0 +1,41 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.Type;
+
+/**
+ * A Constant node represents a constant value. At present, the only constants
+ * that we allow are integer literals; therefore Constants contain an integer
+ * value. As we add other types of constants, we will refactor how we represent
+ * Constants.
+ * <p>
+ * Constants have no semantic inputs. However, we set Start as an input to
+ * Constants to enable a forward graph walk. This edge carries no semantic
+ * meaning, and it is present <em>solely</em> to allow visitation.
+ * <p>
+ * The Constant's value is the value stored in it.
+ */
+public class ConstantNode extends Node {
+ Type _con;
+ public ConstantNode( Type type ) {
+ super(Parser.START);
+ _con = type;
+ }
+
+ @Override
+ public String label() { return "#"+_con; }
+
+ @Override
+ public String uniqueName() { return "Con_" + _nid; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ return _con._print(sb);
+ }
+
+ @Override
+ public Type compute() { return _con; }
+
+ @Override
+ public Node idealize() { return null; }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/DivNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/DivNode.java
new file mode 100644
index 00000000..01314c74
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/DivNode.java
@@ -0,0 +1,37 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeInteger;
+
+public class DivNode extends Node {
+ public DivNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
+
+ @Override public String label() { return "Div"; }
+
+ @Override public String glabel() { return "//"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ in(1)._print0(sb.append("("));
+ in(2)._print0(sb.append("/"));
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ if (in(1)._type instanceof TypeInteger i0 &&
+ in(2)._type instanceof TypeInteger i1) {
+ if (i0.isConstant() && i1.isConstant())
+ return i1.value() == 0
+ ? TypeInteger.ZERO
+ : TypeInteger.constant(i0.value()/i1.value());
+ return i0.meet(i1);
+ }
+ return Type.BOTTOM;
+ }
+
+ @Override
+ public Node idealize() { return null; }
+
+ @Override Node copy(Node lhs, Node rhs) { return new DivNode(lhs,rhs); }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/IfNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/IfNode.java
new file mode 100644
index 00000000..9e88710c
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/IfNode.java
@@ -0,0 +1,44 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeInteger;
+import com.seaofnodes.simple.type.TypeTuple;
+
+public class IfNode extends MultiNode {
+
+ public IfNode(Node ctrl, Node pred) {
+ super(ctrl, pred);
+ }
+
+ @Override
+ public String label() { return "If"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ sb.append("if( ");
+ return in(1)._print0(sb).append(" )");
+ }
+
+ @Override public boolean isCFG() { return true; }
+
+ public Node ctrl() { return in(0); }
+ public Node pred() { return in(1); }
+
+ @Override
+ public Type compute() {
+ // If the If node is not reachable then neither is any following Proj
+ if (ctrl()._type != Type.CONTROL) return TypeTuple.IF_NEITHER;
+ // If constant is 0 then false branch is reachable
+ // Else true branch is reachable
+ if (pred()._type instanceof TypeInteger ti && ti.isConstant()) {
+ if (ti.value() == 0) return TypeTuple.IF_FALSE;
+ else return TypeTuple.IF_TRUE;
+ }
+ return TypeTuple.IF_BOTH;
+ }
+
+ @Override
+ public Node idealize() {
+ return null;
+ }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/MinusNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/MinusNode.java
new file mode 100644
index 00000000..08924563
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/MinusNode.java
@@ -0,0 +1,28 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeInteger;
+
+public class MinusNode extends Node {
+ public MinusNode(Node in) { super(null, in); }
+
+ @Override public String label() { return "Minus"; }
+
+ @Override public String glabel() { return "-"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ in(1)._print0(sb.append("(-"));
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ if (in(1)._type instanceof TypeInteger i0)
+ return i0.isConstant() ? TypeInteger.constant(-i0.value()) : i0;
+ return Type.BOTTOM;
+ }
+
+ @Override
+ public Node idealize() { return null; }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/MulNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/MulNode.java
new file mode 100644
index 00000000..31683a4e
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/MulNode.java
@@ -0,0 +1,56 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeInteger;
+
+public class MulNode extends Node {
+ public MulNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
+
+ @Override public String label() { return "Mul"; }
+
+ @Override public String glabel() { return "*"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ in(1)._print0(sb.append("("));
+ in(2)._print0(sb.append("*"));
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ if (in(1)._type instanceof TypeInteger i0 &&
+ in(2)._type instanceof TypeInteger i1) {
+ if (i0.isConstant() && i1.isConstant())
+ return TypeInteger.constant(i0.value()*i1.value());
+ return i0.meet(i1);
+ }
+ return Type.BOTTOM;
+ }
+
+ @Override
+ public Node idealize() {
+ Node lhs = in(1);
+ Node rhs = in(2);
+ Type t1 = lhs._type;
+ Type t2 = rhs._type;
+
+ // Mul of 1. We do not check for (1*x) because this will already
+ // canonicalize to (x*1)
+ if ( t2.isConstant() && t2 instanceof TypeInteger i && i.value()==1 )
+ return lhs;
+
+ // Move constants to RHS: con*arg becomes arg*con
+ if ( t1.isConstant() && !t2.isConstant() )
+ return swap12();
+
+ // Do we have ((x * (phi cons)) * con) ?
+ // Do we have ((x * (phi cons)) * (phi cons)) ?
+ // Push constant up through the phi: x * (phi con0*con0 con1*con1...)
+ Node phicon = AddNode.phiCon(this,true);
+ if( phicon!=null ) return phicon;
+
+ return null;
+ }
+ @Override Node copy(Node lhs, Node rhs) { return new MulNode(lhs,rhs); }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/MultiNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/MultiNode.java
new file mode 100644
index 00000000..fe3fddf8
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/MultiNode.java
@@ -0,0 +1,9 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+
+public abstract class MultiNode extends Node {
+
+ public MultiNode(Node... inputs) { super(inputs); }
+
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter06/src/main/java/com/seaofnodes/simple/node/Node.java
new file mode 100644
index 00000000..fd1b870c
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -0,0 +1,380 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.Type;
+
+import java.util.*;
+
+/**
+ * All Nodes in the Sea of Nodes IR inherit from the Node class.
+ * The Node class provides common functionality used by all subtypes.
+ * Subtypes of Node specialize by overriding methods.
+ */
+public abstract class Node {
+
+ /**
+ * Each node has a unique dense Node ID within a compilation context
+ * The ID is useful for debugging, for using as an offset in a bitvector,
+ * as well as for computing equality of nodes (to be implemented later).
+ */
+ public final int _nid;
+
+ /**
+ * Inputs to the node. These are use-def references to Nodes.
+ * <p>
+ * Generally fixed length, ordered, nulls allowed, no unused trailing space.
+ * Ordering is required because e.g. "a/b" is different from "b/a".
+ * The first input (offset 0) is often a {@link #isCFG} node.
+ */
+ public final ArrayList<Node> _inputs;
+
+ /**
+ * Outputs reference Nodes that are not null and have this Node as an
+ * input. These nodes are users of this node, thus these are def-use
+ * references to Nodes.
+ * <p>
+ * Outputs directly match inputs, making a directed graph that can be
+ * walked in either direction. These outputs are typically used for
+ * efficient optimizations but otherwise have no semantics meaning.
+ */
+ public final ArrayList<Node> _outputs;
+
+
+ /**
+ * Current computed type for this Node. This value changes as the graph
+ * changes and more knowledge is gained about the program.
+ */
+ public Type _type;
+
+ /**
+ * A private Global Static mutable counter, for unique node id generation.
+ * To make the compiler multithreaded, this field will have to move into a TLS.
+ * Starting with value 1, to avoid bugs confusing node ID 0 with uninitialized values.
+ * */
+ private static int UNIQUE_ID = 1;
+
+ protected Node(Node... inputs) {
+ _nid = UNIQUE_ID++; // allocate unique dense ID
+ _inputs = new ArrayList<>();
+ Collections.addAll(_inputs,inputs);
+ _outputs = new ArrayList<>();
+ for( Node n : _inputs )
+ if( n != null )
+ n.addUse( this );
+ }
+
+ // Easy reading label for debugger, e.g. "Add" or "Region" or "EQ"
+ public abstract String label();
+
+ // Unique label for graph visualization, e.g. "Add12" or "Region30" or "EQ99"
+ public String uniqueName() { return label() + _nid; }
+
+ // Graphical label, e.g. "+" or "Region" or "=="
+ public String glabel() { return label(); }
+
+ @Override
+ public final String toString() {
+ // TODO: This needs a lot of work
+ return print();
+ }
+
+ // This is a *deep* print. This version will fail on cycles, which we will
+ // correct later when we can parse programs with loops. We print with a
+ // tik-tok style; the common _print0 calls the per-Node _print1, which
+ // calls back to _print0;
+ public final String print() {
+ return _print0(new StringBuilder()).toString();
+ }
+ // This is the common print: check for DEAD and print "DEAD" else call the
+ // per-Node print1.
+ final StringBuilder _print0(StringBuilder sb) {
+ return isDead()
+ ? sb.append(uniqueName()).append(":DEAD")
+ : _print1(sb);
+ }
+ // Every Node implements this.
+ abstract StringBuilder _print1(StringBuilder sb);
+
+
+ /**
+ * Gets the ith input node
+ * @param i Offset of the input node
+ * @return Input node or null
+ */
+ public Node in(int i) { return _inputs.get(i); }
+
+ public int nIns() { return _inputs.size(); }
+
+ public int nOuts() { return _outputs.size(); }
+
+ public boolean isUnused() { return nOuts() == 0; }
+
+ public boolean isCFG() { return false; }
+
+ /**
+ * We allow disabling peephole opt so that we can observe the
+ * full graph, vs the optimized graph.
+ */
+ public static boolean _disablePeephole = false;
+
+ /**
+ * Try to peephole at this node and return a better replacement Node if
+ * possible. We compute a {@link Type} and then check and replace:
+ * <ul>
+ * <li>if the Type {@link Type#isConstant}, we replace with a {@link ConstantNode}</li>
+ * <li>in a future chapter we will look for a
+ * <a href="https://en.wikipedia.org/wiki/Common_subexpression_elimination">Common Subexpression</a>
+ * to eliminate.</li>
+ * <li>we ask the Node for a better replacement. The "better replacement"
+ * is things like {@code (1+2)} becomes {@code 3} and {@code (1+(x+2))} becomes
+ * {@code (x+(1+2))}. By canonicalizing expressions we fold common addressing
+ * math constants, remove algebraic identities and generally simplify the
+ * code. </li>
+ * </ul>
+ */
+ public final Node peephole( ) {
+ // Compute initial or improved Type
+ Type type = _type = compute();
+
+ if (_disablePeephole)
+ return this; // Peephole optimizations turned off
+
+ // Replace constant computations from non-constants with a constant node
+ if (!(this instanceof ConstantNode) && type.isConstant())
+ return deadCodeElim(new ConstantNode(type).peephole());
+
+ // Future chapter: Global Value Numbering goes here
+
+ // Ask each node for a better replacement
+ Node n = idealize();
+ if( n != null ) // Something changed
+ // Recursively optimize
+ return deadCodeElim(n.peephole());
+
+ return this; // No progress
+ }
+
+ // m is the new Node, self is the old.
+ // Return 'm', which may have zero uses but is alive nonetheless.
+ // If self has zero uses (and is not 'm'), {@link #kill} self.
+ private Node deadCodeElim(Node m) {
+ // If self is going dead and not being returned here (Nodes returned
+ // from peephole commonly have no uses (yet)), then kill self.
+ if( m != this && isUnused() ) {
+ // Killing self - and since self recursively kills self's inputs we
+ // might end up killing 'm', which we are returning as a live Node.
+ // So we add a bogus extra null output edge to stop kill().
+ m.addUse(null); // Add bogus null use to keep m alive
+ kill(); // Kill self because replacing with 'm'
+ m.delUse(null); // Remove bogus null.
+ }
+ return m;
+ }
+
+
+ /**
+ * Change a <em>def</em> into a Node. Keeps the edges correct, by removing
+ * the corresponding <em>use->def</em> edge. This may make the original
+ * <em>def</em> go dead. This function is co-recursive with {@link #kill}.
+ * <p>
+
+ * This method is the normal path for altering a Node, because it does the
+ * proper default edge maintenance. It also <em>immediately</em> kills
+ * Nodes that lose their last use; at times care must be taken to avoid
+ * killing Nodes that are being used without having an output Node. This
+ * definitely happens in the middle of recursive {@link #peephole} calls.
+ *
+ * @param idx which def to set
+ * @param new_def the new definition
+ * @return this for flow coding
+ */
+ Node set_def(int idx, Node new_def ) {
+ Node old_def = in(idx);
+ if( old_def == new_def ) return this; // No change
+ // If new def is not null, add the corresponding def->use edge
+ // This needs to happen before removing the old node's def->use edge as
+ // the new_def might get killed if the old node kills it recursively.
+ if( new_def != null )
+ new_def.addUse(this);
+ if( old_def != null && // If the old def exists, remove a def->use edge
+ old_def.delUse(this) ) // If we removed the last use, the old def is now dead
+ old_def.kill(); // Kill old def
+ // Set the new_def over the old (killed) edge
+ _inputs.set(idx,new_def);
+ // Return self for easy flow-coding
+ return this;
+ }
+
+ // Remove the numbered input, compressing the inputs in-place. This
+ // shuffles the order deterministically - which is suitable for Region and
+ // Phi, but not for every Node.
+ void delDef(int idx) {
+ Node old_def = in(idx);
+ if( old_def != null && // If the old def exists, remove a def->use edge
+ old_def.delUse(this) ) // If we removed the last use, the old def is now dead
+ old_def.kill(); // Kill old def
+ // Set the new_def over the old (killed) edge
+ int last = nIns()-1;
+ _inputs.set(idx,in(last));
+ _inputs.remove(last);
+ }
+
+ /**
+ * Add a new def to an existing Node. Keep the edges correct by
+ * adding the corresponding <em>def->use</em> edge.
+ *
+ * @param new_def the new definition, appended to the end of existing definitions
+ * @return new_def for flow coding
+ */
+ Node add_def(Node new_def) {
+ // Add use->def edge
+ _inputs.add(new_def);
+ // If new def is not null, add the corresponding def->use edge
+ if( new_def != null )
+ new_def.addUse(this);
+ return new_def;
+ }
+
+ // Breaks the edge invariants, used temporarily
+ protected <N extends Node> N addUse(Node n) { _outputs.add(n); return (N)this; }
+ // Shortcuts to stop DCE mid-parse
+ public <N extends Node> N keep() { return addUse(null); }
+ public Node unkeep() { delUse(null); return this; }
+
+ // Remove node 'use' from 'def's (i.e. our) output list, by compressing the list in-place.
+ // Return true if the output list is empty afterward.
+ // Error is 'use' does not exist; ok for 'use' to be null.
+ protected boolean delUse( Node use ) {
+ ArrayList<Node> outs = _outputs;
+ int lidx = outs.size()-1; // Last index
+ // This 1-line hack compresses an element out of an ArrayList
+ // without having to copy the contents. The last element is
+ // stuffed over the deleted element, and then the size is reduced.
+ outs.set(outs.indexOf(use),outs.get(lidx));
+ outs.remove(lidx); // Reduce ArrayList size without copying anything
+ return lidx==0;
+ }
+
+ // Shortcut for "popping" n nodes. A "pop" is basically a
+ // set_def(last,null) followed by lowering the nIns() count.
+ void pop_n(int n) {
+ for( int i=0; i<n; i++ ) {
+ Node old_def = _inputs.remove(_inputs.size()-1);
+ if( old_def != null && // If it exists and
+ old_def.delUse(this) ) // If we removed the last use, the old def is now dead
+ old_def.kill(); // Kill old def
+ }
+ }
+
+ /**
+ * Kill a Node with no <em>uses</em>, by setting all of its <em>defs</em>
+ * to null. This may recursively kill more Nodes and is basically dead
+ * code elimination. This function is co-recursive with {@link #pop_n}.
+ */
+ public void kill( ) {
+ assert isUnused(); // Has no uses, so it is dead
+ pop_n(nIns()); // Set all inputs to null, recursively killing unused Nodes
+ _type=null; // Flag as dead
+ assert isDead(); // Really dead now
+ }
+
+ // Mostly used for asserts and printing.
+ boolean isDead() { return isUnused() && nIns()==0 && _type==null; }
+
+ /**
+ * This function needs to be
+ * <a href="https://en.wikipedia.org/wiki/Monotonic_function">Monotonic</a>
+ * as it is part of a Monotone Analysis Framework.
+ * <a href="https://www.cse.psu.edu/~gxt29/teaching/cse597s21/slides/08monotoneFramework.pdf">See for example this set of slides</a>.
+ * <p>
+ * For Chapter 2, all our Types are really integer constants, and so all
+ * the needed properties are trivially true, and we can ignore the high
+ * theory. Much later on, this will become important and allow us to do
+ * many fancy complex optimizations trivially... because theory.
+ * <p>
+ * compute() needs to be stand-alone, and cannot recursively call compute
+ * on its inputs programs are cyclic (have loops!) and this will just
+ * infinitely recurse until stack overflow. Instead, compute typically
+ * computes a new type from the {@link #_type} field of its inputs.
+ */
+ public abstract Type compute();
+
+ /**
+ * This function rewrites the current Node into a more "idealized" form.
+ * This is the bulk of our peephole rewrite rules, and we use this to
+ * e.g. turn arbitrary collections of adds and multiplies with mixed
+ * constants into a normal form that's easy for hardware to implement.
+ * Example: An array addressing expression:
+ * <pre> ary[idx+1]</pre>
+ * might turn into Sea-of-Nodes IR:
+ * <pre> (ary+12)+((idx+1) * 4)</pre>
+ * This expression can then be idealized into:
+ * <pre> ary + ((idx*4) + (12 + (1*4)))</pre>
+ * And more folding:
+ * <pre> ary + ((idx<<2) + 16)</pre>
+ * And during code-gen:
+ * <pre> MOV4 Rary,Ridx,16 // or some such hardware-specific notation </pre>
+ * <p>
+ * {@link #idealize} has a very specific calling convention:
+ * <ul>
+ * <li>If NO change is made, return {@code null}
+ * <li>If ANY change is made, return not-null; this can be {@code this}
+ * <li>The returned Node does NOT call {@link #peephole} on itself; the {@link #peephole} call will recursively peephole it.
+ * <li>Any NEW nodes that are not directly returned DO call {@link #peephole}.
+ * </ul>
+ * <p>
+ * Examples:
+ * <table border="3">
+ * <tr><th> before </th><th> after </th><th>return </th><th>comment </th></tr>
+ * <tr><td>{@code (x+5) }</td><td>{@code (x+5) }</td><td>{@code null }</td><td>No change</td></tr>
+ * <tr><td>{@code (5+x) }</td><td>{@code (x+5) }</td><td>{@code this }</td><td>Swapped arguments</td></tr>
+ * <tr><td>{@code ((x+1)+2)}</td><td>{@code (x+(1+2))}</td><td>{@code (x+_) }</td><td>Returns 2 new Nodes</td></tr>
+ * </table>
+ *
+ * The last entry deserves more discussion. The new Node {@code (1+2)}
+ * created in {@link #idealize} calls {@link #peephole} (which then folds
+ * into a constant). The other new Node {@code (x+3)} does not call
+ * peephole, because it is returned and peephole itself will recursively
+ * call peephole.
+ * <p>
+ * Since idealize calls peephole and peephole calls idealize, you must be
+ * careful that all idealizations are <em>monotonic</em>: all transforms remove
+ * some feature, so that the set of available transforms always shrinks.
+ * If you don't, you risk an infinite peephole loop!
+ *
+ * @return Either a new or changed node, or null for no changes.
+ */
+ public abstract Node idealize();
+
+
+ // -----------------------
+ // Peephole utilities
+
+ // Swap inputs without letting either input go dead during the swap.
+ Node swap12() {
+ Node tmp = in(1);
+ _inputs.set(1,in(2));
+ _inputs.set(2,tmp);
+ return this;
+ }
+
+ // does this node contain all constants?
+ // Ignores in(0), as is usually control.
+ boolean all_cons() {
+ for( int i=1; i<nIns(); i++ )
+ if( !(in(i)._type.isConstant()) )
+ return false;
+ return true;
+ }
+
+ // Make a shallow copy (same class) of this Node, with given inputs and
+ // empty outputs and a new Node ID. The original inputs are ignored.
+ // Does not need to be implemented in isCFG() nodes.
+ Node copy(Node lhs, Node rhs) { throw Parser.TODO("Binary ops need to implement copy"); }
+
+ /**
+ * Used to allow repeating tests in the same JVM. This just resets the
+ * Node unique id generator, and is done as part of making a new Parser.
+ */
+ public static void reset() { UNIQUE_ID = 1; }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/NotNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/NotNode.java
new file mode 100644
index 00000000..5dd6a86e
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/NotNode.java
@@ -0,0 +1,28 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeInteger;
+
+public class NotNode extends Node {
+ public NotNode(Node in) { super(null, in); }
+
+ @Override public String label() { return "Not"; }
+
+ @Override public String glabel() { return "!"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ in(1)._print0(sb.append("(!"));
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ if( in(1)._type instanceof TypeInteger i0 )
+ return i0.isConstant() ? TypeInteger.constant(i0.value()==0 ? 1 : 0) : i0;
+ return Type.BOTTOM;
+ }
+
+ @Override
+ public Node idealize() { return null; }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/PhiNode.java
new file mode 100644
index 00000000..a5750922
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/PhiNode.java
@@ -0,0 +1,98 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+
+public class PhiNode extends Node {
+
+ final String _label;
+
+ public PhiNode(String label, Node... inputs) { super(inputs); _label = label; }
+
+ @Override public String label() { return "Phi_"+_label; }
+
+ @Override public String glabel() { return "&phi;_"+_label; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ sb.append("Phi(");
+ for( Node in : _inputs )
+ in._print0(sb).append(",");
+ sb.setLength(sb.length()-1);
+ sb.append(")");
+ return sb;
+ }
+
+ Node region() { return in(0); }
+
+ @Override
+ public Type compute() {
+ Type t = Type.TOP;
+ for (int i = 1; i < nIns(); i++)
+ t = t.meet(in(i)._type);
+ return t;
+ }
+
+ @Override
+ public Node idealize() {
+ // Remove a "junk" Phi: Phi(x,x) is just x
+ if( same_inputs() )
+ return in(1);
+
+ // If only 1 of our input values have live control
+ // then return that as phi is dead
+ Node live = singleLiveInput();
+ if (live != null)
+ return live;
+
+ // Pull "down" a common data op. One less op in the world. One more
+ // Phi, but Phis do not make code.
+ // Phi(op(A,B),op(Q,R),op(X,Y)) becomes
+ // op(Phi(A,Q,X), Phi(B,R,Y)).
+ Node op = in(1);
+ if( op.nIns()==3 && op.in(0)==null && !op.isCFG() && same_op() ) {
+ Node[] lhss = new Node[nIns()];
+ Node[] rhss = new Node[nIns()];
+ lhss[0] = rhss[0] = in(0); // Set Region
+ for( int i=1; i<nIns(); i++ ) {
+ lhss[i] = in(i).in(1);
+ rhss[i] = in(i).in(2);
+ }
+ Node phi_lhs = new PhiNode(_label,lhss).peephole();
+ Node phi_rhs = new PhiNode(_label,rhss).peephole();
+ return op.copy(phi_lhs,phi_rhs);
+ }
+
+ return null;
+ }
+
+ private boolean same_op() {
+ for( int i=2; i<nIns(); i++ )
+ if( in(1).getClass() != in(i).getClass() )
+ return false;
+ return true;
+ }
+
+ private boolean same_inputs() {
+ for( int i=2; i<nIns(); i++ )
+ if( in(1) != in(i) )
+ return false;
+ return true;
+ }
+
+ /**
+ * If only 1 of the inputs is live then return it
+ */
+ private Node singleLiveInput() {
+ Node live = null;
+ for( int i=1; i<nIns(); i++ )
+ // The control type is in the region
+ if( region().in(i)._type != Type.XCONTROL )
+ if (live == null)
+ live = in(i);
+ else
+ return null;
+ return live;
+ }
+
+
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/ProjNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/ProjNode.java
new file mode 100644
index 00000000..424a1e9b
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/ProjNode.java
@@ -0,0 +1,50 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeTuple;
+
+public class ProjNode extends Node {
+
+ // Which slice of the incoming multi-part value
+ public final int _idx;
+
+ // Debugging label
+ public final String _label;
+
+ public ProjNode(MultiNode ctrl, int idx, String label) {
+ super(ctrl);
+ _idx = idx;
+ _label = label;
+ }
+
+ @Override
+ public String label() { return _label; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) { return sb.append(_label); }
+
+ @Override public boolean isCFG() { return _idx==0 || ctrl() instanceof IfNode; }
+
+ public MultiNode ctrl() { return (MultiNode)in(0); }
+
+ @Override
+ public Type compute() {
+ Type t = ctrl()._type;
+ return getType(t);
+ }
+
+ private Type getType(Type t) {
+ return t instanceof TypeTuple tt ? tt._types[_idx] : Type.BOTTOM;
+ }
+
+ @Override
+ public Node idealize() {
+ if( ctrl()._type instanceof TypeTuple tt ) {
+ if( tt._types[_idx]==Type.XCONTROL )
+ return new ConstantNode(Type.XCONTROL).peephole(); // We are dead
+ if( tt._types[1-_idx]==Type.XCONTROL ) // Only true for IfNodes
+ return ctrl().in(0); // We become our input control
+ }
+ return null;
+ }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/RegionNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/RegionNode.java
new file mode 100644
index 00000000..1ddf520b
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/RegionNode.java
@@ -0,0 +1,56 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.Type;
+
+public class RegionNode extends Node {
+ public RegionNode(Node... inputs) { super(inputs); }
+
+ @Override
+ public String label() { return "Region"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ return sb.append("Region").append(_nid);
+ }
+
+ @Override public boolean isCFG() { return true; }
+
+ @Override
+ public Type compute() {
+ Type t = Type.XCONTROL;
+ for (int i = 1; i < nIns(); i++)
+ t = t.meet(in(i)._type);
+ return t;
+ }
+
+ @Override
+ public Node idealize() {
+ int path = findDeadInput();
+ if( path != 0 ) {
+ for( Node phi : _outputs )
+ if( phi instanceof PhiNode )
+ phi.delDef(path);
+ delDef(path);
+
+ // If down to a single input, become that input - but also make all
+ // Phis an identity on *their* single input.
+ if( nIns()==2 ) {
+ for( Node phi : _outputs )
+ if( phi instanceof PhiNode )
+ // Currently does not happen, because no loops
+ throw Parser.TODO();
+ return in(1);
+ }
+ return this;
+ }
+ return null;
+ }
+
+ private int findDeadInput() {
+ for( int i=1; i<nIns(); i++ )
+ if( in(i)._type==Type.XCONTROL )
+ return i;
+ return 0; // All inputs alive
+ }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/ReturnNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
new file mode 100644
index 00000000..ba2b83ac
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
@@ -0,0 +1,44 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.*;
+
+/**
+ * The Return node has two inputs. The first input is a control node and the
+ * second is the data node that supplies the return value.
+ * <p>
+ * In this presentation, Return functions as a Stop node, since multiple <code>return</code> statements are not possible.
+ * The Stop node will be introduced in Chapter 6 when we implement <code>if</code> statements.
+ * <p>
+ * The Return's output is the value from the data node.
+ */
+public class ReturnNode extends Node {
+
+ public ReturnNode(Node ctrl, Node data) {
+ super(ctrl, data);
+ }
+
+ public Node ctrl() { return in(0); }
+ public Node expr() { return in(1); }
+
+ @Override
+ public String label() { return "Return"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ return expr()._print0(sb.append("return ")).append(";");
+ }
+
+ @Override public boolean isCFG() { return true; }
+
+ @Override
+ public Type compute() {
+ return new TypeTuple(ctrl()._type,expr()._type);
+ }
+
+ @Override
+ public Node idealize() {
+ if( ctrl()._type==Type.XCONTROL )
+ return ctrl();
+ return null;
+ }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/ScopeNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
new file mode 100644
index 00000000..eecab9f9
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
@@ -0,0 +1,138 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.Type;
+
+import java.util.*;
+
+/**
+ * The Scope node is a purely parser helper - it tracks names to nodes with a
+ * stack of scopes.
+ */
+public class ScopeNode extends Node {
+
+ /**
+ * Stack of lexical scopes, each scope is a symbol table
+ * that binds variable names to Nodes.
+ * The top of this stack represents current scope.
+ */
+ public final Stack<HashMap<String, Integer>> _scopes = new Stack<>();
+
+ // Reverse lookup for nicer graph printing
+ private final ArrayList<String> _rlabels = new ArrayList<>();
+
+ @Override public String label() { return "Scope"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ sb.append(label());
+ for( HashMap<String,Integer> scope : _scopes ) {
+ sb.append("[");
+ boolean first=true;
+ for( String name : scope.keySet() ) {
+ if( !first ) sb.append(", ");
+ first=false;
+ sb.append(name).append(":");
+ Node n = in(scope.get(name));
+ if( n==null ) sb.append("null");
+ else n._print0(sb);
+ }
+ sb.append("]");
+ }
+ return sb;
+ }
+
+ @Override
+ public Type compute() { return Type.BOTTOM; }
+
+ @Override public Node idealize() { return null; }
+
+ // Add an empty lexical scope
+ public HashMap<String, Integer> push() {
+ return _scopes.push(new HashMap<>());
+ }
+
+ // Remove the current lexical scope, killing all unused nodes.
+ public void pop() {
+ HashMap<String,Integer> scope = _scopes.pop();
+ pop_n(scope.size());
+ }
+
+ // Create a new name in the current scope
+ public Node define( String name, Node n ) {
+ HashMap<String,Integer> scope = _scopes.lastElement();
+ if( scope.put(name,nIns()) != null )
+ return null; // Double define
+ _rlabels.add(name);
+ return add_def(n);
+ }
+
+ // Lookup a name in all scopes
+ public Node lookup(String name) {
+ for (int i = _scopes.size() - 1; i >= 0; i--) {
+ var idx = _scopes.get(i).get(name);
+ if (idx != null) return in(idx);
+ }
+ return null;
+ }
+
+ public Node ctrl() { assert lookup(Parser.CTRL)==in(0); return in(0); }
+
+ public Node ctrl(Node n) {
+ assert lookup(Parser.CTRL)==in(0);
+ set_def(0,n);
+ return n;
+ }
+
+
+ // If the name is present in any scope, then redefine
+ public Node update(String name, Node n) {
+ for (int i = _scopes.size() - 1; i >= 0; i--) {
+ HashMap<String, Integer> scope = _scopes.get(i);
+ Integer idx = scope.get(name);
+ if (idx != null) // Found prior def
+ return set_def(idx,n); // Update def in scope
+ }
+ return null;
+ }
+
+ /**
+ * Duplicate a ScopeNode; including all levels, up to Nodes. So this is
+ * neither shallow (would dup the Scope but not the internal HashMap
+ * tables), nor deep (would dup the Scope, the HashMap tables, but then
+ * also the program Nodes).
+ * <p>
+ * The new Scope is a full-fledged Node with proper use<->def edges.
+ */
+ public ScopeNode dup() {
+ // Our goals are:
+ // 1) duplicate the name bindings of the ScopeNode across all stack levels
+ // 2) Make the new ScopeNode a user of all the nodes bound
+ // 3) Ensure that the order of defs is the same to allow easy merging
+ ScopeNode dup = new ScopeNode();
+ for( HashMap<String, Integer> tab : _scopes )
+ dup._scopes.push(new HashMap<>(tab));
+ for( int i=0; i<nIns(); i++ )
+ dup.add_def(in(i));
+ dup._rlabels.addAll(_rlabels);
+ return dup;
+ }
+
+ /**
+ * Merges the names whose node bindings differ, by creating Phi node for such names
+ * The names could occur at all stack levels, but a given name can only differ in the
+ * innermost stack level where the name is bound.
+ *
+ * @param that The ScopeNode to be merged into this
+ * @return A new node representing the merge point
+ */
+ public Node mergeScopes(ScopeNode that) {
+ RegionNode r = (RegionNode) ctrl(new RegionNode(null,ctrl(), that.ctrl()).keep());
+ for (int i = 1; i < nIns(); i++)
+ if (in(i) != that.in(i)) // No need for redundant Phis
+ set_def(i, new PhiNode(_rlabels.get(i), r, in(i), that.in(i)).peephole());
+ that.kill(); // Kill merged scope
+ return r.unkeep().peephole();
+ }
+
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/StartNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/StartNode.java
new file mode 100644
index 00000000..85c6cd34
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/StartNode.java
@@ -0,0 +1,37 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeTuple;
+
+/**
+ * The Start node represents the start of the function. For now, we do not
+ * have any inputs to Start because our function does not yet accept
+ * parameters. When we add parameters the value of Start will be a tuple, and
+ * will require Projections to extract the values. We discuss this in detail
+ * in Chapter 9: Functions and Calls.
+ */
+public class StartNode extends MultiNode {
+
+ final TypeTuple _args;
+
+ public StartNode(Type[] args) {
+ super();
+ _args = new TypeTuple(args);
+ }
+
+ @Override
+ public String label() { return "Start"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ return sb.append(label());
+ }
+
+ @Override public boolean isCFG() { return true; }
+
+ @Override
+ public TypeTuple compute() { return _args; }
+
+ @Override
+ public Node idealize() { return null; }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/StopNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/StopNode.java
new file mode 100644
index 00000000..f17c7ec7
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/StopNode.java
@@ -0,0 +1,51 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+
+public class StopNode extends Node {
+ public StopNode(Node... inputs) {
+ super(inputs);
+ }
+
+ @Override
+ public String label() {
+ return "Stop";
+ }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ if( ret()!=null ) return ret()._print0(sb);
+ sb.append("Stop[ ");
+ for( Node ret : _inputs )
+ ret._print0(sb).append(" ");
+ return sb.append("]");
+ }
+
+ @Override public boolean isCFG() { return true; }
+
+ // If a single Return, return it.
+ // Otherwise, null because ambiguous.
+ public ReturnNode ret() {
+ return nIns()==1 ? (ReturnNode)in(0) : null;
+ }
+
+ @Override
+ public Type compute() {
+ return Type.BOTTOM;
+ }
+
+ @Override
+ public Node idealize() {
+ int len = nIns();
+ for( int i=0; i<nIns(); i++ )
+ if( in(i)._type==Type.XCONTROL )
+ delDef(i--);
+ if( len != nIns() ) return this;
+ return null;
+ }
+
+ public Node addReturn(Node node) {
+ return add_def(node);
+ }
+
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter06/src/main/java/com/seaofnodes/simple/node/SubNode.java
new file mode 100644
index 00000000..41d7d092
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -0,0 +1,41 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeInteger;
+
+public class SubNode extends Node {
+ public SubNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
+
+ @Override public String label() { return "Sub"; }
+
+ @Override public String glabel() { return "-"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb) {
+ in(1)._print0(sb.append("("));
+ in(2)._print0(sb.append("-"));
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ if (in(1)._type instanceof TypeInteger i0 &&
+ in(2)._type instanceof TypeInteger i1) {
+ if (i0.isConstant() && i1.isConstant())
+ return TypeInteger.constant(i0.value()-i1.value());
+ return i0.meet(i1);
+ }
+ return Type.BOTTOM;
+ }
+
+ @Override
+ public Node idealize() {
+ // Sub of same is 0
+ if( in(1)==in(2) )
+ return new ConstantNode(TypeInteger.constant(0));
+
+ return null;
+ }
+
+ @Override Node copy(Node lhs, Node rhs) { return new SubNode(lhs,rhs); }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/type/Type.java b/chapter06/src/main/java/com/seaofnodes/simple/type/Type.java
new file mode 100644
index 00000000..78908375
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/type/Type.java
@@ -0,0 +1,77 @@
+package com.seaofnodes.simple.type;
+
+
+/**
+ * These types are part of a Monotone Analysis Framework,
+ * @see <a href="https://www.cse.psu.edu/~gxt29/teaching/cse597s21/slides/08monotoneFramework.pdf">see for example this set of slides</a>.
+ * <p>
+ * The types form a lattice; @see <a href="https://en.wikipedia.org/wiki/Lattice_(order)">a symmetric complete bounded (ranked) lattice.</a>
+ * <p>
+ * This wild lattice theory will be needed later to allow us to easily beef up
+ * the analysis and optimization of the Simple compiler... but we don't need it
+ * now, just know that it is coming along in a later Chapter.
+ * <p>g
+ * One of the fun things here is that while the theory is deep and subtle, the
+ * actual implementation is darn near trivial and is generally really obvious
+ * what we're doing with it. Right now, it's just simple integer math to do
+ * simple constant folding e.g. 1+2 == 3 stuff.
+ */
+
+public class Type {
+
+ // ----------------------------------------------------------
+ // Simple types are implemented fully here. "Simple" means: the code and
+ // type hierarchy are simple, not that the Type is conceptually simple.
+ static final byte TBOT = 0; // Bottom (ALL)
+ static final byte TTOP = 1; // Top (ANY)
+ static final byte TCTRL = 2; // Ctrl flow bottom
+ static final byte TXCTRL = 3; // Ctrl flow top (mini-lattice: any-xctrl-ctrl-all)
+ static final byte TSIMPLE = 4; // End of the Simple Types
+ static final byte TINT = 5; // All Integers; see TypeInteger
+ static final byte TTUPLE = 6; // Tuples; finite collections of unrelated Types, kept in parallel
+
+ public final byte _type;
+
+ public boolean is_simple() { return _type < TSIMPLE; }
+ private static final String[] STRS = new String[]{"BOTTOM","TOP","CONTROL","~CONTROL"};
+ protected Type(byte type) { _type = type; }
+
+ public static final Type BOTTOM = new Type( TBOT ); // ALL
+ public static final Type TOP = new Type( TTOP ); // ANY
+ public static final Type CONTROL = new Type( TCTRL ); // Ctrl
+ public static final Type XCONTROL = new Type( TXCTRL ); // ~Ctrl
+
+ public boolean isConstant() { return _type == TTOP || _type == TXCTRL; }
+
+ public StringBuilder _print(StringBuilder sb) {return is_simple() ? sb.append(STRS[_type]) : sb;}
+
+ public final Type meet(Type t) {
+ // Shortcut for the self case
+ if( t == this ) return this;
+ // Same-type is always safe in the subclasses
+ if( _type==t._type ) return xmeet(t);
+ // Reverse; xmeet 2nd arg is never "is_simple" and never equal to "this".
+ if( is_simple() ) return this.xmeet(t );
+ if( t.is_simple() ) return t .xmeet(this);
+ return BOTTOM; // Mixing 2 unrelated types
+ }
+
+ // Compute meet right now. Overridden in subclasses.
+ // Handle cases where 'this.is_simple()' and unequal to 't'.
+ // Subclassed xmeet calls can assert that '!t.is_simple()'.
+ protected Type xmeet(Type t) {
+ assert is_simple(); // Should be overridden in subclass
+ // ANY meet anything is thing; thing meet ALL is ALL
+ if( _type==TBOT || t._type==TTOP ) return this;
+ if( _type==TTOP || t._type==TBOT ) return t;
+ // 'this' is {TCTRL,TXCTRL}
+ if( !t.is_simple() ) return BOTTOM;
+ // 't' is {TCTRL,TXCTRL}
+ return _type==TCTRL || t._type==TCTRL ? CONTROL : XCONTROL;
+ }
+
+ @Override
+ public final String toString() {
+ return _print(new StringBuilder()).toString();
+ }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/type/TypeInteger.java b/chapter06/src/main/java/com/seaofnodes/simple/type/TypeInteger.java
new file mode 100644
index 00000000..93f5eae3
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/type/TypeInteger.java
@@ -0,0 +1,63 @@
+package com.seaofnodes.simple.type;
+
+/**
+ * Integer Type
+ */
+public class TypeInteger extends Type {
+
+ public final static TypeInteger TOP = new TypeInteger(false, 0);
+ public final static TypeInteger BOT = new TypeInteger(false, 1);
+ public final static TypeInteger ZERO= new TypeInteger(true, 0);
+
+ private final boolean _is_con;
+
+ /**
+ * The constant value or
+ * if not constant then 0=bottom, 1=top.
+ */
+ private final long _con;
+
+ public TypeInteger(boolean is_con, long con) {
+ super(TINT);
+ _is_con = is_con;
+ _con = con;
+ }
+
+ public static TypeInteger constant(long con) { return new TypeInteger(true, con); }
+
+ public boolean isTop() { return !_is_con && _con==0; }
+ public boolean isBot() { return !_is_con && _con==1; }
+
+ @Override
+ public StringBuilder _print(StringBuilder sb) {
+ if (isTop()) return sb.append("IntTop");
+ if (isBot()) return sb.append("IntBot");
+ return sb.append(_con);
+ }
+
+ @Override
+ public boolean isConstant() { return _is_con; }
+
+ public long value() { return _con; }
+
+ @Override
+ public Type xmeet(Type other) {
+ if( this==other ) return this;
+ if (!(other instanceof TypeInteger i)) return super.meet(other);
+ // BOT wins
+ if ( isBot() ) return this;
+ if ( i.isBot() ) return i ;
+ // TOP loses
+ if ( i.isTop() ) return this;
+ if ( isTop() ) return i ;
+ assert isConstant() && i.isConstant();
+ return _con==i._con ? this : TypeInteger.BOT;
+ }
+
+ @Override
+ public boolean equals( Object o ) {
+ if( o==this ) return true;
+ if( !(o instanceof TypeInteger i) ) return false;
+ return _con==i._con && _is_con==i._is_con;
+ }
+}
diff --git a/chapter06/src/main/java/com/seaofnodes/simple/type/TypeTuple.java b/chapter06/src/main/java/com/seaofnodes/simple/type/TypeTuple.java
new file mode 100644
index 00000000..2e6152a8
--- /dev/null
+++ b/chapter06/src/main/java/com/seaofnodes/simple/type/TypeTuple.java
@@ -0,0 +1,32 @@
+package com.seaofnodes.simple.type;
+
+public class TypeTuple extends Type {
+
+ public final Type[] _types;
+
+ public TypeTuple(Type... _types) {
+ super(TTUPLE);
+ this._types = _types;
+ }
+
+ @Override
+ public Type xmeet(Type other) {
+ throw new UnsupportedOperationException("Meet on Tuple Type not yet implemented");
+ }
+
+ @Override
+ public StringBuilder _print(StringBuilder sb) {
+ sb.append("[ ");
+ for( Type t : _types )
+ t._print(sb).append(",");
+ sb.setLength(sb.length()-1);
+ sb.append("]");
+ return sb;
+ }
+
+ public static final TypeTuple IF_BOTH = new TypeTuple(new Type[]{Type.CONTROL, Type.CONTROL});
+ public static final TypeTuple IF_NEITHER = new TypeTuple(new Type[]{Type.XCONTROL,Type.XCONTROL});
+ public static final TypeTuple IF_TRUE = new TypeTuple(new Type[]{Type.CONTROL, Type.XCONTROL});
+ public static final TypeTuple IF_FALSE = new TypeTuple(new Type[]{Type.XCONTROL,Type.CONTROL});
+
+}
diff --git a/chapter06/src/test/java/com/seaofnodes/simple/Chapter06Test.java b/chapter06/src/test/java/com/seaofnodes/simple/Chapter06Test.java
new file mode 100644
index 00000000..801f8d3f
--- /dev/null
+++ b/chapter06/src/test/java/com/seaofnodes/simple/Chapter06Test.java
@@ -0,0 +1,795 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.TypeInteger;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class Chapter06Test {
+
+ @Test
+ public void testChapter6PeepholeReturn() {
+ Parser parser = new Parser(
+"""
+if( true ) return 2;
+return 1;
+""");
+ StopNode stop = parser.parse(true);
+ assertEquals("return 2;", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ }
+
+ @Test
+ public void testChapter6PeepholeRotate() {
+ Parser parser = new Parser(
+"""
+int a = 1;
+if (arg)
+ a = 2;
+return (arg < a) < 3;
+""");
+ StopNode stop = parser.parse(false);
+ assertEquals("return ((arg<Phi(Region12,2,1))<3);", stop.toString());
+ }
+
+ @Test
+ public void testChapter6PeepholeCFG() {
+ Parser parser = new Parser(
+"""
+int a=1;
+if( true )
+ a=2;
+else
+ a=3;
+return a;
+""");
+ StopNode stop = parser.parse(true);
+ assertEquals("return 2;", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ }
+
+ @Test
+ public void testChapter6IfIf() {
+ Parser parser = new Parser(
+"""
+int a=1;
+if( arg!=1 )
+ a=2;
+else
+ a=3;
+int b=4;
+if( a==2 )
+ b=42;
+else
+ b=5;
+return b;""", TypeInteger.BOT);
+ StopNode stop = parser.parse(true);
+ assertEquals("return Phi(Region32,42,5);", stop.toString());
+ }
+
+ @Test
+ public void testChapter6IfArgIf() {
+ Parser parser = new Parser(
+"""
+int a=1;
+if( 1==1 )
+ a=2;
+else
+ a=3;
+int b=4;
+if( arg==2 )
+ b=a;
+else
+ b=5;
+return b;""", TypeInteger.BOT);
+ StopNode stop = parser.parse(true);
+ assertEquals("return Phi(Region28,2,5);", stop.toString());
+ }
+
+ @Test
+ public void testChapter6Merge3With2() {
+ Parser parser = new Parser(
+"""
+int a=1;
+if( arg==1 )
+ if( arg==2 )
+ a=2;
+ else
+ a=3;
+else if( arg==3 )
+ a=4;
+else
+ a=5;
+return a;
+#showGraph;""", TypeInteger.constant(2));
+ StopNode stop = parser.parse();
+ assertEquals("return 5;", stop.toString());
+ }
+
+ @Test
+ public void testChapter6Merge3With1() {
+ Parser parser = new Parser(
+"""
+int a=1;
+if( arg==1 )
+ if( arg==2 )
+ a=2;
+ else
+ a=3;
+else if( arg==3 )
+ a=4;
+else
+ a=5;
+return a;
+""", TypeInteger.constant(1));
+ StopNode stop = parser.parse(true);
+ assertEquals("return 3;", stop.toString());
+ }
+
+ @Test
+ public void testChapter6Merge3Peephole() {
+ Parser parser = new Parser(
+"""
+int a=1;
+if( arg==1 )
+ if( 1==2 )
+ a=2;
+ else
+ a=3;
+else if( arg==3 )
+ a=4;
+else
+ a=5;
+return a;
+""", TypeInteger.BOT);
+ StopNode stop = parser.parse(true);
+ assertEquals("return Phi(Region36,3,Phi(Region34,4,5));", stop.toString());
+ }
+
+ @Test
+ public void testChapter6Merge3Peephole1() {
+ Parser parser = new Parser(
+"""
+int a=1;
+if( arg==1 )
+ if( 1==2 )
+ a=2;
+ else
+ a=3;
+else if( arg==3 )
+ a=4;
+else
+ a=5;
+return a;
+""", TypeInteger.constant(1));
+ StopNode stop = parser.parse(true);
+ assertEquals("return 3;", stop.toString());
+ }
+
+ @Test
+ public void testChapter6Merge3Peephole3() {
+ Parser parser = new Parser(
+"""
+int a=1;
+if( arg==1 )
+ if( 1==2 )
+ a=2;
+ else
+ a=3;
+else if( arg==3 )
+ a=4;
+else
+ a=5;
+return a;
+""", TypeInteger.constant(3));
+ StopNode stop = parser.parse(true);
+ assertEquals("return 4;", stop.toString());
+ }
+
+ @Test
+ public void testChapter6Demo1NonConst() {
+ Parser parser = new Parser("""
+int a = 0;
+int b = 1;
+if( arg ) {
+ a = 2;
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b;
+""");
+ StopNode ret = parser.parse(true);
+ assertEquals("return (Phi(Region21,Phi(Region19,2,3),1)+Phi(Region21,2,0));", ret.toString());
+ }
+
+
+ @Test
+ public void testChapter6Demo1True() {
+ Parser parser = new Parser("""
+int a = 0;
+int b = 1;
+if( arg ) {
+ a = 2;
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b;
+""", TypeInteger.constant(1));
+ StopNode ret = parser.parse(true);
+ assertEquals("return 4;", ret.toString());
+ }
+
+ @Test
+ public void testChapter6Demo1False() {
+ Parser parser = new Parser("""
+int a = 0;
+int b = 1;
+if( arg ) {
+ a = 2;
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b;
+""", TypeInteger.constant(0));
+ StopNode ret = parser.parse(true);
+ assertEquals("return 1;", ret.toString());
+ }
+
+ @Test
+ public void testChapter6Demo2NonConst() {
+ Parser parser = new Parser("""
+int a = 0;
+int b = 1;
+int c = 0;
+if( arg ) {
+ a = 1;
+ if( arg==2 ) { c=2; } else { c=3; }
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b+c;
+""");
+ StopNode ret = parser.parse(true);
+ assertEquals("return ((Phi(Region32,Phi(Region22,2,3),0)+Phi(Region32,Phi(Region30,2,3),1))+Phi(Region32,1,0));", ret.toString());
+ }
+
+
+ @Test
+ public void testChapter6Demo2True() {
+ Parser parser = new Parser("""
+int a = 0;
+int b = 1;
+int c = 0;
+if( arg ) {
+ a = 1;
+ if( arg==2 ) { c=2; } else { c=3; }
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b+c;
+""", TypeInteger.constant(1));
+ StopNode ret = parser.parse(true);
+ assertEquals("return 6;", ret.toString());
+ }
+
+ @Test
+ public void testChapter6Demo2arg2() {
+ Parser parser = new Parser("""
+int a = 0;
+int b = 1;
+int c = 0;
+if( arg ) {
+ a = 1;
+ if( arg==2 ) { c=2; } else { c=3; }
+ if( arg ) { b = 2; }
+ else b = 3;
+}
+return a+b+c;
+""", TypeInteger.constant(2));
+ StopNode ret = parser.parse(true);
+ assertEquals("return 5;", ret.toString());
+ }
+
+
+
+ @Test
+ public void testChapter5IfStmt() {
+ Parser parser = new Parser(
+"""
+int a = 1;
+if (arg == 1)
+ a = arg+2;
+else {
+ a = arg-3;
+ #showGraph;
+}
+#showGraph;
+return a;""");
+ StopNode ret = parser.parse(true);
+ assertEquals("return Phi(Region17,(arg+2),(arg-3));", ret.toString());
+ }
+
+ @Test
+ public void testChapter5Test() {
+ Parser parser = new Parser(
+"""
+int c = 3;
+int b = 2;
+if (arg == 1) {
+ b = 3;
+ c = 4;
+}
+return c;""", TypeInteger.BOT);
+ StopNode ret = parser.parse(true);
+ assertEquals("return Phi(Region16,4,3);", ret.toString());
+ }
+
+ @Test
+ public void testChapter5Return2() {
+ Parser parser = new Parser(
+"""
+if( arg==1 )
+ return 3;
+else
+ return 4;
+#showGraph;""", TypeInteger.BOT);
+ StopNode stop = parser.parse();
+ assertEquals("Stop[ return 3; return 4; ]", stop.toString());
+ }
+
+ @Test
+ public void testChapter5IfMergeB() {
+ Parser parser = new Parser(
+"""
+int a=arg+1;
+int b=0;
+if( arg==1 )
+ b=a;
+else
+ b=a+1;
+return a+b;""");
+ StopNode ret = parser.parse(true);
+ assertEquals("return ((arg*2)+Phi(Region20,2,3));", ret.toString());
+ }
+
+ @Test
+ public void testChapter5IfMerge2() {
+ Parser parser = new Parser(
+"""
+int a=arg+1;
+int b=arg+2;
+if( arg==1 )
+ b=b+a;
+else
+ a=b+1;
+return a+b;""");
+ StopNode ret = parser.parse(true);
+ assertEquals("return ((Phi(Region31,(arg*2),arg)+arg)+Phi(Region31,4,5));", ret.toString());
+ }
+
+ @Test
+ public void testChapter5Merge3() {
+ Parser parser = new Parser(
+"""
+int a=1;
+if( arg==1 )
+ if( arg==2 )
+ a=2;
+ else
+ a=3;
+else if( arg==3 )
+ a=4;
+else
+ a=5;
+return a;
+#showGraph;""", TypeInteger.BOT);
+ StopNode stop = parser.parse();
+ assertEquals("return Phi(Region33,Phi(Region21,2,3),Phi(Region31,4,5));", stop.toString());
+ }
+
+ @Test
+ public void testChapter5Merge4() {
+ Parser parser = new Parser(
+"""
+int a=0;
+int b=0;
+if( arg )
+ a=1;
+if( arg==0 )
+ b=2;
+return arg+a+b;
+#showGraph;""", TypeInteger.BOT);
+ StopNode stop = parser.parse();
+ assertEquals("return ((arg+Phi(Region13,1,0))+Phi(Region22,2,0));", stop.toString());
+ }
+
+ @Test
+ public void testChapter5Merge5() {
+ Parser parser = new Parser(
+"""
+int a=arg==2;
+if( arg==1 )
+{
+ a=arg==3;
+}
+return a;""");
+ StopNode ret = parser.parse(true);
+ assertEquals("return (arg==Phi(Region16,3,2));", ret.toString());
+ }
+
+ @Test
+ public void testChapter5True() {
+ StopNode stop = new Parser("return true;").parse();
+ assertEquals("return 1;",stop.toString());
+ }
+
+ @Test
+ public void testChapter5HalfDef() {
+ try {
+ new Parser("if( arg==1 ) int b=2; return b;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Cannot define a new name on one arm of an if",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testChapter5HalfDef2() {
+ try {
+ new Parser("if( arg==1 ) { int b=2; } else { int b=3; } return b;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Undefined name 'b'",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testChapter5BadNum() {
+ try {
+ new Parser("return 1-;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error, expected an identifier or expression: ;",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testChapter5Keyword1() {
+ try {
+ new Parser("int true=0; return true;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Expected an identifier, found 'true'",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testChapter5Keyword2() {
+ try {
+ new Parser("int else=arg; if(else) else=2; else else=1; return else;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Expected an identifier, found 'else'",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testChapter5Keyword3() {
+ try {
+ new Parser("int a=1; ififif(arg)inta=2;return a;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error, expected =: (",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testChapter4Peephole() {
+ Parser parser = new Parser("return 1+arg+2; #showGraph;");
+ StopNode ret = parser.parse();
+ assertEquals("return (arg+3);", ret.print());
+ }
+
+ @Test
+ public void testChapter4Peephole2() {
+ Parser parser = new Parser("return (1+arg)+2;");
+ StopNode ret = parser.parse();
+ assertEquals("return (arg+3);", ret.print());
+ }
+
+ @Test
+ public void testChapter4Add0() {
+ Parser parser = new Parser("return 0+arg;");
+ StopNode ret = parser.parse();
+ assertEquals("return arg;", ret.print());
+ }
+
+ @Test
+ public void testChapter4AddAddMul() {
+ Parser parser = new Parser("return arg+0+arg;");
+ StopNode ret = parser.parse();
+ assertEquals("return (arg*2);", ret.print());
+ }
+
+ @Test
+ public void testChapter4Peephole3() {
+ Parser parser = new Parser("return 1+arg+2+arg+3; #showGraph;");
+ StopNode ret = parser.parse();
+ assertEquals("return ((arg*2)+6);", ret.print());
+ }
+
+ @Test
+ public void testChapter4Mul1() {
+ Parser parser = new Parser("return 1*arg;");
+ StopNode ret = parser.parse();
+ assertEquals("return arg;", ret.print());
+ }
+
+ @Test
+ public void testChapter4VarArg() {
+ Parser parser = new Parser("return arg; #showGraph;");
+ StopNode stop = parser.parse();
+ ReturnNode ret = stop.ret();
+ assertTrue(ret.in(0) instanceof ProjNode);
+ assertTrue(ret.in(1) instanceof ProjNode);
+ }
+
+ @Test
+ public void testChapter4ConstantArg() {
+ Parser parser = new Parser("return arg; #showGraph;", TypeInteger.constant(2));
+ StopNode ret = parser.parse();
+ assertEquals("return 2;", ret.print());
+ }
+
+ @Test
+ public void testChapter4CompEq() {
+ Parser parser = new Parser("return 3==3; #showGraph;");
+ StopNode ret = parser.parse();
+ assertEquals("return 1;", ret.print());
+ }
+
+ @Test
+ public void testChapter4CompEq2() {
+ Parser parser = new Parser("return 3==4; #showGraph;");
+ StopNode ret = parser.parse();
+ assertEquals("return 0;", ret.print());
+ }
+
+ @Test
+ public void testChapter4CompNEq() {
+ Parser parser = new Parser("return 3!=3; #showGraph;");
+ StopNode ret = parser.parse();
+ assertEquals("return 0;", ret.print());
+ }
+
+ @Test
+ public void testChapter4CompNEq2() {
+ Parser parser = new Parser("return 3!=4; #showGraph;");
+ StopNode ret = parser.parse();
+ assertEquals("return 1;", ret.print());
+ }
+
+ @Test
+ public void testChapter4Bug1() {
+ Parser parser = new Parser("int a=arg+1; int b=a; b=1; return a+2; #showGraph;");
+ StopNode ret = parser.parse();
+ assertEquals("return (arg+3);", ret.print());
+ }
+
+ @Test
+ public void testChapter4Bug2() {
+ Parser parser = new Parser("int a=arg+1; a=a; return a; #showGraph;");
+ StopNode ret = parser.parse();
+ assertEquals("return (arg+1);", ret.print());
+ }
+
+ @Test
+ public void testChapter4Bug3() {
+ try {
+ new Parser("inta=1; return a;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Undefined name 'inta'",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testChapter4Bug4() {
+ Parser parser = new Parser("return -arg;");
+ StopNode ret = parser.parse();
+ assertEquals("return (-arg);", ret.print());
+ }
+
+ @Test
+ public void testVarDecl() {
+ Parser parser = new Parser("int a=1; return a;");
+ StopNode ret = parser.parse();
+ assertEquals("return 1;", ret.print());
+ }
+
+ @Test
+ public void testVarAdd() {
+ Parser parser = new Parser("int a=1; int b=2; return a+b;");
+ StopNode ret = parser.parse();
+ assertEquals("return 3;", ret.print());
+ }
+
+ @Test
+ public void testVarScope() {
+ Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; } return c;");
+ StopNode ret = parser.parse();
+ assertEquals("return 4;", ret.print());
+ }
+
+ @Test
+ public void testVarScopeNoPeephole() {
+ Node._disablePeephole = true;
+ Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; #showGraph; } return c; #showGraph;");
+ StopNode ret = parser.parse();
+ Node._disablePeephole = false;
+ assertEquals("return (1+3);", ret.print());
+ }
+
+ @Test
+ public void testVarDist() {
+ Parser parser = new Parser("int x0=1; int y0=2; int x1=3; int y1=4; return (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1); #showGraph;");
+ StopNode ret = parser.parse();
+ assertEquals("return 8;", ret.print());
+ }
+
+ @Test
+ public void testSelfAssign() {
+ try {
+ new Parser("int a=a; return a;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Undefined name 'a'",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testChapter2ParseGrammar() {
+ Node._disablePeephole = true; // disable peephole so we can observe full graph
+ Parser parser = new Parser("return 1+2*3+-5;");
+ StopNode ret = parser.parse();
+ assertEquals("return (1+((2*3)+(-5)));", ret.print());
+ GraphVisualizer gv = new GraphVisualizer();
+ System.out.println(gv.generateDotOutput(parser));
+ Node._disablePeephole = false;
+ }
+
+ @Test
+ public void testChapter2AddPeephole() {
+ Parser parser = new Parser("return 1+2;");
+ StopNode ret = parser.parse();
+ assertEquals("return 3;", ret.print());
+ }
+
+ @Test
+ public void testChapter2SubPeephole() {
+ Parser parser = new Parser("return 1-2;");
+ StopNode ret = parser.parse();
+ assertEquals("return -1;", ret.print());
+ }
+
+ @Test
+ public void testChapter2MulPeephole() {
+ Parser parser = new Parser("return 2*3;");
+ StopNode ret = parser.parse();
+ assertEquals("return 6;", ret.print());
+ }
+
+ @Test
+ public void testChapter2DivPeephole() {
+ Parser parser = new Parser("return 6/3;");
+ StopNode ret = parser.parse();
+ assertEquals("return 2;", ret.print());
+ }
+
+ @Test
+ public void testChapter2MinusPeephole() {
+ Parser parser = new Parser("return 6/-3;");
+ StopNode ret = parser.parse();
+ assertEquals("return -2;", ret.print());
+ }
+
+ @Test
+ public void testChapter2Example() {
+ Parser parser = new Parser("return 1+2*3+-5;");
+ StopNode ret = parser.parse();
+ assertEquals("return 2;", ret.print());
+ GraphVisualizer gv = new GraphVisualizer();
+ System.out.println(gv.generateDotOutput(parser));
+ }
+
+ @Test
+ public void testSimpleProgram() {
+ Parser parser = new Parser("return 1;");
+ StopNode stop = parser.parse();
+ StartNode start = Parser.START;
+ ReturnNode ret = (ReturnNode)stop.in(0);
+
+ assertTrue(ret.ctrl() instanceof ProjNode);
+ Node expr = ret.expr();
+ if( expr instanceof ConstantNode con ) {
+ assertEquals(start,con.in(0));
+ assertEquals(TypeInteger.constant(1), con._type);
+ } else {
+ fail();
+ }
+ }
+
+ @Test
+ public void testZero() {
+ Parser parser = new Parser("return 0;");
+ parser.parse();
+ StartNode start = Parser.START;
+ for( Node use : start._outputs )
+ if( use instanceof ConstantNode con )
+ assertEquals(TypeInteger.constant(0),con._type);
+ }
+
+ @Test
+ public void testBad1() {
+ try {
+ new Parser("ret").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error, expected =: ",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testBad2() {
+ try {
+ new Parser("return 0123;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error: integer values cannot start with '0'",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testNotBad3() {
+ // this test used to fail in chapter 1
+ assertEquals("return 12;", new Parser("return --12;").parse().print());
+ }
+
+ @Test
+ public void testBad4() {
+ try {
+ new Parser("return 100").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error, expected ;: ",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testNotBad5() {
+ // this test used to fail in chapter 1
+ assertEquals("return -100;", new Parser("return -100;").parse().print());
+ }
+
+ @Test
+ public void testBad6() {
+ try {
+ new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error, expected }: ",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testBad7() {
+ try {
+ new Parser("return 1;}").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error, unexpected }",e.getMessage());
+ }
+ }
+
+}