blob: db7211a3eab37469004dd94c20f992066e9b041c [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//
Jeroen Ketemaad659c32016-04-08 09:19:02 +000010// This file implements the --echo command in llvm-c-test.
Amaury Sechete8ea7d82016-02-04 23:26:19 +000011//
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) {
Serge Pavlov76d8cce2018-02-20 05:41:26 +000093 Params = static_cast<LLVMTypeRef*>(
94 safe_malloc(ParamCount * sizeof(LLVMTypeRef)));
Amaury Sechetaad93532016-02-10 00:38:50 +000095 LLVMGetParamTypes(Src, Params);
96 for (unsigned i = 0; i < ParamCount; i++)
97 Params[i] = Clone(Params[i]);
98 }
99
100 LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)),
101 Params, ParamCount,
102 LLVMIsFunctionVarArg(Src));
103 if (ParamCount > 0)
104 free(Params);
105 return FunTy;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000106 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000107 case LLVMStructTypeKind: {
108 LLVMTypeRef S = nullptr;
109 const char *Name = LLVMGetStructName(Src);
110 if (Name) {
111 S = LLVMGetTypeByName(M, Name);
112 if (S)
113 return S;
114 S = LLVMStructCreateNamed(Ctx, Name);
115 if (LLVMIsOpaqueStruct(Src))
116 return S;
117 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000118
Amaury Sechetaad93532016-02-10 00:38:50 +0000119 unsigned EltCount = LLVMCountStructElementTypes(Src);
120 SmallVector<LLVMTypeRef, 8> Elts;
121 for (unsigned i = 0; i < EltCount; i++)
122 Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i)));
123 if (Name)
124 LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src));
125 else
126 S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount,
127 LLVMIsPackedStruct(Src));
128 return S;
129 }
130 case LLVMArrayTypeKind:
131 return LLVMArrayType(
132 Clone(LLVMGetElementType(Src)),
133 LLVMGetArrayLength(Src)
134 );
135 case LLVMPointerTypeKind:
136 return LLVMPointerType(
137 Clone(LLVMGetElementType(Src)),
138 LLVMGetPointerAddressSpace(Src)
139 );
140 case LLVMVectorTypeKind:
141 return LLVMVectorType(
142 Clone(LLVMGetElementType(Src)),
143 LLVMGetVectorSize(Src)
144 );
145 case LLVMMetadataTypeKind:
whitequark131f98f2017-10-27 11:51:40 +0000146 return LLVMMetadataTypeInContext(Ctx);
Amaury Sechetaad93532016-02-10 00:38:50 +0000147 case LLVMX86_MMXTypeKind:
148 return LLVMX86MMXTypeInContext(Ctx);
149 default:
150 break;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000151 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000152
Amaury Sechetaad93532016-02-10 00:38:50 +0000153 fprintf(stderr, "%d is not a supported typekind\n", Kind);
154 exit(-1);
155 }
156};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000157
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000158static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000159 unsigned Count = LLVMCountParams(Src);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000160 if (Count != LLVMCountParams(Dst))
161 report_fatal_error("Parameter count mismatch");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000162
163 ValueMap VMap;
164 if (Count == 0)
165 return VMap;
166
167 LLVMValueRef SrcFirst = LLVMGetFirstParam(Src);
168 LLVMValueRef DstFirst = LLVMGetFirstParam(Dst);
169 LLVMValueRef SrcLast = LLVMGetLastParam(Src);
170 LLVMValueRef DstLast = LLVMGetLastParam(Dst);
171
172 LLVMValueRef SrcCur = SrcFirst;
173 LLVMValueRef DstCur = DstFirst;
174 LLVMValueRef SrcNext = nullptr;
175 LLVMValueRef DstNext = nullptr;
176 while (true) {
177 const char *Name = LLVMGetValueName(SrcCur);
178 LLVMSetValueName(DstCur, Name);
179
180 VMap[SrcCur] = DstCur;
181
182 Count--;
183 SrcNext = LLVMGetNextParam(SrcCur);
184 DstNext = LLVMGetNextParam(DstCur);
185 if (SrcNext == nullptr && DstNext == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000186 if (SrcCur != SrcLast)
187 report_fatal_error("SrcLast param does not match End");
188 if (DstCur != DstLast)
189 report_fatal_error("DstLast param does not match End");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000190 break;
191 }
192
Amaury Sechetd01c8612016-02-14 10:06:34 +0000193 if (SrcNext == nullptr)
194 report_fatal_error("SrcNext was unexpectedly null");
195 if (DstNext == nullptr)
196 report_fatal_error("DstNext was unexpectedly null");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000197
198 LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000199 if (SrcPrev != SrcCur)
200 report_fatal_error("SrcNext.Previous param is not Current");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000201
202 LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000203 if (DstPrev != DstCur)
204 report_fatal_error("DstNext.Previous param is not Current");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000205
206 SrcCur = SrcNext;
207 DstCur = DstNext;
208 }
209
Amaury Sechetd01c8612016-02-14 10:06:34 +0000210 if (Count != 0)
211 report_fatal_error("Parameter count does not match iteration");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000212
213 return VMap;
214}
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000215
Amaury Sechet49491312016-04-07 05:56:20 +0000216static void check_value_kind(LLVMValueRef V, LLVMValueKind K) {
217 if (LLVMGetValueKind(V) != K)
218 report_fatal_error("LLVMGetValueKind returned incorrect type");
219}
220
221static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M);
222
223static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
224 LLVMValueRef Ret = clone_constant_impl(Cst, M);
225 check_value_kind(Ret, LLVMGetValueKind(Cst));
226 return Ret;
227}
228
229static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000230 if (!LLVMIsAConstant(Cst))
231 report_fatal_error("Expected a constant");
232
233 // Maybe it is a symbol
234 if (LLVMIsAGlobalValue(Cst)) {
235 const char *Name = LLVMGetValueName(Cst);
236
237 // Try function
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000238 if (LLVMIsAFunction(Cst)) {
Amaury Sechet49491312016-04-07 05:56:20 +0000239 check_value_kind(Cst, LLVMFunctionValueKind);
240 LLVMValueRef Dst = LLVMGetNamedFunction(M, Name);
241 if (Dst)
242 return Dst;
243 report_fatal_error("Could not find function");
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000244 }
Amaury Sechet49491312016-04-07 05:56:20 +0000245
246 // Try global variable
247 if (LLVMIsAGlobalVariable(Cst)) {
248 check_value_kind(Cst, LLVMGlobalVariableValueKind);
249 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
250 if (Dst)
251 return Dst;
252 report_fatal_error("Could not find function");
253 }
254
255 fprintf(stderr, "Could not find @%s\n", Name);
256 exit(-1);
257 }
258
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000259 // Try integer literal
Amaury Sechet49491312016-04-07 05:56:20 +0000260 if (LLVMIsAConstantInt(Cst)) {
261 check_value_kind(Cst, LLVMConstantIntValueKind);
262 return LLVMConstInt(TypeCloner(M).Clone(Cst),
263 LLVMConstIntGetZExtValue(Cst), false);
264 }
265
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000266 // Try zeroinitializer
Amaury Sechet49491312016-04-07 05:56:20 +0000267 if (LLVMIsAConstantAggregateZero(Cst)) {
268 check_value_kind(Cst, LLVMConstantAggregateZeroValueKind);
269 return LLVMConstNull(TypeCloner(M).Clone(Cst));
270 }
271
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000272 // Try constant array
Amaury Sechet49491312016-04-07 05:56:20 +0000273 if (LLVMIsAConstantArray(Cst)) {
274 check_value_kind(Cst, LLVMConstantArrayValueKind);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000275 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
276 unsigned EltCount = LLVMGetArrayLength(Ty);
277 SmallVector<LLVMValueRef, 8> Elts;
278 for (unsigned i = 0; i < EltCount; i++)
279 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
Amaury Sechet49491312016-04-07 05:56:20 +0000280 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
281 }
282
Amaury Sechetb3256862016-03-13 00:58:25 +0000283 // Try contant data array
Amaury Sechet49491312016-04-07 05:56:20 +0000284 if (LLVMIsAConstantDataArray(Cst)) {
285 check_value_kind(Cst, LLVMConstantDataArrayValueKind);
Amaury Sechetb3256862016-03-13 00:58:25 +0000286 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
287 unsigned EltCount = LLVMGetArrayLength(Ty);
288 SmallVector<LLVMValueRef, 8> Elts;
289 for (unsigned i = 0; i < EltCount; i++)
290 Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
Amaury Sechet49491312016-04-07 05:56:20 +0000291 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
292 }
293
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000294 // Try constant struct
Amaury Sechet49491312016-04-07 05:56:20 +0000295 if (LLVMIsAConstantStruct(Cst)) {
296 check_value_kind(Cst, LLVMConstantStructValueKind);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000297 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
298 unsigned EltCount = LLVMCountStructElementTypes(Ty);
299 SmallVector<LLVMValueRef, 8> Elts;
300 for (unsigned i = 0; i < EltCount; i++)
301 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
302 if (LLVMGetStructName(Ty))
Amaury Sechet49491312016-04-07 05:56:20 +0000303 return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
304 return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
305 EltCount, LLVMIsPackedStruct(Ty));
306 }
307
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000308 // Try undef
Amaury Sechet49491312016-04-07 05:56:20 +0000309 if (LLVMIsUndef(Cst)) {
310 check_value_kind(Cst, LLVMUndefValueValueKind);
311 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
312 }
313
Amaury Sechet40bbe512016-02-17 23:55:59 +0000314 // Try float literal
Amaury Sechet49491312016-04-07 05:56:20 +0000315 if (LLVMIsAConstantFP(Cst)) {
316 check_value_kind(Cst, LLVMConstantFPValueKind);
Amaury Sechet40bbe512016-02-17 23:55:59 +0000317 report_fatal_error("ConstantFP is not supported");
Amaury Sechet49491312016-04-07 05:56:20 +0000318 }
319
Amaury Sechet6b16c232016-02-16 07:33:23 +0000320 // This kind of constant is not supported
Amaury Sechet49491312016-04-07 05:56:20 +0000321 if (!LLVMIsAConstantExpr(Cst))
Amaury Sechet6b16c232016-02-16 07:33:23 +0000322 report_fatal_error("Expected a constant expression");
Amaury Sechet49491312016-04-07 05:56:20 +0000323
324 // At this point, it must be a constant expression
325 check_value_kind(Cst, LLVMConstantExprValueKind);
326
327 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
328 switch(Op) {
329 case LLVMBitCast:
330 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
331 TypeCloner(M).Clone(Cst));
332 default:
333 fprintf(stderr, "%d is not a supported opcode\n", Op);
334 exit(-1);
Amaury Sechet40bbe512016-02-17 23:55:59 +0000335 }
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000336}
337
Amaury Secheta82042e2016-02-09 22:36:41 +0000338struct FunCloner {
339 LLVMValueRef Fun;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000340 LLVMModuleRef M;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000341
Amaury Secheta82042e2016-02-09 22:36:41 +0000342 ValueMap VMap;
343 BasicBlockMap BBMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000344
Amaury Sechetaad93532016-02-10 00:38:50 +0000345 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
346 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
347
348 LLVMTypeRef CloneType(LLVMTypeRef Src) {
349 return TypeCloner(M).Clone(Src);
350 }
351
352 LLVMTypeRef CloneType(LLVMValueRef Src) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000353 return TypeCloner(M).Clone(Src);
Amaury Sechetaad93532016-02-10 00:38:50 +0000354 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000355
Amaury Secheta82042e2016-02-09 22:36:41 +0000356 // Try to clone everything in the llvm::Value hierarchy.
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000357 LLVMValueRef CloneValue(LLVMValueRef Src) {
Amaury Secheta82042e2016-02-09 22:36:41 +0000358 // First, the value may be constant.
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000359 if (LLVMIsAConstant(Src))
360 return clone_constant(Src, M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000361
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000362 // Function argument should always be in the map already.
Amaury Sechet2f432082016-02-11 21:37:54 +0000363 auto i = VMap.find(Src);
364 if (i != VMap.end())
365 return i->second;
Amaury Secheta82042e2016-02-09 22:36:41 +0000366
Amaury Sechet2f432082016-02-11 21:37:54 +0000367 if (!LLVMIsAInstruction(Src))
368 report_fatal_error("Expected an instruction");
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000369
Amaury Sechet2f432082016-02-11 21:37:54 +0000370 auto Ctx = LLVMGetModuleContext(M);
371 auto Builder = LLVMCreateBuilderInContext(Ctx);
372 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
373 LLVMPositionBuilderAtEnd(Builder, BB);
374 auto Dst = CloneInstruction(Src, Builder);
375 LLVMDisposeBuilder(Builder);
376 return Dst;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000377 }
378
Amaury Secheta65a2372016-06-15 05:14:29 +0000379 void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) {
380 auto Ctx = LLVMGetModuleContext(M);
381 int ArgCount = LLVMGetNumArgOperands(Src);
382 for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) {
383 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
384 if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) {
385 auto Val = LLVMGetEnumAttributeValue(SrcA);
386 auto A = LLVMCreateEnumAttribute(Ctx, k, Val);
387 LLVMAddCallSiteAttribute(Dst, i, A);
388 }
389 }
390 }
391 }
392
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000393 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
Amaury Sechet49491312016-04-07 05:56:20 +0000394 check_value_kind(Src, LLVMInstructionValueKind);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000395 if (!LLVMIsAInstruction(Src))
396 report_fatal_error("Expected an instruction");
397
Amaury Sechet49491312016-04-07 05:56:20 +0000398 const char *Name = LLVMGetValueName(Src);
Peter Zotov3e4561c2016-04-06 22:21:29 +0000399
Amaury Secheta82042e2016-02-09 22:36:41 +0000400 // Check if this is something we already computed.
401 {
402 auto i = VMap.find(Src);
Amaury Sechet2f432082016-02-11 21:37:54 +0000403 if (i != VMap.end()) {
404 // If we have a hit, it means we already generated the instruction
405 // as a dependancy to somethign else. We need to make sure
406 // it is ordered properly.
407 auto I = i->second;
408 LLVMInstructionRemoveFromParent(I);
409 LLVMInsertIntoBuilderWithName(Builder, I, Name);
410 return I;
411 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000412 }
413
414 // We tried everything, it must be an instruction
415 // that hasn't been generated already.
416 LLVMValueRef Dst = nullptr;
417
418 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
419 switch(Op) {
420 case LLVMRet: {
421 int OpCount = LLVMGetNumOperands(Src);
422 if (OpCount == 0)
423 Dst = LLVMBuildRetVoid(Builder);
424 else
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000425 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
Amaury Secheta82042e2016-02-09 22:36:41 +0000426 break;
427 }
428 case LLVMBr: {
Amaury Sechete7e62172016-02-09 23:15:02 +0000429 if (!LLVMIsConditional(Src)) {
430 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
431 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
432 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
433 break;
434 }
435
436 LLVMValueRef Cond = LLVMGetCondition(Src);
437 LLVMValueRef Else = LLVMGetOperand(Src, 1);
438 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
439 LLVMValueRef Then = LLVMGetOperand(Src, 2);
440 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
441 Dst = LLVMBuildCondBr(Builder, Cond, ThenBB, ElseBB);
Amaury Secheta82042e2016-02-09 22:36:41 +0000442 break;
443 }
444 case LLVMSwitch:
445 case LLVMIndirectBr:
Amaury Secheta82042e2016-02-09 22:36:41 +0000446 break;
Amaury Sechete39e8532016-02-18 20:38:32 +0000447 case LLVMInvoke: {
448 SmallVector<LLVMValueRef, 8> Args;
449 int ArgCount = LLVMGetNumArgOperands(Src);
450 for (int i = 0; i < ArgCount; i++)
451 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
452 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
453 LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
454 LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
455 Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
456 Then, Unwind, Name);
Amaury Secheta65a2372016-06-15 05:14:29 +0000457 CloneAttrs(Src, Dst);
Amaury Sechete39e8532016-02-18 20:38:32 +0000458 break;
459 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000460 case LLVMUnreachable:
461 Dst = LLVMBuildUnreachable(Builder);
462 break;
463 case LLVMAdd: {
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 = LLVMBuildAdd(Builder, LHS, RHS, Name);
467 break;
468 }
469 case LLVMSub: {
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 = LLVMBuildSub(Builder, LHS, RHS, Name);
473 break;
474 }
475 case LLVMMul: {
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 = LLVMBuildMul(Builder, LHS, RHS, Name);
479 break;
480 }
481 case LLVMUDiv: {
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 = LLVMBuildUDiv(Builder, LHS, RHS, Name);
485 break;
486 }
487 case LLVMSDiv: {
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 = LLVMBuildSDiv(Builder, LHS, RHS, Name);
491 break;
492 }
493 case LLVMURem: {
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 = LLVMBuildURem(Builder, LHS, RHS, Name);
497 break;
498 }
499 case LLVMSRem: {
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 = LLVMBuildSRem(Builder, LHS, RHS, Name);
503 break;
504 }
505 case LLVMShl: {
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 = LLVMBuildShl(Builder, LHS, RHS, Name);
509 break;
510 }
511 case LLVMLShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000512 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
513 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000514 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
515 break;
516 }
517 case LLVMAShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000518 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
519 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000520 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
521 break;
522 }
523 case LLVMAnd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000524 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
525 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000526 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
527 break;
528 }
529 case LLVMOr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000530 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
531 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000532 Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
533 break;
534 }
535 case LLVMXor: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000536 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
537 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000538 Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
539 break;
540 }
541 case LLVMAlloca: {
Amaury Sechetaad93532016-02-10 00:38:50 +0000542 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000543 Dst = LLVMBuildAlloca(Builder, Ty, Name);
544 break;
545 }
Amaury Sechet053ac452016-02-17 22:51:03 +0000546 case LLVMLoad: {
547 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
548 Dst = LLVMBuildLoad(Builder, Ptr, Name);
549 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
550 break;
551 }
552 case LLVMStore: {
553 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
554 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
555 Dst = LLVMBuildStore(Builder, Val, Ptr);
556 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
557 break;
558 }
559 case LLVMGetElementPtr: {
560 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
561 SmallVector<LLVMValueRef, 8> Idx;
562 int NumIdx = LLVMGetNumIndices(Src);
563 for (int i = 1; i <= NumIdx; i++)
564 Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
565 if (LLVMIsInBounds(Src))
566 Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
567 else
568 Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
569 break;
570 }
Mehdi Amini43165d92016-03-19 21:28:28 +0000571 case LLVMAtomicCmpXchg: {
572 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
573 LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
574 LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
575 LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
576 LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
577 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
578
579 Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
580 SingleThread);
581 } break;
Amaury Sechet40bbe512016-02-17 23:55:59 +0000582 case LLVMBitCast: {
583 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
584 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
585 break;
586 }
Amaury Sechete7e62172016-02-09 23:15:02 +0000587 case LLVMICmp: {
588 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000589 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
590 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Sechete7e62172016-02-09 23:15:02 +0000591 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
592 break;
593 }
Amaury Sechet2f432082016-02-11 21:37:54 +0000594 case LLVMPHI: {
Simon Pilgrim68168d12017-03-30 12:59:53 +0000595 // We need to aggressively set things here because of loops.
Amaury Sechet2f432082016-02-11 21:37:54 +0000596 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
597
598 SmallVector<LLVMValueRef, 8> Values;
599 SmallVector<LLVMBasicBlockRef, 8> Blocks;
600
601 unsigned IncomingCount = LLVMCountIncoming(Src);
602 for (unsigned i = 0; i < IncomingCount; ++i) {
603 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
604 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
605 }
606
607 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
608 return Dst;
609 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000610 case LLVMCall: {
Amaury Secheta82042e2016-02-09 22:36:41 +0000611 SmallVector<LLVMValueRef, 8> Args;
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000612 int ArgCount = LLVMGetNumArgOperands(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000613 for (int i = 0; i < ArgCount; i++)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000614 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000615 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000616 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
Amaury Sechete39e8532016-02-18 20:38:32 +0000617 LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
Amaury Secheta65a2372016-06-15 05:14:29 +0000618 CloneAttrs(Src, Dst);
Amaury Sechete39e8532016-02-18 20:38:32 +0000619 break;
620 }
621 case LLVMResume: {
622 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
623 break;
624 }
625 case LLVMLandingPad: {
626 // The landing pad API is a bit screwed up for historical reasons.
627 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
628 unsigned NumClauses = LLVMGetNumClauses(Src);
629 for (unsigned i = 0; i < NumClauses; ++i)
630 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
631 LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000632 break;
633 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000634 case LLVMExtractValue: {
635 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
636 if (LLVMGetNumIndices(Src) != 1)
637 report_fatal_error("Expected only one indice");
638 auto I = LLVMGetIndices(Src)[0];
639 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
640 break;
641 }
642 case LLVMInsertValue: {
643 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
644 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
645 if (LLVMGetNumIndices(Src) != 1)
646 report_fatal_error("Expected only one indice");
647 auto I = LLVMGetIndices(Src)[0];
648 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
649 break;
650 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000651 default:
652 break;
653 }
654
655 if (Dst == nullptr) {
656 fprintf(stderr, "%d is not a supported opcode\n", Op);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000657 exit(-1);
658 }
659
Amaury Sechet49491312016-04-07 05:56:20 +0000660 check_value_kind(Dst, LLVMInstructionValueKind);
Amaury Secheta82042e2016-02-09 22:36:41 +0000661 return VMap[Src] = Dst;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000662 }
663
Amaury Secheta82042e2016-02-09 22:36:41 +0000664 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
665 // Check if this is something we already computed.
666 {
667 auto i = BBMap.find(Src);
668 if (i != BBMap.end()) {
669 return i->second;
670 }
671 }
672
Amaury Secheta82042e2016-02-09 22:36:41 +0000673 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
674 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000675 report_fatal_error("Basic block is not a basic block");
Amaury Secheta82042e2016-02-09 22:36:41 +0000676
Amaury Sechetd01c8612016-02-14 10:06:34 +0000677 const char *Name = LLVMGetBasicBlockName(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000678 const char *VName = LLVMGetValueName(V);
679 if (Name != VName)
680 report_fatal_error("Basic block name mismatch");
681
682 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
683 return BBMap[Src] = BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000684 }
685
Amaury Secheta82042e2016-02-09 22:36:41 +0000686 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
687 LLVMBasicBlockRef BB = DeclareBB(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000688
Amaury Secheta82042e2016-02-09 22:36:41 +0000689 // Make sure ordering is correct.
690 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
691 if (Prev)
692 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000693
Amaury Secheta82042e2016-02-09 22:36:41 +0000694 LLVMValueRef First = LLVMGetFirstInstruction(Src);
695 LLVMValueRef Last = LLVMGetLastInstruction(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000696
Amaury Secheta82042e2016-02-09 22:36:41 +0000697 if (First == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000698 if (Last != nullptr)
699 report_fatal_error("Has no first instruction, but last one");
Amaury Secheta82042e2016-02-09 22:36:41 +0000700 return BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000701 }
702
Amaury Sechetaad93532016-02-10 00:38:50 +0000703 auto Ctx = LLVMGetModuleContext(M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000704 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
705 LLVMPositionBuilderAtEnd(Builder, BB);
706
707 LLVMValueRef Cur = First;
708 LLVMValueRef Next = nullptr;
709 while(true) {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000710 CloneInstruction(Cur, Builder);
Amaury Secheta82042e2016-02-09 22:36:41 +0000711 Next = LLVMGetNextInstruction(Cur);
712 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000713 if (Cur != Last)
714 report_fatal_error("Final instruction does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000715 break;
716 }
717
718 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000719 if (Prev != Cur)
720 report_fatal_error("Next.Previous instruction is not Current");
Amaury Secheta82042e2016-02-09 22:36:41 +0000721
722 Cur = Next;
723 }
724
725 LLVMDisposeBuilder(Builder);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000726 return BB;
727 }
728
Amaury Secheta82042e2016-02-09 22:36:41 +0000729 void CloneBBs(LLVMValueRef Src) {
730 unsigned Count = LLVMCountBasicBlocks(Src);
731 if (Count == 0)
732 return;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000733
Amaury Secheta82042e2016-02-09 22:36:41 +0000734 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
735 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
736
737 LLVMBasicBlockRef Cur = First;
738 LLVMBasicBlockRef Next = nullptr;
739 while(true) {
740 CloneBB(Cur);
741 Count--;
742 Next = LLVMGetNextBasicBlock(Cur);
743 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000744 if (Cur != Last)
745 report_fatal_error("Final basic block does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000746 break;
747 }
748
749 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000750 if (Prev != Cur)
751 report_fatal_error("Next.Previous basic bloc is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000752
Amaury Secheta82042e2016-02-09 22:36:41 +0000753 Cur = Next;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000754 }
755
Amaury Sechetd01c8612016-02-14 10:06:34 +0000756 if (Count != 0)
757 report_fatal_error("Basic block count does not match iterration");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000758 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000759};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000760
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000761static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000762 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
763 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000764
765 LLVMValueRef Cur = Begin;
766 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000767 if (!Begin) {
768 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000769 report_fatal_error("Range has an end but no beginning");
Amaury Sechet58946a92016-02-17 22:30:05 +0000770 goto FunDecl;
771 }
772
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000773 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000774 const char *Name = LLVMGetValueName(Cur);
775 if (LLVMGetNamedGlobal(M, Name))
776 report_fatal_error("GlobalVariable already cloned");
777 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
778
779 Next = LLVMGetNextGlobal(Cur);
780 if (Next == nullptr) {
781 if (Cur != End)
782 report_fatal_error("");
783 break;
784 }
785
786 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
787 if (Prev != Cur)
788 report_fatal_error("Next.Previous global is not Current");
789
790 Cur = Next;
791 }
792
Amaury Sechet58946a92016-02-17 22:30:05 +0000793FunDecl:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000794 Begin = LLVMGetFirstFunction(Src);
795 End = LLVMGetLastFunction(Src);
796 if (!Begin) {
797 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000798 report_fatal_error("Range has an end but no beginning");
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000799 return;
800 }
801
Amaury Sechet5db224e2016-06-12 06:17:24 +0000802 auto Ctx = LLVMGetModuleContext(M);
803
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000804 Cur = Begin;
805 Next = nullptr;
806 while (true) {
807 const char *Name = LLVMGetValueName(Cur);
808 if (LLVMGetNamedFunction(M, Name))
809 report_fatal_error("Function already cloned");
Amaury Sechet5db224e2016-06-12 06:17:24 +0000810 auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur));
811 auto F = LLVMAddFunction(M, Name, Ty);
812
813 // Copy attributes
814 for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F);
815 i <= c; ++i) {
816 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
817 if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) {
818 auto Val = LLVMGetEnumAttributeValue(SrcA);
819 auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val);
820 LLVMAddAttributeAtIndex(F, i, DstA);
821 }
822 }
823 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000824
Amaury Sechetf64e8342016-02-16 07:08:49 +0000825 Next = LLVMGetNextFunction(Cur);
826 if (Next == nullptr) {
827 if (Cur != End)
828 report_fatal_error("Last function does not match End");
829 break;
830 }
831
832 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
833 if (Prev != Cur)
834 report_fatal_error("Next.Previous function is not Current");
835
836 Cur = Next;
837 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000838}
Amaury Sechetf64e8342016-02-16 07:08:49 +0000839
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000840static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000841 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
842 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000843
844 LLVMValueRef Cur = Begin;
845 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000846 if (!Begin) {
847 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000848 report_fatal_error("Range has an end but no beginning");
Amaury Sechet58946a92016-02-17 22:30:05 +0000849 goto FunClone;
850 }
851
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000852 while (true) {
853 const char *Name = LLVMGetValueName(Cur);
854 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
855 if (!G)
856 report_fatal_error("GlobalVariable must have been declared already");
857
858 if (auto I = LLVMGetInitializer(Cur))
859 LLVMSetInitializer(G, clone_constant(I, M));
860
861 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
862 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
863 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
864 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
865 LLVMSetSection(G, LLVMGetSection(Cur));
866 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
867 LLVMSetUnnamedAddr(G, LLVMHasUnnamedAddr(Cur));
868 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
869
870 Next = LLVMGetNextGlobal(Cur);
871 if (Next == nullptr) {
872 if (Cur != End)
873 report_fatal_error("");
874 break;
875 }
876
877 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
878 if (Prev != Cur)
879 report_fatal_error("Next.Previous global is not Current");
880
881 Cur = Next;
882 }
883
Amaury Sechet58946a92016-02-17 22:30:05 +0000884FunClone:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000885 Begin = LLVMGetFirstFunction(Src);
886 End = LLVMGetLastFunction(Src);
887 if (!Begin) {
888 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000889 report_fatal_error("Range has an end but no beginning");
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000890 return;
891 }
892
Amaury Sechetf64e8342016-02-16 07:08:49 +0000893 Cur = Begin;
894 Next = nullptr;
895 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000896 const char *Name = LLVMGetValueName(Cur);
897 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
898 if (!Fun)
899 report_fatal_error("Function must have been declared already");
Amaury Sechete39e8532016-02-18 20:38:32 +0000900
901 if (LLVMHasPersonalityFn(Cur)) {
902 const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur));
903 LLVMValueRef P = LLVMGetNamedFunction(M, FName);
904 if (!P)
905 report_fatal_error("Could not find personality function");
906 LLVMSetPersonalityFn(Fun, P);
907 }
908
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000909 FunCloner FC(Cur, Fun);
910 FC.CloneBBs(Cur);
911
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000912 Next = LLVMGetNextFunction(Cur);
913 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000914 if (Cur != End)
915 report_fatal_error("Last function does not match End");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000916 break;
917 }
918
919 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000920 if (Prev != Cur)
921 report_fatal_error("Next.Previous function is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000922
923 Cur = Next;
924 }
925}
926
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000927int llvm_echo(void) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000928 LLVMEnablePrettyStackTrace();
929
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000930 LLVMModuleRef Src = llvm_load_module(false, true);
Robert Widmann490a5802018-01-30 21:34:29 +0000931 size_t SourceFileLen;
932 const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen);
933 size_t ModuleIdentLen;
934 const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000935 LLVMContextRef Ctx = LLVMContextCreate();
Peter Zotov0a2fa0a2016-04-05 13:56:59 +0000936 LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
937
Robert Widmann490a5802018-01-30 21:34:29 +0000938 LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
939 LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000940
Amaury Sechet55909672016-02-16 05:11:24 +0000941 LLVMSetTarget(M, LLVMGetTarget(Src));
942 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
943 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
944 report_fatal_error("Inconsistent DataLayout string representation");
945
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000946 declare_symbols(Src, M);
947 clone_symbols(Src, M);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000948 char *Str = LLVMPrintModuleToString(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000949 fputs(Str, stdout);
950
951 LLVMDisposeMessage(Str);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000952 LLVMDisposeModule(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000953 LLVMContextDispose(Ctx);
954
955 return 0;
956}