From f5f0580331b4e29a0a0dc43d9885dfe0f0aed865 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Fri, 13 Jun 2025 15:42:08 -0700 Subject: Chapter 25: SSA Before Types Are Known MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: Tamás Hadházy <85063808+Hels15@users.noreply.github.com> Co-authored-by: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: Symious <14933944+symious@users.noreply.github.com> Co-authored-by: Jun Kawasaki Co-authored-by: Hadházy Tamás Co-authored-by: Claude Opus 5 --- README.ja.md | 2 +- README.md | 2 +- docs/chapter25/24-grammar.ja.md | 168 +++++ docs/chapter25/24-grammar.md | 168 +++++ docs/chapter25/Makefile | 12 + docs/chapter25/README.ja.md | 17 + docs/chapter25/README.md | 151 +++++ docs/chapter25/examples/A_helloWorld.smp | 3 + docs/chapter25/examples/BubbleSort.smp | 37 ++ docs/chapter25/font-awesome-4.7.0.zip | Bin 0 -> 669808 bytes docs/chapter25/ifg_trim.pdf | Bin 0 -> 155190 bytes docs/chapter25/lattice.gv | 203 ++++++ docs/chapter25/lattice.svg | 691 +++++++++++++++++++++ docs/chapter25/lattice2.gv | 85 +++ docs/chapter25/lattice2.svg | 260 ++++++++ docs/chapter25/pardoe.docx | Bin 0 -> 22583 bytes docs/chapter25/webtcp.min.js | 65 ++ src/main/java/com/seaofnodes/simple/IterPeeps.java | 2 +- src/main/java/com/seaofnodes/simple/Parser.java | 152 +++-- .../com/seaofnodes/simple/codegen/CodeGen.java | 9 + .../com/seaofnodes/simple/codegen/InstSelect.java | 23 +- .../com/seaofnodes/simple/codegen/Machine.java | 5 +- .../java/com/seaofnodes/simple/codegen/Opto.java | 62 ++ .../com/seaofnodes/simple/codegen/TypeCheck.java | 36 +- .../java/com/seaofnodes/simple/node/AddFNode.java | 1 + .../java/com/seaofnodes/simple/node/AddNode.java | 43 +- .../java/com/seaofnodes/simple/node/AndNode.java | 6 +- .../com/seaofnodes/simple/node/ArithFNode.java | 1 + .../java/com/seaofnodes/simple/node/ArithNode.java | 184 ++++-- .../java/com/seaofnodes/simple/node/BoolNode.java | 128 ++-- .../com/seaofnodes/simple/node/BulkMemPhiNode.java | 3 +- .../java/com/seaofnodes/simple/node/CallNode.java | 2 +- .../java/com/seaofnodes/simple/node/CastNode.java | 8 + .../com/seaofnodes/simple/node/ConstantNode.java | 10 +- .../com/seaofnodes/simple/node/ConvertNode.java | 103 +++ .../java/com/seaofnodes/simple/node/DivFNode.java | 1 + .../java/com/seaofnodes/simple/node/DivNode.java | 23 +- .../com/seaofnodes/simple/node/FldOffNode.java | 24 + .../java/com/seaofnodes/simple/node/FunNode.java | 8 + .../java/com/seaofnodes/simple/node/GuardMach.java | 17 + .../java/com/seaofnodes/simple/node/GuardNode.java | 66 ++ .../java/com/seaofnodes/simple/node/IfNode.java | 4 +- .../java/com/seaofnodes/simple/node/LoadNode.java | 85 +-- .../java/com/seaofnodes/simple/node/MemOpNode.java | 10 +- .../com/seaofnodes/simple/node/MemPhiNode.java | 5 +- .../com/seaofnodes/simple/node/MinusFNode.java | 1 + .../java/com/seaofnodes/simple/node/MinusNode.java | 66 +- .../java/com/seaofnodes/simple/node/ModeNode.java | 12 + .../java/com/seaofnodes/simple/node/MulFNode.java | 1 + .../java/com/seaofnodes/simple/node/MulNode.java | 15 +- .../java/com/seaofnodes/simple/node/NewNode.java | 10 +- src/main/java/com/seaofnodes/simple/node/Node.java | 14 +- .../java/com/seaofnodes/simple/node/OrNode.java | 5 +- .../java/com/seaofnodes/simple/node/ParmNode.java | 24 +- .../java/com/seaofnodes/simple/node/PhiNode.java | 91 ++- .../com/seaofnodes/simple/node/PtrToIntMach.java | 17 + .../com/seaofnodes/simple/node/PtrToIntNode.java | 35 ++ .../com/seaofnodes/simple/node/ReturnNode.java | 25 +- .../java/com/seaofnodes/simple/node/SarNode.java | 6 +- .../java/com/seaofnodes/simple/node/ScopeNode.java | 31 +- .../java/com/seaofnodes/simple/node/ShlNode.java | 8 +- .../java/com/seaofnodes/simple/node/ShrNode.java | 5 +- .../java/com/seaofnodes/simple/node/StoreNode.java | 23 +- .../com/seaofnodes/simple/node/StructNode.java | 11 +- .../java/com/seaofnodes/simple/node/SubFNode.java | 1 + .../java/com/seaofnodes/simple/node/SubNode.java | 13 +- .../java/com/seaofnodes/simple/node/XorNode.java | 6 +- .../seaofnodes/simple/node/cpus/arm/CallARM.java | 2 +- .../com/seaofnodes/simple/node/cpus/arm/arm.java | 2 +- .../simple/node/cpus/riscv/CallRISC.java | 2 +- .../simple/node/cpus/x86_64_v2/CallX86.java | 2 +- .../simple/node/cpus/x86_64_v2/x86_64_v2.java | 2 +- .../java/com/seaofnodes/simple/type/Field.java | 2 +- src/main/java/com/seaofnodes/simple/type/Type.java | 23 +- .../com/seaofnodes/simple/type/TypeConAry.java | 2 +- .../java/com/seaofnodes/simple/type/TypeFloat.java | 2 +- .../com/seaofnodes/simple/type/TypeFunPtr.java | 17 +- .../com/seaofnodes/simple/type/TypeInteger.java | 2 +- .../java/com/seaofnodes/simple/type/TypeMem.java | 9 +- .../com/seaofnodes/simple/type/TypeMemPtr.java | 6 +- .../java/com/seaofnodes/simple/type/TypeNil.java | 2 +- .../com/seaofnodes/simple/type/TypeScalar.java | 31 + .../com/seaofnodes/simple/type/TypeStruct.java | 27 +- .../java/com/seaofnodes/simple/type/TypeTuple.java | 23 +- .../java/com/seaofnodes/simple/Chapter07Test.java | 10 +- .../java/com/seaofnodes/simple/Chapter09Test.java | 2 +- .../java/com/seaofnodes/simple/Chapter12Test.java | 10 +- .../com/seaofnodes/simple/Chapter14FloatTest.java | 2 +- .../java/com/seaofnodes/simple/Chapter14Test.java | 2 +- .../java/com/seaofnodes/simple/Chapter18Test.java | 4 +- .../java/com/seaofnodes/simple/Chapter19Test.java | 4 +- .../java/com/seaofnodes/simple/Chapter20Test.java | 6 +- .../java/com/seaofnodes/simple/Chapter21Test.java | 16 +- .../java/com/seaofnodes/simple/Chapter23Test.java | 24 +- src/test/java/com/seaofnodes/simple/Eval2.java | 15 +- .../com/seaofnodes/simple/IncompleteTypesTest.java | 40 ++ .../java/com/seaofnodes/simple/MergeSortTest.java | 10 +- src/test/java/com/seaofnodes/simple/TypeTest.java | 4 +- .../simple/codegen/RegAllocTestSupport.java | 6 +- 99 files changed, 3205 insertions(+), 411 deletions(-) create mode 100644 docs/chapter25/24-grammar.ja.md create mode 100644 docs/chapter25/24-grammar.md create mode 100644 docs/chapter25/Makefile create mode 100644 docs/chapter25/README.ja.md create mode 100644 docs/chapter25/README.md create mode 100644 docs/chapter25/examples/A_helloWorld.smp create mode 100644 docs/chapter25/examples/BubbleSort.smp create mode 100644 docs/chapter25/font-awesome-4.7.0.zip create mode 100644 docs/chapter25/ifg_trim.pdf create mode 100644 docs/chapter25/lattice.gv create mode 100644 docs/chapter25/lattice.svg create mode 100644 docs/chapter25/lattice2.gv create mode 100644 docs/chapter25/lattice2.svg create mode 100644 docs/chapter25/pardoe.docx create mode 100644 docs/chapter25/webtcp.min.js create mode 100644 src/main/java/com/seaofnodes/simple/node/ConvertNode.java create mode 100644 src/main/java/com/seaofnodes/simple/node/FldOffNode.java create mode 100644 src/main/java/com/seaofnodes/simple/node/GuardMach.java create mode 100644 src/main/java/com/seaofnodes/simple/node/GuardNode.java create mode 100644 src/main/java/com/seaofnodes/simple/node/ModeNode.java create mode 100644 src/main/java/com/seaofnodes/simple/node/PtrToIntMach.java create mode 100644 src/main/java/com/seaofnodes/simple/node/PtrToIntNode.java create mode 100644 src/main/java/com/seaofnodes/simple/type/TypeScalar.java create mode 100644 src/test/java/com/seaofnodes/simple/IncompleteTypesTest.java diff --git a/README.ja.md b/README.ja.md index 596a8354..bd16eb1a 100644 --- a/README.ja.md +++ b/README.ja.md @@ -63,7 +63,7 @@ Simple 言語のスタイルは、C または Java のサブセットに倣っ * [第23章](docs/chapter23/README.ja.md):メソッドと型の再検討。 * [第24a章](docs/chapter24a/README.ja.md):関数内の SCCP * [第24b章](docs/chapter24b/README.ja.md):関数間の SCCP -* 第25章:型が決まる前の SSA +* [第25章](docs/chapter25/README.ja.md):型が決まる前の SSA * 第26章:構築とプライベートメモリ * 第27章:エスケープ解析と未知の呼び出し元 * 第28a章:モジュールと遅延クラス初期化 diff --git a/README.md b/README.md index d4c6bb31..e2251028 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ The Simple language is styled after a subset of C or Java. * [Chapter 23](docs/chapter23/README.md): Methods and Types Revisited * [Chapter 24a](docs/chapter24a/README.md): SCCP Within a Function * [Chapter 24b](docs/chapter24b/README.md): SCCP Across Functions -* Chapter 25: SSA Before Types Are Known +* [Chapter 25](docs/chapter25/README.md): SSA Before Types Are Known * Chapter 26: Construction and Private Memory * Chapter 27: Escape Analysis and Unknown Callers * Chapter 28a: Modules and Lazy Class Initialization diff --git a/docs/chapter25/24-grammar.ja.md b/docs/chapter25/24-grammar.ja.md new file mode 100644 index 00000000..fd3fa232 --- /dev/null +++ b/docs/chapter25/24-grammar.ja.md @@ -0,0 +1,168 @@ +# 第24章の文法 + +[English](24-grammar.md) | 日本語 + +```antlrv4 +grammar SimpleLanguage; + +// Name lookup, assignable operands, type compatibility, and access permissions +// are checked by the compiler in addition to these syntax rules. +program : statement* EOF ; + +block : '{' statement* '}' ; + +statement + : block + | returnStatement + | ifStatement + | whileStatement + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER '{' structMember* '}' ';' ; +structMember : methodDeclaration | statement ; + +// A method is an ordinary fixed function binding in a struct definition. +// The compiler supplies self; it is not written in parameterList. +// The general declaration rule also admits explicitly typed function fields. +methodDeclaration : 'val' bindingModifier? IDENTIFIER '=' functionExpression ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +binding : bindingModifier? IDENTIFIER ('=' initializer)? ; +bindingModifier : '!' | '~' ; +// A declaration initializer consisting of the string "C" declares an extern +// symbol. Ordinary strings elsewhere are array-valued expressions. +initializer : assignment ; + +// The prefix qualifies the named struct reference, not an enclosing array. +// Each [] / [~] suffix qualifies its own array layer. ? requires a reference. +type + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* + ; +accessModifier : '!' | '~' ; +typeSuffix : '?' | '[]' | '[~]' ; +typeName : IDENTIFIER ('.' IDENTIFIER)* ; + +primitiveType + : 'int' | 'i8' | 'i16' | 'i32' | 'i64' + | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool' + | 'flt' | 'f32' | 'f64' + ; + +assignment : expression (assignmentOperator assignment)? ; +assignmentOperator + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; + +expression : logicalExpression ('?' assignment (':' assignment)?)? ; + +// Both logical operators short-circuit and associate to the right here. +logicalExpression : bitwiseExpression (('&&' | '||') logicalExpression)? ; + +bitwiseExpression + : equalityExpression (('&' | '|' | '^') equalityExpression)* + ; + +equalityExpression : comparisonExpression (('==' | '!=') comparisonExpression)* ; + +// Chained comparisons test adjacent operands and short-circuit. A chain must +// keep its direction; equality has lower precedence and does not join a chain. +comparisonExpression + : shiftExpression + ( (('<' | '<=') shiftExpression)+ | (('>' | '>=') shiftExpression)+ )? + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix | methodCallSuffix ; + +// For a fixed function-valued member, object.name(args) passes object as self. +// This overlaps ordinary field selection followed by a call syntactically; +// name/type resolution determines which call form applies. +methodCallSuffix : '.' IDENTIFIER callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | STRING_LITERAL + | CHARACTER_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// Struct initialization uses a block; array allocation supplies a length. +newExpression : 'new' type ('[' assignment ']' | block)? ; + +INTEGER_LITERAL : '0' | [1-9] DIGIT* ; +FLOAT_LITERAL : DIGIT+ ('.' DIGIT* ('e' DIGIT+)? | 'e' '-'? DIGIT+) ; +STRING_LITERAL : '"' (ESCAPE | ~["\\])* '"' ; +CHARACTER_LITERAL : '\'' (ESCAPE | ~['\\]) '\'' ; +fragment ESCAPE : '\\' [0ntr\\'"] ; +IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; +fragment NON_DIGIT : [a-zA-Z_] ; +fragment DIGIT : [0-9] ; +WHITESPACE : [ \t\r\n]+ -> skip ; +LINE_COMMENT : '//' ~[\r\n]* -> skip ; +BLOCK_COMMENT : '/*' .*? '*/' -> skip ; +``` diff --git a/docs/chapter25/24-grammar.md b/docs/chapter25/24-grammar.md new file mode 100644 index 00000000..22867355 --- /dev/null +++ b/docs/chapter25/24-grammar.md @@ -0,0 +1,168 @@ +# Grammar for Chapter 24 + +English | [日本語](24-grammar.ja.md) + +```antlrv4 +grammar SimpleLanguage; + +// Name lookup, assignable operands, type compatibility, and access permissions +// are checked by the compiler in addition to these syntax rules. +program : statement* EOF ; + +block : '{' statement* '}' ; + +statement + : block + | returnStatement + | ifStatement + | whileStatement + | forStatement + | breakStatement + | continueStatement + | structDeclaration + | declaration ';' + | assignment ';' + | ';' + ; + +returnStatement : 'return' assignment ';' ; +ifStatement : 'if' '(' assignment ')' statement ('else' statement)? ; +whileStatement : 'while' '(' assignment ')' statement ; +breakStatement : 'break' ';' ; +continueStatement : 'continue' ';' ; + +forStatement + : 'for' '(' (declaration | assignment)? ';' assignment? ';' assignment? ')' statement + ; + +structDeclaration : 'struct' IDENTIFIER '{' structMember* '}' ';' ; +structMember : methodDeclaration | statement ; + +// A method is an ordinary fixed function binding in a struct definition. +// The compiler supplies self; it is not written in parameterList. +// The general declaration rule also admits explicitly typed function fields. +methodDeclaration : 'val' bindingModifier? IDENTIFIER '=' functionExpression ';' ; + +declaration : declarationType binding (',' binding)* ; +declarationType : type | 'var' | 'val' ; +binding : bindingModifier? IDENTIFIER ('=' initializer)? ; +bindingModifier : '!' | '~' ; +// A declaration initializer consisting of the string "C" declares an extern +// symbol. Ordinary strings elsewhere are array-valued expressions. +initializer : assignment ; + +// The prefix qualifies the named struct reference, not an enclosing array. +// Each [] / [~] suffix qualifies its own array layer. ? requires a reference. +type + : functionType + | primitiveType typeSuffix* + | accessModifier? typeName typeSuffix* + ; +accessModifier : '!' | '~' ; +typeSuffix : '?' | '[]' | '[~]' ; +typeName : IDENTIFIER ('.' IDENTIFIER)* ; + +primitiveType + : 'int' | 'i8' | 'i16' | 'i32' | 'i64' + | 'u1' | 'u8' | 'u16' | 'u32' | 'byte' | 'bool' + | 'flt' | 'f32' | 'f64' + ; + +assignment : expression (assignmentOperator assignment)? ; +assignmentOperator + : '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' + | '<<=' | '>>=' | '>>>=' + ; + +expression : logicalExpression ('?' assignment (':' assignment)?)? ; + +// Both logical operators short-circuit and associate to the right here. +logicalExpression : bitwiseExpression (('&&' | '||') logicalExpression)? ; + +bitwiseExpression + : equalityExpression (('&' | '|' | '^') equalityExpression)* + ; + +equalityExpression : comparisonExpression (('==' | '!=') comparisonExpression)* ; + +// Chained comparisons test adjacent operands and short-circuit. A chain must +// keep its direction; equality has lower precedence and does not join a chain. +comparisonExpression + : shiftExpression + ( (('<' | '<=') shiftExpression)+ | (('>' | '>=') shiftExpression)+ )? + ; + +shiftExpression + : additiveExpression (('<<' | '>>' | '>>>') additiveExpression)* + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-' | '!') unaryExpression + | updateOperator IDENTIFIER + | postfixExpression + ; + +// Prefix updates require a local name; postfix updates also allow fields +// and array elements. All updates require an assignable operand. +updateOperator : '++' | '--' ; +postfixExpression : primaryExpression postfixSuffix* updateOperator? ; +postfixSuffix : '.' IDENTIFIER | '[' assignment ']' | '#' | callSuffix | methodCallSuffix ; + +// For a fixed function-valued member, object.name(args) passes object as self. +// This overlaps ordinary field selection followed by a call syntactically; +// name/type resolution determines which call form applies. +methodCallSuffix : '.' IDENTIFIER callSuffix ; + +primaryExpression + : INTEGER_LITERAL + | FLOAT_LITERAL + | STRING_LITERAL + | CHARACTER_LITERAL + | 'true' + | 'false' + | 'null' + | IDENTIFIER + | '(' assignment ')' + | newExpression + | functionExpression + ; + +// A function is an expression bound with an ordinary declaration. +// Its last statement supplies the implicit result; return can exit earlier. +functionExpression : '{' parameterList? '->' statement* '}' ; +parameterList : (parameter ','?)+ ; +parameter : type bindingModifier? IDENTIFIER ; + +// Function types have unnamed, space-separated argument types. +// {int} is the zero-argument type; its function expression still uses ->. +functionType + : '{' type+ '->' type '}' '?'? + | '{' type '}' '?'? + ; + +callSuffix : '(' argumentList? ')' ; +argumentList : assignment (',' assignment)* ','? ; + +// Struct initialization uses a block; array allocation supplies a length. +newExpression : 'new' type ('[' assignment ']' | block)? ; + +INTEGER_LITERAL : '0' | [1-9] DIGIT* ; +FLOAT_LITERAL : DIGIT+ ('.' DIGIT* ('e' DIGIT+)? | 'e' '-'? DIGIT+) ; +STRING_LITERAL : '"' (ESCAPE | ~["\\])* '"' ; +CHARACTER_LITERAL : '\'' (ESCAPE | ~['\\]) '\'' ; +fragment ESCAPE : '\\' [0ntr\\'"] ; +IDENTIFIER : NON_DIGIT (NON_DIGIT | DIGIT)* ; +fragment NON_DIGIT : [a-zA-Z_] ; +fragment DIGIT : [0-9] ; +WHITESPACE : [ \t\r\n]+ -> skip ; +LINE_COMMENT : '//' ~[\r\n]* -> skip ; +BLOCK_COMMENT : '/*' .*? '*/' -> skip ; +``` diff --git a/docs/chapter25/Makefile b/docs/chapter25/Makefile new file mode 100644 index 00000000..12659b8f --- /dev/null +++ b/docs/chapter25/Makefile @@ -0,0 +1,12 @@ +GV := $(wildcard *.gv) +SVG := $(patsubst %.gv,%.svg,$(GV)) + +.PHONY: all +all: $(SVG) + +%.svg: %.gv + dot -Tsvg $< > $@ + +.PHONY: clean +clean: + @rm -f $(SVG) diff --git a/docs/chapter25/README.ja.md b/docs/chapter25/README.ja.md new file mode 100644 index 00000000..8eaef623 --- /dev/null +++ b/docs/chapter25/README.ja.md @@ -0,0 +1,17 @@ +# 第25章: 型が確定する前の SSA 構築 + +[English](README.md) | 日本語 + +[Previous: Chapter 24b](../chapter24b/README.ja.md) | +[Next: Chapter 26](../chapter26/README.ja.md) + +この章では、前方参照やループのために型がまだ分からない場合でも、演算と +フィールド参照をグラフに記録します。数値演算のモード、Convert、Guard、 +シンボルによるフィールド位置を使い、型と配置が確定した後に解決します。 +値の Phi と Load は、パーサが与える仮の型の下限を使いません。 + +コンストラクタは前章の方式を維持します。初期化用の呼び出し、非公開の +メモリ、オブジェクトの公開は次の章で扱います。詳しい説明と例は +[英語版](README.md)を参照してください。 + +このディレクトリで `make lib tests CTAGS=` を実行できます。 diff --git a/docs/chapter25/README.md b/docs/chapter25/README.md new file mode 100644 index 00000000..1dda8928 --- /dev/null +++ b/docs/chapter25/README.md @@ -0,0 +1,151 @@ +# Chapter 25: SSA Before Types Are Known + +English | [日本語](README.ja.md) + +[Previous: Chapter 24b](../chapter24b/README.md) | +[Next: Chapter 26](../chapter26/README.md) + +The previous compiler solved for recursive types and calls, but its parser +still needs types to choose e.g. float-vs-integer arithmetic instructions or +field types. Forward references, external references, or even simple loop +structures can hide what's needed. + +This chapter records the operations first and resolves their type-dependent +parts later. Scalar types, numeric modes, conversions, guards, symbolic +fields, and unbounded value Phis and Loads have to get typed *together*, as +they all have mutually interlocking type dependencies. + + +## A definition can follow its use + +```simple +val add = { Later p -> return p.x + 1; }; +struct Later { flt x; }; +return add(new Later { x = 2.5; }); +``` + +When parsing `p.x + 1`, the compiler knows the receiver's name (type `Later`), +but not its fields! It makes a Load with a symbolic field offset `FldOff` and +an `Add` whose numeric mode is still unset: might be an integer add or a +floating-point add. Later we parse `Later`, which supplies the field and its +storage type. Type propagation then selects floating-point addition and +converts the integer `1` operand. The result is some floating point math +`2.5 + (flt)1`, which folds to `3.5`. + +Forward writes work the same way. Until a field is known, its Store carries +whole memory - it aliases everything until we know what it aliases. Once +resolved, the Store converts its value to the declared field type and updates +that field's alias, which generally will result in a cascade of peepholes +sharpening the memory edges. Resolving the alias preserves the Store's control +input. Update operators such as `x += y` work like `x = x + y`, while evaluating +the destination only once. Storing into a `u8` field may narrow an integer +result; storing into an `f64` field may instead convert it to floating point. + +`FldOff` deserves more mention: it represents a named field reference (Load or +Store) to an incomplete `TypeStruct`. In the example above, that's `p.x`: field +`x` of a `Later` whose fields are not yet declared. The struct can gain more +fields, and the field type itself is still unknown, so +`FldOff` really knows very little about what's going on. It's more of a *shape* +placeholder, and awaits stronger types to peephole into a "real" field +reference. + + +## Values need their own bounds + +The parser always knows when it has *values* vs memory or control, but maybe +not whether something is an int or a flt until much later. Instead it uses +`TypeScalar` for this. `TypeScalar` contains integers, floats, object +pointers, and function pointers. These are classic *values* in the sense that +they represent small concrete things that typically fit in a machine register. +Values exclude control and memory. Scalar bottom says that a value's family is +not known; its top represents an unreachable scalar result. A scalar union can +still be tested for truth, even when its alternatives use different families; +integers, floats, and pointers all carry a notion of zero. + +A scalar Phi now meets its live inputs; it no longer carries a parser-supplied +lower bound. An unfinished loop Phi starts at scalar bottom because its +backedge is missing. Parameters retain their declared interface types, and +memory Phis retain their memory role. Loop Phis, Parms, and recursive CallEnds +still do widening. + + +## Record conversions and branch facts + +The parser ends up inserting `Convert` nodes where it used to directly convert +values. This includes converting integers to match float operators and narrowing +values stored into integer fields. Loads derive their value types from memory; +they no longer impose a parser-supplied bound. A bad load or store still produces +bottom. Type checking looks through a conversion with an already-broken input +so it can report the original error. + +`Convert` records a known destination type, such as a local declaration or +function parameter. It waits for its input type to sharpen, and then peepholes +into a widening, truncation, permission check, or no operation. + +`Guard` records a branch fact: zero/null or nonzero/non-null. Its value family +comes from its input when known. The control edge keeps the fact on the path +where it was proved. A guard output is the same runtime value as its input; it +emits no instruction, just the sharpened type result. Guards are typically +used to track not-null pointer tests across large areas. + +Arithmetic nodes carry a `mode`: a 3-state enum tracking an unset, integer, or +floating-point mode. Known inputs can settle it during peepholes. SCCP +resolves remaining recursive numeric components, then propagates the result +again. Once the mode is known, the machine instruction selection sections will +pick the correct machine ops. + + +## Complete declarations, then update the graph + +```simple +val foo = { A a -> a.b; }; +struct A { B b; }; +``` + +When parsing `foo`, we know that `a` points to an `A`, but not the fields of +`A` or the type of `a.b`. The load records that `A` must have a field named `b`. +Forward shapes (`A`) remain *open* until their declaration, as in prior +chapters. When parsing `struct A` we find `B`, but `B` here is never declared... and +does not need to be. + +After parsing, the compiler *closes* all declarations as a group, including +type cycles (such as a `struct LinkedList` having a `next` field typed as a +`LinkedList`) and references through function signatures. It then replaces old +type shapes in cached node types and stored declarations, preserving pointer +permissions and nullability. Declared types now have their complete field sets. +An undeclared type such as `B` may remain opaque: this example only passes its +pointer around and does not need to allocate a `B` or access its fields. + +Closing declarations does not itself prove the program will type-check. + +At this point `FldOff` can begin to peephole into a `ConFldOff` - moving +from a symbolic field reference of an incomplete type to a concrete +field of a known type. Later, after structures get a *layout*, `ConFldOff` +will become a concrete field offset, a number like `4` or `8`. + + +## The construction boundary + +This snapshot keeps the preceding chapter's constructor model: collect field +values in the constructor scope, check completion, then allocate and initialize. +An unknown expression type is no longer evidence of an unwritten field, so the +completion check follows explicit missing-initialization values through merges. + +Allocation helpers, hidden initializer calls, private self-memory, and explicit +publication belong to the next chapter. Escape discovery, source modules, lazy +class initialization, and serialized ideal IR follow later. These changes use +the existing conservative policy for functions exposed to unknown callers. +Method binding retains the preceding parser's requirement that the owner's +field declaration be known. Forward data fields are resolved here; moving +method and initializer bodies out of that parser scope is construction work. + +## Building and checking + +Run `make lib tests CTAGS=` here, or from the repository root run +`make tests CHAPTERS=chapter25 CTAGS=`. + +`IncompleteTypesTest` exercises a forward field with deferred numeric mode and +a forward store whose byte width must be resolved. The inherited suite covers +recursive objects, null guards, initialization errors, loops, all three code +generators, and native calls. Earlier parse-only graph expectations now show +conversions that disappear after type propagation. diff --git a/docs/chapter25/examples/A_helloWorld.smp b/docs/chapter25/examples/A_helloWorld.smp new file mode 100644 index 00000000..07ff935f --- /dev/null +++ b/docs/chapter25/examples/A_helloWorld.smp @@ -0,0 +1,3 @@ +// -*- mode: java; -*- +sys.io.p("Hello, World!"); +return 0; diff --git a/docs/chapter25/examples/BubbleSort.smp b/docs/chapter25/examples/BubbleSort.smp new file mode 100644 index 00000000..938e3181 --- /dev/null +++ b/docs/chapter25/examples/BubbleSort.smp @@ -0,0 +1,37 @@ +// -*- mode: java; -*- + +// An implementation of +// https://sampleprograms.io/projects/bubble-sort/ +// +// Bubble sort, with reading (parsing) and writing (formatting) the data. + +val sort = { !sys.aryi64 nums -> + var done = false; + while( !done ) { + done = true; + for( var i = 0; i < nums.len-1; i++ ) + if( nums.buf[i] > nums.buf[i+1] ) { + int tmp = nums.buf[i]; + nums.buf[i] = nums.buf[i+1]; + nums.buf[i+1] = tmp; // Swap + done = false; + } + } +}; + +// Read from stdin a line of the form "[ 4, 3, 2, 1]" and print it sorted. +val main = { -> + // Parse array of numbers from stdin + var nums = sys.parseAryI64( sys.io.stdin() ); + if( !nums ) { + sys.io.p("stdin must look like [4, 3, 2, 1]"); + return 1; + } + + // Bubble sort array of integers + sort(nums); + + // Print a list of numbers to stdout + nums.str().write(1); + return 0; +}; diff --git a/docs/chapter25/font-awesome-4.7.0.zip b/docs/chapter25/font-awesome-4.7.0.zip new file mode 100644 index 00000000..b6bdb4bd Binary files /dev/null and b/docs/chapter25/font-awesome-4.7.0.zip differ diff --git a/docs/chapter25/ifg_trim.pdf b/docs/chapter25/ifg_trim.pdf new file mode 100644 index 00000000..d952fd4f Binary files /dev/null and b/docs/chapter25/ifg_trim.pdf differ diff --git a/docs/chapter25/lattice.gv b/docs/chapter25/lattice.gv new file mode 100644 index 00000000..8661082d --- /dev/null +++ b/docs/chapter25/lattice.gv @@ -0,0 +1,203 @@ +digraph Lattice { + compound = true; + // Define nodes + splines=false; + node [shape=circle, style=filled]; + nodesep=0.2; + ranksep=0.2; + + TOP [label=<⊤>, fillcolor=black, fontcolor=white]; + BOT [label=<⊥>, fillcolor=black, fontcolor=white]; + + // ---------------------- + // Control + XCTRL [label=<⊤:ctrl>, fillcolor=yellow, shape=oval]; + CTRL [label=<⊥:ctrl>, fillcolor=yellow, shape=oval]; + + TOP -> XCTRL; + XCTRL -> CTRL; + CTRL -> BOT; + + // ---------------------- + // Integers + INT_TOP [label=<⊤:int>, fillcolor=lightblue, shape=oval]; + INT_BOT [label=<⊥:int>, fillcolor=lightblue, shape=oval]; + + INT_HI [label="[min>max]" , fillcolor=lightblue, shape=oval]; + INT_XBOOL [label="[1...0]", fillcolor=lightblue, shape=oval]; + INT_CON [label="constants", fillcolor=lightblue, shape=oval]; + INT_CON0 [label="0", fillcolor=lightblue]; + INT_CON1 [label="1", fillcolor=lightblue]; + INT_BOOL [label="[0...1]", fillcolor=lightblue,shape=oval]; + INT_LO [label="[min INT_TOP; + INT_TOP -> INT_HI; + INT_HI -> INT_XBOOL; + INT_XBOOL -> INT_CON0; + INT_XBOOL -> INT_CON1; + INT_HI -> INT_CON; + INT_CON0 -> INT_BOOL; + INT_CON1 -> INT_BOOL; + INT_BOOL -> INT_LO; + INT_CON -> INT_LO; + INT_LO -> INT_BOT; + INT_BOT -> BOT; + + INT_CON0 -> INT_CON1 -> INT_CON [style=invis]; + + // ---------------------- + // Floats + FLT_TOP [label=<⊤:flt>, fillcolor=aqua, shape=oval]; + FLT_BOT [label=<⊥:flt>, fillcolor=aqua, shape=oval]; + + FLT_CON64 [label="64b consts\n[excludes\n32b consts]" , fillcolor=aqua, shape=oval]; + FLT_CON32 [label="32b consts", fillcolor=aqua, shape=oval]; + FLT_CONPI [label="3.14159...", fillcolor=aqua, shape=oval]; + FLT_X32 [label="~f32", fillcolor=aqua]; + FLT_CON0 [label="0." , fillcolor=aqua]; + FLT_32 [label="f32" , fillcolor=aqua]; + + TOP -> FLT_TOP; + FLT_TOP -> FLT_CON64 + FLT_TOP -> FLT_CONPI + FLT_CON64 -> FLT_BOT + FLT_CONPI -> FLT_BOT + + FLT_TOP -> FLT_X32; + FLT_X32 -> FLT_CON0; + FLT_X32 -> FLT_CON32; + FLT_CON0 -> FLT_32; + FLT_CON32 -> FLT_32; + FLT_32 -> FLT_BOT + + FLT_BOT -> BOT; + + FLT_CON0 -> FLT_CON32 -> FLT_CONPI -> FLT_CON64 [style=invis]; + + + // ---------------------- + // Pointers, just a boolean nil/not and a struct + PTR [shape=none, fillcolor=none, label=< + + + +
*ptr
struct
+ >]; + PTR0 [shape=none, fillcolor=none, label=< + + + +
*ptr?
struct
+ >]; + + TOP -> PTR; + PTR -> PTR0; + PTR0 -> BOT; + + // ---------------------- + // Structs + STRUCT_TOP [label=<⊤:struct>, fillcolor=lightgreen, shape=oval]; + STRUCT_BOT [label=<⊥:struct>, fillcolor=lightgreen, shape=oval]; + + STRUCT_S1 [shape=none, fillcolor=none, label=< + + +
fieldint/ptr
+ >]; + STRUCT_S2 [shape=none, fillcolor=none, label=< + + + +
fieldint/ptr
fieldint/ptr
+ >]; + STRUCT_SN [shape=none, fillcolor=none, label=< + + + + +
fieldint/ptr
... int/ptr
fieldint/ptr
+ >]; + + + TOP -> STRUCT_TOP; + STRUCT_TOP -> STRUCT_S1; + STRUCT_TOP -> STRUCT_S2; + STRUCT_TOP -> STRUCT_SN; + STRUCT_S1 -> STRUCT_BOT; + STRUCT_S2 -> STRUCT_BOT; + STRUCT_SN -> STRUCT_BOT; + STRUCT_BOT -> BOT; + + STRUCT_S1 -> STRUCT_S2 -> STRUCT_SN [style=invis]; + + // ---------------------- + // Memory + MEM_TOP [label=<⊤:mem>, fillcolor=blue, fontcolor=white, shape=oval]; + MEM_BOT [label=<⊥:mem>, fillcolor=blue, fontcolor=white, shape=oval]; + MEM_ALIAS1 [label="#1" , fillcolor=blue, fontcolor=white]; + MEM_ALIASDOTS [label="..." , fillcolor=blue, fontcolor=white, shape=oval]; + MEM_ALIASN [label="#n" , fillcolor=blue, fontcolor=white]; + + TOP -> MEM_TOP; + MEM_TOP -> MEM_ALIAS1; + MEM_TOP -> MEM_ALIASDOTS; + MEM_TOP -> MEM_ALIASN; + MEM_ALIAS1 -> MEM_BOT; + MEM_ALIASDOTS -> MEM_BOT; + MEM_ALIASN -> MEM_BOT; + MEM_BOT -> BOT; + + MEM_ALIAS1 -> MEM_ALIASDOTS -> MEM_ALIASN [style=invis]; + + // ---------------------- + // Tuples + + TUPLE_TOP [label=<⊤:tuple>, fillcolor=pink, shape=oval]; + TUPLE_BOT [label=<⊥:tuple>, fillcolor=pink, shape=oval]; + + TUPLE1 [shape=none, fillcolor=none, label=< + + + +
Tuple
type
+ >]; + TUPLE2 [shape=none, fillcolor=none, label=< + + + + +
Tuple
type
type
+ >]; + TUPLEN [shape=none, fillcolor=none, label=< + + + + + +
Tuple
type
...
type
+ >]; + + + TOP -> TUPLE_TOP; + TUPLE_TOP -> TUPLE1; + TUPLE_TOP -> TUPLE2; + TUPLE_TOP -> TUPLEN; + TUPLE1 -> TUPLE_BOT; + TUPLE2 -> TUPLE_BOT; + TUPLEN -> TUPLE_BOT; + TUPLE_BOT -> BOT; + + // ---------------------- + // Additional customization + {rank=same; TOP;} + {rank=same; INT_TOP; FLT_TOP; XCTRL; PTR; STRUCT_TOP; MEM_TOP; TUPLE_TOP} + {rank=same; INT_CON; INT_CON0; INT_CON1; + FLT_CON32; FLT_CON64; FLT_CON0; FLT_CONPI; + STRUCT_S1; STRUCT_S2; STRUCT_SN; + MEM_ALIAS1; MEM_ALIASDOTS; MEM_ALIASN; + TUPLE1; TUPLE2; TUPLEN; + } + {rank=same; INT_BOT; FLT_BOT; CTRL; PTR0; STRUCT_BOT; MEM_BOT; TUPLE_BOT} + {rank=same; BOT;} +} diff --git a/docs/chapter25/lattice.svg b/docs/chapter25/lattice.svg new file mode 100644 index 00000000..e72b473b --- /dev/null +++ b/docs/chapter25/lattice.svg @@ -0,0 +1,691 @@ + + + + + + +Lattice + + + +TOP + +⊤ + + + +XCTRL + +⊤ +:ctrl + + + +TOP->XCTRL + + + + + +INT_TOP + +⊤ +:int + + + +TOP->INT_TOP + + + + + +FLT_TOP + +⊤ +:flt + + + +TOP->FLT_TOP + + + + + +PTR + + + +*ptr + + +struct + + + +TOP->PTR + + + + + +STRUCT_TOP + +⊤ +:struct + + + +TOP->STRUCT_TOP + + + + + +MEM_TOP + +⊤ +:mem + + + +TOP->MEM_TOP + + + + + +TUPLE_TOP + +⊤ +:tuple + + + +TOP->TUPLE_TOP + + + + + +BOT + +⊥ + + + +CTRL + +⊥ +:ctrl + + + +XCTRL->CTRL + + + + + +CTRL->BOT + + + + + +INT_HI + +[min>max] + + + +INT_TOP->INT_HI + + + + + +INT_BOT + +⊥ +:int + + + +INT_BOT->BOT + + + + + +INT_XBOOL + +[1...0] + + + +INT_HI->INT_XBOOL + + + + + +INT_CON + +constants + + + +INT_HI->INT_CON + + + + + +INT_CON0 + +0 + + + +INT_XBOOL->INT_CON0 + + + + + +INT_CON1 + +1 + + + +INT_XBOOL->INT_CON1 + + + + + +INT_LO + +[min<max] + + + +INT_CON->INT_LO + + + + + + +INT_BOOL + +[0...1] + + + +INT_CON0->INT_BOOL + + + + + + +INT_CON1->INT_BOOL + + + + + +INT_BOOL->INT_LO + + + + + +INT_LO->INT_BOT + + + + + +FLT_CON64 + +64b consts +[excludes +32b consts] + + + +FLT_TOP->FLT_CON64 + + + + + +FLT_CONPI + +3.14159... + + + +FLT_TOP->FLT_CONPI + + + + + +FLT_X32 + +~f32 + + + +FLT_TOP->FLT_X32 + + + + + +FLT_BOT + +⊥ +:flt + + + +FLT_BOT->BOT + + + + + +FLT_CON64->FLT_BOT + + + + + +FLT_CON32 + +32b consts + + + + +FLT_32 + +f32 + + + +FLT_CON32->FLT_32 + + + + + +FLT_CONPI->FLT_BOT + + + + + + +FLT_X32->FLT_CON32 + + + + + +FLT_CON0 + +0. + + + +FLT_X32->FLT_CON0 + + + + + + +FLT_CON0->FLT_32 + + + + + +FLT_32->FLT_BOT + + + + + +PTR0 + + + +*ptr? + + +struct + + + +PTR->PTR0 + + + + + +PTR0->BOT + + + + + +STRUCT_S1 + + + +field + + + + + + + + +int/ptr + + + +STRUCT_TOP->STRUCT_S1 + + + + + +STRUCT_S2 + + + +field + + + + + + + + +int/ptr + + +field + + + + + + + + +int/ptr + + + +STRUCT_TOP->STRUCT_S2 + + + + + +STRUCT_SN + + + +field + + + + + + + + +int/ptr + + +...   + + + + + + + + +int/ptr + + +field + + + + + + + + +int/ptr + + + +STRUCT_TOP->STRUCT_SN + + + + + +STRUCT_BOT + +⊥ +:struct + + + +STRUCT_BOT->BOT + + + + + +STRUCT_S1->STRUCT_BOT + + + + + + +STRUCT_S2->STRUCT_BOT + + + + + + +STRUCT_SN->STRUCT_BOT + + + + + +MEM_ALIAS1 + +#1 + + + +MEM_TOP->MEM_ALIAS1 + + + + + +MEM_ALIASDOTS + +... + + + +MEM_TOP->MEM_ALIASDOTS + + + + + +MEM_ALIASN + +#n + + + +MEM_TOP->MEM_ALIASN + + + + + +MEM_BOT + +⊥ +:mem + + + +MEM_BOT->BOT + + + + + +MEM_ALIAS1->MEM_BOT + + + + + + +MEM_ALIASDOTS->MEM_BOT + + + + + + +MEM_ALIASN->MEM_BOT + + + + + +TUPLE1 + + + +Tuple + + +type + + + +TUPLE_TOP->TUPLE1 + + + + + +TUPLE2 + + + +Tuple + + +type + + +type + + + +TUPLE_TOP->TUPLE2 + + + + + +TUPLEN + + + +Tuple + + +type + + +... + + +type + + + +TUPLE_TOP->TUPLEN + + + + + +TUPLE_BOT + +⊥ +:tuple + + + +TUPLE_BOT->BOT + + + + + +TUPLE1->TUPLE_BOT + + + + + +TUPLE2->TUPLE_BOT + + + + + +TUPLEN->TUPLE_BOT + + + + + diff --git a/docs/chapter25/lattice2.gv b/docs/chapter25/lattice2.gv new file mode 100644 index 00000000..6aa37fa0 --- /dev/null +++ b/docs/chapter25/lattice2.gv @@ -0,0 +1,85 @@ +digraph Lattice { + compound = true; + // Define nodes + splines=false; + node [shape=oval, style=filled]; + nodesep=0.2; + ranksep=0.4; + + TOP [label="~scalar\n+int+tmp+0", fillcolor=black, fontcolor=white, group=center]; + + // ------------------------ + PTRHQ [label="~tmp\n+tmp+0", fillcolor=green, fontcolor=white, group=left]; + TOP -> PTRHQ + + VALH_ [label="~nscalar\n+int+tmp", fillcolor=white, group=center]; + TOP -> VALH_ + + INTHQ [label="~int\n+int+0", fillcolor=lightblue, group=right]; + TOP -> INTHQ + + LEFTHI [label="junk", style=invis, group=junkleft] + + + PTRHQ -> LEFTHI-> VALH_ -> INTHQ [style=invis]; + { rank=same; PTRHQ; LEFTHI; VALH_; INTHQ } + + // ------------------------ + PTRH_ [label="~ntmp\n+tmp", fillcolor=green, fontcolor=white, group=left]; + VALH_ -> PTRH_ + PTRHQ -> PTRH_ + + INTH_ [label="~nint\n+int", fillcolor=lightblue, group=right]; + VALH_ -> INTH_ + INTHQ -> INTH_ + { rank=same; PTRH_; INTH_ } + + // ------------------------ + NILX_ [label="~nil\n[int,tmp]+0", fillcolor=aqua, group=junkleft]; + INTHQ -> NILX_ + PTRHQ -> NILX_ + + NIL__ [label="nil\n[+int+tmp],0", fillcolor=pink]; + VALH_ -> NIL__; + + NILX_ -> NIL__ [style=invis]; + { rank=same; NIL__; NILX_ } + + // ------------------------ + PTRL_ [label="ntmp\ntmp", fillcolor=green, fontcolor=white, group=left]; + PTRH_ -> PTRL_ + + INTL_ [label="nint\nint", fillcolor=lightblue, group=right]; + INTH_ -> INTL_ + { rank=same; PTRL_; INTL_ } + + // ------------------------ + PTRL0 [label="tmp\ntmp,0", fillcolor=green, fontcolor=white, group=left]; + PTRL_ -> PTRL0 + NIL__ -> PTRL0 + + VALL_ [label="nscalar\nint,tmp", fillcolor=white, group=center]; + PTRL_ -> VALL_ + INTL_ -> VALL_ + NILX_ -> VALL_ + + INTL0 [label="int\nint,0", fillcolor=lightblue, group=right]; + INTL_ -> INTL0 + NIL__ -> INTL0 + + PTRL0 -> VALL_ -> INTL0 [style=invis]; + { rank=same; PTRL0; VALL_; INTL0 } + + // ------------------------ + BOT [label="scalar\nint,tmp,0", fillcolor=black, fontcolor=white, group=center]; + PTRL0 -> BOT + INTL0 -> BOT + VALL_ -> BOT + + // ------------------------ + // Verticle alignment + TOP -> VALH_ -> VALL_ -> BOT [style=invis]; + PTRHQ -> PTRH_ -> PTRL_ -> PTRL0 [style=invis]; + INTHQ -> INTH_ -> INTL_ -> INTL0 [style=invis]; + LEFTHI -> NILX_ [style=invis] +} diff --git a/docs/chapter25/lattice2.svg b/docs/chapter25/lattice2.svg new file mode 100644 index 00000000..8b682aa7 --- /dev/null +++ b/docs/chapter25/lattice2.svg @@ -0,0 +1,260 @@ + + + + + + +Lattice + + + +TOP + +~scalar ++int+tmp+0 + + + +PTRHQ + +~tmp ++tmp+0 + + + +TOP->PTRHQ + + + + + +VALH_ + +~nscalar ++int+tmp + + + +TOP->VALH_ + + + + + + +INTHQ + +~int ++int+0 + + + +TOP->INTHQ + + + + + + + +PTRH_ + +~ntmp ++tmp + + + +PTRHQ->PTRH_ + + + + + + +NILX_ + +~nil +[int,tmp]+0 + + + +PTRHQ->NILX_ + + + + + + +VALH_->PTRH_ + + + + + +INTH_ + +~nint ++int + + + +VALH_->INTH_ + + + + + +NIL__ + +nil +[+int+tmp],0 + + + +VALH_->NIL__ + + + + + +VALL_ + +nscalar +int,tmp + + + + +INTHQ->INTH_ + + + + + + +INTHQ->NILX_ + + + + + + + +PTRL_ + +ntmp +tmp + + + +PTRH_->PTRL_ + + + + + + +INTL_ + +nint +int + + + +INTH_->INTL_ + + + + + + + +NILX_->VALL_ + + + + + +PTRL0 + +tmp +tmp,0 + + + +NIL__->PTRL0 + + + + + +INTL0 + +int +int,0 + + + +NIL__->INTL0 + + + + + +PTRL_->PTRL0 + + + + + + +PTRL_->VALL_ + + + + + +INTL_->VALL_ + + + + + +INTL_->INTL0 + + + + + + + +BOT + +scalar +int,tmp,0 + + + +PTRL0->BOT + + + + + + +VALL_->BOT + + + + + + +INTL0->BOT + + + + + diff --git a/docs/chapter25/pardoe.docx b/docs/chapter25/pardoe.docx new file mode 100644 index 00000000..24fde921 Binary files /dev/null and b/docs/chapter25/pardoe.docx differ diff --git a/docs/chapter25/webtcp.min.js b/docs/chapter25/webtcp.min.js new file mode 100644 index 00000000..bf23ee5a --- /dev/null +++ b/docs/chapter25/webtcp.min.js @@ -0,0 +1,65 @@ +function Http(j){this.cID=Math.random().toString(36).substr(2);this.webtcp=j;this.webtcp.httpClients[this.cID]=this;this.createPacket=function(){return{cID:this.cID,type:"http",host:this.remoteAddress,port:this.remotePort,method:null,data:null}};this.ondata=function(){};this.get=function(g,c){this.on("data",c);this.rpc("httpGet",[g])};this.post=function(g,c,j){this.on("data",j);this.rpc("httpPost",[g,c])}}Http.prototype=new WebTCPIO;function Socket(j,g,c,p){this.sID=Math.random().toString(36).substr(2);this.webtcp=j;this.closed=this.ready=!1;this.options=p;this.sockOpts={_updating:!1};this.remoteAddress=g;this.remotePort=c;this.webtcp.sockets[this.sID]=this;this.createPacket=function(){return{id:Math.random().toString(36).substr(2),sID:this.sID,type:"tcp",options:this.options,host:this.remoteAddress,port:this.remotePort,data:null}};this.onconnect=function(){this.webtcp.sockets[sID].ready=!0;this.webtcp.sockets[sID].closed=!1}; +this.ondata=function(){};this.onend=function(){};this.ontimeout=function(){};this.ondrain=function(){};this.onclose=function(){this.webtcp.sockets[sID].ready=!1;this.webtcp.sockets[sID].closed=!0};this.getSockOpts=function(){this.sockOpts._updating=!0;this.rpc("getSockOpts")};this.onSockOptsRcv=function(c){this.sockOpts=c;this.sockOpts._updating=!1}}Socket.prototype=new WebTCPIO;var JSON;JSON||(JSON={}); +(function(){function j(c,p){var q,t,n,x,i=k,e,l=p[c];l&&"object"==typeof l&&"function"==typeof l.toJSON&&(l=l.toJSON(c));"function"==typeof r&&(l=r.call(p,c,l));switch(typeof l){case "string":return g(l);case "number":return isFinite(l)?String(l):"null";case "boolean":case "null":return String(l);case "object":if(!l)return"null";k+=v;e=[];if("[object Array]"===Object.prototype.toString.apply(l)){x=l.length;for(q=0;qc?"0"+c:c}"use strict";"function"!=typeof Date.prototype.toJSON&&(Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+c(this.getUTCMonth()+1)+"-"+c(this.getUTCDate())+"T"+c(this.getUTCHours())+":"+c(this.getUTCMinutes())+":"+c(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()});var p=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, +n=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,k,v,D={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},r;"function"!=typeof JSON.stringify&&(JSON.stringify=function(c,g,p){var n;v=k="";if("number"==typeof p)for(n=0;n=a};c.countRTO=function(a){var b;return 100b;b++)d.push(String.fromCharCode(b));B=(r.lastIndex=0,d.join("").replace(r,function(a){return c[a]="\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4),""}),r.lastIndex=0,c)}a=a.replace(r,function(a){return B[a]})}return a};var q="websocket xdr-streaming xhr-streaming iframe-eventsource iframe-htmlfile xdr-polling xhr-polling iframe-xhr-polling jsonp-polling".split(" "); +c.probeProtocols=function(){for(var a={},b=0;b