1 //===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the --echo command in llvm-c-test.
11 // This command uses the C API to read a module and output an exact copy of it
12 // as output. It is used to check that the resulting module matches the input
13 // to validate that the C API can read and write modules properly.
15 //===----------------------------------------------------------------------===//
17 #include "llvm-c-test.h"
18 #include "llvm-c/DebugInfo.h"
19 #include "llvm-c/Target.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/Support/ErrorHandling.h"
28 // Provide DenseMapInfo for C API opaque types.
30 struct CAPIDenseMap
{};
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.
36 struct 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
);
42 static inline T
* getTombstoneKey() {
43 uintptr_t Val
= static_cast<uintptr_t>(-2);
44 return reinterpret_cast<T
*>(Val
);
46 static unsigned getHashValue(const T
*PtrVal
) {
47 return hash_value(PtrVal
);
49 static bool isEqual(const T
*LHS
, const T
*RHS
) { return LHS
== RHS
; }
52 typedef DenseMap
<T
*, T
*, CAPIDenseMapInfo
> Map
;
55 typedef CAPIDenseMap
<LLVMValueRef
>::Map ValueMap
;
56 typedef CAPIDenseMap
<LLVMBasicBlockRef
>::Map BasicBlockMap
;
62 TypeCloner(LLVMModuleRef M
): M(M
), Ctx(LLVMGetModuleContext(M
)) {}
64 LLVMTypeRef
Clone(LLVMValueRef Src
) {
65 return Clone(LLVMTypeOf(Src
));
68 LLVMTypeRef
Clone(LLVMTypeRef Src
) {
69 LLVMTypeKind Kind
= LLVMGetTypeKind(Src
);
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;
93 Params
= static_cast<LLVMTypeRef
*>(
94 safe_malloc(ParamCount
* sizeof(LLVMTypeRef
)));
95 LLVMGetParamTypes(Src
, Params
);
96 for (unsigned i
= 0; i
< ParamCount
; i
++)
97 Params
[i
] = Clone(Params
[i
]);
100 LLVMTypeRef FunTy
= LLVMFunctionType(Clone(LLVMGetReturnType(Src
)),
102 LLVMIsFunctionVarArg(Src
));
107 case LLVMStructTypeKind
: {
108 LLVMTypeRef S
= nullptr;
109 const char *Name
= LLVMGetStructName(Src
);
111 S
= LLVMGetTypeByName(M
, Name
);
114 S
= LLVMStructCreateNamed(Ctx
, Name
);
115 if (LLVMIsOpaqueStruct(Src
))
119 unsigned EltCount
= LLVMCountStructElementTypes(Src
);
120 SmallVector
<LLVMTypeRef
, 8> Elts
;
121 for (unsigned i
= 0; i
< EltCount
; i
++)
122 Elts
.push_back(Clone(LLVMStructGetTypeAtIndex(Src
, i
)));
124 LLVMStructSetBody(S
, Elts
.data(), EltCount
, LLVMIsPackedStruct(Src
));
126 S
= LLVMStructTypeInContext(Ctx
, Elts
.data(), EltCount
,
127 LLVMIsPackedStruct(Src
));
130 case LLVMArrayTypeKind
:
131 return LLVMArrayType(
132 Clone(LLVMGetElementType(Src
)),
133 LLVMGetArrayLength(Src
)
135 case LLVMPointerTypeKind
:
136 return LLVMPointerType(
137 Clone(LLVMGetElementType(Src
)),
138 LLVMGetPointerAddressSpace(Src
)
140 case LLVMVectorTypeKind
:
141 return LLVMVectorType(
142 Clone(LLVMGetElementType(Src
)),
143 LLVMGetVectorSize(Src
)
145 case LLVMMetadataTypeKind
:
146 return LLVMMetadataTypeInContext(Ctx
);
147 case LLVMX86_MMXTypeKind
:
148 return LLVMX86MMXTypeInContext(Ctx
);
149 case LLVMTokenTypeKind
:
150 return LLVMTokenTypeInContext(Ctx
);
153 fprintf(stderr
, "%d is not a supported typekind\n", Kind
);
158 static ValueMap
clone_params(LLVMValueRef Src
, LLVMValueRef Dst
) {
159 unsigned Count
= LLVMCountParams(Src
);
160 if (Count
!= LLVMCountParams(Dst
))
161 report_fatal_error("Parameter count mismatch");
167 LLVMValueRef SrcFirst
= LLVMGetFirstParam(Src
);
168 LLVMValueRef DstFirst
= LLVMGetFirstParam(Dst
);
169 LLVMValueRef SrcLast
= LLVMGetLastParam(Src
);
170 LLVMValueRef DstLast
= LLVMGetLastParam(Dst
);
172 LLVMValueRef SrcCur
= SrcFirst
;
173 LLVMValueRef DstCur
= DstFirst
;
174 LLVMValueRef SrcNext
= nullptr;
175 LLVMValueRef DstNext
= nullptr;
178 const char *Name
= LLVMGetValueName2(SrcCur
, &NameLen
);
179 LLVMSetValueName2(DstCur
, Name
, NameLen
);
181 VMap
[SrcCur
] = DstCur
;
184 SrcNext
= LLVMGetNextParam(SrcCur
);
185 DstNext
= LLVMGetNextParam(DstCur
);
186 if (SrcNext
== nullptr && DstNext
== nullptr) {
187 if (SrcCur
!= SrcLast
)
188 report_fatal_error("SrcLast param does not match End");
189 if (DstCur
!= DstLast
)
190 report_fatal_error("DstLast param does not match End");
194 if (SrcNext
== nullptr)
195 report_fatal_error("SrcNext was unexpectedly null");
196 if (DstNext
== nullptr)
197 report_fatal_error("DstNext was unexpectedly null");
199 LLVMValueRef SrcPrev
= LLVMGetPreviousParam(SrcNext
);
200 if (SrcPrev
!= SrcCur
)
201 report_fatal_error("SrcNext.Previous param is not Current");
203 LLVMValueRef DstPrev
= LLVMGetPreviousParam(DstNext
);
204 if (DstPrev
!= DstCur
)
205 report_fatal_error("DstNext.Previous param is not Current");
212 report_fatal_error("Parameter count does not match iteration");
217 static void check_value_kind(LLVMValueRef V
, LLVMValueKind K
) {
218 if (LLVMGetValueKind(V
) != K
)
219 report_fatal_error("LLVMGetValueKind returned incorrect type");
222 static LLVMValueRef
clone_constant_impl(LLVMValueRef Cst
, LLVMModuleRef M
);
224 static LLVMValueRef
clone_constant(LLVMValueRef Cst
, LLVMModuleRef M
) {
225 LLVMValueRef Ret
= clone_constant_impl(Cst
, M
);
226 check_value_kind(Ret
, LLVMGetValueKind(Cst
));
230 static LLVMValueRef
clone_constant_impl(LLVMValueRef Cst
, LLVMModuleRef M
) {
231 if (!LLVMIsAConstant(Cst
))
232 report_fatal_error("Expected a constant");
234 // Maybe it is a symbol
235 if (LLVMIsAGlobalValue(Cst
)) {
237 const char *Name
= LLVMGetValueName2(Cst
, &NameLen
);
240 if (LLVMIsAFunction(Cst
)) {
241 check_value_kind(Cst
, LLVMFunctionValueKind
);
243 LLVMValueRef Dst
= nullptr;
245 unsigned ID
= LLVMGetIntrinsicID(Cst
);
246 if (ID
> 0 && !LLVMIntrinsicIsOverloaded(ID
)) {
247 Dst
= LLVMGetIntrinsicDeclaration(M
, ID
, nullptr, 0);
249 // Try a normal function
250 Dst
= LLVMGetNamedFunction(M
, Name
);
255 report_fatal_error("Could not find function");
258 // Try global variable
259 if (LLVMIsAGlobalVariable(Cst
)) {
260 check_value_kind(Cst
, LLVMGlobalVariableValueKind
);
261 LLVMValueRef Dst
= LLVMGetNamedGlobal(M
, Name
);
264 report_fatal_error("Could not find variable");
268 if (LLVMIsAGlobalAlias(Cst
)) {
269 check_value_kind(Cst
, LLVMGlobalAliasValueKind
);
270 LLVMValueRef Dst
= LLVMGetNamedGlobalAlias(M
, Name
, NameLen
);
273 report_fatal_error("Could not find alias");
276 fprintf(stderr
, "Could not find @%s\n", Name
);
280 // Try integer literal
281 if (LLVMIsAConstantInt(Cst
)) {
282 check_value_kind(Cst
, LLVMConstantIntValueKind
);
283 return LLVMConstInt(TypeCloner(M
).Clone(Cst
),
284 LLVMConstIntGetZExtValue(Cst
), false);
287 // Try zeroinitializer
288 if (LLVMIsAConstantAggregateZero(Cst
)) {
289 check_value_kind(Cst
, LLVMConstantAggregateZeroValueKind
);
290 return LLVMConstNull(TypeCloner(M
).Clone(Cst
));
293 // Try constant array
294 if (LLVMIsAConstantArray(Cst
)) {
295 check_value_kind(Cst
, LLVMConstantArrayValueKind
);
296 LLVMTypeRef Ty
= TypeCloner(M
).Clone(Cst
);
297 unsigned EltCount
= LLVMGetArrayLength(Ty
);
298 SmallVector
<LLVMValueRef
, 8> Elts
;
299 for (unsigned i
= 0; i
< EltCount
; i
++)
300 Elts
.push_back(clone_constant(LLVMGetOperand(Cst
, i
), M
));
301 return LLVMConstArray(LLVMGetElementType(Ty
), Elts
.data(), EltCount
);
304 // Try contant data array
305 if (LLVMIsAConstantDataArray(Cst
)) {
306 check_value_kind(Cst
, LLVMConstantDataArrayValueKind
);
307 LLVMTypeRef Ty
= TypeCloner(M
).Clone(Cst
);
308 unsigned EltCount
= LLVMGetArrayLength(Ty
);
309 SmallVector
<LLVMValueRef
, 8> Elts
;
310 for (unsigned i
= 0; i
< EltCount
; i
++)
311 Elts
.push_back(clone_constant(LLVMGetElementAsConstant(Cst
, i
), M
));
312 return LLVMConstArray(LLVMGetElementType(Ty
), Elts
.data(), EltCount
);
315 // Try constant struct
316 if (LLVMIsAConstantStruct(Cst
)) {
317 check_value_kind(Cst
, LLVMConstantStructValueKind
);
318 LLVMTypeRef Ty
= TypeCloner(M
).Clone(Cst
);
319 unsigned EltCount
= LLVMCountStructElementTypes(Ty
);
320 SmallVector
<LLVMValueRef
, 8> Elts
;
321 for (unsigned i
= 0; i
< EltCount
; i
++)
322 Elts
.push_back(clone_constant(LLVMGetOperand(Cst
, i
), M
));
323 if (LLVMGetStructName(Ty
))
324 return LLVMConstNamedStruct(Ty
, Elts
.data(), EltCount
);
325 return LLVMConstStructInContext(LLVMGetModuleContext(M
), Elts
.data(),
326 EltCount
, LLVMIsPackedStruct(Ty
));
330 if (LLVMIsUndef(Cst
)) {
331 check_value_kind(Cst
, LLVMUndefValueValueKind
);
332 return LLVMGetUndef(TypeCloner(M
).Clone(Cst
));
336 if (LLVMIsNull(Cst
)) {
337 check_value_kind(Cst
, LLVMConstantTokenNoneValueKind
);
338 LLVMTypeRef Ty
= TypeCloner(M
).Clone(Cst
);
339 return LLVMConstNull(Ty
);
343 if (LLVMIsAConstantFP(Cst
)) {
344 check_value_kind(Cst
, LLVMConstantFPValueKind
);
345 report_fatal_error("ConstantFP is not supported");
348 // This kind of constant is not supported
349 if (!LLVMIsAConstantExpr(Cst
))
350 report_fatal_error("Expected a constant expression");
352 // At this point, it must be a constant expression
353 check_value_kind(Cst
, LLVMConstantExprValueKind
);
355 LLVMOpcode Op
= LLVMGetConstOpcode(Cst
);
358 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst
, 0), M
),
359 TypeCloner(M
).Clone(Cst
));
361 fprintf(stderr
, "%d is not a supported opcode\n", Op
);
373 FunCloner(LLVMValueRef Src
, LLVMValueRef Dst
): Fun(Dst
),
374 M(LLVMGetGlobalParent(Fun
)), VMap(clone_params(Src
, Dst
)) {}
376 LLVMTypeRef
CloneType(LLVMTypeRef Src
) {
377 return TypeCloner(M
).Clone(Src
);
380 LLVMTypeRef
CloneType(LLVMValueRef Src
) {
381 return TypeCloner(M
).Clone(Src
);
384 // Try to clone everything in the llvm::Value hierarchy.
385 LLVMValueRef
CloneValue(LLVMValueRef Src
) {
386 // First, the value may be constant.
387 if (LLVMIsAConstant(Src
))
388 return clone_constant(Src
, M
);
390 // Function argument should always be in the map already.
391 auto i
= VMap
.find(Src
);
395 if (!LLVMIsAInstruction(Src
))
396 report_fatal_error("Expected an instruction");
398 auto Ctx
= LLVMGetModuleContext(M
);
399 auto Builder
= LLVMCreateBuilderInContext(Ctx
);
400 auto BB
= DeclareBB(LLVMGetInstructionParent(Src
));
401 LLVMPositionBuilderAtEnd(Builder
, BB
);
402 auto Dst
= CloneInstruction(Src
, Builder
);
403 LLVMDisposeBuilder(Builder
);
407 void CloneAttrs(LLVMValueRef Src
, LLVMValueRef Dst
) {
408 auto Ctx
= LLVMGetModuleContext(M
);
409 int ArgCount
= LLVMGetNumArgOperands(Src
);
410 for (int i
= LLVMAttributeReturnIndex
; i
<= ArgCount
; i
++) {
411 for (unsigned k
= 0, e
= LLVMGetLastEnumAttributeKind(); k
< e
; ++k
) {
412 if (auto SrcA
= LLVMGetCallSiteEnumAttribute(Src
, i
, k
)) {
413 auto Val
= LLVMGetEnumAttributeValue(SrcA
);
414 auto A
= LLVMCreateEnumAttribute(Ctx
, k
, Val
);
415 LLVMAddCallSiteAttribute(Dst
, i
, A
);
421 LLVMValueRef
CloneInstruction(LLVMValueRef Src
, LLVMBuilderRef Builder
) {
422 check_value_kind(Src
, LLVMInstructionValueKind
);
423 if (!LLVMIsAInstruction(Src
))
424 report_fatal_error("Expected an instruction");
427 const char *Name
= LLVMGetValueName2(Src
, &NameLen
);
429 // Check if this is something we already computed.
431 auto i
= VMap
.find(Src
);
432 if (i
!= VMap
.end()) {
433 // If we have a hit, it means we already generated the instruction
434 // as a dependancy to somethign else. We need to make sure
435 // it is ordered properly.
437 LLVMInstructionRemoveFromParent(I
);
438 LLVMInsertIntoBuilderWithName(Builder
, I
, Name
);
443 // We tried everything, it must be an instruction
444 // that hasn't been generated already.
445 LLVMValueRef Dst
= nullptr;
447 LLVMOpcode Op
= LLVMGetInstructionOpcode(Src
);
450 int OpCount
= LLVMGetNumOperands(Src
);
452 Dst
= LLVMBuildRetVoid(Builder
);
454 Dst
= LLVMBuildRet(Builder
, CloneValue(LLVMGetOperand(Src
, 0)));
458 if (!LLVMIsConditional(Src
)) {
459 LLVMValueRef SrcOp
= LLVMGetOperand(Src
, 0);
460 LLVMBasicBlockRef SrcBB
= LLVMValueAsBasicBlock(SrcOp
);
461 Dst
= LLVMBuildBr(Builder
, DeclareBB(SrcBB
));
465 LLVMValueRef Cond
= LLVMGetCondition(Src
);
466 LLVMValueRef Else
= LLVMGetOperand(Src
, 1);
467 LLVMBasicBlockRef ElseBB
= DeclareBB(LLVMValueAsBasicBlock(Else
));
468 LLVMValueRef Then
= LLVMGetOperand(Src
, 2);
469 LLVMBasicBlockRef ThenBB
= DeclareBB(LLVMValueAsBasicBlock(Then
));
470 Dst
= LLVMBuildCondBr(Builder
, CloneValue(Cond
), ThenBB
, ElseBB
);
477 SmallVector
<LLVMValueRef
, 8> Args
;
478 int ArgCount
= LLVMGetNumArgOperands(Src
);
479 for (int i
= 0; i
< ArgCount
; i
++)
480 Args
.push_back(CloneValue(LLVMGetOperand(Src
, i
)));
481 LLVMValueRef Fn
= CloneValue(LLVMGetCalledValue(Src
));
482 LLVMBasicBlockRef Then
= DeclareBB(LLVMGetNormalDest(Src
));
483 LLVMBasicBlockRef Unwind
= DeclareBB(LLVMGetUnwindDest(Src
));
484 Dst
= LLVMBuildInvoke(Builder
, Fn
, Args
.data(), ArgCount
,
486 CloneAttrs(Src
, Dst
);
489 case LLVMUnreachable
:
490 Dst
= LLVMBuildUnreachable(Builder
);
493 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
494 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
495 Dst
= LLVMBuildAdd(Builder
, LHS
, RHS
, Name
);
499 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
500 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
501 Dst
= LLVMBuildSub(Builder
, LHS
, RHS
, Name
);
505 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
506 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
507 Dst
= LLVMBuildMul(Builder
, LHS
, RHS
, Name
);
511 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
512 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
513 Dst
= LLVMBuildUDiv(Builder
, LHS
, RHS
, Name
);
517 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
518 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
519 Dst
= LLVMBuildSDiv(Builder
, LHS
, RHS
, Name
);
523 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
524 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
525 Dst
= LLVMBuildURem(Builder
, LHS
, RHS
, Name
);
529 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
530 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
531 Dst
= LLVMBuildSRem(Builder
, LHS
, RHS
, Name
);
535 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
536 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
537 Dst
= LLVMBuildShl(Builder
, LHS
, RHS
, Name
);
541 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
542 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
543 Dst
= LLVMBuildLShr(Builder
, LHS
, RHS
, Name
);
547 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
548 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
549 Dst
= LLVMBuildAShr(Builder
, LHS
, RHS
, Name
);
553 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
554 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
555 Dst
= LLVMBuildAnd(Builder
, LHS
, RHS
, Name
);
559 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
560 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
561 Dst
= LLVMBuildOr(Builder
, LHS
, RHS
, Name
);
565 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
566 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
567 Dst
= LLVMBuildXor(Builder
, LHS
, RHS
, Name
);
571 LLVMTypeRef Ty
= CloneType(LLVMGetAllocatedType(Src
));
572 Dst
= LLVMBuildAlloca(Builder
, Ty
, Name
);
576 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 0));
577 Dst
= LLVMBuildLoad(Builder
, Ptr
, Name
);
578 LLVMSetAlignment(Dst
, LLVMGetAlignment(Src
));
582 LLVMValueRef Val
= CloneValue(LLVMGetOperand(Src
, 0));
583 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 1));
584 Dst
= LLVMBuildStore(Builder
, Val
, Ptr
);
585 LLVMSetAlignment(Dst
, LLVMGetAlignment(Src
));
588 case LLVMGetElementPtr
: {
589 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 0));
590 SmallVector
<LLVMValueRef
, 8> Idx
;
591 int NumIdx
= LLVMGetNumIndices(Src
);
592 for (int i
= 1; i
<= NumIdx
; i
++)
593 Idx
.push_back(CloneValue(LLVMGetOperand(Src
, i
)));
594 if (LLVMIsInBounds(Src
))
595 Dst
= LLVMBuildInBoundsGEP(Builder
, Ptr
, Idx
.data(), NumIdx
, Name
);
597 Dst
= LLVMBuildGEP(Builder
, Ptr
, Idx
.data(), NumIdx
, Name
);
600 case LLVMAtomicCmpXchg
: {
601 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 0));
602 LLVMValueRef Cmp
= CloneValue(LLVMGetOperand(Src
, 1));
603 LLVMValueRef New
= CloneValue(LLVMGetOperand(Src
, 2));
604 LLVMAtomicOrdering Succ
= LLVMGetCmpXchgSuccessOrdering(Src
);
605 LLVMAtomicOrdering Fail
= LLVMGetCmpXchgFailureOrdering(Src
);
606 LLVMBool SingleThread
= LLVMIsAtomicSingleThread(Src
);
608 Dst
= LLVMBuildAtomicCmpXchg(Builder
, Ptr
, Cmp
, New
, Succ
, Fail
,
612 LLVMValueRef V
= CloneValue(LLVMGetOperand(Src
, 0));
613 Dst
= LLVMBuildBitCast(Builder
, V
, CloneType(Src
), Name
);
617 LLVMIntPredicate Pred
= LLVMGetICmpPredicate(Src
);
618 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
619 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
620 Dst
= LLVMBuildICmp(Builder
, Pred
, LHS
, RHS
, Name
);
624 // We need to aggressively set things here because of loops.
625 VMap
[Src
] = Dst
= LLVMBuildPhi(Builder
, CloneType(Src
), Name
);
627 SmallVector
<LLVMValueRef
, 8> Values
;
628 SmallVector
<LLVMBasicBlockRef
, 8> Blocks
;
630 unsigned IncomingCount
= LLVMCountIncoming(Src
);
631 for (unsigned i
= 0; i
< IncomingCount
; ++i
) {
632 Blocks
.push_back(DeclareBB(LLVMGetIncomingBlock(Src
, i
)));
633 Values
.push_back(CloneValue(LLVMGetIncomingValue(Src
, i
)));
636 LLVMAddIncoming(Dst
, Values
.data(), Blocks
.data(), IncomingCount
);
640 SmallVector
<LLVMValueRef
, 8> Args
;
641 int ArgCount
= LLVMGetNumArgOperands(Src
);
642 for (int i
= 0; i
< ArgCount
; i
++)
643 Args
.push_back(CloneValue(LLVMGetOperand(Src
, i
)));
644 LLVMValueRef Fn
= CloneValue(LLVMGetCalledValue(Src
));
645 Dst
= LLVMBuildCall(Builder
, Fn
, Args
.data(), ArgCount
, Name
);
646 LLVMSetTailCall(Dst
, LLVMIsTailCall(Src
));
647 CloneAttrs(Src
, Dst
);
651 Dst
= LLVMBuildResume(Builder
, CloneValue(LLVMGetOperand(Src
, 0)));
654 case LLVMLandingPad
: {
655 // The landing pad API is a bit screwed up for historical reasons.
656 Dst
= LLVMBuildLandingPad(Builder
, CloneType(Src
), nullptr, 0, Name
);
657 unsigned NumClauses
= LLVMGetNumClauses(Src
);
658 for (unsigned i
= 0; i
< NumClauses
; ++i
)
659 LLVMAddClause(Dst
, CloneValue(LLVMGetClause(Src
, i
)));
660 LLVMSetCleanup(Dst
, LLVMIsCleanup(Src
));
663 case LLVMCleanupRet
: {
664 LLVMValueRef CatchPad
= CloneValue(LLVMGetOperand(Src
, 0));
665 LLVMBasicBlockRef Unwind
= nullptr;
666 if (LLVMBasicBlockRef UDest
= LLVMGetUnwindDest(Src
))
667 Unwind
= DeclareBB(UDest
);
668 Dst
= LLVMBuildCleanupRet(Builder
, CatchPad
, Unwind
);
672 LLVMValueRef CatchPad
= CloneValue(LLVMGetOperand(Src
, 0));
673 LLVMBasicBlockRef SuccBB
= DeclareBB(LLVMGetSuccessor(Src
, 0));
674 Dst
= LLVMBuildCatchRet(Builder
, CatchPad
, SuccBB
);
678 LLVMValueRef ParentPad
= CloneValue(LLVMGetParentCatchSwitch(Src
));
679 SmallVector
<LLVMValueRef
, 8> Args
;
680 int ArgCount
= LLVMGetNumArgOperands(Src
);
681 for (int i
= 0; i
< ArgCount
; i
++)
682 Args
.push_back(CloneValue(LLVMGetOperand(Src
, i
)));
683 Dst
= LLVMBuildCatchPad(Builder
, ParentPad
,
684 Args
.data(), ArgCount
, Name
);
687 case LLVMCleanupPad
: {
688 LLVMValueRef ParentPad
= CloneValue(LLVMGetOperand(Src
, 0));
689 SmallVector
<LLVMValueRef
, 8> Args
;
690 int ArgCount
= LLVMGetNumArgOperands(Src
);
691 for (int i
= 0; i
< ArgCount
; i
++)
692 Args
.push_back(CloneValue(LLVMGetArgOperand(Src
, i
)));
693 Dst
= LLVMBuildCleanupPad(Builder
, ParentPad
,
694 Args
.data(), ArgCount
, Name
);
697 case LLVMCatchSwitch
: {
698 LLVMValueRef ParentPad
= CloneValue(LLVMGetOperand(Src
, 0));
699 LLVMBasicBlockRef UnwindBB
= nullptr;
700 if (LLVMBasicBlockRef UDest
= LLVMGetUnwindDest(Src
)) {
701 UnwindBB
= DeclareBB(UDest
);
703 unsigned NumHandlers
= LLVMGetNumHandlers(Src
);
704 Dst
= LLVMBuildCatchSwitch(Builder
, ParentPad
, UnwindBB
, NumHandlers
, Name
);
705 if (NumHandlers
> 0) {
706 LLVMBasicBlockRef
*Handlers
= static_cast<LLVMBasicBlockRef
*>(
707 safe_malloc(NumHandlers
* sizeof(LLVMBasicBlockRef
)));
708 LLVMGetHandlers(Src
, Handlers
);
709 for (unsigned i
= 0; i
< NumHandlers
; i
++)
710 LLVMAddHandler(Dst
, DeclareBB(Handlers
[i
]));
715 case LLVMExtractValue
: {
716 LLVMValueRef Agg
= CloneValue(LLVMGetOperand(Src
, 0));
717 if (LLVMGetNumIndices(Src
) != 1)
718 report_fatal_error("Expected only one indice");
719 auto I
= LLVMGetIndices(Src
)[0];
720 Dst
= LLVMBuildExtractValue(Builder
, Agg
, I
, Name
);
723 case LLVMInsertValue
: {
724 LLVMValueRef Agg
= CloneValue(LLVMGetOperand(Src
, 0));
725 LLVMValueRef V
= CloneValue(LLVMGetOperand(Src
, 1));
726 if (LLVMGetNumIndices(Src
) != 1)
727 report_fatal_error("Expected only one indice");
728 auto I
= LLVMGetIndices(Src
)[0];
729 Dst
= LLVMBuildInsertValue(Builder
, Agg
, V
, I
, Name
);
736 if (Dst
== nullptr) {
737 fprintf(stderr
, "%d is not a supported opcode\n", Op
);
741 auto Ctx
= LLVMGetModuleContext(M
);
742 size_t NumMetadataEntries
;
744 LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src
,
745 &NumMetadataEntries
);
746 for (unsigned i
= 0; i
< NumMetadataEntries
; ++i
) {
747 unsigned Kind
= LLVMValueMetadataEntriesGetKind(AllMetadata
, i
);
748 LLVMMetadataRef MD
= LLVMValueMetadataEntriesGetMetadata(AllMetadata
, i
);
749 LLVMSetMetadata(Dst
, Kind
, LLVMMetadataAsValue(Ctx
, MD
));
751 LLVMDisposeValueMetadataEntries(AllMetadata
);
752 LLVMSetInstDebugLocation(Builder
, Dst
);
754 check_value_kind(Dst
, LLVMInstructionValueKind
);
755 return VMap
[Src
] = Dst
;
758 LLVMBasicBlockRef
DeclareBB(LLVMBasicBlockRef Src
) {
759 // Check if this is something we already computed.
761 auto i
= BBMap
.find(Src
);
762 if (i
!= BBMap
.end()) {
767 LLVMValueRef V
= LLVMBasicBlockAsValue(Src
);
768 if (!LLVMValueIsBasicBlock(V
) || LLVMValueAsBasicBlock(V
) != Src
)
769 report_fatal_error("Basic block is not a basic block");
771 const char *Name
= LLVMGetBasicBlockName(Src
);
773 const char *VName
= LLVMGetValueName2(V
, &NameLen
);
775 report_fatal_error("Basic block name mismatch");
777 LLVMBasicBlockRef BB
= LLVMAppendBasicBlock(Fun
, Name
);
778 return BBMap
[Src
] = BB
;
781 LLVMBasicBlockRef
CloneBB(LLVMBasicBlockRef Src
) {
782 LLVMBasicBlockRef BB
= DeclareBB(Src
);
784 // Make sure ordering is correct.
785 LLVMBasicBlockRef Prev
= LLVMGetPreviousBasicBlock(Src
);
787 LLVMMoveBasicBlockAfter(BB
, DeclareBB(Prev
));
789 LLVMValueRef First
= LLVMGetFirstInstruction(Src
);
790 LLVMValueRef Last
= LLVMGetLastInstruction(Src
);
792 if (First
== nullptr) {
794 report_fatal_error("Has no first instruction, but last one");
798 auto Ctx
= LLVMGetModuleContext(M
);
799 LLVMBuilderRef Builder
= LLVMCreateBuilderInContext(Ctx
);
800 LLVMPositionBuilderAtEnd(Builder
, BB
);
802 LLVMValueRef Cur
= First
;
803 LLVMValueRef Next
= nullptr;
805 CloneInstruction(Cur
, Builder
);
806 Next
= LLVMGetNextInstruction(Cur
);
807 if (Next
== nullptr) {
809 report_fatal_error("Final instruction does not match Last");
813 LLVMValueRef Prev
= LLVMGetPreviousInstruction(Next
);
815 report_fatal_error("Next.Previous instruction is not Current");
820 LLVMDisposeBuilder(Builder
);
824 void CloneBBs(LLVMValueRef Src
) {
825 unsigned Count
= LLVMCountBasicBlocks(Src
);
829 LLVMBasicBlockRef First
= LLVMGetFirstBasicBlock(Src
);
830 LLVMBasicBlockRef Last
= LLVMGetLastBasicBlock(Src
);
832 LLVMBasicBlockRef Cur
= First
;
833 LLVMBasicBlockRef Next
= nullptr;
837 Next
= LLVMGetNextBasicBlock(Cur
);
838 if (Next
== nullptr) {
840 report_fatal_error("Final basic block does not match Last");
844 LLVMBasicBlockRef Prev
= LLVMGetPreviousBasicBlock(Next
);
846 report_fatal_error("Next.Previous basic bloc is not Current");
852 report_fatal_error("Basic block count does not match iterration");
856 static void declare_symbols(LLVMModuleRef Src
, LLVMModuleRef M
) {
857 auto Ctx
= LLVMGetModuleContext(M
);
859 LLVMValueRef Begin
= LLVMGetFirstGlobal(Src
);
860 LLVMValueRef End
= LLVMGetLastGlobal(Src
);
862 LLVMValueRef Cur
= Begin
;
863 LLVMValueRef Next
= nullptr;
866 report_fatal_error("Range has an end but no beginning");
872 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
873 if (LLVMGetNamedGlobal(M
, Name
))
874 report_fatal_error("GlobalVariable already cloned");
875 LLVMAddGlobal(M
, LLVMGetElementType(TypeCloner(M
).Clone(Cur
)), Name
);
877 Next
= LLVMGetNextGlobal(Cur
);
878 if (Next
== nullptr) {
880 report_fatal_error("");
884 LLVMValueRef Prev
= LLVMGetPreviousGlobal(Next
);
886 report_fatal_error("Next.Previous global is not Current");
892 Begin
= LLVMGetFirstFunction(Src
);
893 End
= LLVMGetLastFunction(Src
);
896 report_fatal_error("Range has an end but no beginning");
904 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
905 if (LLVMGetNamedFunction(M
, Name
))
906 report_fatal_error("Function already cloned");
907 auto Ty
= LLVMGetElementType(TypeCloner(M
).Clone(Cur
));
908 auto F
= LLVMAddFunction(M
, Name
, Ty
);
911 for (int i
= LLVMAttributeFunctionIndex
, c
= LLVMCountParams(F
);
913 for (unsigned k
= 0, e
= LLVMGetLastEnumAttributeKind(); k
< e
; ++k
) {
914 if (auto SrcA
= LLVMGetEnumAttributeAtIndex(Cur
, i
, k
)) {
915 auto Val
= LLVMGetEnumAttributeValue(SrcA
);
916 auto DstA
= LLVMCreateEnumAttribute(Ctx
, k
, Val
);
917 LLVMAddAttributeAtIndex(F
, i
, DstA
);
922 Next
= LLVMGetNextFunction(Cur
);
923 if (Next
== nullptr) {
925 report_fatal_error("Last function does not match End");
929 LLVMValueRef Prev
= LLVMGetPreviousFunction(Next
);
931 report_fatal_error("Next.Previous function is not Current");
937 Begin
= LLVMGetFirstGlobalAlias(Src
);
938 End
= LLVMGetLastGlobalAlias(Src
);
941 report_fatal_error("Range has an end but no beginning");
942 goto GlobalIFuncDecl
;
949 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
950 if (LLVMGetNamedGlobalAlias(M
, Name
, NameLen
))
951 report_fatal_error("Global alias already cloned");
952 LLVMTypeRef CurType
= TypeCloner(M
).Clone(Cur
);
953 // FIXME: Allow NULL aliasee.
954 LLVMAddAlias(M
, CurType
, LLVMGetUndef(CurType
), Name
);
956 Next
= LLVMGetNextGlobalAlias(Cur
);
957 if (Next
== nullptr) {
959 report_fatal_error("");
963 LLVMValueRef Prev
= LLVMGetPreviousGlobalAlias(Next
);
965 report_fatal_error("Next.Previous global is not Current");
971 Begin
= LLVMGetFirstGlobalIFunc(Src
);
972 End
= LLVMGetLastGlobalIFunc(Src
);
975 report_fatal_error("Range has an end but no beginning");
983 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
984 if (LLVMGetNamedGlobalIFunc(M
, Name
, NameLen
))
985 report_fatal_error("Global ifunc already cloned");
986 LLVMTypeRef CurType
= TypeCloner(M
).Clone(LLVMGlobalGetValueType(Cur
));
987 // FIXME: Allow NULL resolver.
988 LLVMAddGlobalIFunc(M
, Name
, NameLen
,
989 CurType
, /*addressSpace*/ 0, LLVMGetUndef(CurType
));
991 Next
= LLVMGetNextGlobalIFunc(Cur
);
992 if (Next
== nullptr) {
994 report_fatal_error("");
998 LLVMValueRef Prev
= LLVMGetPreviousGlobalIFunc(Next
);
1000 report_fatal_error("Next.Previous global is not Current");
1006 LLVMNamedMDNodeRef BeginMD
= LLVMGetFirstNamedMetadata(Src
);
1007 LLVMNamedMDNodeRef EndMD
= LLVMGetLastNamedMetadata(Src
);
1009 if (EndMD
!= nullptr)
1010 report_fatal_error("Range has an end but no beginning");
1014 LLVMNamedMDNodeRef CurMD
= BeginMD
;
1015 LLVMNamedMDNodeRef NextMD
= nullptr;
1018 const char *Name
= LLVMGetNamedMetadataName(CurMD
, &NameLen
);
1019 if (LLVMGetNamedMetadata(M
, Name
, NameLen
))
1020 report_fatal_error("Named Metadata Node already cloned");
1021 LLVMGetOrInsertNamedMetadata(M
, Name
, NameLen
);
1023 NextMD
= LLVMGetNextNamedMetadata(CurMD
);
1024 if (NextMD
== nullptr) {
1026 report_fatal_error("");
1030 LLVMNamedMDNodeRef PrevMD
= LLVMGetPreviousNamedMetadata(NextMD
);
1031 if (PrevMD
!= CurMD
)
1032 report_fatal_error("Next.Previous global is not Current");
1038 static void clone_symbols(LLVMModuleRef Src
, LLVMModuleRef M
) {
1039 LLVMValueRef Begin
= LLVMGetFirstGlobal(Src
);
1040 LLVMValueRef End
= LLVMGetLastGlobal(Src
);
1042 LLVMValueRef Cur
= Begin
;
1043 LLVMValueRef Next
= nullptr;
1046 report_fatal_error("Range has an end but no beginning");
1052 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1053 LLVMValueRef G
= LLVMGetNamedGlobal(M
, Name
);
1055 report_fatal_error("GlobalVariable must have been declared already");
1057 if (auto I
= LLVMGetInitializer(Cur
))
1058 LLVMSetInitializer(G
, clone_constant(I
, M
));
1060 size_t NumMetadataEntries
;
1061 auto *AllMetadata
= LLVMGlobalCopyAllMetadata(Cur
, &NumMetadataEntries
);
1062 for (unsigned i
= 0; i
< NumMetadataEntries
; ++i
) {
1063 unsigned Kind
= LLVMValueMetadataEntriesGetKind(AllMetadata
, i
);
1064 LLVMMetadataRef MD
= LLVMValueMetadataEntriesGetMetadata(AllMetadata
, i
);
1065 LLVMGlobalSetMetadata(G
, Kind
, MD
);
1067 LLVMDisposeValueMetadataEntries(AllMetadata
);
1069 LLVMSetGlobalConstant(G
, LLVMIsGlobalConstant(Cur
));
1070 LLVMSetThreadLocal(G
, LLVMIsThreadLocal(Cur
));
1071 LLVMSetExternallyInitialized(G
, LLVMIsExternallyInitialized(Cur
));
1072 LLVMSetLinkage(G
, LLVMGetLinkage(Cur
));
1073 LLVMSetSection(G
, LLVMGetSection(Cur
));
1074 LLVMSetVisibility(G
, LLVMGetVisibility(Cur
));
1075 LLVMSetUnnamedAddress(G
, LLVMGetUnnamedAddress(Cur
));
1076 LLVMSetAlignment(G
, LLVMGetAlignment(Cur
));
1078 Next
= LLVMGetNextGlobal(Cur
);
1079 if (Next
== nullptr) {
1081 report_fatal_error("");
1085 LLVMValueRef Prev
= LLVMGetPreviousGlobal(Next
);
1087 report_fatal_error("Next.Previous global is not Current");
1093 Begin
= LLVMGetFirstFunction(Src
);
1094 End
= LLVMGetLastFunction(Src
);
1097 report_fatal_error("Range has an end but no beginning");
1105 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1106 LLVMValueRef Fun
= LLVMGetNamedFunction(M
, Name
);
1108 report_fatal_error("Function must have been declared already");
1110 if (LLVMHasPersonalityFn(Cur
)) {
1112 const char *FName
= LLVMGetValueName2(LLVMGetPersonalityFn(Cur
),
1114 LLVMValueRef P
= LLVMGetNamedFunction(M
, FName
);
1116 report_fatal_error("Could not find personality function");
1117 LLVMSetPersonalityFn(Fun
, P
);
1120 size_t NumMetadataEntries
;
1121 auto *AllMetadata
= LLVMGlobalCopyAllMetadata(Cur
, &NumMetadataEntries
);
1122 for (unsigned i
= 0; i
< NumMetadataEntries
; ++i
) {
1123 unsigned Kind
= LLVMValueMetadataEntriesGetKind(AllMetadata
, i
);
1124 LLVMMetadataRef MD
= LLVMValueMetadataEntriesGetMetadata(AllMetadata
, i
);
1125 LLVMGlobalSetMetadata(Fun
, Kind
, MD
);
1127 LLVMDisposeValueMetadataEntries(AllMetadata
);
1129 FunCloner
FC(Cur
, Fun
);
1132 Next
= LLVMGetNextFunction(Cur
);
1133 if (Next
== nullptr) {
1135 report_fatal_error("Last function does not match End");
1139 LLVMValueRef Prev
= LLVMGetPreviousFunction(Next
);
1141 report_fatal_error("Next.Previous function is not Current");
1147 Begin
= LLVMGetFirstGlobalAlias(Src
);
1148 End
= LLVMGetLastGlobalAlias(Src
);
1151 report_fatal_error("Range has an end but no beginning");
1152 goto GlobalIFuncClone
;
1159 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1160 LLVMValueRef Alias
= LLVMGetNamedGlobalAlias(M
, Name
, NameLen
);
1162 report_fatal_error("Global alias must have been declared already");
1164 if (LLVMValueRef Aliasee
= LLVMAliasGetAliasee(Cur
)) {
1165 LLVMAliasSetAliasee(Alias
, clone_constant(Aliasee
, M
));
1168 LLVMSetLinkage(Alias
, LLVMGetLinkage(Cur
));
1169 LLVMSetUnnamedAddress(Alias
, LLVMGetUnnamedAddress(Cur
));
1171 Next
= LLVMGetNextGlobalAlias(Cur
);
1172 if (Next
== nullptr) {
1174 report_fatal_error("Last global alias does not match End");
1178 LLVMValueRef Prev
= LLVMGetPreviousGlobalAlias(Next
);
1180 report_fatal_error("Next.Previous global alias is not Current");
1186 Begin
= LLVMGetFirstGlobalIFunc(Src
);
1187 End
= LLVMGetLastGlobalIFunc(Src
);
1190 report_fatal_error("Range has an end but no beginning");
1198 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1199 LLVMValueRef IFunc
= LLVMGetNamedGlobalIFunc(M
, Name
, NameLen
);
1201 report_fatal_error("Global ifunc must have been declared already");
1203 if (LLVMValueRef Resolver
= LLVMGetGlobalIFuncResolver(Cur
)) {
1204 LLVMSetGlobalIFuncResolver(IFunc
, clone_constant(Resolver
, M
));
1207 LLVMSetLinkage(IFunc
, LLVMGetLinkage(Cur
));
1208 LLVMSetUnnamedAddress(IFunc
, LLVMGetUnnamedAddress(Cur
));
1210 Next
= LLVMGetNextGlobalIFunc(Cur
);
1211 if (Next
== nullptr) {
1213 report_fatal_error("Last global alias does not match End");
1217 LLVMValueRef Prev
= LLVMGetPreviousGlobalIFunc(Next
);
1219 report_fatal_error("Next.Previous global alias is not Current");
1225 LLVMNamedMDNodeRef BeginMD
= LLVMGetFirstNamedMetadata(Src
);
1226 LLVMNamedMDNodeRef EndMD
= LLVMGetLastNamedMetadata(Src
);
1228 if (EndMD
!= nullptr)
1229 report_fatal_error("Range has an end but no beginning");
1233 LLVMNamedMDNodeRef CurMD
= BeginMD
;
1234 LLVMNamedMDNodeRef NextMD
= nullptr;
1237 const char *Name
= LLVMGetNamedMetadataName(CurMD
, &NameLen
);
1238 LLVMNamedMDNodeRef NamedMD
= LLVMGetNamedMetadata(M
, Name
, NameLen
);
1240 report_fatal_error("Named MD Node must have been declared already");
1242 unsigned OperandCount
= LLVMGetNamedMetadataNumOperands(Src
, Name
);
1243 LLVMValueRef
*OperandBuf
= static_cast<LLVMValueRef
*>(
1244 safe_malloc(OperandCount
* sizeof(LLVMValueRef
)));
1245 LLVMGetNamedMetadataOperands(Src
, Name
, OperandBuf
);
1246 for (unsigned i
= 0, e
= OperandCount
; i
!= e
; ++i
) {
1247 LLVMAddNamedMetadataOperand(M
, Name
, OperandBuf
[i
]);
1251 NextMD
= LLVMGetNextNamedMetadata(CurMD
);
1252 if (NextMD
== nullptr) {
1254 report_fatal_error("Last Named MD Node does not match End");
1258 LLVMNamedMDNodeRef PrevMD
= LLVMGetPreviousNamedMetadata(NextMD
);
1259 if (PrevMD
!= CurMD
)
1260 report_fatal_error("Next.Previous Named MD Node is not Current");
1266 int llvm_echo(void) {
1267 LLVMEnablePrettyStackTrace();
1269 LLVMModuleRef Src
= llvm_load_module(false, true);
1270 size_t SourceFileLen
;
1271 const char *SourceFileName
= LLVMGetSourceFileName(Src
, &SourceFileLen
);
1272 size_t ModuleIdentLen
;
1273 const char *ModuleName
= LLVMGetModuleIdentifier(Src
, &ModuleIdentLen
);
1274 LLVMContextRef Ctx
= LLVMContextCreate();
1275 LLVMModuleRef M
= LLVMModuleCreateWithNameInContext(ModuleName
, Ctx
);
1277 LLVMSetSourceFileName(M
, SourceFileName
, SourceFileLen
);
1278 LLVMSetModuleIdentifier(M
, ModuleName
, ModuleIdentLen
);
1280 LLVMSetTarget(M
, LLVMGetTarget(Src
));
1281 LLVMSetModuleDataLayout(M
, LLVMGetModuleDataLayout(Src
));
1282 if (strcmp(LLVMGetDataLayoutStr(M
), LLVMGetDataLayoutStr(Src
)))
1283 report_fatal_error("Inconsistent DataLayout string representation");
1285 size_t ModuleInlineAsmLen
;
1286 const char *ModuleAsm
= LLVMGetModuleInlineAsm(Src
, &ModuleInlineAsmLen
);
1287 LLVMSetModuleInlineAsm2(M
, ModuleAsm
, ModuleInlineAsmLen
);
1289 declare_symbols(Src
, M
);
1290 clone_symbols(Src
, M
);
1291 char *Str
= LLVMPrintModuleToString(M
);
1294 LLVMDisposeMessage(Str
);
1295 LLVMDisposeModule(Src
);
1296 LLVMDisposeModule(M
);
1297 LLVMContextDispose(Ctx
);