Browse code

bb#10981 - applied LLVM 3.1-3.4 patch for testing

Conflicts:

libclamav/c++/Makefile.am
libclamav/c++/bytecode2llvm.cpp

Kevin Lin authored on 2014/05/23 03:33:44
Showing 7 changed files
... ...
@@ -27,7 +27,6 @@
27 27
 #include "llvm/ADT/SCCIterator.h"
28 28
 #include "llvm/Analysis/CallGraph.h"
29 29
 #include "llvm/Analysis/Verifier.h"
30
-#include "llvm/Analysis/DebugInfo.h"
31 30
 #include "llvm/Analysis/Dominators.h"
32 31
 #include "llvm/Analysis/ConstantFolding.h"
33 32
 //#include "llvm/Analysis/LiveValues.h"
... ...
@@ -42,25 +41,49 @@
42 42
 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
43 43
 #include "llvm/Analysis/ScalarEvolutionExpander.h"
44 44
 #include "llvm/Config/config.h"
45
-#include "llvm/DerivedTypes.h"
46
-#include "llvm/Instructions.h"
47
-#include "llvm/IntrinsicInst.h"
48
-#include "llvm/Intrinsics.h"
49
-#include "llvm/LLVMContext.h"
50
-#include "llvm/Module.h"
51 45
 #include "llvm/Pass.h"
52 46
 #include "llvm/Support/CommandLine.h"
53 47
 #include "llvm/Support/DataFlow.h"
54 48
 #include "llvm/Support/InstIterator.h"
55
-#include "llvm/Support/InstVisitor.h"
56 49
 #include "llvm/Support/GetElementPtrTypeIterator.h"
57 50
 #include "llvm/ADT/DepthFirstIterator.h"
58
-#include "llvm/Target/TargetData.h"
59 51
 #include "llvm/Transforms/Scalar.h"
60 52
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
61 53
 #include "llvm/Support/Debug.h"
62 54
 #include "llvm30_compat.h"
63 55
 
56
+#if LLVM_VERSION < 32
57
+#include "llvm/Analysis/DebugInfo.h"
58
+#else
59
+#include "llvm/DebugInfo.h"
60
+#endif
61
+
62
+#if LLVM_VERSION < 32
63
+#include "llvm/Target/TargetData.h"
64
+#elif LLVM_VERSION < 33
65
+#include "llvm/DataLayout.h"
66
+#else
67
+#include "llvm/IR/DataLayout.h"
68
+#endif
69
+
70
+#if LLVM_VERSION < 33
71
+#include "llvm/DerivedTypes.h"
72
+#include "llvm/Instructions.h"
73
+#include "llvm/IntrinsicInst.h"
74
+#include "llvm/Intrinsics.h"
75
+#include "llvm/LLVMContext.h"
76
+#include "llvm/Module.h"
77
+#include "llvm/Support/InstVisitor.h"
78
+#else
79
+#include "llvm/IR/DerivedTypes.h"
80
+#include "llvm/IR/Instructions.h"
81
+#include "llvm/IR/IntrinsicInst.h"
82
+#include "llvm/IR/Intrinsics.h"
83
+#include "llvm/IR/LLVMContext.h"
84
+#include "llvm/IR/Module.h"
85
+#include "llvm/InstVisitor.h"
86
+#endif
87
+
64 88
 #ifndef LLVM28
65 89
 #define LLVM28
66 90
 #endif
... ...
@@ -72,7 +95,11 @@
72 72
 
73 73
 using namespace llvm;
74 74
 #ifndef LLVM29
75
+#if LLVM_VERSION < 32
75 76
 static Value *GetUnderlyingObject(Value *P, TargetData *TD)
77
+#else
78
+static Value *GetUnderlyingObject(Value *P, DataLayout *TD)
79
+#endif
76 80
 {
77 81
     return P->getUnderlyingObject();
78 82
 }
