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
Shinjuku.rs#15 Rustでつくるx86アセンブラ
Search
monochrome
April 27, 2021
Programming
0
1.7k
Shinjuku.rs#15 Rustでつくるx86アセンブラ
Making JIT assembler for x86-64 written in Rust.
monochrome
April 27, 2021
Tweet
Share
More Decks by monochrome
See All by monochrome
Improving my own Ruby thereafter
sisshiki1969
1
210
あなたとJIT, 今すぐアセンブ ル
sisshiki1969
1
960
Improve my own Ruby
sisshiki1969
1
460
My own Ruby, thereafter
sisshiki1969
0
380
Running Optcarrot (faster) on my own Ruby.
sisshiki1969
1
280
仮想マシンにおけるスタックの管理
sisshiki1969
0
230
Rustでゴミ集め
sisshiki1969
1
360
RustでつくるRubyのFiber
sisshiki1969
0
310
fukuoka.rb#202 RustでつくるRuby
sisshiki1969
1
860
Other Decks in Programming
See All in Programming
米国のサイバーセキュリティタイムラインと見る Goの暗号パッケージの進化
tomtwinkle
2
460
受け入れテスト駆動開発(ATDD)×AI駆動開発 AI時代のATDDの取り組み方を考える
kztakasaki
2
540
社内規程RAGの精度を73.3% → 100%に改善した話
oharu121
13
7.7k
TipKitTips
ktcryomm
0
160
Rubyと楽しいをつくる / Creating joy with Ruby
chobishiba
0
210
nilとは何か 〜interfaceの構造とnil!=nilから理解する〜
kuro_kurorrr
3
1.7k
Rで始めるML・LLM活用入門
wakamatsu_takumu
0
170
AI駆動開発の本音 〜Claude Code並列開発で見えたエンジニアの新しい役割〜
hisuzuya
4
490
クライアントワークでSREをするということ。あるいは事業会社におけるSREと同じこと・違うこと
nnaka2992
1
310
「やめとこ」がなくなった — 1月にZennを始めて22本書いた AI共創開発のリアル
atani14
0
360
2026年は Rust 置き換えが流行る! / 20260220-niigata-5min-tech
girigiribauer
0
220
日本だけで解禁されているアプリ起動の方法
ryunakayama
0
370
Featured
See All Featured
Marketing to machines
jonoalderson
1
5k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
110
Art, The Web, and Tiny UX
lynnandtonic
304
21k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
1
150
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
220
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
2.4k
Prompt Engineering for Job Search
mfonobong
0
180
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.1k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.4k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Transcript
Rustでつくるx86アセンブラ monochrome twitter: @s_isshiki1969 Slack: プログラミング言語処理系が好きな人の集まり https://prog-lang-sys-ja-slack.github.io/wiki/ monoasm: https://github.com/sisshiki1969/monoasm
アセンブラ is 何 int main() { return 42; } main:
push rbp mov rbp, rsp mov eax, 42 pop rbp ret 55 48 89 e5 b8 2a 00 00 00 5d c3 0f 1f 00 hoge.c hoge.s hoge.o これ アセンブリのテキストファイルを機械語へ変換
インラインアセンブラ in Rust let i: u64 = 3; let o:
u64; unsafe { asm!( "mov {0}, {1}", "add {0}, {number}", out(reg) o, in(reg) i, number = const 5, ); } 1. asmマクロのある位置に機械語を挿入 2. 変数や値を埋め込むことができる
ダイナミックアセンブラ • 「実行時に機械語を吐くコード」を生成するライブラリ • メモリ上に機械語を格納するバッファを確保し、実行中にそこへ機械語を 生成していく • 応用例としてはJITコンパイラなど • 例:Xbyak(C++、テンプレート)
DynASM(C、プリプロセッサ) monoasm(Rust、手続きマクロ)
手続きマクロ(proc macro) マクロの中身をRustコードへ変換するRustプログラム monoasm!(jit, pushq rbp;) jit.enc_o(80, Reg::from(5));
fn hello_world() { let func = hello(); func(()); } fn
hello() -> fn(()) -> () { let hello = "hello world!\n"; let mut jit = JitMemory::new(); let label = jit.label(); monoasm!(jit, // prologue pushq rbp; movq rbp, rsp; pushq r15; pushq r14; movq r15, (hello.as_ptr() as u64); movq r14, 13; label: movq rdi, [r15]; movq rax, (test::PUTC as u64); call rax; addq r15, 1; subq r14, 1; cmpq r14, 0; jne label; // epilogue popq r14; popq r15; movq rsp, rbp; popq rbp; ret; ); jit.finalize() }
jit.enc_o(80, Reg::from(5)); jit.enc_rexw_mr(0x89, Reg::from(4), Or::reg(Reg::from(5))); jit.emitb(0xe8); jit.save_reloc(fac, 4); jit.emitl(0); jit.enc_rexw_mr(0x89,
Reg::from(0), Or::reg(Reg::from(15))); jit.enc_rexw_mr(0x89, Reg::from(0), Or::reg(Reg::from(6))); let imm = fmt.as_ptr() as u64; let rm_op = Or::reg(Reg::from(7)); if imm == 0 { jit.enc_rexw_mr(49, Reg::from(7), Or::reg(Reg::from(7))); } else if imm <= 0xffff_ffff { jit.enc_rexw_mi(0xc7, rm_op); jit.emitl(imm as u32); } else { jit.enc_rexw_o(0xb8, Reg::from(7)); jit.emitq(imm); }; monoasm!が吐くRustコード
monoruby • Rubyっぽい文法 • 変数は整数のみ、四則演算できる • if文が使える • 関数・ローカル変数が使える •
パーサはnomを使用 • 抽象構文木をアセンブリに変換
加算命令のコード生成 R12 R13 3 R14 5 R15 stack top R12
R13 8 R14 5 R15 stack top
条件分岐のコード生成
monoruby 1.064 s JS(node) 2.035 s C 0.763 s Ruby
10.700 s Benchmark