Skip to content

Commit

Permalink
Update README.md to reflect this fork
Browse files Browse the repository at this point in the history
  • Loading branch information
tommythorn committed Sep 22, 2023
1 parent 8ffb55c commit 60f6c91
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
56 changes: 52 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,62 @@
c4 - C in four functions
========================
c4 - A self-contained C Compiler and VM that can run itself
===========================================================

An exercise in minimalism.
C4 is Robert Swierczek's tour-de-force exercise in minimalism, a four
function C-subset compiler that can run itself. This fork aims to
evolving it a bit, forgoing the "four function" constraint, but
keeping the self-compiling and self-contained property.

Wishlist
--------

- Merge the switch + struct work into c5 (the c4 branch that adds an
intermediate AST)

- Enable a standalone VM (currently host addresses are embedded into
the instruction stream). Make the VM byte-code based, increasing
the code density substantially.

- Migrate to a more efficient execution model. The current stack
machine is rather inefficient; for example `x += y` (which both are
local variables) are translated into

```
LEA -6; PSH; LEA -6; LI; PSH; LEA -3; LI; ADD; SI
```
where we really just need something like
```
ADD -6, -3`
```
(Note, this looks a lot like a register machine, but the "registers"
here are stack allocated variables).

- Switch statements should be table-lookup based.

- Expand the language in the direction of C99, notably adding locals
with initialization.


Tricks
------

Robert deploys a range of marvelous tricks to keep this simple and
small. An incomplete list includes:

- Tokens are in operator precedence order which simplifies the
precedence-climbing recursive descent parser.

- Lvalue addresses are handled by undoing the last load (this only
applies to c4 as with the AST something different is done).

Usage
=====

Try the following:

gcc -o c4 c4.c
./c4 hello.c
./c4 -s hello.c

./c4 c4.c hello.c
./c4 c4.c c4.c hello.c

3 changes: 2 additions & 1 deletion c4.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// if, while, return, and expression statements
// just enough features to allow self-compilation and a bit more

// Written by Robert Swierczek
// Written by Robert Swierczek, 2014-2019
// Tweaked by Tommy Thorn, 2023

#include <stdio.h>
#include <stdlib.h>
Expand Down

0 comments on commit 60f6c91

Please sign in to comment.