How dstogov/simple works
Main Components
• Frontend (Language Parsing): At the beginning of the pipeline, the “Parser” (found in each chapter’s com/seaofnodes/simple package) converts the Simple source code into an initial IR. This includes the recursive descent parser and the AST-to-IR conversion.
• Intermediate Representation (IR) & Node System: The “node” package (embedded in nearly every chapter) defines the core IR classes. This is the heart of the Sea‑of‑Nodes approach, where each node represents an operation (e.g., AddNode, IfNode, LoopNode). Also include supporting “type” classes (e.g., TypeInteger, TypeTuple) that annotate the IR.
• Optimizations and Transformations: Multiple chapters showcase optimizations. Illustrate components such as:
– Peephole optimizers (e.g., IterPeeps, constant folding algorithms)
– Control Flow Graph (CFG) and SSA transformations – restructuring, dead code elimination, and global value numbering.
– Global Code Motion and Scheduling: Modules like GlobalCodeMotion and the scheduling tests are shown here.
• Backend / Code Generation:
– Evaluator: Early chapters show an interpreter (Evaluator) that “executes” the IR.
– Code Generation: In later chapters (e.g., Chapters 19 and 20), the IR is transformed via instruction selection (CodeGen) and then register allocation (RegAlloc) for target machines (with submodules for RISC-V, x86_64, ARM, etc.).
• Testing / Transpiling Module: There is also a “transpile-tests” module that demonstrates another angle by translating IR constructs into another language (Rust, for example) and verifies them using JUnit tests.
Relationships and Workflow
– Data flows from source programs written in Simple into the Parser.
– Parsed results are used to build the IR (nodes and control flow graphs).
– The IR proceeds through several layers of optimization (peephole, CFG transformations, SSA optimizations) and may be visualized via GraphVisualizer/IRPrinter.
– Finally, the IR is either interpreted directly, transformed into target specific intermediate code via CodeGen, and then advanced (e.g., register allocation in Chapter 20) for eventual execution.
– Alongside these processing stages, testing frameworks validate each chapter’s implementation.
Architectural Patterns & Design Principles
• Layered/Modular Design: The system naturally splits into frontend, middle-end (IR manipulation and optimization), and backend.
• Separation of Concerns: While the IR and node system encapsulate the program’s core, optimizations, code generation, and debugging are treated as clearly distinct modules that interact through well-defined interfaces.
Relevant Technologies and Artifacts
– Technologies: The project is implemented in Java, and uses Maven (noted by the multiple pom.xml files). Testing is done via the JUnit framework.