Skip to content

IvanxAj/C90-compiler

Repository files navigation

C90 to RISC-V Compiler

This repository contains the source code for a pre-processed ANSI C to RISC-V assembly compiler. This project currently scores 84% of all seen test cases.

Program build and execution

The compiler is built using the command:

 make bin/c_compiler

The compilation function is invoked using the flag -S, with the source file and output file specified on the command line:

bin/c_compiler -S [source-file.c] -o [dest-file.s]

The test script can be run by

./test.sh

./test_single.sh single.c

docs

In this directory, a concise overview of the final implementation and an explanation of various components can be found in summary.

For those interested in the development journey, a journal is also provided, detailing changes over time.

Specification

All input source code is valid and there is no expectation for the compiler to optimise its input. Specification

Some supported features

Basic

  • single function with no arguments
  • variables of int type
  • local variables
  • arithmetic and logical expressions
  • if-else statements
  • while loops

Intermediate

  • multiple functions that call each other
  • functions that take up to eight parameters
  • for loops
  • arrays declared locally (i.e. inside a function)
  • reading and writing elements of an array
  • recursive function calls
  • switch statements
  • the break and continue keywords
  • the enum keyword
  • arrays declared globally

Advanced

  • variables of double, float, unsigned, and pointer types
  • mutually recursive function calls
  • locally scoped variable declarations (e.g. a variable that is declared inside the body of a while loop, such as while(...) { int x = ...;}.
  • the sizeof(...) function (which takes either a type or a variable)
  • taking the address of a variable using the & operator
  • dereferencing a pointer-variable using the * operator
  • pointer arithmetic
  • typedef keyword
  • declaration and use of structs
  • variables of char and struct types
  • strings (as NULL-terminated character arrays)

Acknowledgements

  • The coursework was originally designed by David Thomas, who lectured this module until 2017-18. It is now maintained by John Wickerson, to whom any feedback should be sent.
  • Thanks to James Nock for overhauling the scripts for configuring the development environment, for writing detailed instructions for setting this up on various operating systems, and for creating GitHub actions capable of automatically testing compilers.

About

C90 to RISC-V Compiler

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published