... ...
@@ -137,8 +164,11 @@ namespace llvm {
137 137
       BasicBlock::iterator It = F.getEntryBlock().begin();
138 138
       while (isa<AllocaInst>(It) || isa<PHINode>(It)) ++It;
139 139
       EP = &*It;
140
-
140
+#if LLVM_VERSION < 32
141 141
       TD = &getAnalysis<TargetData>();
142
+#else
143
+      TD = &getAnalysis<DataLayout>();
144
+#endif
142 145
       SE = &getAnalysis<ScalarEvolution>();
143 146
       PT = &getAnalysis<PointerTracking>();
144 147
       DT = &getAnalysis<DominatorTree>();
... ...
@@ -247,8 +277,13 @@ namespace llvm {
247 247
 	CallInst *AbrtC = CallInst::Create(func_abort, "", UI);
248 248
         AbrtC->setCallingConv(CallingConv::C);
249 249
         AbrtC->setTailCall(true);
250
+#if LLVM_VERSION < 32
250 251
         AbrtC->setDoesNotReturn(true);
251 252
         AbrtC->setDoesNotThrow(true);
253
+#else
254
+        AbrtC->setDoesNotReturn();
255
+        AbrtC->setDoesNotThrow();
256
+#endif
252 257
 	// remove all instructions from entry
253 258
 	BasicBlock::iterator BBI = I, BBE=BB->end();
254 259
 	while (BBI != BBE) {
... ...
@@ -265,7 +300,11 @@ namespace llvm {
265 265
     }
266 266
 
267 267
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
268
+#if LLVM_VERSION < 32
268 269
       AU.addRequired<TargetData>();
270
+#else
271
+      AU.addRequired<DataLayout>();
272
+#endif
269 273
       AU.addRequired<DominatorTree>();
270 274
       AU.addRequired<ScalarEvolution>();
271 275
       AU.addRequired<PointerTracking>();
... ...
@@ -275,7 +314,11 @@ namespace llvm {
275 275
     bool isValid() const { return valid; }
276 276
   private:
277 277
     PointerTracking *PT;
278
+#if LLVM_VERSION < 32
278 279
     TargetData *TD;
280
+#else
281
+    DataLayout *TD;
282
+#endif
279 283
     ScalarEvolution *SE;
280 284
     DominatorTree *DT;
281 285
     DenseMap<Value*, Value*> BaseMap;
... ...
@@ -511,13 +554,22 @@ namespace llvm {
511 511
           CallInst *RtErrCall = CallInst::Create(func_rterr, PN, "", AbrtBB);
512 512
           RtErrCall->setCallingConv(CallingConv::C);
513 513
           RtErrCall->setTailCall(true);
514
+#if LLVM_VERSION < 32
514 515
           RtErrCall->setDoesNotThrow(true);
516
+#else
517
+          RtErrCall->setDoesNotThrow();
518
+#endif
515 519
         }
516 520
         CallInst* AbrtC = CallInst::Create(func_abort, "", AbrtBB);
517 521
         AbrtC->setCallingConv(CallingConv::C);
518 522
         AbrtC->setTailCall(true);
523
+#if LLVM_VERSION < 32
519 524
         AbrtC->setDoesNotReturn(true);
520 525
         AbrtC->setDoesNotThrow(true);
526
+#else
527
+        AbrtC->setDoesNotReturn();
528
+        AbrtC->setDoesNotThrow();
529
+#endif
521 530
         new UnreachableInst(BB->getContext(), AbrtBB);
522 531
         DT->addNewBlock(AbrtBB, BB);
523 532
         //verifyFunction(*BB->getParent());
... ...
@@ -732,10 +784,18 @@ namespace llvm {
732 732
 }
733 733
 #ifdef LLVM30
734 734
 INITIALIZE_PASS_BEGIN(PtrVerifier, "", "", false, false)
735
+#if LLVM_VERSION < 32
735 736
 INITIALIZE_PASS_DEPENDENCY(TargetData)
737
+#else
738
+INITIALIZE_PASS_DEPENDENCY(DataLayout)
739
+#endif
736 740
 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
737 741
 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
742
+#if LLVM_VERSION < 34
738 743
 INITIALIZE_AG_DEPENDENCY(CallGraph)
744
+#else
745
+INITIALIZE_PASS_DEPENDENCY(CallGraph)
746
+#endif
739 747
 INITIALIZE_PASS_DEPENDENCY(PointerTracking)
740 748
 INITIALIZE_PASS_END(PtrVerifier, "clambcrtchecks", "ClamBC RTchecks", false, false)
741 749
 #endif
... ...
@@ -38,6 +38,7 @@ libclamavcxx_la_CXXFLAGS = $(AM_CPPFLAGS) @LLVMCONFIG_CXXFLAGS@ -fexceptions -DL
38 38
 if BUILD_LLVM3
39 39
 libclamavcxx_la_CXXFLAGS += -DLLVM30
40 40
 endif
41
+libclamavcxx_la_CXXFLAGS += -DLLVM_VERSION=${LLVM_VERSION}
41 42
 libclamavcxx_la_LDFLAGS = @LLVMCONFIG_LDFLAGS@ @LLVMCONFIG_LIBS@ @JSON_LDFLAGS@
42 43
 libclamavcxx_la_DEPENDENCIES = @LLVMCONFIG_LIBFILES@ @JSON_LIBS@
43 44
 noinst_LTLIBRARIES = libclamavcxx.la
... ...
@@ -51,7 +52,7 @@ noinst_LTLIBRARIES = libclamavcxx.la libllvmsystem.la\
51 51
 libclamavcxx_la_LIBADD=libllvmjit.la
52 52
 libclamavcxx_la_DEPENDENCIES=libllvmjit.la libllvmcodegen.la libllvmsystem.la
53 53
 libclamavcxx_la_LDFLAGS=-no-undefined
54
-libclamavcxx_la_CXXFLAGS = $(LLVM_CXXFLAGS) @JSON_CPPFLAGS@
54
+libclamavcxx_la_CXXFLAGS = $(LLVM_CXXFLAGS) -DLLVM_VERSION=${LLVM_VERSION} @JSON_CPPFLAGS@
55 55
 
56 56
 
57 57
 if BUILD_X86
... ...
@@ -23,19 +23,36 @@
23 23
 #include "PointerTracking.h"
24 24
 #include "llvm/Analysis/ScalarEvolution.h"
25 25
 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
26
-#include "llvm/Constants.h"
27
-#include "llvm/Module.h"
28
-#include "llvm/Value.h"
29 26
 #include "llvm/Support/CallSite.h"
30 27
 #include "llvm/Support/InstIterator.h"
31 28
 #include "llvm/Support/raw_ostream.h"
29
+#include "llvm/Target/TargetLibraryInfo.h"
30
+
31
+#if LLVM_VERSION < 32
32 32
 #include "llvm/Target/TargetData.h"
33
+#elif LLVM_VERSION < 33
34
+#include "llvm/DataLayout.h"
35
+#else
36
+#include "llvm/IR/DataLayout.h"
37
+#endif
38
+
39
+#if LLVM_VERSION < 33
40
+#include "llvm/Constants.h"
41
+#include "llvm/Module.h"
42
+#include "llvm/Value.h"
43
+#else
44
+#include "llvm/IR/Constants.h"
45
+#include "llvm/IR/Module.h"
46
+#include "llvm/IR/Value.h"
47
+#endif
48
+
33 49
 using namespace llvm;
34 50
 namespace llvm {
35 51
     void initializePointerTrackingPass(llvm::PassRegistry&);
36 52
 };
37 53
 INITIALIZE_PASS_BEGIN(PointerTracking, "pointertracking",
38 54
                 "Track pointer bounds", false, true)
55
+
39 56
 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
40 57
 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
41 58
 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
... ...
@@ -52,7 +69,11 @@ bool PointerTracking::runOnFunction(Function &F) {
52 52
   predCache.clear();
53 53
   assert(analyzing.empty());
54 54
   FF = &F;
55
+#if LLVM_VERSION < 32
55 56
   TD = getAnalysisIfAvailable<TargetData>();
57
+#else
58
+  TD = getAnalysisIfAvailable<DataLayout>();
59
+#endif
56 60
   SE = &getAnalysis<ScalarEvolution>();
57 61
   LI = &getAnalysis<LoopInfo>();
58 62
   DT = &getAnalysis<DominatorTree>();
... ...
@@ -119,9 +140,17 @@ const SCEV *PointerTracking::computeAllocationCount(Value *P,
119 119
     return SE->getSCEV(arraySize);
120 120
   }
121 121
 
122
+#if LLVM_VERSION < 32
122 123
   if (CallInst *CI = extractMallocCall(V)) {
123 124
     Value *arraySize = getMallocArraySize(CI, TD);
124 125
     constType* AllocTy = getMallocAllocatedType(CI);
126
+#else
127
+  TargetLibraryInfo* TLI = new TargetLibraryInfo();
128
+
129
+  if (CallInst *CI = extractMallocCall(V, TLI)) {
130
+    Value *arraySize = getMallocArraySize(CI, TD, TLI);
131
+    constType* AllocTy = getMallocAllocatedType(CI, TLI);
132
+#endif
125 133
     if (!AllocTy || !arraySize) return SE->getCouldNotCompute();
126 134
     Ty = AllocTy;
127 135
     // arraySize elements of type Ty.
... ...
@@ -171,11 +200,21 @@ Value *PointerTracking::computeAllocationCountValue(Value *P, constType *&Ty) co
171 171
     return AI->getArraySize();
172 172
   }
173 173
 
174
+#if LLVM_VERSION < 32
174 175
   if (CallInst *CI = extractMallocCall(V)) {
175 176
     Ty = getMallocAllocatedType(CI);
176 177
     if (!Ty)
177 178
       return 0;
178 179
     Value *arraySize = getMallocArraySize(CI, TD);
180
+#else
181
+  TargetLibraryInfo* TLI = new TargetLibraryInfo();
182
+
183
+  if (CallInst *CI = extractMallocCall(V, TLI)) {
184
+    Ty = getMallocAllocatedType(CI, TLI);
185
+    if (!Ty)
186
+      return 0;
187
+    Value *arraySize = getMallocArraySize(CI, TD, TLI);
188
+#endif
179 189
     if (!arraySize) {
180 190
       Ty = Type::getInt8Ty(P->getContext());
181 191
       return CI->getArgOperand(0);
... ...
@@ -29,18 +29,27 @@
29 29
 
30 30
 #include "llvm/ADT/SmallPtrSet.h"
31 31
 #include "llvm/Analysis/Dominators.h"
32
-#include "llvm/Instructions.h"
33 32
 #include "llvm/Pass.h"
34 33
 #include "llvm/Support/PredIteratorCache.h"
35 34
 #include "llvm30_compat.h"
36 35
 
36
+#if LLVM_VERSION < 33
37
+#include "llvm/Instructions.h"
38
+#else
39
+#include "llvm/IR/Instructions.h"
40
+#endif
41
+
37 42
 namespace llvm {
38 43
   class DominatorTree;
39 44
   class ScalarEvolution;
40 45
   class SCEV;
41 46
   class Loop;
42 47
   class LoopInfo;
48
+#if LLVM_VERSION < 32
43 49
   class TargetData;
50
+#else
51
+  class DataLayout;
52
+#endif
44 53
 
45 54
   // Result from solver, assuming pointer is not NULL,
46 55
   // and it is not a use-after-free situation.
... ...
@@ -106,7 +115,11 @@ namespace llvm {
106 106
     Value *computeAllocationCountValue(Value *P, constType *&Ty) const;
107 107
   private:
108 108
     Function *FF;
109
+#if LLVM_VERSION < 32
109 110
     TargetData *TD;
111
+#else
112
+    DataLayout *TD;
113
+#endif
110 114
     ScalarEvolution *SE;
111 115
     LoopInfo *LI;
112 116
     DominatorTree *DT;
... ...
@@ -24,10 +24,8 @@
24 24
 #ifndef _WIN32
25 25
 #include <sys/time.h>
26 26
 #endif
27
-
28 27
 #include "ClamBCModule.h"
29 28
 #include "ClamBCDiagnostics.h"
30
-#include "llvm/Analysis/DebugInfo.h"
31 29
 #include "llvm/ADT/DenseMap.h"
32 30
 #include "llvm/ADT/BitVector.h"
33 31
 #include "llvm/ADT/PostOrderIterator.h"
... ...
@@ -40,15 +38,9 @@
40 40
 #include "llvm/Analysis/ScalarEvolution.h"
41 41
 #include "llvm/Analysis/Verifier.h"
42 42
 #include "llvm/AutoUpgrade.h"
43
-#include "llvm/CallingConv.h"
44
-#include "llvm/DerivedTypes.h"
45
-#include "llvm/Function.h"
46 43
 #include "llvm/ExecutionEngine/ExecutionEngine.h"
47 44
 #include "llvm/ExecutionEngine/JIT.h"
48 45
 #include "llvm/ExecutionEngine/JITEventListener.h"
49
-#include "llvm/LLVMContext.h"
50
-#include "llvm/Intrinsics.h"
51
-#include "llvm/Module.h"
52 46
 #include "llvm/PassManager.h"
53 47
 #include "llvm/Support/Compiler.h"
54 48
 #include "llvm/Support/Debug.h"
... ...
@@ -58,7 +50,6 @@
58 58
 #include "llvm/Support/MemoryBuffer.h"
59 59
 #include "llvm/Support/raw_ostream.h"
60 60
 #include "llvm/Support/SourceMgr.h"
61
-#include "llvm/Support/IRBuilder.h"
62 61
 #include "llvm/Support/PrettyStackTrace.h"
63 62
 
64 63
 #ifdef LLVM29
... ...
@@ -70,8 +61,12 @@
70 70
 #include "llvm/Support/Signals.h"
71 71
 #include "llvm/Support/Threading.h"
72 72
 #include "llvm/Support/ThreadLocal.h"
73
-#include "llvm/IntrinsicInst.h"
74 73
 #include "llvm/PassRegistry.h"
74
+#if LLVM_VERSION < 33
75
+#include "llvm/IntrinsicInst.h"
76
+#else
77
+#include "llvm/IR/IntrinsicInst.h"
78
+#endif
75 79
 #else
76 80
 #include "llvm/System/DataTypes.h"
77 81
 #include "llvm/System/Host.h"
... ...
@@ -94,7 +89,6 @@ void LLVMInitializePowerPCAsmPrinter();
94 94
 #else
95 95
 #include "llvm/Target/TargetSelect.h"
96 96
 #endif
97
-#include "llvm/Target/TargetData.h"
98 97
 #include "llvm/Target/TargetOptions.h"
99 98
 #include "llvm/Support/TargetFolder.h"
100 99
 #include "llvm/Transforms/Scalar.h"
... ...
@@ -106,13 +100,58 @@ void LLVMInitializePowerPCAsmPrinter();
106 106
 #include <cerrno>
107 107
 #include <string>
108 108
 
109
+#if LLVM_VERSION < 32
110
+#include "llvm/Analysis/DebugInfo.h"
111
+#else
112
+#include "llvm/DebugInfo.h"
113
+#endif
114
+
115
+#if LLVM_VERSION < 32
116
+#include "llvm/Support/IRBuilder.h"
117
+#include "llvm/Target/TargetData.h"
118
+#elif LLVM_VERSION < 33
119
+#include "llvm/IRBuilder.h"
120
+#include "llvm/DataLayout.h"
121
+#else
122
+#include "llvm/IR/IRBuilder.h"
123
+#include "llvm/IR/DataLayout.h"
124
+#endif
125
+
126
+#if LLVM_VERSION < 33
127
+#include "llvm/CallingConv.h"
128
+#include "llvm/DerivedTypes.h"
129
+#include "llvm/Function.h"
130
+#include "llvm/LLVMContext.h"
131
+#include "llvm/Intrinsics.h"
132
+#include "llvm/Module.h"
133
+#else
134
+#include "llvm/IR/CallingConv.h"
135
+#include "llvm/IR/DerivedTypes.h"
136
+#include "llvm/IR/Function.h"
137
+#include "llvm/IR/LLVMContext.h"
138
+#include "llvm/IR/Intrinsics.h"
139
+#include "llvm/IR/Module.h"
140
+#endif
141
+
142
+#if LLVM_VERSION >= 34
143
+#include "llvm/Analysis/CFG.h"
144
+#endif
145
+
109 146
 //#define TIMING
110 147
 #undef TIMING
111 148
 
112 149
 #include "llvm/Config/config.h"
150
+#ifdef ENABLE_THREADS
113 151
 #if !ENABLE_THREADS
114 152
 #error "Thread support was explicitly disabled. Cannot continue"
115 153
 #endif
154
+#endif
155
+
156
+#ifdef LLVM_ENABLE_THREADS
157
+#if !LLVM_ENABLE_THREADS
158
+#error "Thread support was explicitly disabled. Cannot continue"
159
+#endif
160
+#endif
116 161
 
117 162
 #ifdef _GLIBCXX_PARALLEL
118 163
 #error "libstdc++ parallel mode is not supported for ClamAV. Please remove -D_GLIBCXX_PARALLEL from CXXFLAGS!"
... ...
@@ -127,13 +166,15 @@ void LLVMInitializePowerPCAsmPrinter();
127 127
 #undef PACKAGE_URL
128 128
 #include "clamav-config.h"
129 129
 #endif
130
-
131 130
 #include "dconf.h"
132 131
 #include "clamav.h"
133 132
 #include "clambc.h"
134 133
 #include "bytecode.h"
135 134
 #include "bytecode_priv.h"
136 135
 #include "type_desc.h"
136
+extern "C" {
137
+#include "md5.h"
138
+}
137 139
 
138 140
 #define MODULE "libclamav JIT: "
139 141
 
... ...
@@ -267,7 +308,11 @@ static void NORETURN jit_ssp_handler(void)
267 267
     jit_exception_handler();
268 268
 }
269 269
 
270
+#if LLVM_VERSION < 33
270 271
 void llvm_error_handler(void *user_data, const std::string &reason)
272
+#else
273
+void llvm_error_handler(void *user_data, const std::string &reason, bool gen_crash_diag = true)
274
+#endif
271 275
 {
272 276
     // Output it to stderr, it might exceed the 1k/4k limit of cli_errmsg
273 277
     cli_errmsg("[Bytecode JIT]: [LLVM error] %s\n", reason.c_str());
... ...
@@ -376,7 +421,11 @@ public:
376 376
 	if (!cli_debug_flag)
377 377
 	    return;
378 378
 	cli_dbgmsg_internal("[Bytecode JIT]: emitted function %s of %ld bytes at %p\n",
379
+#if LLVM_VERSION < 31
379 380
 			    F.getNameStr().c_str(), (long)Size, Code);
381
+#else
382
+			    F.getName().str().c_str(), (long)Size, Code);
383
+#endif
380 384
     }
381 385
 };
382 386
 
... ...
@@ -669,8 +718,13 @@ public:
669 669
         CallInst* AbrtC = CallInst::Create(func_abort, "", AbrtBB);
670 670
         AbrtC->setCallingConv(CallingConv::C);
671 671
         AbrtC->setTailCall(true);
672
+#if LLVM_VERSION < 32
672 673
         AbrtC->setDoesNotReturn(true);
673 674
         AbrtC->setDoesNotThrow(true);
675
+#else
676
+        AbrtC->setDoesNotReturn();
677
+        AbrtC->setDoesNotThrow();
678
+#endif
674 679
         new UnreachableInst(F.getContext(), AbrtBB);
675 680
 	IRBuilder<false> Builder(F.getContext());
676 681
 
... ...
@@ -1020,7 +1074,11 @@ public:
1020 1020
 	: bc(bc), M(M), Context(M->getContext()), EE(EE),
1021 1021
 	PM(PM),PMUnsigned(PMUnsigned), TypeMap(), apiFuncs(apiFuncs),apiMap(apiMap),
1022 1022
 	compiledFunctions(cFuncs), BytecodeID("bc"+Twine(bc->id)),
1023
+#if LLVM_VERSION < 32
1023 1024
 	Folder(EE->getTargetData()), Builder(Context, Folder), Values(), CF(CF) {
1025
+#else
1026
+	Folder(EE->getDataLayout()), Builder(Context, Folder), Values(), CF(CF) {
1027
+#endif
1024 1028
 
1025 1029
 	for (unsigned i=0;i<cli_apicall_maxglobal - _FIRST_GLOBAL;i++) {
1026 1030
 	    unsigned id = cli_globals[i].globalid;
... ...
@@ -1151,7 +1209,11 @@ public:
1151 1151
 	    }
1152 1152
 	    V = SI->getOperand(0);
1153 1153
 	}
1154
+#if LLVM_VERSION < 32
1154 1155
 	if (EE->getTargetData()->getPointerSize() == 8) {
1156
+#else
1157
+	if (EE->getDataLayout()->getPointerSize() == 8) {
1158
+#endif
1155 1159
 	    // eliminate useless trunc, GEP can take i64 too
1156 1160
 	    if (TruncInst *I = dyn_cast<TruncInst>(V)) {
1157 1161
 		Value *Src = I->getOperand(0);
... ...
@@ -1226,7 +1288,12 @@ public:
1226 1226
 	    Functions[j]->setLinkage(GlobalValue::InternalLinkage);
1227 1227
 #ifdef C_LINUX
1228 1228
 	    /* bb #2270, this should really be fixed either by LLVM or GCC.*/
1229
+#if LLVM_VERSION < 32
1229 1230
 	    Functions[j]->addFnAttr(Attribute::constructStackAlignmentFromInt(16));
1231
+#else
1232
+	// TODO: How does this translate?
1233
+//	    Functions[j]->addFnAttr(Attribute::StackAlignment);
1234
+#endif
1230 1235
 #endif
1231 1236
 	}
1232 1237
 	constType *I32Ty = Type::getInt32Ty(Context);
... ...
@@ -1527,7 +1594,11 @@ public:
1527 1527
 			    }
1528 1528
 			    CallInst *CI = Builder.CreateCall(DestF, ARRAYREF(Value*, args.begin(), args.end()));
1529 1529
 			    CI->setCallingConv(CallingConv::Fast);
1530
+#if LLVM_VERSION < 32
1530 1531
 			    CI->setDoesNotThrow(true);
1532
+#else
1533
+			    CI->setDoesNotThrow();
1534
+#endif
1531 1535
 			    if (CI->getType()->getTypeID() != Type::VoidTyID)
1532 1536
 				Store(inst->dest, CI);
1533 1537
 			    break;
... ...
@@ -1548,7 +1619,11 @@ public:
1548 1548
 				args.push_back(convertOperand(func, DestF->getFunctionType()->getParamType(a+1), op));
1549 1549
 			    }
1550 1550
 			    CallInst *CI = Builder.CreateCall(DestF, ARRAYREFVECTOR(Value*, args));
1551
+#if LLVM_VERSION < 32
1551 1552
 			    CI->setDoesNotThrow(true);
1553
+#else
1554
+			    CI->setDoesNotThrow();
1555
+#endif
1552 1556
 			    Store(inst->dest, CI);
1553 1557
 			    }
1554 1558
 			    break;
... ...
@@ -1681,7 +1756,11 @@ public:
1681 1681
 			    Dst = Builder.CreatePointerCast(Dst, PointerType::getUnqual(Type::getInt8Ty(Context)));
1682 1682
 			    Value *Src = convertOperand(func, inst, inst->u.three[1]);
1683 1683
 			    Src = Builder.CreatePointerCast(Src, PointerType::getUnqual(Type::getInt8Ty(Context)));
1684
+#if LLVM_VERSION < 32
1684 1685
 			    Value *Len = convertOperand(func, EE->getTargetData()->getIntPtrType(Context), inst->u.three[2]);
1686
+#else
1687
+			    Value *Len = convertOperand(func, EE->getDataLayout()->getIntPtrType(Context), inst->u.three[2]);
1688
+#endif
1685 1689
 			    CallInst *c = Builder.CreateCall3(CF->FRealmemcmp, Dst, Src, Len);
1686 1690
 			    c->setTailCall(true);
1687 1691
 			    c->setDoesNotThrow();
... ...
@@ -1706,7 +1785,11 @@ public:
1706 1706
 			    {
1707 1707
 				CallInst *C = Builder.CreateCall(CF->FBSwap16, convertOperand(func, inst, inst->u.unaryop));
1708 1708
 				C->setTailCall(true);
1709
+#if LLVM_VERSION < 32
1709 1710
 				C->setDoesNotThrow(true);
1711
+#else
1712
+				C->setDoesNotThrow();
1713
+#endif
1710 1714
 				Store(inst->dest, C);
1711 1715
 				break;
1712 1716
 			    }
... ...
@@ -1714,7 +1797,11 @@ public:
1714 1714
 			    {
1715 1715
 				CallInst *C = Builder.CreateCall(CF->FBSwap32, convertOperand(func, inst, inst->u.unaryop));
1716 1716
 				C->setTailCall(true);
1717
+#if LLVM_VERSION < 32
1717 1718
 				C->setDoesNotThrow(true);
1719
+#else
1720
+				C->setDoesNotThrow();
1721
+#endif
1718 1722
 				Store(inst->dest, C);
1719 1723
 				break;
1720 1724
 			    }
... ...
@@ -1722,7 +1809,11 @@ public:
1722 1722
 			    {
1723 1723
 				CallInst *C = Builder.CreateCall(CF->FBSwap64, convertOperand(func, inst, inst->u.unaryop));
1724 1724
 				C->setTailCall(true);
1725
+#if LLVM_VERSION < 32
1725 1726
 				C->setDoesNotThrow(true);
1727
+#else
1728
+				C->setDoesNotThrow();
1729
+#endif
1726 1730
 				Store(inst->dest, C);
1727 1731
 				break;
1728 1732
 			    }
... ...
@@ -1827,7 +1918,11 @@ public:
1827 1827
 	// entrypoint can only be C, emit wrapper
1828 1828
 	Function *F = Function::Create(Functions[0]->getFunctionType(),
1829 1829
 				       Function::ExternalLinkage,
1830
+#if LLVM_VERSION < 33
1830 1831
 				       Functions[0]->getName()+"_wrap", M);
1832
+#else
1833
+				       Functions[0]->getName().str()+"_wrap", M);
1834
+#endif
1831 1835
 	F->setDoesNotThrow();
1832 1836
 	BasicBlock *BB = BasicBlock::Create(Context, "", F);
1833 1837
 	std::vector<Value*> Args;
... ...
@@ -1886,8 +1981,11 @@ static void addFunctionProtos(struct CommonFunctions *CF, ExecutionEngine *EE, M
1886 1886
 					  "clamjit.fail", M);
1887 1887
     CF->FHandler->setDoesNotReturn();
1888 1888
     CF->FHandler->setDoesNotThrow();
1889
+#if LLVM_VERSION == 32
1890
+    CF->FHandler->addFnAttr(Attributes::NoInline);
1891
+#else
1889 1892
     CF->FHandler->addFnAttr(Attribute::NoInline);
1890
-
1893
+#endif
1891 1894
     EE->addGlobalMapping(CF->FHandler, (void*)(intptr_t)jit_exception_handler);
1892 1895
     EE->InstallLazyFunctionCreator(noUnknownFunctions);
1893 1896
     EE->getPointerToFunction(CF->FHandler);
... ...
@@ -1910,7 +2008,11 @@ static void addFunctionProtos(struct CommonFunctions *CF, ExecutionEngine *EE, M
1910 1910
 #endif
1911 1911
 					 M);
1912 1912
     CF->FMemset->setDoesNotThrow();
1913
+#if LLVM_VERSION < 32
1913 1914
     CF->FMemset->setDoesNotCapture(1, true);
1915
+#else
1916
+    CF->FMemset->setDoesNotCapture(1);
1917
+#endif
1914 1918
 
1915 1919
     args.clear();
1916 1920
     args.push_back(PointerType::getUnqual(Type::getInt8Ty(Context)));
... ...
@@ -1930,7 +2032,11 @@ static void addFunctionProtos(struct CommonFunctions *CF, ExecutionEngine *EE, M
1930 1930
 #endif
1931 1931
 					  M);
1932 1932
     CF->FMemmove->setDoesNotThrow();
1933
+#if LLVM_VERSION < 32
1933 1934
     CF->FMemmove->setDoesNotCapture(1, true);
1935
+#else
1936
+    CF->FMemmove->setDoesNotCapture(1);
1937
+#endif
1934 1938
 
1935 1939
     CF->FMemcpy = Function::Create(FuncTy_4, GlobalValue::ExternalLinkage,
1936 1940
 #ifdef LLVM30
... ...
@@ -1940,7 +2046,11 @@ static void addFunctionProtos(struct CommonFunctions *CF, ExecutionEngine *EE, M
1940 1940
 #endif
1941 1941
 					 M);
1942 1942
     CF->FMemcpy->setDoesNotThrow();
1943
+#if LLVM_VERSION < 32
1943 1944
     CF->FMemcpy->setDoesNotCapture(1, true);
1945
+#else
1946
+    CF->FMemcpy->setDoesNotCapture(1);
1947
+#endif
1944 1948
 
1945 1949
     args.clear();
1946 1950
     args.push_back(Type::getInt16Ty(Context));
... ...
@@ -1980,7 +2090,11 @@ static void addFunctionProtos(struct CommonFunctions *CF, ExecutionEngine *EE, M
1980 1980
     args.clear();
1981 1981
     args.push_back(PointerType::getUnqual(Type::getInt8Ty(Context)));
1982 1982
     args.push_back(PointerType::getUnqual(Type::getInt8Ty(Context)));
1983
+#if LLVM_VERSION < 32
1983 1984
     args.push_back(EE->getTargetData()->getIntPtrType(Context));
1985
+#else
1986
+    args.push_back(EE->getDataLayout()->getIntPtrType(Context));
1987
+#endif
1984 1988
     FuncTy_5 = FunctionType::get(Type::getInt32Ty(Context),
1985 1989
 				 args, false);
1986 1990
     CF->FRealmemcmp = Function::Create(FuncTy_5, GlobalValue::ExternalLinkage, "memcmp", M);
... ...
@@ -2200,19 +2314,29 @@ int cli_vm_execute_jit(const struct cli_all_bc *bcs, struct cli_bc_ctx *ctx,
2200 2200
 static unsigned char name_salt[16] = { 16, 38, 97, 12, 8, 4, 72, 196, 217, 144, 33, 124, 18, 11, 17, 253 };
2201 2201
 static void setGuard(unsigned char* guardbuf)
2202 2202
 {
2203
+    cli_md5_ctx ctx;
2203 2204
     char salt[48];
2204 2205
     memcpy(salt, name_salt, 16);
2205 2206
     for(unsigned i = 16; i < 48; i++)
2206
-        salt[i] = cli_rndnum(255);
2207
+	salt[i] = cli_rndnum(255);
2207 2208
 
2208
-    cl_hash_data("md5", salt, 48, guardbuf, NULL);
2209
+    cli_md5_init(&ctx);
2210
+    cli_md5_update(&ctx, salt, 48);
2211
+    cli_md5_final(guardbuf, &ctx);
2209 2212
 }
2210
-
2213
+#if LLVM_VERSION < 32
2211 2214
 static void addFPasses(FunctionPassManager &FPM, bool trusted, const TargetData *TD)
2215
+#else
2216
+static void addFPasses(FunctionPassManager &FPM, bool trusted, const DataLayout *TD)
2217
+#endif
2212 2218
 {
2213 2219
     // Set up the optimizer pipeline.  Start with registering info about how
2214 2220
     // the target lays out data structures.
2221
+#if LLVM_VERSION < 32
2215 2222
     FPM.add(new TargetData(*TD));
2223
+#else
2224
+    FPM.add(new DataLayout(*TD));
2225
+#endif
2216 2226
     // Promote allocas to registers.
2217 2227
     FPM.add(createPromoteMemoryToRegisterPass());
2218 2228
     FPM.add(new BrSimplifier());
... ...
@@ -2234,6 +2358,24 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
2234 2234
 	// Create the JIT.
2235 2235
 	std::string ErrorMsg;
2236 2236
 	EngineBuilder builder(M);
2237
+
2238
+#if LLVM_VERSION >= 31
2239
+	TargetOptions Options;
2240
+#ifdef CL_DEBUG
2241
+	//disable this for now, it leaks
2242
+	Options.JITEmitDebugInfo = false;
2243
+//	Options.JITEmitDebugInfo = true;
2244
+#else
2245
+	Options.JITEmitDebugInfo = false;
2246
+#endif
2247
+#if LLVM_VERSION < 34
2248
+	Options.DwarfExceptionHandling = false;
2249
+#else
2250
+	// TODO: How to do this now?
2251
+#endif
2252
+	builder.setTargetOptions(Options);
2253
+#endif
2254
+
2237 2255
 	builder.setErrorStr(&ErrorMsg);
2238 2256
 	builder.setEngineKind(EngineKind::JIT);
2239 2257
 	builder.setOptLevel(CodeGenOpt::Default);
... ...
@@ -2258,11 +2400,24 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
2258 2258
 	addFunctionProtos(&CF, EE, M);
2259 2259
 
2260 2260
 	FunctionPassManager OurFPM(M), OurFPMUnsigned(M);
2261
+#if LLVM_VERSION < 32
2261 2262
 	M->setDataLayout(EE->getTargetData()->getStringRepresentation());
2263
+#else
2264
+	M->setDataLayout(EE->getDataLayout()->getStringRepresentation());
2265
+#endif
2266
+#if LLVM_VERSION < 31
2262 2267
 	M->setTargetTriple(sys::getHostTriple());
2263
-
2268
+#else
2269
+	M->setTargetTriple(sys::getDefaultTargetTriple());
2270
+#endif
2271
+#if LLVM_VERSION < 32
2264 2272
 	addFPasses(OurFPM, true, EE->getTargetData());
2265 2273
 	addFPasses(OurFPMUnsigned, false, EE->getTargetData());
2274
+#else
2275
+	addFPasses(OurFPM, true, EE->getDataLayout());
2276
+	addFPasses(OurFPMUnsigned, false, EE->getDataLayout());
2277
+#endif
2278
+
2266 2279
 
2267 2280
 	//TODO: create a wrapper that calls pthread_getspecific
2268 2281
 	unsigned maxh = cli_globals[0].offset + sizeof(struct cli_bc_hooks);
... ...
@@ -2369,7 +2524,11 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
2369 2369
 	    }
2370 2370
 	}
2371 2371
 	PassManager PM;
2372
+#if LLVM_VERSION < 32
2372 2373
 	PM.add(new TargetData(*EE->getTargetData()));
2374
+#else
2375
+	PM.add(new DataLayout(*EE->getDataLayout()));
2376
+#endif
2373 2377
 	// TODO: only run this on the untrusted bytecodes, not all of them...
2374 2378
 	if (has_untrusted)
2375 2379
 	    PM.add(createClamBCRTChecks());
... ...
@@ -2434,11 +2593,17 @@ int bytecode_init(void)
2434 2434
     llvm_install_error_handler(llvm_error_handler);
2435 2435
 #ifdef CL_DEBUG
2436 2436
     sys::PrintStackTraceOnErrorSignal();
2437
+#if LLVM_VERSION >= 34
2438
+    llvm::EnablePrettyStackTrace();
2439
+#endif
2437 2440
 #else
2441
+#if LLVM_VERSION < 34
2438 2442
     llvm::DisablePrettyStackTrace = true;
2439 2443
 #endif
2444
+#endif
2440 2445
     atexit(do_shutdown);
2441 2446
 
2447
+#if LLVM_VERSION < 31
2442 2448
 #ifdef CL_DEBUG
2443 2449
     //disable this for now, it leaks
2444 2450
     llvm::JITEmitDebugInfo = false;
... ...
@@ -2447,6 +2612,7 @@ int bytecode_init(void)
2447 2447
     llvm::JITEmitDebugInfo = false;
2448 2448
 #endif
2449 2449
     llvm::DwarfExceptionHandling = false;
2450
+#endif
2450 2451
     llvm_start_multithreaded();
2451 2452
 
2452 2453
     // If we have a native target, initialize it to ensure it is linked in and
... ...
@@ -2605,7 +2771,11 @@ namespace ClamBCModule {
2605 2605
 void stop(const char *msg, llvm::Function* F, llvm::Instruction* I)
2606 2606
 {
2607 2607
     if (F && F->hasName()) {
2608
+#if LLVM_VERSION < 31
2608 2609
 	cli_warnmsg("[Bytecode JIT] in function %s: %s", F->getNameStr().c_str(), msg);
2610
+#else
2611
+	cli_warnmsg("[Bytecode JIT] in function %s: %s", F->getName().str().c_str(), msg);
2612
+#endif
2609 2613
     } else {
2610 2614
 	cli_warnmsg("[Bytecode JIT] %s", msg);
2611 2615
     }
... ...
@@ -2672,7 +2842,12 @@ static const DbgDeclareInst *findDbgDeclare(const Value *V) {
2672 2672
 static bool getLocationInfo(const Value *V, std::string &DisplayName,
2673 2673
                             std::string &Type, unsigned &LineNo,
2674 2674
                             std::string &File, std::string &Dir) {
2675
+#if LLVM_VERSION < 33
2675 2676
   DICompileUnit Unit;
2677
+#else
2678
+  StringRef G;
2679
+  StringRef H;
2680
+#endif
2676 2681
   DIType TypeD;
2677 2682
 
2678 2683
   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
... ...
@@ -2684,7 +2859,12 @@ static bool getLocationInfo(const Value *V, std::string &DisplayName,
2684 2684
     if (!D.empty())
2685 2685
       DisplayName = D;
2686 2686
     LineNo = Var.getLineNumber();
2687
+#if LLVM_VERSION < 33
2687 2688
     Unit = Var.getCompileUnit();
2689
+#else
2690
+    G = Var.getFilename();
2691
+    H = Var.getDirectory();
2692
+#endif
2688 2693
     TypeD = Var.getType();
2689 2694
   } else if (Function *F = dyn_cast<Function>(const_cast<Value*>(V))){
2690 2695
     Value *DIF = findDbgSubprogramDeclare(F);
... ...
@@ -2695,7 +2875,12 @@ static bool getLocationInfo(const Value *V, std::string &DisplayName,
2695 2695
     if (!D.empty())
2696 2696
       DisplayName = D;
2697 2697
     LineNo = Var.getLineNumber();
2698
+#if LLVM_VERSION < 33
2698 2699
     Unit = Var.getCompileUnit();
2700
+#else
2701
+    G = Var.getFilename();
2702
+    H = Var.getDirectory();
2703
+#endif
2699 2704
     TypeD = Var.getType();
2700 2705
   } else {
2701 2706
     const DbgDeclareInst *DDI = findDbgDeclare(V);
... ...
@@ -2706,19 +2891,27 @@ static bool getLocationInfo(const Value *V, std::string &DisplayName,
2706 2706
     if (!D.empty())
2707 2707
       DisplayName = D;
2708 2708
     LineNo = Var.getLineNumber();
2709
+#if LLVM_VERSION < 33
2709 2710
     Unit = Var.getCompileUnit();
2711
+#else
2712
+    // getFilename and getDirectory are not defined
2713
+    G = StringRef();
2714
+    H = StringRef();
2715
+#endif
2710 2716
     TypeD = Var.getType();
2711 2717
   }
2712 2718
 
2713 2719
   StringRef T = TypeD.getName();
2714 2720
   if (!T.empty())
2715 2721
     Type = T;
2716
-  StringRef F = Unit.getFilename();
2717
-  if (!F.empty())
2718
-    File = F;
2719
-  StringRef D = Unit.getDirectory();
2720
-  if (!D.empty())
2721
-    Dir = D;
2722
+#if LLVM_VERSION < 33
2723
+  StringRef G = Unit.getFilename();
2724
+  StringRef H = Unit.getDirectory();
2725
+#endif
2726
+  if (!G.empty())
2727
+    File = G;
2728
+  if (!H.empty())
2729
+    Dir = H;
2722 2730
   return true;
2723 2731
 }
2724 2732
 #endif
... ...
@@ -59,10 +59,7 @@ AC_ARG_WITH([system-llvm], AC_HELP_STRING([-with-system-llvm],
59 59
   *)
60 60
     llvmconfig="$withval"
61 61
     llvmver=`$llvmconfig --version`
62
-    if test "$llvmver" != "2.9" && test "$llvmver" != "3.0svn" &&
63
-	test "$llvmver" != "3.0"; then
64
-	AC_MSG_ERROR([LLVM 2.9 required, but "$llvmver" found])
65
-    fi
62
+
66 63
     AC_SUBST(LLVMCONFIG_CXXFLAGS, [`$llvmconfig --cxxflags`])
67 64
     AC_SUBST(LLVMCONFIG_LDFLAGS, [`$llvmconfig --ldflags`])
68 65
     AC_SUBST(LLVMCONFIG_LIBS, [`$llvmconfig --libs jit nativecodegen scalaropts ipo`])
... ...
@@ -75,9 +72,24 @@ AC_ARG_WITH([system-llvm], AC_HELP_STRING([-with-system-llvm],
75 75
   esac
76 76
 ])
77 77
 
78
+AC_MSG_CHECKING([for supported LLVM version])
78 79
 if test "x$llvmconfig" = "x"; then
79 80
     AC_CONFIG_SUBDIRS([llvm])
81
+    llvmver=`llvm/configure --version | sed -n 1p | sed 's/llvm configure //'`
82
+fi
83
+
84
+llvmver_int=`echo "$llvmver" | sed -e 's/\.//g' | sed -e 's/svn//g'`
85
+
86
+if test $llvmver_int -lt 28; then
87
+    AC_MSG_RESULT([no ($llvmver)])
88
+    AC_MSG_ERROR([LLVM >= 2.8 required, but "$llvmver" found])
89
+elif test $llvmver_int -gt 34; then
90
+    AC_MSG_RESULT([no ($llvmver)])
91
+    AC_MSG_ERROR([LLVM <= 3.4 required, but "$llvmver" found])
92
+else
93
+AC_MSG_RESULT([ok ($llvmver)])
80 94
 fi
95
+AC_SUBST([LLVM_VERSION], [$llvmver_int])
81 96
 
82 97
 AC_ARG_ENABLE([llvm],AC_HELP_STRING([-enable-llvm],
83 98
 				    [Enable 'llvm' JIT/verifier support @<:@default=auto@:>@]),
... ...
@@ -56,16 +56,27 @@ static void warn_assumptions(const char *msg, int a, int b)
56 56
 
57 57
 void cli_detect_env_jit(struct cli_environment *env)
58 58
 {
59
+#if LLVM_VERSION < 31
59 60
     std::string host_triple = sys::getHostTriple();
61
+#else
62
+    std::string host_triple = sys::getDefaultTargetTriple();
63
+#endif
60 64
     INIT_STRFIELD(env->triple, host_triple.c_str());
61 65
 
62 66
     std::string cpu = sys::getHostCPUName();
63 67
     INIT_STRFIELD(env->cpu, cpu.c_str());
64 68
 
69
+#if LLVM_VERSION < 33
65 70
     if (env->big_endian != (int)sys::isBigEndianHost()) {
66 71
 	warn_assumptions("host endianness", env->big_endian, sys::isBigEndianHost());
67 72
 	env->big_endian = sys::isBigEndianHost();
68 73
     }
74
+#else
75
+    if (env->big_endian != (int)sys::IsBigEndianHost) {
76
+	warn_assumptions("host endianness", env->big_endian, sys::IsBigEndianHost);
77
+	env->big_endian = sys::IsBigEndianHost;
78
+    }
79
+#endif
69 80
 
70 81
 #ifdef __GNUC__
71 82
     env->cpp_version = MAKE_VERSION(0, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
... ...
@@ -149,7 +160,9 @@ void cli_detect_env_jit(struct cli_environment *env)
149 149
 #endif
150 150
 	CASE_OS(NetBSD,  os_bsd);
151 151
 	CASE_OS(OpenBSD, os_bsd);
152
+#if LLVM_VERSION < 31
152 153
 	CASE_OS(Psp, os_unknown);
154
+#endif
153 155
 	CASE_OS(Solaris, os_solaris);
154 156
 	case Triple::Win32:
155 157
 	     env->os = llvm_os_Win32;