• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
54 
55 /**
56  * Redefine these LLVM entrypoints as invalid macros to make sure we
57  * don't accidentally use them.  We need to use the functions which
58  * take an explicit LLVMContextRef parameter.
59  */
60 #define LLVMInt1Type ILLEGAL_LLVM_FUNCTION
61 #define LLVMInt8Type ILLEGAL_LLVM_FUNCTION
62 #define LLVMInt16Type ILLEGAL_LLVM_FUNCTION
63 #define LLVMInt32Type ILLEGAL_LLVM_FUNCTION
64 #define LLVMInt64Type ILLEGAL_LLVM_FUNCTION
65 #define LLVMIntType ILLEGAL_LLVM_FUNCTION
66 #define LLVMFloatType ILLEGAL_LLVM_FUNCTION
67 #define LLVMDoubleType ILLEGAL_LLVM_FUNCTION
68 #define LLVMX86FP80Type ILLEGAL_LLVM_FUNCTION
69 #define LLVMFP128Type ILLEGAL_LLVM_FUNCTION
70 #define LLVMPPCFP128Type ILLEGAL_LLVM_FUNCTION
71 #define LLVMStructType ILLEGAL_LLVM_FUNCTION
72 #define LLVMVoidType ILLEGAL_LLVM_FUNCTION
73 #define LLVMLabelType ILLEGAL_LLVM_FUNCTION
74 #define LLVMOpaqueType ILLEGAL_LLVM_FUNCTION
75 #define LLVMUnionType ILLEGAL_LLVM_FUNCTION
76 #define LLVMMDString ILLEGAL_LLVM_FUNCTION
77 #define LLVMMDNode ILLEGAL_LLVM_FUNCTION
78 #define LLVMConstString ILLEGAL_LLVM_FUNCTION
79 #define LLVMConstStruct ILLEGAL_LLVM_FUNCTION
80 #define LLVMAppendBasicBlock ILLEGAL_LLVM_FUNCTION
81 #define LLVMInsertBasicBlock ILLEGAL_LLVM_FUNCTION
82 #define LLVMCreateBuilder ILLEGAL_LLVM_FUNCTION
83 
84 #if LLVM_VERSION_MAJOR >= 15
85 #define GALLIVM_HAVE_CORO 0
86 #define GALLIVM_USE_NEW_PASS 1
87 #elif LLVM_VERSION_MAJOR >= 8
88 #define GALLIVM_HAVE_CORO 1
89 #define GALLIVM_USE_NEW_PASS 0
90 #else
91 #define GALLIVM_HAVE_CORO 0
92 #define GALLIVM_USE_NEW_PASS 0
93 #endif
94 
95 #define GALLIVM_COROUTINES (GALLIVM_HAVE_CORO || GALLIVM_USE_NEW_PASS)
96 
97 /* LLVM is transitioning to "opaque pointers", and as such deprecates
98  * LLVMBuildGEP, LLVMBuildCall, LLVMBuildLoad, replacing them with
99  * LLVMBuildGEP2, LLVMBuildCall2, LLVMBuildLoad2 respectivelly.
100  * These new functions were added in LLVM 8.0; so for LLVM before 8.0 we
101  * simply forward to the non-opaque-pointer variants.
102  */
103 #if LLVM_VERSION_MAJOR < 8
104 
105 static inline LLVMValueRef
LLVMBuildGEP2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Pointer,LLVMValueRef * Indices,unsigned NumIndices,const char * Name)106 LLVMBuildGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
107               LLVMValueRef Pointer, LLVMValueRef *Indices,
108               unsigned NumIndices, const char *Name)
109 {
110    return LLVMBuildGEP(B, Pointer, Indices, NumIndices, Name);
111 }
112 
113 static inline LLVMValueRef
LLVMBuildInBoundsGEP2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Pointer,LLVMValueRef * Indices,unsigned NumIndices,const char * Name)114 LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
115                       LLVMValueRef Pointer, LLVMValueRef *Indices,
116                       unsigned NumIndices, const char *Name)
117 {
118    return LLVMBuildInBoundsGEP(B, Pointer, Indices, NumIndices, Name);
119 }
120 
121 static inline LLVMValueRef
LLVMBuildLoad2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef PointerVal,const char * Name)122 LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,
123                LLVMValueRef PointerVal, const char *Name)
124 {
125   LLVMValueRef val = LLVMBuildLoad(B, PointerVal, Name);
126   return LLVMTypeOf(val) == Ty ? val : LLVMBuildBitCast(B, val, Ty, Name);
127 }
128 
129 static inline LLVMValueRef
LLVMBuildCall2(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Fn,LLVMValueRef * Args,unsigned NumArgs,const char * Name)130 LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn,
131                LLVMValueRef *Args, unsigned NumArgs,
132                const char *Name)
133 {
134    return LLVMBuildCall(B, Fn, Args, NumArgs, Name);
135 }
136 
137 #endif /* LLVM_VERSION_MAJOR < 8 */
138 
139 #endif /* LP_BLD_H */
140