blob: fc6bb7b2eada4c71ab6a1532ecca33823f63b37c [file] [log] [blame]
Amaury Sechete8ea7d82016-02-04 23:26:19 +00001//===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the --echo commands in llvm-c-test.
11//
12// This command uses the C API to read a module and output an exact copy of it
13// as output. It is used to check that the resulting module matches the input
14// to validate that the C API can read and write modules properly.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm-c-test.h"
Amaury Sechet55909672016-02-16 05:11:24 +000019#include "llvm-c/Target.h"
Amaury Sechete8ea7d82016-02-04 23:26:19 +000020#include "llvm/ADT/DenseMap.h"
Amaury Secheta82042e2016-02-09 22:36:41 +000021#include "llvm/Support/ErrorHandling.h"
Amaury Sechete8ea7d82016-02-04 23:26:19 +000022
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000023#include <stdio.h>
24#include <stdlib.h>
Amaury Sechete8ea7d82016-02-04 23:26:19 +000025
26using namespace llvm;
27
28// Provide DenseMapInfo for C API opaque types.
29template<typename T>
30struct CAPIDenseMap {};
31
32// The default DenseMapInfo require to know about pointer alignement.
33// Because the C API uses opaques pointer types, their alignement is unknown.
34// As a result, we need to roll out our own implementation.
35template<typename T>
36struct CAPIDenseMap<T*> {
37 struct CAPIDenseMapInfo {
38 static inline T* getEmptyKey() {
39 uintptr_t Val = static_cast<uintptr_t>(-1);
40 return reinterpret_cast<T*>(Val);
41 }
42 static inline T* getTombstoneKey() {
43 uintptr_t Val = static_cast<uintptr_t>(-2);
44 return reinterpret_cast<T*>(Val);
45 }
46 static unsigned getHashValue(const T *PtrVal) {
47 return hash_value(PtrVal);
48 }
49 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
50 };
51
52 typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map;
53};
54
55typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap;
Amaury Secheta82042e2016-02-09 22:36:41 +000056typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +000057
Amaury Sechetaad93532016-02-10 00:38:50 +000058struct TypeCloner {
59 LLVMModuleRef M;
60 LLVMContextRef Ctx;
61
62 TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {}
63
Amaury Sechetc679dbd2016-02-14 09:30:42 +000064 LLVMTypeRef Clone(LLVMValueRef Src) {
65 return Clone(LLVMTypeOf(Src));
66 }
67
Amaury Sechetaad93532016-02-10 00:38:50 +000068 LLVMTypeRef Clone(LLVMTypeRef Src) {
69 LLVMTypeKind Kind = LLVMGetTypeKind(Src);
70 switch (Kind) {
71 case LLVMVoidTypeKind:
72 return LLVMVoidTypeInContext(Ctx);
73 case LLVMHalfTypeKind:
74 return LLVMHalfTypeInContext(Ctx);
75 case LLVMFloatTypeKind:
76 return LLVMFloatTypeInContext(Ctx);
77 case LLVMDoubleTypeKind:
78 return LLVMDoubleTypeInContext(Ctx);
79 case LLVMX86_FP80TypeKind:
80 return LLVMX86FP80TypeInContext(Ctx);
81 case LLVMFP128TypeKind:
82 return LLVMFP128TypeInContext(Ctx);
83 case LLVMPPC_FP128TypeKind:
84 return LLVMPPCFP128TypeInContext(Ctx);
85 case LLVMLabelTypeKind:
86 return LLVMLabelTypeInContext(Ctx);
87 case LLVMIntegerTypeKind:
88 return LLVMIntTypeInContext(Ctx, LLVMGetIntTypeWidth(Src));
89 case LLVMFunctionTypeKind: {
90 unsigned ParamCount = LLVMCountParamTypes(Src);
91 LLVMTypeRef* Params = nullptr;
92 if (ParamCount > 0) {
93 Params = (LLVMTypeRef*) malloc(ParamCount * sizeof(LLVMTypeRef));
94 LLVMGetParamTypes(Src, Params);
95 for (unsigned i = 0; i < ParamCount; i++)
96 Params[i] = Clone(Params[i]);
97 }
98
99 LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)),
100 Params, ParamCount,
101 LLVMIsFunctionVarArg(Src));
102 if (ParamCount > 0)
103 free(Params);
104 return FunTy;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000105 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000106 case LLVMStructTypeKind: {
107 LLVMTypeRef S = nullptr;
108 const char *Name = LLVMGetStructName(Src);
109 if (Name) {
110 S = LLVMGetTypeByName(M, Name);
111 if (S)
112 return S;
113 S = LLVMStructCreateNamed(Ctx, Name);
114 if (LLVMIsOpaqueStruct(Src))
115 return S;
116 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000117
Amaury Sechetaad93532016-02-10 00:38:50 +0000118 unsigned EltCount = LLVMCountStructElementTypes(Src);
119 SmallVector<LLVMTypeRef, 8> Elts;
120 for (unsigned i = 0; i < EltCount; i++)
121 Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i)));
122 if (Name)
123 LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src));
124 else
125 S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount,
126 LLVMIsPackedStruct(Src));
127 return S;
128 }
129 case LLVMArrayTypeKind:
130 return LLVMArrayType(
131 Clone(LLVMGetElementType(Src)),
132 LLVMGetArrayLength(Src)
133 );
134 case LLVMPointerTypeKind:
135 return LLVMPointerType(
136 Clone(LLVMGetElementType(Src)),
137 LLVMGetPointerAddressSpace(Src)
138 );
139 case LLVMVectorTypeKind:
140 return LLVMVectorType(
141 Clone(LLVMGetElementType(Src)),
142 LLVMGetVectorSize(Src)
143 );
144 case LLVMMetadataTypeKind:
145 break;
146 case LLVMX86_MMXTypeKind:
147 return LLVMX86MMXTypeInContext(Ctx);
148 default:
149 break;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000150 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000151
Amaury Sechetaad93532016-02-10 00:38:50 +0000152 fprintf(stderr, "%d is not a supported typekind\n", Kind);
153 exit(-1);
154 }
155};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000156
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000157static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000158 unsigned Count = LLVMCountParams(Src);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000159 if (Count != LLVMCountParams(Dst))
160 report_fatal_error("Parameter count mismatch");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000161
162 ValueMap VMap;
163 if (Count == 0)
164 return VMap;
165
166 LLVMValueRef SrcFirst = LLVMGetFirstParam(Src);
167 LLVMValueRef DstFirst = LLVMGetFirstParam(Dst);
168 LLVMValueRef SrcLast = LLVMGetLastParam(Src);
169 LLVMValueRef DstLast = LLVMGetLastParam(Dst);
170
171 LLVMValueRef SrcCur = SrcFirst;
172 LLVMValueRef DstCur = DstFirst;
173 LLVMValueRef SrcNext = nullptr;
174 LLVMValueRef DstNext = nullptr;
175 while (true) {
176 const char *Name = LLVMGetValueName(SrcCur);
177 LLVMSetValueName(DstCur, Name);
178
179 VMap[SrcCur] = DstCur;
180
181 Count--;
182 SrcNext = LLVMGetNextParam(SrcCur);
183 DstNext = LLVMGetNextParam(DstCur);
184 if (SrcNext == nullptr && DstNext == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000185 if (SrcCur != SrcLast)
186 report_fatal_error("SrcLast param does not match End");
187 if (DstCur != DstLast)
188 report_fatal_error("DstLast param does not match End");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000189 break;
190 }
191
Amaury Sechetd01c8612016-02-14 10:06:34 +0000192 if (SrcNext == nullptr)
193 report_fatal_error("SrcNext was unexpectedly null");
194 if (DstNext == nullptr)
195 report_fatal_error("DstNext was unexpectedly null");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000196
197 LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000198 if (SrcPrev != SrcCur)
199 report_fatal_error("SrcNext.Previous param is not Current");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000200
201 LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000202 if (DstPrev != DstCur)
203 report_fatal_error("DstNext.Previous param is not Current");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000204
205 SrcCur = SrcNext;
206 DstCur = DstNext;
207 }
208
Amaury Sechetd01c8612016-02-14 10:06:34 +0000209 if (Count != 0)
210 report_fatal_error("Parameter count does not match iteration");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000211
212 return VMap;
213}
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000214
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000215LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
Peter Zotov3e4561c2016-04-06 22:21:29 +0000216 LLVMValueRef Ret;
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000217 if (!LLVMIsAConstant(Cst))
218 report_fatal_error("Expected a constant");
219
220 // Maybe it is a symbol
221 if (LLVMIsAGlobalValue(Cst)) {
222 const char *Name = LLVMGetValueName(Cst);
223
224 // Try function
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000225 if (LLVMIsAFunction(Cst)) {
Peter Zotov3e4561c2016-04-06 22:21:29 +0000226 if (LLVMGetValueKind(Cst) != LLVMFunctionValueKind)
227 report_fatal_error("LLVMGetValueKind returned incorrect type");
228 Ret = LLVMGetNamedFunction(M, Name);
229 if (!Ret)
230 report_fatal_error("Could not find function");
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000231 // Try global variable
Peter Zotov3e4561c2016-04-06 22:21:29 +0000232 } else if (LLVMIsAGlobalVariable(Cst)) {
233 if (LLVMGetValueKind(Cst) != LLVMGlobalVariableValueKind)
234 report_fatal_error("LLVMGetValueKind returned incorrect type");
235 Ret = LLVMGetNamedGlobal(M, Name);
236 if (!Ret)
237 report_fatal_error("Could not find function");
238 } else {
239 fprintf(stderr, "Could not find @%s\n", Name);
240 exit(-1);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000241 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000242 // Try integer literal
Peter Zotov3e4561c2016-04-06 22:21:29 +0000243 } else if (LLVMIsAConstantInt(Cst)) {
244 if (LLVMGetValueKind(Cst) != LLVMConstantIntValueKind)
245 report_fatal_error("LLVMGetValueKind returned incorrect type");
246 Ret = LLVMConstInt(TypeCloner(M).Clone(Cst), LLVMConstIntGetZExtValue(Cst),
247 false);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000248 // Try zeroinitializer
Peter Zotov3e4561c2016-04-06 22:21:29 +0000249 } else if (LLVMIsAConstantAggregateZero(Cst)) {
250 if (LLVMGetValueKind(Cst) != LLVMConstantAggregateZeroValueKind)
251 report_fatal_error("LLVMGetValueKind returned incorrect type");
252 Ret = LLVMConstNull(TypeCloner(M).Clone(Cst));
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000253 // Try constant array
Peter Zotov3e4561c2016-04-06 22:21:29 +0000254 } else if (LLVMIsAConstantArray(Cst)) {
255 if (LLVMGetValueKind(Cst) != LLVMConstantArrayValueKind)
256 report_fatal_error("LLVMGetValueKind returned incorrect type");
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000257 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
258 unsigned EltCount = LLVMGetArrayLength(Ty);
259 SmallVector<LLVMValueRef, 8> Elts;
260 for (unsigned i = 0; i < EltCount; i++)
261 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
Peter Zotov3e4561c2016-04-06 22:21:29 +0000262 Ret = LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
Amaury Sechetb3256862016-03-13 00:58:25 +0000263 // Try contant data array
Peter Zotov3e4561c2016-04-06 22:21:29 +0000264 } else if (LLVMIsAConstantDataArray(Cst)) {
265 if (LLVMGetValueKind(Cst) != LLVMConstantDataArrayValueKind)
266 report_fatal_error("LLVMGetValueKind returned incorrect type");
Amaury Sechetb3256862016-03-13 00:58:25 +0000267 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
268 unsigned EltCount = LLVMGetArrayLength(Ty);
269 SmallVector<LLVMValueRef, 8> Elts;
270 for (unsigned i = 0; i < EltCount; i++)
271 Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
Peter Zotov3e4561c2016-04-06 22:21:29 +0000272 Ret = LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000273 // Try constant struct
Peter Zotov3e4561c2016-04-06 22:21:29 +0000274 } else if (LLVMIsAConstantStruct(Cst)) {
275 if (LLVMGetValueKind(Cst) != LLVMConstantStructValueKind)
276 report_fatal_error("LLVMGetValueKind returned incorrect type");
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000277 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
278 unsigned EltCount = LLVMCountStructElementTypes(Ty);
279 SmallVector<LLVMValueRef, 8> Elts;
280 for (unsigned i = 0; i < EltCount; i++)
281 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
282 if (LLVMGetStructName(Ty))
Peter Zotov3e4561c2016-04-06 22:21:29 +0000283 Ret = LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
284 else
285 Ret = LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
286 EltCount, LLVMIsPackedStruct(Ty));
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000287 // Try undef
Peter Zotov3e4561c2016-04-06 22:21:29 +0000288 } else if (LLVMIsUndef(Cst)) {
289 if (LLVMGetValueKind(Cst) != LLVMUndefValueValueKind)
290 report_fatal_error("LLVMGetValueKind returned incorrect type");
291 Ret = LLVMGetUndef(TypeCloner(M).Clone(Cst));
Amaury Sechet40bbe512016-02-17 23:55:59 +0000292 // Try float literal
Peter Zotov3e4561c2016-04-06 22:21:29 +0000293 } else if (LLVMIsAConstantFP(Cst)) {
294 if (LLVMGetValueKind(Cst) != LLVMConstantFPValueKind)
295 report_fatal_error("LLVMGetValueKind returned incorrect type");
Amaury Sechet40bbe512016-02-17 23:55:59 +0000296 report_fatal_error("ConstantFP is not supported");
Amaury Sechet6b16c232016-02-16 07:33:23 +0000297 // This kind of constant is not supported
Peter Zotov3e4561c2016-04-06 22:21:29 +0000298 } else if (!LLVMIsAConstantExpr(Cst)) {
Amaury Sechet6b16c232016-02-16 07:33:23 +0000299 report_fatal_error("Expected a constant expression");
Peter Zotov3e4561c2016-04-06 22:21:29 +0000300 // At this point, it must be a constant expression, and thus, an opcode
301 } else {
302 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
303 switch(Op) {
304 case LLVMBitCast:
305 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
306 TypeCloner(M).Clone(Cst));
307 default:
308 fprintf(stderr, "%d is not a supported opcode\n", Op);
309 exit(-1);
310 }
Amaury Sechet40bbe512016-02-17 23:55:59 +0000311 }
Peter Zotov3e4561c2016-04-06 22:21:29 +0000312 if (LLVMGetValueKind(Ret) != LLVMGetValueKind(Cst)) {
313 report_fatal_error(
314 "The ValueKind of Ret is not equal to the ValueKind of Cst");
315 }
316 return Ret;
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000317}
318
Amaury Secheta82042e2016-02-09 22:36:41 +0000319struct FunCloner {
320 LLVMValueRef Fun;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000321 LLVMModuleRef M;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000322
Amaury Secheta82042e2016-02-09 22:36:41 +0000323 ValueMap VMap;
324 BasicBlockMap BBMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000325
Amaury Sechetaad93532016-02-10 00:38:50 +0000326 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
327 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
328
329 LLVMTypeRef CloneType(LLVMTypeRef Src) {
330 return TypeCloner(M).Clone(Src);
331 }
332
333 LLVMTypeRef CloneType(LLVMValueRef Src) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000334 return TypeCloner(M).Clone(Src);
Amaury Sechetaad93532016-02-10 00:38:50 +0000335 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000336
Amaury Secheta82042e2016-02-09 22:36:41 +0000337 // Try to clone everything in the llvm::Value hierarchy.
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000338 LLVMValueRef CloneValue(LLVMValueRef Src) {
Amaury Secheta82042e2016-02-09 22:36:41 +0000339 // First, the value may be constant.
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000340 if (LLVMIsAConstant(Src))
341 return clone_constant(Src, M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000342
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000343 // Function argument should always be in the map already.
Amaury Sechet2f432082016-02-11 21:37:54 +0000344 auto i = VMap.find(Src);
345 if (i != VMap.end())
346 return i->second;
Amaury Secheta82042e2016-02-09 22:36:41 +0000347
Amaury Sechet2f432082016-02-11 21:37:54 +0000348 if (!LLVMIsAInstruction(Src))
349 report_fatal_error("Expected an instruction");
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000350
Peter Zotov3e4561c2016-04-06 22:21:29 +0000351 if (LLVMGetValueKind(Src) != LLVMInstructionValueKind)
352 report_fatal_error("LLVMGetValueKind returned incorrect type");
353
Amaury Sechet2f432082016-02-11 21:37:54 +0000354 auto Ctx = LLVMGetModuleContext(M);
355 auto Builder = LLVMCreateBuilderInContext(Ctx);
356 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
357 LLVMPositionBuilderAtEnd(Builder, BB);
358 auto Dst = CloneInstruction(Src, Builder);
359 LLVMDisposeBuilder(Builder);
360 return Dst;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000361 }
362
363 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
364 const char *Name = LLVMGetValueName(Src);
365 if (!LLVMIsAInstruction(Src))
366 report_fatal_error("Expected an instruction");
367
Peter Zotov3e4561c2016-04-06 22:21:29 +0000368 if (LLVMGetValueKind(Src) != LLVMInstructionValueKind)
369 report_fatal_error("LLVMGetValueKind returned incorrect type");
370
Amaury Secheta82042e2016-02-09 22:36:41 +0000371 // Check if this is something we already computed.
372 {
373 auto i = VMap.find(Src);
Amaury Sechet2f432082016-02-11 21:37:54 +0000374 if (i != VMap.end()) {
375 // If we have a hit, it means we already generated the instruction
376 // as a dependancy to somethign else. We need to make sure
377 // it is ordered properly.
378 auto I = i->second;
379 LLVMInstructionRemoveFromParent(I);
380 LLVMInsertIntoBuilderWithName(Builder, I, Name);
381 return I;
382 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000383 }
384
385 // We tried everything, it must be an instruction
386 // that hasn't been generated already.
387 LLVMValueRef Dst = nullptr;
388
389 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
390 switch(Op) {
391 case LLVMRet: {
392 int OpCount = LLVMGetNumOperands(Src);
393 if (OpCount == 0)
394 Dst = LLVMBuildRetVoid(Builder);
395 else
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000396 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
Amaury Secheta82042e2016-02-09 22:36:41 +0000397 break;
398 }
399 case LLVMBr: {
Amaury Sechete7e62172016-02-09 23:15:02 +0000400 if (!LLVMIsConditional(Src)) {
401 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
402 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
403 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
404 break;
405 }
406
407 LLVMValueRef Cond = LLVMGetCondition(Src);
408 LLVMValueRef Else = LLVMGetOperand(Src, 1);
409 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
410 LLVMValueRef Then = LLVMGetOperand(Src, 2);
411 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
412 Dst = LLVMBuildCondBr(Builder, Cond, ThenBB, ElseBB);
Amaury Secheta82042e2016-02-09 22:36:41 +0000413 break;
414 }
415 case LLVMSwitch:
416 case LLVMIndirectBr:
Amaury Secheta82042e2016-02-09 22:36:41 +0000417 break;
Amaury Sechete39e8532016-02-18 20:38:32 +0000418 case LLVMInvoke: {
419 SmallVector<LLVMValueRef, 8> Args;
420 int ArgCount = LLVMGetNumArgOperands(Src);
421 for (int i = 0; i < ArgCount; i++)
422 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
423 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
424 LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
425 LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
426 Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
427 Then, Unwind, Name);
428 break;
429 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000430 case LLVMUnreachable:
431 Dst = LLVMBuildUnreachable(Builder);
432 break;
433 case LLVMAdd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000434 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
435 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000436 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
437 break;
438 }
439 case LLVMSub: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000440 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
441 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000442 Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
443 break;
444 }
445 case LLVMMul: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000446 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
447 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000448 Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
449 break;
450 }
451 case LLVMUDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000452 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
453 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000454 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
455 break;
456 }
457 case LLVMSDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000458 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
459 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000460 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
461 break;
462 }
463 case LLVMURem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000464 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
465 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000466 Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
467 break;
468 }
469 case LLVMSRem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000470 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
471 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000472 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
473 break;
474 }
475 case LLVMShl: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000476 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
477 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000478 Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
479 break;
480 }
481 case LLVMLShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000482 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
483 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000484 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
485 break;
486 }
487 case LLVMAShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000488 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
489 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000490 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
491 break;
492 }
493 case LLVMAnd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000494 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
495 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000496 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
497 break;
498 }
499 case LLVMOr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000500 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
501 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000502 Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
503 break;
504 }
505 case LLVMXor: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000506 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
507 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000508 Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
509 break;
510 }
511 case LLVMAlloca: {
Amaury Sechetaad93532016-02-10 00:38:50 +0000512 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000513 Dst = LLVMBuildAlloca(Builder, Ty, Name);
514 break;
515 }
Amaury Sechet053ac452016-02-17 22:51:03 +0000516 case LLVMLoad: {
517 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
518 Dst = LLVMBuildLoad(Builder, Ptr, Name);
519 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
520 break;
521 }
522 case LLVMStore: {
523 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
524 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
525 Dst = LLVMBuildStore(Builder, Val, Ptr);
526 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
527 break;
528 }
529 case LLVMGetElementPtr: {
530 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
531 SmallVector<LLVMValueRef, 8> Idx;
532 int NumIdx = LLVMGetNumIndices(Src);
533 for (int i = 1; i <= NumIdx; i++)
534 Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
535 if (LLVMIsInBounds(Src))
536 Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
537 else
538 Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
539 break;
540 }
Mehdi Amini43165d92016-03-19 21:28:28 +0000541 case LLVMAtomicCmpXchg: {
542 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
543 LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
544 LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
545 LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
546 LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
547 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
548
549 Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
550 SingleThread);
551 } break;
Amaury Sechet40bbe512016-02-17 23:55:59 +0000552 case LLVMBitCast: {
553 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
554 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
555 break;
556 }
Amaury Sechete7e62172016-02-09 23:15:02 +0000557 case LLVMICmp: {
558 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000559 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
560 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Sechete7e62172016-02-09 23:15:02 +0000561 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
562 break;
563 }
Amaury Sechet2f432082016-02-11 21:37:54 +0000564 case LLVMPHI: {
565 // We need to agressively set things here because of loops.
566 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
567
568 SmallVector<LLVMValueRef, 8> Values;
569 SmallVector<LLVMBasicBlockRef, 8> Blocks;
570
571 unsigned IncomingCount = LLVMCountIncoming(Src);
572 for (unsigned i = 0; i < IncomingCount; ++i) {
573 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
574 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
575 }
576
577 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
578 return Dst;
579 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000580 case LLVMCall: {
Amaury Secheta82042e2016-02-09 22:36:41 +0000581 SmallVector<LLVMValueRef, 8> Args;
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000582 int ArgCount = LLVMGetNumArgOperands(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000583 for (int i = 0; i < ArgCount; i++)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000584 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000585 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000586 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
Amaury Sechete39e8532016-02-18 20:38:32 +0000587 LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
588 break;
589 }
590 case LLVMResume: {
591 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
592 break;
593 }
594 case LLVMLandingPad: {
595 // The landing pad API is a bit screwed up for historical reasons.
596 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
597 unsigned NumClauses = LLVMGetNumClauses(Src);
598 for (unsigned i = 0; i < NumClauses; ++i)
599 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
600 LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000601 break;
602 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000603 case LLVMExtractValue: {
604 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
605 if (LLVMGetNumIndices(Src) != 1)
606 report_fatal_error("Expected only one indice");
607 auto I = LLVMGetIndices(Src)[0];
608 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
609 break;
610 }
611 case LLVMInsertValue: {
612 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
613 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
614 if (LLVMGetNumIndices(Src) != 1)
615 report_fatal_error("Expected only one indice");
616 auto I = LLVMGetIndices(Src)[0];
617 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
618 break;
619 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000620 default:
621 break;
622 }
623
624 if (Dst == nullptr) {
625 fprintf(stderr, "%d is not a supported opcode\n", Op);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000626 exit(-1);
627 }
628
Amaury Secheta82042e2016-02-09 22:36:41 +0000629 return VMap[Src] = Dst;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000630 }
631
Amaury Secheta82042e2016-02-09 22:36:41 +0000632 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
633 // Check if this is something we already computed.
634 {
635 auto i = BBMap.find(Src);
636 if (i != BBMap.end()) {
637 return i->second;
638 }
639 }
640
Amaury Secheta82042e2016-02-09 22:36:41 +0000641 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
642 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000643 report_fatal_error("Basic block is not a basic block");
Amaury Secheta82042e2016-02-09 22:36:41 +0000644
Amaury Sechetd01c8612016-02-14 10:06:34 +0000645 const char *Name = LLVMGetBasicBlockName(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000646 const char *VName = LLVMGetValueName(V);
647 if (Name != VName)
648 report_fatal_error("Basic block name mismatch");
649
650 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
651 return BBMap[Src] = BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000652 }
653
Amaury Secheta82042e2016-02-09 22:36:41 +0000654 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
655 LLVMBasicBlockRef BB = DeclareBB(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000656
Amaury Secheta82042e2016-02-09 22:36:41 +0000657 // Make sure ordering is correct.
658 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
659 if (Prev)
660 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000661
Amaury Secheta82042e2016-02-09 22:36:41 +0000662 LLVMValueRef First = LLVMGetFirstInstruction(Src);
663 LLVMValueRef Last = LLVMGetLastInstruction(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000664
Amaury Secheta82042e2016-02-09 22:36:41 +0000665 if (First == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000666 if (Last != nullptr)
667 report_fatal_error("Has no first instruction, but last one");
Amaury Secheta82042e2016-02-09 22:36:41 +0000668 return BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000669 }
670
Amaury Sechetaad93532016-02-10 00:38:50 +0000671 auto Ctx = LLVMGetModuleContext(M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000672 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
673 LLVMPositionBuilderAtEnd(Builder, BB);
674
675 LLVMValueRef Cur = First;
676 LLVMValueRef Next = nullptr;
677 while(true) {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000678 CloneInstruction(Cur, Builder);
Amaury Secheta82042e2016-02-09 22:36:41 +0000679 Next = LLVMGetNextInstruction(Cur);
680 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000681 if (Cur != Last)
682 report_fatal_error("Final instruction does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000683 break;
684 }
685
686 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000687 if (Prev != Cur)
688 report_fatal_error("Next.Previous instruction is not Current");
Amaury Secheta82042e2016-02-09 22:36:41 +0000689
690 Cur = Next;
691 }
692
693 LLVMDisposeBuilder(Builder);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000694 return BB;
695 }
696
Amaury Secheta82042e2016-02-09 22:36:41 +0000697 void CloneBBs(LLVMValueRef Src) {
698 unsigned Count = LLVMCountBasicBlocks(Src);
699 if (Count == 0)
700 return;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000701
Amaury Secheta82042e2016-02-09 22:36:41 +0000702 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
703 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
704
705 LLVMBasicBlockRef Cur = First;
706 LLVMBasicBlockRef Next = nullptr;
707 while(true) {
708 CloneBB(Cur);
709 Count--;
710 Next = LLVMGetNextBasicBlock(Cur);
711 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000712 if (Cur != Last)
713 report_fatal_error("Final basic block does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000714 break;
715 }
716
717 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000718 if (Prev != Cur)
719 report_fatal_error("Next.Previous basic bloc is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000720
Amaury Secheta82042e2016-02-09 22:36:41 +0000721 Cur = Next;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000722 }
723
Amaury Sechetd01c8612016-02-14 10:06:34 +0000724 if (Count != 0)
725 report_fatal_error("Basic block count does not match iterration");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000726 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000727};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000728
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000729static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000730 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
731 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000732
733 LLVMValueRef Cur = Begin;
734 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000735 if (!Begin) {
736 if (End != nullptr)
737 report_fatal_error("Range has an end but no begining");
738 goto FunDecl;
739 }
740
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000741 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000742 const char *Name = LLVMGetValueName(Cur);
743 if (LLVMGetNamedGlobal(M, Name))
744 report_fatal_error("GlobalVariable already cloned");
745 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
746
747 Next = LLVMGetNextGlobal(Cur);
748 if (Next == nullptr) {
749 if (Cur != End)
750 report_fatal_error("");
751 break;
752 }
753
754 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
755 if (Prev != Cur)
756 report_fatal_error("Next.Previous global is not Current");
757
758 Cur = Next;
759 }
760
Amaury Sechet58946a92016-02-17 22:30:05 +0000761FunDecl:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000762 Begin = LLVMGetFirstFunction(Src);
763 End = LLVMGetLastFunction(Src);
764 if (!Begin) {
765 if (End != nullptr)
766 report_fatal_error("Range has an end but no begining");
767 return;
768 }
769
770 Cur = Begin;
771 Next = nullptr;
772 while (true) {
773 const char *Name = LLVMGetValueName(Cur);
774 if (LLVMGetNamedFunction(M, Name))
775 report_fatal_error("Function already cloned");
776 LLVMAddFunction(M, Name, LLVMGetElementType(TypeCloner(M).Clone(Cur)));
777
Amaury Sechetf64e8342016-02-16 07:08:49 +0000778 Next = LLVMGetNextFunction(Cur);
779 if (Next == nullptr) {
780 if (Cur != End)
781 report_fatal_error("Last function does not match End");
782 break;
783 }
784
785 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
786 if (Prev != Cur)
787 report_fatal_error("Next.Previous function is not Current");
788
789 Cur = Next;
790 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000791}
Amaury Sechetf64e8342016-02-16 07:08:49 +0000792
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000793static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000794 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
795 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000796
797 LLVMValueRef Cur = Begin;
798 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000799 if (!Begin) {
800 if (End != nullptr)
801 report_fatal_error("Range has an end but no begining");
802 goto FunClone;
803 }
804
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000805 while (true) {
806 const char *Name = LLVMGetValueName(Cur);
807 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
808 if (!G)
809 report_fatal_error("GlobalVariable must have been declared already");
810
811 if (auto I = LLVMGetInitializer(Cur))
812 LLVMSetInitializer(G, clone_constant(I, M));
813
814 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
815 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
816 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
817 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
818 LLVMSetSection(G, LLVMGetSection(Cur));
819 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
820 LLVMSetUnnamedAddr(G, LLVMHasUnnamedAddr(Cur));
821 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
822
823 Next = LLVMGetNextGlobal(Cur);
824 if (Next == nullptr) {
825 if (Cur != End)
826 report_fatal_error("");
827 break;
828 }
829
830 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
831 if (Prev != Cur)
832 report_fatal_error("Next.Previous global is not Current");
833
834 Cur = Next;
835 }
836
Amaury Sechet58946a92016-02-17 22:30:05 +0000837FunClone:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000838 Begin = LLVMGetFirstFunction(Src);
839 End = LLVMGetLastFunction(Src);
840 if (!Begin) {
841 if (End != nullptr)
842 report_fatal_error("Range has an end but no begining");
843 return;
844 }
845
Amaury Sechetf64e8342016-02-16 07:08:49 +0000846 Cur = Begin;
847 Next = nullptr;
848 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000849 const char *Name = LLVMGetValueName(Cur);
850 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
851 if (!Fun)
852 report_fatal_error("Function must have been declared already");
Amaury Sechete39e8532016-02-18 20:38:32 +0000853
854 if (LLVMHasPersonalityFn(Cur)) {
855 const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur));
856 LLVMValueRef P = LLVMGetNamedFunction(M, FName);
857 if (!P)
858 report_fatal_error("Could not find personality function");
859 LLVMSetPersonalityFn(Fun, P);
860 }
861
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000862 FunCloner FC(Cur, Fun);
863 FC.CloneBBs(Cur);
864
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000865 Next = LLVMGetNextFunction(Cur);
866 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000867 if (Cur != End)
868 report_fatal_error("Last function does not match End");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000869 break;
870 }
871
872 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000873 if (Prev != Cur)
874 report_fatal_error("Next.Previous function is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000875
876 Cur = Next;
877 }
878}
879
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000880int llvm_echo(void) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000881 LLVMEnablePrettyStackTrace();
882
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000883 LLVMModuleRef Src = llvm_load_module(false, true);
Peter Zotov0a2fa0a2016-04-05 13:56:59 +0000884 size_t Len;
885 const char *ModuleName = LLVMGetModuleIdentifier(Src, &Len);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000886 LLVMContextRef Ctx = LLVMContextCreate();
Peter Zotov0a2fa0a2016-04-05 13:56:59 +0000887 LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
888
889 // This whole switcharound is done because the C API has no way to
890 // set the source_filename
891 LLVMSetModuleIdentifier(M, "", 0);
892 LLVMGetModuleIdentifier(M, &Len);
893 if (Len != 0)
894 report_fatal_error("LLVM{Set,Get}ModuleIdentifier failed");
895 LLVMSetModuleIdentifier(M, ModuleName, strlen(ModuleName));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000896
Amaury Sechet55909672016-02-16 05:11:24 +0000897 LLVMSetTarget(M, LLVMGetTarget(Src));
898 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
899 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
900 report_fatal_error("Inconsistent DataLayout string representation");
901
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000902 declare_symbols(Src, M);
903 clone_symbols(Src, M);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000904 char *Str = LLVMPrintModuleToString(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000905 fputs(Str, stdout);
906
907 LLVMDisposeMessage(Str);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000908 LLVMDisposeModule(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000909 LLVMContextDispose(Ctx);
910
911 return 0;
912}