Qiang: a C11 compiler front end that lowers to QBE IR (formerly qpcc)
sh tools/bootstrap.sh # clone ../qbe.mbt if it is not there
moon build --target native # build qiang/qiang.exe
sh test.sh # clang oracle over tests/*.csource.c --(clang -E)--> tokens --(front)--> AST
--(sema)--> CheckedProgram --(codegen)--> ir_builder --> Mach-O / asm| Package | Role |
|---|---|
| front | lexer, surface AST, full C11 + GNU-extension parser |
| sema | name resolution, types, layout, constant evaluation, diagnostics |
| codegen | lowers the checked AST onto ir_builder |
| qiang | driver CLI (the executable is named after this package) |
moon build --target native qiang
# one step: preprocess, compile, link and run
qiang run hello.c [-- args...]
# or drive the pieces yourself
qiang input.c -o input.o [--emit obj|asm|qbe] [-std=c11|c23|c2y] [--check]
clang input.o -o a.out| Header | Macro |
|---|---|
| stdcountof.h | countof -> _Countof (N3469) |
| stddefer.h | defer -> _Defer (TS 25755) |
| stdmaxof.h | maxof -> _Maxof (Qiang extension) |
| stdminof.h | minof -> _Minof (Qiang extension) |
| taggedunion.h | tagunion -> _Tagged_union, static_tag -> _Static_tag, dynamic_tag -> _Dynamic_tag (Qiang extension) |
| function_pointer.h | function_pointer -> _Function_pointer (N3914) |
| lambda.h | lambda -> _Lambda, closure_environment -> _Closure_environment (Qiang extension) |
#define MkResult(T, E) typedef tagunion { T ok; E err; } Result_([T, E]);
MkResult(int, int)
Result_([int, int]) r = { .ok = 1 };_Tagged_union Value { int as_int; float as_float; };
_Tagged_union Value x = { .as_int = 100 };
x = (_Tagged_union Value){ .as_float = 1.5f }; // sets the tag
switch (_Dynamic_tag(x)) {
case _Static_tag(_Tagged_union Value, as_int): break;
case _Static_tag(_Tagged_union Value, as_float): break;
}_Function_pointer int (int, char *) a; /* int (*a)(int, char *) */
_Function_pointer b = a; /* stores any function pointer */
int (*c)(int, char *) = b; /* ... and converts back */int a = 100, b = 20;
auto f = _Lambda(a, &b) int (int x, int y) { return a + (*b) + x + y; };
int *pb = _Closure_environment(f)->b;sh examples/selfhost/build-qbe-with-Qiang.sh
# ... test corpus: 76 identical, 0 differing, 76 totalInstall
Download zipQiang: a C11 compiler front end that lowers to QBE IR (formerly qpcc)