Skip to content

Commit

Permalink
Added Module.link() which links two LLVM Modules together. (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntherning committed Feb 9, 2015
1 parent 99cdb0c commit 6e26191
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 14 deletions.
4 changes: 2 additions & 2 deletions llvm/CMakeLists.txt
Expand Up @@ -93,9 +93,9 @@ endif()
add_definitions(-D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS)
include_directories(${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2} ${CMAKE_BINARY_DIR}/llvm/include)

# This was generated using 'llvm-config --libs core bitreader bitwriter irreader ipo arm x86 aarch64 debuginfo'
# This was generated using 'llvm-config --libs core bitreader bitwriter irreader ipo arm x86 aarch64 debuginfo linker'
set(LLVM_LIBS
-lLLVMDebugInfo -lLLVMAArch64Disassembler -lLLVMAArch64CodeGen
-lLLVMLinker -lLLVMDebugInfo -lLLVMAArch64Disassembler -lLLVMAArch64CodeGen
-lLLVMAArch64AsmParser -lLLVMAArch64Desc -lLLVMAArch64Info
-lLLVMAArch64AsmPrinter -lLLVMAArch64Utils -lLLVMX86Disassembler
-lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMX86Desc -lLLVMX86Info
Expand Down
25 changes: 17 additions & 8 deletions llvm/src/main/java/org/robovm/llvm/Module.java
Expand Up @@ -29,7 +29,7 @@
*
*/
public class Module implements AutoCloseable {
protected ModuleRef ref;
private ModuleRef ref;

private Module(ModuleRef moduleRef) {
this.ref = moduleRef;
Expand All @@ -40,10 +40,14 @@ protected final void checkDisposed() {
throw new LlvmException("Already disposed");
}
}
public synchronized void dispose() {

protected ModuleRef getRef() {
checkDisposed();
LLVM.DisposeModule(ref);
return ref;
}

public synchronized void dispose() {
LLVM.DisposeModule(getRef());
ref = null;
}

Expand All @@ -53,17 +57,22 @@ public void close() {
}

public Type getTypeByName(String name) {
checkDisposed();
return new Type(LLVM.GetTypeByName(ref, name));
return new Type(LLVM.GetTypeByName(getRef(), name));
}

public void writeBitcode(File file) {
checkDisposed();
if (LLVM.WriteBitcodeToFile(ref, file.getAbsolutePath()) != 0) {
if (LLVM.WriteBitcodeToFile(getRef(), file.getAbsolutePath()) != 0) {
throw new LlvmException("Write failed");
}
}

public void link(Module other) {
StringOut errorMessage = new StringOut();
if (LLVM.LinkModules(getRef(), other.getRef(), 0, errorMessage)) {
throw new LlvmException(errorMessage.getValue().trim());
}
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
3 changes: 2 additions & 1 deletion llvm/src/main/java/org/robovm/llvm/ObjectFile.java
Expand Up @@ -34,7 +34,7 @@
*/
public class ObjectFile implements AutoCloseable {
private final File file;
protected ObjectFileRef ref;
private ObjectFileRef ref;

private ObjectFile(File file, ObjectFileRef objectFileRef) {
this.file = file;
Expand All @@ -48,6 +48,7 @@ protected final void checkDisposed() {
}

protected ObjectFileRef getRef() {
checkDisposed();
return ref;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/src/main/java/org/robovm/llvm/PassManager.java
Expand Up @@ -240,7 +240,7 @@ public void addVerifierPass() {

public void run(Module module) {
checkDisposed();
LLVM.RunPassManager(ref, module.ref);
LLVM.RunPassManager(ref, module.getRef());
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions llvm/src/main/java/org/robovm/llvm/TargetMachine.java
Expand Up @@ -109,7 +109,7 @@ public void emit(Module module, OutputStream out, CodeGenFileType fileType) {
checkDisposed();
module.checkDisposed();
StringOut ErrorMessage = new StringOut();
if (LLVM.TargetMachineEmitToOutputStream(ref, module.ref, out, fileType, ErrorMessage)) {
if (LLVM.TargetMachineEmitToOutputStream(ref, module.getRef(), out, fileType, ErrorMessage)) {
// Returns true on failure!
throw new LlvmException(ErrorMessage.getValue().trim());
}
Expand All @@ -119,7 +119,7 @@ public void emit(Module module, File outFile, CodeGenFileType fileType) {
checkDisposed();
module.checkDisposed();
StringOut ErrorMessage = new StringOut();
if (LLVM.TargetMachineEmitToFile(ref, module.ref, outFile.getAbsolutePath(), fileType, ErrorMessage)) {
if (LLVM.TargetMachineEmitToFile(ref, module.getRef(), outFile.getAbsolutePath(), fileType, ErrorMessage)) {
// Returns true on failure!
throw new LlvmException(ErrorMessage.getValue().trim());
}
Expand Down
4 changes: 4 additions & 0 deletions llvm/src/main/java/org/robovm/llvm/binding/LLVM.java
Expand Up @@ -3095,6 +3095,10 @@ public static void AddAnalysisPasses(TargetMachineRef T, PassManagerRef PM) {
LLVMJNI.AddAnalysisPasses(TargetMachineRef.getCPtr(T), PassManagerRef.getCPtr(PM));
}

public static boolean LinkModules(ModuleRef Dest, ModuleRef Src, int Unused, StringOut OutMessage) {
return LLVMJNI.LinkModules(ModuleRef.getCPtr(Dest), ModuleRef.getCPtr(Src), Unused, StringOut.getCPtr(OutMessage), OutMessage);
}

public static String getLlvmHostTriple() {
return LLVMJNI.llvmHostTriple_get();
}
Expand Down
1 change: 1 addition & 0 deletions llvm/src/main/java/org/robovm/llvm/binding/LLVMJNI.java
Expand Up @@ -740,6 +740,7 @@ public class LLVMJNI {
public final static native boolean TargetMachineEmitToMemoryBuffer(long jarg1, long jarg2, int jarg3, long jarg4, StringOut jarg4_, long jarg5, MemoryBufferRefOut jarg5_);
public final static native String GetDefaultTargetTriple();
public final static native void AddAnalysisPasses(long jarg1, long jarg2);
public final static native boolean LinkModules(long jarg1, long jarg2, int jarg3, long jarg4, StringOut jarg4_);
public final static native String llvmHostTriple_get();
public final static native void PassManagerBuilderSetDisableTailCalls(long jarg1, boolean jarg2);
public final static native void PassManagerBuilderUseAlwaysInliner(long jarg1, boolean jarg2);
Expand Down
22 changes: 22 additions & 0 deletions llvm/src/main/native/LLVM_wrap.c
Expand Up @@ -198,6 +198,7 @@ static void SWIGUNUSED SWIG_JavaThrowException(JNIEnv *jenv, SWIG_JavaExceptionC
#include <llvm-c/Transforms/Vectorize.h>
#include <llvm-c/Target.h>
#include <llvm-c/TargetMachine.h>
#include <llvm-c/Linker.h>
#include "../native/LLVMExtra.h"

struct LongArray;
Expand Down Expand Up @@ -11775,6 +11776,27 @@ SWIGEXPORT void JNICALL Java_org_robovm_llvm_binding_LLVMJNI_AddAnalysisPasses(J
}


SWIGEXPORT jboolean JNICALL Java_org_robovm_llvm_binding_LLVMJNI_LinkModules(JNIEnv *jenv, jclass jcls, jlong jarg1, jlong jarg2, jint jarg3, jlong jarg4, jobject jarg4_) {
jboolean jresult = 0 ;
LLVMModuleRef arg1 = (LLVMModuleRef) 0 ;
LLVMModuleRef arg2 = (LLVMModuleRef) 0 ;
unsigned int arg3 ;
char **arg4 = (char **) 0 ;
LLVMBool result;

(void)jenv;
(void)jcls;
(void)jarg4_;
arg1 = *(LLVMModuleRef *)&jarg1;
arg2 = *(LLVMModuleRef *)&jarg2;
arg3 = (unsigned int)jarg3;
arg4 = *(char ***)&jarg4;
result = LLVMLinkModules(arg1,arg2,arg3,arg4);
jresult = result;
return jresult;
}


SWIGEXPORT jstring JNICALL Java_org_robovm_llvm_binding_LLVMJNI_llvmHostTriple_1get(JNIEnv *jenv, jclass jcls) {
jstring jresult = 0 ;
char *result = 0 ;
Expand Down
2 changes: 2 additions & 0 deletions llvm/src/main/swig/LLVM.i
Expand Up @@ -11,6 +11,7 @@
#include <llvm-c/Transforms/Vectorize.h>
#include <llvm-c/Target.h>
#include <llvm-c/TargetMachine.h>
#include <llvm-c/Linker.h>
#include "../native/LLVMExtra.h"

struct LongArray;
Expand Down Expand Up @@ -257,6 +258,7 @@ typedef jlong uint64_t;
%include "llvm-c/Transforms/Vectorize.h"
%include "llvm-c/Target.h"
%include "llvm-c/TargetMachine.h"
%include "llvm-c/Linker.h"
%include "../native/LLVMExtra.h"

%pragma(java) jniclasscode=%{
Expand Down

0 comments on commit 6e26191

Please sign in to comment.