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
libdcompile
Search
Fadis
May 09, 2015
Programming
0
240
libdcompile
LLVMとclangを使ってC++を実行時にコンパイルする方法を解説します
Fadis
May 09, 2015
Tweet
Share
More Decks by Fadis
See All by Fadis
C++でシェーダを書く
fadis
6
4.4k
ライトたくさんReSTIRを実装しよう
fadis
2
1.1k
コンピュータグラフィクスの空
fadis
7
2.3k
NNEFを読めるようになろう
fadis
0
950
低レイヤーから始める GUI
fadis
18
12k
いまどきのVulkan
fadis
14
7.6k
L2 WireGuard
fadis
2
4.4k
Wasserstein逆FM音源
fadis
3
3.6k
HSEとは何か
fadis
15
3.3k
Other Decks in Programming
See All in Programming
103 Early Hints
sugi_0000
1
230
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
230
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
770
Zoneless Testing
rainerhahnekamp
0
120
Haze - Real time background blurring
chrisbanes
1
510
Recoilを剥がしている話
kirik
5
6.6k
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
310
return文におけるstd::moveについて
onihusube
1
980
ソフトウェアの振る舞いに着目し 複雑な要件の開発に立ち向かう
rickyban
0
890
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
200
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
2
460
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
GraphQLとの向き合い方2022年版
quramy
44
13k
A designer walks into a library…
pauljervisheath
204
24k
Become a Pro
speakerdeck
PRO
26
5k
It's Worth the Effort
3n
183
28k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
How to train your dragon (web standard)
notwaldorf
88
5.7k
The Cult of Friendly URLs
andyhume
78
6.1k
Scaling GitHub
holman
458
140k
Optimising Largest Contentful Paint
csswizardry
33
3k
Transcript
libdcompile Naomasa Matsubayashi
@fadis_ ࠷ۙͷ"ࣾ $ 7JN (FOUPP มଶΛࣝผ͢Δཁૉ
int main() {! float a = 5.0f;! std::vector< boost::filesystem::path >
f;! f.push_back( "hoge.cpp" );! f.push_back( "fuga.cpp" );! dcompile::dynamic_compiler dc;! dc.getLoader().enableSystemPath();! if( !dc.getLoader().load( "jpeg" ) )! throw Ouch();! dc.getHeaderPath().enableSystemPath();! dcompile::module lib = dc( f.begin(), f.end() );! boost::optional< dcompile::function > foo =! lib.getFunction( "foo" );! if( foo )! (*foo)( &a );! else! throw Ouch();! std::cout << a << " outside" << std::endl;! }
$ ʹແͯ͘൵͍͠ͷ FWBM
FWBMͱ 1FSMͷ߹ FWBMQSJOUa)FMMP 8PSMEaOa ͔͜͜Β ͜͜·Ͱ จࣈྻ จࣈྻͰ༩͑ΒΕͨίʔυΛ࣮ߦ͢Δ
FWBMͱ จࣈྻͰ༩͑ΒΕͨίʔυΛ࣮ߦ͢Δ @45%*/ FWBM@ ਖ਼͍͠ DBUIPHFQM @45%*/ FWBM@ FDIPQSJOU)FMMP 8PSMEaOcQFSMIPHFQM
)FMMP 8PSME
FWBMͱ DBUIPHFQM @45%*/ FWBM@ FDIPQSJOU)FMMP 8PSMEaOcQFSMIPHFQM )FMMP 8PSME ॲཧ༰͕QSJOUʹܾ·ͬͨͷ࣮ߦ࣌
࣮ߦ࣌ʹίʔυΛղऍͯ͠ Ϛγϯޠʹམͱ͢खஈ͕ඞཁ
FWBMͱ ࣮ߦ࣌ʹίʔυΛղऍͯ͠ Ϛγϯޠʹམͱ͢खஈ͕ඞཁ 1FSM 0, 3VCZ 0, 1ZUIPO 0, +BWB4DSJQU
0, $ PS[ $ PS[ % ίϯύΠϧ࣌ͷΈ +BWB PS[ $ 0, )BTLFMM 0, ίϯύΠϥ͕ϥΠϒϥʹؚ·Ε͍ͯΔ
ϥΠϒϥϦʹͳͬͯΔ$ ίϯύΠϥ --7. DMBOH ͜ΕΛ͑$ ͰFWBMͰ͖Δͣ
MJCEDPNQJMF IUUQTHJUIVCDPN'BEJTMJCEDPNQJMF
Ϛγϯޠ Ϛγϯޠͷੜ DMBOH --7. VTFS $ #$ $ #$ ߏจղੳ
#$*3 ϦϯΫ
--7.ಡΈࠐΜͩ#$ͷѻ͍Λ छྨ͔ΒબΔ ίϯύΠϧ Ϛγϯޠʹมͯ݁͠ՌΛ ϑΝΠϧʹॻ͖ग़͢ ΠϯλϓϦλ ߦͮͭಡΈࠐΜͰ࣮ߦ +*5ίϯύΠϧ ඞཁ࠷খݶͷ෦͚ͩΛ ίϯύΠϧ͠ͳ͕Β࣮ߦ
௨ৗ$ ͷίʔυΛίϯύΠϧ͢Δ߹ ʮίϯύΠϧʯ͕༻͍ΒΕΔ
FWBMΛ͢Δ߹ίϯύΠϧΛ͖͢Ͱͳ͍ --7.ʹ&-'ͱ͔1&ͱ͔Λѻ͑ΔϦϯΧ͕ͳ͍ 3FRVJSFNFOU(/6CJOVUJMT ͱ͔ͯͳ͍͠ ίϯύΠϧҎ֎ͷํ๏Ͱ࣮ߦ͢Δͱ ෆࢥٞͳྗʹΑΓԿࣄແ࣮͘ߦग़དྷΔ
ϦϯΧΛΘͣʹͲ͏ͬͯ ґଘϥΠϒϥϦΛϦϯΫ͍ͯ͠Δͷ͔ --7.ΠϯλϓϦλ+*5Ͱ࣮ߦ͢Δࡍ ಉҰ#$தʹݟ͔ͭΒͳ͍γϯϘϧΛ EMGDOͰ୳ͦ͏ͱ͢Δ άϩʔόϧม JOUB ίϯύΠϧ࣌ίϯύΠϧ JODMVEFJPTUFSBN FYUFSOJOUB
JOUNBJO \ TUEDPVUB TUEFOEM ^ ࣮ߦ࣌ίϯύΠϧ ݟ͑Δʂ
--7.ΠϯλϓϦλ+*5Ͱ࣮ߦ͢Δࡍ ಉҰ--7.*3தʹݟ͔ͭΒͳ͍γϯϘϧΛ EMGDOͰ୳ͦ͏ͱ͢Δ ϦϯΧΛΘͣʹͲ͏ͬͯ ґଘϥΠϒϥϦΛϦϯΫ͍ͯ͠Δͷ͔ EMGDOͰݟ͔ͭΔͷ ݩʑόΠφϦதʹ͋ͬ ͨγϯϘϧ 35%-@(-0#"-Ͱ ϩʔυͨ͠γϯϘϧ
͋Β͔͡ΊඞཁͳϥΠϒϥϦΛEMPQFOͯ͋͠Δͱ ࣮ߦ࣌ʹίϯύΠϧ͞ΕΔίʔυͷத͔Β ͦͷϥΠϒϥϦͷ͕ؔԿͱͳ͘ݺΔ
ϦϯΧΛΘͣʹͲ͏ͬͯ ґଘϥΠϒϥϦΛϦϯΫ͍ͯ͠Δͷ͔ EMGDO104*9ݻ༗ --7.ʹ γεςϜͷμΠφϛοΫϩʔυΛநԽͨ͠ ͕ؔ༻ҙ͞Ε͍ͯΔ ͨͩ͠ϩʔυ͚ͩ MMWNTZT%ZOBNJD-JCSBSZ-PBE-JCSBSZ1FSNBOFOUMZ
ϦϯΧΛΘͣʹͲ͏ͬͯ ґଘϥΠϒϥϦΛϦϯΫ͍ͯ͠Δͷ͔ JODMVEFKQFHMJCI JOUNBJO \ TUSVDUKQFH@DPNQSFTT@TUSVDUDJOGP KQFH@DSFBUF@DPNQSFTT DJOGP ^
MJCKQFH͕ཁΔ ࣮ߦ։࢝ MJCKQFHΛ μΠφϛοΫϩʔυ ࠨ্ͷίʔυΛ +*5ίϯύΠϧ ͜ͷ࣌ͰEMGDO͔Βݟ͑Δͱ͜Ζʹ MJCKQFHͷγϯϘϧ͕͋ΔͷͰ Կࣄແ͔͔ͬͨͷΑ͏ʹ࣮ߦग़དྷΔ
int main() {! float a = 5.0f;! std::vector< boost::filesystem::path >
f;! f.push_back( "hoge.cpp" );! f.push_back( "fuga.cpp" );! dcompile::dynamic_compiler dc;! dc.getLoader().enableSystemPath();! if( !dc.getLoader().load( "jpeg" ) )! throw Ouch();! dc.getHeaderPath().enableSystemPath();! dcompile::module lib = dc( f.begin(), f.end() );! boost::optional< dcompile::function > foo =! lib.getFunction( "foo" );! if( foo )! (*foo)( &a );! else! throw Ouch();! std::cout << a << " outside" << std::endl;! } f.push_back( "hoge.cpp" );! f.push_back( "fuga.cpp" ); dcompile::dynamic_compiler dc; dc.getLoader().enableSystemPath();! if(! !dc.getLoader().load( "jpeg" ) )! throw Ouch(); dc.getHeaderPath()! .enableSystemPath(); dcompile::module lib =! dc( f.begin(), f.end() ); boost::optional<! dcompile::function! > foo = lib.getFunction( "foo" ); (*foo)( &a );
#include <jpeglib.h>! extern "C" void moo( float *a );! extern
"C" void foo( float *a ) {! struct jpeg_compress_struct cinfo;! jpeg_create_compress( &cinfo );! moo( a );! } #include <iostream>! extern "C"! void moo( float *a ) {! std::cout << *a << " inslide" << std::endl;! std::cout << "Hello, world!" << std::endl;! *a += 0.1f;! } IPHFDQQ GVHBDQQ
ಉҰϓϩηε ϗετίʔυ MJCKQFH NBJO qPBUB IPHFDQQ GPP GVHBDQQ NPP ॲཧͷྲྀΕ
JOTMJEF )FMMP XPSME PVUTJEF ͳΜ͔ ૿͑ͯΔ
%ͳΜͱ͔͞Μ͕࡞ͬͨ #0045@11ͩΒ͚ͷίʔυ தͳΜͱ͔͞Μ͕࡞ͬͨ DPOTUFYQSͩΒ͚ͷίʔυ ࣮ߦ࣌ʹ࣮ߦग़དྷΔʂ ͍͢͝ʂ ࠷ॳ͔Β࣮ߦ࣌ʹॲཧ͢ΔΑ͏ʹॻ͚ʂ
DMBOHͷา͖ํ DMBOHͷιʔεʹؔ͢Δ࠷ݶͷυΩϡϝϯτ ͦΕEPYZHFO DMBOHͷιʔεʹؔ͢ΔৄࡉͳυΩϡϝϯτ ͦΕιʔε Θ͔Βͳ͍͜ͱιʔεʹฉ͚
DMBOHͷา͖ํ --7.ͱDMBOHͷࠓ͕Θ͔Δ ߪಡແྉ HJUDMPOFIUU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FSDQQ ߦ͋ͨΓ͔ΒNBJO
DMBOHͷา͖ํ MMWN*OJUJBMJ[F"MM5BSHFUT MMWN*OJUJBMJ[F"MM5BSHFU.$T MMWN*OJUJBMJ[F"MM"TN1SJOUFST MMWN*OJUJBMJ[F"MM"TN1BSTFST
--7.@4063$&@%*3 UPPMTDMBOHUPPMTESJWFSDD@NBJODQQ ߦ͋ͨΓ͔ΒDD@NBJO MMWNͷ͍͍ՃݮͳॳظԽ 4VDDFTT&YFDVUF$PNQJMFS*OWPDBUJPO $MBOHHFU --7.@4063$&@%*3 UPPMTDMBOHMJC'SPOUFOE5PPM&YFDVUF$PNQJMFS*OWPDBUJPODQQ bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) { ߦ͋ͨΓ͔Β&YFDVUF$PNQJMFS*OWPDBUJPO
DMBOHͷา͖ํ ॏཁΫϥε$PNQJMFS*OTUBODF $PNQJMFS*OTUBOD 'JMF.BOBHFS 4PVSDF.BOBHFS %JBHOPTUJDT "DUJPO 5BSHFU0QUJPO
DMBOHͷา͖ํ ॏཁΫϥε$PNQJMFS*OTUBODF Success = Clang->ExecuteAction(*Act); --7.@4063$&@%*3 UPPMTDMBOHMJC'SPOUFOE5PPM&YFDVUF$PNQJMFS*OWPDBUJPODQQ ߦ͋ͨΓ͔Β&YFDVUF$PNQJMFS*OWPDBUJPO OwningPtr<FrontendAction>
Act(CreateFrontendAction(*Clang)); "DUJPOΛ$PNQJMFS*OTUBODFʹͯ͠ &YFDVUF"DUJPO͢Δ $PNQJMFS*OTUBODF͕ύʔαΛݺͼग़͠ ߏจղੳͷ݁Ռݟ͔ͭͬͨཁૉʹԠͯ͡ "DUJPOͷରԠ͢Δϝϯό͕ؔݺΕΔ
DMBOHͷา͖ํ ࠷ॳ͔Β༻ҙ͞Ε͍ͯΔΞΫγϣϯ &NJU"TTFNCMZ"DUJPO &NJU#$"DUJPO &NJU--7."DUJPO &NJU--7.0OMZ"DUJPO &NJU$PEF(FO0OMZ"DUJPO &NJU0CK"DUJPO 0CK·Ͱ͍࣋ͬͯͬͯ͠·͏ͱ ϦϯΫग़དྷͳͯ͘٧Ή
--7.ͷา͖ํ ෳͷ--7.Ϟδϡʔϧ CDͱ͔͍ͭͯΔͭ Λ ͭͷ--7.Ϟδϡʔϧʹ·ͱΊΔ --7.-JOLFSʹ͞ΕͨϞδϡʔϧ --7.-JOLFS͕ղ์͢Δ --7.-JOLFS͕࡞ͬͨϞδϡʔϧ ໌ࣔతʹࢦఆ͠ͳ͍ͱ--7.-JOLFS͕ղ์͢Δ #$
#$ #$ ϦϯΫ
--7.ͷา͖ํ ग़དྷ্͕ͬͨϞδϡʔϧ Ͳ͏࣮ͬͯߦ͢Ε͍͍ͷ ιʔεʹฉ͚ʂ --7.@4063$&@%*3 UPPMTMMJMMJDQQ ߦ͋ͨΓ͔ΒNBJO *OJUJBMJ[F/BUJWF5BSHFU
*OJUJBMJ[F/BUJWF5BSHFU"TN1SJOUFS ωΠςΟϒλʔήοτͷॳظԽ
--7.ͷา͖ํ ϞδϡʔϧΛ&OHJOF#VJMEFSʹ͠ λʔήοτΛࢦఆͯ͠ ΠϯλϓϦλ࣮ߦ͔+*5ίϯύΠϧ͔બͼ &YFDVUJPO&OHJOFΛੜ͢Δ &OHJOF#VJMEFSCVJMEFS .PE CVJMEFSTFU."SDI ."SDI
CVJMEFSTFU.$16 .$16 CVJMEFSTFU."UUST ."UUST CVJMEFSTFU6TF.$+*5 USVF &&CVJMEFSDSFBUF
--7.ͷา͖ํ &&SVO4UBUJD$POTUSVDUPST%FTUSVDUPST GBMTF ϞδϡʔϧͷίϯετϥΫλΛ࣮ߦ JOU3FTVMU&&SVO'VODUJPO"T.BJO &OUSZ'O *OQVU"SHW FOWQ
ࢦఆͨؔ͠ΛNBJOؔͱݟͳ࣮ͯ͠ߦ ࣗͰҾΛਖ਼͘͠ઃఆ͢Ε NBJOؔͱܕͷҧ͏͔ؔΒ࣮ߦ͢Δ͜ͱՄೳ (FOFSJD7BMVF3FTVMUFOHJOFSVO'VODUJPO &OUSZ'O "SHT &&SVO4UBUJD$POTUSVDUPST%FTUSVDUPST USVF ϞδϡʔϧͷσετϥΫλΛ࣮ߦ
MJCDMBOHʹ͍ͭͯ DMBOHͷϥΠϒϥϦͬͯࠓྲྀߦͷMJCDMBOH ͕͍ͪ·͢ DMBOHͷϥΠϒϥϦ܉ஂ MJCDMBOH"3$.JHSBUF MJCDMBOH"45B MJCDMBOH"OBMZTJTB MJCDMBOH#BTJDB MJCDMBOH$PEF(FOB MJCDMBOH%SJWFSB
MJCDMBOHTP DMBOHͷϥΠϒϥϦΛ $ݴޠΠϯλʔϑΣʔεͰ ϥοϓͨ͠ͷ
MJCDMBOHʹ͍ͭͯ MJCDMBOH͞Μ ߏจղੳΛߦ͏ͱ͜Ζ·Ͱ͔͠ϥοϓ͍ͯ͠ͳ͍ MJCDMBOHͰݡ͍γϯλοΫεͱ͔͋ͬͯ MJCDMBOHͰίϯύΠϧͱ͔ฉ͔ͳ͍ͷͦͷͨΊ $ FSϥούΛհ͞ͳͯ͘ શͯͷϥΠϒϥϦΛར༻Ͱ͖Δ MJCDMBOHͱ͔͍ΒΜ͔ͬͨΜʂ $
FSʹݶΔ
ϦϯΫ͖͢ϥΠϒϥϦ 2DMBOHͱ--7.ͷϥΠϒϥϦࢁ͋Γ·͕͢ɺ ࣗͷίʔυΛಈ͔ͨ͢Ίʹඞཁͳͷ ͲΕͱͲΕͰ͠ΐ͏ ͲͷγϯϘϧ͕ͲͷϥΠϒϥϦʹؚ·Ε͍ͯΔ͔ Ͳ͜ʹॻ͔Ε͍ͯͳ͍ "શ෦ϦϯΫ͔ͯ͠Βͭͮͭ֎ͯ͠Έͯɺ ϦϯΫΤϥʔ͕ग़ΔϠπͬͯΔʂ
ϦϯΫ͖͢ϥΠϒϥϦ MJCDMBOH"3$.JHSBUF MJCDMBOH"45B MJCDMBOH"OBMZTJTB MJCDMBOH#BTJDB MJCDMBOH$PEF(FOB MJCDMBOH%SJWFSB MJCDMBOH&EJUB MJCDMBOH'SPOU&OEB MJCDMBOH'SPOU&OE5PPM
MJCDMBOH*OEFYB MJCDMBOH-FYB MJCDMBOH1BSTFB MJCDMBOH3FXSJUFB MJCDMBOH4FNBB MJCDMBOH4FSJBMJ[BUJPO MJC--7."TN1BSTFSB MJC--7."TN1SJOUFSB MJC--7.#JU3FBEFSB MJC--7.&YFDVUJPO&OHJOF MJC--7.+*5B MJC--7..$+*5B MJC--7.$PEF(FOB MJC--7.JQPB MJC--7.$PSFB MJC--7.-JOLFSB MJC--7.*OTUSVNFOUBUJPOB MJCEDPNQJMFͷ߹
ݱ࣌Ͱͷ՝ ࣮ߦ࣌ίϯύΠϧ͞Εͨίʔυͷؔݺͼग़࣌͠ ҾͷੵΈํ͕ಾ ݱঢ়όΠτҎԼͷQPEܕ͔ͤ͠ͳ͍ ϓϦϓϩηοαεςʔδͷΈ࣮ߦ͕Ͱ͖ͳ͍ ݱࡏ࣮࡞ۀத --7.ͷόʔδϣϯ্͕͕Δͨͼʹ ΠϯλʔϑΣʔε͕มΘΔ
݁ $ Ͱ࣮ߦ࣌ίϯύΠϧ Ͱ͖Δ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