Guest User

Conrado Miranda

a guest
Jan 30th, 2010
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include "llvm/LLVMContext.h"
  2. #include "llvm/Module.h"
  3. #include "llvm/ModuleProvider.h"
  4. #include "llvm/Type.h"
  5. #include "llvm/Bitcode/ReaderWriter.h"
  6. #include "llvm/CodeGen/LinkAllCodegenComponents.h"
  7. #include "llvm/ExecutionEngine/GenericValue.h"
  8. #include "llvm/ExecutionEngine/Interpreter.h"
  9. #include "llvm/ExecutionEngine/JIT.h"
  10. #include "llvm/ExecutionEngine/JITEventListener.h"
  11. #include "llvm/Support/CommandLine.h"
  12. #include "llvm/Support/ManagedStatic.h"
  13. #include "llvm/Support/MemoryBuffer.h"
  14. #include "llvm/Support/PluginLoader.h"
  15. #include "llvm/Support/PrettyStackTrace.h"
  16. #include "llvm/System/Process.h"
  17. #include "llvm/System/Signals.h"
  18. #include "llvm/Target/TargetSelect.h"
  19. #include "llvm/Linker.h"
  20. #include "llvm/Analysis/Verifier.h"
  21. #include "llvm/Bitcode/ReaderWriter.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/ManagedStatic.h"
  24. #include "llvm/Support/MemoryBuffer.h"
  25. #include "llvm/Support/PrettyStackTrace.h"
  26. #include "llvm/System/Signals.h"
  27. #include "llvm/System/Path.h"
  28.  
  29. #include <stdio.h>
  30. #include <string>
  31.  
  32. using namespace llvm;
  33. using namespace std;
  34.  
  35. static inline Module* LoadFile(const std::string &FN,
  36.     LLVMContext& Context) {
  37.   Module* Result = 0;
  38.   string ErrorMessage;
  39.  
  40.   if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(FN,
  41.         &ErrorMessage)) {
  42.     Result = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
  43.     delete Buffer;
  44.   }
  45.   if (Result) return Result;   // Load successful!
  46.  
  47.   else {                                                                                                                                                                                                                                                                                                                                                                                                                                  
  48.     errs() << "Bitcode file: '" << FN.c_str() << "' does not exist.\n";
  49.   }
  50.  
  51.   return NULL;
  52. }
  53.  
  54. int main(int argc, char **argv, char * const *envp) {
  55.   InitializeNativeTarget();
  56.  
  57.   LLVMContext &Context = getGlobalContext();
  58.  
  59.   Module *print1(LoadFile("print1.o", Context));
  60.   Module *print2(LoadFile("print2.o", Context));
  61.   Module *main_file(LoadFile("main_file.o", Context));
  62.  
  63.   Function *func, *print1f, *print2f;
  64.   print2f = print2->getFunction ("print");
  65.   print2f->setName ("test_name");
  66.  
  67.   Linker::LinkModules(main_file, print1, NULL);
  68.   Linker::LinkModules(main_file, print2, NULL);
  69.  
  70.   ExecutionEngine *EE = EngineBuilder(main_file).create();
  71.  
  72.   EE->runStaticConstructorsDestructors(false);
  73.  
  74.   func = EE->FindFunctionNamed ("do_print");
  75.   EE->runFunction(func, std::vector<GenericValue> ());
  76.  
  77.   print1f = main_file->getFunction ("print");
  78.   print2f = main_file->getFunction ("test_name");
  79.   print1f->replaceAllUsesWith (print2f);
  80.   EE->runFunction(func, std::vector<GenericValue> ());
  81.  
  82.   EE->runStaticConstructorsDestructors(true);
  83. }
Add Comment
Please, Sign In to add comment