blob: 05d803631627d84afceae66e88a128e206948201 [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) {
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:
whitequark131f98f2017-10-27 11:51:40 +0000145 return LLVMMetadataTypeInContext(Ctx);
Amaury Sechetaad93532016-02-10 00:38:50 +0000146 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 Sechet49491312016-04-07 05:56:20 +0000215static void check_value_kind(LLVMValueRef V, LLVMValueKind K) {
216 if (LLVMGetValueKind(V) != K)
217 report_fatal_error("LLVMGetValueKind returned incorrect type");
218}
219
220static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M);
221
222static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
223 LLVMValueRef Ret = clone_constant_impl(Cst, M);
224 check_value_kind(Ret, LLVMGetValueKind(Cst));
225 return Ret;
226}
227
228static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000229 if (!LLVMIsAConstant(Cst))
230 report_fatal_error("Expected a constant");
231
232 // Maybe it is a symbol
233 if (LLVMIsAGlobalValue(Cst)) {
234 const char *Name = LLVMGetValueName(Cst);
235
236 // Try function
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000237 if (LLVMIsAFunction(Cst)) {
Amaury Sechet49491312016-04-07 05:56:20 +0000238 check_value_kind(Cst, LLVMFunctionValueKind);
239 LLVMValueRef Dst = LLVMGetNamedFunction(M, Name);
240 if (Dst)
241 return Dst;
242 report_fatal_error("Could not find function");
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000243 }
Amaury Sechet49491312016-04-07 05:56:20 +0000244
245 // Try global variable
246 if (LLVMIsAGlobalVariable(Cst)) {
247 check_value_kind(Cst, LLVMGlobalVariableValueKind);
248 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
249 if (Dst)
250 return Dst;
251 report_fatal_error("Could not find function");
252 }
253
254 fprintf(stderr, "Could not find @%s\n", Name);
255 exit(-1);
256 }
257
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000258 // Try integer literal
Amaury Sechet49491312016-04-07 05:56:20 +0000259 if (LLVMIsAConstantInt(Cst)) {
260 check_value_kind(Cst, LLVMConstantIntValueKind);
261 return LLVMConstInt(TypeCloner(M).Clone(Cst),
262 LLVMConstIntGetZExtValue(Cst), false);
263 }
264
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000265 // Try zeroinitializer
Amaury Sechet49491312016-04-07 05:56:20 +0000266 if (LLVMIsAConstantAggregateZero(Cst)) {
267 check_value_kind(Cst, LLVMConstantAggregateZeroValueKind);
268 return LLVMConstNull(TypeCloner(M).Clone(Cst));
269 }
270
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000271 // Try constant array
Amaury Sechet49491312016-04-07 05:56:20 +0000272 if (LLVMIsAConstantArray(Cst)) {
273 check_value_kind(Cst, LLVMConstantArrayValueKind);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000274 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
275 unsigned EltCount = LLVMGetArrayLength(Ty);
276 SmallVector<LLVMValueRef, 8> Elts;
277 for (unsigned i = 0; i < EltCount; i++)
278 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
Amaury Sechet49491312016-04-07 05:56:20 +0000279 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
280 }
281
Amaury Sechetb3256862016-03-13 00:58:25 +0000282 // Try contant data array
Amaury Sechet49491312016-04-07 05:56:20 +0000283 if (LLVMIsAConstantDataArray(Cst)) {
284 check_value_kind(Cst, LLVMConstantDataArrayValueKind);
Amaury Sechetb3256862016-03-13 00:58:25 +0000285 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
286 unsigned EltCount = LLVMGetArrayLength(Ty);
287 SmallVector<LLVMValueRef, 8> Elts;
288 for (unsigned i = 0; i < EltCount; i++)
289 Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
Amaury Sechet49491312016-04-07 05:56:20 +0000290 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
291 }
292
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000293 // Try constant struct
Amaury Sechet49491312016-04-07 05:56:20 +0000294 if (LLVMIsAConstantStruct(Cst)) {
295 check_value_kind(Cst, LLVMConstantStructValueKind);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000296 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
297 unsigned EltCount = LLVMCountStructElementTypes(Ty);
298 SmallVector<LLVMValueRef, 8> Elts;
299 for (unsigned i = 0; i < EltCount; i++)
300 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
301 if (LLVMGetStructName(Ty))
Amaury Sechet49491312016-04-07 05:56:20 +0000302 return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
303 return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
304 EltCount, LLVMIsPackedStruct(Ty));
305 }
306
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000307 // Try undef
Amaury Sechet49491312016-04-07 05:56:20 +0000308 if (LLVMIsUndef(Cst)) {
309 check_value_kind(Cst, LLVMUndefValueValueKind);
310 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
311 }
312
Amaury Sechet40bbe512016-02-17 23:55:59 +0000313 // Try float literal
Amaury Sechet49491312016-04-07 05:56:20 +0000314 if (LLVMIsAConstantFP(Cst)) {
315 check_value_kind(Cst, LLVMConstantFPValueKind);
Amaury Sechet40bbe512016-02-17 23:55:59 +0000316 report_fatal_error("ConstantFP is not supported");
Amaury Sechet49491312016-04-07 05:56:20 +0000317 }
318
Amaury Sechet6b16c232016-02-16 07:33:23 +0000319 // This kind of constant is not supported
Amaury Sechet49491312016-04-07 05:56:20 +0000320 if (!LLVMIsAConstantExpr(Cst))
Amaury Sechet6b16c232016-02-16 07:33:23 +0000321 report_fatal_error("Expected a constant expression");
Amaury Sechet49491312016-04-07 05:56:20 +0000322
323 // At this point, it must be a constant expression
324 check_value_kind(Cst, LLVMConstantExprValueKind);
325
326 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
327 switch(Op) {
328 case LLVMBitCast:
329 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
330 TypeCloner(M).Clone(Cst));
331 default:
332 fprintf(stderr, "%d is not a supported opcode\n", Op);
333 exit(-1);
Amaury Sechet40bbe512016-02-17 23:55:59 +0000334 }
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000335}
336
Amaury Secheta82042e2016-02-09 22:36:41 +0000337struct FunCloner {
338 LLVMValueRef Fun;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000339 LLVMModuleRef M;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000340
Amaury Secheta82042e2016-02-09 22:36:41 +0000341 ValueMap VMap;
342 BasicBlockMap BBMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000343
Amaury Sechetaad93532016-02-10 00:38:50 +0000344 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
345 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
346
347 LLVMTypeRef CloneType(LLVMTypeRef Src) {
348 return TypeCloner(M).Clone(Src);
349 }
350
351 LLVMTypeRef CloneType(LLVMValueRef Src) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000352 return TypeCloner(M).Clone(Src);
Amaury Sechetaad93532016-02-10 00:38:50 +0000353 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000354
Amaury Secheta82042e2016-02-09 22:36:41 +0000355 // Try to clone everything in the llvm::Value hierarchy.
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000356 LLVMValueRef CloneValue(LLVMValueRef Src) {
Amaury Secheta82042e2016-02-09 22:36:41 +0000357 // First, the value may be constant.
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000358 if (LLVMIsAConstant(Src))
359 return clone_constant(Src, M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000360
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000361 // Function argument should always be in the map already.
Amaury Sechet2f432082016-02-11 21:37:54 +0000362 auto i = VMap.find(Src);
363 if (i != VMap.end())
364 return i->second;
Amaury Secheta82042e2016-02-09 22:36:41 +0000365
Amaury Sechet2f432082016-02-11 21:37:54 +0000366 if (!LLVMIsAInstruction(Src))
367 report_fatal_error("Expected an instruction");
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000368
Amaury Sechet2f432082016-02-11 21:37:54 +0000369 auto Ctx = LLVMGetModuleContext(M);
370 auto Builder = LLVMCreateBuilderInContext(Ctx);
371 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
372 LLVMPositionBuilderAtEnd(Builder, BB);
373 auto Dst = CloneInstruction(Src, Builder);
374 LLVMDisposeBuilder(Builder);
375 return Dst;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000376 }
377
Amaury Secheta65a2372016-06-15 05:14:29 +0000378 void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) {
379 auto Ctx = LLVMGetModuleContext(M);
380 int ArgCount = LLVMGetNumArgOperands(Src);
381 for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) {
382 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
383 if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) {
384 auto Val = LLVMGetEnumAttributeValue(SrcA);
385 auto A = LLVMCreateEnumAttribute(Ctx, k, Val);
386 LLVMAddCallSiteAttribute(Dst, i, A);
387 }
388 }
389 }
390 }
391
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000392 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
Amaury Sechet49491312016-04-07 05:56:20 +0000393 check_value_kind(Src, LLVMInstructionValueKind);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000394 if (!LLVMIsAInstruction(Src))
395 report_fatal_error("Expected an instruction");
396
Amaury Sechet49491312016-04-07 05:56:20 +0000397 const char *Name = LLVMGetValueName(Src);
Peter Zotov3e4561c2016-04-06 22:21:29 +0000398
Amaury Secheta82042e2016-02-09 22:36:41 +0000399 // Check if this is something we already computed.
400 {
401 auto i = VMap.find(Src);
Amaury Sechet2f432082016-02-11 21:37:54 +0000402 if (i != VMap.end()) {
403 // If we have a hit, it means we already generated the instruction
404 // as a dependancy to somethign else. We need to make sure
405 // it is ordered properly.
406 auto I = i->second;
407 LLVMInstructionRemoveFromParent(I);
408 LLVMInsertIntoBuilderWithName(Builder, I, Name);
409 return I;
410 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000411 }
412
413 // We tried everything, it must be an instruction
414 // that hasn't been generated already.
415 LLVMValueRef Dst = nullptr;
416
417 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
418 switch(Op) {
419 case LLVMRet: {
420 int OpCount = LLVMGetNumOperands(Src);
421 if (OpCount == 0)
422 Dst = LLVMBuildRetVoid(Builder);
423 else
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000424 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
Amaury Secheta82042e2016-02-09 22:36:41 +0000425 break;
426 }
427 case LLVMBr: {
Amaury Sechete7e62172016-02-09 23:15:02 +0000428 if (!LLVMIsConditional(Src)) {
429 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
430 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
431 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
432 break;
433 }
434
435 LLVMValueRef Cond = LLVMGetCondition(Src);
436 LLVMValueRef Else = LLVMGetOperand(Src, 1);
437 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
438 LLVMValueRef Then = LLVMGetOperand(Src, 2);
439 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
440 Dst = LLVMBuildCondBr(Builder, Cond, ThenBB, ElseBB);
Amaury Secheta82042e2016-02-09 22:36:41 +0000441 break;
442 }
443 case LLVMSwitch:
444 case LLVMIndirectBr:
Amaury Secheta82042e2016-02-09 22:36:41 +0000445 break;
Amaury Sechete39e8532016-02-18 20:38:32 +0000446 case LLVMInvoke: {
447 SmallVector<LLVMValueRef, 8> Args;
448 int ArgCount = LLVMGetNumArgOperands(Src);
449 for (int i = 0; i < ArgCount; i++)
450 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
451 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
452 LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
453 LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
454 Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
455 Then, Unwind, Name);
Amaury Secheta65a2372016-06-15 05:14:29 +0000456 CloneAttrs(Src, Dst);
Amaury Sechete39e8532016-02-18 20:38:32 +0000457 break;
458 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000459 case LLVMUnreachable:
460 Dst = LLVMBuildUnreachable(Builder);
461 break;
462 case LLVMAdd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000463 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
464 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000465 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
466 break;
467 }
468 case LLVMSub: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000469 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
470 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000471 Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
472 break;
473 }
474 case LLVMMul: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000475 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
476 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000477 Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
478 break;
479 }
480 case LLVMUDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000481 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
482 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000483 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
484 break;
485 }
486 case LLVMSDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000487 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
488 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000489 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
490 break;
491 }
492 case LLVMURem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000493 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
494 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000495 Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
496 break;
497 }
498 case LLVMSRem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000499 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
500 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000501 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
502 break;
503 }
504 case LLVMShl: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000505 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
506 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000507 Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
508 break;
509 }
510 case LLVMLShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000511 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
512 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000513 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
514 break;
515 }
516 case LLVMAShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000517 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
518 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000519 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
520 break;
521 }
522 case LLVMAnd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000523 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
524 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000525 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
526 break;
527 }
528 case LLVMOr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000529 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
530 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000531 Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
532 break;
533 }
534 case LLVMXor: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000535 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
536 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000537 Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
538 break;
539 }
540 case LLVMAlloca: {
Amaury Sechetaad93532016-02-10 00:38:50 +0000541 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000542 Dst = LLVMBuildAlloca(Builder, Ty, Name);
543 break;
544 }
Amaury Sechet053ac452016-02-17 22:51:03 +0000545 case LLVMLoad: {
546 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
547 Dst = LLVMBuildLoad(Builder, Ptr, Name);
548 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
549 break;
550 }
551 case LLVMStore: {
552 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
553 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
554 Dst = LLVMBuildStore(Builder, Val, Ptr);
555 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
556 break;
557 }
558 case LLVMGetElementPtr: {
559 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
560 SmallVector<LLVMValueRef, 8> Idx;
561 int NumIdx = LLVMGetNumIndices(Src);
562 for (int i = 1; i <= NumIdx; i++)
563 Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
564 if (LLVMIsInBounds(Src))
565 Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
566 else
567 Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
568 break;
569 }
Mehdi Amini43165d92016-03-19 21:28:28 +0000570 case LLVMAtomicCmpXchg: {
571 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
572 LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
573 LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
574 LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
575 LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
576 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
577
578 Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
579 SingleThread);
580 } break;
Amaury Sechet40bbe512016-02-17 23:55:59 +0000581 case LLVMBitCast: {
582 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
583 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
584 break;
585 }
Amaury Sechete7e62172016-02-09 23:15:02 +0000586 case LLVMICmp: {
587 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000588 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
589 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Sechete7e62172016-02-09 23:15:02 +0000590 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
591 break;
592 }
Amaury Sechet2f432082016-02-11 21:37:54 +0000593 case LLVMPHI: {
Simon Pilgrim68168d12017-03-30 12:59:53 +0000594 // We need to aggressively set things here because of loops.
Amaury Sechet2f432082016-02-11 21:37:54 +0000595 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
596
597 SmallVector<LLVMValueRef, 8> Values;
598 SmallVector<LLVMBasicBlockRef, 8> Blocks;
599
600 unsigned IncomingCount = LLVMCountIncoming(Src);
601 for (unsigned i = 0; i < IncomingCount; ++i) {
602 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
603 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
604 }
605
606 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
607 return Dst;
608 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000609 case LLVMCall: {
Amaury Secheta82042e2016-02-09 22:36:41 +0000610 SmallVector<LLVMValueRef, 8> Args;
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000611 int ArgCount = LLVMGetNumArgOperands(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000612 for (int i = 0; i < ArgCount; i++)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000613 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000614 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000615 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
Amaury Sechete39e8532016-02-18 20:38:32 +0000616 LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
Amaury Secheta65a2372016-06-15 05:14:29 +0000617 CloneAttrs(Src, Dst);
Amaury Sechete39e8532016-02-18 20:38:32 +0000618 break;
619 }
620 case LLVMResume: {
621 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
622 break;
623 }
624 case LLVMLandingPad: {
625 // The landing pad API is a bit screwed up for historical reasons.
626 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
627 unsigned NumClauses = LLVMGetNumClauses(Src);
628 for (unsigned i = 0; i < NumClauses; ++i)
629 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
630 LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000631 break;
632 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000633 case LLVMExtractValue: {
634 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
635 if (LLVMGetNumIndices(Src) != 1)
636 report_fatal_error("Expected only one indice");
637 auto I = LLVMGetIndices(Src)[0];
638 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
639 break;
640 }
641 case LLVMInsertValue: {
642 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
643 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
644 if (LLVMGetNumIndices(Src) != 1)
645 report_fatal_error("Expected only one indice");
646 auto I = LLVMGetIndices(Src)[0];
647 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
648 break;
649 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000650 default:
651 break;
652 }
653
654 if (Dst == nullptr) {
655 fprintf(stderr, "%d is not a supported opcode\n", Op);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000656 exit(-1);
657 }
658
Amaury Sechet49491312016-04-07 05:56:20 +0000659 check_value_kind(Dst, LLVMInstructionValueKind);
Amaury Secheta82042e2016-02-09 22:36:41 +0000660 return VMap[Src] = Dst;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000661 }
662
Amaury Secheta82042e2016-02-09 22:36:41 +0000663 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
664 // Check if this is something we already computed.
665 {
666 auto i = BBMap.find(Src);
667 if (i != BBMap.end()) {
668 return i->second;
669 }
670 }
671
Amaury Secheta82042e2016-02-09 22:36:41 +0000672 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
673 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000674 report_fatal_error("Basic block is not a basic block");
Amaury Secheta82042e2016-02-09 22:36:41 +0000675
Amaury Sechetd01c8612016-02-14 10:06:34 +0000676 const char *Name = LLVMGetBasicBlockName(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000677 const char *VName = LLVMGetValueName(V);
678 if (Name != VName)
679 report_fatal_error("Basic block name mismatch");
680
681 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
682 return BBMap[Src] = BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000683 }
684
Amaury Secheta82042e2016-02-09 22:36:41 +0000685 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
686 LLVMBasicBlockRef BB = DeclareBB(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000687
Amaury Secheta82042e2016-02-09 22:36:41 +0000688 // Make sure ordering is correct.
689 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
690 if (Prev)
691 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000692
Amaury Secheta82042e2016-02-09 22:36:41 +0000693 LLVMValueRef First = LLVMGetFirstInstruction(Src);
694 LLVMValueRef Last = LLVMGetLastInstruction(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000695
Amaury Secheta82042e2016-02-09 22:36:41 +0000696 if (First == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000697 if (Last != nullptr)
698 report_fatal_error("Has no first instruction, but last one");
Amaury Secheta82042e2016-02-09 22:36:41 +0000699 return BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000700 }
701
Amaury Sechetaad93532016-02-10 00:38:50 +0000702 auto Ctx = LLVMGetModuleContext(M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000703 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
704 LLVMPositionBuilderAtEnd(Builder, BB);
705
706 LLVMValueRef Cur = First;
707 LLVMValueRef Next = nullptr;
708 while(true) {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000709 CloneInstruction(Cur, Builder);
Amaury Secheta82042e2016-02-09 22:36:41 +0000710 Next = LLVMGetNextInstruction(Cur);
711 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000712 if (Cur != Last)
713 report_fatal_error("Final instruction does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000714 break;
715 }
716
717 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000718 if (Prev != Cur)
719 report_fatal_error("Next.Previous instruction is not Current");
Amaury Secheta82042e2016-02-09 22:36:41 +0000720
721 Cur = Next;
722 }
723
724 LLVMDisposeBuilder(Builder);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000725 return BB;
726 }
727
Amaury Secheta82042e2016-02-09 22:36:41 +0000728 void CloneBBs(LLVMValueRef Src) {
729 unsigned Count = LLVMCountBasicBlocks(Src);
730 if (Count == 0)
731 return;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000732
Amaury Secheta82042e2016-02-09 22:36:41 +0000733 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
734 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
735
736 LLVMBasicBlockRef Cur = First;
737 LLVMBasicBlockRef Next = nullptr;
738 while(true) {
739 CloneBB(Cur);
740 Count--;
741 Next = LLVMGetNextBasicBlock(Cur);
742 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000743 if (Cur != Last)
744 report_fatal_error("Final basic block does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000745 break;
746 }
747
748 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000749 if (Prev != Cur)
750 report_fatal_error("Next.Previous basic bloc is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000751
Amaury Secheta82042e2016-02-09 22:36:41 +0000752 Cur = Next;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000753 }
754
Amaury Sechetd01c8612016-02-14 10:06:34 +0000755 if (Count != 0)
756 report_fatal_error("Basic block count does not match iterration");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000757 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000758};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000759
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000760static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000761 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
762 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000763
764 LLVMValueRef Cur = Begin;
765 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000766 if (!Begin) {
767 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000768 report_fatal_error("Range has an end but no beginning");
Amaury Sechet58946a92016-02-17 22:30:05 +0000769 goto FunDecl;
770 }
771
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000772 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000773 const char *Name = LLVMGetValueName(Cur);
774 if (LLVMGetNamedGlobal(M, Name))
775 report_fatal_error("GlobalVariable already cloned");
776 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
777
778 Next = LLVMGetNextGlobal(Cur);
779 if (Next == nullptr) {
780 if (Cur != End)
781 report_fatal_error("");
782 break;
783 }
784
785 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
786 if (Prev != Cur)
787 report_fatal_error("Next.Previous global is not Current");
788
789 Cur = Next;
790 }
791
Amaury Sechet58946a92016-02-17 22:30:05 +0000792FunDecl:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000793 Begin = LLVMGetFirstFunction(Src);
794 End = LLVMGetLastFunction(Src);
795 if (!Begin) {
796 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000797 report_fatal_error("Range has an end but no beginning");
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000798 return;
799 }
800
Amaury Sechet5db224e2016-06-12 06:17:24 +0000801 auto Ctx = LLVMGetModuleContext(M);
802
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000803 Cur = Begin;
804 Next = nullptr;
805 while (true) {
806 const char *Name = LLVMGetValueName(Cur);
807 if (LLVMGetNamedFunction(M, Name))
808 report_fatal_error("Function already cloned");
Amaury Sechet5db224e2016-06-12 06:17:24 +0000809 auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur));
810 auto F = LLVMAddFunction(M, Name, Ty);
811
812 // Copy attributes
813 for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F);
814 i <= c; ++i) {
815 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
816 if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) {
817 auto Val = LLVMGetEnumAttributeValue(SrcA);
818 auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val);
819 LLVMAddAttributeAtIndex(F, i, DstA);
820 }
821 }
822 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000823
Amaury Sechetf64e8342016-02-16 07:08:49 +0000824 Next = LLVMGetNextFunction(Cur);
825 if (Next == nullptr) {
826 if (Cur != End)
827 report_fatal_error("Last function does not match End");
828 break;
829 }
830
831 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
832 if (Prev != Cur)
833 report_fatal_error("Next.Previous function is not Current");
834
835 Cur = Next;
836 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000837}
Amaury Sechetf64e8342016-02-16 07:08:49 +0000838
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000839static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000840 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
841 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000842
843 LLVMValueRef Cur = Begin;
844 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000845 if (!Begin) {
846 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000847 report_fatal_error("Range has an end but no beginning");
Amaury Sechet58946a92016-02-17 22:30:05 +0000848 goto FunClone;
849 }
850
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000851 while (true) {
852 const char *Name = LLVMGetValueName(Cur);
853 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
854 if (!G)
855 report_fatal_error("GlobalVariable must have been declared already");
856
857 if (auto I = LLVMGetInitializer(Cur))
858 LLVMSetInitializer(G, clone_constant(I, M));
859
860 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
861 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
862 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
863 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
864 LLVMSetSection(G, LLVMGetSection(Cur));
865 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
866 LLVMSetUnnamedAddr(G, LLVMHasUnnamedAddr(Cur));
867 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
868
869 Next = LLVMGetNextGlobal(Cur);
870 if (Next == nullptr) {
871 if (Cur != End)
872 report_fatal_error("");
873 break;
874 }
875
876 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
877 if (Prev != Cur)
878 report_fatal_error("Next.Previous global is not Current");
879
880 Cur = Next;
881 }
882
Amaury Sechet58946a92016-02-17 22:30:05 +0000883FunClone:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000884 Begin = LLVMGetFirstFunction(Src);
885 End = LLVMGetLastFunction(Src);
886 if (!Begin) {
887 if (End != nullptr)
Hiroshi Inouee9dea6e2017-07-13 06:48:39 +0000888 report_fatal_error("Range has an end but no beginning");
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000889 return;
890 }
891
Amaury Sechetf64e8342016-02-16 07:08:49 +0000892 Cur = Begin;
893 Next = nullptr;
894 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000895 const char *Name = LLVMGetValueName(Cur);
896 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
897 if (!Fun)
898 report_fatal_error("Function must have been declared already");
Amaury Sechete39e8532016-02-18 20:38:32 +0000899
900 if (LLVMHasPersonalityFn(Cur)) {
901 const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur));
902 LLVMValueRef P = LLVMGetNamedFunction(M, FName);
903 if (!P)
904 report_fatal_error("Could not find personality function");
905 LLVMSetPersonalityFn(Fun, P);
906 }
907
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000908 FunCloner FC(Cur, Fun);
909 FC.CloneBBs(Cur);
910
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000911 Next = LLVMGetNextFunction(Cur);
912 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000913 if (Cur != End)
914 report_fatal_error("Last function does not match End");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000915 break;
916 }
917
918 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000919 if (Prev != Cur)
920 report_fatal_error("Next.Previous function is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000921
922 Cur = Next;
923 }
924}
925
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000926int llvm_echo(void) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000927 LLVMEnablePrettyStackTrace();
928
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000929 LLVMModuleRef Src = llvm_load_module(false, true);
Robert Widmann490a5802018-01-30 21:34:29 +0000930 size_t SourceFileLen;
931 const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen);
932 size_t ModuleIdentLen;
933 const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000934 LLVMContextRef Ctx = LLVMContextCreate();
Peter Zotov0a2fa0a2016-04-05 13:56:59 +0000935 LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
936
Robert Widmann490a5802018-01-30 21:34:29 +0000937 LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
938 LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000939
Amaury Sechet55909672016-02-16 05:11:24 +0000940 LLVMSetTarget(M, LLVMGetTarget(Src));
941 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
942 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
943 report_fatal_error("Inconsistent DataLayout string representation");
944
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000945 declare_symbols(Src, M);
946 clone_symbols(Src, M);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000947 char *Str = LLVMPrintModuleToString(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000948 fputs(Str, stdout);
949
950 LLVMDisposeMessage(Str);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000951 LLVMDisposeModule(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000952 LLVMContextDispose(Ctx);
953
954 return 0;
955}