Loading...
Rustコンパイラの中間表現を可視化する
自己紹介
@tomoyamachi
Rust歴 3ヶ月
Rustで
RQL(Resource Query Language)パーサのライブラリ
実装中
昨年まではGo言語でOSSなどに貢献
DockerイメージのLintツール :
https://github.com/goodwithtech/dockle
リモートのDockerイメージのタグチェック :
https://github.com/goodwithtech/dockertags
対象者
所有権/借用のイメージがわかない人
Rust自体の設計に興味がある人
はじめたての頃
所有権のイメージがよくわからない→ とりあえず x.clone() で済ませる
CNCFチャンネルで、面白そうな動画があったのでチェックすると、Rustのコンパイラがどのように動いているかがわかった。
The Rust Borrow Checker - A Deep Dive
Rustコンパイル時に起こっているのかを分解
Introducing MIR
Token Stream
ソースコードはまずlexerによりtoken列に変換される
AST(Abstract Syntax Tree)
単純なtoken列からASTに変換
マクロの展開や、一部のsyntax checkなどが行われる
HIR(High-level IR)
syntax sugarの展開
型を明示的に示す
type checkやprivacy checkが行われる
MIR(Middle-level IR)
木構造からCFG(Control-Flow Graph)形式に変換
basic blockとその間をつなぐcontrol flow edge
borrow checkや最適化が行われる
LLVM IR:
RustはバックエンドにLLVMを用いている
※ IR : Intermediate Representation: 中間表現
余談
このあたりに興味がある人は「
Writing An Interpreter In GO
」「
Writing A Compiler In GO
」が、初学者でも理解しやすく書かれていた。Go言語で書かれているが、別言語で置き換えて実装している人がいるので、そのレポジトリを参考にすることもできる。
Rustでの実装例 :
GitHub - shuhei/cymbal: Yet another Rust implementation of the Monkey language from "Writing an Interpreter in Go" and "Writing a Compiler in Go"
Yet another Rust implementation of the Monkey language from "Writing an Interpreter in Go" and "Writing a Compiler in Go" - GitHub - shuhei/cymbal: Yet another Rust implementati...
github.com
Rustの各状態を見てみたい
Please turn on JavaScript to use Paper in all of its awesomeness. ^_^
自己紹介
対象者
はじめたての頃
Rustコンパイル時に起こっているのかを分解
Rustの各状態を見てみたい