Slide 1

Slide 1 text

Ruby in Ruby Building an AOT Compiler with Ruby katsyoshi Repro Inc.

Slide 2

Slide 2 text

self

Slide 3

Slide 3 text

self 開発者募集中!!! Rails や Kafka をやりたい/できるひと 上記以外にも Go での配信基盤開発や Java/Objective-C などでAndroid/iOS SDK開発も

Slide 4

Slide 4 text

self Twitter, GitHub, bluesky…: katsyoshi Hobbies: keyboard, fishing, watching football… Work: Rails Application Programmer

Slide 5

Slide 5 text

My Gems fluent-plugins fluent-plugin-serialport… itamae-plugins itamae-plugin-resource-pip… irb-theme-dracula vaporware-compiler considering rename…

Slide 6

Slide 6 text

Dragon Book Club: Member フロントエンドエンジニアの集まり コンパイラーとしての話です そろそろ読みたいと思い20年が経ち… @spikeolaf「#lr-parser にいる人は全員参 加するのはあたりまえだよなあ??? しゃーない行くかー よみたかったしな!

Slide 7

Slide 7 text

Introduction AOT コンパイラーつくるぞ Matz も言ってたしそろそろ欲しいよね とりあえずうごくもの作ろう

Slide 8

Slide 8 text

Self Motivation コンパイラーつくったことないし一度作って みるのもいいよね 直接バイナリー作たい!! アセンブラー書いて機械語書いてみたい! 幸い資料も実装もたくさんあるしな!

Slide 9

Slide 9 text

Simmilar Implementations LLVM Backends: RubyMotion うーん… Rust Backends: monoruby もうやってるひといるしな Go Backends: goruby もうやってるひといるしな Ruby でやるぞ!!!

Slide 10

Slide 10 text

C Language Compile Flow C言語でコンパイルすると上図のようになる これらの流れを Ruby でもできるようにするぞ! 全部一旦実装する!

Slide 11

Slide 11 text

Create Components Compiler Assembler Linker

Slide 12

Slide 12 text

Compiler 整数演算と簡単な制御構文が使えるようにす る 四則演算 変数 条件分岐 if, while

Slide 13

Slide 13 text

Compiler Ruby が AST(Abstruct Syntax Tree) を持っ ている Ruby からアクセス可能

Slide 14

Slide 14 text

Compiler RubyVM::AbstructSyntaxTree.parse 木構造なので辿るだけで大丈夫 x86_64 Assembly 言語へ変換

Slide 15

Slide 15 text

Assembler 中間言語表現からオブジェクトファイルを生 成 今回は x86_64 Assembly を選択 決まった中間言語表現から Binary Format へ変換するだけ ということで Format を決めましょう

Slide 16

Slide 16 text

Binary Format Linux ELF

Slide 17

Slide 17 text

ELF Executable and Linking Format Linux などの Unix 系 OS で動作するバイナ リフォーマット

Slide 18

Slide 18 text

Structure of ELF ELF Header Sections Section Headers

Slide 19

Slide 19 text

Structure of ELF

Slide 20

Slide 20 text

ELF Header ELF オブジェクトファイルのヘッダー ファイルサイズなどが書かれている 0x40 bytes

Slide 21

Slide 21 text

ELF Header

Slide 22

Slide 22 text

Sections Text Section: 本体 Data Section, BSS Section, Note Section 今回はサポートしないHeaderと必要最低限の実装 Symtab Section Strtab Section Shstrtab Section

Slide 23

Slide 23 text

Sections: Text Section プログラムの本体

Slide 24

Slide 24 text

Section Headers 各 Section の開始点など情報を持つ部分

Slide 25

Slide 25 text

Structure of ELF 完全理解した あとは実装するだけ!

Slide 26

Slide 26 text

Assembler Assembly 言語から機械語へ変換する 今回はx86_64のELFへ変換

Slide 27

Slide 27 text

Learn x86_64 Machine Language 次に機械語を覗いてみよう 基本的に Assembly に書いてある命令をその まま変換すればいい

Slide 28

Slide 28 text

Learn x86_64 Machine Language

Slide 29

Slide 29 text

Learn x86_64 Machine Language やりたいこと: 64bit で加算したい 加算命令: ADD REX.W 01 と書いてあるやつを選べばよさそう

Slide 30

Slide 30 text

Linker オブジェクトファイルと実行に必要なライブ ラリを繋げてくれて実行ファイルを作ってく れるやつ 今回はここはできあいの mold, gold, lld で

Slide 31

Slide 31 text

Current ((1 + 2) * 3) / (5-4) 上記のコードをx86_64 Assembly 言語にコ ンパイル コンパイルされた Assembly 言語から ELF のオブジェクトファイルへアセンブル ELF のオブジェクトファイルと必要なライブ ラリをリンクし実行ファイルを生成

Slide 32

Slide 32 text

Current まだまだ実装たりない 簡単なコードを AST からネイティブに変換 しただけ Linker もない

Slide 33

Slide 33 text

Futures Linker を作る 構文サポート 制御構文、クラス、メソッド…

Slide 34

Slide 34 text

Conclusion Compiler、Assembler を作ってみた 現状としてはまだまだ ベンチマークとれるレベルまで持っていく