From 6347fa6dc2ee8d3060f2f0db21f03f11d374b0bc Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Tue, 29 Sep 2026 12:34:58 -0700 Subject: Squashed commit of the following: commit 3ba6686fc991998a0a420709dfb0012bbddf77ab Author: Cliff Click Date: Tue Sep 29 12:21:46 2026 -0700 Backport lazy memory splits to ch10, split into ch10a and b commit fa7d56f72455828826d0ab7e1c142ca814e6e2aa Author: Cliff Click Date: Tue Sep 29 10:10:53 2026 -0700 Reverse edges commit e1bba17ff54400a34ede0eaacad863420d021119 Author: Cliff Click Date: Tue Sep 29 09:44:41 2026 -0700 Split ch10 Basic memory introduction in 10a, then precise aliases in 10b. --- chapter10a/docs/10-grammar.md | 125 +++++++++ chapter10a/docs/Makefile | 14 + chapter10a/docs/example1.gv | 10 + chapter10a/docs/example1.svg | 73 +++++ chapter10a/docs/example2a.gv | 10 + chapter10a/docs/example2a.svg | 85 ++++++ chapter10a/docs/example2b.gv | 11 + chapter10a/docs/example2b.svg | 74 +++++ chapter10a/docs/example2c.gv | 9 + chapter10a/docs/example2c.svg | 50 ++++ chapter10a/docs/lattice.gv | 108 ++++++++ chapter10a/docs/lattice.svg | 570 +++++++++++++++++++++++++++++++++++++++ chapter10a/docs/type-control.svg | 5 + chapter10a/docs/type-integer.svg | 5 + chapter10a/docs/type-memory.svg | 5 + chapter10a/docs/type-pointer.svg | 5 + chapter10a/docs/type-struct.svg | 5 + chapter10a/docs/type-tuple.svg | 5 + 18 files changed, 1169 insertions(+) create mode 100644 chapter10a/docs/10-grammar.md create mode 100644 chapter10a/docs/Makefile create mode 100644 chapter10a/docs/example1.gv create mode 100644 chapter10a/docs/example1.svg create mode 100644 chapter10a/docs/example2a.gv create mode 100644 chapter10a/docs/example2a.svg create mode 100644 chapter10a/docs/example2b.gv create mode 100644 chapter10a/docs/example2b.svg create mode 100644 chapter10a/docs/example2c.gv create mode 100644 chapter10a/docs/example2c.svg create mode 100644 chapter10a/docs/lattice.gv create mode 100644 chapter10a/docs/lattice.svg create mode 100644 chapter10a/docs/type-control.svg create mode 100644 chapter10a/docs/type-integer.svg create mode 100644 chapter10a/docs/type-memory.svg create mode 100644 chapter10a/docs/type-pointer.svg create mode 100644 chapter10a/docs/type-struct.svg create mode 100644 chapter10a/docs/type-tuple.svg (limited to 'chapter10a/docs') diff --git a/chapter10a/docs/10-grammar.md b/chapter10a/docs/10-grammar.md new file mode 100644 index 00000000..5b9b680e --- /dev/null +++ b/chapter10a/docs/10-grammar.md @@ -0,0 +1,125 @@ +# Grammar for Chapter 10a + +```antlrv4 +grammar SimpleLanguage; + +program + : statement+ EOF + ; + +statement + : returnStatement + | structDeclaration + | declStatement + | blockStatment + | expressionStatement + | ifStatement + | whileStatement + | breakStatement + | continueStatment + ; + +field + : 'int' IDENTIFIER ';' + ; + +fields + : field+ + ; + +structDeclaration + : 'struct' IDENTIFIER '{' fields '}' + ; + +whileStatement + : 'while' '(' expression ')' statement + ; + +breakStatement + : 'break' ';' + ; + +continueStatement + : 'continue' ';' + ; + +ifStatement + : 'if' '(' expression ')' statement ('else' statement)? + ; + + +expressionStatement + : IDENTIFIER '=' expression ';' + | fieldExpression '=' expression ';' + ; + +blockStatement + : '{' statement+ '}' + ; + +structName + : IDENTIFIER + ; + +declStatement + : 'int' IDENTIFIER '=' expression ';' + | structName ('?')? IDENTIFIER '=' expression ';' + ; + +returnStatement + : 'return' expression ';' + ; + +expression + : comparisonExpression + ; + +comparisonExpression + : additiveExpression (('==' | '!='| '>'| '<'| '>='| '<=') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-') unaryExpression + | '!' unaryExpression + | primaryExpression + ; + +newExpression + : 'new' IDENTIFIER + ; + +fieldExpression + : primaryExpresson '.' IDENTIFIER + ; + +primaryExpression + : IDENTIFIER + | INTEGER_LITERAL + | 'true' + | 'false' + | 'null' + | newExpression + | '(' expression ')' + | fieldExpression + ; + +INTEGER_LITERAL + : [1-9][0-9]* + | [0] + ; + +IDENTIFIER + : NON_DIGIT (NON_DIGIT | DIGIT)* + ; + +NON_DIGIT: [a-zA-Z_]; +DEC_DIGIT: [0-9]; +``` diff --git a/chapter10a/docs/Makefile b/chapter10a/docs/Makefile new file mode 100644 index 00000000..055882b2 --- /dev/null +++ b/chapter10a/docs/Makefile @@ -0,0 +1,14 @@ +GV := $(wildcard *.gv) +SVG := $(patsubst %.gv,%.svg,$(GV)) +.DELETE_ON_ERROR: + +.PHONY: all +all: $(SVG) + +%.svg: %.gv + dot -Tsvg $< > $@ + @sed -i 's/\r$$//' $@ + +.PHONY: clean +clean: + @rm -f $(SVG) diff --git a/chapter10a/docs/example1.gv b/chapter10a/docs/example1.gv new file mode 100644 index 00000000..d399b562 --- /dev/null +++ b/chapter10a/docs/example1.gv @@ -0,0 +1,10 @@ +digraph Memory { + rankdir=RL; + node [shape=box]; + M0 [label="Initial memory"]; + X [label="Store p.x = 3"]; + Y [label="Store p.y = arg"]; + L [label="Load p.x"]; + L -> Y -> X -> M0 [color=blue]; + Return -> Y [color=blue]; Return -> L; +} diff --git a/chapter10a/docs/example1.svg b/chapter10a/docs/example1.svg new file mode 100644 index 00000000..d1c5e3bd --- /dev/null +++ b/chapter10a/docs/example1.svg @@ -0,0 +1,73 @@ + + + + + + +Memory + + + +M0 + +Initial memory + + + +X + +Store p.x = 3 + + + +X->M0 + + + + + +Y + +Store p.y = arg + + + +Y->X + + + + + +L + +Load p.x + + + +L->Y + + + + + +Return + +Return + + + +Return->Y + + + + + +Return->L + + + + + diff --git a/chapter10a/docs/example2a.gv b/chapter10a/docs/example2a.gv new file mode 100644 index 00000000..ec5afbdc --- /dev/null +++ b/chapter10a/docs/example2a.gv @@ -0,0 +1,10 @@ +digraph Memory { + rankdir=RL; + node [shape=box]; + M [label="Incoming memory"]; + A [label="True: Store x"]; + B [label="False: Store y"]; + P [label="Phi(memory)"]; + P -> A -> M [color=blue]; P -> B -> M [color=blue]; + P -> Region [style=dashed]; Return -> P [color=blue]; +} diff --git a/chapter10a/docs/example2a.svg b/chapter10a/docs/example2a.svg new file mode 100644 index 00000000..f9a9b4fd --- /dev/null +++ b/chapter10a/docs/example2a.svg @@ -0,0 +1,85 @@ + + + + + + +Memory + + + +M + +Incoming memory + + + +A + +True: Store x + + + +A->M + + + + + +B + +False: Store y + + + +B->M + + + + + +P + +Phi(memory) + + + +P->A + + + + + +P->B + + + + + +Region + +Region + + + +P->Region + + + + + +Return + +Return + + + +Return->P + + + + + diff --git a/chapter10a/docs/example2b.gv b/chapter10a/docs/example2b.gv new file mode 100644 index 00000000..3f3309e5 --- /dev/null +++ b/chapter10a/docs/example2b.gv @@ -0,0 +1,11 @@ +digraph Memory { + rankdir=RL; + node [shape=box]; + P [label="Phi(memory)"]; + S [label="Loop-body Store"]; + P -> Entry [color=blue]; + P -> S [color=blue,constraint=false]; // Backedge must not place Store before Phi. + S -> P [color=blue]; + P -> Loop [style=dashed]; Return -> P [color=blue]; + Return -> S [style=invis]; // Layout only: Phi, Store, Return from left to right. +} diff --git a/chapter10a/docs/example2b.svg b/chapter10a/docs/example2b.svg new file mode 100644 index 00000000..8fff33d4 --- /dev/null +++ b/chapter10a/docs/example2b.svg @@ -0,0 +1,74 @@ + + + + + + +Memory + + + +P + +Phi(memory) + + + +S + +Loop-body Store + + + +P->S + + + + + +Entry + +Entry + + + +P->Entry + + + + + +Loop + +Loop + + + +P->Loop + + + + + +S->P + + + + + +Return + +Return + + + +Return->P + + + + + + diff --git a/chapter10a/docs/example2c.gv b/chapter10a/docs/example2c.gv new file mode 100644 index 00000000..ef8a5fc8 --- /dev/null +++ b/chapter10a/docs/example2c.gv @@ -0,0 +1,9 @@ +digraph Memory { + rankdir=RL; + node [shape=box]; + M [label="Prior memory"]; + L [label="Load p.x"]; + S [label="Store p.x"]; + L -> M [color=blue]; S -> M [color=blue]; + S -> L [style=dashed,label="execute load first"]; +} diff --git a/chapter10a/docs/example2c.svg b/chapter10a/docs/example2c.svg new file mode 100644 index 00000000..c7b37912 --- /dev/null +++ b/chapter10a/docs/example2c.svg @@ -0,0 +1,50 @@ + + + + + + +Memory + + + +M + +Prior memory + + + +L + +Load p.x + + + +L->M + + + + + +S + +Store p.x + + + +S->M + + + + + +S->L + + +execute load first + + + diff --git a/chapter10a/docs/lattice.gv b/chapter10a/docs/lattice.gv new file mode 100644 index 00000000..733a5287 --- /dev/null +++ b/chapter10a/docs/lattice.gv @@ -0,0 +1,108 @@ +digraph Lattice { + rankdir=TB; + newrank=true; + // Preserve the same-rank ordering edges through cluster layout. + remincross=false; + nodesep=0.22; + ranksep=0.65; + node [shape=ellipse, style=filled, fontname="DejaVu Sans", fontsize=13]; + edge [color="#555555", arrowsize=0.6]; + + // These arrows are lattice-order edges, not use/definition edges. + TOP [label="⊤", fillcolor=black, fontcolor=white]; + BOT [label="⊥", fillcolor=black, fontcolor=white]; + + subgraph cluster_control { + label="Control"; fontname="Arial"; color="#cccccc"; + node [fillcolor="#ffff00"]; + XCTRL [label="⊤:ctrl"]; + CTRL [label="⊥:ctrl"]; + XCTRL -> CTRL; + } + + subgraph cluster_integer { + label="Integer"; fontname="Arial"; color="#cccccc"; + node [fillcolor="#add8e6"]; + INT_TOP [label="⊤:int"]; + INT_BOT [label="⊥:int"]; + INT_NEG [label="… −2, −1"]; + INT_ZERO [label="0"]; + INT_POS [label="1, 2, …"]; + {rank=same; INT_NEG; INT_ZERO; INT_POS;} + INT_NEG -> INT_ZERO -> INT_POS [style=invis]; + INT_TOP -> {INT_NEG INT_ZERO INT_POS}; + {INT_NEG INT_ZERO INT_POS} -> INT_BOT; + } + + subgraph cluster_pointer { + label="Pointer = nullability × struct type"; + fontname="Arial"; color="#cccccc"; + node [fillcolor="#008000", fontcolor=white]; + PTR_TOP [label="⊤:ptr\n[*⊤:struct]"]; + PTR_BOT [label="⊥:ptr\n[*⊥:struct?]"]; + PTR_NULL [label="null"]; + PTR_ANY [label="*⊥:struct\n[ptr any struct]"]; + PTR_PERSON [label="*Person"]; + PTR_S1 [label="*s1"]; + PTR_MORE [label="*s2, …"]; + PTR_PERSON_NIL [label="*Person?"]; + PTR_S1_NIL [label="*s1?"]; + PTR_MORE_NIL [label="*s2?, …"]; + {rank=same; PTR_PERSON; PTR_S1; PTR_MORE; PTR_NULL;} + {rank=same; PTR_ANY; PTR_PERSON_NIL; PTR_S1_NIL; PTR_MORE_NIL;} + PTR_PERSON -> PTR_S1 -> PTR_MORE -> PTR_NULL [style=invis]; + PTR_ANY -> PTR_PERSON_NIL -> PTR_S1_NIL -> PTR_MORE_NIL [style=invis]; + PTR_TOP -> {PTR_PERSON PTR_S1 PTR_MORE PTR_NULL}; + {PTR_PERSON PTR_S1 PTR_MORE} -> PTR_ANY; + PTR_PERSON -> PTR_PERSON_NIL; + PTR_S1 -> PTR_S1_NIL; + PTR_MORE -> PTR_MORE_NIL; + PTR_NULL -> {PTR_PERSON_NIL PTR_S1_NIL PTR_MORE_NIL}; + {PTR_ANY PTR_PERSON_NIL PTR_S1_NIL PTR_MORE_NIL} -> PTR_BOT; + } + + subgraph cluster_struct { + label="Struct\nnamed collection of named scalar types"; + fontname="Arial"; color="#cccccc"; + node [fillcolor="#90ee90"]; + STRUCT_TOP [label="⊤:struct\n[struct top]"]; + STRUCT_BOT [label="⊥:struct\n[struct bottom]"]; + STRUCT_PERSON [label="Person\nage: int", shape=box]; + STRUCT_S1 [label="s1\nx: int", shape=box]; + STRUCT_S2 [label="s2\nx: int, y: *s1", shape=box]; + STRUCT_MORE [label="…", shape=box]; + {rank=same; STRUCT_PERSON; STRUCT_S1; STRUCT_S2; STRUCT_MORE;} + STRUCT_PERSON -> STRUCT_S1 -> STRUCT_S2 -> STRUCT_MORE [style=invis]; + STRUCT_TOP -> {STRUCT_PERSON STRUCT_S1 STRUCT_S2 STRUCT_MORE}; + {STRUCT_PERSON STRUCT_S1 STRUCT_S2 STRUCT_MORE} -> STRUCT_BOT; + } + + subgraph cluster_memory { + label="Memory"; fontname="Arial"; color="#cccccc"; + node [fillcolor="#00008b", fontcolor=white]; + MEM_TOP [label="⊤:mem"]; + MEM_BOT [label="⊥:mem"]; + MEM_TOP -> MEM_BOT; + } + + subgraph cluster_tuple { + label="Tuple\ncollection of types"; + fontname="Arial"; color="#cccccc"; + node [fillcolor="#ffc0cb"]; + TUPLE_TOP [label="⊤:tuple"]; + TUPLE_BOT [label="⊥:tuple"]; + TUPLE1 [label="(type)", shape=box]; + TUPLE2 [label="(type, type)", shape=box]; + TUPLEN [label="(type, …)", shape=box]; + {rank=same; TUPLE1; TUPLE2; TUPLEN;} + TUPLE1 -> TUPLE2 -> TUPLEN [style=invis]; + TUPLE_TOP -> {TUPLE1 TUPLE2 TUPLEN}; + {TUPLE1 TUPLE2 TUPLEN} -> TUPLE_BOT; + } + + TOP -> {XCTRL INT_TOP PTR_TOP STRUCT_TOP MEM_TOP TUPLE_TOP}; + {CTRL INT_BOT PTR_BOT STRUCT_BOT MEM_BOT TUPLE_BOT} -> BOT; + {rank=same; XCTRL; INT_TOP; PTR_TOP; STRUCT_TOP; MEM_TOP; TUPLE_TOP;} + {rank=same; CTRL; INT_BOT; PTR_BOT; STRUCT_BOT; MEM_BOT; TUPLE_BOT;} + XCTRL -> INT_TOP -> PTR_TOP -> STRUCT_TOP -> MEM_TOP -> TUPLE_TOP [style=invis]; +} diff --git a/chapter10a/docs/lattice.svg b/chapter10a/docs/lattice.svg new file mode 100644 index 00000000..24c0de8c --- /dev/null +++ b/chapter10a/docs/lattice.svg @@ -0,0 +1,570 @@ + + + + + + +Lattice + + +cluster_control + +Control + + +cluster_integer + +Integer + + +cluster_pointer + +Pointer = nullability × struct type + + +cluster_struct + +Struct +named collection of named scalar types + + +cluster_memory + +Memory + + +cluster_tuple + +Tuple +collection of types + + + +TOP + +⊤ + + + +XCTRL + +⊤:ctrl + + + +TOP->XCTRL + + + + + +INT_TOP + +⊤:int + + + +TOP->INT_TOP + + + + + +PTR_TOP + +⊤:ptr +[*⊤:struct] + + + +TOP->PTR_TOP + + + + + +STRUCT_TOP + +⊤:struct +[struct top] + + + +TOP->STRUCT_TOP + + + + + +MEM_TOP + +⊤:mem + + + +TOP->MEM_TOP + + + + + +TUPLE_TOP + +⊤:tuple + + + +TOP->TUPLE_TOP + + + + + +BOT + +⊥ + + + +CTRL + +⊥:ctrl + + + +XCTRL->CTRL + + + + + + +CTRL->BOT + + + + + +INT_NEG + +… −2, −1 + + + +INT_TOP->INT_NEG + + + + + +INT_ZERO + +0 + + + +INT_TOP->INT_ZERO + + + + + +INT_POS + +1, 2, … + + + +INT_TOP->INT_POS + + + + + + +INT_BOT + +⊥:int + + + +INT_BOT->BOT + + + + + +INT_NEG->INT_BOT + + + + + + +INT_ZERO->INT_BOT + + + + + + +INT_POS->INT_BOT + + + + + +PTR_NULL + +null +[*⊤:struct?] + + + +PTR_TOP->PTR_NULL + + + + + +PTR_PERSON + +*Person + + + +PTR_TOP->PTR_PERSON + + + + + +PTR_S1 + +*s1 + + + +PTR_TOP->PTR_S1 + + + + + +PTR_MORE + +*s2, … + + + +PTR_TOP->PTR_MORE + + + + + + +PTR_BOT + +⊥:ptr +[*⊥:struct?] + + + +PTR_BOT->BOT + + + + + +PTR_PERSON_NIL + +*Person? + + + +PTR_NULL->PTR_PERSON_NIL + + + + + +PTR_S1_NIL + +*s1? + + + +PTR_NULL->PTR_S1_NIL + + + + + +PTR_MORE_NIL + +*s2?, … + + + +PTR_NULL->PTR_MORE_NIL + + + + + +PTR_ANY + +*⊥:struct +[ptr any struct] + + + +PTR_ANY->PTR_BOT + + + + + + +PTR_PERSON->PTR_ANY + + + + + + +PTR_PERSON->PTR_PERSON_NIL + + + + + +PTR_S1->PTR_ANY + + + + + + +PTR_S1->PTR_S1_NIL + + + + + + +PTR_MORE->PTR_ANY + + + + + +PTR_MORE->PTR_MORE_NIL + + + + + +PTR_PERSON_NIL->PTR_BOT + + + + + + +PTR_S1_NIL->PTR_BOT + + + + + + +PTR_MORE_NIL->PTR_BOT + + + + + +STRUCT_PERSON + +Person +age: int + + + +STRUCT_TOP->STRUCT_PERSON + + + + + +STRUCT_S1 + +s1 +x: int + + + +STRUCT_TOP->STRUCT_S1 + + + + + +STRUCT_S2 + +s2 +x: int, y: int + + + +STRUCT_TOP->STRUCT_S2 + + + + + +STRUCT_MORE + +… + + + +STRUCT_TOP->STRUCT_MORE + + + + + + +STRUCT_BOT + +⊥:struct +[struct bottom] + + + +STRUCT_BOT->BOT + + + + + +STRUCT_PERSON->STRUCT_BOT + + + + + + +STRUCT_S1->STRUCT_BOT + + + + + + +STRUCT_S2->STRUCT_BOT + + + + + + +STRUCT_MORE->STRUCT_BOT + + + + + +MEM_BOT + +⊥:mem + + + +MEM_TOP->MEM_BOT + + + + + + +MEM_BOT->BOT + + + + + +TUPLE1 + +(type) + + + +TUPLE_TOP->TUPLE1 + + + + + +TUPLE2 + +(type, type) + + + +TUPLE_TOP->TUPLE2 + + + + + +TUPLEN + +(type, …) + + + +TUPLE_TOP->TUPLEN + + + + + +TUPLE_BOT + +⊥:tuple + + + +TUPLE_BOT->BOT + + + + + +TUPLE1->TUPLE_BOT + + + + + + +TUPLE2->TUPLE_BOT + + + + + + +TUPLEN->TUPLE_BOT + + + + + diff --git a/chapter10a/docs/type-control.svg b/chapter10a/docs/type-control.svg new file mode 100644 index 00000000..d4b79c53 --- /dev/null +++ b/chapter10a/docs/type-control.svg @@ -0,0 +1,5 @@ + + Control + + Control + diff --git a/chapter10a/docs/type-integer.svg b/chapter10a/docs/type-integer.svg new file mode 100644 index 00000000..97d03372 --- /dev/null +++ b/chapter10a/docs/type-integer.svg @@ -0,0 +1,5 @@ + + Integer + + Integer + diff --git a/chapter10a/docs/type-memory.svg b/chapter10a/docs/type-memory.svg new file mode 100644 index 00000000..16b5121f --- /dev/null +++ b/chapter10a/docs/type-memory.svg @@ -0,0 +1,5 @@ + + Memory + + Memory + diff --git a/chapter10a/docs/type-pointer.svg b/chapter10a/docs/type-pointer.svg new file mode 100644 index 00000000..7604492e --- /dev/null +++ b/chapter10a/docs/type-pointer.svg @@ -0,0 +1,5 @@ + + Pointer + + Pointer + diff --git a/chapter10a/docs/type-struct.svg b/chapter10a/docs/type-struct.svg new file mode 100644 index 00000000..97a16562 --- /dev/null +++ b/chapter10a/docs/type-struct.svg @@ -0,0 +1,5 @@ + + Struct + + Struct + diff --git a/chapter10a/docs/type-tuple.svg b/chapter10a/docs/type-tuple.svg new file mode 100644 index 00000000..691478ac --- /dev/null +++ b/chapter10a/docs/type-tuple.svg @@ -0,0 +1,5 @@ + + Tuple + + Tuple + -- cgit v1.2.3