summaryrefslogtreecommitdiff
path: root/chapter04/docs
diff options
context:
space:
mode:
Diffstat (limited to 'chapter04/docs')
-rw-r--r--chapter04/docs/04-grammar.md78
-rw-r--r--chapter04/docs/04-post-peephole.gv41
-rw-r--r--chapter04/docs/04-post-peephole.svg110
-rw-r--r--chapter04/docs/04-pre-peephole.gv45
-rw-r--r--chapter04/docs/04-pre-peephole.svg134
5 files changed, 408 insertions, 0 deletions
diff --git a/chapter04/docs/04-grammar.md b/chapter04/docs/04-grammar.md
new file mode 100644
index 00000000..eca2e649
--- /dev/null
+++ b/chapter04/docs/04-grammar.md
@@ -0,0 +1,78 @@
+# Grammar for Chapter 1
+
+```antlrv4
+grammar SimpleLanguage;
+
+program
+ : statement+ EOF
+ ;
+
+statement
+ : returnStatement
+ | declStatement
+ | blockStatment
+ | expressionStatement
+ | metaStatement
+ ;
+
+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/chapter04/docs/04-post-peephole.gv b/chapter04/docs/04-post-peephole.gv
new file mode 100644
index 00000000..22f55468
--- /dev/null
+++ b/chapter04/docs/04-post-peephole.gv
@@ -0,0 +1,41 @@
+digraph chapter04 {
+/*
+return 1+arg+2; #showGraph;
+*/
+ rankdir=BT;
+ subgraph cluster_Nodes {
+ Start1 [ 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>>
+ ];
+ Con_9 [ label="#3" ];
+ Add10 [ label="Add" ];
+ Return11 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ }
+ node [shape=plaintext];
+ subgraph cluster_0 {
+ scope0 [label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD BGCOLOR="cyan">0</TD><TD PORT="scope0_$ctrl">$ctrl</TD><TD PORT="scope0_arg">arg</TD></TR>
+ </TABLE>>];
+ subgraph cluster_1 {
+ scope1 [label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD BGCOLOR="cyan">1</TD></TR>
+ </TABLE>>];
+ }
+ }
+ Add10 -> Start1:p1;
+ Add10 -> Con_9;
+ Return11 -> Start1:p0 [color=red];
+ Return11 -> Add10;
+ edge [style=dashed color=cornflowerblue];
+ scope0:"scope0_$ctrl" -> Start1:p0;
+ scope0:"scope0_arg" -> Start1:p1;
+} \ No newline at end of file
diff --git a/chapter04/docs/04-post-peephole.svg b/chapter04/docs/04-post-peephole.svg
new file mode 100644
index 00000000..5a45722c
--- /dev/null
+++ b/chapter04/docs/04-post-peephole.svg
@@ -0,0 +1,110 @@
+<?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: chapter04 Pages: 1 -->
+<svg width="417pt" height="248pt"
+ viewBox="0.00 0.00 417.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>chapter04</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-244 413,-244 413,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-232 193,-232 193,-8 8,-8"/>
+</g>
+<g id="clust2" class="cluster">
+<title>cluster_0</title>
+<polygon fill="none" stroke="black" points="201,-72 201,-140 401,-140 401,-72 201,-72"/>
+</g>
+<g id="clust3" class="cluster">
+<title>cluster_1</title>
+<polygon fill="none" stroke="black" points="323,-80 323,-132 393,-132 393,-80 323,-80"/>
+</g>
+<!-- Start1 -->
+<g id="node1" class="node">
+<title>Start1</title>
+<polygon fill="yellow" stroke="transparent" points="97,-195 97,-220 178,-220 178,-195 97,-195"/>
+<polygon fill="none" stroke="black" points="97,-195 97,-220 178,-220 178,-195 97,-195"/>
+<text text-anchor="start" x="119" y="-203.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="97,-164 97,-195 178,-195 178,-164 97,-164"/>
+<polygon fill="yellow" stroke="transparent" points="102,-169 102,-190 142,-190 142,-169 102,-169"/>
+<polygon fill="none" stroke="black" points="102,-169 102,-190 142,-190 142,-169 102,-169"/>
+<text text-anchor="start" x="105" y="-175.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+<polygon fill="none" stroke="black" points="142,-169 142,-190 173,-190 173,-169 142,-169"/>
+<text text-anchor="start" x="145" y="-175.8" font-family="Times,serif" font-size="14.00">arg</text>
+</g>
+<!-- Con_9 -->
+<g id="node2" class="node">
+<title>Con_9</title>
+<ellipse fill="none" stroke="black" cx="43" cy="-192" rx="27" ry="18"/>
+<text text-anchor="middle" x="43" y="-188.3" font-family="Times,serif" font-size="14.00">#3</text>
+</g>
+<!-- Add10 -->
+<g id="node3" class="node">
+<title>Add10</title>
+<ellipse fill="none" stroke="black" cx="156" cy="-106" rx="28.7" ry="18"/>
+<text text-anchor="middle" x="156" y="-102.3" font-family="Times,serif" font-size="14.00">Add</text>
+</g>
+<!-- Add10&#45;&gt;Start1 -->
+<g id="edge1" class="edge">
+<title>Add10&#45;&gt;Start1:p1</title>
+<path fill="none" stroke="black" d="M156.86,-124.09C157.28,-133.54 157.73,-145.77 157.92,-157.68"/>
+<polygon fill="black" stroke="black" points="154.42,-158.03 158,-168 161.42,-157.97 154.42,-158.03"/>
+</g>
+<!-- Add10&#45;&gt;Con_9 -->
+<g id="edge2" class="edge">
+<title>Add10&#45;&gt;Con_9</title>
+<path fill="none" stroke="black" d="M136.93,-119.61C121.35,-130.08 98.91,-145.53 80,-160 75.7,-163.29 71.22,-166.89 66.91,-170.44"/>
+<polygon fill="black" stroke="black" points="64.38,-168 58.96,-177.1 68.87,-173.37 64.38,-168"/>
+</g>
+<!-- Return11 -->
+<g id="node4" class="node">
+<title>Return11</title>
+<polygon fill="yellow" stroke="black" points="163,-52 97,-52 97,-16 163,-16 163,-52"/>
+<text text-anchor="middle" x="130" y="-30.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Return11&#45;&gt;Start1 -->
+<g id="edge3" class="edge">
+<title>Return11&#45;&gt;Start1:p0</title>
+<path fill="none" stroke="red" d="M123.24,-52.04C121.12,-58.23 119.05,-65.33 118,-72 113.28,-101.85 115.86,-109.85 118,-140 118.58,-148.23 120.12,-152.3 121.11,-157.68"/>
+<polygon fill="red" stroke="red" points="117.66,-158.34 122,-168 124.63,-157.74 117.66,-158.34"/>
+</g>
+<!-- Return11&#45;&gt;Add10 -->
+<g id="edge4" class="edge">
+<title>Return11&#45;&gt;Add10</title>
+<path fill="none" stroke="black" d="M136.43,-52.3C139.4,-60.3 142.99,-69.98 146.29,-78.85"/>
+<polygon fill="black" stroke="black" points="143.06,-80.22 149.83,-88.38 149.63,-77.78 143.06,-80.22"/>
+</g>
+<!-- scope0 -->
+<g id="node5" class="node">
+<title>scope0</title>
+<polygon fill="cyan" stroke="transparent" points="218,-95 218,-116 234,-116 234,-95 218,-95"/>
+<polygon fill="none" stroke="black" points="218,-95 218,-116 234,-116 234,-95 218,-95"/>
+<text text-anchor="start" x="221" y="-101.8" font-family="Times,serif" font-size="14.00">0</text>
+<polygon fill="none" stroke="black" points="234,-95 234,-116 274,-116 274,-95 234,-95"/>
+<text text-anchor="start" x="237" y="-101.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+<polygon fill="none" stroke="black" points="274,-95 274,-116 305,-116 305,-95 274,-95"/>
+<text text-anchor="start" x="277" y="-101.8" font-family="Times,serif" font-size="14.00">arg</text>
+</g>
+<!-- scope0&#45;&gt;Start1 -->
+<g id="edge5" class="edge">
+<title>scope0:scope0_$ctrl&#45;&gt;Start1:p0</title>
+<path fill="none" stroke="cornflowerblue" stroke-dasharray="5,2" d="M254,-117C254,-176.09 137.5,-113.66 123.39,-158.03"/>
+<polygon fill="cornflowerblue" stroke="cornflowerblue" points="119.92,-157.61 122,-168 126.85,-158.58 119.92,-157.61"/>
+</g>
+<!-- scope0&#45;&gt;Start1 -->
+<g id="edge6" class="edge">
+<title>scope0:scope0_arg&#45;&gt;Start1:p1</title>
+<path fill="none" stroke="cornflowerblue" stroke-dasharray="5,2" d="M290,-117C290,-172.03 239,-178.4 184.25,-178.95"/>
+<polygon fill="cornflowerblue" stroke="cornflowerblue" points="183.98,-175.45 174,-179 184.02,-182.45 183.98,-175.45"/>
+</g>
+<!-- scope1 -->
+<g id="node6" class="node">
+<title>scope1</title>
+<polygon fill="cyan" stroke="transparent" points="350,-95 350,-116 366,-116 366,-95 350,-95"/>
+<polygon fill="none" stroke="black" points="350,-95 350,-116 366,-116 366,-95 350,-95"/>
+<text text-anchor="start" x="353" y="-101.8" font-family="Times,serif" font-size="14.00">1</text>
+</g>
+</g>
+</svg>
diff --git a/chapter04/docs/04-pre-peephole.gv b/chapter04/docs/04-pre-peephole.gv
new file mode 100644
index 00000000..e8016c87
--- /dev/null
+++ b/chapter04/docs/04-pre-peephole.gv
@@ -0,0 +1,45 @@
+digraph chapter04 {
+/*
+return 1+arg+2; #showGraph;
+*/
+ rankdir=BT;
+ subgraph cluster_Nodes {
+ Start1 [ 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>>
+ ];
+ Con_4 [ label="#1" ];
+ Con_5 [ label="#2" ];
+ Add6 [ label="Add" ];
+ Add7 [ label="Add" ];
+ Return8 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ }
+ node [shape=plaintext];
+ subgraph cluster_0 {
+ scope0 [label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD BGCOLOR="cyan">0</TD><TD PORT="scope0_$ctrl">$ctrl</TD><TD PORT="scope0_arg">arg</TD></TR>
+ </TABLE>>];
+ subgraph cluster_1 {
+ scope1 [label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD BGCOLOR="cyan">1</TD></TR>
+ </TABLE>>];
+ }
+ }
+ Add6 -> Start1:p1;
+ Add6 -> Con_5;
+ Add7 -> Con_4;
+ Add7 -> Add6;
+ Return8 -> Start1:p0 [color=red];
+ Return8 -> Add7;
+ edge [style=dashed color=cornflowerblue];
+ scope0:"scope0_$ctrl" -> Start1:p0;
+ scope0:"scope0_arg" -> Start1:p1;
+}
diff --git a/chapter04/docs/04-pre-peephole.svg b/chapter04/docs/04-pre-peephole.svg
new file mode 100644
index 00000000..b1ab4eee
--- /dev/null
+++ b/chapter04/docs/04-pre-peephole.svg
@@ -0,0 +1,134 @@
+<?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: chapter04 Pages: 1 -->
+<svg width="417pt" height="320pt"
+ viewBox="0.00 0.00 417.00 320.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 316)">
+<title>chapter04</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-316 413,-316 413,4 -4,4"/>
+<g id="clust1" class="cluster">
+<title>cluster_Nodes</title>
+<polygon fill="none" stroke="black" points="8,-8 8,-304 193,-304 193,-8 8,-8"/>
+</g>
+<g id="clust2" class="cluster">
+<title>cluster_0</title>
+<polygon fill="none" stroke="black" points="201,-144 201,-212 401,-212 401,-144 201,-144"/>
+</g>
+<g id="clust3" class="cluster">
+<title>cluster_1</title>
+<polygon fill="none" stroke="black" points="323,-152 323,-204 393,-204 393,-152 323,-152"/>
+</g>
+<!-- Start1 -->
+<g id="node1" class="node">
+<title>Start1</title>
+<polygon fill="yellow" stroke="transparent" points="97,-267 97,-292 178,-292 178,-267 97,-267"/>
+<polygon fill="none" stroke="black" points="97,-267 97,-292 178,-292 178,-267 97,-267"/>
+<text text-anchor="start" x="119" y="-275.8" font-family="Times,serif" font-size="14.00">Start</text>
+<polygon fill="none" stroke="black" points="97,-236 97,-267 178,-267 178,-236 97,-236"/>
+<polygon fill="yellow" stroke="transparent" points="102,-241 102,-262 142,-262 142,-241 102,-241"/>
+<polygon fill="none" stroke="black" points="102,-241 102,-262 142,-262 142,-241 102,-241"/>
+<text text-anchor="start" x="105" y="-247.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+<polygon fill="none" stroke="black" points="142,-241 142,-262 173,-262 173,-241 142,-241"/>
+<text text-anchor="start" x="145" y="-247.8" font-family="Times,serif" font-size="14.00">arg</text>
+</g>
+<!-- Con_4 -->
+<g id="node2" class="node">
+<title>Con_4</title>
+<ellipse fill="none" stroke="black" cx="119" cy="-178" rx="27" ry="18"/>
+<text text-anchor="middle" x="119" y="-174.3" font-family="Times,serif" font-size="14.00">#1</text>
+</g>
+<!-- Con_5 -->
+<g id="node3" class="node">
+<title>Con_5</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>
+<!-- Add6 -->
+<g id="node4" class="node">
+<title>Add6</title>
+<ellipse fill="none" stroke="black" cx="45" cy="-178" rx="28.7" ry="18"/>
+<text text-anchor="middle" x="45" y="-174.3" font-family="Times,serif" font-size="14.00">Add</text>
+</g>
+<!-- Add6&#45;&gt;Start1 -->
+<g id="edge1" class="edge">
+<title>Add6&#45;&gt;Start1:p1</title>
+<path fill="none" stroke="black" d="M59.56,-193.83C66.26,-200.06 74.59,-207 83,-212 110.12,-228.12 148.12,-209.69 156.37,-230.12"/>
+<polygon fill="black" stroke="black" points="152.92,-230.7 158,-240 159.83,-229.56 152.92,-230.7"/>
+</g>
+<!-- Add6&#45;&gt;Con_5 -->
+<g id="edge2" class="edge">
+<title>Add6&#45;&gt;Con_5</title>
+<path fill="none" stroke="black" d="M44.6,-196C44.33,-207.31 43.97,-222.42 43.66,-235.43"/>
+<polygon fill="black" stroke="black" points="40.15,-235.66 43.41,-245.74 47.15,-235.82 40.15,-235.66"/>
+</g>
+<!-- Add7 -->
+<g id="node5" class="node">
+<title>Add7</title>
+<ellipse fill="none" stroke="black" cx="117" cy="-106" rx="28.7" ry="18"/>
+<text text-anchor="middle" x="117" y="-102.3" font-family="Times,serif" font-size="14.00">Add</text>
+</g>
+<!-- Add7&#45;&gt;Con_4 -->
+<g id="edge3" class="edge">
+<title>Add7&#45;&gt;Con_4</title>
+<path fill="none" stroke="black" d="M117.49,-124.3C117.71,-132.02 117.98,-141.29 118.23,-149.89"/>
+<polygon fill="black" stroke="black" points="114.73,-150 118.51,-159.9 121.72,-149.8 114.73,-150"/>
+</g>
+<!-- Add7&#45;&gt;Add6 -->
+<g id="edge4" class="edge">
+<title>Add7&#45;&gt;Add6</title>
+<path fill="none" stroke="black" d="M102.08,-121.5C92.04,-131.27 78.62,-144.31 67.32,-155.3"/>
+<polygon fill="black" stroke="black" points="64.63,-153.03 59.9,-162.51 69.51,-158.05 64.63,-153.03"/>
+</g>
+<!-- Return8 -->
+<g id="node6" class="node">
+<title>Return8</title>
+<polygon fill="yellow" stroke="black" points="167,-52 101,-52 101,-16 167,-16 167,-52"/>
+<text text-anchor="middle" x="134" y="-30.3" font-family="Times,serif" font-size="14.00">Return</text>
+</g>
+<!-- Return8&#45;&gt;Start1 -->
+<g id="edge5" class="edge">
+<title>Return8&#45;&gt;Start1:p0</title>
+<path fill="none" stroke="red" d="M142.72,-52.17C147.37,-62.31 152.62,-75.56 155,-88 165.33,-142.13 178.15,-161.99 155,-212 148.81,-225.36 132.76,-223.4 125.57,-230.38"/>
+<polygon fill="red" stroke="red" points="122.2,-229.41 122,-240 128.76,-231.84 122.2,-229.41"/>
+</g>
+<!-- Return8&#45;&gt;Add7 -->
+<g id="edge6" class="edge">
+<title>Return8&#45;&gt;Add7</title>
+<path fill="none" stroke="black" d="M129.8,-52.3C127.9,-60.1 125.62,-69.49 123.52,-78.17"/>
+<polygon fill="black" stroke="black" points="120.11,-77.35 121.15,-87.9 126.92,-79 120.11,-77.35"/>
+</g>
+<!-- scope0 -->
+<g id="node7" class="node">
+<title>scope0</title>
+<polygon fill="cyan" stroke="transparent" points="218,-167 218,-188 234,-188 234,-167 218,-167"/>
+<polygon fill="none" stroke="black" points="218,-167 218,-188 234,-188 234,-167 218,-167"/>
+<text text-anchor="start" x="221" y="-173.8" font-family="Times,serif" font-size="14.00">0</text>
+<polygon fill="none" stroke="black" points="234,-167 234,-188 274,-188 274,-167 234,-167"/>
+<text text-anchor="start" x="237" y="-173.8" font-family="Times,serif" font-size="14.00">$ctrl</text>
+<polygon fill="none" stroke="black" points="274,-167 274,-188 305,-188 305,-167 274,-167"/>
+<text text-anchor="start" x="277" y="-173.8" font-family="Times,serif" font-size="14.00">arg</text>
+</g>
+<!-- scope0&#45;&gt;Start1 -->
+<g id="edge7" class="edge">
+<title>scope0:scope0_$ctrl&#45;&gt;Start1:p0</title>
+<path fill="none" stroke="cornflowerblue" stroke-dasharray="5,2" d="M254,-189C254,-248.09 137.5,-185.66 123.39,-230.03"/>
+<polygon fill="cornflowerblue" stroke="cornflowerblue" points="119.92,-229.61 122,-240 126.85,-230.58 119.92,-229.61"/>
+</g>
+<!-- scope0&#45;&gt;Start1 -->
+<g id="edge8" class="edge">
+<title>scope0:scope0_arg&#45;&gt;Start1:p1</title>
+<path fill="none" stroke="cornflowerblue" stroke-dasharray="5,2" d="M290,-189C290,-244.03 239,-250.4 184.25,-250.95"/>
+<polygon fill="cornflowerblue" stroke="cornflowerblue" points="183.98,-247.45 174,-251 184.02,-254.45 183.98,-247.45"/>
+</g>
+<!-- scope1 -->
+<g id="node8" class="node">
+<title>scope1</title>
+<polygon fill="cyan" stroke="transparent" points="350,-167 350,-188 366,-188 366,-167 350,-167"/>
+<polygon fill="none" stroke="black" points="350,-167 350,-188 366,-188 366,-167 350,-167"/>
+<text text-anchor="start" x="353" y="-173.8" font-family="Times,serif" font-size="14.00">1</text>
+</g>
+</g>
+</svg>