LoginSignup
2
2

More than 3 years have passed since last update.

RustでLLVMするためのcrate、Inkwellを触ってみた

Posted at

RustでLLVMを使った言語処理系を作りたかったので、とりあえずInkwellを動かすところまでやりました。

Inkwellはllvm-sys.rsのラッパーで、"淡々とunsafeな関数をラップしてsafeな関数にしていくという面倒な作業がなくなるというのは大変ありがたいです"とのことです。良さげですね。

Cargo.toml

以下を追加します。LLVMの最新版は3月にリリースされたLLVM 8ですが、まだ対応ブランチがないので、LLVM 7を使うことにします。

Cargo.toml
[dependencies]
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "llvm7-0" }

LLVM 7を入れる

これだけだと、cargo buildしてもLLVMがないというエラーになりました。ということで brew install llvm@7 でLLVM 7.0.1を入れます。 (llvmenvも試したけどコンパイルできなかった)

インストール後、inkwell(というかllvm-sysかな)が見つけられるようパスを通してやる必要があります。

export PATH="/usr/local/opt/llvm@7/bin:$PATH"

サンプル

これで、READMEにあるサンプルがcargo runできるようになりました。

$ cargo run
   Compiling shiika v0.0.1 (/Users/yhara/Dropbox/proj/shiika)
warning: unused imports: `InitializationConfig`, `Target`
  --> src/main.rs:95:24
   |
95 | use inkwell::targets::{InitializationConfig, Target};
   |                        ^^^^^^^^^^^^^^^^^^^^  ^^^^^^
   |
   = note: #[warn(unused_imports)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 1.85s
     Running `target/debug/shiika`
1 + 2 + 3 = 6

このサンプルではメモリ上でLLVM IRを作ってそれを実行するところまでやっていますが、やりたいのは実行ファイルの生成なので、そのやり方もまた調べてみたいと思います。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2