[cfe-dev] Calling Extern Function

o.j.sivart at gmail.com o.j.sivart at gmail.com
Wed Aug 18 07:36:51 PDT 2010


I assume you are compiling your program as c++. In this case your problem is that "yipee" will have been name mangled by the c++ compiler.

On 18/08/2010, at 9:08 PM, Paul Griffiths wrote:

> 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;
> }
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev





More information about the cfe-dev mailing list