Upgrade to Pro — share decks privately, control downloads, hide ads and more …

libdcompile

 libdcompile

LLVMとclangを使ってC++を実行時にコンパイルする方法を解説します

Fadis

May 09, 2015
Tweet

More Decks by Fadis

Other Decks in Programming

Transcript

  1. 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;! }
  2. FWBMͱ͸ ࣮ߦ࣌ʹίʔυΛղऍͯ͠ Ϛγϯޠʹམͱ͢खஈ͕ඞཁ 1FSM 0, 3VCZ 0, 1ZUIPO 0, +BWB4DSJQU

    0, $ PS[ $ PS[ % ίϯύΠϧ࣌ͷΈ +BWB PS[ $ 0, )BTLFMM 0, ίϯύΠϥ͕ϥΠϒϥʹؚ·Ε͍ͯΔ
  3. ϦϯΧΛ࢖ΘͣʹͲ͏΍ͬͯ ґଘϥΠϒϥϦΛϦϯΫ͍ͯ͠Δͷ͔ JODMVEFKQFHMJCI JOUNBJO \ TUSVDUKQFH@DPNQSFTT@TUSVDUDJOGP KQFH@DSFBUF@DPNQSFTT DJOGP  ^

    MJCKQFH͕ཁΔ ࣮ߦ։࢝ MJCKQFHΛ μΠφϛοΫϩʔυ ࠨ্ͷίʔυΛ +*5ίϯύΠϧ ͜ͷ࣌఺ͰEMGDO͔Βݟ͑Δͱ͜Ζʹ MJCKQFHͷγϯϘϧ͕͋ΔͷͰ Կࣄ΋ແ͔͔ͬͨͷΑ͏ʹ࣮ߦग़དྷΔ
  4. 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 );
  5. #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
  6. ಉҰϓϩηε ϗετίʔυ MJCKQFH NBJO qPBUB IPHFDQQ GPP GVHBDQQ NPP ॲཧͷྲྀΕ

    JOTMJEF )FMMP XPSME PVUTJEF ͳΜ͔ ૿͑ͯΔ
  7. DMBOHͷา͖ํ ͰɺͲ͔͜ΒಡΊ͹ྑ͍ͷ NBJOؔ਺͔ΒͩΖKL 0XOJOH1US$PNQJMBUJPO$ 5IF%SJWFS#VJME$PNQJMBUJPO BSHW  JOU3FT DPOTU$PNNBOE 'BJMJOH$PNNBOE

    JG $HFU  3FT5IF%SJWFS&YFDVUF$PNQJMBUJPO $ 'BJMJOH$PNNBOE   --7.@4063$&@%*3 UPPMTDMBOHUPPMTESJWFSESJWFSDQQ DMBOHίϚϯυͷNBJOؔ਺ͷࡏॲ ߦ໨͋ͨΓ͔ΒNBJO
  8. DMBOHͷา͖ํ JOU3FT$&YFDVUF+PC $HFU+PCT 'BJMJOH$PNNB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CompilerInstance *Clang) { ߦ໨͋ͨΓ͔Β&YFDVUF$PNQJMFS*OWPDBUJPO
  11. 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ͷରԠ͢Δϝϯόؔ਺͕ݺ͹ΕΔ
  12. --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  ϞδϡʔϧͷσετϥΫλΛ࣮ߦ
  13. ϦϯΫ͢΂͖ϥΠϒϥϦ 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 M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