[LLVMdev] clang: call extern function using JIT

gafferuk gafferuk at gmail.com
Wed Aug 18 03:58:06 PDT 2010

Im trying to call fuctions in my music application from c code which will be
used by the JIT.

Here my function, i have made no other changes to the clang interpreter
example. Can you see anything else I have to add to either the fuction below
or to the c code to be JIT at the bottom?

// function I wish to call.
int yipee(int aVar)
    aVar = 5;

int Execute(llvm::Module *Mod, char * const *envp) {

	std::string Error;
	llvm::OwningPtr<llvm::ExecutionEngine> EE(
		llvm::ExecutionEngine::createJIT(Mod, &Error));
	if (!EE) {
		llvm::errs() << "unable to make execution engine: " << Error << "\n";
		return 255;

	// code I have added
	llvm::FunctionType* ft =
		std::vector<const llvm::Type*>(0,
llvm::Type::getInt32Ty(llvm::getGlobalContext())), false);

	llvm::Function* f = llvm::Function::Create(ft,
llvm::Function::ExternalLinkage, "yipee", Mod);
	EE->addGlobalMapping(f, yipee);
	llvm::Function *YipeeFn = Mod->getFunction("yipee");
	if (!YipeeFn) {
		llvm::errs() << "'yipee' function not found in module.\n";
		return 255;
	// end code I have added

	llvm::Function *EntryFn = Mod->getFunction("main");
	if (!EntryFn) {
		llvm::errs() << "'main' function not found in module.\n";
		return 255;

	// FIXME: Support passing arguments.
	std::vector<std::string> Args;

	return EE->runFunctionAsMain(EntryFn, Args, envp);

// here the C code to be ran with JIT

int main() {
	int dd = yipee(1);
  return 1;

Heres another screenshot of my music application, im very happy with the
results but the code editor is lookin a little bare, i really need clang! :)


Eric Christopher-2 wrote:
> On Aug 17, 2010, at 1:56 PM, gafferuk wrote:
>> hi, im creating a music application(image below).
>> At the moment im using tcc compiler but im moving across to clang because
>> of
>> the improved compiler warnings.
>> Can anyone please explain and show code so I can use clang's JIT to call
>> functions in my application?
> You'll probably want to take a look at the Kaleidoscope tutorial[1] on how
> to get
> the JIT up and running inside an application of its own.  If you can give
> me
> a little more information as to what you're trying to JIT it'd be helpful.
> -eric
> [1] http://llvm.org/docs/tutorial/
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

View this message in context: http://old.nabble.com/clang%3A-call-extern-function-using-JIT-tp29449300p29469972.html
Sent from the LLVM - Dev mailing list archive at Nabble.com.

More information about the llvm-dev mailing list