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 alignment.
33 // Because the C API uses opaques pointer types, their alignment 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
));
329 // Try ConstantPointerNull
330 if (LLVMIsAConstantPointerNull(Cst
)) {
331 check_value_kind(Cst
, LLVMConstantPointerNullValueKind
);
332 LLVMTypeRef Ty
= TypeCloner(M
).Clone(Cst
);
333 return LLVMConstNull(Ty
);
337 if (LLVMIsUndef(Cst
)) {
338 check_value_kind(Cst
, LLVMUndefValueValueKind
);
339 return LLVMGetUndef(TypeCloner(M
).Clone(Cst
));
343 if (LLVMIsNull(Cst
)) {
344 check_value_kind(Cst
, LLVMConstantTokenNoneValueKind
);
345 LLVMTypeRef Ty
= TypeCloner(M
).Clone(Cst
);
346 return LLVMConstNull(Ty
);
350 if (LLVMIsAConstantFP(Cst
)) {
351 check_value_kind(Cst
, LLVMConstantFPValueKind
);
352 report_fatal_error("ConstantFP is not supported");
355 // This kind of constant is not supported
356 if (!LLVMIsAConstantExpr(Cst
))
357 report_fatal_error("Expected a constant expression");
359 // At this point, it must be a constant expression
360 check_value_kind(Cst
, LLVMConstantExprValueKind
);
362 LLVMOpcode Op
= LLVMGetConstOpcode(Cst
);
365 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst
, 0), M
),
366 TypeCloner(M
).Clone(Cst
));
368 fprintf(stderr
, "%d is not a supported opcode\n", Op
);
380 FunCloner(LLVMValueRef Src
, LLVMValueRef Dst
): Fun(Dst
),
381 M(LLVMGetGlobalParent(Fun
)), VMap(clone_params(Src
, Dst
)) {}
383 LLVMTypeRef
CloneType(LLVMTypeRef Src
) {
384 return TypeCloner(M
).Clone(Src
);
387 LLVMTypeRef
CloneType(LLVMValueRef Src
) {
388 return TypeCloner(M
).Clone(Src
);
391 // Try to clone everything in the llvm::Value hierarchy.
392 LLVMValueRef
CloneValue(LLVMValueRef Src
) {
393 // First, the value may be constant.
394 if (LLVMIsAConstant(Src
))
395 return clone_constant(Src
, M
);
397 // Function argument should always be in the map already.
398 auto i
= VMap
.find(Src
);
402 if (!LLVMIsAInstruction(Src
))
403 report_fatal_error("Expected an instruction");
405 auto Ctx
= LLVMGetModuleContext(M
);
406 auto Builder
= LLVMCreateBuilderInContext(Ctx
);
407 auto BB
= DeclareBB(LLVMGetInstructionParent(Src
));
408 LLVMPositionBuilderAtEnd(Builder
, BB
);
409 auto Dst
= CloneInstruction(Src
, Builder
);
410 LLVMDisposeBuilder(Builder
);
414 void CloneAttrs(LLVMValueRef Src
, LLVMValueRef Dst
) {
415 auto Ctx
= LLVMGetModuleContext(M
);
416 int ArgCount
= LLVMGetNumArgOperands(Src
);
417 for (int i
= LLVMAttributeReturnIndex
; i
<= ArgCount
; i
++) {
418 for (unsigned k
= 0, e
= LLVMGetLastEnumAttributeKind(); k
< e
; ++k
) {
419 if (auto SrcA
= LLVMGetCallSiteEnumAttribute(Src
, i
, k
)) {
420 auto Val
= LLVMGetEnumAttributeValue(SrcA
);
421 auto A
= LLVMCreateEnumAttribute(Ctx
, k
, Val
);
422 LLVMAddCallSiteAttribute(Dst
, i
, A
);
428 LLVMValueRef
CloneInstruction(LLVMValueRef Src
, LLVMBuilderRef Builder
) {
429 check_value_kind(Src
, LLVMInstructionValueKind
);
430 if (!LLVMIsAInstruction(Src
))
431 report_fatal_error("Expected an instruction");
434 const char *Name
= LLVMGetValueName2(Src
, &NameLen
);
436 // Check if this is something we already computed.
438 auto i
= VMap
.find(Src
);
439 if (i
!= VMap
.end()) {
440 // If we have a hit, it means we already generated the instruction
441 // as a dependancy to somethign else. We need to make sure
442 // it is ordered properly.
444 LLVMInstructionRemoveFromParent(I
);
445 LLVMInsertIntoBuilderWithName(Builder
, I
, Name
);
450 // We tried everything, it must be an instruction
451 // that hasn't been generated already.
452 LLVMValueRef Dst
= nullptr;
454 LLVMOpcode Op
= LLVMGetInstructionOpcode(Src
);
457 int OpCount
= LLVMGetNumOperands(Src
);
459 Dst
= LLVMBuildRetVoid(Builder
);
461 Dst
= LLVMBuildRet(Builder
, CloneValue(LLVMGetOperand(Src
, 0)));
465 if (!LLVMIsConditional(Src
)) {
466 LLVMValueRef SrcOp
= LLVMGetOperand(Src
, 0);
467 LLVMBasicBlockRef SrcBB
= LLVMValueAsBasicBlock(SrcOp
);
468 Dst
= LLVMBuildBr(Builder
, DeclareBB(SrcBB
));
472 LLVMValueRef Cond
= LLVMGetCondition(Src
);
473 LLVMValueRef Else
= LLVMGetOperand(Src
, 1);
474 LLVMBasicBlockRef ElseBB
= DeclareBB(LLVMValueAsBasicBlock(Else
));
475 LLVMValueRef Then
= LLVMGetOperand(Src
, 2);
476 LLVMBasicBlockRef ThenBB
= DeclareBB(LLVMValueAsBasicBlock(Then
));
477 Dst
= LLVMBuildCondBr(Builder
, CloneValue(Cond
), ThenBB
, ElseBB
);
484 SmallVector
<LLVMValueRef
, 8> Args
;
485 int ArgCount
= LLVMGetNumArgOperands(Src
);
486 for (int i
= 0; i
< ArgCount
; i
++)
487 Args
.push_back(CloneValue(LLVMGetOperand(Src
, i
)));
488 LLVMValueRef Fn
= CloneValue(LLVMGetCalledValue(Src
));
489 LLVMBasicBlockRef Then
= DeclareBB(LLVMGetNormalDest(Src
));
490 LLVMBasicBlockRef Unwind
= DeclareBB(LLVMGetUnwindDest(Src
));
491 Dst
= LLVMBuildInvoke(Builder
, Fn
, Args
.data(), ArgCount
,
493 CloneAttrs(Src
, Dst
);
496 case LLVMUnreachable
:
497 Dst
= LLVMBuildUnreachable(Builder
);
500 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
501 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
502 Dst
= LLVMBuildAdd(Builder
, LHS
, RHS
, Name
);
506 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
507 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
508 Dst
= LLVMBuildSub(Builder
, LHS
, RHS
, Name
);
512 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
513 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
514 Dst
= LLVMBuildMul(Builder
, LHS
, RHS
, Name
);
518 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
519 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
520 Dst
= LLVMBuildUDiv(Builder
, LHS
, RHS
, Name
);
524 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
525 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
526 Dst
= LLVMBuildSDiv(Builder
, LHS
, RHS
, Name
);
530 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
531 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
532 Dst
= LLVMBuildURem(Builder
, LHS
, RHS
, Name
);
536 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
537 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
538 Dst
= LLVMBuildSRem(Builder
, LHS
, RHS
, Name
);
542 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
543 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
544 Dst
= LLVMBuildShl(Builder
, LHS
, RHS
, Name
);
548 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
549 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
550 Dst
= LLVMBuildLShr(Builder
, LHS
, RHS
, Name
);
554 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
555 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
556 Dst
= LLVMBuildAShr(Builder
, LHS
, RHS
, Name
);
560 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
561 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
562 Dst
= LLVMBuildAnd(Builder
, LHS
, RHS
, Name
);
566 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
567 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
568 Dst
= LLVMBuildOr(Builder
, LHS
, RHS
, Name
);
572 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
573 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
574 Dst
= LLVMBuildXor(Builder
, LHS
, RHS
, Name
);
578 LLVMTypeRef Ty
= CloneType(LLVMGetAllocatedType(Src
));
579 Dst
= LLVMBuildAlloca(Builder
, Ty
, Name
);
583 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 0));
584 Dst
= LLVMBuildLoad(Builder
, Ptr
, Name
);
585 LLVMSetAlignment(Dst
, LLVMGetAlignment(Src
));
586 LLVMSetOrdering(Dst
, LLVMGetOrdering(Src
));
587 LLVMSetVolatile(Dst
, LLVMGetVolatile(Src
));
591 LLVMValueRef Val
= CloneValue(LLVMGetOperand(Src
, 0));
592 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 1));
593 Dst
= LLVMBuildStore(Builder
, Val
, Ptr
);
594 LLVMSetAlignment(Dst
, LLVMGetAlignment(Src
));
595 LLVMSetOrdering(Dst
, LLVMGetOrdering(Src
));
596 LLVMSetVolatile(Dst
, LLVMGetVolatile(Src
));
599 case LLVMGetElementPtr
: {
600 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 0));
601 SmallVector
<LLVMValueRef
, 8> Idx
;
602 int NumIdx
= LLVMGetNumIndices(Src
);
603 for (int i
= 1; i
<= NumIdx
; i
++)
604 Idx
.push_back(CloneValue(LLVMGetOperand(Src
, i
)));
605 if (LLVMIsInBounds(Src
))
606 Dst
= LLVMBuildInBoundsGEP(Builder
, Ptr
, Idx
.data(), NumIdx
, Name
);
608 Dst
= LLVMBuildGEP(Builder
, Ptr
, Idx
.data(), NumIdx
, Name
);
611 case LLVMAtomicRMW
: {
612 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 0));
613 LLVMValueRef Val
= CloneValue(LLVMGetOperand(Src
, 1));
614 LLVMAtomicRMWBinOp BinOp
= LLVMGetAtomicRMWBinOp(Src
);
615 LLVMAtomicOrdering Ord
= LLVMGetOrdering(Src
);
616 LLVMBool SingleThread
= LLVMIsAtomicSingleThread(Src
);
617 Dst
= LLVMBuildAtomicRMW(Builder
, BinOp
, Ptr
, Val
, Ord
, SingleThread
);
618 LLVMSetVolatile(Dst
, LLVMGetVolatile(Src
));
619 LLVMSetValueName2(Dst
, Name
, NameLen
);
622 case LLVMAtomicCmpXchg
: {
623 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 0));
624 LLVMValueRef Cmp
= CloneValue(LLVMGetOperand(Src
, 1));
625 LLVMValueRef New
= CloneValue(LLVMGetOperand(Src
, 2));
626 LLVMAtomicOrdering Succ
= LLVMGetCmpXchgSuccessOrdering(Src
);
627 LLVMAtomicOrdering Fail
= LLVMGetCmpXchgFailureOrdering(Src
);
628 LLVMBool SingleThread
= LLVMIsAtomicSingleThread(Src
);
630 Dst
= LLVMBuildAtomicCmpXchg(Builder
, Ptr
, Cmp
, New
, Succ
, Fail
,
632 LLVMSetVolatile(Dst
, LLVMGetVolatile(Src
));
633 LLVMSetWeak(Dst
, LLVMGetWeak(Src
));
634 LLVMSetValueName2(Dst
, Name
, NameLen
);
638 LLVMValueRef V
= CloneValue(LLVMGetOperand(Src
, 0));
639 Dst
= LLVMBuildBitCast(Builder
, V
, CloneType(Src
), Name
);
643 LLVMIntPredicate Pred
= LLVMGetICmpPredicate(Src
);
644 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
645 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
646 Dst
= LLVMBuildICmp(Builder
, Pred
, LHS
, RHS
, Name
);
650 // We need to aggressively set things here because of loops.
651 VMap
[Src
] = Dst
= LLVMBuildPhi(Builder
, CloneType(Src
), Name
);
653 SmallVector
<LLVMValueRef
, 8> Values
;
654 SmallVector
<LLVMBasicBlockRef
, 8> Blocks
;
656 unsigned IncomingCount
= LLVMCountIncoming(Src
);
657 for (unsigned i
= 0; i
< IncomingCount
; ++i
) {
658 Blocks
.push_back(DeclareBB(LLVMGetIncomingBlock(Src
, i
)));
659 Values
.push_back(CloneValue(LLVMGetIncomingValue(Src
, i
)));
662 LLVMAddIncoming(Dst
, Values
.data(), Blocks
.data(), IncomingCount
);
666 SmallVector
<LLVMValueRef
, 8> Args
;
667 int ArgCount
= LLVMGetNumArgOperands(Src
);
668 for (int i
= 0; i
< ArgCount
; i
++)
669 Args
.push_back(CloneValue(LLVMGetOperand(Src
, i
)));
670 LLVMValueRef Fn
= CloneValue(LLVMGetCalledValue(Src
));
671 Dst
= LLVMBuildCall(Builder
, Fn
, Args
.data(), ArgCount
, Name
);
672 LLVMSetTailCall(Dst
, LLVMIsTailCall(Src
));
673 CloneAttrs(Src
, Dst
);
677 Dst
= LLVMBuildResume(Builder
, CloneValue(LLVMGetOperand(Src
, 0)));
680 case LLVMLandingPad
: {
681 // The landing pad API is a bit screwed up for historical reasons.
682 Dst
= LLVMBuildLandingPad(Builder
, CloneType(Src
), nullptr, 0, Name
);
683 unsigned NumClauses
= LLVMGetNumClauses(Src
);
684 for (unsigned i
= 0; i
< NumClauses
; ++i
)
685 LLVMAddClause(Dst
, CloneValue(LLVMGetClause(Src
, i
)));
686 LLVMSetCleanup(Dst
, LLVMIsCleanup(Src
));
689 case LLVMCleanupRet
: {
690 LLVMValueRef CatchPad
= CloneValue(LLVMGetOperand(Src
, 0));
691 LLVMBasicBlockRef Unwind
= nullptr;
692 if (LLVMBasicBlockRef UDest
= LLVMGetUnwindDest(Src
))
693 Unwind
= DeclareBB(UDest
);
694 Dst
= LLVMBuildCleanupRet(Builder
, CatchPad
, Unwind
);
698 LLVMValueRef CatchPad
= CloneValue(LLVMGetOperand(Src
, 0));
699 LLVMBasicBlockRef SuccBB
= DeclareBB(LLVMGetSuccessor(Src
, 0));
700 Dst
= LLVMBuildCatchRet(Builder
, CatchPad
, SuccBB
);
704 LLVMValueRef ParentPad
= CloneValue(LLVMGetParentCatchSwitch(Src
));
705 SmallVector
<LLVMValueRef
, 8> Args
;
706 int ArgCount
= LLVMGetNumArgOperands(Src
);
707 for (int i
= 0; i
< ArgCount
; i
++)
708 Args
.push_back(CloneValue(LLVMGetOperand(Src
, i
)));
709 Dst
= LLVMBuildCatchPad(Builder
, ParentPad
,
710 Args
.data(), ArgCount
, Name
);
713 case LLVMCleanupPad
: {
714 LLVMValueRef ParentPad
= CloneValue(LLVMGetOperand(Src
, 0));
715 SmallVector
<LLVMValueRef
, 8> Args
;
716 int ArgCount
= LLVMGetNumArgOperands(Src
);
717 for (int i
= 0; i
< ArgCount
; i
++)
718 Args
.push_back(CloneValue(LLVMGetArgOperand(Src
, i
)));
719 Dst
= LLVMBuildCleanupPad(Builder
, ParentPad
,
720 Args
.data(), ArgCount
, Name
);
723 case LLVMCatchSwitch
: {
724 LLVMValueRef ParentPad
= CloneValue(LLVMGetOperand(Src
, 0));
725 LLVMBasicBlockRef UnwindBB
= nullptr;
726 if (LLVMBasicBlockRef UDest
= LLVMGetUnwindDest(Src
)) {
727 UnwindBB
= DeclareBB(UDest
);
729 unsigned NumHandlers
= LLVMGetNumHandlers(Src
);
730 Dst
= LLVMBuildCatchSwitch(Builder
, ParentPad
, UnwindBB
, NumHandlers
, Name
);
731 if (NumHandlers
> 0) {
732 LLVMBasicBlockRef
*Handlers
= static_cast<LLVMBasicBlockRef
*>(
733 safe_malloc(NumHandlers
* sizeof(LLVMBasicBlockRef
)));
734 LLVMGetHandlers(Src
, Handlers
);
735 for (unsigned i
= 0; i
< NumHandlers
; i
++)
736 LLVMAddHandler(Dst
, DeclareBB(Handlers
[i
]));
741 case LLVMExtractValue
: {
742 LLVMValueRef Agg
= CloneValue(LLVMGetOperand(Src
, 0));
743 if (LLVMGetNumIndices(Src
) != 1)
744 report_fatal_error("Expected only one indice");
745 auto I
= LLVMGetIndices(Src
)[0];
746 Dst
= LLVMBuildExtractValue(Builder
, Agg
, I
, Name
);
749 case LLVMInsertValue
: {
750 LLVMValueRef Agg
= CloneValue(LLVMGetOperand(Src
, 0));
751 LLVMValueRef V
= CloneValue(LLVMGetOperand(Src
, 1));
752 if (LLVMGetNumIndices(Src
) != 1)
753 report_fatal_error("Expected only one indice");
754 auto I
= LLVMGetIndices(Src
)[0];
755 Dst
= LLVMBuildInsertValue(Builder
, Agg
, V
, I
, Name
);
759 LLVMValueRef Arg
= CloneValue(LLVMGetOperand(Src
, 0));
760 Dst
= LLVMBuildFreeze(Builder
, Arg
, Name
);
767 if (Dst
== nullptr) {
768 fprintf(stderr
, "%d is not a supported opcode\n", Op
);
772 auto Ctx
= LLVMGetModuleContext(M
);
773 size_t NumMetadataEntries
;
775 LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src
,
776 &NumMetadataEntries
);
777 for (unsigned i
= 0; i
< NumMetadataEntries
; ++i
) {
778 unsigned Kind
= LLVMValueMetadataEntriesGetKind(AllMetadata
, i
);
779 LLVMMetadataRef MD
= LLVMValueMetadataEntriesGetMetadata(AllMetadata
, i
);
780 LLVMSetMetadata(Dst
, Kind
, LLVMMetadataAsValue(Ctx
, MD
));
782 LLVMDisposeValueMetadataEntries(AllMetadata
);
783 LLVMSetInstDebugLocation(Builder
, Dst
);
785 check_value_kind(Dst
, LLVMInstructionValueKind
);
786 return VMap
[Src
] = Dst
;
789 LLVMBasicBlockRef
DeclareBB(LLVMBasicBlockRef Src
) {
790 // Check if this is something we already computed.
792 auto i
= BBMap
.find(Src
);
793 if (i
!= BBMap
.end()) {
798 LLVMValueRef V
= LLVMBasicBlockAsValue(Src
);
799 if (!LLVMValueIsBasicBlock(V
) || LLVMValueAsBasicBlock(V
) != Src
)
800 report_fatal_error("Basic block is not a basic block");
802 const char *Name
= LLVMGetBasicBlockName(Src
);
804 const char *VName
= LLVMGetValueName2(V
, &NameLen
);
806 report_fatal_error("Basic block name mismatch");
808 LLVMBasicBlockRef BB
= LLVMAppendBasicBlock(Fun
, Name
);
809 return BBMap
[Src
] = BB
;
812 LLVMBasicBlockRef
CloneBB(LLVMBasicBlockRef Src
) {
813 LLVMBasicBlockRef BB
= DeclareBB(Src
);
815 // Make sure ordering is correct.
816 LLVMBasicBlockRef Prev
= LLVMGetPreviousBasicBlock(Src
);
818 LLVMMoveBasicBlockAfter(BB
, DeclareBB(Prev
));
820 LLVMValueRef First
= LLVMGetFirstInstruction(Src
);
821 LLVMValueRef Last
= LLVMGetLastInstruction(Src
);
823 if (First
== nullptr) {
825 report_fatal_error("Has no first instruction, but last one");
829 auto Ctx
= LLVMGetModuleContext(M
);
830 LLVMBuilderRef Builder
= LLVMCreateBuilderInContext(Ctx
);
831 LLVMPositionBuilderAtEnd(Builder
, BB
);
833 LLVMValueRef Cur
= First
;
834 LLVMValueRef Next
= nullptr;
836 CloneInstruction(Cur
, Builder
);
837 Next
= LLVMGetNextInstruction(Cur
);
838 if (Next
== nullptr) {
840 report_fatal_error("Final instruction does not match Last");
844 LLVMValueRef Prev
= LLVMGetPreviousInstruction(Next
);
846 report_fatal_error("Next.Previous instruction is not Current");
851 LLVMDisposeBuilder(Builder
);
855 void CloneBBs(LLVMValueRef Src
) {
856 unsigned Count
= LLVMCountBasicBlocks(Src
);
860 LLVMBasicBlockRef First
= LLVMGetFirstBasicBlock(Src
);
861 LLVMBasicBlockRef Last
= LLVMGetLastBasicBlock(Src
);
863 LLVMBasicBlockRef Cur
= First
;
864 LLVMBasicBlockRef Next
= nullptr;
868 Next
= LLVMGetNextBasicBlock(Cur
);
869 if (Next
== nullptr) {
871 report_fatal_error("Final basic block does not match Last");
875 LLVMBasicBlockRef Prev
= LLVMGetPreviousBasicBlock(Next
);
877 report_fatal_error("Next.Previous basic bloc is not Current");
883 report_fatal_error("Basic block count does not match iterration");
887 static void declare_symbols(LLVMModuleRef Src
, LLVMModuleRef M
) {
888 auto Ctx
= LLVMGetModuleContext(M
);
890 LLVMValueRef Begin
= LLVMGetFirstGlobal(Src
);
891 LLVMValueRef End
= LLVMGetLastGlobal(Src
);
893 LLVMValueRef Cur
= Begin
;
894 LLVMValueRef Next
= nullptr;
897 report_fatal_error("Range has an end but no beginning");
903 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
904 if (LLVMGetNamedGlobal(M
, Name
))
905 report_fatal_error("GlobalVariable already cloned");
906 LLVMAddGlobal(M
, LLVMGetElementType(TypeCloner(M
).Clone(Cur
)), Name
);
908 Next
= LLVMGetNextGlobal(Cur
);
909 if (Next
== nullptr) {
911 report_fatal_error("");
915 LLVMValueRef Prev
= LLVMGetPreviousGlobal(Next
);
917 report_fatal_error("Next.Previous global is not Current");
923 Begin
= LLVMGetFirstFunction(Src
);
924 End
= LLVMGetLastFunction(Src
);
927 report_fatal_error("Range has an end but no beginning");
935 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
936 if (LLVMGetNamedFunction(M
, Name
))
937 report_fatal_error("Function already cloned");
938 auto Ty
= LLVMGetElementType(TypeCloner(M
).Clone(Cur
));
939 auto F
= LLVMAddFunction(M
, Name
, Ty
);
942 for (int i
= LLVMAttributeFunctionIndex
, c
= LLVMCountParams(F
);
944 for (unsigned k
= 0, e
= LLVMGetLastEnumAttributeKind(); k
< e
; ++k
) {
945 if (auto SrcA
= LLVMGetEnumAttributeAtIndex(Cur
, i
, k
)) {
946 auto Val
= LLVMGetEnumAttributeValue(SrcA
);
947 auto DstA
= LLVMCreateEnumAttribute(Ctx
, k
, Val
);
948 LLVMAddAttributeAtIndex(F
, i
, DstA
);
953 Next
= LLVMGetNextFunction(Cur
);
954 if (Next
== nullptr) {
956 report_fatal_error("Last function does not match End");
960 LLVMValueRef Prev
= LLVMGetPreviousFunction(Next
);
962 report_fatal_error("Next.Previous function is not Current");
968 Begin
= LLVMGetFirstGlobalAlias(Src
);
969 End
= LLVMGetLastGlobalAlias(Src
);
972 report_fatal_error("Range has an end but no beginning");
973 goto GlobalIFuncDecl
;
980 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
981 if (LLVMGetNamedGlobalAlias(M
, Name
, NameLen
))
982 report_fatal_error("Global alias already cloned");
983 LLVMTypeRef CurType
= TypeCloner(M
).Clone(Cur
);
984 // FIXME: Allow NULL aliasee.
985 LLVMAddAlias(M
, CurType
, LLVMGetUndef(CurType
), Name
);
987 Next
= LLVMGetNextGlobalAlias(Cur
);
988 if (Next
== nullptr) {
990 report_fatal_error("");
994 LLVMValueRef Prev
= LLVMGetPreviousGlobalAlias(Next
);
996 report_fatal_error("Next.Previous global is not Current");
1002 Begin
= LLVMGetFirstGlobalIFunc(Src
);
1003 End
= LLVMGetLastGlobalIFunc(Src
);
1006 report_fatal_error("Range has an end but no beginning");
1014 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1015 if (LLVMGetNamedGlobalIFunc(M
, Name
, NameLen
))
1016 report_fatal_error("Global ifunc already cloned");
1017 LLVMTypeRef CurType
= TypeCloner(M
).Clone(LLVMGlobalGetValueType(Cur
));
1018 // FIXME: Allow NULL resolver.
1019 LLVMAddGlobalIFunc(M
, Name
, NameLen
,
1020 CurType
, /*addressSpace*/ 0, LLVMGetUndef(CurType
));
1022 Next
= LLVMGetNextGlobalIFunc(Cur
);
1023 if (Next
== nullptr) {
1025 report_fatal_error("");
1029 LLVMValueRef Prev
= LLVMGetPreviousGlobalIFunc(Next
);
1031 report_fatal_error("Next.Previous global is not Current");
1037 LLVMNamedMDNodeRef BeginMD
= LLVMGetFirstNamedMetadata(Src
);
1038 LLVMNamedMDNodeRef EndMD
= LLVMGetLastNamedMetadata(Src
);
1040 if (EndMD
!= nullptr)
1041 report_fatal_error("Range has an end but no beginning");
1045 LLVMNamedMDNodeRef CurMD
= BeginMD
;
1046 LLVMNamedMDNodeRef NextMD
= nullptr;
1049 const char *Name
= LLVMGetNamedMetadataName(CurMD
, &NameLen
);
1050 if (LLVMGetNamedMetadata(M
, Name
, NameLen
))
1051 report_fatal_error("Named Metadata Node already cloned");
1052 LLVMGetOrInsertNamedMetadata(M
, Name
, NameLen
);
1054 NextMD
= LLVMGetNextNamedMetadata(CurMD
);
1055 if (NextMD
== nullptr) {
1057 report_fatal_error("");
1061 LLVMNamedMDNodeRef PrevMD
= LLVMGetPreviousNamedMetadata(NextMD
);
1062 if (PrevMD
!= CurMD
)
1063 report_fatal_error("Next.Previous global is not Current");
1069 static void clone_symbols(LLVMModuleRef Src
, LLVMModuleRef M
) {
1070 LLVMValueRef Begin
= LLVMGetFirstGlobal(Src
);
1071 LLVMValueRef End
= LLVMGetLastGlobal(Src
);
1073 LLVMValueRef Cur
= Begin
;
1074 LLVMValueRef Next
= nullptr;
1077 report_fatal_error("Range has an end but no beginning");
1083 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1084 LLVMValueRef G
= LLVMGetNamedGlobal(M
, Name
);
1086 report_fatal_error("GlobalVariable must have been declared already");
1088 if (auto I
= LLVMGetInitializer(Cur
))
1089 LLVMSetInitializer(G
, clone_constant(I
, M
));
1091 size_t NumMetadataEntries
;
1092 auto *AllMetadata
= LLVMGlobalCopyAllMetadata(Cur
, &NumMetadataEntries
);
1093 for (unsigned i
= 0; i
< NumMetadataEntries
; ++i
) {
1094 unsigned Kind
= LLVMValueMetadataEntriesGetKind(AllMetadata
, i
);
1095 LLVMMetadataRef MD
= LLVMValueMetadataEntriesGetMetadata(AllMetadata
, i
);
1096 LLVMGlobalSetMetadata(G
, Kind
, MD
);
1098 LLVMDisposeValueMetadataEntries(AllMetadata
);
1100 LLVMSetGlobalConstant(G
, LLVMIsGlobalConstant(Cur
));
1101 LLVMSetThreadLocal(G
, LLVMIsThreadLocal(Cur
));
1102 LLVMSetExternallyInitialized(G
, LLVMIsExternallyInitialized(Cur
));
1103 LLVMSetLinkage(G
, LLVMGetLinkage(Cur
));
1104 LLVMSetSection(G
, LLVMGetSection(Cur
));
1105 LLVMSetVisibility(G
, LLVMGetVisibility(Cur
));
1106 LLVMSetUnnamedAddress(G
, LLVMGetUnnamedAddress(Cur
));
1107 LLVMSetAlignment(G
, LLVMGetAlignment(Cur
));
1109 Next
= LLVMGetNextGlobal(Cur
);
1110 if (Next
== nullptr) {
1112 report_fatal_error("");
1116 LLVMValueRef Prev
= LLVMGetPreviousGlobal(Next
);
1118 report_fatal_error("Next.Previous global is not Current");
1124 Begin
= LLVMGetFirstFunction(Src
);
1125 End
= LLVMGetLastFunction(Src
);
1128 report_fatal_error("Range has an end but no beginning");
1136 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1137 LLVMValueRef Fun
= LLVMGetNamedFunction(M
, Name
);
1139 report_fatal_error("Function must have been declared already");
1141 if (LLVMHasPersonalityFn(Cur
)) {
1143 const char *FName
= LLVMGetValueName2(LLVMGetPersonalityFn(Cur
),
1145 LLVMValueRef P
= LLVMGetNamedFunction(M
, FName
);
1147 report_fatal_error("Could not find personality function");
1148 LLVMSetPersonalityFn(Fun
, P
);
1151 size_t NumMetadataEntries
;
1152 auto *AllMetadata
= LLVMGlobalCopyAllMetadata(Cur
, &NumMetadataEntries
);
1153 for (unsigned i
= 0; i
< NumMetadataEntries
; ++i
) {
1154 unsigned Kind
= LLVMValueMetadataEntriesGetKind(AllMetadata
, i
);
1155 LLVMMetadataRef MD
= LLVMValueMetadataEntriesGetMetadata(AllMetadata
, i
);
1156 LLVMGlobalSetMetadata(Fun
, Kind
, MD
);
1158 LLVMDisposeValueMetadataEntries(AllMetadata
);
1160 FunCloner
FC(Cur
, Fun
);
1163 Next
= LLVMGetNextFunction(Cur
);
1164 if (Next
== nullptr) {
1166 report_fatal_error("Last function does not match End");
1170 LLVMValueRef Prev
= LLVMGetPreviousFunction(Next
);
1172 report_fatal_error("Next.Previous function is not Current");
1178 Begin
= LLVMGetFirstGlobalAlias(Src
);
1179 End
= LLVMGetLastGlobalAlias(Src
);
1182 report_fatal_error("Range has an end but no beginning");
1183 goto GlobalIFuncClone
;
1190 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1191 LLVMValueRef Alias
= LLVMGetNamedGlobalAlias(M
, Name
, NameLen
);
1193 report_fatal_error("Global alias must have been declared already");
1195 if (LLVMValueRef Aliasee
= LLVMAliasGetAliasee(Cur
)) {
1196 LLVMAliasSetAliasee(Alias
, clone_constant(Aliasee
, M
));
1199 LLVMSetLinkage(Alias
, LLVMGetLinkage(Cur
));
1200 LLVMSetUnnamedAddress(Alias
, LLVMGetUnnamedAddress(Cur
));
1202 Next
= LLVMGetNextGlobalAlias(Cur
);
1203 if (Next
== nullptr) {
1205 report_fatal_error("Last global alias does not match End");
1209 LLVMValueRef Prev
= LLVMGetPreviousGlobalAlias(Next
);
1211 report_fatal_error("Next.Previous global alias is not Current");
1217 Begin
= LLVMGetFirstGlobalIFunc(Src
);
1218 End
= LLVMGetLastGlobalIFunc(Src
);
1221 report_fatal_error("Range has an end but no beginning");
1229 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1230 LLVMValueRef IFunc
= LLVMGetNamedGlobalIFunc(M
, Name
, NameLen
);
1232 report_fatal_error("Global ifunc must have been declared already");
1234 if (LLVMValueRef Resolver
= LLVMGetGlobalIFuncResolver(Cur
)) {
1235 LLVMSetGlobalIFuncResolver(IFunc
, clone_constant(Resolver
, M
));
1238 LLVMSetLinkage(IFunc
, LLVMGetLinkage(Cur
));
1239 LLVMSetUnnamedAddress(IFunc
, LLVMGetUnnamedAddress(Cur
));
1241 Next
= LLVMGetNextGlobalIFunc(Cur
);
1242 if (Next
== nullptr) {
1244 report_fatal_error("Last global alias does not match End");
1248 LLVMValueRef Prev
= LLVMGetPreviousGlobalIFunc(Next
);
1250 report_fatal_error("Next.Previous global alias is not Current");
1256 LLVMNamedMDNodeRef BeginMD
= LLVMGetFirstNamedMetadata(Src
);
1257 LLVMNamedMDNodeRef EndMD
= LLVMGetLastNamedMetadata(Src
);
1259 if (EndMD
!= nullptr)
1260 report_fatal_error("Range has an end but no beginning");
1264 LLVMNamedMDNodeRef CurMD
= BeginMD
;
1265 LLVMNamedMDNodeRef NextMD
= nullptr;
1268 const char *Name
= LLVMGetNamedMetadataName(CurMD
, &NameLen
);
1269 LLVMNamedMDNodeRef NamedMD
= LLVMGetNamedMetadata(M
, Name
, NameLen
);
1271 report_fatal_error("Named MD Node must have been declared already");
1273 unsigned OperandCount
= LLVMGetNamedMetadataNumOperands(Src
, Name
);
1274 LLVMValueRef
*OperandBuf
= static_cast<LLVMValueRef
*>(
1275 safe_malloc(OperandCount
* sizeof(LLVMValueRef
)));
1276 LLVMGetNamedMetadataOperands(Src
, Name
, OperandBuf
);
1277 for (unsigned i
= 0, e
= OperandCount
; i
!= e
; ++i
) {
1278 LLVMAddNamedMetadataOperand(M
, Name
, OperandBuf
[i
]);
1282 NextMD
= LLVMGetNextNamedMetadata(CurMD
);
1283 if (NextMD
== nullptr) {
1285 report_fatal_error("Last Named MD Node does not match End");
1289 LLVMNamedMDNodeRef PrevMD
= LLVMGetPreviousNamedMetadata(NextMD
);
1290 if (PrevMD
!= CurMD
)
1291 report_fatal_error("Next.Previous Named MD Node is not Current");
1297 int llvm_echo(void) {
1298 LLVMEnablePrettyStackTrace();
1300 LLVMModuleRef Src
= llvm_load_module(false, true);
1301 size_t SourceFileLen
;
1302 const char *SourceFileName
= LLVMGetSourceFileName(Src
, &SourceFileLen
);
1303 size_t ModuleIdentLen
;
1304 const char *ModuleName
= LLVMGetModuleIdentifier(Src
, &ModuleIdentLen
);
1305 LLVMContextRef Ctx
= LLVMContextCreate();
1306 LLVMModuleRef M
= LLVMModuleCreateWithNameInContext(ModuleName
, Ctx
);
1308 LLVMSetSourceFileName(M
, SourceFileName
, SourceFileLen
);
1309 LLVMSetModuleIdentifier(M
, ModuleName
, ModuleIdentLen
);
1311 LLVMSetTarget(M
, LLVMGetTarget(Src
));
1312 LLVMSetModuleDataLayout(M
, LLVMGetModuleDataLayout(Src
));
1313 if (strcmp(LLVMGetDataLayoutStr(M
), LLVMGetDataLayoutStr(Src
)))
1314 report_fatal_error("Inconsistent DataLayout string representation");
1316 size_t ModuleInlineAsmLen
;
1317 const char *ModuleAsm
= LLVMGetModuleInlineAsm(Src
, &ModuleInlineAsmLen
);
1318 LLVMSetModuleInlineAsm2(M
, ModuleAsm
, ModuleInlineAsmLen
);
1320 declare_symbols(Src
, M
);
1321 clone_symbols(Src
, M
);
1322 char *Str
= LLVMPrintModuleToString(M
);
1325 LLVMDisposeMessage(Str
);
1326 LLVMDisposeModule(Src
);
1327 LLVMDisposeModule(M
);
1328 LLVMContextDispose(Ctx
);