Lab 9: A Basic Compiler Pipeline

Project
November 19, 2021

In this lab you make a start with compiling ChocoPy to RISC-V code using Stratego. You translate expression statements with expressions containing constants, operators, and variables. While it would be possible to completely compute the values of such expressions using constant folding, this is not the aim of this lab. Rather, we want the basis for a translation of expressions using variables and function calls as well.

A Minimial Viable Example

The following is a minimal ChocoPy program:

y : int = 3
1 + y

Aim to get the complete compiler pipeline working for this example first. Then work on extending the various stages.

Nano-Pass Compiler Architecture

Follow the setup of the pipeline in Essentials of Compiling consisting of the following strategies:

compile =
  compile-cpy-to-cir
  ; compile-cir-to-rv32im
  ; compile-rv32im

compile-cpy-to-cir =
  explicate-types
  ; desugar
  ; uniquify
  ; remove-complex-operands
  ; explicate-control

compile-cir-to-rv32im =
  select-instructions-cprogram

compile-rv32im =
  assign-homes
  ; patch-instructions

Be sure to replace the compile rule in src/compile.str2 with the above compile strategy.

RISC-V Resources

Consult the following resources as documentation on the RISC-V architecture and instruction set:

Prebuilt

The prebuilt directory of your project template should have

  • The ChocoPy signature and pretty-printer
  • The RV32IM signature and pretty-printer

The template should also have implementations of the ChocoPy parser and type checker.

Compiler Testing

Develop tests in SPT to test your compiler.

module test

language chocopy

test 1+1 [[
1+1
]] transform "To RV32IM and execute" to "2"