c++ - How to declare a function in LLVM and define it later -


how can declare function in llvm (with specific signature) , create call it, e.g.

llvm::value* return = m_builder.createcall( function, arguments ); 

but define body of function later (which must inlineasm function) ?

i'm later accessing functions in module in following way

for (llvm::module::iterator = mod->begin(), end = mod->end(); != end; ++it)  {      if( needsimplementation(it) ) {         llvm::inlineasm* inlinecall = ...         it.body = inlinecall // doesn't exist, pseudocode need      } } 

since signature same should possible believe.

from "kaleidoscope: code generation llvm ir" manual: http://llvm.org/docs/tutorial/langimpl3.html

3.4. function code generation

code generation prototypes , functions must handle number of details, make code less beautiful expression code generation, allows illustrate important points. first, lets talk code generation prototypes: they used both function bodies , external function declarations. code starts with:

function *prototypeast::codegen() {   // make function type:  double(double,double) etc.   std::vector<type*> doubles(args.size(),                              type::getdoublety(getglobalcontext()));   functiontype *ft = functiontype::get(type::getdoublety(getglobalcontext()),                                        doubles, false);    function *f = function::create(ft, function::externallinkage, name, themodule); 

later, when want add ir function should declaration module: themodule->getfunction(name); , add basicblock:

basicblock *bb = basicblock::create(getglobalcontext(), "entry", thefunction); builder.setinsertpoint(bb); 

ps: answer untested , answerer not expert in llvm.

pps: inlineasm function, think after doing searches metager, can't declare function cited kaleidoscope. way have inlineasm function created @ place of call. example of such usage here: cyanogenmod/android/art/compiler/llvm/runtime_support_builder_x86.cc#44

44 value* runtimesupportbuilderx86::emitgetcurrentthread() { 45  function* ori_func = getruntimesupportfunction(runtime_support::getcurrentthread);           //  ^^^^^ used know right type of function. 46  std::string inline_asm(stringprintf("mov %%fs:%d, $0", thread::selfoffset().int32value()));  // <<< define body of inlineasm 47  inlineasm* func = inlineasm::get(ori_func->getfunctiontype(), inline_asm, "=r", false);  // << create inlineasm function 48  callinst* thread = irb_.createcall(func); // << call 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -