<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Felix,<div><br></div><div>It really depends on what you are doing with the JIT.<div><br></div><div>In my case, I have a scripting language that has a limited set of base data types. I wanted to be able to trivially interface to new C++ methods and classes, so I'm interfacing to C++ classes by creating external definitions much like the way Kaleidoscope accesses external C functions. I only have to deal with a few types so I can do the name mangling automatically pretty easily without having to do too much work. If you are just dealing with relatively simple functions it's not too hard. See section 5.1.2 of the document here:</div><div><br></div><div><a href="http://www.codesourcery.com/public/cxx-abi/abi.html">http://www.codesourcery.com/public/cxx-abi/abi.html</a> </div><div><br></div><div>for details on the mangling, if you are targeting the Itanium ABI which is the gcc/clang default ABI for X86-64.</div><div><br></div><div>I have found the following suggestion (made by someone on the llvm IRC channel) to figure out how LLVM IR works extremely helpful. First, I write a small sample code in C or C++ which I then compile into LLVM IR using:</div><div><br></div><div><font class="Apple-style-span" face="'Courier New'" size="3"><span class="Apple-style-span" style="font-size: 12px;">/usr/local/bin/clang++ -S -O0 -emit-llvm source.cpp</span></font></div><div><br></div><div>where <span class="Apple-style-span" style="font-family: 'Courier New'; font-size: 12px; ">source.cpp</span>  contains my sample C++ code or </div><div><br></div><div><font class="Apple-style-span" face="'Courier New'" size="3"><span class="Apple-style-span" style="font-size: 12px;">/usr/local/bin/clang -S -O0 -emit-llvm source.c </span></font></div><div><br></div><div><font class="Apple-style-span" face="'Courier New'" size="3"><span class="Apple-style-span" style="font-size: 12px;"><span class="Apple-style-span" style="font-family: Arial; font-size: medium; ">for C code.</span></span></font></div><div><br></div><div><font class="Apple-style-span" face="'Courier New'" size="3"><span class="Apple-style-span" style="font-size: 12px;"><span class="Apple-style-span" style="font-family: Arial; font-size: medium; ">I suggest building clang and LLVM from trunk if you are going to be doing much with C++ as 2.7's C++ support is not near as good as trunk's. </span></span></font></div><div><br></div><div><font class="Apple-style-span" face="'Courier New'" size="3"><span class="Apple-style-span" style="font-size: 12px;"><span class="Apple-style-span" style="font-family: Arial; font-size: medium; ">Once you've decided exactly what LLVM IR you want. You can see how to use the IRBuilder to create it using the very handy:</span></span></font></div><div><font class="Apple-style-span" face="'Courier New'" size="3"><span class="Apple-style-span" style="font-size: 12px;"><br></span></font></div><div><font class="Apple-style-span" face="'Courier New'" size="3"><span class="Apple-style-span" style="font-size: 12px;">/usr/local/bin/llc -march=cpp source.s</span></font></div><div><br></div><div>which creates a file called <font class="Apple-style-span" face="'Courier New'" size="3"><span class="Apple-style-span" style="font-size: 12px;">source.s.cpp</span></font> which contains the C++ which will create the LLVM IR found in <font class="Apple-style-span" face="'Courier New'" size="3"><span class="Apple-style-span" style="font-size: 12px;">source.s</span></font>.</div><div><br></div><div>I hope that helps.</div><div><br></div><div>- Curtis</div><div><br></div><div><br></div><div><div><div>On Jun 24, 2010, at 5:32 PM, Eli Friedman wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>2010/6/24 Félix Cloutier <<a href="mailto:felixcca@yahoo.ca">felixcca@yahoo.ca</a>>:<br><blockquote type="cite">Hello everyone,<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">I've been watching LLVM since about a year now, and I thought it was finally time I'd do something with it. So I've got my hands dirty with a cool project (who knows? maybe I'll end up releasing it), but since I'm fairly new to the whole thing, there are still a number of things I'm not too sure about.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">First, the way we create instructions confuses me a little. My best experience with factory-like patterns is the JavaScript DOM interface, where you'd do like document.createElement, mess with the new element, then put it back somewhere doing node.appendChild. However, seeing the results from llvm2cpp, it looks like simply creating an instruction is enough to put it somewhere (at the end of the specified basic block, I suppose) unless no parameter is specified. Skimming through the classes reference, I see no 'obvious' way to add an existing "orphan" instruction to a block. What would it be?<br></blockquote><br>Instruction::insertBefore?  Or if you prefer, you can mess with the<br>result of BasicBlock::getInstList() directly.<br><br><blockquote type="cite">Also, I'd like to integrate C++ method calls to my jitted code. From what I've seen in the Kaleidoscope tutorial it's easy to use external C functions but there's nothing about C++ functions or methods. Problem is, since their names are mangled, it won't be as easy as C for C functions. What I was really looking for was a way to create a Function object from a function pointer. I think I understand why it can't be that simple (LLVM really needs a function name, and function pointers don't have that) so I've made a funny hack with dladdr to get the name of a method at runtime, but just in case I really missed something I'll ask you guys: is there a "standard", LLVM-backed way to make a Function object from a function or method with C++ linkage? (I know I could also make an extern "C" function to wrap the call, but that's not quite as fun).<br></blockquote><br>You can cast your pointer to an integer, create an LLVM integer<br>constant, and create a constant cast from that to the relevant pointer<br>type.  Or, you can add an arbitrarily-named Function to the module,<br>and map it to the appropriate address with<br>ExecutionEngine::addGlobalMapping.  Whichever is more convenient...<br><br>-Eli<br><br>_______________________________________________<br>LLVM Developers mailing list<br><a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br></div></blockquote></div><br></div></div></body></html>