[cfe-dev] Calling Extern Function

Paul Griffiths gafferuk at gmail.com
Wed Aug 18 04:38:24 PDT 2010


Can someone please tell me what I am doing wrong?

Im trying to register an external function with the JIT, so can call
functions in my music application(heres an image :
http://img534.imageshack.us/img534/3995/87647073.jpg).

Here my functions

int yipee(int aVar)
{
    return 5;
}

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

	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 =
llvm::FunctionType::get(llvm::Type::getInt32Ty(llvm::getGlobalContext()),
		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;
	Args.push_back(Mod->getModuleIdentifier());

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



// here the C code to be ran with JIT
-------------------------------------------------------------------

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



More information about the cfe-dev mailing list