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
));
579 LLVMSetOrdering(Dst
, LLVMGetOrdering(Src
));
580 LLVMSetVolatile(Dst
, LLVMGetVolatile(Src
));
584 LLVMValueRef Val
= CloneValue(LLVMGetOperand(Src
, 0));
585 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 1));
586 Dst
= LLVMBuildStore(Builder
, Val
, Ptr
);
587 LLVMSetAlignment(Dst
, LLVMGetAlignment(Src
));
588 LLVMSetOrdering(Dst
, LLVMGetOrdering(Src
));
589 LLVMSetVolatile(Dst
, LLVMGetVolatile(Src
));
592 case LLVMGetElementPtr
: {
593 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 0));
594 SmallVector
<LLVMValueRef
, 8> Idx
;
595 int NumIdx
= LLVMGetNumIndices(Src
);
596 for (int i
= 1; i
<= NumIdx
; i
++)
597 Idx
.push_back(CloneValue(LLVMGetOperand(Src
, i
)));
598 if (LLVMIsInBounds(Src
))
599 Dst
= LLVMBuildInBoundsGEP(Builder
, Ptr
, Idx
.data(), NumIdx
, Name
);
601 Dst
= LLVMBuildGEP(Builder
, Ptr
, Idx
.data(), NumIdx
, Name
);
604 case LLVMAtomicRMW
: {
605 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 0));
606 LLVMValueRef Val
= CloneValue(LLVMGetOperand(Src
, 1));
607 LLVMAtomicRMWBinOp BinOp
= LLVMGetAtomicRMWBinOp(Src
);
608 LLVMAtomicOrdering Ord
= LLVMGetOrdering(Src
);
609 LLVMBool SingleThread
= LLVMIsAtomicSingleThread(Src
);
610 Dst
= LLVMBuildAtomicRMW(Builder
, BinOp
, Ptr
, Val
, Ord
, SingleThread
);
611 LLVMSetVolatile(Dst
, LLVMGetVolatile(Src
));
612 LLVMSetValueName2(Dst
, Name
, NameLen
);
615 case LLVMAtomicCmpXchg
: {
616 LLVMValueRef Ptr
= CloneValue(LLVMGetOperand(Src
, 0));
617 LLVMValueRef Cmp
= CloneValue(LLVMGetOperand(Src
, 1));
618 LLVMValueRef New
= CloneValue(LLVMGetOperand(Src
, 2));
619 LLVMAtomicOrdering Succ
= LLVMGetCmpXchgSuccessOrdering(Src
);
620 LLVMAtomicOrdering Fail
= LLVMGetCmpXchgFailureOrdering(Src
);
621 LLVMBool SingleThread
= LLVMIsAtomicSingleThread(Src
);
623 Dst
= LLVMBuildAtomicCmpXchg(Builder
, Ptr
, Cmp
, New
, Succ
, Fail
,
625 LLVMSetVolatile(Dst
, LLVMGetVolatile(Src
));
626 LLVMSetWeak(Dst
, LLVMGetWeak(Src
));
627 LLVMSetValueName2(Dst
, Name
, NameLen
);
631 LLVMValueRef V
= CloneValue(LLVMGetOperand(Src
, 0));
632 Dst
= LLVMBuildBitCast(Builder
, V
, CloneType(Src
), Name
);
636 LLVMIntPredicate Pred
= LLVMGetICmpPredicate(Src
);
637 LLVMValueRef LHS
= CloneValue(LLVMGetOperand(Src
, 0));
638 LLVMValueRef RHS
= CloneValue(LLVMGetOperand(Src
, 1));
639 Dst
= LLVMBuildICmp(Builder
, Pred
, LHS
, RHS
, Name
);
643 // We need to aggressively set things here because of loops.
644 VMap
[Src
] = Dst
= LLVMBuildPhi(Builder
, CloneType(Src
), Name
);
646 SmallVector
<LLVMValueRef
, 8> Values
;
647 SmallVector
<LLVMBasicBlockRef
, 8> Blocks
;
649 unsigned IncomingCount
= LLVMCountIncoming(Src
);
650 for (unsigned i
= 0; i
< IncomingCount
; ++i
) {
651 Blocks
.push_back(DeclareBB(LLVMGetIncomingBlock(Src
, i
)));
652 Values
.push_back(CloneValue(LLVMGetIncomingValue(Src
, i
)));
655 LLVMAddIncoming(Dst
, Values
.data(), Blocks
.data(), IncomingCount
);
659 SmallVector
<LLVMValueRef
, 8> Args
;
660 int ArgCount
= LLVMGetNumArgOperands(Src
);
661 for (int i
= 0; i
< ArgCount
; i
++)
662 Args
.push_back(CloneValue(LLVMGetOperand(Src
, i
)));
663 LLVMValueRef Fn
= CloneValue(LLVMGetCalledValue(Src
));
664 Dst
= LLVMBuildCall(Builder
, Fn
, Args
.data(), ArgCount
, Name
);
665 LLVMSetTailCall(Dst
, LLVMIsTailCall(Src
));
666 CloneAttrs(Src
, Dst
);
670 Dst
= LLVMBuildResume(Builder
, CloneValue(LLVMGetOperand(Src
, 0)));
673 case LLVMLandingPad
: {
674 // The landing pad API is a bit screwed up for historical reasons.
675 Dst
= LLVMBuildLandingPad(Builder
, CloneType(Src
), nullptr, 0, Name
);
676 unsigned NumClauses
= LLVMGetNumClauses(Src
);
677 for (unsigned i
= 0; i
< NumClauses
; ++i
)
678 LLVMAddClause(Dst
, CloneValue(LLVMGetClause(Src
, i
)));
679 LLVMSetCleanup(Dst
, LLVMIsCleanup(Src
));
682 case LLVMCleanupRet
: {
683 LLVMValueRef CatchPad
= CloneValue(LLVMGetOperand(Src
, 0));
684 LLVMBasicBlockRef Unwind
= nullptr;
685 if (LLVMBasicBlockRef UDest
= LLVMGetUnwindDest(Src
))
686 Unwind
= DeclareBB(UDest
);
687 Dst
= LLVMBuildCleanupRet(Builder
, CatchPad
, Unwind
);
691 LLVMValueRef CatchPad
= CloneValue(LLVMGetOperand(Src
, 0));
692 LLVMBasicBlockRef SuccBB
= DeclareBB(LLVMGetSuccessor(Src
, 0));
693 Dst
= LLVMBuildCatchRet(Builder
, CatchPad
, SuccBB
);
697 LLVMValueRef ParentPad
= CloneValue(LLVMGetParentCatchSwitch(Src
));
698 SmallVector
<LLVMValueRef
, 8> Args
;
699 int ArgCount
= LLVMGetNumArgOperands(Src
);
700 for (int i
= 0; i
< ArgCount
; i
++)
701 Args
.push_back(CloneValue(LLVMGetOperand(Src
, i
)));
702 Dst
= LLVMBuildCatchPad(Builder
, ParentPad
,
703 Args
.data(), ArgCount
, Name
);
706 case LLVMCleanupPad
: {
707 LLVMValueRef ParentPad
= CloneValue(LLVMGetOperand(Src
, 0));
708 SmallVector
<LLVMValueRef
, 8> Args
;
709 int ArgCount
= LLVMGetNumArgOperands(Src
);
710 for (int i
= 0; i
< ArgCount
; i
++)
711 Args
.push_back(CloneValue(LLVMGetArgOperand(Src
, i
)));
712 Dst
= LLVMBuildCleanupPad(Builder
, ParentPad
,
713 Args
.data(), ArgCount
, Name
);
716 case LLVMCatchSwitch
: {
717 LLVMValueRef ParentPad
= CloneValue(LLVMGetOperand(Src
, 0));
718 LLVMBasicBlockRef UnwindBB
= nullptr;
719 if (LLVMBasicBlockRef UDest
= LLVMGetUnwindDest(Src
)) {
720 UnwindBB
= DeclareBB(UDest
);
722 unsigned NumHandlers
= LLVMGetNumHandlers(Src
);
723 Dst
= LLVMBuildCatchSwitch(Builder
, ParentPad
, UnwindBB
, NumHandlers
, Name
);
724 if (NumHandlers
> 0) {
725 LLVMBasicBlockRef
*Handlers
= static_cast<LLVMBasicBlockRef
*>(
726 safe_malloc(NumHandlers
* sizeof(LLVMBasicBlockRef
)));
727 LLVMGetHandlers(Src
, Handlers
);
728 for (unsigned i
= 0; i
< NumHandlers
; i
++)
729 LLVMAddHandler(Dst
, DeclareBB(Handlers
[i
]));
734 case LLVMExtractValue
: {
735 LLVMValueRef Agg
= CloneValue(LLVMGetOperand(Src
, 0));
736 if (LLVMGetNumIndices(Src
) != 1)
737 report_fatal_error("Expected only one indice");
738 auto I
= LLVMGetIndices(Src
)[0];
739 Dst
= LLVMBuildExtractValue(Builder
, Agg
, I
, Name
);
742 case LLVMInsertValue
: {
743 LLVMValueRef Agg
= CloneValue(LLVMGetOperand(Src
, 0));
744 LLVMValueRef V
= CloneValue(LLVMGetOperand(Src
, 1));
745 if (LLVMGetNumIndices(Src
) != 1)
746 report_fatal_error("Expected only one indice");
747 auto I
= LLVMGetIndices(Src
)[0];
748 Dst
= LLVMBuildInsertValue(Builder
, Agg
, V
, I
, Name
);
755 if (Dst
== nullptr) {
756 fprintf(stderr
, "%d is not a supported opcode\n", Op
);
760 auto Ctx
= LLVMGetModuleContext(M
);
761 size_t NumMetadataEntries
;
763 LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src
,
764 &NumMetadataEntries
);
765 for (unsigned i
= 0; i
< NumMetadataEntries
; ++i
) {
766 unsigned Kind
= LLVMValueMetadataEntriesGetKind(AllMetadata
, i
);
767 LLVMMetadataRef MD
= LLVMValueMetadataEntriesGetMetadata(AllMetadata
, i
);
768 LLVMSetMetadata(Dst
, Kind
, LLVMMetadataAsValue(Ctx
, MD
));
770 LLVMDisposeValueMetadataEntries(AllMetadata
);
771 LLVMSetInstDebugLocation(Builder
, Dst
);
773 check_value_kind(Dst
, LLVMInstructionValueKind
);
774 return VMap
[Src
] = Dst
;
777 LLVMBasicBlockRef
DeclareBB(LLVMBasicBlockRef Src
) {
778 // Check if this is something we already computed.
780 auto i
= BBMap
.find(Src
);
781 if (i
!= BBMap
.end()) {
786 LLVMValueRef V
= LLVMBasicBlockAsValue(Src
);
787 if (!LLVMValueIsBasicBlock(V
) || LLVMValueAsBasicBlock(V
) != Src
)
788 report_fatal_error("Basic block is not a basic block");
790 const char *Name
= LLVMGetBasicBlockName(Src
);
792 const char *VName
= LLVMGetValueName2(V
, &NameLen
);
794 report_fatal_error("Basic block name mismatch");
796 LLVMBasicBlockRef BB
= LLVMAppendBasicBlock(Fun
, Name
);
797 return BBMap
[Src
] = BB
;
800 LLVMBasicBlockRef
CloneBB(LLVMBasicBlockRef Src
) {
801 LLVMBasicBlockRef BB
= DeclareBB(Src
);
803 // Make sure ordering is correct.
804 LLVMBasicBlockRef Prev
= LLVMGetPreviousBasicBlock(Src
);
806 LLVMMoveBasicBlockAfter(BB
, DeclareBB(Prev
));
808 LLVMValueRef First
= LLVMGetFirstInstruction(Src
);
809 LLVMValueRef Last
= LLVMGetLastInstruction(Src
);
811 if (First
== nullptr) {
813 report_fatal_error("Has no first instruction, but last one");
817 auto Ctx
= LLVMGetModuleContext(M
);
818 LLVMBuilderRef Builder
= LLVMCreateBuilderInContext(Ctx
);
819 LLVMPositionBuilderAtEnd(Builder
, BB
);
821 LLVMValueRef Cur
= First
;
822 LLVMValueRef Next
= nullptr;
824 CloneInstruction(Cur
, Builder
);
825 Next
= LLVMGetNextInstruction(Cur
);
826 if (Next
== nullptr) {
828 report_fatal_error("Final instruction does not match Last");
832 LLVMValueRef Prev
= LLVMGetPreviousInstruction(Next
);
834 report_fatal_error("Next.Previous instruction is not Current");
839 LLVMDisposeBuilder(Builder
);
843 void CloneBBs(LLVMValueRef Src
) {
844 unsigned Count
= LLVMCountBasicBlocks(Src
);
848 LLVMBasicBlockRef First
= LLVMGetFirstBasicBlock(Src
);
849 LLVMBasicBlockRef Last
= LLVMGetLastBasicBlock(Src
);
851 LLVMBasicBlockRef Cur
= First
;
852 LLVMBasicBlockRef Next
= nullptr;
856 Next
= LLVMGetNextBasicBlock(Cur
);
857 if (Next
== nullptr) {
859 report_fatal_error("Final basic block does not match Last");
863 LLVMBasicBlockRef Prev
= LLVMGetPreviousBasicBlock(Next
);
865 report_fatal_error("Next.Previous basic bloc is not Current");
871 report_fatal_error("Basic block count does not match iterration");
875 static void declare_symbols(LLVMModuleRef Src
, LLVMModuleRef M
) {
876 auto Ctx
= LLVMGetModuleContext(M
);
878 LLVMValueRef Begin
= LLVMGetFirstGlobal(Src
);
879 LLVMValueRef End
= LLVMGetLastGlobal(Src
);
881 LLVMValueRef Cur
= Begin
;
882 LLVMValueRef Next
= nullptr;
885 report_fatal_error("Range has an end but no beginning");
891 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
892 if (LLVMGetNamedGlobal(M
, Name
))
893 report_fatal_error("GlobalVariable already cloned");
894 LLVMAddGlobal(M
, LLVMGetElementType(TypeCloner(M
).Clone(Cur
)), Name
);
896 Next
= LLVMGetNextGlobal(Cur
);
897 if (Next
== nullptr) {
899 report_fatal_error("");
903 LLVMValueRef Prev
= LLVMGetPreviousGlobal(Next
);
905 report_fatal_error("Next.Previous global is not Current");
911 Begin
= LLVMGetFirstFunction(Src
);
912 End
= LLVMGetLastFunction(Src
);
915 report_fatal_error("Range has an end but no beginning");
923 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
924 if (LLVMGetNamedFunction(M
, Name
))
925 report_fatal_error("Function already cloned");
926 auto Ty
= LLVMGetElementType(TypeCloner(M
).Clone(Cur
));
927 auto F
= LLVMAddFunction(M
, Name
, Ty
);
930 for (int i
= LLVMAttributeFunctionIndex
, c
= LLVMCountParams(F
);
932 for (unsigned k
= 0, e
= LLVMGetLastEnumAttributeKind(); k
< e
; ++k
) {
933 if (auto SrcA
= LLVMGetEnumAttributeAtIndex(Cur
, i
, k
)) {
934 auto Val
= LLVMGetEnumAttributeValue(SrcA
);
935 auto DstA
= LLVMCreateEnumAttribute(Ctx
, k
, Val
);
936 LLVMAddAttributeAtIndex(F
, i
, DstA
);
941 Next
= LLVMGetNextFunction(Cur
);
942 if (Next
== nullptr) {
944 report_fatal_error("Last function does not match End");
948 LLVMValueRef Prev
= LLVMGetPreviousFunction(Next
);
950 report_fatal_error("Next.Previous function is not Current");
956 Begin
= LLVMGetFirstGlobalAlias(Src
);
957 End
= LLVMGetLastGlobalAlias(Src
);
960 report_fatal_error("Range has an end but no beginning");
961 goto GlobalIFuncDecl
;
968 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
969 if (LLVMGetNamedGlobalAlias(M
, Name
, NameLen
))
970 report_fatal_error("Global alias already cloned");
971 LLVMTypeRef CurType
= TypeCloner(M
).Clone(Cur
);
972 // FIXME: Allow NULL aliasee.
973 LLVMAddAlias(M
, CurType
, LLVMGetUndef(CurType
), Name
);
975 Next
= LLVMGetNextGlobalAlias(Cur
);
976 if (Next
== nullptr) {
978 report_fatal_error("");
982 LLVMValueRef Prev
= LLVMGetPreviousGlobalAlias(Next
);
984 report_fatal_error("Next.Previous global is not Current");
990 Begin
= LLVMGetFirstGlobalIFunc(Src
);
991 End
= LLVMGetLastGlobalIFunc(Src
);
994 report_fatal_error("Range has an end but no beginning");
1002 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1003 if (LLVMGetNamedGlobalIFunc(M
, Name
, NameLen
))
1004 report_fatal_error("Global ifunc already cloned");
1005 LLVMTypeRef CurType
= TypeCloner(M
).Clone(LLVMGlobalGetValueType(Cur
));
1006 // FIXME: Allow NULL resolver.
1007 LLVMAddGlobalIFunc(M
, Name
, NameLen
,
1008 CurType
, /*addressSpace*/ 0, LLVMGetUndef(CurType
));
1010 Next
= LLVMGetNextGlobalIFunc(Cur
);
1011 if (Next
== nullptr) {
1013 report_fatal_error("");
1017 LLVMValueRef Prev
= LLVMGetPreviousGlobalIFunc(Next
);
1019 report_fatal_error("Next.Previous global is not Current");
1025 LLVMNamedMDNodeRef BeginMD
= LLVMGetFirstNamedMetadata(Src
);
1026 LLVMNamedMDNodeRef EndMD
= LLVMGetLastNamedMetadata(Src
);
1028 if (EndMD
!= nullptr)
1029 report_fatal_error("Range has an end but no beginning");
1033 LLVMNamedMDNodeRef CurMD
= BeginMD
;
1034 LLVMNamedMDNodeRef NextMD
= nullptr;
1037 const char *Name
= LLVMGetNamedMetadataName(CurMD
, &NameLen
);
1038 if (LLVMGetNamedMetadata(M
, Name
, NameLen
))
1039 report_fatal_error("Named Metadata Node already cloned");
1040 LLVMGetOrInsertNamedMetadata(M
, Name
, NameLen
);
1042 NextMD
= LLVMGetNextNamedMetadata(CurMD
);
1043 if (NextMD
== nullptr) {
1045 report_fatal_error("");
1049 LLVMNamedMDNodeRef PrevMD
= LLVMGetPreviousNamedMetadata(NextMD
);
1050 if (PrevMD
!= CurMD
)
1051 report_fatal_error("Next.Previous global is not Current");
1057 static void clone_symbols(LLVMModuleRef Src
, LLVMModuleRef M
) {
1058 LLVMValueRef Begin
= LLVMGetFirstGlobal(Src
);
1059 LLVMValueRef End
= LLVMGetLastGlobal(Src
);
1061 LLVMValueRef Cur
= Begin
;
1062 LLVMValueRef Next
= nullptr;
1065 report_fatal_error("Range has an end but no beginning");
1071 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1072 LLVMValueRef G
= LLVMGetNamedGlobal(M
, Name
);
1074 report_fatal_error("GlobalVariable must have been declared already");
1076 if (auto I
= LLVMGetInitializer(Cur
))
1077 LLVMSetInitializer(G
, clone_constant(I
, M
));
1079 size_t NumMetadataEntries
;
1080 auto *AllMetadata
= LLVMGlobalCopyAllMetadata(Cur
, &NumMetadataEntries
);
1081 for (unsigned i
= 0; i
< NumMetadataEntries
; ++i
) {
1082 unsigned Kind
= LLVMValueMetadataEntriesGetKind(AllMetadata
, i
);
1083 LLVMMetadataRef MD
= LLVMValueMetadataEntriesGetMetadata(AllMetadata
, i
);
1084 LLVMGlobalSetMetadata(G
, Kind
, MD
);
1086 LLVMDisposeValueMetadataEntries(AllMetadata
);
1088 LLVMSetGlobalConstant(G
, LLVMIsGlobalConstant(Cur
));
1089 LLVMSetThreadLocal(G
, LLVMIsThreadLocal(Cur
));
1090 LLVMSetExternallyInitialized(G
, LLVMIsExternallyInitialized(Cur
));
1091 LLVMSetLinkage(G
, LLVMGetLinkage(Cur
));
1092 LLVMSetSection(G
, LLVMGetSection(Cur
));
1093 LLVMSetVisibility(G
, LLVMGetVisibility(Cur
));
1094 LLVMSetUnnamedAddress(G
, LLVMGetUnnamedAddress(Cur
));
1095 LLVMSetAlignment(G
, LLVMGetAlignment(Cur
));
1097 Next
= LLVMGetNextGlobal(Cur
);
1098 if (Next
== nullptr) {
1100 report_fatal_error("");
1104 LLVMValueRef Prev
= LLVMGetPreviousGlobal(Next
);
1106 report_fatal_error("Next.Previous global is not Current");
1112 Begin
= LLVMGetFirstFunction(Src
);
1113 End
= LLVMGetLastFunction(Src
);
1116 report_fatal_error("Range has an end but no beginning");
1124 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1125 LLVMValueRef Fun
= LLVMGetNamedFunction(M
, Name
);
1127 report_fatal_error("Function must have been declared already");
1129 if (LLVMHasPersonalityFn(Cur
)) {
1131 const char *FName
= LLVMGetValueName2(LLVMGetPersonalityFn(Cur
),
1133 LLVMValueRef P
= LLVMGetNamedFunction(M
, FName
);
1135 report_fatal_error("Could not find personality function");
1136 LLVMSetPersonalityFn(Fun
, P
);
1139 size_t NumMetadataEntries
;
1140 auto *AllMetadata
= LLVMGlobalCopyAllMetadata(Cur
, &NumMetadataEntries
);
1141 for (unsigned i
= 0; i
< NumMetadataEntries
; ++i
) {
1142 unsigned Kind
= LLVMValueMetadataEntriesGetKind(AllMetadata
, i
);
1143 LLVMMetadataRef MD
= LLVMValueMetadataEntriesGetMetadata(AllMetadata
, i
);
1144 LLVMGlobalSetMetadata(Fun
, Kind
, MD
);
1146 LLVMDisposeValueMetadataEntries(AllMetadata
);
1148 FunCloner
FC(Cur
, Fun
);
1151 Next
= LLVMGetNextFunction(Cur
);
1152 if (Next
== nullptr) {
1154 report_fatal_error("Last function does not match End");
1158 LLVMValueRef Prev
= LLVMGetPreviousFunction(Next
);
1160 report_fatal_error("Next.Previous function is not Current");
1166 Begin
= LLVMGetFirstGlobalAlias(Src
);
1167 End
= LLVMGetLastGlobalAlias(Src
);
1170 report_fatal_error("Range has an end but no beginning");
1171 goto GlobalIFuncClone
;
1178 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1179 LLVMValueRef Alias
= LLVMGetNamedGlobalAlias(M
, Name
, NameLen
);
1181 report_fatal_error("Global alias must have been declared already");
1183 if (LLVMValueRef Aliasee
= LLVMAliasGetAliasee(Cur
)) {
1184 LLVMAliasSetAliasee(Alias
, clone_constant(Aliasee
, M
));
1187 LLVMSetLinkage(Alias
, LLVMGetLinkage(Cur
));
1188 LLVMSetUnnamedAddress(Alias
, LLVMGetUnnamedAddress(Cur
));
1190 Next
= LLVMGetNextGlobalAlias(Cur
);
1191 if (Next
== nullptr) {
1193 report_fatal_error("Last global alias does not match End");
1197 LLVMValueRef Prev
= LLVMGetPreviousGlobalAlias(Next
);
1199 report_fatal_error("Next.Previous global alias is not Current");
1205 Begin
= LLVMGetFirstGlobalIFunc(Src
);
1206 End
= LLVMGetLastGlobalIFunc(Src
);
1209 report_fatal_error("Range has an end but no beginning");
1217 const char *Name
= LLVMGetValueName2(Cur
, &NameLen
);
1218 LLVMValueRef IFunc
= LLVMGetNamedGlobalIFunc(M
, Name
, NameLen
);
1220 report_fatal_error("Global ifunc must have been declared already");
1222 if (LLVMValueRef Resolver
= LLVMGetGlobalIFuncResolver(Cur
)) {
1223 LLVMSetGlobalIFuncResolver(IFunc
, clone_constant(Resolver
, M
));
1226 LLVMSetLinkage(IFunc
, LLVMGetLinkage(Cur
));
1227 LLVMSetUnnamedAddress(IFunc
, LLVMGetUnnamedAddress(Cur
));
1229 Next
= LLVMGetNextGlobalIFunc(Cur
);
1230 if (Next
== nullptr) {
1232 report_fatal_error("Last global alias does not match End");
1236 LLVMValueRef Prev
= LLVMGetPreviousGlobalIFunc(Next
);
1238 report_fatal_error("Next.Previous global alias is not Current");
1244 LLVMNamedMDNodeRef BeginMD
= LLVMGetFirstNamedMetadata(Src
);
1245 LLVMNamedMDNodeRef EndMD
= LLVMGetLastNamedMetadata(Src
);
1247 if (EndMD
!= nullptr)
1248 report_fatal_error("Range has an end but no beginning");
1252 LLVMNamedMDNodeRef CurMD
= BeginMD
;
1253 LLVMNamedMDNodeRef NextMD
= nullptr;
1256 const char *Name
= LLVMGetNamedMetadataName(CurMD
, &NameLen
);
1257 LLVMNamedMDNodeRef NamedMD
= LLVMGetNamedMetadata(M
, Name
, NameLen
);
1259 report_fatal_error("Named MD Node must have been declared already");
1261 unsigned OperandCount
= LLVMGetNamedMetadataNumOperands(Src
, Name
);
1262 LLVMValueRef
*OperandBuf
= static_cast<LLVMValueRef
*>(
1263 safe_malloc(OperandCount
* sizeof(LLVMValueRef
)));
1264 LLVMGetNamedMetadataOperands(Src
, Name
, OperandBuf
);
1265 for (unsigned i
= 0, e
= OperandCount
; i
!= e
; ++i
) {
1266 LLVMAddNamedMetadataOperand(M
, Name
, OperandBuf
[i
]);
1270 NextMD
= LLVMGetNextNamedMetadata(CurMD
);
1271 if (NextMD
== nullptr) {
1273 report_fatal_error("Last Named MD Node does not match End");
1277 LLVMNamedMDNodeRef PrevMD
= LLVMGetPreviousNamedMetadata(NextMD
);
1278 if (PrevMD
!= CurMD
)
1279 report_fatal_error("Next.Previous Named MD Node is not Current");
1285 int llvm_echo(void) {
1286 LLVMEnablePrettyStackTrace();
1288 LLVMModuleRef Src
= llvm_load_module(false, true);
1289 size_t SourceFileLen
;
1290 const char *SourceFileName
= LLVMGetSourceFileName(Src
, &SourceFileLen
);
1291 size_t ModuleIdentLen
;
1292 const char *ModuleName
= LLVMGetModuleIdentifier(Src
, &ModuleIdentLen
);
1293 LLVMContextRef Ctx
= LLVMContextCreate();
1294 LLVMModuleRef M
= LLVMModuleCreateWithNameInContext(ModuleName
, Ctx
);
1296 LLVMSetSourceFileName(M
, SourceFileName
, SourceFileLen
);
1297 LLVMSetModuleIdentifier(M
, ModuleName
, ModuleIdentLen
);
1299 LLVMSetTarget(M
, LLVMGetTarget(Src
));
1300 LLVMSetModuleDataLayout(M
, LLVMGetModuleDataLayout(Src
));
1301 if (strcmp(LLVMGetDataLayoutStr(M
), LLVMGetDataLayoutStr(Src
)))
1302 report_fatal_error("Inconsistent DataLayout string representation");
1304 size_t ModuleInlineAsmLen
;
1305 const char *ModuleAsm
= LLVMGetModuleInlineAsm(Src
, &ModuleInlineAsmLen
);
1306 LLVMSetModuleInlineAsm2(M
, ModuleAsm
, ModuleInlineAsmLen
);
1308 declare_symbols(Src
, M
);
1309 clone_symbols(Src
, M
);
1310 char *Str
= LLVMPrintModuleToString(M
);
1313 LLVMDisposeMessage(Str
);
1314 LLVMDisposeModule(Src
);
1315 LLVMDisposeModule(M
);
1316 LLVMContextDispose(Ctx
);