Skip to main content

ShivyC Build Status Code Coverage

A hobby C compiler created in Python.

ShivyC demo GIF.


ShivyC is a hobby C compiler written in Python 3 that supports a subset of the C11 standard and generates reasonably efficient binaries, including some optimizations. ShivyC also generates helpful compile-time error messages.

This implementation of a trie is an example of what ShivyC can compile today. For a more comprehensive list of features, see the feature test directory.

Quickstart

x86-64 Linux

ShivyC requires only Python 3.6 or later to compile C code. Assembling and linking are done using the GNU binutils and glibc, which you almost certainly already have installed.

To install ShivyC:

pip3 install shivyc

To create, compile, and run an example program:

$ vim hello.c
$ cat hello.c

#include <stdio.h>
int main() {
  printf("hello, world!\n");
}

$ shivyc hello.c
$ ./out
hello, world!

To run the tests:

git clone https://github.com/ShivamSarodia/ShivyC.git
cd ShivyC
python3 -m unittest discover

Other Architectures

For the convenience of those not running Linux, the docker/ directory provides a Dockerfile that sets up an x86-64 Linux Ubuntu environment with everything necessary for ShivyC. To use this, run:

git clone https://github.com/ShivamSarodia/ShivyC.git
cd ShivyC
docker build -t shivyc docker/
docker/shell

This will open up a shell in an environment with ShivyC installed and ready to use with

shivyc any_c_file.c           # to compile a file
python3 -m unittest discover  # to run tests

The Docker ShivyC executable will update live with any changes made in your local ShivyC directory.

Implementation Overview

Preprocessor

ShivyC today has a very limited preprocessor that parses out comments and expands #include directives. These features are implemented between lexer.py and preproc.py.

Lexer

The ShivyC lexer is implemented primarily in lexer.py. Additionally, tokens.py contains definitions of the token classes used in the lexer and token_kinds.py contains instances of recognized keyword and symbol tokens.

Parser

The ShivyC parser uses recursive descent techniques for all parsing. It is implented in parser/*.py and creates a parse tree of nodes defined in tree/nodes.py and tree/expr_nodes.py.

IL generation

ShivyC traverses the parse tree to generate a flat custom IL (intermediate language). The commands for this IL are in il_cmds/*.py . Objects used for IL generation are in il_gen.py , but most of the IL generating code is in the make_code function of each tree node in tree/*.py.

ASM generation

ShivyC sequentially reads the IL commands, converting each into Intel-format x86-64 assembly code. ShivyC performs register allocation using George and Appel’s iterated register coalescing algorithm (see References below). The general ASM generation functionality is in asm_gen.py , but much of the ASM generating code is in the make_asm function of each IL command in il_cmds/*.py.

Contributing

Pull requests to ShivyC are very welcome. A good place to start is the Issues page. All issues labeled "feature" are TODO tasks. Issues labeled "bug" are individual miscompilations in ShivyC. If you have any questions, please feel free to ask in the comments of the relevant issue or create a new issue labeled "question". Of course, please add test(s) for all new functionality.

Many thanks to our current and past contributers:

References

Metadata

Release files for shivyc 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shivyc 0.5.0
File Size Uploaded
shivyc-0.5.0.tar.gz 68.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for shivyc 0.5.0
File Interpreter ABI Platform
shivyc-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 149.6 kB

Release files / shivyc-0.5.0.tar.gz

Download URL shivyc-0.5.0.tar.gz
Size 68.5 kB
Tags Source
SHA-256 checksum
How to use checksums
cb43ed9f4470228c60f2f41e2cdc7f63d1dffa8df0ed2c1e15faaac113e8f83a
BLAKE2b-256 checksum
How to use checksums
d2c342e8d1cd0bb51b6fe376d3af7ac815902cc4f50546d744bc9e109490ca93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / shivyc-0.5.0-py3-none-any.whl

Download URL shivyc-0.5.0-py3-none-any.whl
Size 81.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f874a0c9dc8a137c3ff208f49065ba2b8167e3c91a1face86f50f525930a2b4
BLAKE2b-256 checksum
How to use checksums
c3c16e42aff25bdb783a877e7d4fb417e28d4d0d19cb81d5a1ed8d354f4fb988
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.5

2 release files

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.2.0

2 release files

0.1.2

3 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page