[LLVMdev] Redefining function

Conrado Miranda miranda.conrado at gmail.com
Fri Jan 29 11:50:59 PST 2010


Hi everybody.

I've just started learning about LLVM and didn't get too far studying the
core.
I couldn't find the solution to my problem (if it has one) in the mailing
list or the source code. The problem is: how can I redefine a function
that's been called already by some other function?

Suppose I have 3 files, all compiled to bytecode through llvm-gcc (I think
it could be clang instead).

File1.c:
void do_print() { print(); }

File2.c:
void print() { printf("File2.c\n"); }

File3.c:
void print() { printf("File3.c\n"); }

Then, I have the main file compiled to executable like this:
int main() {
  // initialize and get context (not shown)
  Module *file1(LoadFile("file1.bc",Context));
  Module *file2(LoadFile("file2.bc",Context));
  Module *file3(LoadFile("file3.bc",Context));

  Linker::LinkModules(file1, file2, NULL);

  EngineBuilder builder(file1);
  ExecutionEngine *EE = builder.create();

  EE->runStaticConstructorsDestructors(false);

  func = EE->FindFunctionNamed("do_print");
  EE->runFunction(func, std::vector<GenericValue>());

  //swap the definition of the function "print" from the one in File2.c to
File3.c
  swap (file1, file2, file3);

  EE->runFunction(func, std::vector<GenericValue>());

  EE->runStaticConstructorsDestructors(true);

  return 0;
}

I can get everything before the swap working (if I comment the rest, the
output is OK). I've tried to build the "swap" function many times but I
can't get it to work.
The expected output is:
File2.c
File3.c

If someone know how to do this or knows it's impossible, I would be very
thankful. I don't even know if this is the best way to do it.

Miranda
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100129/1973c42d/attachment.html>


More information about the llvm-dev mailing list