Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby in Ruby - Building an AOT Compiler with Ruby
Search
MATSUMOTO Katsuyoshi
September 25, 2024
Programming
0
81
Ruby in Ruby - Building an AOT Compiler with Ruby
MATSUMOTO Katsuyoshi
September 25, 2024
Tweet
Share
More Decks by MATSUMOTO Katsuyoshi
See All by MATSUMOTO Katsuyoshi
(私的)RubyKaigi の歩き方
katsyoshi
0
1k
Building Ruby Native Extension using Ruby
katsyoshi
0
1.8k
ちいさなクラウド業者やってみた
katsyoshi
0
170
fluentdとxlsx
katsyoshi
0
120
mikutterとJubatusとfluentd
katsyoshi
0
11k
ZFSでNASやってはまったこと
katsyoshi
0
2k
Asakusa.rbに一年間通ったらこうなった
katsyoshi
1
2.6k
mikutterとJubatusで遊んでみた
katsyoshi
0
580
BeagleBoard-xM向けに td-agent-armhf.debをつ くってみた
katsyoshi
0
800
Other Decks in Programming
See All in Programming
Делим тесты между QA и разработчиком
mariyasaygina
0
470
AWS Lambda Web Adapterを活用する新しいサーバーレスの実装パターン
tmokmss
6
5.5k
◯◯エンジニアになった理由
gessy0129
PRO
0
600
M5Stack に色々な M5ユニットをつないで扱う為の新たなアプローチ
gob
0
200
ECS向けのドリフト検知機構を実装してみた
tkikuc
0
270
Pydantic x Database API:turu-pyの開発
yassun7010
1
470
モジュラモノリス、その前に / Modular monolith, before that
euglena1215
5
580
Re:PandasAI:生成AIがデータ分析業務にもたらすパラダイムシフト【増補改訂版】
negi111111
1
790
Applied NLP in the Age of Generative AI
inesmontani
PRO
3
1.1k
フロントエンドの標準仕様をどう追っているか / How I follow the frontend standards specs
petamoriken
2
170
"Swarming" をコンセプトに掲げるアジャイルチームのベストプラクティス
boykush
2
170
XP2024 っていう国際会議に行ってきたよの記 / XP2024 Conference Report
bonotake
4
160
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
8.8k
How to Think Like a Performance Engineer
csswizardry
16
1k
Designing the Hi-DPI Web
ddemaree
279
34k
From Idea to $5000 a Month in 5 Months
shpigford
380
46k
How to Ace a Technical Interview
jacobian
275
23k
Six Lessons from altMBA
skipperchong
26
3.4k
The Cost Of JavaScript in 2023
addyosmani
43
5.8k
Embracing the Ebb and Flow
colly
83
4.4k
The Art of Programming - Codeland 2020
erikaheidi
50
13k
Art, The Web, and Tiny UX
lynnandtonic
295
20k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
158
15k
WebSockets: Embracing the real-time Web
robhawkes
59
7.3k
Transcript
Ruby in Ruby Building an AOT Compiler with Ruby katsyoshi
Repro Inc.
self
self 開発者募集中!!! Rails や Kafka をやりたい/できるひと 上記以外にも Go での配信基盤開発や Java/Objective-C
などでAndroid/iOS SDK開発も
self Twitter, GitHub, bluesky…: katsyoshi Hobbies: keyboard, fishing, watching football…
Work: Rails Application Programmer
My Gems fluent-plugins fluent-plugin-serialport… itamae-plugins itamae-plugin-resource-pip… irb-theme-dracula vaporware-compiler considering rename…
Dragon Book Club: Member フロントエンドエンジニアの集まり コンパイラーとしての話です そろそろ読みたいと思い20年が経ち… @spikeolaf「#lr-parser にいる人は全員参 加するのはあたりまえだよなあ???
しゃーない行くかー よみたかったしな!
Introduction AOT コンパイラーつくるぞ Matz も言ってたしそろそろ欲しいよね とりあえずうごくもの作ろう
Self Motivation コンパイラーつくったことないし一度作って みるのもいいよね 直接バイナリー作たい!! アセンブラー書いて機械語書いてみたい! 幸い資料も実装もたくさんあるしな!
Simmilar Implementations LLVM Backends: RubyMotion うーん… Rust Backends: monoruby もうやってるひといるしな
Go Backends: goruby もうやってるひといるしな Ruby でやるぞ!!!
C Language Compile Flow C言語でコンパイルすると上図のようになる これらの流れを Ruby でもできるようにするぞ! 全部一旦実装する!
Create Components Compiler Assembler Linker
Compiler 整数演算と簡単な制御構文が使えるようにす る 四則演算 変数 条件分岐 if, while
Compiler Ruby が AST(Abstruct Syntax Tree) を持っ ている Ruby からアクセス可能
Compiler RubyVM::AbstructSyntaxTree.parse 木構造なので辿るだけで大丈夫 x86_64 Assembly 言語へ変換
Assembler 中間言語表現からオブジェクトファイルを生 成 今回は x86_64 Assembly を選択 決まった中間言語表現から Binary Format
へ変換するだけ ということで Format を決めましょう
Binary Format Linux ELF
ELF Executable and Linking Format Linux などの Unix 系 OS
で動作するバイナ リフォーマット
Structure of ELF ELF Header Sections Section Headers
Structure of ELF
ELF Header ELF オブジェクトファイルのヘッダー ファイルサイズなどが書かれている 0x40 bytes
ELF Header
Sections Text Section: 本体 Data Section, BSS Section, Note Section
今回はサポートしないHeaderと必要最低限の実装 Symtab Section Strtab Section Shstrtab Section
Sections: Text Section プログラムの本体
Section Headers 各 Section の開始点など情報を持つ部分
Structure of ELF 完全理解した あとは実装するだけ!
Assembler Assembly 言語から機械語へ変換する 今回はx86_64のELFへ変換
Learn x86_64 Machine Language 次に機械語を覗いてみよう 基本的に Assembly に書いてある命令をその まま変換すればいい
Learn x86_64 Machine Language
Learn x86_64 Machine Language やりたいこと: 64bit で加算したい 加算命令: ADD REX.W
01 と書いてあるやつを選べばよさそう
Linker オブジェクトファイルと実行に必要なライブ ラリを繋げてくれて実行ファイルを作ってく れるやつ 今回はここはできあいの mold, gold, lld で
Current ((1 + 2) * 3) / (5-4) 上記のコードをx86_64 Assembly
言語にコ ンパイル コンパイルされた Assembly 言語から ELF のオブジェクトファイルへアセンブル ELF のオブジェクトファイルと必要なライブ ラリをリンクし実行ファイルを生成
Current まだまだ実装たりない 簡単なコードを AST からネイティブに変換 しただけ Linker もない
Futures Linker を作る 構文サポート 制御構文、クラス、メソッド…
Conclusion Compiler、Assembler を作ってみた 現状としてはまだまだ ベンチマークとれるレベルまで持っていく