Skip to content

Compilation Process

Mike Anderson edited this page Jul 16, 2017 · 6 revisions

Phase of compilation

Source Loader

Source is loaded as needed and converted into one or more Strings

Reader

Each String containing Magic source code is parsed into a list of "Raw" AST Nodes.

These raw nodes have not yet been fully analysed: in particular:

  • No macros / expanders will have been run
  • No analysis of symbols will have been performed - they are just Lookup nodes containing symbols.

We don't currently parse into data structures for several reasons:

  • We want to attach source location directly to the AST nodes.
  • Simplicity: it avoids an extra layer of analysis converting data structures -> ASTs
  • Performance: it avoids extra processing and intermediate object creation

If you want to round-trip between code and data in true Lisp style, you still can, using:

  • node.toForm() to convert AST nodes to equivalent data structures
  • Node.toNode(form) to convert back into AST nodes

Expander

Raw AST nodes are converted into semantic AST nodes by a recursive expansion procedure:

  • Expand the top level form
  • Expand subforms

Expansion in Magic uses Expansion Passing Style to allow extensible expansion methods to be developed (inspired by this paper: ftp://www.cs.indiana.edu/pub/techreports/TR195.pdf)

Analyser

Once nodes are fully expanded, they are analysed in the current context.

Intention of analysis includes:

  • Resolving symbols in the current context (if available, otherwise a symbolic dependency is created)
  • Initial type analysis and type checking

Optimiser

Fully analysed AST nodes are optimised, taking advantage of:

  • Type information of nodes and subnodes
  • Known context information

Context Compiler

At this point, all symbolic dependencies and types of nodes should be known.

The nodes are then evaluated.

Nodes which implement def cause the definition AST nodes to be cached in the current context for later optimisation / compilation on demand. The analysis, optimisation and compilation of these nodes is repeated if any dependencies are modified in the context.

Clone this wiki locally