blob: 1922b429eb2eab70dd0d73a4f863407ad2e8b298 [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
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000023#include <stdio.h>
24#include <stdlib.h>
Amaury Sechete8ea7d82016-02-04 23:26:19 +000025
26using namespace llvm;
27
28// Provide DenseMapInfo for C API opaque types.
29template<typename T>
30struct CAPIDenseMap {};
31
32// The default DenseMapInfo require to know about pointer alignement.
33// Because the C API uses opaques pointer types, their alignement is unknown.
34// As a result, we need to roll out our own implementation.
35template<typename T>
36struct CAPIDenseMap<T*> {
37 struct CAPIDenseMapInfo {
38 static inline T* getEmptyKey() {
39 uintptr_t Val = static_cast<uintptr_t>(-1);
40 return reinterpret_cast<T*>(Val);
41 }
42 static inline T* getTombstoneKey() {
43 uintptr_t Val = static_cast<uintptr_t>(-2);
44 return reinterpret_cast<T*>(Val);
45 }
46 static unsigned getHashValue(const T *PtrVal) {
47 return hash_value(PtrVal);
48 }
49 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
50 };
51
52 typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map;
53};
54
55typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap;
Amaury Secheta82042e2016-02-09 22:36:41 +000056typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +000057
Amaury Sechetaad93532016-02-10 00:38:50 +000058struct TypeCloner {
59 LLVMModuleRef M;
60 LLVMContextRef Ctx;
61
62 TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {}
63
Amaury Sechetc679dbd2016-02-14 09:30:42 +000064 LLVMTypeRef Clone(LLVMValueRef Src) {
65 return Clone(LLVMTypeOf(Src));
66 }
67
Amaury Sechetaad93532016-02-10 00:38:50 +000068 LLVMTypeRef Clone(LLVMTypeRef Src) {
69 LLVMTypeKind Kind = LLVMGetTypeKind(Src);
70 switch (Kind) {
71 case LLVMVoidTypeKind:
72 return LLVMVoidTypeInContext(Ctx);
73 case LLVMHalfTypeKind:
74 return LLVMHalfTypeInContext(Ctx);
75 case LLVMFloatTypeKind:
76 return LLVMFloatTypeInContext(Ctx);
77 case LLVMDoubleTypeKind:
78 return LLVMDoubleTypeInContext(Ctx);
79 case LLVMX86_FP80TypeKind:
80 return LLVMX86FP80TypeInContext(Ctx);
81 case LLVMFP128TypeKind:
82 return LLVMFP128TypeInContext(Ctx);
83 case LLVMPPC_FP128TypeKind:
84 return LLVMPPCFP128TypeInContext(Ctx);
85 case LLVMLabelTypeKind:
86 return LLVMLabelTypeInContext(Ctx);
87 case LLVMIntegerTypeKind:
88 return LLVMIntTypeInContext(Ctx, LLVMGetIntTypeWidth(Src));
89 case LLVMFunctionTypeKind: {
90 unsigned ParamCount = LLVMCountParamTypes(Src);
91 LLVMTypeRef* Params = nullptr;
92 if (ParamCount > 0) {
93 Params = (LLVMTypeRef*) malloc(ParamCount * sizeof(LLVMTypeRef));
94 LLVMGetParamTypes(Src, Params);
95 for (unsigned i = 0; i < ParamCount; i++)
96 Params[i] = Clone(Params[i]);
97 }
98
99 LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)),
100 Params, ParamCount,
101 LLVMIsFunctionVarArg(Src));
102 if (ParamCount > 0)
103 free(Params);
104 return FunTy;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000105 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000106 case LLVMStructTypeKind: {
107 LLVMTypeRef S = nullptr;
108 const char *Name = LLVMGetStructName(Src);
109 if (Name) {
110 S = LLVMGetTypeByName(M, Name);
111 if (S)
112 return S;
113 S = LLVMStructCreateNamed(Ctx, Name);
114 if (LLVMIsOpaqueStruct(Src))
115 return S;
116 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000117
Amaury Sechetaad93532016-02-10 00:38:50 +0000118 unsigned EltCount = LLVMCountStructElementTypes(Src);
119 SmallVector<LLVMTypeRef, 8> Elts;
120 for (unsigned i = 0; i < EltCount; i++)
121 Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i)));
122 if (Name)
123 LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src));
124 else
125 S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount,
126 LLVMIsPackedStruct(Src));
127 return S;
128 }
129 case LLVMArrayTypeKind:
130 return LLVMArrayType(
131 Clone(LLVMGetElementType(Src)),
132 LLVMGetArrayLength(Src)
133 );
134 case LLVMPointerTypeKind:
135 return LLVMPointerType(
136 Clone(LLVMGetElementType(Src)),
137 LLVMGetPointerAddressSpace(Src)
138 );
139 case LLVMVectorTypeKind:
140 return LLVMVectorType(
141 Clone(LLVMGetElementType(Src)),
142 LLVMGetVectorSize(Src)
143 );
144 case LLVMMetadataTypeKind:
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
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000157static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000158 unsigned Count = LLVMCountParams(Src);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000159 if (Count != LLVMCountParams(Dst))
160 report_fatal_error("Parameter count mismatch");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000161
162 ValueMap VMap;
163 if (Count == 0)
164 return VMap;
165
166 LLVMValueRef SrcFirst = LLVMGetFirstParam(Src);
167 LLVMValueRef DstFirst = LLVMGetFirstParam(Dst);
168 LLVMValueRef SrcLast = LLVMGetLastParam(Src);
169 LLVMValueRef DstLast = LLVMGetLastParam(Dst);
170
171 LLVMValueRef SrcCur = SrcFirst;
172 LLVMValueRef DstCur = DstFirst;
173 LLVMValueRef SrcNext = nullptr;
174 LLVMValueRef DstNext = nullptr;
175 while (true) {
176 const char *Name = LLVMGetValueName(SrcCur);
177 LLVMSetValueName(DstCur, Name);
178
179 VMap[SrcCur] = DstCur;
180
181 Count--;
182 SrcNext = LLVMGetNextParam(SrcCur);
183 DstNext = LLVMGetNextParam(DstCur);
184 if (SrcNext == nullptr && DstNext == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000185 if (SrcCur != SrcLast)
186 report_fatal_error("SrcLast param does not match End");
187 if (DstCur != DstLast)
188 report_fatal_error("DstLast param does not match End");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000189 break;
190 }
191
Amaury Sechetd01c8612016-02-14 10:06:34 +0000192 if (SrcNext == nullptr)
193 report_fatal_error("SrcNext was unexpectedly null");
194 if (DstNext == nullptr)
195 report_fatal_error("DstNext was unexpectedly null");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000196
197 LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000198 if (SrcPrev != SrcCur)
199 report_fatal_error("SrcNext.Previous param is not Current");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000200
201 LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000202 if (DstPrev != DstCur)
203 report_fatal_error("DstNext.Previous param is not Current");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000204
205 SrcCur = SrcNext;
206 DstCur = DstNext;
207 }
208
Amaury Sechetd01c8612016-02-14 10:06:34 +0000209 if (Count != 0)
210 report_fatal_error("Parameter count does not match iteration");
Amaury Sechetecda4ea2016-02-14 09:14:30 +0000211
212 return VMap;
213}
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000214
Amaury 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
Amaury Sechetb3256862016-03-13 00:58:25 +0000262 // Try contant data array
263 if (LLVMIsAConstantDataArray(Cst)) {
264 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
265 unsigned EltCount = LLVMGetArrayLength(Ty);
266 SmallVector<LLVMValueRef, 8> Elts;
267 for (unsigned i = 0; i < EltCount; i++)
268 Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
269 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
270 }
271
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000272 // Try constant struct
273 if (LLVMIsAConstantStruct(Cst)) {
274 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
275 unsigned EltCount = LLVMCountStructElementTypes(Ty);
276 SmallVector<LLVMValueRef, 8> Elts;
277 for (unsigned i = 0; i < EltCount; i++)
278 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
279 if (LLVMGetStructName(Ty))
280 return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
281 return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
282 EltCount, LLVMIsPackedStruct(Ty));
283 }
284
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000285 // Try undef
286 if (LLVMIsUndef(Cst))
287 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
288
Amaury Sechet40bbe512016-02-17 23:55:59 +0000289 // Try float literal
290 if (LLVMIsAConstantFP(Cst))
291 report_fatal_error("ConstantFP is not supported");
292
Amaury Sechet6b16c232016-02-16 07:33:23 +0000293 // This kind of constant is not supported
294 if (!LLVMIsAConstantExpr(Cst))
295 report_fatal_error("Expected a constant expression");
296
297 // At this point, it must be a constant expression
Amaury Sechet40bbe512016-02-17 23:55:59 +0000298 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
299 switch(Op) {
300 case LLVMBitCast:
301 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
302 TypeCloner(M).Clone(Cst));
303 default:
304 fprintf(stderr, "%d is not a supported opcode\n", Op);
305 exit(-1);
306 }
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000307}
308
Amaury Secheta82042e2016-02-09 22:36:41 +0000309struct FunCloner {
310 LLVMValueRef Fun;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000311 LLVMModuleRef M;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000312
Amaury Secheta82042e2016-02-09 22:36:41 +0000313 ValueMap VMap;
314 BasicBlockMap BBMap;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000315
Amaury Sechetaad93532016-02-10 00:38:50 +0000316 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
317 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
318
319 LLVMTypeRef CloneType(LLVMTypeRef Src) {
320 return TypeCloner(M).Clone(Src);
321 }
322
323 LLVMTypeRef CloneType(LLVMValueRef Src) {
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000324 return TypeCloner(M).Clone(Src);
Amaury Sechetaad93532016-02-10 00:38:50 +0000325 }
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000326
Amaury Secheta82042e2016-02-09 22:36:41 +0000327 // Try to clone everything in the llvm::Value hierarchy.
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000328 LLVMValueRef CloneValue(LLVMValueRef Src) {
Amaury Secheta82042e2016-02-09 22:36:41 +0000329 // First, the value may be constant.
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000330 if (LLVMIsAConstant(Src))
331 return clone_constant(Src, M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000332
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000333 // Function argument should always be in the map already.
Amaury Sechet2f432082016-02-11 21:37:54 +0000334 auto i = VMap.find(Src);
335 if (i != VMap.end())
336 return i->second;
Amaury Secheta82042e2016-02-09 22:36:41 +0000337
Amaury Sechet2f432082016-02-11 21:37:54 +0000338 if (!LLVMIsAInstruction(Src))
339 report_fatal_error("Expected an instruction");
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000340
Amaury Sechet2f432082016-02-11 21:37:54 +0000341 auto Ctx = LLVMGetModuleContext(M);
342 auto Builder = LLVMCreateBuilderInContext(Ctx);
343 auto BB = DeclareBB(LLVMGetInstructionParent(Src));
344 LLVMPositionBuilderAtEnd(Builder, BB);
345 auto Dst = CloneInstruction(Src, Builder);
346 LLVMDisposeBuilder(Builder);
347 return Dst;
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000348 }
349
350 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
351 const char *Name = LLVMGetValueName(Src);
352 if (!LLVMIsAInstruction(Src))
353 report_fatal_error("Expected an instruction");
354
Amaury Secheta82042e2016-02-09 22:36:41 +0000355 // Check if this is something we already computed.
356 {
357 auto i = VMap.find(Src);
Amaury Sechet2f432082016-02-11 21:37:54 +0000358 if (i != VMap.end()) {
359 // If we have a hit, it means we already generated the instruction
360 // as a dependancy to somethign else. We need to make sure
361 // it is ordered properly.
362 auto I = i->second;
363 LLVMInstructionRemoveFromParent(I);
364 LLVMInsertIntoBuilderWithName(Builder, I, Name);
365 return I;
366 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000367 }
368
369 // We tried everything, it must be an instruction
370 // that hasn't been generated already.
371 LLVMValueRef Dst = nullptr;
372
373 LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
374 switch(Op) {
375 case LLVMRet: {
376 int OpCount = LLVMGetNumOperands(Src);
377 if (OpCount == 0)
378 Dst = LLVMBuildRetVoid(Builder);
379 else
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000380 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
Amaury Secheta82042e2016-02-09 22:36:41 +0000381 break;
382 }
383 case LLVMBr: {
Amaury Sechete7e62172016-02-09 23:15:02 +0000384 if (!LLVMIsConditional(Src)) {
385 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
386 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
387 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
388 break;
389 }
390
391 LLVMValueRef Cond = LLVMGetCondition(Src);
392 LLVMValueRef Else = LLVMGetOperand(Src, 1);
393 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
394 LLVMValueRef Then = LLVMGetOperand(Src, 2);
395 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
396 Dst = LLVMBuildCondBr(Builder, Cond, ThenBB, ElseBB);
Amaury Secheta82042e2016-02-09 22:36:41 +0000397 break;
398 }
399 case LLVMSwitch:
400 case LLVMIndirectBr:
Amaury Secheta82042e2016-02-09 22:36:41 +0000401 break;
Amaury Sechete39e8532016-02-18 20:38:32 +0000402 case LLVMInvoke: {
403 SmallVector<LLVMValueRef, 8> Args;
404 int ArgCount = LLVMGetNumArgOperands(Src);
405 for (int i = 0; i < ArgCount; i++)
406 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
407 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
408 LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
409 LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
410 Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
411 Then, Unwind, Name);
412 break;
413 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000414 case LLVMUnreachable:
415 Dst = LLVMBuildUnreachable(Builder);
416 break;
417 case LLVMAdd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000418 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
419 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000420 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
421 break;
422 }
423 case LLVMSub: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000424 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
425 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000426 Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
427 break;
428 }
429 case LLVMMul: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000430 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
431 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000432 Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
433 break;
434 }
435 case LLVMUDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000436 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
437 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000438 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
439 break;
440 }
441 case LLVMSDiv: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000442 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
443 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000444 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
445 break;
446 }
447 case LLVMURem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000448 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
449 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000450 Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
451 break;
452 }
453 case LLVMSRem: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000454 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
455 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000456 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
457 break;
458 }
459 case LLVMShl: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000460 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
461 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000462 Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
463 break;
464 }
465 case LLVMLShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000466 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
467 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000468 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
469 break;
470 }
471 case LLVMAShr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000472 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
473 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000474 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
475 break;
476 }
477 case LLVMAnd: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000478 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
479 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000480 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
481 break;
482 }
483 case LLVMOr: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000484 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
485 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000486 Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
487 break;
488 }
489 case LLVMXor: {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000490 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
491 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Secheta82042e2016-02-09 22:36:41 +0000492 Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
493 break;
494 }
495 case LLVMAlloca: {
Amaury Sechetaad93532016-02-10 00:38:50 +0000496 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000497 Dst = LLVMBuildAlloca(Builder, Ty, Name);
498 break;
499 }
Amaury Sechet053ac452016-02-17 22:51:03 +0000500 case LLVMLoad: {
501 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
502 Dst = LLVMBuildLoad(Builder, Ptr, Name);
503 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
504 break;
505 }
506 case LLVMStore: {
507 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
508 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
509 Dst = LLVMBuildStore(Builder, Val, Ptr);
510 LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
511 break;
512 }
513 case LLVMGetElementPtr: {
514 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
515 SmallVector<LLVMValueRef, 8> Idx;
516 int NumIdx = LLVMGetNumIndices(Src);
517 for (int i = 1; i <= NumIdx; i++)
518 Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
519 if (LLVMIsInBounds(Src))
520 Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
521 else
522 Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
523 break;
524 }
Mehdi Amini43165d92016-03-19 21:28:28 +0000525 case LLVMAtomicCmpXchg: {
526 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
527 LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
528 LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
529 LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
530 LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
531 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
532
533 Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
534 SingleThread);
535 } break;
Amaury Sechet40bbe512016-02-17 23:55:59 +0000536 case LLVMBitCast: {
537 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
538 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
539 break;
540 }
Amaury Sechete7e62172016-02-09 23:15:02 +0000541 case LLVMICmp: {
542 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000543 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
544 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Sechete7e62172016-02-09 23:15:02 +0000545 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
546 break;
547 }
Amaury Sechet2f432082016-02-11 21:37:54 +0000548 case LLVMPHI: {
549 // We need to agressively set things here because of loops.
550 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
551
552 SmallVector<LLVMValueRef, 8> Values;
553 SmallVector<LLVMBasicBlockRef, 8> Blocks;
554
555 unsigned IncomingCount = LLVMCountIncoming(Src);
556 for (unsigned i = 0; i < IncomingCount; ++i) {
557 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
558 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
559 }
560
561 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
562 return Dst;
563 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000564 case LLVMCall: {
Amaury Secheta82042e2016-02-09 22:36:41 +0000565 SmallVector<LLVMValueRef, 8> Args;
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000566 int ArgCount = LLVMGetNumArgOperands(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000567 for (int i = 0; i < ArgCount; i++)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000568 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000569 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000570 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
Amaury Sechete39e8532016-02-18 20:38:32 +0000571 LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
572 break;
573 }
574 case LLVMResume: {
575 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
576 break;
577 }
578 case LLVMLandingPad: {
579 // The landing pad API is a bit screwed up for historical reasons.
580 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
581 unsigned NumClauses = LLVMGetNumClauses(Src);
582 for (unsigned i = 0; i < NumClauses; ++i)
583 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
584 LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000585 break;
586 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000587 case LLVMExtractValue: {
588 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
589 if (LLVMGetNumIndices(Src) != 1)
590 report_fatal_error("Expected only one indice");
591 auto I = LLVMGetIndices(Src)[0];
592 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
593 break;
594 }
595 case LLVMInsertValue: {
596 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
597 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
598 if (LLVMGetNumIndices(Src) != 1)
599 report_fatal_error("Expected only one indice");
600 auto I = LLVMGetIndices(Src)[0];
601 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
602 break;
603 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000604 default:
605 break;
606 }
607
608 if (Dst == nullptr) {
609 fprintf(stderr, "%d is not a supported opcode\n", Op);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000610 exit(-1);
611 }
612
Amaury Secheta82042e2016-02-09 22:36:41 +0000613 return VMap[Src] = Dst;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000614 }
615
Amaury Secheta82042e2016-02-09 22:36:41 +0000616 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
617 // Check if this is something we already computed.
618 {
619 auto i = BBMap.find(Src);
620 if (i != BBMap.end()) {
621 return i->second;
622 }
623 }
624
Amaury Secheta82042e2016-02-09 22:36:41 +0000625 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
626 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000627 report_fatal_error("Basic block is not a basic block");
Amaury Secheta82042e2016-02-09 22:36:41 +0000628
Amaury Sechetd01c8612016-02-14 10:06:34 +0000629 const char *Name = LLVMGetBasicBlockName(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000630 const char *VName = LLVMGetValueName(V);
631 if (Name != VName)
632 report_fatal_error("Basic block name mismatch");
633
634 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
635 return BBMap[Src] = BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000636 }
637
Amaury Secheta82042e2016-02-09 22:36:41 +0000638 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
639 LLVMBasicBlockRef BB = DeclareBB(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000640
Amaury Secheta82042e2016-02-09 22:36:41 +0000641 // Make sure ordering is correct.
642 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
643 if (Prev)
644 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000645
Amaury Secheta82042e2016-02-09 22:36:41 +0000646 LLVMValueRef First = LLVMGetFirstInstruction(Src);
647 LLVMValueRef Last = LLVMGetLastInstruction(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000648
Amaury Secheta82042e2016-02-09 22:36:41 +0000649 if (First == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000650 if (Last != nullptr)
651 report_fatal_error("Has no first instruction, but last one");
Amaury Secheta82042e2016-02-09 22:36:41 +0000652 return BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000653 }
654
Amaury Sechetaad93532016-02-10 00:38:50 +0000655 auto Ctx = LLVMGetModuleContext(M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000656 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
657 LLVMPositionBuilderAtEnd(Builder, BB);
658
659 LLVMValueRef Cur = First;
660 LLVMValueRef Next = nullptr;
661 while(true) {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000662 CloneInstruction(Cur, Builder);
Amaury Secheta82042e2016-02-09 22:36:41 +0000663 Next = LLVMGetNextInstruction(Cur);
664 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000665 if (Cur != Last)
666 report_fatal_error("Final instruction does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000667 break;
668 }
669
670 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000671 if (Prev != Cur)
672 report_fatal_error("Next.Previous instruction is not Current");
Amaury Secheta82042e2016-02-09 22:36:41 +0000673
674 Cur = Next;
675 }
676
677 LLVMDisposeBuilder(Builder);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000678 return BB;
679 }
680
Amaury Secheta82042e2016-02-09 22:36:41 +0000681 void CloneBBs(LLVMValueRef Src) {
682 unsigned Count = LLVMCountBasicBlocks(Src);
683 if (Count == 0)
684 return;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000685
Amaury Secheta82042e2016-02-09 22:36:41 +0000686 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
687 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
688
689 LLVMBasicBlockRef Cur = First;
690 LLVMBasicBlockRef Next = nullptr;
691 while(true) {
692 CloneBB(Cur);
693 Count--;
694 Next = LLVMGetNextBasicBlock(Cur);
695 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000696 if (Cur != Last)
697 report_fatal_error("Final basic block does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000698 break;
699 }
700
701 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000702 if (Prev != Cur)
703 report_fatal_error("Next.Previous basic bloc is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000704
Amaury Secheta82042e2016-02-09 22:36:41 +0000705 Cur = Next;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000706 }
707
Amaury Sechetd01c8612016-02-14 10:06:34 +0000708 if (Count != 0)
709 report_fatal_error("Basic block count does not match iterration");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000710 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000711};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000712
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000713static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000714 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
715 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000716
717 LLVMValueRef Cur = Begin;
718 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000719 if (!Begin) {
720 if (End != nullptr)
721 report_fatal_error("Range has an end but no begining");
722 goto FunDecl;
723 }
724
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000725 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000726 const char *Name = LLVMGetValueName(Cur);
727 if (LLVMGetNamedGlobal(M, Name))
728 report_fatal_error("GlobalVariable already cloned");
729 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
730
731 Next = LLVMGetNextGlobal(Cur);
732 if (Next == nullptr) {
733 if (Cur != End)
734 report_fatal_error("");
735 break;
736 }
737
738 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
739 if (Prev != Cur)
740 report_fatal_error("Next.Previous global is not Current");
741
742 Cur = Next;
743 }
744
Amaury Sechet58946a92016-02-17 22:30:05 +0000745FunDecl:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000746 Begin = LLVMGetFirstFunction(Src);
747 End = LLVMGetLastFunction(Src);
748 if (!Begin) {
749 if (End != nullptr)
750 report_fatal_error("Range has an end but no begining");
751 return;
752 }
753
754 Cur = Begin;
755 Next = nullptr;
756 while (true) {
757 const char *Name = LLVMGetValueName(Cur);
758 if (LLVMGetNamedFunction(M, Name))
759 report_fatal_error("Function already cloned");
760 LLVMAddFunction(M, Name, LLVMGetElementType(TypeCloner(M).Clone(Cur)));
761
Amaury Sechetf64e8342016-02-16 07:08:49 +0000762 Next = LLVMGetNextFunction(Cur);
763 if (Next == nullptr) {
764 if (Cur != End)
765 report_fatal_error("Last function does not match End");
766 break;
767 }
768
769 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
770 if (Prev != Cur)
771 report_fatal_error("Next.Previous function is not Current");
772
773 Cur = Next;
774 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000775}
Amaury Sechetf64e8342016-02-16 07:08:49 +0000776
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000777static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000778 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
779 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000780
781 LLVMValueRef Cur = Begin;
782 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000783 if (!Begin) {
784 if (End != nullptr)
785 report_fatal_error("Range has an end but no begining");
786 goto FunClone;
787 }
788
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000789 while (true) {
790 const char *Name = LLVMGetValueName(Cur);
791 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
792 if (!G)
793 report_fatal_error("GlobalVariable must have been declared already");
794
795 if (auto I = LLVMGetInitializer(Cur))
796 LLVMSetInitializer(G, clone_constant(I, M));
797
798 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
799 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
800 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
801 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
802 LLVMSetSection(G, LLVMGetSection(Cur));
803 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
804 LLVMSetUnnamedAddr(G, LLVMHasUnnamedAddr(Cur));
805 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
806
807 Next = LLVMGetNextGlobal(Cur);
808 if (Next == nullptr) {
809 if (Cur != End)
810 report_fatal_error("");
811 break;
812 }
813
814 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
815 if (Prev != Cur)
816 report_fatal_error("Next.Previous global is not Current");
817
818 Cur = Next;
819 }
820
Amaury Sechet58946a92016-02-17 22:30:05 +0000821FunClone:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000822 Begin = LLVMGetFirstFunction(Src);
823 End = LLVMGetLastFunction(Src);
824 if (!Begin) {
825 if (End != nullptr)
826 report_fatal_error("Range has an end but no begining");
827 return;
828 }
829
Amaury Sechetf64e8342016-02-16 07:08:49 +0000830 Cur = Begin;
831 Next = nullptr;
832 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000833 const char *Name = LLVMGetValueName(Cur);
834 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
835 if (!Fun)
836 report_fatal_error("Function must have been declared already");
Amaury Sechete39e8532016-02-18 20:38:32 +0000837
838 if (LLVMHasPersonalityFn(Cur)) {
839 const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur));
840 LLVMValueRef P = LLVMGetNamedFunction(M, FName);
841 if (!P)
842 report_fatal_error("Could not find personality function");
843 LLVMSetPersonalityFn(Fun, P);
844 }
845
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000846 FunCloner FC(Cur, Fun);
847 FC.CloneBBs(Cur);
848
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000849 Next = LLVMGetNextFunction(Cur);
850 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000851 if (Cur != End)
852 report_fatal_error("Last function does not match End");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000853 break;
854 }
855
856 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000857 if (Prev != Cur)
858 report_fatal_error("Next.Previous function is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000859
860 Cur = Next;
861 }
862}
863
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000864int llvm_echo(void) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000865 LLVMEnablePrettyStackTrace();
866
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000867 LLVMModuleRef Src = llvm_load_module(false, true);
Peter Zotov0a2fa0a2016-04-05 13:56:59 +0000868 size_t Len;
869 const char *ModuleName = LLVMGetModuleIdentifier(Src, &Len);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000870 LLVMContextRef Ctx = LLVMContextCreate();
Peter Zotov0a2fa0a2016-04-05 13:56:59 +0000871 LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
872
873 // This whole switcharound is done because the C API has no way to
874 // set the source_filename
875 LLVMSetModuleIdentifier(M, "", 0);
876 LLVMGetModuleIdentifier(M, &Len);
877 if (Len != 0)
878 report_fatal_error("LLVM{Set,Get}ModuleIdentifier failed");
879 LLVMSetModuleIdentifier(M, ModuleName, strlen(ModuleName));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000880
Amaury Sechet55909672016-02-16 05:11:24 +0000881 LLVMSetTarget(M, LLVMGetTarget(Src));
882 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
883 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
884 report_fatal_error("Inconsistent DataLayout string representation");
885
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000886 declare_symbols(Src, M);
887 clone_symbols(Src, M);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000888 char *Str = LLVMPrintModuleToString(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000889 fputs(Str, stdout);
890
891 LLVMDisposeMessage(Str);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000892 LLVMDisposeModule(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000893 LLVMContextDispose(Ctx);
894
895 return 0;
896}