[llvm-commits] LLVM-C Addition: LLVMCombineModules

F van der Meeren llvm-dev at code2develop.com
Thu Sep 16 10:22:22 PDT 2010


- author: Filip van der Meeren


Index: include/llvm-c/Core.h
===================================================================
--- include/llvm-c/Core.h	(revision 114074)
+++ include/llvm-c/Core.h	(working copy)
@@ -303,6 +303,20 @@
 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
                                                 LLVMContextRef C);
 
+/**
+ * Combines all the given modules into one.
+ * The list of modules must end with a NULL pointer.
+ * The returned module must be disposed by the user by using LLVMDisposeModule.
+ *
+ * If an error occurs, the message will be returned through the errMsg
+ * which also must be disposed by the user by using LLVMDisposeMessage.
+ */
+LLVMModuleRef
+LLVMCombineModules(char const * const newModuleName,
+                   char ** const errMsg,
+                   LLVMModuleRef aFirstModule,
+                   ...);
+
 /** See llvm::Module::~Module. */
 void LLVMDisposeModule(LLVMModuleRef M);
 
Index: lib/VMCore/Core.cpp
===================================================================
--- lib/VMCore/Core.cpp	(revision 114074)
+++ lib/VMCore/Core.cpp	(working copy)
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm-c/Core.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
@@ -22,6 +23,7 @@
 #include "llvm/TypeSymbolTable.h"
 #include "llvm/InlineAsm.h"
 #include "llvm/IntrinsicInst.h"
+#include "llvm/Linker.h"
 #include "llvm/PassManager.h"
 #include "llvm/Support/CallSite.h"
 #include "llvm/Support/Debug.h"
@@ -30,7 +32,10 @@
 #include "llvm/Support/raw_ostream.h"
 #include <cassert>
 #include <cstdlib>
+#include <cstdarg>
 #include <cstring>
+#include <string>
+#include <memory>
 
 using namespace llvm;
 
@@ -77,6 +82,40 @@
   return wrap(new Module(ModuleID, *unwrap(C)));
 }
 
+LLVMModuleRef
+LLVMCombineModules(char const * const newModuleName,
+                   char ** const errMsg,
+                   LLVMModuleRef aFirstModule,
+                   ...) {
+  va_list argptr;
+  std::string error;
+  Module * result = NULL;
+  Module * const firstModule = unwrap(aFirstModule);
+  Module * toLink = firstModule;
+  std::auto_ptr<Linker> linker(new Linker(StringRef(""),
+                                          StringRef(newModuleName),
+                                          firstModule->getContext(),
+                                          0));
+  
+  va_start(argptr, aFirstModule);
+  while(toLink != NULL) {
+    
+    error.clear();
+    if(linker->LinkInModule(toLink, &error) || !error.empty()) {
+      if(errMsg)
+        (*errMsg) = strdup(error.c_str());
+      break;
+    }
+    
+    toLink = unwrap(va_arg(argptr, LLVMModuleRef));
+  }
+  va_end(argptr);
+  
+  result = linker->getModule();
+  linker->releaseModule();
+  return wrap(result);
+}
+
 void LLVMDisposeModule(LLVMModuleRef M) {
   delete unwrap(M);
 }



-------------- next part --------------
A non-text attachment was scrubbed...
Name: LLVMCombineModules.diff
Type: application/octet-stream
Size: 2977 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20100916/3fcc0251/attachment.obj>


More information about the llvm-commits mailing list