<div dir="ltr">Hi all,<div><br></div><div>So I managed to get something together from different bits available of Internet:</div><div><p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(8,250,149);background-color:rgb(0,0,0)"><span style="color:rgb(255,255,255)"> </span>llvm<span style="color:rgb(255,255,255)">::</span>LLVMContext<span style="color:rgb(255,255,255)"> context;</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(255,255,255);background-color:rgb(0,0,0);min-height:13px"> </p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(8,250,149);background-color:rgb(0,0,0)"><span style="color:rgb(255,255,255)"> </span>std<span style="color:rgb(255,255,255)">::</span><span style="color:rgb(0,177,255)">unique_ptr</span><span style="color:rgb(255,255,255)"><</span>clang<span style="color:rgb(255,255,255)">::</span>CodeGenAction<span style="color:rgb(255,255,255)">> action = </span>std<span style="color:rgb(255,255,255)">::</span><span style="color:rgb(0,177,255)">make_unique</span><span style="color:rgb(255,255,255)"><</span>clang<span style="color:rgb(255,255,255)">::</span>EmitLLVMOnlyAction<span style="color:rgb(255,255,255)">>(&context);</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(255,255,255);background-color:rgb(0,0,0)"> <span style="color:rgb(8,250,149)">clang</span>::<span style="color:rgb(8,250,149)">tooling</span>::<span style="color:rgb(8,250,149)">runToolOnCode</span><span style="color:rgb(75,209,86)">/*WithArgs*/</span>(action.<span style="color:rgb(0,177,255)">get</span>(), fullfile);</p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(255,255,255);background-color:rgb(0,0,0);min-height:13px"> </p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(255,255,255);background-color:rgb(0,0,0)"> <span style="color:rgb(8,250,149)">std</span>::<span style="color:rgb(0,177,255)">unique_ptr</span><<span style="color:rgb(8,250,149)">llvm</span>::<span style="color:rgb(8,250,149)">Module</span>> module = action-><span style="color:rgb(8,250,149)">takeModule</span>();</p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(8,250,149);background-color:rgb(0,0,0)"><span style="color:rgb(255,255,255)"> </span>llvm<span style="color:rgb(255,255,255)">::</span>InitializeNativeTarget<span style="color:rgb(255,255,255)">();</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(8,250,149);background-color:rgb(0,0,0)"><span style="color:rgb(255,255,255)"> </span>llvm<span style="color:rgb(255,255,255)">::</span>InitializeNativeTargetAsmPrinter<span style="color:rgb(255,255,255)">();</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(8,250,149);background-color:rgb(0,0,0)"><span style="color:rgb(255,255,255)"> </span>llvm<span style="color:rgb(255,255,255)">::</span>InitializeNativeTargetAsmParser<span style="color:rgb(255,255,255)">();</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(255,255,255);background-color:rgb(0,0,0);min-height:13px"> </p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(255,255,255);background-color:rgb(0,0,0)"> <span style="color:rgb(8,250,149)">llvm</span>::<span style="color:rgb(8,250,149)">EngineBuilder</span> builder(<span style="color:rgb(8,250,149)">std</span>::<span style="color:rgb(0,177,255)">move</span>(module));</p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(255,255,255);background-color:rgb(0,0,0)"> builder.<span style="color:rgb(8,250,149)">setMCJITMemoryManager</span>(<span style="color:rgb(8,250,149)">std</span>::<span style="color:rgb(0,177,255)">make_unique</span><<span style="color:rgb(8,250,149)">llvm</span>::<span style="color:rgb(8,250,149)">SectionMemoryManager</span>>());<br></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(8,250,149);background-color:rgb(0,0,0)"><span style="color:rgb(255,255,255)"> builder.</span>setOptLevel<span style="color:rgb(255,255,255)">(</span>llvm<span style="color:rgb(255,255,255)">::</span>CodeGenOpt<span style="color:rgb(255,255,255)">::</span>Level<span style="color:rgb(255,255,255)">::</span>Aggressive<span style="color:rgb(255,255,255)">);</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(255,255,255);background-color:rgb(0,0,0)"> <span style="color:rgb(8,250,149)">EE</span>.<span style="color:rgb(0,177,255)">reset</span>(builder.<span style="color:rgb(8,250,149)">create</span>());</p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(255,255,255);background-color:rgb(0,0,0);min-height:13px"> </p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:"Fira Code";color:rgb(255,255,255);background-color:rgb(0,0,0)"> <span style="color:rgb(222,56,165)">return</span> <span style="color:rgb(222,56,165)">reinterpret_cast</span><Function>(<span style="color:rgb(8,250,149)">EE</span>-><span style="color:rgb(8,250,149)">getFunctionAddress</span>(function));</p></div><div><br></div><div>Unfortunately, when calling builder.create, there is an ASAN error:</div><div><p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Andale Mono";color:rgb(195,55,32);background-color:rgba(0,0,0,0.901961)"><span style="font-variant-ligatures:no-common-ligatures">==18530==ERROR: AddressSanitizer: SEGV on unknown address 0x0000000000d8 (pc 0x0001114da737 bp 0x7ffee62a2530 sp 0x7ffee62a2510 T0)</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Andale Mono";color:rgb(40,254,20);background-color:rgba(0,0,0,0.901961)"><span style="font-variant-ligatures:no-common-ligatures">==18530==The signal is caused by a READ memory access.</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Andale Mono";color:rgb(40,254,20);background-color:rgba(0,0,0,0.901961)"><span style="font-variant-ligatures:no-common-ligatures">==18530==Hint: address points to the zero page.</span></p><p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Andale Mono";color:rgb(40,254,20);background-color:rgba(0,0,0,0.901961)"><span style="font-variant-ligatures:no-common-ligatures"> #0 0x1114da736 in std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (libc++.1.dylib:x86_64+0x3e736)</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Andale Mono";color:rgb(40,254,20);background-color:rgba(0,0,0,0.901961)"><span style="font-variant-ligatures:no-common-ligatures"> #1 0x10a43446c in llvm::Twine::str() const</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Andale Mono";color:rgb(40,254,20);background-color:rgba(0,0,0,0.901961)"><span style="font-variant-ligatures:no-common-ligatures"> #2 0x10a42ef9e in llvm::Triple::Triple(llvm::Twine const&)</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Andale Mono";color:rgb(40,254,20);background-color:rgba(0,0,0,0.901961)"><span style="font-variant-ligatures:no-common-ligatures"> #3 0x109f320cc in llvm::EngineBuilder::create(llvm::TargetMachine*)</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Andale Mono";color:rgb(40,254,20);background-color:rgba(0,0,0,0.901961)"><span style="font-variant-ligatures:no-common-ligatures"> #4 0x109a4b614 in llvm::EngineBuilder::create() ExecutionEngine.h:659</span></p></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div>I suppose there is one bit missing in the native target initialization, but can't find any relavant pointer online.</div><div>Does anyone have a clue?</div><div><br></div><div>Cheers,</div><div><br></div><div>Matthieu</div></div><br><div class="gmail_quote"><div dir="ltr">Le sam. 14 juil. 2018 à 09:35, Matthieu Brucher <<a href="mailto:matthieu.brucher@gmail.com">matthieu.brucher@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi all,<div><br></div><div>I'm wondering how difficult it is to go from the libtooling interface to the LLVM IR. The idea is to then use the JIT facilities to execute some C++ code.</div><div>It seems Libtooling requires far less code and far less adaptations to go from a code snippet to enything than clang itself (to get all the pieces together, it seems that it requires almost 50 lines whereas libtooling more or less has only one statement).<br clear="all"><div>Any pointers for this?</div><div><br></div><div>Cheers,</div><div><br></div><div>Matthieu</div>-- <br><div dir="ltr" class="m_-2751895553628607050gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Quantitative analyst, Ph.D.<br>Blog: <a href="http://blog.audio-tk.com/" target="_blank">http://blog.audio-tk.com/</a><br>LinkedIn: <a href="http://www.linkedin.com/in/matthieubrucher" target="_blank">http://www.linkedin.com/in/matthieubrucher</a></div></div></div></div></div></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Quantitative analyst, Ph.D.<br>Blog: <a href="http://blog.audio-tk.com/" target="_blank">http://blog.audio-tk.com/</a><br>LinkedIn: <a href="http://www.linkedin.com/in/matthieubrucher" target="_blank">http://www.linkedin.com/in/matthieubrucher</a></div></div></div></div></div>