1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Wrapper for LLVM header file #includes.
31 */
32
33
34 #ifndef LP_BLD_H
35 #define LP_BLD_H
36
37
38 /**
39 * @file
40 * LLVM IR building helpers interfaces.
41 *
42 * We use LLVM-C bindings for now. They are not documented, but follow the C++
43 * interfaces very closely, and appear to be complete enough for code
44 * genration. See
45 * http://npcontemplation.blogspot.com/2008/06/secret-of-llvm-c-bindings.html
46 * for a standalone example.
47 */
48
49 #include <llvm/Config/llvm-config.h>
50
51 #include <llvm-c/Core.h>
52
53 #if GALLIVM_USE_ORCJIT
54 #include <llvm-c/Orc.h>
55 #endif
56
57 #include <assert.h>
58 #include <stdbool.h>
59 #include <stddef.h>
60
61 /**
62 * Redefine these LLVM entrypoints as invalid macros to make sure we
63 * don't accidentally use them. We need to use the functions which
64 * take an explicit LLVMContextRef parameter.
65 */
66 #define LLVMInt1Type ILLEGAL_LLVM_FUNCTION
67 #define LLVMInt8Type ILLEGAL_LLVM_FUNCTION
68 #define LLVMInt16Type ILLEGAL_LLVM_FUNCTION
69 #define LLVMInt32Type ILLEGAL_LLVM_FUNCTION
70 #define LLVMInt64Type ILLEGAL_LLVM_FUNCTION
71 #define LLVMIntType ILLEGAL_LLVM_FUNCTION
72 #define LLVMFloatType ILLEGAL_LLVM_FUNCTION
73 #define LLVMDoubleType ILLEGAL_LLVM_FUNCTION
74 #define LLVMX86FP80Type ILLEGAL_LLVM_FUNCTION
75 #define LLVMFP128Type ILLEGAL_LLVM_FUNCTION
76 #define LLVMPPCFP128Type ILLEGAL_LLVM_FUNCTION
77 #define LLVMStructType ILLEGAL_LLVM_FUNCTION
78 #define LLVMVoidType ILLEGAL_LLVM_FUNCTION
79 #define LLVMLabelType ILLEGAL_LLVM_FUNCTION
80 #define LLVMOpaqueType ILLEGAL_LLVM_FUNCTION
81 #define LLVMUnionType ILLEGAL_LLVM_FUNCTION
82 #define LLVMMDString ILLEGAL_LLVM_FUNCTION
83 #define LLVMMDNode ILLEGAL_LLVM_FUNCTION
84 #define LLVMConstString ILLEGAL_LLVM_FUNCTION
85 #define LLVMConstStruct ILLEGAL_LLVM_FUNCTION
86 #define LLVMAppendBasicBlock ILLEGAL_LLVM_FUNCTION
87 #define LLVMInsertBasicBlock ILLEGAL_LLVM_FUNCTION
88 #define LLVMCreateBuilder ILLEGAL_LLVM_FUNCTION
89
90 #if LLVM_VERSION_MAJOR >= 8
91 #define GALLIVM_COROUTINES 1
92 #else
93 #define GALLIVM_COROUTINES 0
94 #endif
95
96 /* LLVM is transitioning to "opaque pointers", and as such deprecates
97 * LLVMBuildGEP, LLVMBuildCall, LLVMBuildLoad, replacing them with
98 * LLVMBuildGEP2, LLVMBuildCall2, LLVMBuildLoad2 respectivelly.
99 * These new functions were added in LLVM 8.0; so for LLVM before 8.0 we
100 * simply forward to the non-opaque-pointer variants.
101 */
102 #if LLVM_VERSION_MAJOR < 8
103
104 static inline LLVMValueRef
LLVMBuildGEP2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Pointer,LLVMValueRef * Indices,unsigned NumIndices,const char * Name)105 LLVMBuildGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
106 LLVMValueRef Pointer, LLVMValueRef *Indices,
107 unsigned NumIndices, const char *Name)
108 {
109 return LLVMBuildGEP(B, Pointer, Indices, NumIndices, Name);
110 }
111
112 static inline LLVMValueRef
LLVMBuildInBoundsGEP2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Pointer,LLVMValueRef * Indices,unsigned NumIndices,const char * Name)113 LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
114 LLVMValueRef Pointer, LLVMValueRef *Indices,
115 unsigned NumIndices, const char *Name)
116 {
117 return LLVMBuildInBoundsGEP(B, Pointer, Indices, NumIndices, Name);
118 }
119
120 static inline LLVMValueRef
LLVMBuildLoad2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef PointerVal,const char * Name)121 LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,
122 LLVMValueRef PointerVal, const char *Name)
123 {
124 LLVMValueRef val = LLVMBuildLoad(B, PointerVal, Name);
125 return LLVMTypeOf(val) == Ty ? val : LLVMBuildBitCast(B, val, Ty, Name);
126 }
127
128 static inline LLVMValueRef
LLVMBuildCall2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Fn,LLVMValueRef * Args,unsigned NumArgs,const char * Name)129 LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
130 LLVMValueRef *Args, unsigned NumArgs,
131 const char *Name)
132 {
133 return LLVMBuildCall(B, Fn, Args, NumArgs, Name);
134 }
135
136 #endif /* LLVM_VERSION_MAJOR < 8 */
137
138 typedef struct lp_context_ref {
139 #if GALLIVM_USE_ORCJIT
140 LLVMOrcThreadSafeContextRef ref;
141 #else
142 LLVMContextRef ref;
143 #endif
144 bool owned;
145 } lp_context_ref;
146
147 static inline void
lp_context_create(lp_context_ref * context)148 lp_context_create(lp_context_ref *context)
149 {
150 assert(context != NULL);
151 #if GALLIVM_USE_ORCJIT
152 context->ref = LLVMOrcCreateNewThreadSafeContext();
153 #else
154 context->ref = LLVMContextCreate();
155 #endif
156 context->owned = true;
157 #if LLVM_VERSION_MAJOR == 15
158 if (context->ref) {
159 #if GALLIVM_USE_ORCJIT
160 LLVMContextSetOpaquePointers(LLVMOrcThreadSafeContextGetContext(context->ref), false);
161 #else
162 LLVMContextSetOpaquePointers(context->ref, false);
163 #endif
164 }
165 #endif
166 }
167
168 static inline void
lp_context_destroy(lp_context_ref * context)169 lp_context_destroy(lp_context_ref *context)
170 {
171 assert(context != NULL);
172 if (context->owned) {
173 #if GALLIVM_USE_ORCJIT
174 LLVMOrcDisposeThreadSafeContext(context->ref);
175 #else
176 LLVMContextDispose(context->ref);
177 #endif
178 context->ref = NULL;
179 }
180 }
181
182 #endif /* LP_BLD_H */
183