Skip to content

RohitRTdev/simc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple C compiler

To learn about the inner working of this compiler, check out this blog https://rohitrtdev.github.io/

To build this, simply execute from top level directory

If you're on GNU compatible system

cmake -B build
cd build
make

If you're on Windows

cmake -B build
open vs sln and build

By default this does a debug build. Switch this by setting the CMAKE_BUILD_TYPE variable to "Release".
All the executables(simcc, sime, code-gen, simc) will be created on the build/bin directory.

To run

./simcc <some-file-name> //To run compiler
./sime <some-file-name> //To run preprocessor
./simc <some-file-name> //This is the frontend, executes both sime and simcc. Run this

Currently, the compiler generates x64 code in att syntax and is made to be compatible only with gcc toolchain.
The compiler generates position independent code. So if you want to compile to machine code, use gcc and don't use the -fno-pic option.
The compiler is a long way from feature complete, but it is usable as of now.

Example

./simc test.c -o test.s
gcc test.s -o test
./test

First command created an assembly file named "test.s" which is then assembled using gcc to elf executable test which you can then run.
Because of certain implementation details, if you currently do not provide any output file names when invoking simc, the output file generated might have some weird name.
Use the -E option to only do preprocess step

Architecture

The design involves three executables. sime, simcc and simc.
sime is the preprocessor, simcc is the compiler(generates assembly) and simc is a frontend executable which is what the user invokes.
simc knows the installation location for the compiler and preprocessor and invokes them approriately to generate the final assembly.

Compiler design

Unlike most toy compilers, we don't use a recursive descendent parser, instead simcc uses a state-machine based parser(like a very crude and specific bison clone). This makes simcc less prone to stack overflow errors on highly nested expressions or statements. Our code generator is written as a separate module which makes it easy to add support for another architecture if needed. Currently we only support the x64 variant(no support for 32 bit) which generates position independent att syntax assembly compatible with gcc toolchain.

What the compiler lacks

We're not feature complete by a long shot. But it's easier for me to state the features that simcc(the compiler) doesn't support.
This could also be considered a TODO list:)

  1. Explicit type cast
  2. struct, union and enum(This one's huge and I probably would never add support for this)
  3. Floating point support(Adding this feature is debatable)
  4. for and do-while loop
  5. ternary expression
  6. variadic functions
  7. goto statements
  8. compound literal and array/struct type initialization(This is dependent on feature no:2, but could just be used for array initialization also)
  9. Array with no size specified(This is a consequence of lack of the previous feature, as arrays with no size requires an array initializer to be present)
  10. _Generic, _atomic, _complex specifiers

About

simc(Simple C compiler) is a hobby project created to build a working C compiler

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages