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
More Decks by lemolatoon
See All by lemolatoon
スマホから Youtube Shortsを見られないようにする
lemolatoon
29
39k
お家miniPCを使ってミリしらでkubernetes clusterたててみる
lemolatoon
1
320
【Rust × C++】 夢の共演を試した話
lemolatoon
1
1.8k
no_stdでもasyncしたい
lemolatoon
0
280
Other Decks in Programming
See All in Programming
Are APIs Still Relevant in the AI Era?
soyuka
0
220
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
0
280
AHC070解法紹介
eijirou
0
120
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
370
新卒PdEのリアル
ryu1013
1
500
新人はどこまで自力でやり、どこからAIに頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
1
620
kubernetes コンポーネント開発入門 / 新卒N年目の勉強会&交流会!〜〇〇への誘い〜 #n_study
mazrean
0
240
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
930
アクセシビリティから考える情報設計
high_g_engineer
0
370
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
0
150
巨大モノリシックアプリ モダン化大作戦
ktcryomm
0
1k
Omarchy Tokyo やると聞いて UMPC 買ってセットアップしてきた
mtsmfm
0
140
Featured
See All Featured
Evolving SEO for Evolving Search Engines
ryanjones
0
290
Dealing with People You Can't Stand - Big Design 2015
cassininazir
368
27k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
580
GitHub's CSS Performance
jonrohan
1033
470k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
460
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
Balancing Empowerment & Direction
lara
6
1.3k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
420
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
320
Building the Perfect Custom Keyboard
takai
2
870
It's Worth the Effort
3n
188
29k
Transcript
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