The Implementation of Icon and Unicon a Compendium Clinton Jeffery and Don Ward, Editors
Total Page:16
File Type:pdf, Size:1020Kb
The Implementation of Icon and Unicon a Compendium Clinton Jeffery and Don Ward, editors The Implementation of Icon and Unicon Ralph and Madge T. Griswold Kenneth W. Walker Clinton L. Jeffery Michael D. Wilder Anthony T. Jones Jafar Al Gharaibeh Copyright © 2017 – 2020 Clinton Jeffery Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foun- dation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Portions of this document ("The Implementation of the Icon Programming Language") are in the public domain and not subject to the above copyright or license. Other portions of this document ("An Optimizing Compiler for Icon") are copyrighted by Kenneth Walker and appear in edited form in this document with the express permission of the author. This is a draft manuscript dated April 3, 2020. Send comments and errata to [email protected]. This document was prepared using LATEX. Special thanks to Don Ward for assistance with the conversion to LATEX. Contents Preface xvii Compendium Introduction.................................1 I The Implementation of the Icon Programming Language3 1 Introduction5 1.1 Implementing Programming Languages.......................6 1.2 The Background for Icon...............................7 2 Icon Language Overview9 2.1 The Icon Programming Language.......................... 10 2.1.1 Data Types.................................. 10 2.1.2 Expression Evaluation............................ 12 2.1.3 Csets and Strings............................... 19 2.1.4 String Scanning................................ 21 2.1.5 Lists...................................... 22 2.1.6 Sets...................................... 24 2.1.7 Tables..................................... 25 2.1.8 Records.................................... 25 2.1.9 Input and Output............................... 26 2.1.10 Procedures.................................. 26 2.1.11 Co-Expressions................................ 31 2.1.12 Diagnostic Facilities............................. 33 2.2 Language Features and the Implementation..................... 35 3 Organization of the Implementation 39 3.1 The Icon Virtual Machine............................... 40 3.2 Components of the Implementation ........................ 40 3.3 The Translator..................................... 41 iii 3.4 The Linker...................................... 42 3.4.1 Scope Resolution............................... 43 3.4.2 Construction of Run-Time Structures.................... 43 3.5 The Run-Time System................................ 43 4 Values and Variables 47 4.1 Descriptors...................................... 48 4.1.1 Strings.................................... 49 4.1.2 The Null Value................................ 50 4.1.3 Integers.................................... 50 4.2 Blocks......................................... 51 4.3 Variables....................................... 52 4.3.1 Operations on Variables........................... 53 4.3.2 Special Variables............................... 54 4.3.2.1 Trapped Variables......................... 54 4.3.2.2 Keyword Variables......................... 55 4.4 Descriptors and Blocks in C............................. 56 4.4.1 Descriptors.................................. 56 4.4.2 Blocks.................................... 57 4.4.3 Defined Constants.............................. 58 4.4.4 RTL Coding................................. 60 5 Strings and Csets 65 5.1 Strings......................................... 66 5.1.1 Representation of Strings........................... 66 5.1.2 Concatenation................................ 67 5.1.3 Substrings................................... 70 5.1.4 Assignment to Subscripted Strings...................... 71 5.1.5 Mapping................................... 73 5.2 Csets.......................................... 77 6 Lists 81 6.1 Structures for Lists.................................. 81 6.2 Queue and Stack Access............................... 85 6.3 Positional Access................................... 93 6.4 Arrays (Unicon only)................................. 96 7 Sets and Tables 99 7.1 Sets.......................................... 100 7.1.1 Data Organization for Sets.......................... 100 7.1.2 Set Operations................................ 102 7.2 Tables......................................... 103 7.2.1 Data Organization for Tables......................... 103 7.3 Hashing Functions.................................. 106 8 The Interpreter 115 8.1 Stack-Based Evaluation................................ 115 8.2 Virtual Machine Instructions............................. 116 8.2.1 Constants................................... 117 8.2.2 Identifiers................................... 118 8.2.3 Operators................................... 126 8.2.4 Functions................................... 128 8.2.5 Self-Modifying Instructions......................... 129 8.3 The Interpreter Proper................................. 130 8.3.1 The Interpreter Loop............................. 130 8.3.2 Interpreter State Variables.......................... 132 9 Expression Evaluation 135 9.1 Bounded Expressions................................. 136 9.1.1 Expression Frames.............................. 138 9.2 Failure......................................... 140 9.3 Generators and Goal-Directed Evaluation...................... 143 9.4 Generative Control Structures............................ 156 9.4.1 Alternation.................................. 156 9.4.2 Repeated Alternation............................. 158 9.4.3 Limitation................................... 159 9.5 Iteration........................................ 160 9.6 String Scanning.................................... 161 10 Functions, Procedures, and Co-Expressions 167 10.1 Invocation Expressions................................ 167 10.2 Procedure Blocks................................... 168 10.3 Invocation....................................... 171 10.3.1 Argument Processing............................. 171 10.3.2 Function Invocation............................. 172 10.3.3 Procedure Invocation............................. 175 10.4 Co-Expressions.................................... 177 11 Storage Management 185 11.1 Memory Layout.................................... 187 11.2 Allocation....................................... 190 11.2.1 The Static Region............................... 191 11.2.2 Blocks.................................... 191 11.2.3 Strings.................................... 191 11.3 Garbage Collection.................................. 192 11.3.1 The Basis................................... 193 11.3.2 The Location Phase.............................. 194 11.3.3 Pointer Adjustment and Compaction..................... 204 11.3.4 Collecting Co-Expression Blocks...................... 214 11.3.5 Multiple Regions............................... 215 11.3.6 Storage Requirements during Garbage Collection.............. 216 11.4 Predictive Need.................................... 217 12 Run-Time Support Operations 223 12.1 Type Checking and Conversion............................ 223 12.2 Dereferencing and Assignment............................ 228 12.2.1 Dereferencing................................. 229 12.2.2 Assignment.................................. 231 12.3 Input and Output................................... 237 12.3.1 Files...................................... 237 12.3.2 Reading and Writing Data.......................... 238 12.4 Diagnostic Facilities................................. 239 II An Optimizing Compiler for Icon 243 Preface to Part II...................................... 245 13 The Optimizing Compiler 247 13.1 Motivation....................................... 247 13.2 Type Inferencing................................... 248 13.3 Liveness Analysis................................... 249 13.4 Analyzing Goal-Directed Evaluation......................... 250 14 The Translation Model 253 14.1 Data Representation.................................. 254 14.2 Intermediate Results................................. 254 14.3 Executable Code................................... 255 15 The Type Inferencing Model 263 15.1 Motivation....................................... 263 15.2 Abstract Interpretation................................ 264 15.3 Collecting Semantics................................. 267 Model 1: Eliminating Control Flow Information...................... 270 Model 2: Decoupling Variables............................... 272 Model 3: A Finite Type System............................... 275 16 Liveness Analysis of Intermediate Values 279 16.1 Implicit Loops..................................... 280 16.2 Liveness Analysis................................... 281 16.3 An Attribute Grammar................................ 285 16.4 Primary Expressions................................. 286 16.5 Operations with Subexpressions........................... 287 16.6 Control Structures................................... 289 17 Overview of the Compiler 293 17.1 Components of the Compiler............................. 293 17.2 The Run-time System................................. 294 17.3 The Implementation Language............................ 295 17.4 Standard and Tailored Operation Implementations.................. 298 18 Organization of Iconc 301 18.1 Compiler Phases................................... 301 18.2