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.
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.
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.
Consult the following resources as documentation on the RISC-V architecture and instruction set:
RISC-V Reference by James Zhu. Provides a concise overview of the instruction set.
Venus Editor on ChocoPy site.
ChocoPy Reference Compiler on the ChocoPy site. Produces RISC-V code with annotations.
RISC-V Specifications. The offical specification.
RISC-V Assembly Programmer’s Manual for an overview of the instruction set.
The prebuilt
directory of your project template should have
The template should also have implementations of the ChocoPy parser and type checker.
Develop tests in SPT to test your compiler.
module test
language chocopy
test 1+1 [[
1+1
]] transform "To RV32IM and execute" to "2"