Compiler Design - nponeccop/HNC GitHub Wiki

The HN Compiler Design

Cabal Targets

The .cabal file contains 7 targets. 4 for HN:

  • spl-hnc - command line HN compiler. Consists of command-line handler, HN parser, type checker, optimizer and C++ back-end.

  • spl-test-hunit - main test suite designed to be run by cabal test. Currently broken (builds but doesn't run).

  • spl-test-hunit-exe - standalone executable for main test suite. Mostly tests type checker, optimizer and C++ back-end. Will be replaced by spl-test-hunit once it's ready.

  • spl-tests-adhoc-hn - a deprecated test suite for HN. Contains mostly HN parser tests and a few basic C++ backend tests. Adhoc means HUnit is not used. Tests should be migrated to the main suite.

And 3 for SPL:

  • spli - "SPL interactive", interpreter and REPL shell for SPL language. Works but deprecated.

  • splm - "SPL mobile". An executable that interprets a fixed SPL program. Works but deprecated.

  • spl-test-adhoc-spl - a deprecated test suite for SPL

SPL stuff should be either thrown away (because it has already been rewritten and integrated into HNC, e.g. type checker) or rewritten and integrated into HNC (e.g. SPL parser, interpreter and REPL).

HNC

The HN command-line compiler is generated by spl-hnc Cabal target. Originally it should have supported multiple source languages (HN and SPL) and multiple target languages (CPP, Perl and JavaSript). The CPP target was perceived as the hardest to implement, so we started from it and now the compiler only supports HN -> CPP scenario, with some rudimentary code for SPL -> CPP scenario.

It consists of:

  1. Command-line handler
  2. Compiler facade (a set of functions used to run parts of the compiler by both HNC and tests)
  3. HN parser
  4. Type checker
  5. Optimizer
  6. C++ generator
  7. C++ pretty printer

Data flows from top to bottom in the list.

To save lines of code, all non-trivial parts are implemented using third-party Haskell components designed for other compilers:

  • the type checker and the C++ generator use UUAGC - a compiler for a DSL for writing tree folds in Haskell, a part of UHC Haskell Compiler
  • the optimizer uses HOOPL - a library for control-flow optimization, a part of GHC Haskell Compiler.