Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Rustの手続きマクロで黒魔術入門
Search
lemolatoon
March 11, 2023
Programming
1.4k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rustの手続きマクロで黒魔術入門
Rustの手続きマクロについて紹介します。そして、自作関数手続きマクロである direct_c_lang についても紹介します。
lemolatoon
March 11, 2023
Transcript
9 slides
Rustの手続きマクロで黒魔術入門 lemolatoon
自己紹介 慶應義塾大学 理工学部1年 セキュリティ・キャンプ2022 Cコンパイラゼミ修了生 Twitter (@lemolatoon1)
(このスライドは上記アカウントでツイートしています) 2
“Rustの手続きマクロ”って? Rustを書くときによく見る手続きマクロの例 1) deriveマクロ 2) attributeマクロ 3 ソースコードを受け取って、ソースコードを返すようなプログラミング言語Rustの特殊な関数
3) 関数マクロ
自由度の高い関数手続きマクロ 4 * カッコの対応などは入力時点で正しい必要がある 関数手続きマクロは正しいRustのコードを出力すれば、 どんな入力*でも受け付ける。
自作関数手続きマクロ direct_c_lang の紹介 5 https://github.com/lemolatoon/direct_c_lang_rs.git C言語のコードを受け取り、Rustから呼べるインターフェースを提供する。 (下記は実際に使っている例 ) Rust側からマクロ内で定義されたC言語
で書かれた関数を呼び出している。 (4~10行) C言語のソースコードをマクロに渡し ている。 (12~25行)
自作関数手続きマクロ direct_c_lang の紹介(マクロの展開) 6 ◼マクロの入力を global_asmマクロ+C言語側の関数 の宣言へ変換 ◼マクロ内部で自作Cコンパイラを使っている ◼マクロ展開前 ◼マクロ展開後
実際の手続きマクロの中身 7 * https://github.com/lemolatoon/direct_c_lang_rs.git マクロの変換前から、マクロの変換後の処理自体をRustでかける。 中略(Cの関数をRustの宣言に変換) マクロの入力として与えられ たC言語のソースコードを アセンブリに変換
マクロの出力(展開先)を関 数の戻り値として返す
まとめ Rustの手続きマクロは自由度が高い上に、マクロ処理自体をRustでかけるため、 C/C++のような複雑性が生じにくい 自由度を生かして自分の独自のマクロを作ると楽しい 「書けばいいことは明確になったけど、これ全部手で書かなきゃいけないのか ……」といったときに便利 8
ありがとうございました 9
More Decks by lemolatoon
See All by lemolatoon
スマホから Youtube Shortsを見られないようにする
lemolatoon
29
39k
お家miniPCを使ってミリしらでkubernetes clusterたててみる
lemolatoon
1
370
【Rust × C++】 夢の共演を試した話
lemolatoon
1
1.8k
no_stdでもasyncしたい
lemolatoon
0
290
Other Decks in Programming
See All in Programming
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
1
370
止まらない比較を止める — 余帰納法で無限ストリームを比べる
tarao
0
190
プロジェクトコードをキレイキレイしよう!暗黙的な参照を読み解く静的解析
ostk0069
0
220
疎結合とは何か?ソフトウェア設計における結合バランスについて考える #phpcon_niigata
kajitack
2
330
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
1
1k
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
320
Spec-Driven Development - Why Vaadin Is the Perfect Fit
simas
PRO
0
260
AI時代のTerminal環境を UNIX哲学で設計・運用する
yasunori0418
1
360
複数ドメインを一つのアプリに統合するFlutterの設計と運用_FlutterKaigi2026登壇資料(須永高弘)
takahirosunaga
0
200
Kotlin JVM用に作られたライブラリのKMPを前提とした再設計
jsoizo
0
190
Zero-Tap Sign-In対応、結局何をすればいいの?
mitohato14
0
2.4k
GraphQL の onError と Nullability のこれから
quramy
0
220
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
356
22k
Building Better People: How to give real-time feedback that sticks.
wjessup
371
20k
Large-scale JavaScript Application Architecture
addyosmani
516
110k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.4k
What does AI have to do with Human Rights?
axbom
PRO
1
2.5k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Designing for Timeless Needs
cassininazir
1
570
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
670
Writing Fast Ruby
sferik
630
63k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k