blob: 22cbcf1b618566ea8c21568169135d03d012ed4f [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
23#include <stdio.h>
24#include <stdlib.h>
25
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
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000157static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
158 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) {
216 if (!LLVMIsAConstant(Cst))
217 report_fatal_error("Expected a constant");
218
219 // Maybe it is a symbol
220 if (LLVMIsAGlobalValue(Cst)) {
221 const char *Name = LLVMGetValueName(Cst);
222
223 // Try function
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000224 if (LLVMIsAFunction(Cst)) {
225 LLVMValueRef Dst = LLVMGetNamedFunction(M, Name);
226 if (Dst)
227 return Dst;
228 report_fatal_error("Could not find function");
229 }
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000230
231 // Try global variable
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000232 if (LLVMIsAGlobalVariable(Cst)) {
233 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
234 if (Dst)
235 return Dst;
236 report_fatal_error("Could not find function");
237 }
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000238
239 fprintf(stderr, "Could not find @%s\n", Name);
240 exit(-1);
241 }
242
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000243 // Try integer literal
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000244 if (LLVMIsAConstantInt(Cst))
245 return LLVMConstInt(TypeCloner(M).Clone(Cst),
246 LLVMConstIntGetZExtValue(Cst), false);
247
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000248 // Try zeroinitializer
249 if (LLVMIsAConstantAggregateZero(Cst))
250 return LLVMConstNull(TypeCloner(M).Clone(Cst));
251
252 // Try constant array
253 if (LLVMIsAConstantArray(Cst)) {
254 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
255 unsigned EltCount = LLVMGetArrayLength(Ty);
256 SmallVector<LLVMValueRef, 8> Elts;
257 for (unsigned i = 0; i < EltCount; i++)
258 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
259 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
260 }
261
262 // Try constant struct
263 if (LLVMIsAConstantStruct(Cst)) {
264 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
265 unsigned EltCount = LLVMCountStructElementTypes(Ty);
266 SmallVector<LLVMValueRef, 8> Elts;
267 for (unsigned i = 0; i < EltCount; i++)
268 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
269 if (LLVMGetStructName(Ty))
270 return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
271 return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
272 EltCount, LLVMIsPackedStruct(Ty));
273 }
274
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000275 // Try undef
276 if (LLVMIsUndef(Cst))
277 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
278
Amaury Sechet6b16c232016-02-16 07:33:23 +0000279 // This kind of constant is not supported
280 if (!LLVMIsAConstantExpr(Cst))
281 report_fatal_error("Expected a constant expression");
282
283 // At this point, it must be a constant expression
284 report_fatal_error("ConstantExpression are not supported");
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000285}
286
Amaury Secheta82042e2016-02-09 22:36:41 +0000287struct FunCloner {
288 LLVMValueRef Fun;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000289 LLVMModuleRef M;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000290
Amaury Secheta82042e2016-02-09 22:36:41 +0000291 ValueMap VMap;
292 BasicBlockMap BBMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000293
Amaury Sechetaad93532016-02-10 00:38:50 +0000294 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
295 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
296
297 LLVMTypeRef CloneType(LLVMTypeRef Src) {
298 return TypeCloner(M).Clone(Src);
299 }
300
301 LLVMTypeRef CloneType(LLVMValueRef Src) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000302 return TypeCloner(M).Clone(Src);
Amaury Sechetaad93532016-02-10 00:38:50 +0000303 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000304
Amaury Secheta82042e2016-02-09 22:36:41 +0000305 // Try to clone everything in the llvm::Value hierarchy.
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000306 LLVMValueRef CloneValue(LLVMValueRef Src) {
Amaury Secheta82042e2016-02-09 22:36:41 +0000307 // First, the value may be constant.
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000308 if (LLVMIsAConstant(Src))
309 return clone_constant(Src, M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000310
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000311 // Function argument should always be in the map already.
Amaury Sechet2f432082016-02-11 21:37:54 +0000312 auto i = VMap.find(Src);
313 if (i != VMap.end())
314 return i->second;
Amaury Secheta82042e2016-02-09 22:36:41 +0000315
Amaury Sechet2f432082016-02-11 21:37:54 +0000316 if (!LLVMIsAInstruction(Src))
317 report_fatal_error("Expected an instruction");
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000318
Amaury Sechet2f432082016-02-11 21:37:54 +0000319 auto Ctx = LLVMGetModuleContext(M);
320 auto Builder = LLVMCreateBuilderInContext(Ctx);
321 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
322 LLVMPositionBuilderAtEnd(Builder, BB);
323 auto Dst = CloneInstruction(Src, Builder);
324 LLVMDisposeBuilder(Builder);
325 return Dst;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000326 }
327
328 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
329 const char *Name = LLVMGetValueName(Src);
330 if (!LLVMIsAInstruction(Src))
331 report_fatal_error("Expected an instruction");
332
Amaury Secheta82042e2016-02-09 22:36:41 +0000333 // Check if this is something we already computed.
334 {
335 auto i = VMap.find(Src);
Amaury Sechet2f432082016-02-11 21:37:54 +0000336 if (i != VMap.end()) {
337 // If we have a hit, it means we already generated the instruction
338 // as a dependancy to somethign else. We need to make sure
339 // it is ordered properly.
340 auto I = i->second;
341 LLVMInstructionRemoveFromParent(I);
342 LLVMInsertIntoBuilderWithName(Builder, I, Name);
343 return I;
344 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000345 }
346
347 // We tried everything, it must be an instruction
348 // that hasn't been generated already.
349 LLVMValueRef Dst = nullptr;
350
351 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
352 switch(Op) {
353 case LLVMRet: {
354 int OpCount = LLVMGetNumOperands(Src);
355 if (OpCount == 0)
356 Dst = LLVMBuildRetVoid(Builder);
357 else
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000358 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
Amaury Secheta82042e2016-02-09 22:36:41 +0000359 break;
360 }
361 case LLVMBr: {
Amaury Sechete7e62172016-02-09 23:15:02 +0000362 if (!LLVMIsConditional(Src)) {
363 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
364 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
365 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
366 break;
367 }
368
369 LLVMValueRef Cond = LLVMGetCondition(Src);
370 LLVMValueRef Else = LLVMGetOperand(Src, 1);
371 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
372 LLVMValueRef Then = LLVMGetOperand(Src, 2);
373 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
374 Dst = LLVMBuildCondBr(Builder, Cond, ThenBB, ElseBB);
Amaury Secheta82042e2016-02-09 22:36:41 +0000375 break;
376 }
377 case LLVMSwitch:
378 case LLVMIndirectBr:
379 case LLVMInvoke:
380 break;
381 case LLVMUnreachable:
382 Dst = LLVMBuildUnreachable(Builder);
383 break;
384 case LLVMAdd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000385 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
386 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000387 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
388 break;
389 }
390 case LLVMSub: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000391 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
392 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000393 Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
394 break;
395 }
396 case LLVMMul: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000397 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
398 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000399 Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
400 break;
401 }
402 case LLVMUDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000403 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
404 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000405 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
406 break;
407 }
408 case LLVMSDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000409 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
410 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000411 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
412 break;
413 }
414 case LLVMURem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000415 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
416 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000417 Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
418 break;
419 }
420 case LLVMSRem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000421 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
422 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000423 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
424 break;
425 }
426 case LLVMShl: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000427 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
428 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000429 Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
430 break;
431 }
432 case LLVMLShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000433 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
434 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000435 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
436 break;
437 }
438 case LLVMAShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000439 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
440 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000441 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
442 break;
443 }
444 case LLVMAnd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000445 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
446 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000447 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
448 break;
449 }
450 case LLVMOr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000451 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
452 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000453 Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
454 break;
455 }
456 case LLVMXor: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000457 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
458 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000459 Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
460 break;
461 }
462 case LLVMAlloca: {
Amaury Sechetaad93532016-02-10 00:38:50 +0000463 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000464 Dst = LLVMBuildAlloca(Builder, Ty, Name);
465 break;
466 }
Amaury Sechet053ac452016-02-17 22:51:03 +0000467 case LLVMLoad: {
468 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
469 Dst = LLVMBuildLoad(Builder, Ptr, Name);
470 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
471 break;
472 }
473 case LLVMStore: {
474 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
475 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
476 Dst = LLVMBuildStore(Builder, Val, Ptr);
477 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
478 break;
479 }
480 case LLVMGetElementPtr: {
481 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
482 SmallVector<LLVMValueRef, 8> Idx;
483 int NumIdx = LLVMGetNumIndices(Src);
484 for (int i = 1; i <= NumIdx; i++)
485 Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
486 if (LLVMIsInBounds(Src))
487 Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
488 else
489 Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
490 break;
491 }
Amaury Sechete7e62172016-02-09 23:15:02 +0000492 case LLVMICmp: {
493 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000494 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
495 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Sechete7e62172016-02-09 23:15:02 +0000496 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
497 break;
498 }
Amaury Sechet2f432082016-02-11 21:37:54 +0000499 case LLVMPHI: {
500 // We need to agressively set things here because of loops.
501 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
502
503 SmallVector<LLVMValueRef, 8> Values;
504 SmallVector<LLVMBasicBlockRef, 8> Blocks;
505
506 unsigned IncomingCount = LLVMCountIncoming(Src);
507 for (unsigned i = 0; i < IncomingCount; ++i) {
508 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
509 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
510 }
511
512 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
513 return Dst;
514 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000515 case LLVMCall: {
Amaury Secheta82042e2016-02-09 22:36:41 +0000516 SmallVector<LLVMValueRef, 8> Args;
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000517 int ArgCount = LLVMGetNumArgOperands(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000518 for (int i = 0; i < ArgCount; i++)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000519 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000520 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000521 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
522 break;
523 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000524 case LLVMExtractValue: {
525 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
526 if (LLVMGetNumIndices(Src) != 1)
527 report_fatal_error("Expected only one indice");
528 auto I = LLVMGetIndices(Src)[0];
529 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
530 break;
531 }
532 case LLVMInsertValue: {
533 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
534 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
535 if (LLVMGetNumIndices(Src) != 1)
536 report_fatal_error("Expected only one indice");
537 auto I = LLVMGetIndices(Src)[0];
538 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
539 break;
540 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000541 default:
542 break;
543 }
544
545 if (Dst == nullptr) {
546 fprintf(stderr, "%d is not a supported opcode\n", Op);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000547 exit(-1);
548 }
549
Amaury Secheta82042e2016-02-09 22:36:41 +0000550 return VMap[Src] = Dst;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000551 }
552
Amaury Secheta82042e2016-02-09 22:36:41 +0000553 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
554 // Check if this is something we already computed.
555 {
556 auto i = BBMap.find(Src);
557 if (i != BBMap.end()) {
558 return i->second;
559 }
560 }
561
Amaury Secheta82042e2016-02-09 22:36:41 +0000562 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
563 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000564 report_fatal_error("Basic block is not a basic block");
Amaury Secheta82042e2016-02-09 22:36:41 +0000565
Amaury Sechetd01c8612016-02-14 10:06:34 +0000566 const char *Name = LLVMGetBasicBlockName(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000567 const char *VName = LLVMGetValueName(V);
568 if (Name != VName)
569 report_fatal_error("Basic block name mismatch");
570
571 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
572 return BBMap[Src] = BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000573 }
574
Amaury Secheta82042e2016-02-09 22:36:41 +0000575 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
576 LLVMBasicBlockRef BB = DeclareBB(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000577
Amaury Secheta82042e2016-02-09 22:36:41 +0000578 // Make sure ordering is correct.
579 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
580 if (Prev)
581 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000582
Amaury Secheta82042e2016-02-09 22:36:41 +0000583 LLVMValueRef First = LLVMGetFirstInstruction(Src);
584 LLVMValueRef Last = LLVMGetLastInstruction(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000585
Amaury Secheta82042e2016-02-09 22:36:41 +0000586 if (First == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000587 if (Last != nullptr)
588 report_fatal_error("Has no first instruction, but last one");
Amaury Secheta82042e2016-02-09 22:36:41 +0000589 return BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000590 }
591
Amaury Sechetaad93532016-02-10 00:38:50 +0000592 auto Ctx = LLVMGetModuleContext(M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000593 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
594 LLVMPositionBuilderAtEnd(Builder, BB);
595
596 LLVMValueRef Cur = First;
597 LLVMValueRef Next = nullptr;
598 while(true) {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000599 CloneInstruction(Cur, Builder);
Amaury Secheta82042e2016-02-09 22:36:41 +0000600 Next = LLVMGetNextInstruction(Cur);
601 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000602 if (Cur != Last)
603 report_fatal_error("Final instruction does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000604 break;
605 }
606
607 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000608 if (Prev != Cur)
609 report_fatal_error("Next.Previous instruction is not Current");
Amaury Secheta82042e2016-02-09 22:36:41 +0000610
611 Cur = Next;
612 }
613
614 LLVMDisposeBuilder(Builder);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000615 return BB;
616 }
617
Amaury Secheta82042e2016-02-09 22:36:41 +0000618 void CloneBBs(LLVMValueRef Src) {
619 unsigned Count = LLVMCountBasicBlocks(Src);
620 if (Count == 0)
621 return;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000622
Amaury Secheta82042e2016-02-09 22:36:41 +0000623 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
624 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
625
626 LLVMBasicBlockRef Cur = First;
627 LLVMBasicBlockRef Next = nullptr;
628 while(true) {
629 CloneBB(Cur);
630 Count--;
631 Next = LLVMGetNextBasicBlock(Cur);
632 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000633 if (Cur != Last)
634 report_fatal_error("Final basic block does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000635 break;
636 }
637
638 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000639 if (Prev != Cur)
640 report_fatal_error("Next.Previous basic bloc is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000641
Amaury Secheta82042e2016-02-09 22:36:41 +0000642 Cur = Next;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000643 }
644
Amaury Sechetd01c8612016-02-14 10:06:34 +0000645 if (Count != 0)
646 report_fatal_error("Basic block count does not match iterration");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000647 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000648};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000649
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000650static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
651 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
652 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000653
654 LLVMValueRef Cur = Begin;
655 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000656 if (!Begin) {
657 if (End != nullptr)
658 report_fatal_error("Range has an end but no begining");
659 goto FunDecl;
660 }
661
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000662 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000663 const char *Name = LLVMGetValueName(Cur);
664 if (LLVMGetNamedGlobal(M, Name))
665 report_fatal_error("GlobalVariable already cloned");
666 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
667
668 Next = LLVMGetNextGlobal(Cur);
669 if (Next == nullptr) {
670 if (Cur != End)
671 report_fatal_error("");
672 break;
673 }
674
675 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
676 if (Prev != Cur)
677 report_fatal_error("Next.Previous global is not Current");
678
679 Cur = Next;
680 }
681
Amaury Sechet58946a92016-02-17 22:30:05 +0000682FunDecl:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000683 Begin = LLVMGetFirstFunction(Src);
684 End = LLVMGetLastFunction(Src);
685 if (!Begin) {
686 if (End != nullptr)
687 report_fatal_error("Range has an end but no begining");
688 return;
689 }
690
691 Cur = Begin;
692 Next = nullptr;
693 while (true) {
694 const char *Name = LLVMGetValueName(Cur);
695 if (LLVMGetNamedFunction(M, Name))
696 report_fatal_error("Function already cloned");
697 LLVMAddFunction(M, Name, LLVMGetElementType(TypeCloner(M).Clone(Cur)));
698
Amaury Sechetf64e8342016-02-16 07:08:49 +0000699 Next = LLVMGetNextFunction(Cur);
700 if (Next == nullptr) {
701 if (Cur != End)
702 report_fatal_error("Last function does not match End");
703 break;
704 }
705
706 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
707 if (Prev != Cur)
708 report_fatal_error("Next.Previous function is not Current");
709
710 Cur = Next;
711 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000712}
Amaury Sechetf64e8342016-02-16 07:08:49 +0000713
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000714static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
715 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
716 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000717
718 LLVMValueRef Cur = Begin;
719 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000720 if (!Begin) {
721 if (End != nullptr)
722 report_fatal_error("Range has an end but no begining");
723 goto FunClone;
724 }
725
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000726 while (true) {
727 const char *Name = LLVMGetValueName(Cur);
728 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
729 if (!G)
730 report_fatal_error("GlobalVariable must have been declared already");
731
732 if (auto I = LLVMGetInitializer(Cur))
733 LLVMSetInitializer(G, clone_constant(I, M));
734
735 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
736 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
737 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
738 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
739 LLVMSetSection(G, LLVMGetSection(Cur));
740 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
741 LLVMSetUnnamedAddr(G, LLVMHasUnnamedAddr(Cur));
742 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
743
744 Next = LLVMGetNextGlobal(Cur);
745 if (Next == nullptr) {
746 if (Cur != End)
747 report_fatal_error("");
748 break;
749 }
750
751 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
752 if (Prev != Cur)
753 report_fatal_error("Next.Previous global is not Current");
754
755 Cur = Next;
756 }
757
Amaury Sechet58946a92016-02-17 22:30:05 +0000758FunClone:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000759 Begin = LLVMGetFirstFunction(Src);
760 End = LLVMGetLastFunction(Src);
761 if (!Begin) {
762 if (End != nullptr)
763 report_fatal_error("Range has an end but no begining");
764 return;
765 }
766
Amaury Sechetf64e8342016-02-16 07:08:49 +0000767 Cur = Begin;
768 Next = nullptr;
769 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000770 const char *Name = LLVMGetValueName(Cur);
771 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
772 if (!Fun)
773 report_fatal_error("Function must have been declared already");
774 FunCloner FC(Cur, Fun);
775 FC.CloneBBs(Cur);
776
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000777 Next = LLVMGetNextFunction(Cur);
778 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000779 if (Cur != End)
780 report_fatal_error("Last function does not match End");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000781 break;
782 }
783
784 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000785 if (Prev != Cur)
786 report_fatal_error("Next.Previous function is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000787
788 Cur = Next;
789 }
790}
791
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000792int llvm_echo(void) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000793 LLVMEnablePrettyStackTrace();
794
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000795 LLVMModuleRef Src = llvm_load_module(false, true);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000796
797 LLVMContextRef Ctx = LLVMContextCreate();
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000798 LLVMModuleRef M = LLVMModuleCreateWithNameInContext("<stdin>", Ctx);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000799
Amaury Sechet55909672016-02-16 05:11:24 +0000800 LLVMSetTarget(M, LLVMGetTarget(Src));
801 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
802 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
803 report_fatal_error("Inconsistent DataLayout string representation");
804
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000805 declare_symbols(Src, M);
806 clone_symbols(Src, M);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000807 char *Str = LLVMPrintModuleToString(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000808 fputs(Str, stdout);
809
810 LLVMDisposeMessage(Str);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000811 LLVMDisposeModule(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000812 LLVMContextDispose(Ctx);
813
814 return 0;
815}