Divyansh Manocha

Electronic and Information (MEng) Engineering

Imperial College London

divyanshmanocha [AT] gmail [DOT] com

Ansi C to MIPS Compiler

Built in C++

Project Specification

The program accepts C source code on stdin and write a heirarchical representation on stdout.

Input Format: The input format is C90

Output Format: The output format is MIPS1 assembly code.

It is possible to assemble and link this code against a C run-time, and have it execute correctly on a MIPS processor as emulated by qemu-mips.

A simplified MIPS architecture. Diagram from Mark S. Schmalz’s lecture series.

Approach

The API involves a context class and memory class. Each class can thereafter generate their own code, without considering other classes.

Context is used to keep track of the scoping, as well as mapping of the variable. The context knows the current scope, and therefore all the declarations within the scope and above it; essentially forming a separate symbol table for each scope. The index of the vector corresponds to the current scope, and the vectors in the structs store the bindings. Subroutines can be called from the context class, as it keeps track of the current function. Allocation of appropriate amounts of memory is evaluated using a separate member function.

Registers and stack allocation are handled by a separate class, passed in the same way as the context. A vector of pointers to structs (that have their own vector) is used for this purpose, with the current value being a string (as they do not have to be initialised). This allows the obtaining of free space (as stack or registers), writing to it, and freeing it as and when necessary. Whether or not a register or the stack is required depends on the member function using it.

Project Outcomes

Compilation: The compiler should be built using: make bin/c_compiler The target environment is Ubuntu 16.04. A test framework was also created for the compiler.

Supported Features


GitHub link: https://github.com/divyanshmanocha/C_compiler