Giter Club home page Giter Club logo

koopa's Introduction

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

cargo add koopa

Koopa IR

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).

Examples

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).

References

Koopa IR library is heavily influenced by LLVM and Cranelift.

Changelog

See CHANGELOG.md.

License

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

koopa's People

Contributors

epitaphial avatar maxxsoft avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.