Koopa

github crates.io docs.rs build status

Library for generating/parsing/optimizing Koopa IR.

Koopa IR is the next generation of education-oriented intermediate representation designed for compiler courses at Peking University.

Usage

toml [dependencies] koopa = "0.0.3"

Koopa IR

Here is a "Hello, world!" program in Koopa IR:

``koopa //putchar` function in libc. decl @putchar(i32): i32

// A helper function for printing strings (integer arrays). fun @putstr(@arr: *i32) { %entry: jump %loop_entry(@arr)

%loopentry(%ptr: *i32): %cur = load %ptr br %cur, %loopbody, %end

%loopbody: call @putchar(%cur) %next = getptr %ptr, 1 jump %loopentry(%next)

%end: ret }

// String "Hello, world!\n\0". global @str = alloc [i32, 15], { 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 10, 0 }

// main function, the entry point of the program. fun @main(): i32 { %entry: %str = getelemptr @str, 0 call @putstr(%str) ret 0 } ```

Koopa IR is a strongly-typed, SSA form based intermediate representation. It's simple but powerful enough to support compilation of code into machine instructions, or some advanced optimizations of it.

For more details, see the document of Koopa IR (Chinese).

Examples

See the examples directory, which contains three examples:

References

Koopa IR library is heavily influenced by LLVM and Cranelift.

Changelog

See CHANGELOG.md.

License

Copyright (C) 2010-2021 MaxXing. License GPLv3.