blob: 1aa6a762dc4e8225e65125b826c64935b7c3c5c9 [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 Sechete7e62172016-02-09 23:15:02 +0000467 case LLVMICmp: {
468 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000469 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
470 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Sechete7e62172016-02-09 23:15:02 +0000471 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
472 break;
473 }
Amaury Sechet2f432082016-02-11 21:37:54 +0000474 case LLVMPHI: {
475 // We need to agressively set things here because of loops.
476 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
477
478 SmallVector<LLVMValueRef, 8> Values;
479 SmallVector<LLVMBasicBlockRef, 8> Blocks;
480
481 unsigned IncomingCount = LLVMCountIncoming(Src);
482 for (unsigned i = 0; i < IncomingCount; ++i) {
483 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
484 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
485 }
486
487 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
488 return Dst;
489 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000490 case LLVMCall: {
Amaury Secheta82042e2016-02-09 22:36:41 +0000491 SmallVector<LLVMValueRef, 8> Args;
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000492 int ArgCount = LLVMGetNumArgOperands(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000493 for (int i = 0; i < ArgCount; i++)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000494 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000495 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000496 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
497 break;
498 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000499 case LLVMExtractValue: {
500 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
501 if (LLVMGetNumIndices(Src) != 1)
502 report_fatal_error("Expected only one indice");
503 auto I = LLVMGetIndices(Src)[0];
504 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
505 break;
506 }
507 case LLVMInsertValue: {
508 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
509 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
510 if (LLVMGetNumIndices(Src) != 1)
511 report_fatal_error("Expected only one indice");
512 auto I = LLVMGetIndices(Src)[0];
513 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
514 break;
515 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000516 default:
517 break;
518 }
519
520 if (Dst == nullptr) {
521 fprintf(stderr, "%d is not a supported opcode\n", Op);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000522 exit(-1);
523 }
524
Amaury Secheta82042e2016-02-09 22:36:41 +0000525 return VMap[Src] = Dst;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000526 }
527
Amaury Secheta82042e2016-02-09 22:36:41 +0000528 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
529 // Check if this is something we already computed.
530 {
531 auto i = BBMap.find(Src);
532 if (i != BBMap.end()) {
533 return i->second;
534 }
535 }
536
Amaury Secheta82042e2016-02-09 22:36:41 +0000537 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
538 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000539 report_fatal_error("Basic block is not a basic block");
Amaury Secheta82042e2016-02-09 22:36:41 +0000540
Amaury Sechetd01c8612016-02-14 10:06:34 +0000541 const char *Name = LLVMGetBasicBlockName(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000542 const char *VName = LLVMGetValueName(V);
543 if (Name != VName)
544 report_fatal_error("Basic block name mismatch");
545
546 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
547 return BBMap[Src] = BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000548 }
549
Amaury Secheta82042e2016-02-09 22:36:41 +0000550 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
551 LLVMBasicBlockRef BB = DeclareBB(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000552
Amaury Secheta82042e2016-02-09 22:36:41 +0000553 // Make sure ordering is correct.
554 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
555 if (Prev)
556 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000557
Amaury Secheta82042e2016-02-09 22:36:41 +0000558 LLVMValueRef First = LLVMGetFirstInstruction(Src);
559 LLVMValueRef Last = LLVMGetLastInstruction(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000560
Amaury Secheta82042e2016-02-09 22:36:41 +0000561 if (First == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000562 if (Last != nullptr)
563 report_fatal_error("Has no first instruction, but last one");
Amaury Secheta82042e2016-02-09 22:36:41 +0000564 return BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000565 }
566
Amaury Sechetaad93532016-02-10 00:38:50 +0000567 auto Ctx = LLVMGetModuleContext(M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000568 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
569 LLVMPositionBuilderAtEnd(Builder, BB);
570
571 LLVMValueRef Cur = First;
572 LLVMValueRef Next = nullptr;
573 while(true) {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000574 CloneInstruction(Cur, Builder);
Amaury Secheta82042e2016-02-09 22:36:41 +0000575 Next = LLVMGetNextInstruction(Cur);
576 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000577 if (Cur != Last)
578 report_fatal_error("Final instruction does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000579 break;
580 }
581
582 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000583 if (Prev != Cur)
584 report_fatal_error("Next.Previous instruction is not Current");
Amaury Secheta82042e2016-02-09 22:36:41 +0000585
586 Cur = Next;
587 }
588
589 LLVMDisposeBuilder(Builder);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000590 return BB;
591 }
592
Amaury Secheta82042e2016-02-09 22:36:41 +0000593 void CloneBBs(LLVMValueRef Src) {
594 unsigned Count = LLVMCountBasicBlocks(Src);
595 if (Count == 0)
596 return;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000597
Amaury Secheta82042e2016-02-09 22:36:41 +0000598 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
599 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
600
601 LLVMBasicBlockRef Cur = First;
602 LLVMBasicBlockRef Next = nullptr;
603 while(true) {
604 CloneBB(Cur);
605 Count--;
606 Next = LLVMGetNextBasicBlock(Cur);
607 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000608 if (Cur != Last)
609 report_fatal_error("Final basic block does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000610 break;
611 }
612
613 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000614 if (Prev != Cur)
615 report_fatal_error("Next.Previous basic bloc is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000616
Amaury Secheta82042e2016-02-09 22:36:41 +0000617 Cur = Next;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000618 }
619
Amaury Sechetd01c8612016-02-14 10:06:34 +0000620 if (Count != 0)
621 report_fatal_error("Basic block count does not match iterration");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000622 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000623};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000624
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000625static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
626 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
627 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000628
629 LLVMValueRef Cur = Begin;
630 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000631 if (!Begin) {
632 if (End != nullptr)
633 report_fatal_error("Range has an end but no begining");
634 goto FunDecl;
635 }
636
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000637 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000638 const char *Name = LLVMGetValueName(Cur);
639 if (LLVMGetNamedGlobal(M, Name))
640 report_fatal_error("GlobalVariable already cloned");
641 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
642
643 Next = LLVMGetNextGlobal(Cur);
644 if (Next == nullptr) {
645 if (Cur != End)
646 report_fatal_error("");
647 break;
648 }
649
650 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
651 if (Prev != Cur)
652 report_fatal_error("Next.Previous global is not Current");
653
654 Cur = Next;
655 }
656
Amaury Sechet58946a92016-02-17 22:30:05 +0000657FunDecl:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000658 Begin = LLVMGetFirstFunction(Src);
659 End = LLVMGetLastFunction(Src);
660 if (!Begin) {
661 if (End != nullptr)
662 report_fatal_error("Range has an end but no begining");
663 return;
664 }
665
666 Cur = Begin;
667 Next = nullptr;
668 while (true) {
669 const char *Name = LLVMGetValueName(Cur);
670 if (LLVMGetNamedFunction(M, Name))
671 report_fatal_error("Function already cloned");
672 LLVMAddFunction(M, Name, LLVMGetElementType(TypeCloner(M).Clone(Cur)));
673
Amaury Sechetf64e8342016-02-16 07:08:49 +0000674 Next = LLVMGetNextFunction(Cur);
675 if (Next == nullptr) {
676 if (Cur != End)
677 report_fatal_error("Last function does not match End");
678 break;
679 }
680
681 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
682 if (Prev != Cur)
683 report_fatal_error("Next.Previous function is not Current");
684
685 Cur = Next;
686 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000687}
Amaury Sechetf64e8342016-02-16 07:08:49 +0000688
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000689static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
690 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
691 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000692
693 LLVMValueRef Cur = Begin;
694 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000695 if (!Begin) {
696 if (End != nullptr)
697 report_fatal_error("Range has an end but no begining");
698 goto FunClone;
699 }
700
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000701 while (true) {
702 const char *Name = LLVMGetValueName(Cur);
703 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
704 if (!G)
705 report_fatal_error("GlobalVariable must have been declared already");
706
707 if (auto I = LLVMGetInitializer(Cur))
708 LLVMSetInitializer(G, clone_constant(I, M));
709
710 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
711 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
712 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
713 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
714 LLVMSetSection(G, LLVMGetSection(Cur));
715 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
716 LLVMSetUnnamedAddr(G, LLVMHasUnnamedAddr(Cur));
717 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
718
719 Next = LLVMGetNextGlobal(Cur);
720 if (Next == nullptr) {
721 if (Cur != End)
722 report_fatal_error("");
723 break;
724 }
725
726 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
727 if (Prev != Cur)
728 report_fatal_error("Next.Previous global is not Current");
729
730 Cur = Next;
731 }
732
Amaury Sechet58946a92016-02-17 22:30:05 +0000733FunClone:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000734 Begin = LLVMGetFirstFunction(Src);
735 End = LLVMGetLastFunction(Src);
736 if (!Begin) {
737 if (End != nullptr)
738 report_fatal_error("Range has an end but no begining");
739 return;
740 }
741
Amaury Sechetf64e8342016-02-16 07:08:49 +0000742 Cur = Begin;
743 Next = nullptr;
744 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000745 const char *Name = LLVMGetValueName(Cur);
746 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
747 if (!Fun)
748 report_fatal_error("Function must have been declared already");
749 FunCloner FC(Cur, Fun);
750 FC.CloneBBs(Cur);
751
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000752 Next = LLVMGetNextFunction(Cur);
753 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000754 if (Cur != End)
755 report_fatal_error("Last function does not match End");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000756 break;
757 }
758
759 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000760 if (Prev != Cur)
761 report_fatal_error("Next.Previous function is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000762
763 Cur = Next;
764 }
765}
766
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000767int llvm_echo(void) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000768 LLVMEnablePrettyStackTrace();
769
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000770 LLVMModuleRef Src = llvm_load_module(false, true);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000771
772 LLVMContextRef Ctx = LLVMContextCreate();
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000773 LLVMModuleRef M = LLVMModuleCreateWithNameInContext("<stdin>", Ctx);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000774
Amaury Sechet55909672016-02-16 05:11:24 +0000775 LLVMSetTarget(M, LLVMGetTarget(Src));
776 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
777 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
778 report_fatal_error("Inconsistent DataLayout string representation");
779
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000780 declare_symbols(Src, M);
781 clone_symbols(Src, M);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000782 char *Str = LLVMPrintModuleToString(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000783 fputs(Str, stdout);
784
785 LLVMDisposeMessage(Str);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000786 LLVMDisposeModule(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000787 LLVMContextDispose(Ctx);
788
789 return 0;
790}