# Chapter 1 In this chapter we aim to compile simple scripts such as: ``` return 1; ``` We implement the `return` statement. The `return` statement accepts an `expression` as an argument. The only `expression` type is an integer literal, as shown in the example above. Here is the [complete language grammar](docs/01-grammar.md) for this chapter. To implement this simple language, we introduce a few key components and data structures. ## Implementation Language Our implementation language is Java. We chose Java as it is widely available and understood. ## Assumptions We assume that the reader is familiar with traditional linear intermediate representations, and is familiar with terms such as Basic Block, Control Flow Graph, etc. No attempt is made to explain these topics. If necessary the reader can consult a standard compiler text book. ## Architecture We construct the intermediate Sea of Nodes (SoN) representation directly as we parse the language. There is no Abstract Syntax Tree representation. The reason for this is to demonstrate a key benefit of the SoN IR: a number of pessimistic peephole optimizations can be performed while parsing a language. This aspect is more fully explored from [Chapter 2](../chapter02/README.md) onwards. ## Data Structures Our data structures are based upon the descriptions provided in following papers: * [From Quads to Graphs: An Intermediate Representation's Journey](http://softlib.rice.edu/pub/CRPC-TRs/reports/CRPC-TR93366-S.pdf) * [Combining Analyses, Combining Optimzations](https://scholarship.rice.edu/bitstream/handle/1911/96451/TR95-252.pdf) * [A Simple Graph-Based Intermediate Representation](https://www.oracle.com/technetwork/java/javase/tech/c2-ir95-150110.pdf) * [Global Code Motion Global Value Numbering](https://courses.cs.washington.edu/courses/cse501/06wi/reading/click-pldi95.pdf) Following the lead from above, we represent our intermediate representation using an object oriented data model. Details of the representation follow. ### Intermediate Representation as a Graph of Nodes The intermediate representation is a graph of Node objects. The `Node` class is the base type for objects in the IR graph. The `Node` class provides common capabilities that are inherited by all subtypes. Each subtype implements semantics relevant to that subtype. Each `Node` represents an "instruction" as it may appear in traditional IRs. ### Nodes are in a Graph The key idea of the Sea of Nodes IR is that each Node is linked to other Nodes by def-use dependencies. As this is such an important and fundamental aspect of the IR, it is important to understand how we implement this, and depict in graph visuals. The base `Node` class maintains a list of Nodes that are inputs to it. An input is an edge from a "def" to a "use". What this means is that if `B` is definition, and `A` uses `B`, then there is a def-use edge from `B` to `A`. Visually we show an arrow from the "use" to the "def". Here is an example:  From an implementation point of view, our `Node` type also maintains a reverse link. This means that in the above scenario: * Since `A` is a "use" of `B`, then `B` will appear in `A`'s list of inputs. * Conversely, `B` maintains a list of outputs, and `A` will appear in this list. * A key invariant is that these edges must be in sync at all times. Here is how this looks in the `Node` class: ```java public abstract class Node { /** * Inputs to the node. These are use-def references to Nodes. *
* 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 Control node.
*/
public final ArrayList
* 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