lynn / chibicc

A small C compiler… for uxn
MIT License
115 stars 5 forks source link
uxn

chibicc-uxn

This is Rui Ueyama's chibicc@historical/old, retargeted to compile C to Uxntal.

Uxntal is the low-level programming language for Uxn, a virtual machine for writing tools and games that run on almost any hardware. See awesome-uxn.

image

chibicc-uxn is featureful enough to write small games and demos. We have even managed to port a classic desktop pet application from X11!

Usage

Running make will build ./chibicc.

chibicc itself has no preprocessor, but any C compiler's -E flag will do. We use -I. to include varvara.h or just uxn.h and -P to eliminate # lines from the preprocessor output:

gcc -I. -P -E examples/day3.c -o tmp.c
./chibicc -O1 tmp.c > tmp.tal
uxnasm tmp.tal tmp.rom
uxnemu tmp.rom

The -O1 or -O flag enables the optimization pass. If the flag is omitted, this is equivalent to -O0 (no optimization).

There's a convenient script that just runs the above commands: ./run.sh examples/day3.c (compile + uxnasm + uxnemu).

For a more complex and visually interesting demo, try ./run.sh examples/star.c.

See also make test, which runs a test suite.

Supported

Not supported

Varvara

Varvara is the set of I/O interfaces for Uxn-based tools and games.

varvara.h defines macros for setting the window size, drawing sprites on the screen, playing sounds, etc.

To set up Varvara event handlers, just define any of the following functions:

If they are defined, the compiled startup code will hook them up to the right devices before calling your main.

void main(int argc, char *argv[]) is supported, but this will add a huge support routine to your code, so in some cases a custom on_console routine is more efficient. Demo: ./run.sh --cli examples/argc_argv.c foo bar 'foo bar'.

Note that the program doesn't terminate when it reaches the end of main; as such, the return value is ignored. Use exit() if you want to exit and set the status code.

Interfacing with assembly

The variadic intrinsic asm() function accepts a number of int arguments which are pushed in order, followed by some Uxntal code that should leave one int on the stack.

int sum_of_squares(int x, int y) {
  return asm(x, y, "DUP2 MUL2 SWP2 DUP2 MUL2 ADD2");
}

This is useful if you just want a little Uxntal idiom in the middle of your C.

However, in this case sum_of_squares still contains a lot of unnecessary code (creating a stack frame, and copying x and y to and from it). We can avoid this by simply writing an Uxntal function in a .tal file and linking it with our compiler output:

// code.c
extern int sum_of_squares(int x, int y);
( sum_of_squares.tal )
( Note the underscore at the end of the function name. TODO?: don't mangle extern function calls. )
@sum_of_squares_ ( y* x* -> result* )
  DUP2 MUL2 SWP2 DUP2 MUL2 ADD2
  JMP2r
# build.sh
chibicc code.c > tmp.tal
cat sum_of_squares.tal >> tmp.tal
uxnasm tmp.tal tmp.rom

See examples/mandelbrot_fast.c.

Calling convention

To write uxntal that's compatible with chibicc, you need to know how chibicc calls functions.

Arguments are pushed to the stack in reverse order: a C function like int foo(int x, int y, int z); corresponds to an uxntal signature like ( z* y* x* -- result* ).

Arguments are always passed on the working stack as shorts (16-bit). The result is always a short, even if the C type is void or char. Your uxntal implementation of a void-returning function should leave a 16-bit dummy value on the stack before returning.

Some examples:

C signature uxntal signature
int f(int a, int b); ( b* a* -- result* )
char g(int a, char b); ( b* a* -- result* )
void h(void); ( -- result* )