",
13 | "llvm-java.h"
14 | }
15 | )
16 | public final class BitReader extends LLVMLoader {
17 |
18 | /**
19 | * Builds a module from the bitcode in the specified memory buffer, returning a
20 | * reference to the module via the OutModule parameter. Returns 0 on success.
21 | * Optionally returns a human-readable error message via OutMessage.
22 | *
23 | *
24 | * @deprecated Use {@link #LLVMParseBitcode2(long, long)}
25 | */
26 | @Deprecated
27 | public static native boolean LLVMParseBitcode(
28 | @LLVMMemoryBufferRef long MemBuf,
29 | @Pointer("LLVMModuleRef *") long OutModule,
30 | @Pointer("char **") long OutMessage
31 | );
32 |
33 | /**
34 | * Builds a module from the bitcode in the specified memory buffer, returning a
35 | * reference to the module via the OutModule parameter. Returns 0 on success.
36 | */
37 | public static native boolean LLVMParseBitcode2(
38 | @LLVMMemoryBufferRef long MemBuf,
39 | @Pointer("LLVMModuleRef *") long OutModule
40 | );
41 |
42 | /**
43 | * @deprecated Use {@link #LLVMParseBitcodeInContext2(long, long, long)}
44 | */
45 | @Deprecated
46 | public static native boolean LLVMParseBitcodeInContext(
47 | @LLVMContextRef long ContextRef,
48 | @LLVMMemoryBufferRef long MemBuf,
49 | @Pointer("LLVMModuleRef *") long OutModule, @Pointer("char **") long OutMessage
50 | );
51 |
52 | public static native boolean LLVMParseBitcodeInContext2(
53 | @LLVMContextRef long ContextRef,
54 | @LLVMMemoryBufferRef long MemBuf,
55 | @Pointer("LLVMModuleRef *") long OutModule
56 | );
57 |
58 | /**
59 | * Reads a module from the specified path, returning via the OutMP parameter
60 | * a module provider which performs lazy deserialization. Returns 0 on success.
61 | * Optionally returns a human-readable error message via OutMessage.
62 | *
63 | * @deprecated Use {@link #LLVMGetBitcodeModuleInContext2(long, long, long)}
64 | */
65 | @Deprecated
66 | public static native boolean LLVMGetBitcodeModuleInContext(
67 | @LLVMContextRef long ContextRef,
68 | @LLVMMemoryBufferRef long MemBuf,
69 | @Pointer("LLVMModuleRef *") long OutM, @Pointer("char **") long OutMessage
70 | );
71 |
72 | /**
73 | * Reads a module from the specified path, returning via the OutMP parameter a
74 | * module provider which performs lazy deserialization. Returns 0 on success.
75 | */
76 | public static native boolean LLVMGetBitcodeModuleInContext2(
77 | @LLVMContextRef long ContextRef,
78 | @LLVMMemoryBufferRef long MemBuf,
79 | @Pointer("LLVMModuleRef *") long OutM
80 | );
81 |
82 | /**
83 | * @deprecated Use {@link #LLVMGetBitcodeModule2(long, long)}
84 | */
85 | @Deprecated
86 | public static native boolean LLVMGetBitcodeModule(
87 | @LLVMMemoryBufferRef long MemBuf, @Pointer("LLVMModuleRef *") long OutM,
88 | @Pointer("char **") long OutMessage
89 | );
90 |
91 | public static native boolean LLVMGetBitcodeModule2(@LLVMMemoryBufferRef long MemBuf, @Pointer("LLVMModuleRef *") long OutM);
92 | }
93 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/BitWriter.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.CInfo;
4 | import asia.kala.llvm.binding.annotations.Pointer;
5 | import asia.kala.llvm.binding.annotations.Signed;
6 |
7 | import static asia.kala.llvm.binding.Types.*;
8 |
9 | @CInfo(
10 | fileName = "BitWriter.c",
11 | include = {
12 | "",
13 | "llvm-java.h"
14 | }
15 | )
16 | public final class BitWriter extends LLVMLoader {
17 | private BitWriter() {
18 | }
19 |
20 | /**
21 | * Writes a module to the specified path. Returns 0 on success.
22 | */
23 | public static native int LLVMWriteBitcodeToFile(@LLVMModuleRef long M, @Pointer("const char *") long Path);
24 |
25 | /**
26 | * Writes a module to an open file descriptor. Returns 0 on success.
27 | */
28 | public static native int LLVMWriteBitcodeToFD(
29 | @LLVMModuleRef long M, @Signed("int") int FD, @Signed("int") int ShouldClose, @Signed("int") int Unbuffered
30 | );
31 |
32 | /**
33 | * Writes a module to an open file descriptor. Returns 0 on success. Closes the Handle.
34 | *
35 | * @deprecated Ues LLVMWriteBitcodeToFD.
36 | */
37 | @Deprecated
38 | public static native int LLVMWriteBitcodeToFileHandle(@LLVMModuleRef long M, @Signed("int") int Handle);
39 |
40 | /**
41 | * Writes a module to a new memory buffer and returns it.
42 | */
43 | public static native @LLVMMemoryBufferRef long LLVMWriteBitcodeToMemoryBuffer(@LLVMModuleRef long M);
44 | }
45 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/Comdat.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.CEnum;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 | import asia.kala.llvm.binding.annotations.Pointer;
6 |
7 | import static asia.kala.llvm.binding.Types.*;
8 |
9 | @CInfo(
10 | fileName = "Comdat.c",
11 | include = {
12 | "",
13 | "llvm-java.h"
14 | }
15 | )
16 | public final class Comdat extends LLVMLoader {
17 | private Comdat() {
18 |
19 | }
20 |
21 | @CEnum("LLVMComdatSelectionKind")
22 | public @interface LLVMComdatSelectionKind {
23 | /**
24 | * The linker may choose any COMDAT.
25 | */
26 | int LLVMAnyComdatSelectionKind = 0;
27 | /**
28 | * The data referenced by the COMDAT must be the same.
29 | */
30 | int LLVMExactMatchComdatSelectionKind = 1;
31 | /**
32 | * The linker will choose the largest COMDAT.
33 | */
34 | int LLVMLargestComdatSelectionKind = 2;
35 | /**
36 | * No other Module may specify this COMDAT.
37 | */
38 | int LLVMNoDuplicatesComdatSelectionKind = 3;
39 | /**
40 | * The data referenced by the COMDAT must be the same size.
41 | */
42 | int LLVMSameSizeComdatSelectionKind = 4;
43 |
44 | }
45 |
46 | /**
47 | * Return the Comdat in the module with the specified name. It is created
48 | * if it didn't already exist.
49 | */
50 | public static native @LLVMComdatRef long LLVMGetOrInsertComdat(@LLVMModuleRef long M, @Pointer("const char *") long Name);
51 |
52 | /**
53 | * Get the Comdat assigned to the given global object.
54 | */
55 | public static native @LLVMComdatRef long LLVMGetComdat(@LLVMValueRef long V);
56 |
57 | /**
58 | * Assign the Comdat to the given global object.
59 | */
60 | public static native void LLVMSetComdat(@LLVMValueRef long V, @LLVMComdatRef long C);
61 |
62 | /**
63 | * Get the conflict resolution selection kind for the Comdat.
64 | */
65 | public static native @LLVMComdatSelectionKind int LLVMGetComdatSelectionKind(@LLVMComdatRef long C);
66 |
67 | /**
68 | * Set the conflict resolution selection kind for the Comdat.
69 | */
70 | public static native void LLVMSetComdatSelectionKind(@LLVMComdatRef long C, @LLVMComdatSelectionKind int Kind);
71 |
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/Disassembler.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.*;
4 |
5 | import static asia.kala.llvm.binding.DisassemblerTypes.*;
6 |
7 | @CInfo(
8 | fileName = "Disassembler.c",
9 | include = {
10 | "",
11 | "llvm-java.h"
12 | }
13 | )
14 | public final class Disassembler extends LLVMLoader {
15 | private Disassembler() {
16 | }
17 |
18 | /**
19 | * Create a disassembler for the TripleName. Symbolic disassembly is supported
20 | * by passing a block of information in the DisInfo parameter and specifying the
21 | * TagType and callback functions as described above. These can all be passed
22 | * as NULL. If successful, this returns a disassembler context. If not, it
23 | * returns NULL. This function is equivalent to calling
24 | * LLVMCreateDisasmCPUFeatures() with an empty CPU name and feature set.
25 | */
26 | public static native @LLVMDisasmContextRef long LLVMCreateDisasm(
27 | @Pointer("const char *") long TripleName, @Pointer("void *") long DisInfo,
28 | @Signed("int") int TagType, @LLVMOpInfoCallback long GetOpInfo,
29 | @LLVMSymbolLookupCallback long SymbolLookUp
30 | );
31 |
32 | /**
33 | * Create a disassembler for the TripleName and a specific CPU. Symbolic
34 | * disassembly is supported by passing a block of information in the DisInfo
35 | * parameter and specifying the TagType and callback functions as described
36 | * above. These can all be passed * as NULL. If successful, this returns a
37 | * disassembler context. If not, it returns NULL. This function is equivalent
38 | * to calling LLVMCreateDisasmCPUFeatures() with an empty feature set.
39 | */
40 | public static native @LLVMDisasmContextRef long LLVMCreateDisasmCPU(
41 | @Pointer("const char *") long Triple, @Pointer("const char *") long CPU,
42 | @Pointer("void *") long DisInfo, @Signed("int") int TagType,
43 | @LLVMOpInfoCallback long GetOpInfo,
44 | @LLVMSymbolLookupCallback long SymbolLookUp
45 | );
46 |
47 | /**
48 | * Create a disassembler for the TripleName, a specific CPU and specific feature
49 | * string. Symbolic disassembly is supported by passing a block of information
50 | * in the DisInfo parameter and specifying the TagType and callback functions as
51 | * described above. These can all be passed * as NULL. If successful, this
52 | * returns a disassembler context. If not, it returns NULL.
53 | */
54 | public static native @LLVMDisasmContextRef long LLVMCreateDisasmCPUFeatures(
55 | @Pointer("const char *") long Triple, @Pointer("const char *") long CPU,
56 | @Pointer("const char *") long Features, @Pointer("void *") long DisInfo, @Signed("int") int TagType,
57 | @LLVMOpInfoCallback long GetOpInfo,
58 | @LLVMSymbolLookupCallback long SymbolLookUp
59 | );
60 |
61 | /**
62 | * Set the disassembler's options. Returns 1 if it can set the Options and 0
63 | * otherwise.
64 | */
65 | public static native int LLVMSetDisasmOptions(@LLVMDisasmContextRef long DC, @Unsigned("uint64_t") long Options);
66 |
67 | /**
68 | * The option to produce marked up assembly.
69 | */
70 | public static final int LLVMDisassembler_Option_UseMarkup = 1;
71 | /**
72 | * The option to print immediates as hex.
73 | */
74 | public static final int LLVMDisassembler_Option_PrintImmHex = 2;
75 | /**
76 | * The option use the other assembler printer variant
77 | */
78 | public static final int LLVMDisassembler_Option_AsmPrinterVariant = 4;
79 | /**
80 | * The option to set comment on instructions
81 | */
82 | public static final int LLVMDisassembler_Option_SetInstrComments = 8;
83 | /**
84 | * The option to print latency information alongside instructions
85 | */
86 | public static final int LLVMDisassembler_Option_PrintLatency = 16;
87 |
88 | /**
89 | * Dispose of a disassembler context.
90 | */
91 | public static native void LLVMDisasmDispose(@LLVMDisasmContextRef long DC);
92 |
93 | /**
94 | * Disassemble a single instruction using the disassembler context specified in
95 | * the parameter DC. The bytes of the instruction are specified in the
96 | * parameter Bytes, and contains at least BytesSize number of bytes. The
97 | * instruction is at the address specified by the PC parameter. If a valid
98 | * instruction can be disassembled, its string is returned indirectly in
99 | * OutString whose size is specified in the parameter OutStringSize. This
100 | * function returns the number of bytes in the instruction or zero if there was
101 | * no valid instruction.
102 | */
103 | public static native @SizeT long LLVMDisasmInstruction(
104 | @LLVMDisasmContextRef long DC, @Pointer("uint8_t *") long Bytes,
105 | @Unsigned("uint64_t") long BytesSize, @Unsigned("uint64_t") long PC,
106 | @Pointer("char *") long OutString, @SizeT long OutStringSize
107 | );
108 |
109 | }
110 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/DisassemblerTypes.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.Pointer;
4 |
5 | import java.lang.annotation.*;
6 |
7 | public final class DisassemblerTypes extends LLVMLoader {
8 | private DisassemblerTypes() {
9 | }
10 |
11 | @Pointer("LLVMDisasmContextRef")
12 | @Documented
13 | @java.lang.annotation.Target(ElementType.TYPE_USE)
14 | @Retention(RetentionPolicy.RUNTIME)
15 | public @interface LLVMDisasmContextRef {
16 | }
17 |
18 | @Pointer("LLVMOpInfoCallback")
19 | @Documented
20 | @java.lang.annotation.Target(ElementType.TYPE_USE)
21 | @Retention(RetentionPolicy.RUNTIME)
22 | public @interface LLVMOpInfoCallback {
23 | }
24 |
25 | /*
26 | struct LLVMOpInfoSymbol1 {
27 | uint64_t Present;
28 | const char *Name;
29 | uint64_t Value;
30 | };
31 |
32 | struct LLVMOpInfo1 {
33 | struct LLVMOpInfoSymbol1 AddSymbol;
34 | struct LLVMOpInfoSymbol1 SubtractSymbol;
35 | uint64_t Value;
36 | uint64_t VariantKind;
37 | };
38 | */
39 |
40 | /*
41 | * The operand VariantKinds for symbolic disassembly.
42 | */
43 |
44 | public static final int LLVMDisassembler_VariantKind_None = 0; /* all targets */
45 |
46 | /*
47 | * The ARM target VariantKinds.
48 | */
49 | public static final int LLVMDisassembler_VariantKind_ARM_HI16 = 1; /* :upper16: */
50 | public static final int LLVMDisassembler_VariantKind_ARM_LO16 = 2; /* :lower16: */
51 |
52 | /*
53 | * The ARM64 target VariantKinds.
54 | */
55 |
56 | public static final int LLVMDisassembler_VariantKind_ARM64_PAGE = 1; /* @page */
57 | public static final int LLVMDisassembler_VariantKind_ARM64_PAGEOFF = 2; /* @pageoff */
58 | public static final int LLVMDisassembler_VariantKind_ARM64_GOTPAGE = 3; /* @gotpage */
59 | public static final int LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF = 4; /* @gotpageoff */
60 | public static final int LLVMDisassembler_VariantKind_ARM64_TLVP = 5; /* @tvlppage */
61 | public static final int LLVMDisassembler_VariantKind_ARM64_TLVOFF = 6; /* @tvlppageoff */
62 |
63 | @Pointer("LLVMSymbolLookupCallback")
64 | @Documented
65 | @java.lang.annotation.Target(ElementType.TYPE_USE)
66 | @Retention(RetentionPolicy.RUNTIME)
67 | public @interface LLVMSymbolLookupCallback {
68 | }
69 |
70 | /*
71 | * The reference types on input and output.
72 | */
73 |
74 | /**
75 | * No input reference type or no output reference type.
76 | */
77 | public static final int LLVMDisassembler_ReferenceType_InOut_None = 0;
78 |
79 | /**
80 | * The input reference is from a branch instruction.
81 | */
82 | public static final int LLVMDisassembler_ReferenceType_In_Branch = 1;
83 | /**
84 | * The input reference is from a PC relative load instruction.
85 | */
86 | public static final int LLVMDisassembler_ReferenceType_In_PCrel_Load = 2;
87 |
88 | /**
89 | * The input reference is from an ARM64::ADRP instruction.
90 | */
91 | public static final long LLVMDisassembler_ReferenceType_In_ARM64_ADRP = 0x100000001L;
92 | /**
93 | * The input reference is from an ARM64::ADDXri instruction.
94 | */
95 | public static final long LLVMDisassembler_ReferenceType_In_ARM64_ADDXri = 0x100000002L;
96 | /**
97 | * The input reference is from an ARM64::LDRXui instruction.
98 | */
99 | public static final long LLVMDisassembler_ReferenceType_In_ARM64_LDRXui = 0x100000003L;
100 | /**
101 | * The input reference is from an ARM64::LDRXl instruction.
102 | */
103 | public static final long LLVMDisassembler_ReferenceType_In_ARM64_LDRXl = 0x100000004L;
104 | /**
105 | * The input reference is from an ARM64::ADR instruction.
106 | */
107 | public static final long LLVMDisassembler_ReferenceType_In_ARM64_ADR = 0x100000005L;
108 |
109 | /**
110 | * The output reference is to as symbol stub.
111 | */
112 | public static final int LLVMDisassembler_ReferenceType_Out_SymbolStub = 1;
113 | /**
114 | * The output reference is to a symbol address in a literal pool.
115 | */
116 | public static final int LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr = 2;
117 | /**
118 | * The output reference is to a cstring address in a literal pool.
119 | */
120 | public static final int LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr = 3;
121 |
122 | /**
123 | * The output reference is to a Objective-C CoreFoundation string.
124 | */
125 | public static final int LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref = 4;
126 | /**
127 | * The output reference is to a Objective-C message.
128 | */
129 | public static final int LLVMDisassembler_ReferenceType_Out_Objc_Message = 5;
130 | /**
131 | * The output reference is to a Objective-C message ref.
132 | */
133 | public static final int LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref = 6;
134 | /**
135 | * The output reference is to a Objective-C selector ref.
136 | */
137 | public static final int LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref = 7;
138 | /**
139 | * The output reference is to a Objective-C class ref.
140 | */
141 | public static final int LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref = 8;
142 |
143 | /**
144 | * The output reference is to a C++ symbol name.
145 | */
146 | public static final int LLVMDisassembler_ReferenceType_DeMangled_Name = 9;
147 | }
148 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/ErrorH.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.CInfo;
4 | import asia.kala.llvm.binding.annotations.Pointer;
5 |
6 | import java.lang.annotation.*;
7 |
8 | @CInfo(
9 | fileName = "Error.c",
10 | include = {
11 | "",
12 | "llvm-java.h"
13 | }
14 | )
15 | public final class ErrorH extends LLVMLoader {
16 | private ErrorH() {
17 | }
18 |
19 | public static final int LLVMErrorSuccess = 0;
20 |
21 |
22 | @Pointer("LLVMErrorRef")
23 | @Documented
24 | @java.lang.annotation.Target(ElementType.TYPE_USE)
25 | @Retention(RetentionPolicy.RUNTIME)
26 | public @interface LLVMErrorRef {
27 | }
28 |
29 | @Pointer("LLVMErrorTypeId")
30 | @Documented
31 | @java.lang.annotation.Target(ElementType.TYPE_USE)
32 | @Retention(RetentionPolicy.RUNTIME)
33 | public @interface LLVMErrorTypeId {
34 | }
35 |
36 | /**
37 | * Returns the type id for the given error instance, which must be a failure
38 | * value (i.e. non-null).
39 | */
40 | public static native @LLVMErrorTypeId long LLVMGetErrorTypeId(@LLVMErrorRef long Err);
41 |
42 | /**
43 | * Dispose of the given error without handling it. This operation consumes the
44 | * error, and the given LLVMErrorRef value is not usable once this call returns.
45 | * Note: This method *only* needs to be called if the error is not being passed
46 | * to some other consuming operation, e.g. LLVMGetErrorMessage.
47 | */
48 | public static native void LLVMConsumeError(@LLVMErrorRef long Err);
49 |
50 | /**
51 | * Returns the given string's error message. This operation consumes the error,
52 | * and the given LLVMErrorRef value is not usable once this call returns.
53 | * The caller is responsible for disposing of the string by calling
54 | * LLVMDisposeErrorMessage.
55 | */
56 | public static native @Pointer("char *") long LLVMGetErrorMessage(@LLVMErrorRef long Err);
57 |
58 | /**
59 | * Dispose of the given error message.
60 | */
61 | public static native void LLVMDisposeErrorMessage(@Pointer("char *") long ErrMsg);
62 |
63 | /**
64 | * Returns the type id for llvm StringError.
65 | */
66 | public static native @LLVMErrorTypeId long LLVMGetStringErrorTypeId();
67 | }
68 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/ErrorHandling.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.CInfo;
4 | import asia.kala.llvm.binding.annotations.Pointer;
5 |
6 | import java.lang.annotation.*;
7 |
8 | @CInfo(
9 | fileName = "ErrorHandling.c",
10 | include = {
11 | "",
12 | "llvm-java.h"
13 | }
14 | )
15 | public final class ErrorHandling extends LLVMLoader {
16 | private ErrorHandling() {
17 | }
18 |
19 | @Pointer("LLVMFatalErrorHandler")
20 | @Documented
21 | @java.lang.annotation.Target(ElementType.TYPE_USE)
22 | @Retention(RetentionPolicy.RUNTIME)
23 | public @interface LLVMFatalErrorHandler {
24 | }
25 |
26 | /**
27 | * Install a fatal error handler. By default, if LLVM detects a fatal error, it
28 | * will call exit(1). This may not be appropriate in many contexts. For example,
29 | * doing exit(1) will bypass many crash reporting/tracing system tools. This
30 | * function allows you to install a callback that will be invoked prior to the
31 | * call to exit(1).
32 | */
33 | public static native void LLVMInstallFatalErrorHandler(@LLVMFatalErrorHandler long Handler);
34 |
35 | /**
36 | * Reset the fatal error handler. This resets LLVM's fatal error handling
37 | * behavior to the default.
38 | */
39 | public static native void LLVMResetFatalErrorHandler();
40 |
41 | /**
42 | * Enable LLVM's built-in stack trace code. This intercepts the OS's crash
43 | * signals and prints which component of LLVM you were in at the time if the
44 | * crash.
45 | */
46 | public static native void LLVMEnablePrettyStackTrace();
47 | }
48 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/ExecutionEngine.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.*;
4 |
5 | import java.lang.annotation.*;
6 |
7 | import static asia.kala.llvm.binding.Types.*;
8 | import static asia.kala.llvm.binding.TargetH.*;
9 | import static asia.kala.llvm.binding.TargetMachine.*;
10 |
11 | @CInfo(
12 | fileName = "ExecutionEngine.c",
13 | include = {
14 | "",
15 | "llvm-java.h"
16 | }
17 | )
18 | public final class ExecutionEngine extends LLVMLoader {
19 | private ExecutionEngine() {
20 | }
21 |
22 | public static native void LLVMLinkInMCJIT();
23 |
24 | public static native void LLVMLinkInInterpreter();
25 |
26 | @Pointer("LLVMGenericValueRef")
27 | @Documented
28 | @java.lang.annotation.Target(ElementType.TYPE_USE)
29 | @Retention(RetentionPolicy.RUNTIME)
30 | public @interface LLVMGenericValueRef {
31 | }
32 |
33 | @Pointer("LLVMExecutionEngineRef")
34 | @Documented
35 | @java.lang.annotation.Target(ElementType.TYPE_USE)
36 | @Retention(RetentionPolicy.RUNTIME)
37 | public @interface LLVMExecutionEngineRef {
38 | }
39 |
40 | @Pointer("LLVMMCJITMemoryManagerRef")
41 | @Documented
42 | @java.lang.annotation.Target(ElementType.TYPE_USE)
43 | @Retention(RetentionPolicy.RUNTIME)
44 | public @interface LLVMMCJITMemoryManagerRef {
45 | }
46 |
47 | @Pointer("struct LLVMMCJITCompilerOptions *")
48 | @Documented
49 | @java.lang.annotation.Target(ElementType.TYPE_USE)
50 | @Retention(RetentionPolicy.RUNTIME)
51 | public @interface LLVMMCJITCompilerOptionsRef {
52 | }
53 |
54 | /*===-- Operations on generic values --------------------------------------===*/
55 |
56 | public static native @LLVMGenericValueRef long LLVMCreateGenericValueOfInt(
57 | @LLVMTypeRef long Ty,
58 | @Unsigned("unsigned long long") long N,
59 | boolean IsSigned
60 | );
61 |
62 | public static native @LLVMGenericValueRef long LLVMCreateGenericValueOfPointer(@Pointer("void *") long P);
63 |
64 | public static native @LLVMGenericValueRef long LLVMCreateGenericValueOfFloat(@LLVMTypeRef long Ty, double N);
65 |
66 | public static native @Unsigned int LLVMGenericValueIntWidth(@LLVMGenericValueRef long GenValRef);
67 |
68 | public static native @Unsigned("unsigned long long") long LLVMGenericValueToInt(
69 | @LLVMGenericValueRef long GenVal,
70 | boolean IsSigned
71 | );
72 |
73 | public static native @Pointer("void *") long LLVMGenericValueToPointer(@LLVMGenericValueRef long GenVal);
74 |
75 | public static native double LLVMGenericValueToFloat(@LLVMTypeRef long TyRef, @LLVMGenericValueRef long GenVal);
76 |
77 | public static native void LLVMDisposeGenericValue(@LLVMGenericValueRef long GenVal);
78 |
79 | /*===-- Operations on execution engines -----------------------------------===*/
80 |
81 | public static native boolean LLVMCreateExecutionEngineForModule(
82 | @Pointer("LLVMExecutionEngineRef *") long OutEE,
83 | @LLVMModuleRef long M,
84 | @Pointer("char **") long OutError
85 | );
86 |
87 | public static native boolean LLVMCreateInterpreterForModule(
88 | @Pointer("LLVMExecutionEngineRef *") long OutInterp,
89 | @LLVMModuleRef long M,
90 | @Pointer("char **") long OutError
91 | );
92 |
93 | public static native boolean LLVMCreateJITCompilerForModule(
94 | @Pointer("LLVMExecutionEngineRef *") long OutJIT,
95 | @LLVMModuleRef long M,
96 | @Unsigned int OptLevel,
97 | @Pointer("char **") long OutError
98 | );
99 |
100 | public static native void LLVMInitializeMCJITCompilerOptions(
101 | @LLVMMCJITCompilerOptionsRef long Options, @SizeT long SizeOfOptions
102 | );
103 |
104 | /**
105 | * Create an MCJIT execution engine for a module, with the given options. It is
106 | * the responsibility of the caller to ensure that all fields in Options up to
107 | * the given SizeOfOptions are initialized. It is correct to pass a smaller
108 | * value of SizeOfOptions that omits some fields. The canonical way of using
109 | * this is:
110 | *
111 | * LLVMMCJITCompilerOptions options;
112 | * LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
113 | * ... fill in those options you care about
114 | * LLVMCreateMCJITCompilerForModule(&jit, mod, &options, sizeof(options),
115 | * &error);
116 | *
117 | * Note that this is also correct, though possibly suboptimal:
118 | *
119 | * LLVMCreateMCJITCompilerForModule(&jit, mod, 0, 0, &error);
120 | */
121 | public static native boolean LLVMCreateMCJITCompilerForModule(
122 | @Pointer("LLVMExecutionEngineRef *") long OutJIT, @LLVMModuleRef long M,
123 | @LLVMMCJITCompilerOptionsRef long Options, @SizeT long SizeOfOptions,
124 | @Pointer("char **") long OutError);
125 |
126 | public static native void LLVMDisposeExecutionEngine(@LLVMExecutionEngineRef long EE);
127 |
128 | public static native void LLVMRunStaticConstructors(@LLVMExecutionEngineRef long EE);
129 |
130 | public static native void LLVMRunStaticDestructors(@LLVMExecutionEngineRef long EE);
131 |
132 | public static native int LLVMRunFunctionAsMain(
133 | @LLVMExecutionEngineRef long EE, @LLVMValueRef long F,
134 | @Unsigned int ArgC, @Pointer("const char * const *") long ArgV,
135 | @Pointer("const char * const *") long EnvP
136 | );
137 |
138 | public static native @LLVMGenericValueRef long LLVMRunFunction(
139 | @LLVMExecutionEngineRef long EE, @LLVMValueRef long F,
140 | @Unsigned int NumArgs,
141 | @Pointer("LLVMGenericValueRef *") long Args
142 | );
143 |
144 | public static native void LLVMFreeMachineCodeForFunction(@LLVMExecutionEngineRef long EE, @LLVMValueRef long F);
145 |
146 | public static native void LLVMAddModule(@LLVMExecutionEngineRef long EE, @LLVMModuleRef long M);
147 |
148 | public static native boolean LLVMRemoveModule(
149 | @LLVMExecutionEngineRef long EE, @LLVMModuleRef long M,
150 | @Pointer("LLVMModuleRef *") long OutMod, @Pointer("char **") long OutError
151 | );
152 |
153 | public static native boolean LLVMFindFunction(
154 | @LLVMExecutionEngineRef long EE, @Pointer("const char *") long Name,
155 | @Pointer("LLVMValueRef *") long OutFn
156 | );
157 |
158 | public static native @Pointer("void *") long LLVMRecompileAndRelinkFunction(
159 | @LLVMExecutionEngineRef long EE,
160 | @LLVMValueRef long Fn
161 | );
162 |
163 | public static native @LLVMTargetDataRef long LLVMGetExecutionEngineTargetData(@LLVMExecutionEngineRef long EE);
164 |
165 | public static native @LLVMTargetMachineRef long LLVMGetExecutionEngineTargetMachine(@LLVMExecutionEngineRef long EE);
166 |
167 | public static native void LLVMAddGlobalMapping(
168 | @LLVMExecutionEngineRef long EE, @LLVMValueRef long Global,
169 | @Pointer("void *") long Addr
170 | );
171 |
172 | public static native @Pointer("void *") long LLVMGetPointerToGlobal(
173 | @LLVMExecutionEngineRef long EE, @LLVMValueRef long Global
174 | );
175 |
176 | public static native @Pointer("uint64_t") long LLVMGetGlobalValueAddress(
177 | @LLVMExecutionEngineRef long EE, @Pointer("const char *") long Name
178 | );
179 |
180 | public static native @Pointer("uint64_t") long LLVMGetFunctionAddress(
181 | @LLVMExecutionEngineRef long EE, @Pointer("const char *") long Name
182 | );
183 |
184 | /*===-- Operations on memory managers -------------------------------------===*/
185 |
186 | @Pointer("LLVMMemoryManagerAllocateCodeSectionCallback")
187 | @Documented
188 | @java.lang.annotation.Target(ElementType.TYPE_USE)
189 | @Retention(RetentionPolicy.RUNTIME)
190 | public @interface LLVMMemoryManagerAllocateCodeSectionCallback {
191 | }
192 |
193 | @Pointer("LLVMMemoryManagerAllocateDataSectionCallback")
194 | @Documented
195 | @java.lang.annotation.Target(ElementType.TYPE_USE)
196 | @Retention(RetentionPolicy.RUNTIME)
197 | public @interface LLVMMemoryManagerAllocateDataSectionCallback {
198 | }
199 |
200 | @Pointer("LLVMMemoryManagerFinalizeMemoryCallback")
201 | @Documented
202 | @java.lang.annotation.Target(ElementType.TYPE_USE)
203 | @Retention(RetentionPolicy.RUNTIME)
204 | public @interface LLVMMemoryManagerFinalizeMemoryCallback {
205 | }
206 |
207 | @Pointer("LLVMMemoryManagerDestroyCallback")
208 | @Documented
209 | @java.lang.annotation.Target(ElementType.TYPE_USE)
210 | @Retention(RetentionPolicy.RUNTIME)
211 | public @interface LLVMMemoryManagerDestroyCallback {
212 | }
213 |
214 |
215 | /**
216 | * Create a simple custom MCJIT memory manager. This memory manager can
217 | * intercept allocations in a module-oblivious way. This will return NULL
218 | * if any of the passed functions are NULL.
219 | *
220 | * @param Opaque An opaque client object to pass back to the callbacks.
221 | * @param AllocateCodeSection Allocate a block of memory for executable code.
222 | * @param AllocateDataSection Allocate a block of memory for data.
223 | * @param FinalizeMemory Set page permissions and flush cache. Return 0 on
224 | * success, 1 on error.
225 | */
226 | public static native @LLVMMCJITMemoryManagerRef long LLVMCreateSimpleMCJITMemoryManager(
227 | @Pointer("void *") long Opaque,
228 | @LLVMMemoryManagerAllocateCodeSectionCallback long AllocateCodeSection,
229 | @LLVMMemoryManagerAllocateDataSectionCallback long AllocateDataSection,
230 | @LLVMMemoryManagerFinalizeMemoryCallback long FinalizeMemory,
231 | @LLVMMemoryManagerDestroyCallback long Destroy);
232 |
233 | public static native void LLVMDisposeMCJITMemoryManager(@LLVMMCJITMemoryManagerRef long MM);
234 |
235 | /*===-- JIT Event Listener functions -------------------------------------===*/
236 |
237 | public static native @LLVMJITEventListenerRef long LLVMCreateGDBRegistrationListener();
238 |
239 | public static native @LLVMJITEventListenerRef long LLVMCreateIntelJITEventListener();
240 |
241 | public static native @LLVMJITEventListenerRef long LLVMCreateOProfileJITEventListener();
242 |
243 | public static native @LLVMJITEventListenerRef long LLVMCreatePerfJITEventListener();
244 | }
245 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/IRReader.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.CInfo;
4 | import asia.kala.llvm.binding.annotations.Pointer;
5 |
6 | import static asia.kala.llvm.binding.Types.*;
7 |
8 | @CInfo(
9 | fileName = "IRReader.c",
10 | include = {
11 | "",
12 | "llvm-java.h"
13 | }
14 | )
15 | public final class IRReader extends LLVMLoader {
16 | private IRReader() {
17 | }
18 |
19 | /**
20 | * Read LLVM IR from a memory buffer and convert it into an in-memory Module
21 | * object. Returns 0 on success.
22 | * Optionally returns a human-readable description of any errors that
23 | * occurred during parsing IR. OutMessage must be disposed with
24 | * LLVMDisposeMessage.
25 | */
26 | public static native boolean LLVMParseIRInContext(
27 | @LLVMContextRef long ContextRef,
28 | @LLVMMemoryBufferRef long MemBuf, @Pointer("LLVMModuleRef *") long OutM,
29 | @Pointer("char **") long OutMessage
30 | );
31 | }
32 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/Initialization.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.CInfo;
4 |
5 | import static asia.kala.llvm.binding.Types.*;
6 |
7 | @CInfo(
8 | fileName = "Initialization.c",
9 | include = {
10 | "",
11 | "llvm-java.h"
12 | }
13 | )
14 | public final class Initialization extends LLVMLoader {
15 | private Initialization() {
16 | }
17 |
18 | public static native void LLVMInitializeCore(@LLVMPassRegistryRef long R);
19 |
20 | public static native void LLVMInitializeTransformUtils(@LLVMPassRegistryRef long R);
21 |
22 | public static native void LLVMInitializeScalarOpts(@LLVMPassRegistryRef long R);
23 |
24 | public static native void LLVMInitializeObjCARCOpts(@LLVMPassRegistryRef long R);
25 |
26 | public static native void LLVMInitializeVectorization(@LLVMPassRegistryRef long R);
27 |
28 | public static native void LLVMInitializeInstCombine(@LLVMPassRegistryRef long R);
29 |
30 | public static native void LLVMInitializeAggressiveInstCombiner(@LLVMPassRegistryRef long R);
31 |
32 | public static native void LLVMInitializeIPO(@LLVMPassRegistryRef long R);
33 |
34 | public static native void LLVMInitializeInstrumentation(@LLVMPassRegistryRef long R);
35 |
36 | public static native void LLVMInitializeAnalysis(@LLVMPassRegistryRef long R);
37 |
38 | public static native void LLVMInitializeIPA(@LLVMPassRegistryRef long R);
39 |
40 | public static native void LLVMInitializeCodeGen(@LLVMPassRegistryRef long R);
41 |
42 | public static native void LLVMInitializeTarget(@LLVMPassRegistryRef long R);
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/LLVMLoader.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.nio.channels.FileChannel;
6 | import java.nio.channels.FileLock;
7 | import java.nio.file.*;
8 | import java.util.Properties;
9 |
10 | public class LLVMLoader {
11 | static boolean loaded = false;
12 |
13 | static Throwable ex;
14 |
15 | private static final String OS = detectOS();
16 | private static final String ARCH = detectArch();
17 |
18 | public static final String PLATFORM =
19 | (OS.equals("UNKNOWN") || ARCH.equals("UNKNOWN")) ? "UNKNOWN" : OS + "-" + ARCH;
20 |
21 | static Properties properties;
22 |
23 | static final boolean debug;
24 |
25 |
26 | static {
27 | debug = Boolean.getBoolean("asia.kala.llvm.debug");
28 |
29 | try (InputStream in = LLVMLoader.class.getResourceAsStream("llvm.properties")) {
30 | properties = new Properties();
31 | properties.load(in);
32 |
33 | if (debug) {
34 | System.out.println("[DEBUG] platform: " + PLATFORM);
35 | System.out.println("[DEBUG] llvm.properties: " + properties);
36 | }
37 |
38 | } catch (IOException e) {
39 | properties = null;
40 | }
41 |
42 | load();
43 | }
44 |
45 | private static String detectOS() {
46 | String osName = System.getProperty("os.name", "").toLowerCase().trim();
47 | String jvmName = System.getProperty("java.vm.name", "").toLowerCase().trim();
48 |
49 | if (osName.startsWith("windows ce")) {
50 | return "windowsce";
51 | }
52 | if (osName.startsWith("windows")) {
53 | return "windows";
54 | }
55 | if (osName.startsWith("mac")) {
56 | return "macos";
57 | }
58 | if (osName.startsWith("darwin")) {
59 | if ("robovm".equals(jvmName)) {
60 | return "ios";
61 | }
62 | return "darwin";
63 | }
64 |
65 | if (osName.startsWith("linux") || osName.equals("gnu")) {
66 | if (jvmName.equals("dalvik")) {
67 | return "android";
68 | }
69 | return "linux";
70 | }
71 |
72 | if (osName.startsWith("aix")) {
73 | return "aix";
74 | }
75 | if (osName.startsWith("solaris") || osName.startsWith("sunos")) {
76 | return "solaris";
77 | }
78 | if (osName.startsWith("freebsd")) {
79 | return "freebsd";
80 | }
81 | if (osName.startsWith("openbsd")) {
82 | return "openbsd";
83 | }
84 | if (osName.startsWith("netbsd")) {
85 | return "netbsd";
86 | }
87 | if (osName.startsWith("dragonfly")) {
88 | return "dragonfly";
89 | }
90 | if (osName.equals("gnu/kfreebsd")) {
91 | return "kfreebsd";
92 | }
93 |
94 | return "UNKNOWN";
95 | }
96 |
97 | private static String detectArch() {
98 | String arch = System.getProperty("os.arch").toLowerCase().trim();
99 | switch (arch) {
100 | case "x86":
101 | case "i386":
102 | case "i486":
103 | case "i586":
104 | case "i686":
105 | return "x86";
106 | case "x64":
107 | case "x86-64":
108 | case "amd64":
109 | return "x86_64";
110 | case "ppc":
111 | case "powerpc":
112 | return "ppc";
113 | case "ppc64":
114 | case "powerpc64":
115 | if ("little".equals(System.getProperty("sun.cpu.endian"))) {
116 | return "ppc64le";
117 | }
118 | return "ppc64";
119 | case "ppc64le":
120 | case "powerpc64le":
121 | return "ppc64le";
122 | case "s390":
123 | case "s390x":
124 | return "s390x";
125 | case "sparc":
126 | return "sparc";
127 | case "sparv9c":
128 | return "sparcv9";
129 | case "mips":
130 | return "mips";
131 | case "mips64":
132 | return "mips64";
133 | case "mipsel":
134 | return "mipsel";
135 | case "mips64el":
136 | return "mips64el";
137 | }
138 |
139 | if (arch.startsWith("aarch64") || arch.startsWith("armv8") || arch.startsWith("arm64")) {
140 | return "arm64";
141 | } else if (arch.startsWith("arm")) {
142 | return "arm";
143 | }
144 |
145 | return "UNKNOWN";
146 | }
147 |
148 | private static void load() {
149 | try {
150 | if ("UNKNOWN".equals(PLATFORM)) {
151 | return;
152 | }
153 | Path temDir = null;
154 |
155 | if (properties != null) {
156 | String v = properties.getProperty("llvm.java.version");
157 | String tem = System.getProperty("java.io.tmpdir");
158 |
159 | if (v != null && !v.endsWith("-SNAPSHOT") && tem != null) {
160 | Path p = Paths.get(tem);
161 | if (Files.exists(p) && Files.isDirectory(p)) {
162 | temDir = p.resolve("asia.kala.llvm").resolve(v).resolve(PLATFORM);
163 | try {
164 | Files.createDirectories(temDir);
165 | } catch (IOException | SecurityException ignored) {
166 | temDir = null;
167 | }
168 | }
169 | }
170 | }
171 | if (temDir == null) {
172 | temDir = Files.createTempDirectory("llvm");
173 | }
174 |
175 | final String libPrefix = OS.startsWith("windows") ? "" : "lib";
176 | final String libSuffix =
177 | OS.startsWith("windows") ?
178 | ".dll" : OS.equals("macos") ?
179 | ".dylib" : ".so";
180 |
181 | final String libName = libPrefix + "llvm-java" + libSuffix;
182 |
183 | Path libPath = temDir.resolve(libName).toAbsolutePath();
184 |
185 | if (Files.exists(libPath)) {
186 | if (!Files.isRegularFile(libPath) || !Files.isReadable(libPath)) {
187 | temDir = Files.createTempDirectory("llvm");
188 | libPath = temDir.resolve(libName).toAbsolutePath();
189 | } else {
190 | System.load(libPath.toString());
191 | loaded = true;
192 | return;
193 | }
194 | }
195 |
196 | try (InputStream input = LLVMLoader.class.getClassLoader().getResourceAsStream(
197 | "asia/kala/llvm/platform/" + PLATFORM + "/" + libName)) {
198 | if (input != null) {
199 | Files.copy(input, libPath);
200 | System.load(libPath.toString());
201 | loaded = true;
202 | }
203 | } catch (FileAlreadyExistsException e) {
204 | System.load(libPath.toString());
205 | loaded = true;
206 | }
207 | } catch (IOException | UnsatisfiedLinkError | SecurityException e) {
208 | ex = e;
209 | loaded = false;
210 | }
211 | }
212 |
213 | public static boolean LLVMIsLoader() {
214 | return loaded;
215 | }
216 | }
217 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/Linker.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.CEnum;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 | import asia.kala.llvm.binding.annotations.Pointer;
6 | import asia.kala.llvm.binding.annotations.Signed;
7 |
8 | import java.lang.annotation.Documented;
9 | import java.lang.annotation.ElementType;
10 | import java.lang.annotation.Retention;
11 | import java.lang.annotation.RetentionPolicy;
12 |
13 | import static asia.kala.llvm.binding.Types.*;
14 |
15 | @CInfo(
16 | fileName = "Linker.c",
17 | include = {
18 | "",
19 | "llvm-java.h"
20 | }
21 | )
22 | public final class Linker extends LLVMLoader {
23 | private Linker() {
24 |
25 | }
26 |
27 | @CEnum("LLVMLinkerMode")
28 | @Documented
29 | @java.lang.annotation.Target(ElementType.TYPE_USE)
30 | @Retention(RetentionPolicy.RUNTIME)
31 | public @interface LLVMLinkerMode {
32 | int LLVMLinkerDestroySource = 0; /* This is the default behavior. */
33 | int LLVMLinkerPreserveSource_Removed = 1; /* This option has been deprecated and should not be used. */
34 | }
35 |
36 | /**
37 | * Links the source module into the destination module. The source module is
38 | * destroyed.
39 | * The return value is true if an error occurred, false otherwise.
40 | * Use the diagnostic handler to get any diagnostic message.
41 | */
42 | public static native boolean LLVMLinkModules2(@LLVMModuleRef long Dest, @LLVMModuleRef long Src);
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/NativeUtils.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.Pointer;
4 | import asia.kala.llvm.binding.annotations.SizeT;
5 | import asia.kala.llvm.binding.annotations.Unsigned;
6 |
7 | import java.nio.ByteBuffer;
8 | import java.nio.charset.Charset;
9 |
10 | public final class NativeUtils extends LLVMLoader {
11 | public static @Pointer("void *") long nullptr = 0L;
12 |
13 | public static native @Pointer("void *") long GetDirectBufferAddress(ByteBuffer buffer);
14 |
15 | public static native ByteBuffer NewDirectByteBuffer(@Pointer("void *") long address, long capacity);
16 |
17 | public static native @Pointer("void *") long Malloc(@SizeT long size);
18 |
19 | public static native void Free(@Pointer("void *") long block);
20 |
21 | public static native @Unsigned("uint8_t") byte GetByte(@Pointer("const uint8_t *") long address);
22 |
23 | public static native void SetByte(@Pointer("uint8_t *") long address, @Unsigned("uint8_t") byte value);
24 |
25 | public static native @Pointer("size_t *") long NewSizeT();
26 |
27 | public static native @SizeT long GetSizeT(@Pointer("const size_t *") long address);
28 |
29 | public static native void SetSizeT(@Pointer("size_t *") long address, @SizeT long value);
30 |
31 | public static native @Pointer("LLVMBool *") long NewLLVMBool();
32 |
33 | public static native boolean GetLLVMBool(@Pointer("const LLVMBool *") long address);
34 |
35 | public static native void SetLLVMBool(@Pointer("LLVMBool *") long address, boolean value);
36 |
37 | public static native void DumpString(@Pointer("const char *") long string);
38 |
39 | public static @Pointer("char *") long CopyStringToNative(String string) {
40 | return CopyStringToNative(string, null);
41 | }
42 |
43 | public static @Pointer("char *") long CopyStringToNative(String string, Charset charset) {
44 | if (string == null) {
45 | return 0L;
46 | }
47 | if (charset == null) {
48 | charset = Charset.defaultCharset();
49 | }
50 |
51 | ByteBuffer encoded = charset.encode(string);
52 | int length = encoded.remaining();
53 |
54 | final long res = Malloc(length + 1);
55 |
56 | long ptr = res;
57 | while (encoded.hasRemaining()) {
58 | SetByte(ptr++, encoded.get());
59 | }
60 | SetByte(ptr, (byte) 0);
61 | return res;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/ObjectH.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.*;
4 |
5 | import java.lang.annotation.*;
6 |
7 | import static asia.kala.llvm.binding.Types.*;
8 |
9 | @CInfo(
10 | fileName = "Object.c",
11 | include = {
12 | "",
13 | "llvm-java.h"
14 | }
15 | )
16 | public final class ObjectH extends LLVMLoader {
17 | private ObjectH() {
18 | }
19 |
20 | @Pointer("LLVMSectionIteratorRef")
21 | @Documented
22 | @java.lang.annotation.Target(ElementType.TYPE_USE)
23 | @Retention(RetentionPolicy.RUNTIME)
24 | public @interface LLVMSectionIteratorRef {
25 | }
26 |
27 | @Pointer("LLVMSymbolIteratorRef")
28 | @Documented
29 | @java.lang.annotation.Target(ElementType.TYPE_USE)
30 | @Retention(RetentionPolicy.RUNTIME)
31 | public @interface LLVMSymbolIteratorRef {
32 | }
33 |
34 | @Pointer("LLVMRelocationIteratorRef")
35 | @Documented
36 | @java.lang.annotation.Target(ElementType.TYPE_USE)
37 | @Retention(RetentionPolicy.RUNTIME)
38 | public @interface LLVMRelocationIteratorRef {
39 | }
40 |
41 | @CEnum("LLVMBinaryType")
42 | @Documented
43 | @java.lang.annotation.Target(ElementType.TYPE_USE)
44 | @Retention(RetentionPolicy.RUNTIME)
45 | public @interface LLVMBinaryType {
46 | /**
47 | * Archive file.
48 | */
49 | int LLVMBinaryTypeArchive = 0;
50 | /**
51 | * Mach-O Universal Binary file.
52 | */
53 | int LLVMBinaryTypeMachOUniversalBinary = 1;
54 | /**
55 | * COFF Import file.
56 | */
57 | int LLVMBinaryTypeCOFFImportFile = 2;
58 | /**
59 | * LLVM IR.
60 | */
61 | int LLVMBinaryTypeIR = 3;
62 | /**
63 | * Windows resource (.res) file.
64 | */
65 | int LLVMBinaryTypeWinRes = 4;
66 | /**
67 | * COFF Object file.
68 | */
69 | int LLVMBinaryTypeCOFF = 5;
70 | /**
71 | * ELF 32-bit, little endian.
72 | */
73 | int LLVMBinaryTypeELF32L = 6;
74 | /**
75 | * ELF 32-bit, big endian.
76 | */
77 | int LLVMBinaryTypeELF32B = 7;
78 | /**
79 | * ELF 64-bit, little endian.
80 | */
81 | int LLVMBinaryTypeELF64L = 8;
82 | /**
83 | * ELF 64-bit, big endian.
84 | */
85 | int LLVMBinaryTypeELF64B = 9;
86 | /**
87 | * MachO 32-bit, little endian.
88 | */
89 | int LLVMBinaryTypeMachO32L = 10;
90 | /**
91 | * MachO 32-bit, big endian.
92 | */
93 | int LLVMBinaryTypeMachO32B = 11;
94 | /**
95 | * MachO 64-bit, little endian.
96 | */
97 | int LLVMBinaryTypeMachO64L = 12;
98 | /**
99 | * MachO 64-bit, big endian.
100 | */
101 | int LLVMBinaryTypeMachO64B = 13;
102 | /**
103 | * Web Assembly.
104 | */
105 | int LLVMBinaryTypeWasm = 14;
106 | }
107 |
108 | /**
109 | * Create a binary file from the given memory buffer.
110 | *
111 | * The exact type of the binary file will be inferred automatically, and the
112 | * appropriate implementation selected. The context may be NULL except if
113 | * the resulting file is an LLVM IR file.
114 | *
115 | * The memory buffer is not consumed by this function. It is the responsibilty
116 | * of the caller to free it with \c LLVMDisposeMemoryBuffer.
117 | *
118 | * If NULL is returned, the \p ErrorMessage parameter is populated with the
119 | * error's description. It is then the caller's responsibility to free this
120 | * message by calling \c LLVMDisposeMessage.
121 | */
122 | public static native @LLVMBinaryRef long LLVMCreateBinary(
123 | @LLVMMemoryBufferRef long MemBuf,
124 | @LLVMContextRef long Context,
125 | @Pointer("char **") long ErrorMessage
126 | );
127 |
128 | /**
129 | * Dispose of a binary file.
130 | *
131 | * The binary file does not own its backing buffer. It is the responsibilty
132 | * of the caller to free it with \c LLVMDisposeMemoryBuffer.
133 | */
134 | public static native void LLVMDisposeBinary(@LLVMBinaryRef long BR);
135 |
136 | /**
137 | * Retrieves a copy of the memory buffer associated with this object file.
138 | *
139 | * The returned buffer is merely a shallow copy and does not own the actual
140 | * backing buffer of the binary. Nevertheless, it is the responsibility of the
141 | * caller to free it with \c LLVMDisposeMemoryBuffer.
142 | */
143 | public static native @LLVMMemoryBufferRef long LLVMBinaryCopyMemoryBuffer(@LLVMBinaryRef long BR);
144 |
145 | /**
146 | * Retrieve the specific type of a binary.
147 | */
148 | public static native @LLVMBinaryType int LLVMBinaryGetType(@LLVMBinaryRef long BR);
149 |
150 | /*
151 | * For a Mach-O universal binary file, retrieves the object file corresponding
152 | * to the given architecture if it is present as a slice.
153 | *
154 | * If NULL is returned, the \p ErrorMessage parameter is populated with the
155 | * error's description. It is then the caller's responsibility to free this
156 | * message by calling \c LLVMDisposeMessage.
157 | *
158 | * It is the responsiblity of the caller to free the returned object file by
159 | * calling \c LLVMDisposeBinary.
160 | */
161 | public static native @LLVMBinaryRef long LLVMMachOUniversalBinaryCopyObjectForArch(
162 | @LLVMBinaryRef long BR,
163 | @Pointer("const char *") long Arch,
164 | @SizeT long ArchLen,
165 | @Pointer("char **") long ErrorMessage
166 | );
167 |
168 | /**
169 | * Retrieve a copy of the section iterator for this object file.
170 | *
171 | * If there are no sections, the result is NULL.
172 | *
173 | * The returned iterator is merely a shallow copy. Nevertheless, it is
174 | * the responsibility of the caller to free it with
175 | * \c LLVMDisposeSectionIterator.
176 | */
177 | public static native @LLVMSectionIteratorRef long LLVMObjectFileCopySectionIterator(@LLVMBinaryRef long BR);
178 |
179 | /**
180 | * Returns whether the given section iterator is at the end.
181 | */
182 | public static native boolean LLVMObjectFileIsSectionIteratorAtEnd(
183 | @LLVMBinaryRef long BR,
184 | @LLVMSectionIteratorRef long SI
185 | );
186 |
187 | /**
188 | * Retrieve a copy of the symbol iterator for this object file.
189 | *
190 | * If there are no symbols, the result is NULL.
191 | *
192 | * The returned iterator is merely a shallow copy. Nevertheless, it is
193 | * the responsibility of the caller to free it with
194 | * \c LLVMDisposeSymbolIterator.
195 | */
196 | public static native @LLVMSymbolIteratorRef long LLVMObjectFileCopySymbolIterator(@LLVMBinaryRef long BR);
197 |
198 | /**
199 | * Returns whether the given symbol iterator is at the end.
200 | */
201 | public static native boolean LLVMObjectFileIsSymbolIteratorAtEnd(
202 | @LLVMBinaryRef long BR,
203 | @LLVMSymbolIteratorRef long SI
204 | );
205 |
206 | public static native void LLVMDisposeSectionIterator(@LLVMSectionIteratorRef long SI);
207 |
208 | public static native void LLVMMoveToNextSection(@LLVMSectionIteratorRef long SI);
209 |
210 | public static native void LLVMMoveToContainingSection(@LLVMSectionIteratorRef long Sect,
211 | @LLVMSymbolIteratorRef long Sym);
212 |
213 | // ObjectFile Symbol iterators
214 | public static native void LLVMDisposeSymbolIterator(@LLVMSymbolIteratorRef long SI);
215 |
216 | public static native void LLVMMoveToNextSymbol(@LLVMSymbolIteratorRef long SI);
217 |
218 | // SectionRef accessors
219 | public static native @Pointer("const char *") long LLVMGetSectionName(@LLVMSectionIteratorRef long SI);
220 |
221 | public static native @Unsigned("uint64_t") long LLVMGetSectionSize(@LLVMSectionIteratorRef long SI);
222 |
223 | public static native @Pointer("const char *") long LLVMGetSectionContents(@LLVMSectionIteratorRef long SI);
224 |
225 | public static native @Unsigned("uint64_t") long LLVMGetSectionAddress(@LLVMSectionIteratorRef long SI);
226 |
227 | public static native boolean LLVMGetSectionContainsSymbol(
228 | @LLVMSectionIteratorRef long SI,
229 | @LLVMSymbolIteratorRef long Sym
230 | );
231 |
232 | // Section Relocation iterators
233 | public static native @LLVMRelocationIteratorRef long LLVMGetRelocations(@LLVMSectionIteratorRef long Section);
234 |
235 | public static native void LLVMDisposeRelocationIterator(@LLVMRelocationIteratorRef long RI);
236 |
237 | public static native boolean LLVMIsRelocationIteratorAtEnd(
238 | @LLVMSectionIteratorRef long Section,
239 | @LLVMRelocationIteratorRef long RI
240 | );
241 |
242 | public static native void LLVMMoveToNextRelocation(@LLVMRelocationIteratorRef long RI);
243 |
244 |
245 | // SymbolRef accessors
246 | public static native @Pointer("const char *") long LLVMGetSymbolName(@LLVMSymbolIteratorRef long SI);
247 |
248 | public static native @Unsigned("uint64_t") long LLVMGetSymbolAddress(@LLVMSymbolIteratorRef long SI);
249 |
250 | public static native @Unsigned("uint64_t") long LLVMGetSymbolSize(@LLVMSymbolIteratorRef long SI);
251 |
252 | // RelocationRef accessors
253 | public static native @Unsigned("uint64_t") long LLVMGetRelocationOffset(@LLVMRelocationIteratorRef long RI);
254 |
255 | public static native @LLVMSymbolIteratorRef long LLVMGetRelocationSymbol(@LLVMRelocationIteratorRef long RI);
256 |
257 | public static native @Unsigned("uint64_t") long LLVMGetRelocationType(@LLVMRelocationIteratorRef long RI);
258 |
259 | // NOTE: Caller takes ownership of returned string of the two
260 | // following functions.
261 | public static native @Pointer("const char *") long LLVMGetRelocationTypeName(@LLVMRelocationIteratorRef long RI);
262 |
263 | public static native @Pointer("const char *") long LLVMGetRelocationValueString(@LLVMRelocationIteratorRef long RI);
264 |
265 | /**
266 | * @deprecated Use @LLVMBinaryRef long instead.
267 | */
268 | @Deprecated
269 | @Pointer("LLVMObjectFileRef")
270 | @Documented
271 | @java.lang.annotation.Target(ElementType.TYPE_USE)
272 | @Retention(RetentionPolicy.RUNTIME)
273 | public @interface LLVMObjectFileRef {
274 | }
275 |
276 | /**
277 | * @deprecated Use LLVMCreateBinary instead.
278 | */
279 | @Deprecated
280 | public static native @LLVMObjectFileRef long LLVMCreateObjectFile(@LLVMMemoryBufferRef long MemBuf);
281 |
282 | /**
283 | * @deprecated Use LLVMDisposeBinary instead.
284 | */
285 | @Deprecated
286 | public static native void LLVMDisposeObjectFile(@LLVMObjectFileRef long ObjectFile);
287 |
288 | /**
289 | * @deprecated Use LLVMObjectFileCopySectionIterator instead.
290 | */
291 | @Deprecated
292 | public static native @LLVMSectionIteratorRef long LLVMGetSections(@LLVMObjectFileRef long ObjectFile);
293 |
294 | /**
295 | * @deprecated Use LLVMObjectFileIsSectionIteratorAtEnd instead.
296 | */
297 | @Deprecated
298 | public static native boolean LLVMIsSectionIteratorAtEnd(
299 | @LLVMObjectFileRef long ObjectFile,
300 | @LLVMSectionIteratorRef long SI
301 | );
302 |
303 | /**
304 | * @deprecated Use LLVMObjectFileCopySymbolIterator instead.
305 | */
306 | @Deprecated
307 | public static native @LLVMSymbolIteratorRef long LLVMGetSymbols(@LLVMObjectFileRef long ObjectFile);
308 |
309 | /**
310 | * @deprecated Use LLVMObjectFileIsSymbolIteratorAtEnd instead.
311 | */
312 | @Deprecated
313 | public static native boolean LLVMIsSymbolIteratorAtEnd(
314 | @LLVMObjectFileRef long ObjectFile,
315 | @LLVMSymbolIteratorRef long SI
316 | );
317 | }
318 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/OrcBindings.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.CInfo;
4 | import asia.kala.llvm.binding.annotations.Pointer;
5 | import asia.kala.llvm.binding.annotations.Unsigned;
6 |
7 | import java.lang.annotation.Documented;
8 | import java.lang.annotation.ElementType;
9 | import java.lang.annotation.Retention;
10 | import java.lang.annotation.RetentionPolicy;
11 |
12 | import static asia.kala.llvm.binding.Types.*;
13 | import static asia.kala.llvm.binding.ErrorH.*;
14 | import static asia.kala.llvm.binding.TargetMachine.*;
15 |
16 | @CInfo(
17 | fileName = "OrcBindings.c",
18 | include = {
19 | "",
20 | "llvm-java.h"
21 | }
22 | )
23 | public final class OrcBindings extends LLVMLoader {
24 | private OrcBindings() {
25 | }
26 |
27 | @Pointer("LLVMOrcJITStackRef")
28 | @Documented
29 | @java.lang.annotation.Target(ElementType.TYPE_USE)
30 | @Retention(RetentionPolicy.RUNTIME)
31 | public @interface LLVMOrcJITStackRef {
32 | }
33 |
34 | @Unsigned("uint64_t")
35 | @Documented
36 | @java.lang.annotation.Target(ElementType.TYPE_USE)
37 | @Retention(RetentionPolicy.RUNTIME)
38 | public @interface LLVMOrcModuleHandle {
39 | }
40 |
41 | @Unsigned("uint64_t")
42 | @Documented
43 | @java.lang.annotation.Target(ElementType.TYPE_USE)
44 | @Retention(RetentionPolicy.RUNTIME)
45 | public @interface LLVMOrcTargetAddress {
46 | }
47 |
48 | @Pointer("LLVMOrcSymbolResolverFn")
49 | @Documented
50 | @java.lang.annotation.Target(ElementType.TYPE_USE)
51 | @Retention(RetentionPolicy.RUNTIME)
52 | public @interface LLVMOrcSymbolResolverFn {
53 | }
54 |
55 | @Pointer("LLVMOrcLazyCompileCallbackFn")
56 | @Documented
57 | @java.lang.annotation.Target(ElementType.TYPE_USE)
58 | @Retention(RetentionPolicy.RUNTIME)
59 | public @interface LLVMOrcLazyCompileCallbackFn {
60 | }
61 |
62 | /**
63 | * Create an ORC JIT stack.
64 | *
65 | * The client owns the resulting stack, and must call OrcDisposeInstance(...)
66 | * to destroy it and free its memory. The JIT stack will take ownership of the
67 | * TargetMachine, which will be destroyed when the stack is destroyed. The
68 | * client should not attempt to dispose of the Target Machine, or it will result
69 | * in a double-free.
70 | */
71 | public static native @LLVMOrcJITStackRef long LLVMOrcCreateInstance(@LLVMTargetMachineRef long TM);
72 |
73 | /**
74 | * Get the error message for the most recent error (if any).
75 | *
76 | * This message is owned by the ORC JIT Stack and will be freed when the stack
77 | * is disposed of by LLVMOrcDisposeInstance.
78 | */
79 | public static native @Pointer("const char*") long LLVMOrcGetErrorMsg(@LLVMOrcJITStackRef long JITStack);
80 |
81 | /**
82 | * Mangle the given symbol.
83 | * Memory will be allocated for MangledSymbol to hold the result. The client
84 | */
85 | public static native void LLVMOrcGetMangledSymbol(
86 | @LLVMOrcJITStackRef long JITStack, @Pointer("char **") long MangledSymbol,
87 | @Pointer("const char*") long Symbol
88 | );
89 |
90 | /**
91 | * Dispose of a mangled symbol.
92 | */
93 | public static native void LLVMOrcDisposeMangledSymbol(@Pointer("char *") long MangledSymbol);
94 |
95 | /**
96 | * Create a lazy compile callback.
97 | */
98 | public static native @LLVMErrorRef long LLVMOrcCreateLazyCompileCallback(
99 | @LLVMOrcJITStackRef long JITStack, @Pointer("LLVMOrcTargetAddress *") long RetAddr,
100 | @LLVMOrcLazyCompileCallbackFn long Callback, @Pointer("void *") long CallbackCtx
101 | );
102 |
103 | /**
104 | * Create a named indirect call stub.
105 | */
106 | public static native @LLVMErrorRef long LLVMOrcCreateIndirectStub(
107 | @LLVMOrcJITStackRef long JITStack,
108 | @Pointer("const char*") long StubName,
109 | @LLVMOrcTargetAddress long InitAddr
110 | );
111 |
112 | /**
113 | * Set the pointer for the given indirect stub.
114 | */
115 | public static native @LLVMErrorRef long LLVMOrcSetIndirectStubPointer(
116 | @LLVMOrcJITStackRef long JITStack,
117 | @Pointer("const char*") long StubName,
118 | @LLVMOrcTargetAddress long NewAddr
119 | );
120 |
121 | /**
122 | * Add module to be eagerly compiled.
123 | */
124 | public static native @LLVMErrorRef long LLVMOrcAddEagerlyCompiledIR(
125 | @LLVMOrcJITStackRef long JITStack,
126 | @Pointer("LLVMOrcModuleHandle *") long RetHandle,
127 | @LLVMModuleRef long Mod,
128 | @LLVMOrcSymbolResolverFn long SymbolResolver,
129 | @Pointer("void *") long SymbolResolverCtx
130 | );
131 |
132 | /**
133 | * Add module to be lazily compiled one function at a time.
134 | */
135 | public static native @LLVMErrorRef long LLVMOrcAddLazilyCompiledIR(
136 | @LLVMOrcJITStackRef long JITStack,
137 | @Pointer("LLVMOrcModuleHandle *") long RetHandle,
138 | @LLVMModuleRef long Mod,
139 | @LLVMOrcSymbolResolverFn long SymbolResolver,
140 | @Pointer("void *") long SymbolResolverCtx
141 | );
142 |
143 | /**
144 | * Add an object file.
145 | *
146 | * This method takes ownership of the given memory buffer and attempts to add
147 | * it to the JIT as an object file.
148 | * Clients should *not* dispose of the 'Obj' argument: the JIT will manage it
149 | * from this call onwards.
150 | */
151 | public static native @LLVMErrorRef long LLVMOrcAddObjectFile(
152 | @LLVMOrcJITStackRef long JITStack,
153 | @Pointer("LLVMOrcModuleHandle *") long RetHandle,
154 | @LLVMMemoryBufferRef long Obj,
155 | @LLVMOrcSymbolResolverFn long SymbolResolver,
156 | @Pointer("void *") long SymbolResolverCtx
157 | );
158 |
159 | /**
160 | * Remove a module set from the JIT.
161 | *
162 | * This works for all modules that can be added via OrcAdd*, including object
163 | * files.
164 | */
165 | public static native @LLVMErrorRef long LLVMOrcRemoveModule(
166 | @LLVMOrcJITStackRef long JITStack,
167 | @LLVMOrcModuleHandle long H
168 | );
169 |
170 | /**
171 | * Get symbol address from JIT instance.
172 | */
173 | public static native @LLVMErrorRef long LLVMOrcGetSymbolAddress(
174 | @LLVMOrcJITStackRef long JITStack,
175 | @Pointer("LLVMOrcTargetAddress *") long RetAddr,
176 | @Pointer("const char*") long SymbolName
177 | );
178 |
179 | /**
180 | * Get symbol address from JIT instance, searching only the specified
181 | * handle.
182 | */
183 | public static native @LLVMErrorRef long LLVMOrcGetSymbolAddressIn(
184 | @LLVMOrcJITStackRef long JITStack,
185 | @Pointer("LLVMOrcTargetAddress *") long RetAddr,
186 | @LLVMOrcModuleHandle long H,
187 | @Pointer("const char*") long SymbolName
188 | );
189 |
190 | /**
191 | * Dispose of an ORC JIT stack.
192 | */
193 | public static native @LLVMErrorRef long LLVMOrcDisposeInstance(@LLVMOrcJITStackRef long JITStack);
194 |
195 | /**
196 | * Register a JIT Event Listener.
197 | *
198 | * A NULL listener is ignored.
199 | */
200 | public static native void LLVMOrcRegisterJITEventListener(
201 | @LLVMOrcJITStackRef long JITStack, @LLVMJITEventListenerRef long L
202 | );
203 |
204 | /**
205 | * Unegister a JIT Event Listener.
206 | *
207 | * A NULL listener is ignored.
208 | */
209 | public static native void LLVMOrcUnregisterJITEventListener(
210 | @LLVMOrcJITStackRef long JITStack, @LLVMJITEventListenerRef long L
211 | );
212 | }
213 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/Remarks.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.*;
4 |
5 | import java.lang.annotation.Documented;
6 | import java.lang.annotation.ElementType;
7 | import java.lang.annotation.Retention;
8 | import java.lang.annotation.RetentionPolicy;
9 | import java.lang.annotation.Target;
10 |
11 | import static asia.kala.llvm.binding.Types.*;
12 |
13 | @CInfo(
14 | fileName = "Remarks.c",
15 | include = {
16 | "",
17 | "llvm-java.h"
18 | }
19 | )
20 | public final class Remarks extends LLVMLoader {
21 | private Remarks() {
22 | }
23 |
24 | // 0 -> 1: Bitstream remarks support.
25 | public static final int REMARKS_API_VERSION = REMARKS_API_VERSION();
26 |
27 | @Statement("return REMARKS_API_VERSION;")
28 | private static native @Signed("int") int REMARKS_API_VERSION();
29 |
30 | @CEnum("LLVMRemarkType")
31 | @Documented
32 | @Target(ElementType.TYPE_USE)
33 | @Retention(RetentionPolicy.RUNTIME)
34 | public @interface LLVMRemarkType {
35 | int LLVMRemarkTypeUnknown = 0;
36 | int LLVMRemarkTypePassed = 1;
37 | int LLVMRemarkTypeMissed = 2;
38 | int LLVMRemarkTypeAnalysis = 3;
39 | int LLVMRemarkTypeAnalysisFPCommute = 4;
40 | int LLVMRemarkTypeAnalysisAliasing = 5;
41 | int LLVMRemarkTypeFailure = 6;
42 | }
43 |
44 | /**
45 | * String containing a buffer and a length. The buffer is not guaranteed to be
46 | * zero-terminated.
47 | *
48 | * @since REMARKS_API_VERSION 0
49 | */
50 | @Pointer("LLVMRemarkStringRef")
51 | @Documented
52 | @Target(ElementType.TYPE_USE)
53 | @Retention(RetentionPolicy.RUNTIME)
54 | public @interface LLVMRemarkStringRef {
55 | }
56 |
57 |
58 | /**
59 | * Returns the buffer holding the string.
60 | *
61 | * @since REMARKS_API_VERSION 0
62 | */
63 | public static native @Pointer("const char *") long LLVMRemarkStringGetData(@LLVMRemarkStringRef long String);
64 |
65 | /**
66 | * Returns the size of the string.
67 | *
68 | * @since REMARKS_API_VERSION 0
69 | */
70 | public static native @Unsigned("uint32_t") int LLVMRemarkStringGetLen(@LLVMRemarkStringRef long String);
71 |
72 | /**
73 | * DebugLoc containing File, Line and Column.
74 | *
75 | * @since REMARKS_API_VERSION 0
76 | */
77 | @Pointer("LLVMRemarkDebugLocRef")
78 | @Documented
79 | @Target(ElementType.TYPE_USE)
80 | @Retention(RetentionPolicy.RUNTIME)
81 | public @interface LLVMRemarkDebugLocRef {
82 | }
83 |
84 | /**
85 | * Return the path to the source file for a debug location.
86 | *
87 | * @since REMARKS_API_VERSION 0
88 | */
89 | public static native @LLVMRemarkStringRef long LLVMRemarkDebugLocGetSourceFilePath(@LLVMRemarkDebugLocRef long DL);
90 |
91 | /**
92 | * Return the line in the source file for a debug location.
93 | *
94 | * @since REMARKS_API_VERSION 0
95 | */
96 | public static native @Unsigned("uint32_t") int LLVMRemarkDebugLocGetSourceLine(@LLVMRemarkDebugLocRef long DL);
97 |
98 | /**
99 | * Return the column in the source file for a debug location.
100 | *
101 | * @since REMARKS_API_VERSION 0
102 | */
103 | public static native @Unsigned("uint32_t") int LLVMRemarkDebugLocGetSourceColumn(@LLVMRemarkDebugLocRef long DL);
104 |
105 | /**
106 | * Element of the "Args" list. The key might give more information about what
107 | * the semantics of the value are, e.g. "Callee" will tell you that the value
108 | * is a symbol that names a function.
109 | *
110 | * @since REMARKS_API_VERSION 0
111 | */
112 | @Pointer("LLVMRemarkArgRef")
113 | @Documented
114 | @Target(ElementType.TYPE_USE)
115 | @Retention(RetentionPolicy.RUNTIME)
116 | public @interface LLVMRemarkArgRef {
117 | }
118 |
119 | /**
120 | * Returns the key of an argument. The key defines what the value is, and the
121 | * same key can appear multiple times in the list of arguments.
122 | *
123 | * @since REMARKS_API_VERSION 0
124 | */
125 | public static native @LLVMRemarkStringRef long LLVMRemarkArgGetKey(@LLVMRemarkArgRef long Arg);
126 |
127 | /**
128 | * Returns the value of an argument. This is a string that can contain newlines.
129 | *
130 | * @since REMARKS_API_VERSION 0
131 | */
132 | public static native @LLVMRemarkStringRef long LLVMRemarkArgGetValue(@LLVMRemarkArgRef long Arg);
133 |
134 | /**
135 | * Returns the debug location that is attached to the value of this argument.
136 | *
137 | * If there is no debug location, the return value will be `NULL`.
138 | *
139 | * @since REMARKS_API_VERSION 0
140 | */
141 | public static native @LLVMRemarkDebugLocRef long LLVMRemarkArgGetDebugLoc(@LLVMRemarkArgRef long Arg);
142 |
143 | /**
144 | * A remark emitted by the compiler.
145 | *
146 | * @since REMARKS_API_VERSION 0
147 | */
148 | @Pointer("LLVMRemarkEntryRef")
149 | @Documented
150 | @Target(ElementType.TYPE_USE)
151 | @Retention(RetentionPolicy.RUNTIME)
152 | public @interface LLVMRemarkEntryRef {
153 | }
154 |
155 | /**
156 | * Free the resources used by the remark entry.
157 | *
158 | * @since REMARKS_API_VERSION 0
159 | */
160 | public static native void LLVMRemarkEntryDispose(@LLVMRemarkEntryRef long Remark);
161 |
162 | /**
163 | * The type of the remark. For example, it can allow users to only keep the
164 | * missed optimizations from the compiler.
165 | *
166 | * @since REMARKS_API_VERSION 0
167 | */
168 | public static native @LLVMRemarkType int LLVMRemarkEntryGetType(@LLVMRemarkEntryRef long Remark);
169 |
170 | /**
171 | * Get the name of the pass that emitted this remark.
172 | *
173 | * @since REMARKS_API_VERSION 0
174 | */
175 | public static native @LLVMRemarkStringRef long
176 | LLVMRemarkEntryGetPassName(@LLVMRemarkEntryRef long Remark);
177 |
178 | /**
179 | * Get an identifier of the remark.
180 | *
181 | * @since REMARKS_API_VERSION 0
182 | */
183 | public static native @LLVMRemarkStringRef long LLVMRemarkEntryGetRemarkName(@LLVMRemarkEntryRef long Remark);
184 |
185 | /**
186 | * Get the name of the function being processed when the remark was emitted.
187 | *
188 | * @since REMARKS_API_VERSION 0
189 | */
190 | public static native @LLVMRemarkStringRef long LLVMRemarkEntryGetFunctionName(@LLVMRemarkEntryRef long Remark);
191 |
192 | /**
193 | * Returns the debug location that is attached to this remark.
194 | *
195 | * If there is no debug location, the return value will be `NULL`.
196 | *
197 | * @since REMARKS_API_VERSION 0
198 | */
199 | public static native @LLVMRemarkDebugLocRef long LLVMRemarkEntryGetDebugLoc(@LLVMRemarkEntryRef long Remark);
200 |
201 | /**
202 | * Return the hotness of the remark.
203 | *
204 | * A hotness of `0` means this value is not set.
205 | *
206 | * @since REMARKS_API_VERSION 0
207 | */
208 | public static native @Unsigned("uint64_t") long LLVMRemarkEntryGetHotness(@LLVMRemarkEntryRef long Remark);
209 |
210 | /**
211 | * The number of arguments the remark holds.
212 | *
213 | * @since REMARKS_API_VERSION 0
214 | */
215 | public static native @Unsigned("uint32_t") int LLVMRemarkEntryGetNumArgs(@LLVMRemarkEntryRef long Remark);
216 |
217 | /**
218 | * Get a new iterator to iterate over a remark's argument.
219 | *
220 | * If there are no arguments in \p Remark, the return value will be `NULL`.
221 | *
222 | * The lifetime of the returned value is bound to the lifetime of \p Remark.
223 | *
224 | * @since REMARKS_API_VERSION 0
225 | */
226 | public static native @LLVMRemarkArgRef long LLVMRemarkEntryGetFirstArg(@LLVMRemarkEntryRef long Remark);
227 |
228 | /**
229 | * Get the next argument in \p Remark from the position of \p It.
230 | *
231 | * Returns `NULL` if there are no more arguments available.
232 | *
233 | * The lifetime of the returned value is bound to the lifetime of \p Remark.
234 | *
235 | * @since REMARKS_API_VERSION 0
236 | */
237 | public static native @LLVMRemarkArgRef long LLVMRemarkEntryGetNextArg(
238 | @LLVMRemarkArgRef long It,
239 | @LLVMRemarkEntryRef long Remark
240 | );
241 |
242 | @Pointer("LLVMRemarkParserRef")
243 | @Documented
244 | @Target(ElementType.TYPE_USE)
245 | @Retention(RetentionPolicy.RUNTIME)
246 | public @interface LLVMRemarkParserRef {
247 | }
248 |
249 | /**
250 | * Creates a remark parser that can be used to parse the buffer located in \p
251 | * Buf of size \p Size bytes.
252 | *
253 | * \p Buf cannot be `NULL`.
254 | *
255 | * This function should be paired with LLVMRemarkParserDispose() to avoid
256 | * leaking resources.
257 | *
258 | * @since REMARKS_API_VERSION 0
259 | */
260 | public static native @LLVMRemarkParserRef long LLVMRemarkParserCreateYAML(
261 | @Pointer("const void *") long Buf,
262 | @Unsigned("uint64_t") long Size
263 | );
264 |
265 | /**
266 | * Creates a remark parser that can be used to parse the buffer located in \p
267 | * Buf of size \p Size bytes.
268 | *
269 | * \p Buf cannot be `NULL`.
270 | *
271 | * This function should be paired with LLVMRemarkParserDispose() to avoid
272 | * leaking resources.
273 | *
274 | * @since REMARKS_API_VERSION 1
275 | */
276 | public static native @LLVMRemarkParserRef long LLVMRemarkParserCreateBitstream(
277 | @Pointer("const void *") long Buf,
278 | @Unsigned("uint64_t") long Size
279 | );
280 |
281 | /**
282 | * Returns the next remark in the file.
283 | *
284 | * The value pointed to by the return value needs to be disposed using a call to
285 | * LLVMRemarkEntryDispose().
286 | *
287 | * All the entries in the returned value that are of LLVMRemarkStringRef type
288 | * will become invalidated once a call to LLVMRemarkParserDispose is made.
289 | *
290 | * If the parser reaches the end of the buffer, the return value will be `NULL`.
291 | *
292 | * In the case of an error, the return value will be `NULL`, and:
293 | *
294 | * 1) LLVMRemarkParserHasError() will return `1`.
295 | *
296 | * 2) LLVMRemarkParserGetErrorMessage() will return a descriptive error
297 | * message.
298 | *
299 | * An error may occur if:
300 | *
301 | * 1) An argument is invalid.
302 | *
303 | * 2) There is a parsing error. This can occur on things like malformed YAML.
304 | *
305 | * 3) There is a Remark semantic error. This can occur on well-formed files with
306 | * missing or extra fields.
307 | *
308 | * Here is a quick example of the usage:
309 | *
310 | * ```
311 | * LLVMRemarkParserRef Parser = LLVMRemarkParserCreateYAML(Buf, Size);
312 | * LLVMRemarkEntryRef Remark = NULL;
313 | * while ((Remark = LLVMRemarkParserGetNext(Parser))) {
314 | * // use Remark
315 | * LLVMRemarkEntryDispose(Remark); // Release memory.
316 | * }
317 | * bool HasError = LLVMRemarkParserHasError(Parser);
318 | * LLVMRemarkParserDispose(Parser);
319 | * ```
320 | *
321 | * @since REMARKS_API_VERSION 0
322 | */
323 | public static native @LLVMRemarkEntryRef long LLVMRemarkParserGetNext(@LLVMRemarkParserRef long Parser);
324 |
325 | /**
326 | * Returns `1` if the parser encountered an error while parsing the buffer.
327 | *
328 | * @since REMARKS_API_VERSION 0
329 | */
330 | public static native boolean LLVMRemarkParserHasError(@LLVMRemarkParserRef long Parser);
331 |
332 | /**
333 | * Returns a null-terminated string containing an error message.
334 | *
335 | * In case of no error, the result is `NULL`.
336 | *
337 | * The memory of the string is bound to the lifetime of \p Parser. If
338 | * LLVMRemarkParserDispose() is called, the memory of the string will be
339 | * released.
340 | *
341 | * @since REMARKS_API_VERSION 0
342 | */
343 | public static native @Pointer("const char *") long LLVMRemarkParserGetErrorMessage(@LLVMRemarkParserRef long Parser);
344 |
345 | /**
346 | * Releases all the resources used by \p Parser.
347 | *
348 | * @since REMARKS_API_VERSION 0
349 | */
350 | public static native void LLVMRemarkParserDispose(@LLVMRemarkParserRef long Parser);
351 |
352 | /**
353 | * Returns the version of the remarks library.
354 | *
355 | * @since REMARKS_API_VERSION 0
356 | */
357 | public static @Unsigned("uint32_t") int LLVMRemarkVersion() {
358 | return REMARKS_API_VERSION;
359 | }
360 | }
361 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/Support.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.CInfo;
4 | import asia.kala.llvm.binding.annotations.Pointer;
5 | import asia.kala.llvm.binding.annotations.Signed;
6 |
7 | @CInfo(
8 | fileName = "Support.c",
9 | include = {
10 | "",
11 | "llvm-java.h"
12 | }
13 | )
14 | public final class Support extends LLVMLoader {
15 |
16 | private Support() {
17 | }
18 |
19 | /**
20 | * This function permanently loads the dynamic library at the given path.
21 | * It is safe to call this function multiple times for the same library.
22 | */
23 | public static native boolean LLVMLoadLibraryPermanently(@Pointer("const char *") long Filename);
24 |
25 | /**
26 | * This function parses the given arguments using the LLVM command line parser.
27 | * Note that the only stable thing about this function is its signature; you
28 | * cannot rely on any particular set of command line arguments being interpreted
29 | * the same way across LLVM versions.
30 | */
31 | public static native void LLVMParseCommandLineOptions(
32 | @Signed("int") int argc, @Pointer("const char *const *") long argv,
33 | @Pointer("const char *") long Overview
34 | );
35 |
36 | /**
37 | * This function will search through all previously loaded dynamic
38 | * libraries for the symbol \p symbolName. If it is found, the address of
39 | * that symbol is returned. If not, null is returned.
40 | */
41 | public static native @Pointer("void *") long LLVMSearchForAddressOfSymbol(@Pointer("const char *") long symbolName);
42 |
43 | /**
44 | * This functions permanently adds the symbol \p symbolName with the
45 | * value \p symbolValue. These symbols are searched before any
46 | * libraries.
47 | */
48 | public static native void LLVMAddSymbol(@Pointer("const char *") long symbolName, @Pointer("void *") long symbolValue);
49 | }
50 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/TargetH.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.*;
4 |
5 | import java.lang.annotation.Documented;
6 | import java.lang.annotation.ElementType;
7 | import java.lang.annotation.Retention;
8 | import java.lang.annotation.RetentionPolicy;
9 |
10 | import static asia.kala.llvm.binding.Types.*;
11 |
12 | @CInfo(
13 | fileName = "Target.c",
14 | include = {
15 | "",
16 | "llvm-java.h"
17 | }
18 | )
19 | public final class TargetH extends LLVMLoader {
20 | private TargetH() {
21 | }
22 |
23 | @CEnum("LLVMByteOrdering")
24 | @Documented
25 | @java.lang.annotation.Target(ElementType.TYPE_USE)
26 | @Retention(RetentionPolicy.RUNTIME)
27 | public @interface LLVMByteOrdering {
28 | int LLVMBigEndian = 0;
29 | int LLVMLittleEndian = 1;
30 | }
31 |
32 | @Pointer("LLVMTargetDataRef")
33 | @Documented
34 | @java.lang.annotation.Target(ElementType.TYPE_USE)
35 | @Retention(RetentionPolicy.RUNTIME)
36 | public @interface LLVMTargetDataRef {
37 | }
38 |
39 | @Pointer("LLVMTargetLibraryInfoRef")
40 | @Documented
41 | @java.lang.annotation.Target(ElementType.TYPE_USE)
42 | @Retention(RetentionPolicy.RUNTIME)
43 | public @interface LLVMTargetLibraryInfoRef {
44 | }
45 |
46 | public static native void LLVMInitializeAArch64TargetInfo();
47 |
48 | public static native void LLVMInitializeAMDGPUTargetInfo();
49 |
50 | public static native void LLVMInitializeARMTargetInfo();
51 |
52 | public static native void LLVMInitializeBPFTargetInfo();
53 |
54 | public static native void LLVMInitializeHexagonTargetInfo();
55 |
56 | public static native void LLVMInitializeLanaiTargetInfo();
57 |
58 | public static native void LLVMInitializeMipsTargetInfo();
59 |
60 | public static native void LLVMInitializeMSP430TargetInfo();
61 |
62 | public static native void LLVMInitializeNVPTXTargetInfo();
63 |
64 | public static native void LLVMInitializePowerPCTargetInfo();
65 |
66 | public static native void LLVMInitializeRISCVTargetInfo();
67 |
68 | public static native void LLVMInitializeSparcTargetInfo();
69 |
70 | public static native void LLVMInitializeSystemZTargetInfo();
71 |
72 | public static native void LLVMInitializeWebAssemblyTargetInfo();
73 |
74 | public static native void LLVMInitializeX86TargetInfo();
75 |
76 | public static native void LLVMInitializeXCoreTargetInfo();
77 |
78 |
79 | public static native void LLVMInitializeAArch64Target();
80 |
81 | public static native void LLVMInitializeAMDGPUTarget();
82 |
83 | public static native void LLVMInitializeARMTarget();
84 |
85 | public static native void LLVMInitializeBPFTarget();
86 |
87 | public static native void LLVMInitializeHexagonTarget();
88 |
89 | public static native void LLVMInitializeLanaiTarget();
90 |
91 | public static native void LLVMInitializeMipsTarget();
92 |
93 | public static native void LLVMInitializeMSP430Target();
94 |
95 | public static native void LLVMInitializeNVPTXTarget();
96 |
97 | public static native void LLVMInitializePowerPCTarget();
98 |
99 | public static native void LLVMInitializeRISCVTarget();
100 |
101 | public static native void LLVMInitializeSparcTarget();
102 |
103 | public static native void LLVMInitializeSystemZTarget();
104 |
105 | public static native void LLVMInitializeWebAssemblyTarget();
106 |
107 | public static native void LLVMInitializeX86Target();
108 |
109 | public static native void LLVMInitializeXCoreTarget();
110 |
111 |
112 | public static native void LLVMInitializeAArch64TargetMC();
113 |
114 | public static native void LLVMInitializeAMDGPUTargetMC();
115 |
116 | public static native void LLVMInitializeARMTargetMC();
117 |
118 | public static native void LLVMInitializeBPFTargetMC();
119 |
120 | public static native void LLVMInitializeHexagonTargetMC();
121 |
122 | public static native void LLVMInitializeLanaiTargetMC();
123 |
124 | public static native void LLVMInitializeMipsTargetMC();
125 |
126 | public static native void LLVMInitializeMSP430TargetMC();
127 |
128 | public static native void LLVMInitializeNVPTXTargetMC();
129 |
130 | public static native void LLVMInitializePowerPCTargetMC();
131 |
132 | public static native void LLVMInitializeRISCVTargetMC();
133 |
134 | public static native void LLVMInitializeSparcTargetMC();
135 |
136 | public static native void LLVMInitializeSystemZTargetMC();
137 |
138 | public static native void LLVMInitializeWebAssemblyTargetMC();
139 |
140 | public static native void LLVMInitializeX86TargetMC();
141 |
142 | public static native void LLVMInitializeXCoreTargetMC();
143 |
144 |
145 | public static native void LLVMInitializeAArch64AsmPrinter();
146 |
147 | public static native void LLVMInitializeAMDGPUAsmPrinter();
148 |
149 | public static native void LLVMInitializeARMAsmPrinter();
150 |
151 | public static native void LLVMInitializeBPFAsmPrinter();
152 |
153 | public static native void LLVMInitializeHexagonAsmPrinter();
154 |
155 | public static native void LLVMInitializeLanaiAsmPrinter();
156 |
157 | public static native void LLVMInitializeMipsAsmPrinter();
158 |
159 | public static native void LLVMInitializeMSP430AsmPrinter();
160 |
161 | public static native void LLVMInitializeNVPTXAsmPrinter();
162 |
163 | public static native void LLVMInitializePowerPCAsmPrinter();
164 |
165 | public static native void LLVMInitializeRISCVAsmPrinter();
166 |
167 | public static native void LLVMInitializeSparcAsmPrinter();
168 |
169 | public static native void LLVMInitializeSystemZAsmPrinter();
170 |
171 | public static native void LLVMInitializeWebAssemblyAsmPrinter();
172 |
173 | public static native void LLVMInitializeX86AsmPrinter();
174 |
175 | public static native void LLVMInitializeXCoreAsmPrinter();
176 |
177 | public static native void LLVMInitializeAArch64AsmParser();
178 |
179 | public static native void LLVMInitializeAMDGPUAsmParser();
180 |
181 | public static native void LLVMInitializeARMAsmParser();
182 |
183 | public static native void LLVMInitializeBPFAsmParser();
184 |
185 | public static native void LLVMInitializeHexagonAsmParser();
186 |
187 | public static native void LLVMInitializeLanaiAsmParser();
188 |
189 | public static native void LLVMInitializeMipsAsmParser();
190 |
191 | public static native void LLVMInitializeMSP430AsmParser();
192 |
193 | public static void LLVMInitializeNVPTXAsmParser() {
194 | throw new UnsupportedOperationException("LLVMInitializeNVPTXAsmParser");
195 | }
196 |
197 | public static native void LLVMInitializePowerPCAsmParser();
198 |
199 | public static native void LLVMInitializeRISCVAsmParser();
200 |
201 | public static native void LLVMInitializeSparcAsmParser();
202 |
203 | public static native void LLVMInitializeSystemZAsmParser();
204 |
205 | public static native void LLVMInitializeWebAssemblyAsmParser();
206 |
207 | public static native void LLVMInitializeX86AsmParser();
208 |
209 | public static void LLVMInitializeXCoreAsmParser() {
210 | throw new UnsupportedOperationException("LLVMInitializeXCoreAsmParser");
211 | }
212 |
213 | public static native void LLVMInitializeAArch64Disassembler();
214 |
215 | public static native void LLVMInitializeAMDGPUDisassembler();
216 |
217 | public static native void LLVMInitializeARMDisassembler();
218 |
219 | public static native void LLVMInitializeBPFDisassembler();
220 |
221 | public static native void LLVMInitializeHexagonDisassembler();
222 |
223 | public static native void LLVMInitializeLanaiDisassembler();
224 |
225 | public static native void LLVMInitializeMipsDisassembler();
226 |
227 | public static native void LLVMInitializeMSP430Disassembler();
228 |
229 | public static void LLVMInitializeNVPTXDisassembler() {
230 | throw new UnsupportedOperationException("LLVMInitializeNVPTXDisassembler");
231 | }
232 |
233 | public static native void LLVMInitializePowerPCDisassembler();
234 |
235 | public static native void LLVMInitializeRISCVDisassembler();
236 |
237 | public static native void LLVMInitializeSparcDisassembler();
238 |
239 | public static native void LLVMInitializeSystemZDisassembler();
240 |
241 | public static native void LLVMInitializeWebAssemblyDisassembler();
242 |
243 | public static native void LLVMInitializeX86Disassembler();
244 |
245 | public static native void LLVMInitializeXCoreDisassembler();
246 |
247 |
248 | public static native void LLVMInitializeAllTargets();
249 |
250 | public static native void LLVMInitializeAllTargetMCs();
251 |
252 | public static native void LLVMInitializeAllAsmPrinters();
253 |
254 | public static native void LLVMInitializeAllAsmParsers();
255 |
256 | public static native void LLVMInitializeAllDisassemblers();
257 |
258 | public static native boolean LLVMInitializeNativeTarget();
259 |
260 | public static native boolean LLVMInitializeNativeAsmParser();
261 |
262 | public static native boolean LLVMInitializeNativeAsmPrinter();
263 |
264 | public static native boolean LLVMInitializeNativeDisassembler();
265 |
266 | /*===-- Target Data -------------------------------------------------------===*/
267 |
268 | /**
269 | * Obtain the data layout for a module.
270 | */
271 | public static native @LLVMTargetDataRef long LLVMGetModuleDataLayout(@LLVMModuleRef long M);
272 |
273 | /**
274 | * Set the data layout for a module.
275 | */
276 | public static native void LLVMSetModuleDataLayout(@LLVMModuleRef long M, @LLVMTargetDataRef long DL);
277 |
278 | /**
279 | * Creates target data from a target layout string.
280 | * See the constructor llvm::DataLayout::DataLayout.
281 | */
282 | public static native @LLVMTargetDataRef long LLVMCreateTargetData(@Pointer("const char *") long StringRep);
283 |
284 | /**
285 | * Deallocates a TargetData.
286 | * See the destructor llvm::DataLayout::~DataLayout.
287 | */
288 | public static native void LLVMDisposeTargetData(@LLVMTargetDataRef long TD);
289 |
290 | /**
291 | * Adds target library information to a pass manager. This does not take
292 | * ownership of the target library info.
293 | * See the method llvm::PassManagerBase::add.
294 | */
295 | public static native void LLVMAddTargetLibraryInfo(
296 | @LLVMTargetLibraryInfoRef long TLI, @LLVMPassManagerRef long PM
297 | );
298 |
299 | /**
300 | * Converts target data to a target layout string. The string must be disposed
301 | * with LLVMDisposeMessage.
302 | * See the constructor llvm::DataLayout::DataLayout.
303 | */
304 | public static native @Pointer("char *") long LLVMCopyStringRepOfTargetData(@LLVMTargetDataRef long TD);
305 |
306 | /**
307 | * Returns the byte order of a target, either LLVMBigEndian or
308 | * LLVMLittleEndian.
309 | * See the method llvm::DataLayout::isLittleEndian.
310 | */
311 | public static native @LLVMByteOrdering int LLVMByteOrder(@LLVMTargetDataRef long TD);
312 |
313 | /**
314 | * Returns the pointer size in bytes for a target.
315 | * See the method llvm::DataLayout::getPointerSize.
316 | */
317 | public static native @Unsigned int LLVMPointerSize(@LLVMTargetDataRef long TD);
318 |
319 | /**
320 | * Returns the pointer size in bytes for a target for a specified
321 | * address space.
322 | * See the method llvm::DataLayout::getPointerSize.
323 | */
324 | public static native @Unsigned int LLVMPointerSizeForAS(@LLVMTargetDataRef long TD, @Unsigned int AS);
325 |
326 | /**
327 | * Returns the integer type that is the same size as a pointer on a target.
328 | * See the method llvm::DataLayout::getIntPtrType.
329 | */
330 | public static native @LLVMTypeRef long LLVMIntPtrType(@LLVMTargetDataRef long TD);
331 |
332 | /**
333 | * Returns the integer type that is the same size as a pointer on a target.
334 | * This version allows the address space to be specified.
335 | * See the method llvm::DataLayout::getIntPtrType.
336 | */
337 | public static native @LLVMTypeRef long LLVMIntPtrTypeForAS(@LLVMTargetDataRef long TD, @Unsigned int AS);
338 |
339 | /**
340 | * Returns the integer type that is the same size as a pointer on a target.
341 | * See the method llvm::DataLayout::getIntPtrType.
342 | */
343 | public static native @LLVMTypeRef long LLVMIntPtrTypeInContext(@LLVMContextRef long C, @LLVMTargetDataRef long TD);
344 |
345 | /**
346 | * Returns the integer type that is the same size as a pointer on a target.
347 | * This version allows the address space to be specified.
348 | * See the method llvm::DataLayout::getIntPtrType.
349 | */
350 | public static native @LLVMTypeRef long LLVMIntPtrTypeForASInContext(@LLVMContextRef long C, @LLVMTargetDataRef long TD,
351 | @Unsigned int AS);
352 |
353 | /**
354 | * Computes the size of a type in bytes for a target.
355 | * See the method llvm::DataLayout::getTypeSizeInBits.
356 | */
357 | public static native @Unsigned("unsigned long long") long LLVMSizeOfTypeInBits(@LLVMTargetDataRef long TD, @LLVMTypeRef long Ty);
358 |
359 | /**
360 | * Computes the storage size of a type in bytes for a target.
361 | * See the method llvm::DataLayout::getTypeStoreSize.
362 | */
363 | public static native @Unsigned("unsigned long long") long LLVMStoreSizeOfType(@LLVMTargetDataRef long TD, @LLVMTypeRef long Ty);
364 |
365 | /**
366 | * Computes the ABI size of a type in bytes for a target.
367 | * See the method llvm::DataLayout::getTypeAllocSize.
368 | */
369 | public static native @Unsigned("unsigned long long") long LLVMABISizeOfType(@LLVMTargetDataRef long TD, @LLVMTypeRef long Ty);
370 |
371 | /**
372 | * Computes the ABI alignment of a type in bytes for a target.
373 | * See the method llvm::DataLayout::getTypeABISize.
374 | */
375 | public static native @Unsigned int LLVMABIAlignmentOfType(@LLVMTargetDataRef long TD, @LLVMTypeRef long Ty);
376 |
377 | /**
378 | * Computes the call frame alignment of a type in bytes for a target.
379 | * See the method llvm::DataLayout::getTypeABISize.
380 | */
381 | public static native @Unsigned int LLVMCallFrameAlignmentOfType(@LLVMTargetDataRef long TD, @LLVMTypeRef long Ty);
382 |
383 | /**
384 | * Computes the preferred alignment of a type in bytes for a target.
385 | * See the method llvm::DataLayout::getTypeABISize.
386 | */
387 | public static native @Unsigned int LLVMPreferredAlignmentOfType(@LLVMTargetDataRef long TD, @LLVMTypeRef long Ty);
388 |
389 | /**
390 | * Computes the preferred alignment of a global variable in bytes for a target.
391 | * See the method llvm::DataLayout::getPreferredAlignment.
392 | */
393 | public static native @Unsigned int LLVMPreferredAlignmentOfGlobal(
394 | @LLVMTargetDataRef long TD, @LLVMValueRef long GlobalVar
395 | );
396 |
397 | /**
398 | * Computes the structure element that contains the byte offset for a target.
399 | * See the method llvm::StructLayout::getElementContainingOffset.
400 | */
401 | public static native @Unsigned int LLVMElementAtOffset(
402 | @LLVMTargetDataRef long TD, @LLVMTypeRef long StructTy,
403 | @Unsigned("unsigned long long") long Offset
404 | );
405 |
406 | /**
407 | * Computes the byte offset of the indexed struct element for a target.
408 | * See the method llvm::StructLayout::getElementContainingOffset.
409 | */
410 | public static native @Unsigned("unsigned long long") long LLVMOffsetOfElement(
411 | @LLVMTargetDataRef long TD,
412 | @LLVMTypeRef long StructTy, @Unsigned int Element
413 | );
414 | }
415 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/TargetMachine.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.CEnum;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 | import asia.kala.llvm.binding.annotations.Pointer;
6 |
7 | import java.lang.annotation.Documented;
8 | import java.lang.annotation.ElementType;
9 | import java.lang.annotation.Retention;
10 | import java.lang.annotation.RetentionPolicy;
11 |
12 | import static asia.kala.llvm.binding.Types.*;
13 | import static asia.kala.llvm.binding.TargetH.*;
14 |
15 | @CInfo(
16 | fileName = "TargetMachine.c",
17 | include = {
18 | "",
19 | "llvm-java.h"
20 | }
21 | )
22 | public final class TargetMachine extends LLVMLoader {
23 | private TargetMachine() {
24 | }
25 |
26 | @Pointer("LLVMTargetMachineRef")
27 | @Documented
28 | @java.lang.annotation.Target(ElementType.TYPE_USE)
29 | @Retention(RetentionPolicy.RUNTIME)
30 | public @interface LLVMTargetMachineRef {
31 | }
32 |
33 | @Pointer("LLVMTargetRef")
34 | @Documented
35 | @java.lang.annotation.Target(ElementType.TYPE_USE)
36 | @Retention(RetentionPolicy.RUNTIME)
37 | public @interface LLVMTargetRef {
38 | }
39 |
40 |
41 | @CEnum("LLVMCodeGenOptLevel")
42 | @Documented
43 | @java.lang.annotation.Target(ElementType.TYPE_USE)
44 | @Retention(RetentionPolicy.RUNTIME)
45 | public @interface LLVMCodeGenOptLevel {
46 | int LLVMCodeGenLevelNone = 0;
47 | int LLVMCodeGenLevelLess = 1;
48 | int LLVMCodeGenLevelDefault = 2;
49 | int LLVMCodeGenLevelAggressive = 3;
50 | }
51 |
52 | @CEnum("LLVMRelocMode")
53 | @Documented
54 | @java.lang.annotation.Target(ElementType.TYPE_USE)
55 | @Retention(RetentionPolicy.RUNTIME)
56 | public @interface LLVMRelocMode {
57 | int LLVMRelocDefault = 0;
58 | int LLVMRelocStatic = 1;
59 | int LLVMRelocPIC = 2;
60 | int LLVMRelocDynamicNoPic = 3;
61 | int LLVMRelocROPI = 4;
62 | int LLVMRelocRWPI = 5;
63 | int LLVMRelocROPI_RWPI = 6;
64 | }
65 |
66 | @CEnum("LLVMCodeModel")
67 | @Documented
68 | @java.lang.annotation.Target(ElementType.TYPE_USE)
69 | @Retention(RetentionPolicy.RUNTIME)
70 | public @interface LLVMCodeModel {
71 | int LLVMCodeModelDefault = 0;
72 | int LLVMCodeModelJITDefault = 1;
73 | int LLVMCodeModelTiny = 2;
74 | int LLVMCodeModelSmall = 3;
75 | int LLVMCodeModelKernel = 4;
76 | int LLVMCodeModelMedium = 5;
77 | int LLVMCodeModelLarge = 6;
78 | }
79 |
80 | @CEnum("LLVMCodeGenFileType")
81 | @Documented
82 | @java.lang.annotation.Target(ElementType.TYPE_USE)
83 | @Retention(RetentionPolicy.RUNTIME)
84 | public @interface LLVMCodeGenFileType {
85 | int LLVMAssemblyFile = 0;
86 | int LLVMObjectFile = 1;
87 | }
88 |
89 | /**
90 | * Returns the first llvm::Target in the registered targets list.
91 | */
92 | public static native @LLVMTargetRef long LLVMGetFirstTarget();
93 |
94 | /**
95 | * Returns the next llvm::Target given a previous one (or null if there's none)
96 | */
97 | public static native @LLVMTargetRef long LLVMGetNextTarget(@LLVMTargetRef long T);
98 |
99 | /*===-- Target ------------------------------------------------------------===*/
100 |
101 | /**
102 | * Finds the target corresponding to the given name and stores it in \p T.
103 | * Returns 0 on success.
104 | */
105 | public static native @LLVMTargetRef long LLVMGetTargetFromName(@Pointer("const char *") long Name);
106 |
107 | /**
108 | * Finds the target corresponding to the given triple and stores it in \p T.
109 | * Returns 0 on success. Optionally returns any error in ErrorMessage.
110 | * Use LLVMDisposeMessage to dispose the message.
111 | */
112 | public static native boolean LLVMGetTargetFromTriple(
113 | @Pointer("const char *") long Triple, @Pointer("LLVMTargetRef *") long T,
114 | @Pointer("char **") long ErrorMessage
115 | );
116 |
117 | /**
118 | * Returns the name of a target. See llvm::Target::getName
119 | */
120 | public static native @Pointer("const char *") long LLVMGetTargetName(@LLVMTargetRef long T);
121 |
122 | /**
123 | * Returns the description of a target. See llvm::Target::getDescription
124 | */
125 | public static native @Pointer("const char *") long LLVMGetTargetDescription(@LLVMTargetRef long T);
126 |
127 | /**
128 | * Returns if the target has a JIT
129 | */
130 | public static native boolean LLVMTargetHasJIT(@LLVMTargetRef long T);
131 |
132 | /**
133 | * Returns if the target has a TargetMachine associated
134 | */
135 | public static native boolean LLVMTargetHasTargetMachine(@LLVMTargetRef long T);
136 |
137 | /**
138 | * Returns if the target as an ASM backend (required for emitting output)
139 | */
140 | public static native boolean LLVMTargetHasAsmBackend(@LLVMTargetRef long T);
141 |
142 | /*===-- Target Machine ----------------------------------------------------===*/
143 |
144 | /**
145 | * Creates a new llvm::TargetMachine. See llvm::Target::createTargetMachine
146 | */
147 | public static native @LLVMTargetMachineRef long LLVMCreateTargetMachine(
148 | @LLVMTargetRef long T,
149 | @Pointer("const char *") long Triple, @Pointer("const char *") long CPU, @Pointer("const char *") long Features,
150 | @LLVMCodeGenOptLevel int Level, @LLVMRelocMode int Reloc, @LLVMCodeModel int CodeModel
151 | );
152 |
153 | /**
154 | * Dispose the LLVMTargetMachineRef instance generated by
155 | * LLVMCreateTargetMachine.
156 | */
157 | public static native void LLVMDisposeTargetMachine(@LLVMTargetMachineRef long T);
158 |
159 | /**
160 | * Returns the Target used in a TargetMachine
161 | */
162 | public static native @LLVMTargetRef long LLVMGetTargetMachineTarget(@LLVMTargetMachineRef long T);
163 |
164 | /**
165 | * Returns the triple used creating this target machine. See
166 | * llvm::TargetMachine::getTriple. The result needs to be disposed with
167 | * LLVMDisposeMessage.
168 | */
169 | public static native @Pointer("char *") long LLVMGetTargetMachineTriple(@LLVMTargetMachineRef long T);
170 |
171 | /**
172 | * Returns the cpu used creating this target machine. See
173 | * llvm::TargetMachine::getCPU. The result needs to be disposed with
174 | * LLVMDisposeMessage.
175 | */
176 | public static native @Pointer("char *") long LLVMGetTargetMachineCPU(@LLVMTargetMachineRef long T);
177 |
178 | /**
179 | * Returns the feature string used creating this target machine. See
180 | * llvm::TargetMachine::getFeatureString. The result needs to be disposed with
181 | * LLVMDisposeMessage.
182 | */
183 | public static native @Pointer("char *") long LLVMGetTargetMachineFeatureString(@LLVMTargetMachineRef long T);
184 |
185 | /**
186 | * Create a DataLayout based on the targetMachine.
187 | */
188 | public static native @LLVMTargetDataRef long LLVMCreateTargetDataLayout(@LLVMTargetMachineRef long T);
189 |
190 | /**
191 | * Set the target machine's ASM verbosity.
192 | */
193 | public static native void LLVMSetTargetMachineAsmVerbosity(@LLVMTargetMachineRef long T,
194 | boolean VerboseAsm);
195 |
196 | /**
197 | * Emits an asm or object file for the given module to the filename. This
198 | * wraps several c++ only classes (among them a file stream). Returns any
199 | * error in ErrorMessage. Use LLVMDisposeMessage to dispose the message.
200 | */
201 | public static native boolean LLVMTargetMachineEmitToFile(
202 | @LLVMTargetMachineRef long T, @LLVMModuleRef long M,
203 | @Pointer("char *") long Filename, @LLVMCodeGenFileType int codegen, @Pointer("char **") long ErrorMessage
204 | );
205 |
206 | /**
207 | * Compile the LLVM IR stored in \p M and store the result in \p OutMemBuf.
208 | */
209 | public static native boolean LLVMTargetMachineEmitToMemoryBuffer(
210 | @LLVMTargetMachineRef long T, @LLVMModuleRef long M,
211 | @LLVMCodeGenFileType int codegen, @Pointer("char**") long ErrorMessage, @Pointer("LLVMMemoryBufferRef *") long OutMemBuf
212 | );
213 |
214 | /*===-- Triple ------------------------------------------------------------===*/
215 |
216 | /**
217 | * Get a triple for the host machine as a string. The result needs to be
218 | * disposed with LLVMDisposeMessage.
219 | */
220 | public static native @Pointer("char*") long LLVMGetDefaultTargetTriple();
221 |
222 | /**
223 | * Normalize a target triple. The result needs to be disposed with
224 | * LLVMDisposeMessage.
225 | */
226 | public static native @Pointer("char*") long LLVMNormalizeTargetTriple(@Pointer("const char *") long triple);
227 |
228 | /**
229 | * Get the host CPU as a string. The result needs to be disposed with
230 | * LLVMDisposeMessage.
231 | */
232 | public static native @Pointer("char*") long LLVMGetHostCPUName();
233 |
234 | /**
235 | * Get the host CPU's features as a string. The result needs to be disposed
236 | * with LLVMDisposeMessage.
237 | */
238 | public static native @Pointer("char*") long LLVMGetHostCPUFeatures();
239 |
240 | /**
241 | * Adds the target-specific analysis passes to the pass manager.
242 | */
243 | public static native void LLVMAddAnalysisPasses(@LLVMTargetMachineRef long T, @LLVMPassManagerRef long PM);
244 | }
245 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/Types.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.Pointer;
4 |
5 | import java.lang.annotation.*;
6 |
7 | public final class Types {
8 | private Types() {
9 | }
10 |
11 | @Pointer("LLVMMemoryBufferRef")
12 | @Documented
13 | @java.lang.annotation.Target(ElementType.TYPE_USE)
14 | @Retention(RetentionPolicy.RUNTIME)
15 | public @interface LLVMMemoryBufferRef {
16 | }
17 |
18 | @Pointer("LLVMContextRef")
19 | @Documented
20 | @java.lang.annotation.Target(ElementType.TYPE_USE)
21 | @Retention(RetentionPolicy.RUNTIME)
22 | public @interface LLVMContextRef {
23 | }
24 |
25 | @Pointer("LLVMModuleRef")
26 | @Documented
27 | @java.lang.annotation.Target(ElementType.TYPE_USE)
28 | @Retention(RetentionPolicy.RUNTIME)
29 | public @interface LLVMModuleRef {
30 | }
31 |
32 | @Pointer("LLVMTypeRef")
33 | @Documented
34 | @java.lang.annotation.Target(ElementType.TYPE_USE)
35 | @Retention(RetentionPolicy.RUNTIME)
36 | public @interface LLVMTypeRef {
37 | }
38 |
39 | @Pointer("LLVMValueRef")
40 | @Documented
41 | @java.lang.annotation.Target(ElementType.TYPE_USE)
42 | @Retention(RetentionPolicy.RUNTIME)
43 | public @interface LLVMValueRef {
44 | }
45 |
46 | @Pointer("LLVMBasicBlockRef")
47 | @Documented
48 | @java.lang.annotation.Target(ElementType.TYPE_USE)
49 | @Retention(RetentionPolicy.RUNTIME)
50 | public @interface LLVMBasicBlockRef {
51 | }
52 |
53 | @Pointer("LLVMMetadataRef")
54 | @Documented
55 | @java.lang.annotation.Target(ElementType.TYPE_USE)
56 | @Retention(RetentionPolicy.RUNTIME)
57 | public @interface LLVMMetadataRef {
58 | }
59 |
60 | @Pointer("LLVMNamedMDNodeRef")
61 | @Documented
62 | @java.lang.annotation.Target(ElementType.TYPE_USE)
63 | @Retention(RetentionPolicy.RUNTIME)
64 | public @interface LLVMNamedMDNodeRef {
65 | }
66 |
67 | @Pointer("LLVMValueMetadataEntry")
68 | @Documented
69 | @java.lang.annotation.Target(ElementType.TYPE_USE)
70 | @Retention(RetentionPolicy.RUNTIME)
71 | public @interface LLVMValueMetadataEntry {
72 | }
73 |
74 | @Pointer("LLVMBuilderRef")
75 | @Documented
76 | @java.lang.annotation.Target(ElementType.TYPE_USE)
77 | @Retention(RetentionPolicy.RUNTIME)
78 | public @interface LLVMBuilderRef {
79 | }
80 |
81 | @Pointer("LLVMDIBuilderRef")
82 | @Documented
83 | @java.lang.annotation.Target(ElementType.TYPE_USE)
84 | @Retention(RetentionPolicy.RUNTIME)
85 | public @interface LLVMDIBuilderRef {
86 | }
87 |
88 | @Pointer("LLVMModuleProviderRef")
89 | @Documented
90 | @java.lang.annotation.Target(ElementType.TYPE_USE)
91 | @Retention(RetentionPolicy.RUNTIME)
92 | public @interface LLVMModuleProviderRef {
93 | }
94 |
95 | @Pointer("LLVMPassManagerRef")
96 | @Documented
97 | @java.lang.annotation.Target(ElementType.TYPE_USE)
98 | @Retention(RetentionPolicy.RUNTIME)
99 | public @interface LLVMPassManagerRef {
100 | }
101 |
102 | @Pointer("LLVMPassRegistryRef")
103 | @Documented
104 | @java.lang.annotation.Target(ElementType.TYPE_USE)
105 | @Retention(RetentionPolicy.RUNTIME)
106 | public @interface LLVMPassRegistryRef {
107 | }
108 |
109 | @Pointer("LLVMUseRef")
110 | @Documented
111 | @java.lang.annotation.Target(ElementType.TYPE_USE)
112 | @Retention(RetentionPolicy.RUNTIME)
113 | public @interface LLVMUseRef {
114 | }
115 |
116 | @Pointer("LLVMAttributeRef")
117 | @Documented
118 | @java.lang.annotation.Target(ElementType.TYPE_USE)
119 | @Retention(RetentionPolicy.RUNTIME)
120 | public @interface LLVMAttributeRef {
121 | }
122 |
123 | @Pointer("LLVMDiagnosticInfoRef")
124 | @Documented
125 | @java.lang.annotation.Target(ElementType.TYPE_USE)
126 | @Retention(RetentionPolicy.RUNTIME)
127 | public @interface LLVMDiagnosticInfoRef {
128 | }
129 |
130 | @Pointer("LLVMComdatRef")
131 | @Documented
132 | @java.lang.annotation.Target(ElementType.TYPE_USE)
133 | @Retention(RetentionPolicy.RUNTIME)
134 | public @interface LLVMComdatRef {
135 | }
136 |
137 | @Pointer("LLVMModuleFlagEntry *")
138 | @Documented
139 | @java.lang.annotation.Target(ElementType.TYPE_USE)
140 | @Retention(RetentionPolicy.RUNTIME)
141 | public @interface LLVMModuleFlagEntryRef {
142 | }
143 |
144 | @Pointer("LLVMJITEventListenerRef")
145 | @Documented
146 | @java.lang.annotation.Target(ElementType.TYPE_USE)
147 | @Retention(RetentionPolicy.RUNTIME)
148 | public @interface LLVMJITEventListenerRef {
149 | }
150 |
151 | @Pointer("LLVMBinaryRef")
152 | @Documented
153 | @java.lang.annotation.Target(ElementType.TYPE_USE)
154 | @Retention(RetentionPolicy.RUNTIME)
155 | public @interface LLVMBinaryRef {
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/annotations/CEnum.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.annotations;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Documented
6 | @java.lang.annotation.Target({ElementType.TYPE_USE, ElementType.ANNOTATION_TYPE})
7 | @Retention(RetentionPolicy.RUNTIME)
8 | public @interface CEnum {
9 | String value();
10 | }
11 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/annotations/CInfo.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.annotations;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Documented
6 | @java.lang.annotation.Target(ElementType.TYPE)
7 | @Retention(RetentionPolicy.RUNTIME)
8 | public @interface CInfo {
9 | String fileName();
10 |
11 | String[] include();
12 | }
13 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/annotations/Extern.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.annotations;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Documented
6 | @java.lang.annotation.Target(ElementType.METHOD)
7 | @Retention(RetentionPolicy.RUNTIME)
8 | public @interface Extern {
9 | }
10 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/annotations/LongLong.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.annotations;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Signed("long long")
6 | @Documented
7 | @java.lang.annotation.Target(ElementType.TYPE_USE)
8 | @Retention(RetentionPolicy.RUNTIME)
9 | public @interface LongLong {
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/annotations/Pointer.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.annotations;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Documented
6 | @java.lang.annotation.Target({ElementType.TYPE_USE, ElementType.ANNOTATION_TYPE})
7 | @Retention(RetentionPolicy.RUNTIME)
8 | public @interface Pointer {
9 | String value();
10 | }
11 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/annotations/Signed.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.annotations;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Documented
6 | @java.lang.annotation.Target({ElementType.TYPE_USE, ElementType.ANNOTATION_TYPE})
7 | @Retention(RetentionPolicy.RUNTIME)
8 | public @interface Signed {
9 | String value();
10 | }
11 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/annotations/SizeT.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.annotations;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Unsigned("size_t")
6 | @Documented
7 | @java.lang.annotation.Target(ElementType.TYPE_USE)
8 | @Retention(RetentionPolicy.RUNTIME)
9 | public @interface SizeT {
10 | }
11 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/annotations/Statement.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.annotations;
2 |
3 | import java.lang.annotation.Documented;
4 | import java.lang.annotation.ElementType;
5 | import java.lang.annotation.Retention;
6 | import java.lang.annotation.RetentionPolicy;
7 |
8 | @Documented
9 | @java.lang.annotation.Target(ElementType.METHOD)
10 | @Retention(RetentionPolicy.RUNTIME)
11 | public @interface Statement {
12 | String value();
13 | }
14 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/annotations/Unsigned.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.annotations;
2 |
3 | import java.lang.annotation.*;
4 |
5 | @Documented
6 | @java.lang.annotation.Target({ElementType.TYPE_USE, ElementType.ANNOTATION_TYPE})
7 | @Retention(RetentionPolicy.RUNTIME)
8 | public @interface Unsigned {
9 | String value() default "unsigned";
10 | }
11 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/lto.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding;
2 |
3 | import asia.kala.llvm.binding.annotations.*;
4 |
5 | import java.lang.annotation.Documented;
6 | import java.lang.annotation.ElementType;
7 | import java.lang.annotation.Retention;
8 | import java.lang.annotation.RetentionPolicy;
9 |
10 | import static asia.kala.llvm.binding.Types.LLVMMemoryBufferRef;
11 | import static asia.kala.llvm.binding.Types.LLVMModuleRef;
12 |
13 | @CInfo(
14 | fileName = "lto.c",
15 | include = {
16 | "",
17 | "llvm-java.h"
18 | }
19 | )
20 | public final class lto extends LLVMLoader {
21 | private lto() {
22 | }
23 |
24 | @Documented
25 | @java.lang.annotation.Target(ElementType.TYPE_USE)
26 | @Retention(RetentionPolicy.RUNTIME)
27 | public @interface lto_bool_t {
28 | }
29 |
30 | public static final int LTO_API_VERSION = LTO_API_VERSION();
31 |
32 | @Statement("return LTO_API_VERSION;")
33 | private static native int LTO_API_VERSION();
34 |
35 | /**
36 | * @since LTO_API_VERSION 3
37 | */
38 | @CEnum("lto_symbol_attributes")
39 | @Documented
40 | @java.lang.annotation.Target(ElementType.TYPE_USE)
41 | @Retention(RetentionPolicy.RUNTIME)
42 | public @interface lto_symbol_attributes {
43 | int
44 | LTO_SYMBOL_ALIGNMENT_MASK = 0x0000001F, /* log2 of alignment */
45 | LTO_SYMBOL_PERMISSIONS_MASK = 0x000000E0,
46 | LTO_SYMBOL_PERMISSIONS_CODE = 0x000000A0,
47 | LTO_SYMBOL_PERMISSIONS_DATA = 0x000000C0,
48 | LTO_SYMBOL_PERMISSIONS_RODATA = 0x00000080,
49 | LTO_SYMBOL_DEFINITION_MASK = 0x00000700,
50 | LTO_SYMBOL_DEFINITION_REGULAR = 0x00000100,
51 | LTO_SYMBOL_DEFINITION_TENTATIVE = 0x00000200,
52 | LTO_SYMBOL_DEFINITION_WEAK = 0x00000300,
53 | LTO_SYMBOL_DEFINITION_UNDEFINED = 0x00000400,
54 | LTO_SYMBOL_DEFINITION_WEAKUNDEF = 0x00000500,
55 | LTO_SYMBOL_SCOPE_MASK = 0x00003800,
56 | LTO_SYMBOL_SCOPE_INTERNAL = 0x00000800,
57 | LTO_SYMBOL_SCOPE_HIDDEN = 0x00001000,
58 | LTO_SYMBOL_SCOPE_PROTECTED = 0x00002000,
59 | LTO_SYMBOL_SCOPE_DEFAULT = 0x00001800,
60 | LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN = 0x00002800,
61 | LTO_SYMBOL_COMDAT = 0x00004000,
62 | LTO_SYMBOL_ALIAS = 0x00008000;
63 | }
64 |
65 | @CEnum("lto_debug_model")
66 | @Documented
67 | @java.lang.annotation.Target(ElementType.TYPE_USE)
68 | @Retention(RetentionPolicy.RUNTIME)
69 | public @interface lto_debug_model {
70 | int LTO_DEBUG_MODEL_NONE = 0;
71 | int LTO_DEBUG_MODEL_DWARF = 1;
72 | }
73 |
74 | @CEnum("lto_codegen_model")
75 | @Documented
76 | @java.lang.annotation.Target(ElementType.TYPE_USE)
77 | @Retention(RetentionPolicy.RUNTIME)
78 | public @interface lto_codegen_model {
79 | int
80 | LTO_CODEGEN_PIC_MODEL_STATIC = 0,
81 | LTO_CODEGEN_PIC_MODEL_DYNAMIC = 1,
82 | LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC = 2,
83 | LTO_CODEGEN_PIC_MODEL_DEFAULT = 3;
84 | }
85 |
86 | @Pointer("lto_module_t")
87 | @Documented
88 | @java.lang.annotation.Target(ElementType.TYPE_USE)
89 | @Retention(RetentionPolicy.RUNTIME)
90 | public @interface lto_module_t {
91 | }
92 |
93 | @Pointer("lto_code_gen_t")
94 | @Documented
95 | @java.lang.annotation.Target(ElementType.TYPE_USE)
96 | @Retention(RetentionPolicy.RUNTIME)
97 | public @interface lto_code_gen_t {
98 | }
99 |
100 | @Pointer("thinlto_code_gen_t")
101 | @Documented
102 | @java.lang.annotation.Target(ElementType.TYPE_USE)
103 | @Retention(RetentionPolicy.RUNTIME)
104 | public @interface thinlto_code_gen_t {
105 | }
106 |
107 | // TODO
108 | }
109 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/transforms/AggressiveInstCombine.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.transforms;
2 |
3 | import asia.kala.llvm.binding.LLVMLoader;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 |
6 | import static asia.kala.llvm.binding.Types.*;
7 |
8 | @CInfo(
9 | fileName = "Transforms/AggressiveInstCombine.c",
10 | include = {
11 | "",
12 | "../llvm-java.h"
13 | }
14 | )
15 | public final class AggressiveInstCombine extends LLVMLoader {
16 | private AggressiveInstCombine() {
17 | }
18 |
19 | public static native void LLVMAddAggressiveInstCombinerPass(@LLVMPassManagerRef long PM);
20 | }
21 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/transforms/Coroutines.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.transforms;
2 |
3 | import asia.kala.llvm.binding.LLVMLoader;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 |
6 | import static asia.kala.llvm.binding.Types.*;
7 |
8 | @CInfo(
9 | fileName = "Transforms/Coroutines.c",
10 | include = {
11 | "",
12 | "../llvm-java.h"
13 | }
14 | )
15 | public final class Coroutines extends LLVMLoader {
16 | private Coroutines() {
17 | }
18 |
19 | public static native void LLVMAddCoroEarlyPass(@LLVMPassManagerRef long PM);
20 |
21 | public static native void LLVMAddCoroSplitPass(@LLVMPassManagerRef long PM);
22 |
23 | public static native void LLVMAddCoroElidePass(@LLVMPassManagerRef long PM);
24 |
25 | public static native void LLVMAddCoroCleanupPass(@LLVMPassManagerRef long PM);
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/transforms/IPO.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.transforms;
2 |
3 | import asia.kala.llvm.binding.LLVMLoader;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 | import asia.kala.llvm.binding.annotations.Pointer;
6 | import asia.kala.llvm.binding.annotations.Unsigned;
7 |
8 | import static asia.kala.llvm.binding.Types.*;
9 |
10 | @CInfo(
11 | fileName = "Transforms/IPO.c",
12 | include = {
13 | "",
14 | "../llvm-java.h"
15 | }
16 | )
17 | public final class IPO extends LLVMLoader {
18 | private IPO() {
19 | }
20 |
21 | public static native void LLVMAddArgumentPromotionPass(@LLVMPassManagerRef long PM);
22 |
23 | public static native void LLVMAddConstantMergePass(@LLVMPassManagerRef long PM);
24 |
25 | public static native void LLVMAddMergeFunctionsPass(@LLVMPassManagerRef long PM);
26 |
27 | public static native void LLVMAddCalledValuePropagationPass(@LLVMPassManagerRef long PM);
28 |
29 | public static native void LLVMAddDeadArgEliminationPass(@LLVMPassManagerRef long PM);
30 |
31 | public static native void LLVMAddFunctionAttrsPass(@LLVMPassManagerRef long PM);
32 |
33 | public static native void LLVMAddFunctionInliningPass(@LLVMPassManagerRef long PM);
34 |
35 | public static native void LLVMAddAlwaysInlinerPass(@LLVMPassManagerRef long PM);
36 |
37 | public static native void LLVMAddGlobalDCEPass(@LLVMPassManagerRef long PM);
38 |
39 | public static native void LLVMAddGlobalOptimizerPass(@LLVMPassManagerRef long PM);
40 |
41 | public static native void LLVMAddIPConstantPropagationPass(@LLVMPassManagerRef long PM);
42 |
43 | public static native void LLVMAddPruneEHPass(@LLVMPassManagerRef long PM);
44 |
45 | public static native void LLVMAddIPSCCPPass(@LLVMPassManagerRef long PM);
46 |
47 | public static native void LLVMAddInternalizePass(@LLVMPassManagerRef long PM, @Unsigned int AllButMain);
48 |
49 | /**
50 | * Create and add the internalize pass to the given pass manager with the
51 | * provided preservation callback.
52 | *
53 | * The context parameter is forwarded to the callback on each invocation.
54 | * As such, it is the responsibility of the caller to extend its lifetime
55 | * until execution of this pass has finished.
56 | */
57 | public static native void LLVMAddInternalizePassWithMustPreservePredicate(
58 | @LLVMPassManagerRef long PM,
59 | @Pointer("void *") long Context,
60 | @Pointer("LLVMBool (*)(LLVMValueRef, void *)") long MustPreserve
61 | );
62 |
63 | public static native void LLVMAddStripDeadPrototypesPass(@LLVMPassManagerRef long PM);
64 |
65 | public static native void LLVMAddStripSymbolsPass(@LLVMPassManagerRef long PM);
66 | }
67 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/transforms/InstCombine.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.transforms;
2 |
3 | import asia.kala.llvm.binding.LLVMLoader;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 |
6 | import static asia.kala.llvm.binding.Types.LLVMPassManagerRef;
7 |
8 | @CInfo(
9 | fileName = "Transforms/InstCombine.c",
10 | include = {
11 | "",
12 | "../llvm-java.h"
13 | }
14 | )
15 | public final class InstCombine extends LLVMLoader {
16 | private InstCombine() {
17 | }
18 |
19 | public static native void LLVMAddInstructionCombiningPass(@LLVMPassManagerRef long PM);
20 | }
21 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/transforms/PassManagerBuilder.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.transforms;
2 |
3 | import asia.kala.llvm.binding.LLVMLoader;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 | import asia.kala.llvm.binding.annotations.Pointer;
6 | import asia.kala.llvm.binding.annotations.Unsigned;
7 |
8 | import java.lang.annotation.*;
9 |
10 | import static asia.kala.llvm.binding.Types.LLVMPassManagerRef;
11 |
12 | @CInfo(
13 | fileName = "Transforms/PassManagerBuilder.c",
14 | include = {
15 | "",
16 | "../llvm-java.h"
17 | }
18 | )
19 | public final class PassManagerBuilder extends LLVMLoader {
20 | private PassManagerBuilder() {
21 | }
22 |
23 | @Pointer("LLVMPassManagerBuilderRef")
24 | @Documented
25 | @Target(ElementType.TYPE_USE)
26 | @Retention(RetentionPolicy.RUNTIME)
27 | public @interface LLVMPassManagerBuilderRef {
28 | }
29 |
30 | public static native @LLVMPassManagerBuilderRef long LLVMPassManagerBuilderCreate();
31 |
32 | public static native void LLVMPassManagerBuilderDispose(@LLVMPassManagerBuilderRef long PMB);
33 |
34 | public static native void LLVMPassManagerBuilderSetOptLevel(
35 | @LLVMPassManagerBuilderRef long PMB, @Unsigned int OptLevel
36 | );
37 |
38 | public static native void LLVMPassManagerBuilderSetSizeLevel(
39 | @LLVMPassManagerBuilderRef long PMB, @Unsigned int SizeLevel
40 | );
41 |
42 | public static native void LLVMPassManagerBuilderSetDisableUnitAtATime(
43 | @LLVMPassManagerBuilderRef long PMB, boolean Value
44 | );
45 |
46 | public static native void LLVMPassManagerBuilderSetDisableUnrollLoops(
47 | @LLVMPassManagerBuilderRef long PMB, boolean Value
48 | );
49 |
50 | public static native void LLVMPassManagerBuilderSetDisableSimplifyLibCalls(
51 | @LLVMPassManagerBuilderRef long PMB, boolean Value
52 | );
53 |
54 | public static native void LLVMPassManagerBuilderUseInlinerWithThreshold(
55 | @LLVMPassManagerBuilderRef long PMB, @Unsigned int Threshold
56 | );
57 |
58 | public static native void LLVMPassManagerBuilderPopulateFunctionPassManager(
59 | @LLVMPassManagerBuilderRef long PMB, @LLVMPassManagerRef long PM
60 | );
61 |
62 | public static native void LLVMPassManagerBuilderPopulateModulePassManager(
63 | @LLVMPassManagerBuilderRef long PMB, @LLVMPassManagerRef long PM
64 | );
65 |
66 | public static native void LLVMPassManagerBuilderPopulateLTOPassManager(
67 | @LLVMPassManagerBuilderRef long PMB, @LLVMPassManagerRef long PM,
68 | boolean Internalize, boolean RunInliner
69 | );
70 | }
71 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/transforms/Scalar.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.transforms;
2 |
3 | import asia.kala.llvm.binding.LLVMLoader;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 | import asia.kala.llvm.binding.annotations.Signed;
6 |
7 | import static asia.kala.llvm.binding.Types.*;
8 |
9 | @CInfo(
10 | fileName = "Transforms/Scalar.c",
11 | include = {
12 | "",
13 | "../llvm-java.h"
14 | }
15 | )
16 | public final class Scalar extends LLVMLoader {
17 | private Scalar() {
18 | }
19 |
20 | public static native void LLVMAddAggressiveDCEPass(@LLVMPassManagerRef long PM);
21 |
22 | public static native void LLVMAddDCEPass(@LLVMPassManagerRef long PM);
23 |
24 | public static native void LLVMAddBitTrackingDCEPass(@LLVMPassManagerRef long PM);
25 |
26 | public static native void LLVMAddAlignmentFromAssumptionsPass(@LLVMPassManagerRef long PM);
27 |
28 | public static native void LLVMAddCFGSimplificationPass(@LLVMPassManagerRef long PM);
29 |
30 | public static native void LLVMAddDeadStoreEliminationPass(@LLVMPassManagerRef long PM);
31 |
32 | public static native void LLVMAddScalarizerPass(@LLVMPassManagerRef long PM);
33 |
34 | public static native void LLVMAddMergedLoadStoreMotionPass(@LLVMPassManagerRef long PM);
35 |
36 | public static native void LLVMAddGVNPass(@LLVMPassManagerRef long PM);
37 |
38 | public static native void LLVMAddNewGVNPass(@LLVMPassManagerRef long PM);
39 |
40 | public static native void LLVMAddIndVarSimplifyPass(@LLVMPassManagerRef long PM);
41 |
42 | public static native void LLVMAddInstructionCombiningPass(@LLVMPassManagerRef long PM);
43 |
44 | public static native void LLVMAddJumpThreadingPass(@LLVMPassManagerRef long PM);
45 |
46 | public static native void LLVMAddLICMPass(@LLVMPassManagerRef long PM);
47 |
48 | public static native void LLVMAddLoopDeletionPass(@LLVMPassManagerRef long PM);
49 |
50 | public static native void LLVMAddLoopIdiomPass(@LLVMPassManagerRef long PM);
51 |
52 | public static native void LLVMAddLoopRotatePass(@LLVMPassManagerRef long PM);
53 |
54 | public static native void LLVMAddLoopRerollPass(@LLVMPassManagerRef long PM);
55 |
56 | public static native void LLVMAddLoopUnrollPass(@LLVMPassManagerRef long PM);
57 |
58 | public static native void LLVMAddLoopUnrollAndJamPass(@LLVMPassManagerRef long PM);
59 |
60 | public static native void LLVMAddLoopUnswitchPass(@LLVMPassManagerRef long PM);
61 |
62 | public static native void LLVMAddLowerAtomicPass(@LLVMPassManagerRef long PM);
63 |
64 | public static native void LLVMAddMemCpyOptPass(@LLVMPassManagerRef long PM);
65 |
66 | public static native void LLVMAddPartiallyInlineLibCallsPass(@LLVMPassManagerRef long PM);
67 |
68 | public static native void LLVMAddReassociatePass(@LLVMPassManagerRef long PM);
69 |
70 | public static native void LLVMAddSCCPPass(@LLVMPassManagerRef long PM);
71 |
72 | public static native void LLVMAddScalarReplAggregatesPass(@LLVMPassManagerRef long PM);
73 |
74 | public static native void LLVMAddScalarReplAggregatesPassSSA(@LLVMPassManagerRef long PM);
75 |
76 | public static native void LLVMAddScalarReplAggregatesPassWithThreshold(
77 | @LLVMPassManagerRef long PM, @Signed("int") int Threshold
78 | );
79 |
80 | public static native void LLVMAddSimplifyLibCallsPass(@LLVMPassManagerRef long PM);
81 |
82 | public static native void LLVMAddTailCallEliminationPass(@LLVMPassManagerRef long PM);
83 |
84 | public static native void LLVMAddConstantPropagationPass(@LLVMPassManagerRef long PM);
85 |
86 | public static native void LLVMAddDemoteMemoryToRegisterPass(@LLVMPassManagerRef long PM);
87 |
88 | public static native void LLVMAddVerifierPass(@LLVMPassManagerRef long PM);
89 |
90 | public static native void LLVMAddCorrelatedValuePropagationPass(@LLVMPassManagerRef long PM);
91 |
92 | public static native void LLVMAddEarlyCSEPass(@LLVMPassManagerRef long PM);
93 |
94 | public static native void LLVMAddEarlyCSEMemSSAPass(@LLVMPassManagerRef long PM);
95 |
96 | public static native void LLVMAddLowerExpectIntrinsicPass(@LLVMPassManagerRef long PM);
97 |
98 | public static native void LLVMAddLowerConstantIntrinsicsPass(@LLVMPassManagerRef long PM);
99 |
100 | public static native void LLVMAddTypeBasedAliasAnalysisPass(@LLVMPassManagerRef long PM);
101 |
102 | public static native void LLVMAddScopedNoAliasAAPass(@LLVMPassManagerRef long PM);
103 |
104 | public static native void LLVMAddBasicAliasAnalysisPass(@LLVMPassManagerRef long PM);
105 |
106 | public static native void LLVMAddUnifyFunctionExitNodesPass(@LLVMPassManagerRef long PM);
107 |
108 | }
109 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/transforms/Utils.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.transforms;
2 |
3 | import asia.kala.llvm.binding.LLVMLoader;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 |
6 | import static asia.kala.llvm.binding.Types.LLVMPassManagerRef;
7 |
8 | @CInfo(
9 | fileName = "Transforms/Utils.c",
10 | include = {
11 | "",
12 | "../llvm-java.h"
13 | }
14 | )
15 | public final class Utils extends LLVMLoader {
16 | private Utils() {
17 | }
18 |
19 | public static native void LLVMAddLowerSwitchPass(@LLVMPassManagerRef long PM);
20 |
21 | public static native void LLVMAddPromoteMemoryToRegisterPass(@LLVMPassManagerRef long PM);
22 |
23 | public static native void LLVMAddAddDiscriminatorsPass(@LLVMPassManagerRef long PM);
24 | }
25 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/asia/kala/llvm/binding/transforms/Vectorize.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.binding.transforms;
2 |
3 | import asia.kala.llvm.binding.LLVMLoader;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 |
6 | import static asia.kala.llvm.binding.Types.LLVMPassManagerRef;
7 |
8 | @CInfo(
9 | fileName = "Transforms/Vectorize.c",
10 | include = {
11 | "",
12 | "../llvm-java.h"
13 | }
14 | )
15 | public final class Vectorize extends LLVMLoader {
16 | private Vectorize() {
17 | }
18 |
19 | public static native void LLVMAddLoopVectorizePass(@LLVMPassManagerRef long PM);
20 |
21 | public static native void LLVMAddSLPVectorizePass(@LLVMPassManagerRef long PM);
22 | }
23 |
--------------------------------------------------------------------------------
/llvm-binding/src/main/java/module-info.java:
--------------------------------------------------------------------------------
1 | module asia.kala.llvm.binding {
2 | exports asia.kala.llvm.binding;
3 | exports asia.kala.llvm.binding.transforms;
4 | exports asia.kala.llvm.binding.annotations;
5 | }
--------------------------------------------------------------------------------
/llvm-platform/build.gradle:
--------------------------------------------------------------------------------
1 | def platforms = ['windows-x86_64', 'windows-x86', 'linux-x86_64']
2 |
3 | def platformTasks = platforms.collect { platform ->
4 | (Jar) tasks.create(name: "$platform-jar", type: Jar, group: 'llvm platform jars') {
5 | from "$platform/module-info.class"
6 |
7 | into("asia/kala/llvm/platform/$platform") {
8 | from "${rootProject.rootDir}/native/$platform"
9 | include '*.dll'
10 | include '*.so'
11 | include '*.dylib'
12 | }
13 | classifier platform
14 | }
15 | }
16 |
17 | platformTasks.each { jar.dependsOn(it) }
18 |
19 | task showLibrariesPath {
20 | doLast {
21 | println(
22 | (rootProject.allprojects.collect { it.jar.outputs.files.getFiles() } +
23 | platformTasks.collect { it.outputs.files.getFiles() }).flatten().join(File.pathSeparator))
24 | }
25 | }
26 |
27 | publishing {
28 | publications {
29 | Kala(MavenPublication) {
30 | platformTasks.each {
31 | artifact it
32 | }
33 | /*
34 | pom.withXml {
35 | Node des = asNode().appendNode('dependencies')
36 | platformTasks.each {
37 | Node n = new Node(des, 'dependency')
38 | n.appendNode('groupId', group)
39 | n.appendNode('artifactId', project.name)
40 | n.appendNode('version', project.version)
41 | n.appendNode('classifier', it.archiveClassifier.get())
42 | n.appendNode('scope', 'compile')
43 | }
44 | }
45 | */
46 | }
47 | }
48 | }
--------------------------------------------------------------------------------
/llvm-platform/linux-x86_64/module-info.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Glavo/llvm-java/e2c39ea42882b6cd42b7c20f9af8b5ec66cf31d3/llvm-platform/linux-x86_64/module-info.class
--------------------------------------------------------------------------------
/llvm-platform/src/main/java/module-info.java:
--------------------------------------------------------------------------------
1 | module asia.kala.llvm.platform {
2 | }
--------------------------------------------------------------------------------
/llvm-platform/windows-x86/module-info.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Glavo/llvm-java/e2c39ea42882b6cd42b7c20f9af8b5ec66cf31d3/llvm-platform/windows-x86/module-info.class
--------------------------------------------------------------------------------
/llvm-platform/windows-x86_64/module-info.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Glavo/llvm-java/e2c39ea42882b6cd42b7c20f9af8b5ec66cf31d3/llvm-platform/windows-x86_64/module-info.class
--------------------------------------------------------------------------------
/native-generator/README.md:
--------------------------------------------------------------------------------
1 | ## native-generator
2 |
3 | `native-generator` 模块用于自动读取 Java 注解生成对应的 JNI 代码。
4 |
5 | 执行 `./gradlew generateNative` 会在项目根路径下生成 `native` 文件夹,
6 | 其中存放着全部 JNI 代码以及用于配置项目的 `CMakeLists.txt` 配置文件,
7 | 可以使用 CMake 构建生成对应的本机库文件。
8 |
9 | 注意:LLVM Java Binding 需要静态链接 LLVM,Windows 上还需要静态链接 vcruntime。
10 | 需要在 Windows 上构建能够静态链接 vcruntime 的 LLVM,请参见 [build-note.md](../build-note.md)。
11 |
--------------------------------------------------------------------------------
/native-generator/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'application'
3 | }
4 |
5 | dependencies {
6 | implementation project(':llvm-binding')
7 | }
8 |
9 | application {
10 | mainClassName = 'asia.kala.llvm.generator.Main'
11 | executableDir = 'build'
12 | }
13 |
14 | task generateNative(dependsOn: run, group: 'generate native') {
15 | doLast {
16 | copy {
17 | from 'src/main/resources'
18 | into '../native'
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/native-generator/src/main/java/asia/kala/llvm/generator/Main.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.generator;
2 |
3 | import asia.kala.llvm.binding.*;
4 | import asia.kala.llvm.binding.annotations.CInfo;
5 | import asia.kala.llvm.binding.annotations.Extern;
6 | import asia.kala.llvm.binding.annotations.Statement;
7 | import asia.kala.llvm.binding.transforms.*;
8 |
9 | import java.io.BufferedWriter;
10 | import java.io.IOException;
11 | import java.lang.reflect.Method;
12 | import java.lang.reflect.Modifier;
13 | import java.lang.reflect.Parameter;
14 | import java.nio.file.Files;
15 | import java.nio.file.Path;
16 | import java.nio.file.Paths;
17 |
18 | public class Main {
19 | public static final Class>[] classes = {
20 | Core.class, Support.class, ErrorH.class, ObjectH.class, Analysis.class, BitReader.class,
21 | BitWriter.class, Comdat.class, DebugInfo.class, Disassembler.class, ErrorHandling.class,
22 | TargetH.class, TargetMachine.class, ExecutionEngine.class, Initialization.class,
23 | IRReader.class, Linker.class, lto.class, OrcBindings.class, Remarks.class,
24 | AggressiveInstCombine.class, Coroutines.class, InstCombine.class, IPO.class,
25 | PassManagerBuilder.class, Scalar.class, Utils.class, Vectorize.class
26 | };
27 |
28 | public static void main(String[] args) throws IOException {
29 | Path p = Paths.get("..", "native");
30 | Files.createDirectories(p.resolve("Transforms"));
31 |
32 | for (Class> cls : classes) {
33 | CInfo info = cls.getAnnotation(CInfo.class);
34 | try (BufferedWriter writer = Files.newBufferedWriter(p.resolve(info.fileName()))) {
35 | for (String include : info.include()) {
36 | if (include.startsWith("<") && include.endsWith(">")) {
37 | writer.append("#include ").append(include).append('\n');
38 | } else {
39 | writer.append("#include ").append('"').append(include).append('"').append('\n');
40 | }
41 | }
42 |
43 | writer.append('\n');
44 |
45 | writer.append("#ifdef __cplusplus\n")
46 | .append("extern \"C\" {\n")
47 | .append("#endif\n")
48 | .append('\n');
49 |
50 |
51 | for (Method method : cls.getDeclaredMethods()) {
52 | if (!Modifier.isNative(method.getModifiers()) || method.getAnnotation(Extern.class) != null) {
53 | continue;
54 | }
55 | try {
56 | Parameter[] parameters = method.getParameters();
57 | TypeMapper retType = TypeMapper.of(method.getAnnotatedReturnType());
58 |
59 | TypeMapper[] parTypes = new TypeMapper[parameters.length];
60 |
61 | writer.append("JNIEXPORT ").append(retType.jniType).append(" JNICALL ")
62 | .append(methodName(method))
63 | .append('(');
64 |
65 | writer.append("JNIEnv *env, jclass jc");
66 |
67 | for (int i = 0; i < parameters.length; i++) {
68 | TypeMapper t = parTypes[i] = TypeMapper.of(parameters[i].getAnnotatedType());
69 | writer.append(", ").append(parTypes[i].jniType).append(' ').append(parameters[i].getName());
70 | }
71 |
72 | writer.append(") {\n");
73 |
74 | Statement statement = method.getAnnotation(Statement.class);
75 | if (statement == null) {
76 |
77 | StringBuilder builder = new StringBuilder();
78 | for (int i = 0; i < parameters.length; i++) {
79 | TypeMapper t = TypeMapper.of(parameters[i].getAnnotatedType());
80 | if (i != 0) {
81 | builder.append(", ");
82 | }
83 |
84 | builder.append(t.castToC(parameters[i].getName()));
85 | }
86 |
87 | if (retType == TypeMapper.OfVoid) {
88 | writer.append(" ").append(method.getName()).append('(').append(builder).append(')');
89 | } else {
90 | writer.append(" return ")
91 | .append(retType.castToJava(method.getName() + "(" + builder.toString() + ")"));
92 | }
93 |
94 | writer.append(";\n");
95 | } else {
96 | writer.append(" ").append(statement.value()).append('\n');
97 | }
98 | writer.append("}\n\n");
99 | } catch (AssertionError e) {
100 | System.err.println("method: " + method);
101 | throw e;
102 | }
103 |
104 | }
105 |
106 |
107 | writer.append("#ifdef __cplusplus\n")
108 | .append("}\n")
109 | .append("#endif\n");
110 | }
111 | }
112 | }
113 |
114 | public static String methodName(Method method) {
115 | String name = method.getName();
116 | Class> cls = method.getDeclaringClass();
117 | Method[] methods = cls.getDeclaredMethods();
118 |
119 | int c = 0;
120 |
121 | for (Method m : methods) {
122 | if (m.getName().equals(name) && Modifier.isNative(m.getModifiers())) {
123 | ++c;
124 | }
125 | }
126 |
127 | StringBuilder n = new StringBuilder("Java_" + cls.getName().replace('.', '_') + '_' + name);
128 | if (c > 1) {
129 | n.append("__");
130 | for (Parameter parameter : method.getParameters()) {
131 | Class> type = parameter.getType();
132 | if (type == boolean.class) {
133 | n.append('Z');
134 | } else if (type == char.class) {
135 | n.append('C');
136 | } else if (type == byte.class) {
137 | n.append('B');
138 | } else if (type == short.class) {
139 | n.append('S');
140 | } else if (type == int.class) {
141 | n.append('I');
142 | } else if (type == long.class) {
143 | n.append('J');
144 | } else {
145 | throw new AssertionError(parameter);
146 | }
147 | }
148 | }
149 | return n.toString();
150 | }
151 | }
152 |
--------------------------------------------------------------------------------
/native-generator/src/main/java/asia/kala/llvm/generator/TypeMapper.java:
--------------------------------------------------------------------------------
1 | package asia.kala.llvm.generator;
2 |
3 | import asia.kala.llvm.binding.annotations.CEnum;
4 | import asia.kala.llvm.binding.annotations.Pointer;
5 | import asia.kala.llvm.binding.annotations.Signed;
6 | import asia.kala.llvm.binding.annotations.Unsigned;
7 | import asia.kala.llvm.binding.lto;
8 |
9 | import java.lang.annotation.Annotation;
10 | import java.lang.reflect.AnnotatedType;
11 |
12 | public abstract class TypeMapper {
13 | public final Class> javaType;
14 | public final String cType;
15 |
16 | public final String jniType;
17 |
18 | public abstract String castToJava(String expr);
19 |
20 | public abstract String castToC(String expr);
21 |
22 | protected TypeMapper(Class> javaType, String cType) {
23 | this.javaType = javaType;
24 | this.cType = cType;
25 | if (javaType == void.class) {
26 | this.jniType = "void";
27 | } else if (javaType.isPrimitive()) {
28 | this.jniType = 'j' + javaType.getName();
29 | } else {
30 | this.jniType = "jobject";
31 | }
32 | }
33 |
34 | public static final TypeMapper OfBoolean = new TypeMapper(boolean.class, "LLVMBool") {
35 | @Override
36 | public String castToJava(String expr) {
37 | return expr + " ? JNI_TRUE : JNI_FALSE";
38 | }
39 |
40 | @Override
41 | public String castToC(String expr) {
42 | return expr;
43 | }
44 | };
45 |
46 | public static final TypeMapper OfLtoBool = new TypeMapper(boolean.class, "lto_bool_t") {
47 | @Override
48 | public String castToJava(String expr) {
49 | return expr + " ? JNI_TRUE : JNI_FALSE";
50 | }
51 |
52 | @Override
53 | public String castToC(String expr) {
54 | return "(lto_bool_t) " + expr;
55 | }
56 | };
57 |
58 | public static final TypeMapper OfInt = new TypeMapper(int.class, "int") {
59 | @Override
60 | public String castToJava(String expr) {
61 | return "(jint)(int32_t) " + expr;
62 | }
63 |
64 | @Override
65 | public String castToC(String expr) {
66 | return "(int)(int32_t) " + expr;
67 | }
68 | };
69 |
70 | public static final TypeMapper OfDouble = new TypeMapper(double.class, "double") {
71 | @Override
72 | public String castToJava(String expr) {
73 | return "(jdouble) " + expr;
74 | }
75 |
76 | @Override
77 | public String castToC(String expr) {
78 | return "(double) " + expr;
79 | }
80 | };
81 |
82 | public static final TypeMapper OfVoid = new TypeMapper(void.class, "void") {
83 | @Override
84 | public String castToJava(String expr) {
85 | throw new AssertionError();
86 | }
87 |
88 | @Override
89 | public String castToC(String expr) {
90 | throw new AssertionError();
91 | }
92 | };
93 |
94 | public static final class OfPointer extends TypeMapper {
95 | public OfPointer(String cType) {
96 | super(long.class, cType);
97 | }
98 |
99 | @Override
100 | public String castToJava(String expr) {
101 | return "(jlong)(uintptr_t) " + expr;
102 | }
103 |
104 | @Override
105 | public String castToC(String expr) {
106 | return String.format("(%s)(uintptr_t) %s", cType, expr);
107 | }
108 | }
109 |
110 | public static final class OfEnum extends TypeMapper {
111 | public OfEnum(String cType) {
112 | super(int.class, cType);
113 | }
114 |
115 | @Override
116 | public String castToJava(String expr) {
117 | return "(jint) " + expr;
118 | }
119 |
120 | @Override
121 | public String castToC(String expr) {
122 | return String.format("(%s) %s", cType, expr);
123 | }
124 | }
125 |
126 | public static final class OfUnsigned extends TypeMapper {
127 |
128 | private final String temType;
129 |
130 | public OfUnsigned(Class> javaType, String cType) {
131 | super(javaType, cType);
132 |
133 | if (javaType == byte.class) {
134 | temType = "uint8_t";
135 | } else if (javaType == short.class) {
136 | temType = "uint16_t";
137 | } else if (javaType == int.class) {
138 | temType = "uint32_t";
139 | } else if (javaType == long.class) {
140 | temType = "uint64_t";
141 | } else {
142 | throw new AssertionError();
143 | }
144 | }
145 |
146 | @Override
147 | public String castToJava(String expr) {
148 | return String.format("(%s)(%s) %s", jniType, temType, expr);
149 | }
150 |
151 | @Override
152 | public String castToC(String expr) {
153 | return String.format("(%s)(%s) %s", cType, temType, expr);
154 | }
155 | }
156 |
157 | public static final class OfSigned extends TypeMapper {
158 |
159 | private final String temType;
160 |
161 | public OfSigned(Class> javaType, String cType) {
162 | super(javaType, cType);
163 |
164 | if (javaType == byte.class) {
165 | temType = "int8_t";
166 | } else if (javaType == short.class) {
167 | temType = "int16_t";
168 | } else if (javaType == int.class) {
169 | temType = "int32_t";
170 | } else if (javaType == long.class) {
171 | temType = "int64_t";
172 | } else {
173 | throw new AssertionError();
174 | }
175 | }
176 |
177 | @Override
178 | public String castToJava(String expr) {
179 | return String.format("(%s)(%s) %s", jniType, temType, expr);
180 | }
181 |
182 | @Override
183 | public String castToC(String expr) {
184 | return String.format("(%s)(%s) %s", cType, temType, expr);
185 | }
186 | }
187 |
188 | private static boolean checkAnnotated(Annotation ann) {
189 | return ann instanceof CEnum
190 | || ann instanceof Unsigned
191 | || ann instanceof Signed
192 | || ann instanceof Pointer
193 | || ann instanceof lto.lto_bool_t;
194 | }
195 |
196 | public static TypeMapper of(AnnotatedType type) {
197 | if (type.getType() == void.class) {
198 | return OfVoid;
199 | }
200 |
201 | Annotation annotation = null;
202 |
203 | for (Annotation ann : type.getAnnotations()) {
204 | if (checkAnnotated(ann)) {
205 | annotation = ann;
206 | break;
207 | }
208 | }
209 | if (annotation == null) {
210 | out:
211 | for (Annotation ann : type.getAnnotations()) {
212 | for (Annotation a : ann.annotationType().getAnnotations()) {
213 | if (checkAnnotated(a)) {
214 | annotation = a;
215 | break out;
216 | }
217 | }
218 | }
219 | }
220 |
221 | if (annotation == null) {
222 | if (type.getType() == boolean.class) {
223 | return OfBoolean;
224 | }
225 | if (type.getType() == int.class) {
226 | return OfInt;
227 | }
228 | if (type.getType() == double.class) {
229 | return OfDouble;
230 | }
231 | throw new AssertionError(type);
232 | }
233 |
234 | if (annotation instanceof lto.lto_bool_t) {
235 | if (type.getType() != boolean.class) {
236 | throw new AssertionError(type);
237 | }
238 | return OfLtoBool;
239 | }
240 |
241 | if (annotation instanceof Pointer) {
242 | if (type.getType() != long.class) {
243 | throw new AssertionError("error pointer type: " + type.getType());
244 | }
245 | return new OfPointer(((Pointer) annotation).value());
246 | }
247 |
248 | if (annotation instanceof CEnum) {
249 | if (type.getType() != int.class) {
250 | throw new AssertionError("error enum type: " + type.getType());
251 | }
252 | return new OfEnum(((CEnum) annotation).value());
253 | }
254 |
255 | if (annotation instanceof Unsigned) {
256 | return new OfUnsigned(((Class>) type.getType()), ((Unsigned) annotation).value());
257 | }
258 |
259 | if (annotation instanceof Signed) {
260 | return new OfSigned(((Class>) type.getType()), ((Signed) annotation).value());
261 | }
262 |
263 | throw new AssertionError();
264 | }
265 | }
266 |
--------------------------------------------------------------------------------
/native-generator/src/main/java/module-info.java:
--------------------------------------------------------------------------------
1 | module asia.kala.llvm.ng {
2 | requires asia.kala.llvm.binding;
3 | }
--------------------------------------------------------------------------------
/native-generator/src/main/resources/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.15)
2 | project(llvm-java-native)
3 |
4 | set(CMAKE_CXX_STANDARD 14)
5 |
6 | find_package(LLVM REQUIRED CONFIG)
7 | find_package(JNI REQUIRED)
8 |
9 | include_directories(${LLVM_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
10 | add_definitions(${LLVM_DEFINITIONS})
11 |
12 | llvm_map_components_to_libnames(llvm_libs
13 | support core irreader transformutils analysis bitreader bitwriter mc mcdisassembler executionengine
14 | target mcjit interpreter objcarcopts irreader lto linker orcjit remarks coroutines
15 | instcombine ipo vectorize
16 |
17 | aarch64asmparser aarch64codegen aarch64desc aarch64disassembler aarch64info aarch64utils
18 | amdgpuasmparser amdgpucodegen amdgpudesc amdgpudisassembler amdgpuinfo amdgpuutils
19 | armasmparser armcodegen armdesc armdisassembler arminfo armutils
20 | bpfasmparser bpfcodegen bpfdesc bpfdisassembler bpfinfo
21 | hexagonasmparser hexagoncodegen hexagondesc hexagondisassembler hexagoninfo
22 | lanaiasmparser lanaicodegen lanaidesc lanaidisassembler lanaiinfo
23 | mipsasmparser mipscodegen mipsdesc mipsdisassembler mipsinfo
24 | msp430asmparser msp430codegen msp430desc msp430disassembler msp430info
25 | nvptxcodegen nvptxdesc nvptxinfo
26 | powerpcasmparser powerpccodegen powerpcdesc powerpcdisassembler powerpcinfo
27 | riscvasmparser riscvcodegen riscvdesc riscvdisassembler riscvinfo riscvutils
28 | sparcasmparser sparccodegen sparcdesc sparcdisassembler sparcinfo
29 | systemzasmparser systemzcodegen systemzdesc systemzdisassembler systemzinfo
30 | webassemblyasmparser webassemblycodegen webassemblydesc webassemblydisassembler webassemblyinfo
31 | x86asmparser x86codegen x86desc x86disassembler x86info x86utils
32 | xcorecodegen xcoredesc xcoredisassembler xcoreinfo
33 | )
34 |
35 | add_library(llvm-java SHARED
36 | llvm-java.h llvm-java.cpp
37 | Core.c Support.c Error.c Object.c Analysis.c BitReader.c BitWriter.c Comdat.c DebugInfo.c Disassembler.c
38 | ErrorHandling.c Target.c TargetMachine.c ExecutionEngine.c Initialization.c IRReader.c Linker.c
39 | lto.c OrcBindings.c Remarks.c
40 | Transforms/AggressiveInstCombine.c Transforms/Coroutines.c Transforms/InstCombine.c Transforms/IPO.c
41 | Transforms/PassManagerBuilder.c Transforms/Scalar.c Transforms/Utils.c Transforms/Vectorize.c
42 | CoreExtern.cpp NativeUtils.cpp
43 | )
44 |
45 | target_link_libraries(llvm-java ${llvm_libs})
46 |
47 | set_property(TARGET llvm-java PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>")
--------------------------------------------------------------------------------
/native-generator/src/main/resources/CoreExtern.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include "llvm-java.h"
5 |
6 | #define LLVM_JAVA_CORE(name) Java_asia_kala_llvm_binding_Core_ ## name
7 |
8 |
9 | extern "C" {
10 |
11 | JNIEXPORT jobject JNICALL LLVM_JAVA_CORE(LLVMGetModuleIdentifier0)(JNIEnv *env, jclass cls, jlong module) {
12 | size_t len;
13 | auto id = LLVMGetModuleIdentifier((LLVMModuleRef) (uintptr_t) module, &len);
14 | return id == nullptr ? nullptr : env->NewDirectByteBuffer((void *) id, (jlong) (uint64_t) len);
15 | }
16 |
17 | JNIEXPORT jobject JNICALL LLVM_JAVA_CORE(LLVMGetSourceFileName0)(JNIEnv *env, jclass cls, jlong module) {
18 | size_t len;
19 | auto id = LLVMGetSourceFileName((LLVMModuleRef) (uintptr_t) module, &len);
20 | return id == nullptr ? nullptr : env->NewDirectByteBuffer((void *) id, (jlong) (uint64_t) len);
21 | }
22 |
23 | JNIEXPORT jlong JNICALL LLVM_JAVA_CORE(LLVMFunctionType__J_3JZ)(
24 | JNIEnv *env, jclass cls,
25 | jlong returnType, jlongArray paramTypes, jboolean isVarArg
26 | ) {
27 | auto retTy = (LLVMTypeRef) (uintptr_t) returnType;
28 |
29 | SizedArray pts(env, paramTypes);
30 | return (jlong) (uintptr_t) LLVMFunctionType(retTy, pts.values, pts.size, isVarArg);
31 |
32 | }
33 |
34 | JNIEXPORT jlongArray JNICALL LLVM_JAVA_CORE(LLVMGetParamTypes__J)(JNIEnv *env, jclass cls, jlong functionType) {
35 | auto ty = llvm::unwrap((LLVMTypeRef) (uintptr_t) functionType);
36 | auto count = ty->getFunctionNumParams();
37 |
38 | auto res = env->NewLongArray((jsize) count);
39 | auto tem = env->GetLongArrayElements(res, nullptr);
40 |
41 | auto dest = tem;
42 | for (auto it = ty->param_begin(), end = ty->param_end(); it != end; ++it) {
43 | *dest++ = (jlong) (uintptr_t) llvm::wrap(*it);
44 | }
45 | env->ReleaseLongArrayElements(res, tem, 0);
46 | return res;
47 | }
48 |
49 | JNIEXPORT jlong JNICALL LLVM_JAVA_CORE(LLVMStructTypeInContext__J_3JZ)(
50 | JNIEnv *env, jclass cls,
51 | jlong context, jlongArray elementTypes, jboolean packed
52 | ) {
53 | auto ct = (LLVMContextRef) (uintptr_t) context;
54 |
55 | SizedArray ets(env, elementTypes);
56 | return (jlong) (uintptr_t) LLVMStructTypeInContext(ct, ets.values, ets.size, packed);
57 |
58 | }
59 |
60 | JNIEXPORT void JNICALL LLVM_JAVA_CORE(LLVMStructSetBody__J_3JZ)(
61 | JNIEnv *env, jclass cls,
62 | jlong structType, jlongArray elementTypes, jboolean packed
63 | ) {
64 | auto st = (LLVMTypeRef) (uintptr_t) structType;
65 | SizedArray ets(env, elementTypes);
66 |
67 | LLVMStructSetBody(st, ets.values, ets.size, packed);
68 | }
69 |
70 | JNIEXPORT jlongArray JNICALL LLVM_JAVA_CORE(LLVMGetStructElementTypes__J)(JNIEnv *env, jclass cls, jlong structType) {
71 | auto ty = llvm::unwrap((LLVMTypeRef) (uintptr_t) structType);
72 | auto count = ty->getStructNumElements();
73 |
74 | auto res = env->NewLongArray((jsize) count);
75 | auto tem = env->GetLongArrayElements(res, nullptr);
76 |
77 | auto dest = tem;
78 | for (auto it = ty->element_begin(), end = ty->element_end(); it != end; ++it) {
79 | *dest++ = (jlong) (uintptr_t) llvm::wrap(*it);
80 | }
81 | env->ReleaseLongArrayElements(res, tem, 0);
82 | return res;
83 | }
84 |
85 | JNIEXPORT jlongArray JNICALL LLVM_JAVA_CORE(LLVMGetSubtypes__J)(JNIEnv *env, jclass cls, jlong type) {
86 | auto ty = llvm::unwrap((LLVMTypeRef) (uintptr_t) type);
87 | auto subtypes = ty->subtypes();
88 | auto count = subtypes.size();
89 |
90 | auto res = env->NewLongArray((jsize) count);
91 | auto tem = env->GetLongArrayElements(res, nullptr);
92 |
93 | for (size_t i = 0; i < count; ++i) {
94 | tem[i] = (jlong)(uintptr_t) llvm::wrap(subtypes[i]);
95 | }
96 | env->ReleaseLongArrayElements(res, tem, 0);
97 | return res;
98 | }
99 |
100 | JNIEXPORT jobject JNICALL LLVM_JAVA_CORE(_LLVMGetValueName2)(JNIEnv *env, jclass cls, jlong value) {
101 | size_t len;
102 | auto name = LLVMGetValueName2((LLVMValueRef) (uintptr_t) value, &len);
103 | return name == nullptr ? nullptr : env->NewDirectByteBuffer((void *) name, (jlong) (uint64_t) len);
104 | }
105 |
106 | JNIEXPORT jobject JNICALL LLVM_JAVA_CORE(LLVMGetAsString0)(JNIEnv *env, jclass cls, jlong c) {
107 | size_t len;
108 | auto res = LLVMGetAsString((LLVMValueRef) (uintptr_t) c, &len);
109 | return res == nullptr ? nullptr : env->NewDirectByteBuffer((void *) res, (jlong) (uint64_t) len);
110 | }
111 |
112 | JNIEXPORT jlong JNICALL LLVM_JAVA_CORE(LLVMConstStructInContext__J_3JZ)(
113 | JNIEnv *env, jclass cls,
114 | jlong context, jlongArray constantValues, jboolean packed
115 | ) {
116 | auto ct = (LLVMContextRef) (uintptr_t) context;
117 |
118 | SizedArray cvs(env, constantValues);
119 | return (jlong) (uintptr_t) LLVMConstStructInContext(ct, cvs.values, cvs.size, packed);
120 | }
121 |
122 | JNIEXPORT jlong JNICALL LLVM_JAVA_CORE(LLVMConstArray__J_3J)(
123 | JNIEnv *env, jclass cls,
124 | jlong elementType, jlongArray constantValues
125 | ) {
126 | auto et = (LLVMTypeRef) (uintptr_t) elementType;
127 |
128 | SizedArray cvs(env, constantValues);
129 | return (jlong) (uintptr_t) LLVMConstArray(et, cvs.values, cvs.size);
130 | }
131 |
132 | JNIEXPORT jlong JNICALL LLVM_JAVA_CORE(LLVMConstNamedStruct__J_3J)(
133 | JNIEnv *env, jclass cls,
134 | jlong structType, jlongArray constantValues
135 | ) {
136 | auto st = (LLVMTypeRef) (uintptr_t) structType;
137 |
138 | SizedArray cvs(env, constantValues);
139 | return (jlong) (uintptr_t) LLVMConstNamedStruct(st, cvs.values, cvs.size);
140 | }
141 |
142 | }
--------------------------------------------------------------------------------
/native-generator/src/main/resources/NativeUtils.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include "llvm-java.h"
5 |
6 | #define LLVM_JAVA_NATIVE_UTILS(name) Java_asia_kala_llvm_binding_NativeUtils_ ## name
7 |
8 | extern "C" {
9 |
10 | JNIEXPORT jlong JNICALL LLVM_JAVA_NATIVE_UTILS(GetDirectBufferAddress)(JNIEnv *env, jclass, jobject buffer) {
11 | return (jlong) (uintptr_t) env->GetDirectBufferAddress(buffer);
12 | }
13 |
14 | JNIEXPORT jobject JNICALL LLVM_JAVA_NATIVE_UTILS(NewDirectByteBuffer)(JNIEnv *env, jclass, jlong address, jlong capacity) {
15 | return env->NewDirectByteBuffer((void *) (uintptr_t) address, capacity);
16 | }
17 |
18 | JNIEXPORT jlong JNICALL LLVM_JAVA_NATIVE_UTILS(Malloc)(JNIEnv *env, jclass, jlong size) {
19 | return (jlong) (uintptr_t) malloc((size_t) (uint64_t) size);
20 | }
21 |
22 | JNIEXPORT void JNICALL LLVM_JAVA_NATIVE_UTILS(Free)(JNIEnv *env, jclass, jlong block) {
23 | free((void *) (uintptr_t) block);
24 | }
25 |
26 | JNIEXPORT jlong JNICALL LLVM_JAVA_NATIVE_UTILS(NewSizeT)(JNIEnv *env, jclass) {
27 | return (jlong) (uintptr_t) malloc(sizeof(size_t));
28 | }
29 |
30 | JNIEXPORT jlong JNICALL LLVM_JAVA_NATIVE_UTILS(GetSizeT)(JNIEnv *env, jclass, jlong address) {
31 | return (jlong) (uint64_t) *((const size_t *) (uintptr_t) address);
32 | }
33 |
34 | JNIEXPORT void JNICALL LLVM_JAVA_NATIVE_UTILS(SetSizeT)(JNIEnv *env, jclass, jlong address, jlong value) {
35 | *((size_t *) (uintptr_t) address) = (size_t) (uint64_t) value;
36 | }
37 |
38 | JNIEXPORT jbyte JNICALL LLVM_JAVA_NATIVE_UTILS(GetByte)(JNIEnv *env, jclass, jlong address) {
39 | return (jbyte) (uint8_t) *((const uint8_t *) (uintptr_t) address);
40 | }
41 |
42 | JNIEXPORT void JNICALL LLVM_JAVA_NATIVE_UTILS(SetByte)(JNIEnv *env, jclass, jlong address, jbyte value) {
43 | *((uint8_t *) (uintptr_t) address) = (uint8_t) value;
44 | }
45 |
46 | JNIEXPORT jlong JNICALL LLVM_JAVA_NATIVE_UTILS(NewLLVMBool)(JNIEnv *env, jclass) {
47 | return (jlong) (uintptr_t) malloc(sizeof(LLVMBool));
48 | }
49 |
50 | JNIEXPORT jboolean JNICALL LLVM_JAVA_NATIVE_UTILS(GetLLVMBool)(JNIEnv *env, jclass, jlong address) {
51 | return (*(const LLVMBool *) (uintptr_t) address) ? JNI_TRUE : JNI_FALSE;
52 | }
53 |
54 | JNIEXPORT void JNICALL LLVM_JAVA_NATIVE_UTILS(SetLLVMBool)(JNIEnv *env, jclass, jlong address, jboolean value) {
55 | *((LLVMBool *) (uintptr_t) address) = value;
56 | }
57 |
58 | JNIEXPORT void JNICALL LLVM_JAVA_NATIVE_UTILS(DumpString)(JNIEnv *env, jclass, jlong string) {
59 | printf("%s", (const char *) (uintptr_t) string);
60 | }
61 |
62 | }
--------------------------------------------------------------------------------
/native-generator/src/main/resources/llvm-java.cpp:
--------------------------------------------------------------------------------
1 | #include "llvm-java.h"
2 |
3 | extern "C" {
4 |
5 | JavaVM *jvm = nullptr;
6 |
7 | JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
8 | jvm = vm;
9 | return JNI_VERSION_1_6;
10 | }
11 |
12 | JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
13 | if (vm == jvm) {
14 | jvm = nullptr;
15 | }
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/native-generator/src/main/resources/llvm-java.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 |
6 | #ifdef __cplusplus
7 |
8 | #include
9 |
10 | #else
11 | #include
12 | #endif
13 |
14 | #ifdef __cplusplus
15 | extern "C" {
16 | #endif
17 |
18 | extern JavaVM *jvm;
19 |
20 | #ifdef __cplusplus
21 | }
22 | #endif
23 |
24 | #ifdef __cplusplus
25 |
26 | template
27 | struct SizedArray {
28 | unsigned size;
29 | T *values;
30 |
31 | SizedArray(unsigned int size, T *values) : size(size), values(values) {}
32 |
33 | SizedArray(const SizedArray &) = delete;
34 |
35 | SizedArray(JNIEnv *env, jlongArray array) {
36 | auto size = (unsigned) env->GetArrayLength(array);
37 | auto values = new T[size];
38 | auto tem = env->GetLongArrayElements(array, nullptr);
39 | for (unsigned i = 0; i < size; ++i) {
40 | values[i] = (T) (uintptr_t) tem[i];
41 | }
42 | env->ReleaseLongArrayElements(array, tem, JNI_ABORT);
43 | this->size = size;
44 | this->values = values;
45 | }
46 |
47 | ~SizedArray() {
48 | delete[] values;
49 | }
50 | };
51 |
52 | #endif
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'llvm'
2 |
3 | include 'llvm-binding', 'llvm-platform'
4 |
5 | include 'native-generator'
--------------------------------------------------------------------------------
/src/main/java/module-info.java:
--------------------------------------------------------------------------------
1 | module asia.kala.llvm {
2 | requires transitive asia.kala.llvm.binding;
3 | }
--------------------------------------------------------------------------------