A compiler for a subset of C written in C.
This is a C to x86_64 assembly compiler.
It is structured as a standard 3-phase compiler. That means, it has a very simple lexer, and RD (recursive-descent) parser
and code generator for x86_64.
Thanks to GeorgeLs for his contribution to the project.
To use the compiler, you first have to compile it. You can use the compile.sh script for that. This will create
the compiler executable 'dicc'.
To compile a program using dicc, run it and pass as command line argument its file name, like: ./dicc [name].c
Example: ./dicc test.c
This will create an x86_64 [name].s assembly file. To create an executable out of that, you just use some assembler, like gcc:
gcc [name].s
I also have included a test.sh script for ease of use with some test file named test.c
Currently, it supports:
- integers (no other types yet)
- logical NOT (
!a
) - bitwise NOT (
~a
) - logical AND (
a && b
) - logical OR (
a || b
) - addition, subtraction, multiplication, division (
+
,-
,*
,/
) - comparison operators (
<
,<=
,>
,>=
,==
) - Any binary expression that combines any of the above expressions.
- C89 style comments (
/* ... */
) - Only one function, main
- return statement
- custom print statement (not in any C standard)
- if/else statements (
if (exp) { stat1; stat2; ... } else { stat11; stat12; ... }
) - local variables: It has function scope only and declare anywhere, but not block scope yet.
- while loops (
while (exp) { stat1; stat2; ... }
) whithbreak
andcontinue
- Possibly other things that I forget...
Generally, the initial target feature set was one, so that the compiler could compile any assignment 1 from
the course Introduction to Programming: http://cgi.di.uoa.gr/~ip/
There are still some things missing. The main one is preprocessor support and also some not-so-important features
(like support for all loop types or else if statements). However, it seems that you can compile any assigment 1 source code
with little modification.
The main goal of this project is to make a descent C compiler written in C, totally from scratch. Currently, apart from the heavy commenting, the compiler tries to put a clear boundary between each phase of compilation. At first, the lexer is run, and outputs its results. Its output is then passed to the parser whose contents are also shown. Finally, code generator takes the output from the parser as input and spits out assembly. Feel free to ask explanation for any part of the code.
Although the compiler has been tested somewhat, it's still far from adequately stable and reliable and it's definetely not supposed to be used in production. For any suggestions, please contact me.