blob: 229530155fcc57daedb1d4ab251e906a82f82c0e [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 Sechet40bbe512016-02-17 23:55:59 +0000279 // Try float literal
280 if (LLVMIsAConstantFP(Cst))
281 report_fatal_error("ConstantFP is not supported");
282
Amaury Sechet6b16c232016-02-16 07:33:23 +0000283 // This kind of constant is not supported
284 if (!LLVMIsAConstantExpr(Cst))
285 report_fatal_error("Expected a constant expression");
286
287 // At this point, it must be a constant expression
Amaury Sechet40bbe512016-02-17 23:55:59 +0000288 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
289 switch(Op) {
290 case LLVMBitCast:
291 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
292 TypeCloner(M).Clone(Cst));
293 default:
294 fprintf(stderr, "%d is not a supported opcode\n", Op);
295 exit(-1);
296 }
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000297}
298
Amaury Secheta82042e2016-02-09 22:36:41 +0000299struct FunCloner {
300 LLVMValueRef Fun;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000301 LLVMModuleRef M;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000302
Amaury Secheta82042e2016-02-09 22:36:41 +0000303 ValueMap VMap;
304 BasicBlockMap BBMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000305
Amaury Sechetaad93532016-02-10 00:38:50 +0000306 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
307 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
308
309 LLVMTypeRef CloneType(LLVMTypeRef Src) {
310 return TypeCloner(M).Clone(Src);
311 }
312
313 LLVMTypeRef CloneType(LLVMValueRef Src) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000314 return TypeCloner(M).Clone(Src);
Amaury Sechetaad93532016-02-10 00:38:50 +0000315 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000316
Amaury Secheta82042e2016-02-09 22:36:41 +0000317 // Try to clone everything in the llvm::Value hierarchy.
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000318 LLVMValueRef CloneValue(LLVMValueRef Src) {
Amaury Secheta82042e2016-02-09 22:36:41 +0000319 // First, the value may be constant.
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000320 if (LLVMIsAConstant(Src))
321 return clone_constant(Src, M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000322
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000323 // Function argument should always be in the map already.
Amaury Sechet2f432082016-02-11 21:37:54 +0000324 auto i = VMap.find(Src);
325 if (i != VMap.end())
326 return i->second;
Amaury Secheta82042e2016-02-09 22:36:41 +0000327
Amaury Sechet2f432082016-02-11 21:37:54 +0000328 if (!LLVMIsAInstruction(Src))
329 report_fatal_error("Expected an instruction");
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000330
Amaury Sechet2f432082016-02-11 21:37:54 +0000331 auto Ctx = LLVMGetModuleContext(M);
332 auto Builder = LLVMCreateBuilderInContext(Ctx);
333 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
334 LLVMPositionBuilderAtEnd(Builder, BB);
335 auto Dst = CloneInstruction(Src, Builder);
336 LLVMDisposeBuilder(Builder);
337 return Dst;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000338 }
339
340 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
341 const char *Name = LLVMGetValueName(Src);
342 if (!LLVMIsAInstruction(Src))
343 report_fatal_error("Expected an instruction");
344
Amaury Secheta82042e2016-02-09 22:36:41 +0000345 // Check if this is something we already computed.
346 {
347 auto i = VMap.find(Src);
Amaury Sechet2f432082016-02-11 21:37:54 +0000348 if (i != VMap.end()) {
349 // If we have a hit, it means we already generated the instruction
350 // as a dependancy to somethign else. We need to make sure
351 // it is ordered properly.
352 auto I = i->second;
353 LLVMInstructionRemoveFromParent(I);
354 LLVMInsertIntoBuilderWithName(Builder, I, Name);
355 return I;
356 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000357 }
358
359 // We tried everything, it must be an instruction
360 // that hasn't been generated already.
361 LLVMValueRef Dst = nullptr;
362
363 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
364 switch(Op) {
365 case LLVMRet: {
366 int OpCount = LLVMGetNumOperands(Src);
367 if (OpCount == 0)
368 Dst = LLVMBuildRetVoid(Builder);
369 else
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000370 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
Amaury Secheta82042e2016-02-09 22:36:41 +0000371 break;
372 }
373 case LLVMBr: {
Amaury Sechete7e62172016-02-09 23:15:02 +0000374 if (!LLVMIsConditional(Src)) {
375 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
376 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
377 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
378 break;
379 }
380
381 LLVMValueRef Cond = LLVMGetCondition(Src);
382 LLVMValueRef Else = LLVMGetOperand(Src, 1);
383 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
384 LLVMValueRef Then = LLVMGetOperand(Src, 2);
385 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
386 Dst = LLVMBuildCondBr(Builder, Cond, ThenBB, ElseBB);
Amaury Secheta82042e2016-02-09 22:36:41 +0000387 break;
388 }
389 case LLVMSwitch:
390 case LLVMIndirectBr:
391 case LLVMInvoke:
392 break;
393 case LLVMUnreachable:
394 Dst = LLVMBuildUnreachable(Builder);
395 break;
396 case LLVMAdd: {
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 = LLVMBuildAdd(Builder, LHS, RHS, Name);
400 break;
401 }
402 case LLVMSub: {
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 = LLVMBuildSub(Builder, LHS, RHS, Name);
406 break;
407 }
408 case LLVMMul: {
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 = LLVMBuildMul(Builder, LHS, RHS, Name);
412 break;
413 }
414 case LLVMUDiv: {
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 = LLVMBuildUDiv(Builder, LHS, RHS, Name);
418 break;
419 }
420 case LLVMSDiv: {
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 = LLVMBuildSDiv(Builder, LHS, RHS, Name);
424 break;
425 }
426 case LLVMURem: {
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 = LLVMBuildURem(Builder, LHS, RHS, Name);
430 break;
431 }
432 case LLVMSRem: {
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 = LLVMBuildSRem(Builder, LHS, RHS, Name);
436 break;
437 }
438 case LLVMShl: {
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 = LLVMBuildShl(Builder, LHS, RHS, Name);
442 break;
443 }
444 case LLVMLShr: {
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 = LLVMBuildLShr(Builder, LHS, RHS, Name);
448 break;
449 }
450 case LLVMAShr: {
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 = LLVMBuildAShr(Builder, LHS, RHS, Name);
454 break;
455 }
456 case LLVMAnd: {
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 = LLVMBuildAnd(Builder, LHS, RHS, Name);
460 break;
461 }
462 case LLVMOr: {
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 = LLVMBuildOr(Builder, LHS, RHS, Name);
466 break;
467 }
468 case LLVMXor: {
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 = LLVMBuildXor(Builder, LHS, RHS, Name);
472 break;
473 }
474 case LLVMAlloca: {
Amaury Sechetaad93532016-02-10 00:38:50 +0000475 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000476 Dst = LLVMBuildAlloca(Builder, Ty, Name);
477 break;
478 }
Amaury Sechet053ac452016-02-17 22:51:03 +0000479 case LLVMLoad: {
480 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
481 Dst = LLVMBuildLoad(Builder, Ptr, Name);
482 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
483 break;
484 }
485 case LLVMStore: {
486 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
487 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
488 Dst = LLVMBuildStore(Builder, Val, Ptr);
489 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
490 break;
491 }
492 case LLVMGetElementPtr: {
493 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
494 SmallVector<LLVMValueRef, 8> Idx;
495 int NumIdx = LLVMGetNumIndices(Src);
496 for (int i = 1; i <= NumIdx; i++)
497 Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
498 if (LLVMIsInBounds(Src))
499 Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
500 else
501 Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
502 break;
503 }
Amaury Sechet40bbe512016-02-17 23:55:59 +0000504 case LLVMBitCast: {
505 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
506 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
507 break;
508 }
Amaury Sechete7e62172016-02-09 23:15:02 +0000509 case LLVMICmp: {
510 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000511 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
512 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Sechete7e62172016-02-09 23:15:02 +0000513 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
514 break;
515 }
Amaury Sechet2f432082016-02-11 21:37:54 +0000516 case LLVMPHI: {
517 // We need to agressively set things here because of loops.
518 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
519
520 SmallVector<LLVMValueRef, 8> Values;
521 SmallVector<LLVMBasicBlockRef, 8> Blocks;
522
523 unsigned IncomingCount = LLVMCountIncoming(Src);
524 for (unsigned i = 0; i < IncomingCount; ++i) {
525 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
526 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
527 }
528
529 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
530 return Dst;
531 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000532 case LLVMCall: {
Amaury Secheta82042e2016-02-09 22:36:41 +0000533 SmallVector<LLVMValueRef, 8> Args;
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000534 int ArgCount = LLVMGetNumArgOperands(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000535 for (int i = 0; i < ArgCount; i++)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000536 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000537 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000538 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
539 break;
540 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000541 case LLVMExtractValue: {
542 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
543 if (LLVMGetNumIndices(Src) != 1)
544 report_fatal_error("Expected only one indice");
545 auto I = LLVMGetIndices(Src)[0];
546 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
547 break;
548 }
549 case LLVMInsertValue: {
550 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
551 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
552 if (LLVMGetNumIndices(Src) != 1)
553 report_fatal_error("Expected only one indice");
554 auto I = LLVMGetIndices(Src)[0];
555 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
556 break;
557 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000558 default:
559 break;
560 }
561
562 if (Dst == nullptr) {
563 fprintf(stderr, "%d is not a supported opcode\n", Op);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000564 exit(-1);
565 }
566
Amaury Secheta82042e2016-02-09 22:36:41 +0000567 return VMap[Src] = Dst;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000568 }
569
Amaury Secheta82042e2016-02-09 22:36:41 +0000570 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
571 // Check if this is something we already computed.
572 {
573 auto i = BBMap.find(Src);
574 if (i != BBMap.end()) {
575 return i->second;
576 }
577 }
578
Amaury Secheta82042e2016-02-09 22:36:41 +0000579 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
580 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000581 report_fatal_error("Basic block is not a basic block");
Amaury Secheta82042e2016-02-09 22:36:41 +0000582
Amaury Sechetd01c8612016-02-14 10:06:34 +0000583 const char *Name = LLVMGetBasicBlockName(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000584 const char *VName = LLVMGetValueName(V);
585 if (Name != VName)
586 report_fatal_error("Basic block name mismatch");
587
588 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
589 return BBMap[Src] = BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000590 }
591
Amaury Secheta82042e2016-02-09 22:36:41 +0000592 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
593 LLVMBasicBlockRef BB = DeclareBB(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000594
Amaury Secheta82042e2016-02-09 22:36:41 +0000595 // Make sure ordering is correct.
596 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
597 if (Prev)
598 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000599
Amaury Secheta82042e2016-02-09 22:36:41 +0000600 LLVMValueRef First = LLVMGetFirstInstruction(Src);
601 LLVMValueRef Last = LLVMGetLastInstruction(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000602
Amaury Secheta82042e2016-02-09 22:36:41 +0000603 if (First == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000604 if (Last != nullptr)
605 report_fatal_error("Has no first instruction, but last one");
Amaury Secheta82042e2016-02-09 22:36:41 +0000606 return BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000607 }
608
Amaury Sechetaad93532016-02-10 00:38:50 +0000609 auto Ctx = LLVMGetModuleContext(M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000610 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
611 LLVMPositionBuilderAtEnd(Builder, BB);
612
613 LLVMValueRef Cur = First;
614 LLVMValueRef Next = nullptr;
615 while(true) {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000616 CloneInstruction(Cur, Builder);
Amaury Secheta82042e2016-02-09 22:36:41 +0000617 Next = LLVMGetNextInstruction(Cur);
618 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000619 if (Cur != Last)
620 report_fatal_error("Final instruction does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000621 break;
622 }
623
624 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000625 if (Prev != Cur)
626 report_fatal_error("Next.Previous instruction is not Current");
Amaury Secheta82042e2016-02-09 22:36:41 +0000627
628 Cur = Next;
629 }
630
631 LLVMDisposeBuilder(Builder);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000632 return BB;
633 }
634
Amaury Secheta82042e2016-02-09 22:36:41 +0000635 void CloneBBs(LLVMValueRef Src) {
636 unsigned Count = LLVMCountBasicBlocks(Src);
637 if (Count == 0)
638 return;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000639
Amaury Secheta82042e2016-02-09 22:36:41 +0000640 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
641 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
642
643 LLVMBasicBlockRef Cur = First;
644 LLVMBasicBlockRef Next = nullptr;
645 while(true) {
646 CloneBB(Cur);
647 Count--;
648 Next = LLVMGetNextBasicBlock(Cur);
649 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000650 if (Cur != Last)
651 report_fatal_error("Final basic block does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000652 break;
653 }
654
655 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000656 if (Prev != Cur)
657 report_fatal_error("Next.Previous basic bloc is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000658
Amaury Secheta82042e2016-02-09 22:36:41 +0000659 Cur = Next;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000660 }
661
Amaury Sechetd01c8612016-02-14 10:06:34 +0000662 if (Count != 0)
663 report_fatal_error("Basic block count does not match iterration");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000664 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000665};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000666
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000667static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
668 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
669 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000670
671 LLVMValueRef Cur = Begin;
672 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000673 if (!Begin) {
674 if (End != nullptr)
675 report_fatal_error("Range has an end but no begining");
676 goto FunDecl;
677 }
678
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000679 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000680 const char *Name = LLVMGetValueName(Cur);
681 if (LLVMGetNamedGlobal(M, Name))
682 report_fatal_error("GlobalVariable already cloned");
683 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
684
685 Next = LLVMGetNextGlobal(Cur);
686 if (Next == nullptr) {
687 if (Cur != End)
688 report_fatal_error("");
689 break;
690 }
691
692 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
693 if (Prev != Cur)
694 report_fatal_error("Next.Previous global is not Current");
695
696 Cur = Next;
697 }
698
Amaury Sechet58946a92016-02-17 22:30:05 +0000699FunDecl:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000700 Begin = LLVMGetFirstFunction(Src);
701 End = LLVMGetLastFunction(Src);
702 if (!Begin) {
703 if (End != nullptr)
704 report_fatal_error("Range has an end but no begining");
705 return;
706 }
707
708 Cur = Begin;
709 Next = nullptr;
710 while (true) {
711 const char *Name = LLVMGetValueName(Cur);
712 if (LLVMGetNamedFunction(M, Name))
713 report_fatal_error("Function already cloned");
714 LLVMAddFunction(M, Name, LLVMGetElementType(TypeCloner(M).Clone(Cur)));
715
Amaury Sechetf64e8342016-02-16 07:08:49 +0000716 Next = LLVMGetNextFunction(Cur);
717 if (Next == nullptr) {
718 if (Cur != End)
719 report_fatal_error("Last function does not match End");
720 break;
721 }
722
723 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
724 if (Prev != Cur)
725 report_fatal_error("Next.Previous function is not Current");
726
727 Cur = Next;
728 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000729}
Amaury Sechetf64e8342016-02-16 07:08:49 +0000730
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000731static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
732 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
733 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000734
735 LLVMValueRef Cur = Begin;
736 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000737 if (!Begin) {
738 if (End != nullptr)
739 report_fatal_error("Range has an end but no begining");
740 goto FunClone;
741 }
742
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000743 while (true) {
744 const char *Name = LLVMGetValueName(Cur);
745 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
746 if (!G)
747 report_fatal_error("GlobalVariable must have been declared already");
748
749 if (auto I = LLVMGetInitializer(Cur))
750 LLVMSetInitializer(G, clone_constant(I, M));
751
752 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
753 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
754 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
755 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
756 LLVMSetSection(G, LLVMGetSection(Cur));
757 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
758 LLVMSetUnnamedAddr(G, LLVMHasUnnamedAddr(Cur));
759 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
760
761 Next = LLVMGetNextGlobal(Cur);
762 if (Next == nullptr) {
763 if (Cur != End)
764 report_fatal_error("");
765 break;
766 }
767
768 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
769 if (Prev != Cur)
770 report_fatal_error("Next.Previous global is not Current");
771
772 Cur = Next;
773 }
774
Amaury Sechet58946a92016-02-17 22:30:05 +0000775FunClone:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000776 Begin = LLVMGetFirstFunction(Src);
777 End = LLVMGetLastFunction(Src);
778 if (!Begin) {
779 if (End != nullptr)
780 report_fatal_error("Range has an end but no begining");
781 return;
782 }
783
Amaury Sechetf64e8342016-02-16 07:08:49 +0000784 Cur = Begin;
785 Next = nullptr;
786 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000787 const char *Name = LLVMGetValueName(Cur);
788 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
789 if (!Fun)
790 report_fatal_error("Function must have been declared already");
791 FunCloner FC(Cur, Fun);
792 FC.CloneBBs(Cur);
793
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000794 Next = LLVMGetNextFunction(Cur);
795 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000796 if (Cur != End)
797 report_fatal_error("Last function does not match End");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000798 break;
799 }
800
801 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000802 if (Prev != Cur)
803 report_fatal_error("Next.Previous function is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000804
805 Cur = Next;
806 }
807}
808
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000809int llvm_echo(void) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000810 LLVMEnablePrettyStackTrace();
811
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000812 LLVMModuleRef Src = llvm_load_module(false, true);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000813
814 LLVMContextRef Ctx = LLVMContextCreate();
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000815 LLVMModuleRef M = LLVMModuleCreateWithNameInContext("<stdin>", Ctx);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000816
Amaury Sechet55909672016-02-16 05:11:24 +0000817 LLVMSetTarget(M, LLVMGetTarget(Src));
818 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
819 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
820 report_fatal_error("Inconsistent DataLayout string representation");
821
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000822 declare_symbols(Src, M);
823 clone_symbols(Src, M);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000824 char *Str = LLVMPrintModuleToString(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000825 fputs(Str, stdout);
826
827 LLVMDisposeMessage(Str);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000828 LLVMDisposeModule(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000829 LLVMContextDispose(Ctx);
830
831 return 0;
832}