[LLVMdev] Hello World

Eli Friedman eli.friedman at gmail.com
Thu Jun 24 14:32:30 PDT 2010


2010/6/24 FĂ©lix Cloutier <felixcca at yahoo.ca>:
> Hello everyone,
>
> 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.
>
> 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?

Instruction::insertBefore?  Or if you prefer, you can mess with the
result of BasicBlock::getInstList() directly.

> 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).

You can cast your pointer to an integer, create an LLVM integer
constant, and create a constant cast from that to the relevant pointer
type.  Or, you can add an arbitrarily-named Function to the module,
and map it to the appropriate address with
ExecutionEngine::addGlobalMapping.  Whichever is more convenient...

-Eli




More information about the llvm-dev mailing list