Crates.io | koopa |
lib.rs | koopa |
version | |
source | src |
created_at | 2021-09-14 12:42:00.731682 |
updated_at | 2024-11-30 07:09:47.610752 |
description | Library for generating/parsing/optimizing Koopa IR. |
homepage | |
repository | https://github.com/pku-minic/koopa |
max_upload_size | |
id | 451195 |
Cargo.toml error: | TOML parse error at line 22, column 1 | 22 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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.
cargo add koopa
Here is a "Hello, world!" program in Koopa IR:
// `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)
%loop_entry(%ptr: *i32):
%cur = load %ptr
br %cur, %loop_body, %end
%loop_body:
call @putchar(%cur)
%next = getptr %ptr, 1
jump %loop_entry(%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).
See the examples
directory, which contains three examples:
opt
: a simple Koopa IR optimizer.brainfuck
: a brainfuck to Koopa IR compiler.interpreter
: a simple Koopa IR interpreter.And there are some more complex examples:
kira-rs
: The Kira compiler (Rust version), which compiles SysY language into Koopa IR and RISC-V assembly.kira-cpp
: The Kira compiler (C++ version).Koopa IR library is heavily influenced by LLVM and Cranelift.
See CHANGELOG.md.
Copyright (C) 2010-2023 MaxXing. License GPLv3.