The compiler compiles from Fasto to Mips assembly.
This is an attempt to port a compiler for the language Fasto from SML to Haskell. Small extensions to the language definition has been made such that it for example is possible to write several declarations within a let statement.
Two library functions are provided: read and write. They compile to system calls compatible with the MARS implementation of the MIPS architechture.
fun int fact(int n) =
if n == 0
then 1
else n*fact(n-1)
fun int main() =
let n = read()
p = fact(n)
in write(p)
fun int fibo(int n) =
if n == 0 then 0
else if n == 1 then 1
else fibo(n - 1) + fibo(n - 2)
fun int main() =
let n = read()
p = fibo(n)
in write(p)
The compiler consists of the following modules
- FastoParser.hs
- Parses the fasto code and generates an abstract syntax tree (AST). The types in the AST are defined in the file Fasto.hs.
- FastoCodeGenerator.hs
- Takes the Fasto AST as input and creates another AST as output. The output AST is of an intermediate language type. The syntax of the intermediate language is defined in Imp.hs. It is a subset of the intermediate language defined in Torben Ægidius Mogensen's Introduction to Compiler Design.
- MipsCodeGenerator.hs
- Generates the symbolic machine code where an infinite number of registers are available but where calling conventions are observed.
- MipsRegAlloc.hs
- Translates from MIPS code with symbolic registers to MIPS code with actual register names. The register names that in the input start with "$" are not mapped to real registers since these registers are already real registers which are set by the MipsCodeGenerator in order to observe the calling conventions.
- TypeChecker.hs
- Not yet being used by Main.hs. Should be inserted between the FastoParser.hs and the FastoCodeGenerator.hs modules.
The calling conventions found in http://www.cs.cornell.edu/courses/cs3410/2012sp/lecture/14-calling-w.pdf is followed at the moment.
You can run
stack install
or compile manually using
ghc Main.hs
And the needed modules should also be installed manually.
The compiled assembly code can be run using the MARS MIPS Simulater available at http://courses.missouristate.edu/KenVollmar/MARS/
The project is released under the MIT license. See LICENSE.txt in the root directory.