Dependencies
| Package | Purpose |
|---|---|
| bobzhang/kimicc/target | Names supported compiler output targets such as darwin-arm64 and linux-amd64. |
| bobzhang/kimicc/preprocessor | Expands C preprocessing directives into ordinary C source. |
| bobzhang/kimicc/parser | Tokenizes and parses preprocessed C source into the public AST. |
| bobzhang/kimicc/codegen | Converts the parser AST into target assembly, Darwin ARM64 Mach-O object bytes, or a JIT image. |
| bobzhang/kimicc/jit | Native-only convenience API that compiles C source and calls int returning functions in memory. |
moon build --target native
moon test --target nativemoon run cmd/main --target native -- input.c -o out
moon run cmd/main --target native -- main.c helper.c support.o -o out
./outmoon run cmd/main --target native -- -E -D FEATURE=1 -I include input.c
moon run cmd/main --target native -- -E '-DADD(x,y)=((x)+(y))' input.c
moon run cmd/main --target native -- -S input.c
moon run cmd/main --target native -- -c input.c
moon run cmd/main --target native -- -fsyntax-only input.c
moon run cmd/main --target native -- -MM -MP input.c
moon run cmd/main --target native -- -c -MMD -MP -MF input.d input.c
moon run cmd/main --target native -- -S --preprocessed input.i -o out.s
moon run cmd/main --target native -- @args.rsp
moon run cmd/main --target native -- -dumpmachine
moon run cmd/main --target native -- -print-target-triple
moon run cmd/main --target native -- -print-resource-dir
moon run cmd/main --target native -- -target linux-amd64 -print-multiarch
moon run cmd/main --target native -- -target linux-amd64 -print-multi-os-directory
moon run cmd/main --target native -- -target linux-amd64 -print-multi-lib
moon run cmd/main --target native -- -print-libgcc-file-namemoon run cmd/main --target native -- -S -target linux-amd64 input.c -o out.s
moon run cmd/main --target native -- -c -target linux-amd64 input.c -o out.odocker run --rm --platform linux/amd64 \
-v "$PWD:/work" -w /work ubuntu:24.04 \
bash scripts/linux-amd64-smoke.shscripts/build-linux-amd64-smoke-image.sh
scripts/linux-amd64-smoke.shscripts/fetch-external-parser-fixtures.sh
moon test test/e2e/e2e_test.mbt --target native \
--filter 'parse tinycc stripped'
moon test test/e2e/e2e_test.mbt --target native \
--filter 'parse quickjs preprocessed'import {
"bobzhang/kimicc/preprocessor"
}@preprocessor.preprocess(
source : String,
options : @preprocessor.PreprocessOptions,
) -> Result[String, @preprocessor.PreprocessError]import {
"bobzhang/kimicc/parser"
}@parser.parse(source : String) -> @parser.Program| Type | Meaning |
|---|---|
| Program | Top-level translation unit: struct or union declarations, global variables, and function declarations. |
| FuncDecl | Function declaration or definition. body is None for declarations without a body. |
| GlobalDecl | Global variable declaration or definition. init is None for declarations without an initializer. |
| StructDecl | Struct or union declaration. is_union distinguishes unions; is_packed records GNU packed layout attributes. |
| Param | Function parameter or aggregate field. bit_width is set for bit-fields. |
| Type | C type model used by the parser and code generator. Aligned records _Alignas and numeric GNU aligned attributes. |
| Expr | Expression tree. Operators are stored as source-level operator strings. |
| Stmt | Statement tree. |
| GlobalInit | Global initializer form. |
| Token, Lexer, Parser | Lower-level lexer/parser building blocks. Prefer parse unless you need token-level behavior. |
///|
let source = "int answer(void) { return 42; }"
///|
let program = @parser.parse(source)ty.size() -> Int
ty.align() -> Int
ty.is_integer() -> Bool
ty.is_signed() -> Bool
ty.is_floating() -> Bool
ty.to_unsigned() -> @parser.Type@parser.fold_const(@parser.Expr::Number(42L))import {
"bobzhang/kimicc/parser",
"bobzhang/kimicc/codegen",
"bobzhang/kimicc/target",
}///|
let program = @parser.parse(source)
///|
let assembly = @codegen.Codegen::new().generate(program)///|
let target = @target.Target::parse("linux-amd64").unwrap()
///|
let docker_style_target = @target.Target::parse("linux/amd64").unwrap()
///|
let assembly = @codegen.generate_assembly_for_target(program, target)///|
let object_bytes = @codegen.generate_macho_object(program)///|
let image = @codegen.generate_jit_image(program)| Field | Meaning |
|---|---|
| code | Full in-memory image bytes. The executable region comes first, followed by writable data. |
| executable_size | Number of leading bytes that should be made executable. Bytes after this offset remain writable data. |
| base_relocations | Little-endian u32 offsets of 64-bit slots that must be adjusted by the image base address. |
| external_relocations | Encoded external-symbol relocations resolved by the native JIT loader with dlsym. |
| symbols | Exported symbols and offsets into code. |
import {
"bobzhang/kimicc/jit"
}let source = "int add(int x, int y) { return x + y; }"
match @jit.compile(source) {
Some(module_) =>
match module_.call_i32_2("add", 20, 22) {
Some(value) => println(value.to_string())
None => println("missing symbol or unsupported call")
}
None => println("compile or load failed")
}@jit.call_i32_0(source, "answer")
@jit.call_i32_1(source, "negate", 42)
@jit.call_i32_2(source, "add", 20, 22)
@jit.call_i32_3(source, "mix", 5, 8, 2)Dependencies