blob: a8f9baf1a3904c2e2834019e31da35d423fca5c2 [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
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 }
Amaury Sechet40bbe512016-02-17 23:55:59 +0000525 case LLVMBitCast: {
526 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
527 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
528 break;
529 }
Amaury Sechete7e62172016-02-09 23:15:02 +0000530 case LLVMICmp: {
531 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000532 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
533 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Amaury Sechete7e62172016-02-09 23:15:02 +0000534 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
535 break;
536 }
Amaury Sechet2f432082016-02-11 21:37:54 +0000537 case LLVMPHI: {
538 // We need to agressively set things here because of loops.
539 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
540
541 SmallVector<LLVMValueRef, 8> Values;
542 SmallVector<LLVMBasicBlockRef, 8> Blocks;
543
544 unsigned IncomingCount = LLVMCountIncoming(Src);
545 for (unsigned i = 0; i < IncomingCount; ++i) {
546 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
547 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
548 }
549
550 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
551 return Dst;
552 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000553 case LLVMCall: {
Amaury Secheta82042e2016-02-09 22:36:41 +0000554 SmallVector<LLVMValueRef, 8> Args;
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000555 int ArgCount = LLVMGetNumArgOperands(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000556 for (int i = 0; i < ArgCount; i++)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000557 Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
Amaury Sechet5c7b3af2016-02-10 00:09:37 +0000558 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000559 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
Amaury Sechete39e8532016-02-18 20:38:32 +0000560 LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
561 break;
562 }
563 case LLVMResume: {
564 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
565 break;
566 }
567 case LLVMLandingPad: {
568 // The landing pad API is a bit screwed up for historical reasons.
569 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
570 unsigned NumClauses = LLVMGetNumClauses(Src);
571 for (unsigned i = 0; i < NumClauses; ++i)
572 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
573 LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
Amaury Secheta82042e2016-02-09 22:36:41 +0000574 break;
575 }
Amaury Sechetaad93532016-02-10 00:38:50 +0000576 case LLVMExtractValue: {
577 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
578 if (LLVMGetNumIndices(Src) != 1)
579 report_fatal_error("Expected only one indice");
580 auto I = LLVMGetIndices(Src)[0];
581 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
582 break;
583 }
584 case LLVMInsertValue: {
585 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
586 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
587 if (LLVMGetNumIndices(Src) != 1)
588 report_fatal_error("Expected only one indice");
589 auto I = LLVMGetIndices(Src)[0];
590 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
591 break;
592 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000593 default:
594 break;
595 }
596
597 if (Dst == nullptr) {
598 fprintf(stderr, "%d is not a supported opcode\n", Op);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000599 exit(-1);
600 }
601
Amaury Secheta82042e2016-02-09 22:36:41 +0000602 return VMap[Src] = Dst;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000603 }
604
Amaury Secheta82042e2016-02-09 22:36:41 +0000605 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
606 // Check if this is something we already computed.
607 {
608 auto i = BBMap.find(Src);
609 if (i != BBMap.end()) {
610 return i->second;
611 }
612 }
613
Amaury Secheta82042e2016-02-09 22:36:41 +0000614 LLVMValueRef V = LLVMBasicBlockAsValue(Src);
615 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000616 report_fatal_error("Basic block is not a basic block");
Amaury Secheta82042e2016-02-09 22:36:41 +0000617
Amaury Sechetd01c8612016-02-14 10:06:34 +0000618 const char *Name = LLVMGetBasicBlockName(Src);
Amaury Secheta82042e2016-02-09 22:36:41 +0000619 const char *VName = LLVMGetValueName(V);
620 if (Name != VName)
621 report_fatal_error("Basic block name mismatch");
622
623 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
624 return BBMap[Src] = BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000625 }
626
Amaury Secheta82042e2016-02-09 22:36:41 +0000627 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
628 LLVMBasicBlockRef BB = DeclareBB(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000629
Amaury Secheta82042e2016-02-09 22:36:41 +0000630 // Make sure ordering is correct.
631 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
632 if (Prev)
633 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000634
Amaury Secheta82042e2016-02-09 22:36:41 +0000635 LLVMValueRef First = LLVMGetFirstInstruction(Src);
636 LLVMValueRef Last = LLVMGetLastInstruction(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000637
Amaury Secheta82042e2016-02-09 22:36:41 +0000638 if (First == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000639 if (Last != nullptr)
640 report_fatal_error("Has no first instruction, but last one");
Amaury Secheta82042e2016-02-09 22:36:41 +0000641 return BB;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000642 }
643
Amaury Sechetaad93532016-02-10 00:38:50 +0000644 auto Ctx = LLVMGetModuleContext(M);
Amaury Secheta82042e2016-02-09 22:36:41 +0000645 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
646 LLVMPositionBuilderAtEnd(Builder, BB);
647
648 LLVMValueRef Cur = First;
649 LLVMValueRef Next = nullptr;
650 while(true) {
Amaury Sechet1e5d7e22016-02-09 23:41:20 +0000651 CloneInstruction(Cur, Builder);
Amaury Secheta82042e2016-02-09 22:36:41 +0000652 Next = LLVMGetNextInstruction(Cur);
653 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000654 if (Cur != Last)
655 report_fatal_error("Final instruction does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000656 break;
657 }
658
659 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000660 if (Prev != Cur)
661 report_fatal_error("Next.Previous instruction is not Current");
Amaury Secheta82042e2016-02-09 22:36:41 +0000662
663 Cur = Next;
664 }
665
666 LLVMDisposeBuilder(Builder);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000667 return BB;
668 }
669
Amaury Secheta82042e2016-02-09 22:36:41 +0000670 void CloneBBs(LLVMValueRef Src) {
671 unsigned Count = LLVMCountBasicBlocks(Src);
672 if (Count == 0)
673 return;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000674
Amaury Secheta82042e2016-02-09 22:36:41 +0000675 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
676 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
677
678 LLVMBasicBlockRef Cur = First;
679 LLVMBasicBlockRef Next = nullptr;
680 while(true) {
681 CloneBB(Cur);
682 Count--;
683 Next = LLVMGetNextBasicBlock(Cur);
684 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000685 if (Cur != Last)
686 report_fatal_error("Final basic block does not match Last");
Amaury Secheta82042e2016-02-09 22:36:41 +0000687 break;
688 }
689
690 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000691 if (Prev != Cur)
692 report_fatal_error("Next.Previous basic bloc is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000693
Amaury Secheta82042e2016-02-09 22:36:41 +0000694 Cur = Next;
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000695 }
696
Amaury Sechetd01c8612016-02-14 10:06:34 +0000697 if (Count != 0)
698 report_fatal_error("Basic block count does not match iterration");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000699 }
Amaury Secheta82042e2016-02-09 22:36:41 +0000700};
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000701
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000702static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
703 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
704 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000705
706 LLVMValueRef Cur = Begin;
707 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000708 if (!Begin) {
709 if (End != nullptr)
710 report_fatal_error("Range has an end but no begining");
711 goto FunDecl;
712 }
713
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000714 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000715 const char *Name = LLVMGetValueName(Cur);
716 if (LLVMGetNamedGlobal(M, Name))
717 report_fatal_error("GlobalVariable already cloned");
718 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
719
720 Next = LLVMGetNextGlobal(Cur);
721 if (Next == nullptr) {
722 if (Cur != End)
723 report_fatal_error("");
724 break;
725 }
726
727 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
728 if (Prev != Cur)
729 report_fatal_error("Next.Previous global is not Current");
730
731 Cur = Next;
732 }
733
Amaury Sechet58946a92016-02-17 22:30:05 +0000734FunDecl:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000735 Begin = LLVMGetFirstFunction(Src);
736 End = LLVMGetLastFunction(Src);
737 if (!Begin) {
738 if (End != nullptr)
739 report_fatal_error("Range has an end but no begining");
740 return;
741 }
742
743 Cur = Begin;
744 Next = nullptr;
745 while (true) {
746 const char *Name = LLVMGetValueName(Cur);
747 if (LLVMGetNamedFunction(M, Name))
748 report_fatal_error("Function already cloned");
749 LLVMAddFunction(M, Name, LLVMGetElementType(TypeCloner(M).Clone(Cur)));
750
Amaury Sechetf64e8342016-02-16 07:08:49 +0000751 Next = LLVMGetNextFunction(Cur);
752 if (Next == nullptr) {
753 if (Cur != End)
754 report_fatal_error("Last function does not match End");
755 break;
756 }
757
758 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
759 if (Prev != Cur)
760 report_fatal_error("Next.Previous function is not Current");
761
762 Cur = Next;
763 }
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000764}
Amaury Sechetf64e8342016-02-16 07:08:49 +0000765
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000766static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
767 LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
768 LLVMValueRef End = LLVMGetLastGlobal(Src);
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000769
770 LLVMValueRef Cur = Begin;
771 LLVMValueRef Next = nullptr;
Amaury Sechet58946a92016-02-17 22:30:05 +0000772 if (!Begin) {
773 if (End != nullptr)
774 report_fatal_error("Range has an end but no begining");
775 goto FunClone;
776 }
777
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000778 while (true) {
779 const char *Name = LLVMGetValueName(Cur);
780 LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
781 if (!G)
782 report_fatal_error("GlobalVariable must have been declared already");
783
784 if (auto I = LLVMGetInitializer(Cur))
785 LLVMSetInitializer(G, clone_constant(I, M));
786
787 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
788 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
789 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
790 LLVMSetLinkage(G, LLVMGetLinkage(Cur));
791 LLVMSetSection(G, LLVMGetSection(Cur));
792 LLVMSetVisibility(G, LLVMGetVisibility(Cur));
793 LLVMSetUnnamedAddr(G, LLVMHasUnnamedAddr(Cur));
794 LLVMSetAlignment(G, LLVMGetAlignment(Cur));
795
796 Next = LLVMGetNextGlobal(Cur);
797 if (Next == nullptr) {
798 if (Cur != End)
799 report_fatal_error("");
800 break;
801 }
802
803 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
804 if (Prev != Cur)
805 report_fatal_error("Next.Previous global is not Current");
806
807 Cur = Next;
808 }
809
Amaury Sechet58946a92016-02-17 22:30:05 +0000810FunClone:
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000811 Begin = LLVMGetFirstFunction(Src);
812 End = LLVMGetLastFunction(Src);
813 if (!Begin) {
814 if (End != nullptr)
815 report_fatal_error("Range has an end but no begining");
816 return;
817 }
818
Amaury Sechetf64e8342016-02-16 07:08:49 +0000819 Cur = Begin;
820 Next = nullptr;
821 while (true) {
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000822 const char *Name = LLVMGetValueName(Cur);
823 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
824 if (!Fun)
825 report_fatal_error("Function must have been declared already");
Amaury Sechete39e8532016-02-18 20:38:32 +0000826
827 if (LLVMHasPersonalityFn(Cur)) {
828 const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur));
829 LLVMValueRef P = LLVMGetNamedFunction(M, FName);
830 if (!P)
831 report_fatal_error("Could not find personality function");
832 LLVMSetPersonalityFn(Fun, P);
833 }
834
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000835 FunCloner FC(Cur, Fun);
836 FC.CloneBBs(Cur);
837
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000838 Next = LLVMGetNextFunction(Cur);
839 if (Next == nullptr) {
Amaury Sechetd01c8612016-02-14 10:06:34 +0000840 if (Cur != End)
841 report_fatal_error("Last function does not match End");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000842 break;
843 }
844
845 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
Amaury Sechetd01c8612016-02-14 10:06:34 +0000846 if (Prev != Cur)
847 report_fatal_error("Next.Previous function is not Current");
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000848
849 Cur = Next;
850 }
851}
852
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000853int llvm_echo(void) {
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000854 LLVMEnablePrettyStackTrace();
855
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000856 LLVMModuleRef Src = llvm_load_module(false, true);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000857
858 LLVMContextRef Ctx = LLVMContextCreate();
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000859 LLVMModuleRef M = LLVMModuleCreateWithNameInContext("<stdin>", Ctx);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000860
Amaury Sechet55909672016-02-16 05:11:24 +0000861 LLVMSetTarget(M, LLVMGetTarget(Src));
862 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
863 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
864 report_fatal_error("Inconsistent DataLayout string representation");
865
Amaury Sechete8ba2bf2016-02-17 22:13:33 +0000866 declare_symbols(Src, M);
867 clone_symbols(Src, M);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000868 char *Str = LLVMPrintModuleToString(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000869 fputs(Str, stdout);
870
871 LLVMDisposeMessage(Str);
Amaury Sechetc679dbd2016-02-14 09:30:42 +0000872 LLVMDisposeModule(M);
Amaury Sechete8ea7d82016-02-04 23:26:19 +0000873 LLVMContextDispose(Ctx);
874
875 return 0;
876}