2016-02-05 07:26:19 +08:00
|
|
|
//===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-04-08 17:19:02 +08:00
|
|
|
// This file implements the --echo command in llvm-c-test.
|
2016-02-05 07:26:19 +08:00
|
|
|
//
|
|
|
|
// This command uses the C API to read a module and output an exact copy of it
|
|
|
|
// as output. It is used to check that the resulting module matches the input
|
|
|
|
// to validate that the C API can read and write modules properly.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c-test.h"
|
2016-02-16 13:11:24 +08:00
|
|
|
#include "llvm-c/Target.h"
|
2016-02-05 07:26:19 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2016-02-10 06:36:41 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-02-05 07:26:19 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// Provide DenseMapInfo for C API opaque types.
|
|
|
|
template<typename T>
|
|
|
|
struct CAPIDenseMap {};
|
|
|
|
|
|
|
|
// The default DenseMapInfo require to know about pointer alignement.
|
|
|
|
// Because the C API uses opaques pointer types, their alignement is unknown.
|
|
|
|
// As a result, we need to roll out our own implementation.
|
|
|
|
template<typename T>
|
|
|
|
struct CAPIDenseMap<T*> {
|
|
|
|
struct CAPIDenseMapInfo {
|
|
|
|
static inline T* getEmptyKey() {
|
|
|
|
uintptr_t Val = static_cast<uintptr_t>(-1);
|
|
|
|
return reinterpret_cast<T*>(Val);
|
|
|
|
}
|
|
|
|
static inline T* getTombstoneKey() {
|
|
|
|
uintptr_t Val = static_cast<uintptr_t>(-2);
|
|
|
|
return reinterpret_cast<T*>(Val);
|
|
|
|
}
|
|
|
|
static unsigned getHashValue(const T *PtrVal) {
|
|
|
|
return hash_value(PtrVal);
|
|
|
|
}
|
|
|
|
static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap;
|
2016-02-10 06:36:41 +08:00
|
|
|
typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap;
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-10 08:38:50 +08:00
|
|
|
struct TypeCloner {
|
|
|
|
LLVMModuleRef M;
|
|
|
|
LLVMContextRef Ctx;
|
|
|
|
|
|
|
|
TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {}
|
|
|
|
|
2016-02-14 17:30:42 +08:00
|
|
|
LLVMTypeRef Clone(LLVMValueRef Src) {
|
|
|
|
return Clone(LLVMTypeOf(Src));
|
|
|
|
}
|
|
|
|
|
2016-02-10 08:38:50 +08:00
|
|
|
LLVMTypeRef Clone(LLVMTypeRef Src) {
|
|
|
|
LLVMTypeKind Kind = LLVMGetTypeKind(Src);
|
|
|
|
switch (Kind) {
|
|
|
|
case LLVMVoidTypeKind:
|
|
|
|
return LLVMVoidTypeInContext(Ctx);
|
|
|
|
case LLVMHalfTypeKind:
|
|
|
|
return LLVMHalfTypeInContext(Ctx);
|
|
|
|
case LLVMFloatTypeKind:
|
|
|
|
return LLVMFloatTypeInContext(Ctx);
|
|
|
|
case LLVMDoubleTypeKind:
|
|
|
|
return LLVMDoubleTypeInContext(Ctx);
|
|
|
|
case LLVMX86_FP80TypeKind:
|
|
|
|
return LLVMX86FP80TypeInContext(Ctx);
|
|
|
|
case LLVMFP128TypeKind:
|
|
|
|
return LLVMFP128TypeInContext(Ctx);
|
|
|
|
case LLVMPPC_FP128TypeKind:
|
|
|
|
return LLVMPPCFP128TypeInContext(Ctx);
|
|
|
|
case LLVMLabelTypeKind:
|
|
|
|
return LLVMLabelTypeInContext(Ctx);
|
|
|
|
case LLVMIntegerTypeKind:
|
|
|
|
return LLVMIntTypeInContext(Ctx, LLVMGetIntTypeWidth(Src));
|
|
|
|
case LLVMFunctionTypeKind: {
|
|
|
|
unsigned ParamCount = LLVMCountParamTypes(Src);
|
|
|
|
LLVMTypeRef* Params = nullptr;
|
|
|
|
if (ParamCount > 0) {
|
|
|
|
Params = (LLVMTypeRef*) malloc(ParamCount * sizeof(LLVMTypeRef));
|
|
|
|
LLVMGetParamTypes(Src, Params);
|
|
|
|
for (unsigned i = 0; i < ParamCount; i++)
|
|
|
|
Params[i] = Clone(Params[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)),
|
|
|
|
Params, ParamCount,
|
|
|
|
LLVMIsFunctionVarArg(Src));
|
|
|
|
if (ParamCount > 0)
|
|
|
|
free(Params);
|
|
|
|
return FunTy;
|
|
|
|
}
|
|
|
|
case LLVMStructTypeKind: {
|
|
|
|
LLVMTypeRef S = nullptr;
|
|
|
|
const char *Name = LLVMGetStructName(Src);
|
|
|
|
if (Name) {
|
|
|
|
S = LLVMGetTypeByName(M, Name);
|
|
|
|
if (S)
|
|
|
|
return S;
|
|
|
|
S = LLVMStructCreateNamed(Ctx, Name);
|
|
|
|
if (LLVMIsOpaqueStruct(Src))
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned EltCount = LLVMCountStructElementTypes(Src);
|
|
|
|
SmallVector<LLVMTypeRef, 8> Elts;
|
|
|
|
for (unsigned i = 0; i < EltCount; i++)
|
|
|
|
Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i)));
|
|
|
|
if (Name)
|
|
|
|
LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src));
|
|
|
|
else
|
|
|
|
S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount,
|
|
|
|
LLVMIsPackedStruct(Src));
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
case LLVMArrayTypeKind:
|
|
|
|
return LLVMArrayType(
|
|
|
|
Clone(LLVMGetElementType(Src)),
|
|
|
|
LLVMGetArrayLength(Src)
|
|
|
|
);
|
|
|
|
case LLVMPointerTypeKind:
|
|
|
|
return LLVMPointerType(
|
|
|
|
Clone(LLVMGetElementType(Src)),
|
|
|
|
LLVMGetPointerAddressSpace(Src)
|
|
|
|
);
|
|
|
|
case LLVMVectorTypeKind:
|
|
|
|
return LLVMVectorType(
|
|
|
|
Clone(LLVMGetElementType(Src)),
|
|
|
|
LLVMGetVectorSize(Src)
|
|
|
|
);
|
|
|
|
case LLVMMetadataTypeKind:
|
|
|
|
break;
|
|
|
|
case LLVMX86_MMXTypeKind:
|
|
|
|
return LLVMX86MMXTypeInContext(Ctx);
|
|
|
|
default:
|
|
|
|
break;
|
2016-02-05 07:26:19 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 08:38:50 +08:00
|
|
|
fprintf(stderr, "%d is not a supported typekind\n", Kind);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
};
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
|
2016-02-14 17:14:30 +08:00
|
|
|
unsigned Count = LLVMCountParams(Src);
|
2016-02-14 18:06:34 +08:00
|
|
|
if (Count != LLVMCountParams(Dst))
|
|
|
|
report_fatal_error("Parameter count mismatch");
|
2016-02-14 17:14:30 +08:00
|
|
|
|
|
|
|
ValueMap VMap;
|
|
|
|
if (Count == 0)
|
|
|
|
return VMap;
|
|
|
|
|
|
|
|
LLVMValueRef SrcFirst = LLVMGetFirstParam(Src);
|
|
|
|
LLVMValueRef DstFirst = LLVMGetFirstParam(Dst);
|
|
|
|
LLVMValueRef SrcLast = LLVMGetLastParam(Src);
|
|
|
|
LLVMValueRef DstLast = LLVMGetLastParam(Dst);
|
|
|
|
|
|
|
|
LLVMValueRef SrcCur = SrcFirst;
|
|
|
|
LLVMValueRef DstCur = DstFirst;
|
|
|
|
LLVMValueRef SrcNext = nullptr;
|
|
|
|
LLVMValueRef DstNext = nullptr;
|
|
|
|
while (true) {
|
|
|
|
const char *Name = LLVMGetValueName(SrcCur);
|
|
|
|
LLVMSetValueName(DstCur, Name);
|
|
|
|
|
|
|
|
VMap[SrcCur] = DstCur;
|
|
|
|
|
|
|
|
Count--;
|
|
|
|
SrcNext = LLVMGetNextParam(SrcCur);
|
|
|
|
DstNext = LLVMGetNextParam(DstCur);
|
|
|
|
if (SrcNext == nullptr && DstNext == nullptr) {
|
2016-02-14 18:06:34 +08:00
|
|
|
if (SrcCur != SrcLast)
|
|
|
|
report_fatal_error("SrcLast param does not match End");
|
|
|
|
if (DstCur != DstLast)
|
|
|
|
report_fatal_error("DstLast param does not match End");
|
2016-02-14 17:14:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-02-14 18:06:34 +08:00
|
|
|
if (SrcNext == nullptr)
|
|
|
|
report_fatal_error("SrcNext was unexpectedly null");
|
|
|
|
if (DstNext == nullptr)
|
|
|
|
report_fatal_error("DstNext was unexpectedly null");
|
2016-02-14 17:14:30 +08:00
|
|
|
|
|
|
|
LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext);
|
2016-02-14 18:06:34 +08:00
|
|
|
if (SrcPrev != SrcCur)
|
|
|
|
report_fatal_error("SrcNext.Previous param is not Current");
|
2016-02-14 17:14:30 +08:00
|
|
|
|
|
|
|
LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext);
|
2016-02-14 18:06:34 +08:00
|
|
|
if (DstPrev != DstCur)
|
|
|
|
report_fatal_error("DstNext.Previous param is not Current");
|
2016-02-14 17:14:30 +08:00
|
|
|
|
|
|
|
SrcCur = SrcNext;
|
|
|
|
DstCur = DstNext;
|
|
|
|
}
|
|
|
|
|
2016-02-14 18:06:34 +08:00
|
|
|
if (Count != 0)
|
|
|
|
report_fatal_error("Parameter count does not match iteration");
|
2016-02-14 17:14:30 +08:00
|
|
|
|
|
|
|
return VMap;
|
|
|
|
}
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-04-07 13:56:20 +08:00
|
|
|
static void check_value_kind(LLVMValueRef V, LLVMValueKind K) {
|
|
|
|
if (LLVMGetValueKind(V) != K)
|
|
|
|
report_fatal_error("LLVMGetValueKind returned incorrect type");
|
|
|
|
}
|
|
|
|
|
|
|
|
static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M);
|
|
|
|
|
|
|
|
static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
|
|
|
|
LLVMValueRef Ret = clone_constant_impl(Cst, M);
|
|
|
|
check_value_kind(Ret, LLVMGetValueKind(Cst));
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
|
2016-02-14 17:30:42 +08:00
|
|
|
if (!LLVMIsAConstant(Cst))
|
|
|
|
report_fatal_error("Expected a constant");
|
|
|
|
|
|
|
|
// Maybe it is a symbol
|
|
|
|
if (LLVMIsAGlobalValue(Cst)) {
|
|
|
|
const char *Name = LLVMGetValueName(Cst);
|
|
|
|
|
|
|
|
// Try function
|
2016-02-18 06:13:33 +08:00
|
|
|
if (LLVMIsAFunction(Cst)) {
|
2016-04-07 13:56:20 +08:00
|
|
|
check_value_kind(Cst, LLVMFunctionValueKind);
|
|
|
|
LLVMValueRef Dst = LLVMGetNamedFunction(M, Name);
|
|
|
|
if (Dst)
|
|
|
|
return Dst;
|
|
|
|
report_fatal_error("Could not find function");
|
|
|
|
}
|
|
|
|
|
2016-02-14 17:30:42 +08:00
|
|
|
// Try global variable
|
2016-04-07 13:56:20 +08:00
|
|
|
if (LLVMIsAGlobalVariable(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMGlobalVariableValueKind);
|
|
|
|
LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
|
|
|
|
if (Dst)
|
|
|
|
return Dst;
|
|
|
|
report_fatal_error("Could not find function");
|
2016-02-18 06:13:33 +08:00
|
|
|
}
|
2016-04-07 13:56:20 +08:00
|
|
|
|
|
|
|
fprintf(stderr, "Could not find @%s\n", Name);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2016-02-18 06:13:33 +08:00
|
|
|
// Try integer literal
|
2016-04-07 13:56:20 +08:00
|
|
|
if (LLVMIsAConstantInt(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantIntValueKind);
|
|
|
|
return LLVMConstInt(TypeCloner(M).Clone(Cst),
|
|
|
|
LLVMConstIntGetZExtValue(Cst), false);
|
|
|
|
}
|
|
|
|
|
2016-02-18 06:13:33 +08:00
|
|
|
// Try zeroinitializer
|
2016-04-07 13:56:20 +08:00
|
|
|
if (LLVMIsAConstantAggregateZero(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantAggregateZeroValueKind);
|
|
|
|
return LLVMConstNull(TypeCloner(M).Clone(Cst));
|
|
|
|
}
|
|
|
|
|
2016-02-18 06:13:33 +08:00
|
|
|
// Try constant array
|
2016-04-07 13:56:20 +08:00
|
|
|
if (LLVMIsAConstantArray(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantArrayValueKind);
|
2016-02-18 06:13:33 +08:00
|
|
|
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
|
|
|
|
unsigned EltCount = LLVMGetArrayLength(Ty);
|
|
|
|
SmallVector<LLVMValueRef, 8> Elts;
|
|
|
|
for (unsigned i = 0; i < EltCount; i++)
|
|
|
|
Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
|
2016-04-07 13:56:20 +08:00
|
|
|
return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
|
|
|
|
}
|
|
|
|
|
2016-03-13 08:58:25 +08:00
|
|
|
// Try contant data array
|
2016-04-07 13:56:20 +08:00
|
|
|
if (LLVMIsAConstantDataArray(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantDataArrayValueKind);
|
2016-03-13 08:58:25 +08:00
|
|
|
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
|
|
|
|
unsigned EltCount = LLVMGetArrayLength(Ty);
|
|
|
|
SmallVector<LLVMValueRef, 8> Elts;
|
|
|
|
for (unsigned i = 0; i < EltCount; i++)
|
|
|
|
Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
|
2016-04-07 13:56:20 +08:00
|
|
|
return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
|
|
|
|
}
|
|
|
|
|
2016-02-18 06:13:33 +08:00
|
|
|
// Try constant struct
|
2016-04-07 13:56:20 +08:00
|
|
|
if (LLVMIsAConstantStruct(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantStructValueKind);
|
2016-02-18 06:13:33 +08:00
|
|
|
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
|
|
|
|
unsigned EltCount = LLVMCountStructElementTypes(Ty);
|
|
|
|
SmallVector<LLVMValueRef, 8> Elts;
|
|
|
|
for (unsigned i = 0; i < EltCount; i++)
|
|
|
|
Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
|
|
|
|
if (LLVMGetStructName(Ty))
|
2016-04-07 13:56:20 +08:00
|
|
|
return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
|
|
|
|
return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
|
|
|
|
EltCount, LLVMIsPackedStruct(Ty));
|
|
|
|
}
|
|
|
|
|
2016-02-14 17:30:42 +08:00
|
|
|
// Try undef
|
2016-04-07 13:56:20 +08:00
|
|
|
if (LLVMIsUndef(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMUndefValueValueKind);
|
|
|
|
return LLVMGetUndef(TypeCloner(M).Clone(Cst));
|
|
|
|
}
|
|
|
|
|
2016-02-18 07:55:59 +08:00
|
|
|
// Try float literal
|
2016-04-07 13:56:20 +08:00
|
|
|
if (LLVMIsAConstantFP(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantFPValueKind);
|
2016-02-18 07:55:59 +08:00
|
|
|
report_fatal_error("ConstantFP is not supported");
|
2016-04-07 13:56:20 +08:00
|
|
|
}
|
|
|
|
|
2016-02-16 15:33:23 +08:00
|
|
|
// This kind of constant is not supported
|
2016-04-07 13:56:20 +08:00
|
|
|
if (!LLVMIsAConstantExpr(Cst))
|
2016-02-16 15:33:23 +08:00
|
|
|
report_fatal_error("Expected a constant expression");
|
2016-04-07 13:56:20 +08:00
|
|
|
|
|
|
|
// At this point, it must be a constant expression
|
|
|
|
check_value_kind(Cst, LLVMConstantExprValueKind);
|
|
|
|
|
|
|
|
LLVMOpcode Op = LLVMGetConstOpcode(Cst);
|
|
|
|
switch(Op) {
|
|
|
|
case LLVMBitCast:
|
|
|
|
return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
|
|
|
|
TypeCloner(M).Clone(Cst));
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%d is not a supported opcode\n", Op);
|
|
|
|
exit(-1);
|
2016-04-07 06:21:29 +08:00
|
|
|
}
|
2016-02-14 17:30:42 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
struct FunCloner {
|
|
|
|
LLVMValueRef Fun;
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMModuleRef M;
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
ValueMap VMap;
|
|
|
|
BasicBlockMap BBMap;
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-10 08:38:50 +08:00
|
|
|
FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
|
|
|
|
M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
|
|
|
|
|
|
|
|
LLVMTypeRef CloneType(LLVMTypeRef Src) {
|
|
|
|
return TypeCloner(M).Clone(Src);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMTypeRef CloneType(LLVMValueRef Src) {
|
2016-02-14 17:30:42 +08:00
|
|
|
return TypeCloner(M).Clone(Src);
|
2016-02-10 08:38:50 +08:00
|
|
|
}
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
// Try to clone everything in the llvm::Value hierarchy.
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef CloneValue(LLVMValueRef Src) {
|
2016-02-10 06:36:41 +08:00
|
|
|
// First, the value may be constant.
|
2016-02-14 17:30:42 +08:00
|
|
|
if (LLVMIsAConstant(Src))
|
|
|
|
return clone_constant(Src, M);
|
2016-02-10 07:41:20 +08:00
|
|
|
|
|
|
|
// Function argument should always be in the map already.
|
2016-02-12 05:37:54 +08:00
|
|
|
auto i = VMap.find(Src);
|
|
|
|
if (i != VMap.end())
|
|
|
|
return i->second;
|
2016-02-10 06:36:41 +08:00
|
|
|
|
2016-02-12 05:37:54 +08:00
|
|
|
if (!LLVMIsAInstruction(Src))
|
|
|
|
report_fatal_error("Expected an instruction");
|
2016-02-10 06:36:41 +08:00
|
|
|
|
2016-02-12 05:37:54 +08:00
|
|
|
auto Ctx = LLVMGetModuleContext(M);
|
|
|
|
auto Builder = LLVMCreateBuilderInContext(Ctx);
|
|
|
|
auto BB = DeclareBB(LLVMGetInstructionParent(Src));
|
|
|
|
LLVMPositionBuilderAtEnd(Builder, BB);
|
|
|
|
auto Dst = CloneInstruction(Src, Builder);
|
|
|
|
LLVMDisposeBuilder(Builder);
|
|
|
|
return Dst;
|
2016-02-10 07:41:20 +08:00
|
|
|
}
|
|
|
|
|
2016-06-15 13:14:29 +08:00
|
|
|
void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) {
|
|
|
|
auto Ctx = LLVMGetModuleContext(M);
|
|
|
|
int ArgCount = LLVMGetNumArgOperands(Src);
|
|
|
|
for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) {
|
|
|
|
for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
|
|
|
|
if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) {
|
|
|
|
auto Val = LLVMGetEnumAttributeValue(SrcA);
|
|
|
|
auto A = LLVMCreateEnumAttribute(Ctx, k, Val);
|
|
|
|
LLVMAddCallSiteAttribute(Dst, i, A);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
|
2016-04-07 13:56:20 +08:00
|
|
|
check_value_kind(Src, LLVMInstructionValueKind);
|
2016-02-10 07:41:20 +08:00
|
|
|
if (!LLVMIsAInstruction(Src))
|
|
|
|
report_fatal_error("Expected an instruction");
|
|
|
|
|
2016-04-07 13:56:20 +08:00
|
|
|
const char *Name = LLVMGetValueName(Src);
|
2016-04-07 06:21:29 +08:00
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
// Check if this is something we already computed.
|
|
|
|
{
|
|
|
|
auto i = VMap.find(Src);
|
2016-02-12 05:37:54 +08:00
|
|
|
if (i != VMap.end()) {
|
|
|
|
// If we have a hit, it means we already generated the instruction
|
|
|
|
// as a dependancy to somethign else. We need to make sure
|
|
|
|
// it is ordered properly.
|
|
|
|
auto I = i->second;
|
|
|
|
LLVMInstructionRemoveFromParent(I);
|
|
|
|
LLVMInsertIntoBuilderWithName(Builder, I, Name);
|
|
|
|
return I;
|
|
|
|
}
|
2016-02-05 09:27:11 +08:00
|
|
|
}
|
2016-02-10 06:36:41 +08:00
|
|
|
|
|
|
|
// We tried everything, it must be an instruction
|
|
|
|
// that hasn't been generated already.
|
|
|
|
LLVMValueRef Dst = nullptr;
|
|
|
|
|
|
|
|
LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
|
|
|
|
switch(Op) {
|
|
|
|
case LLVMRet: {
|
|
|
|
int OpCount = LLVMGetNumOperands(Src);
|
|
|
|
if (OpCount == 0)
|
|
|
|
Dst = LLVMBuildRetVoid(Builder);
|
|
|
|
else
|
2016-02-10 07:41:20 +08:00
|
|
|
Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
|
2016-02-10 06:36:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMBr: {
|
2016-02-10 07:15:02 +08:00
|
|
|
if (!LLVMIsConditional(Src)) {
|
|
|
|
LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
|
|
|
|
LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
|
|
|
|
Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Cond = LLVMGetCondition(Src);
|
|
|
|
LLVMValueRef Else = LLVMGetOperand(Src, 1);
|
|
|
|
LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
|
|
|
|
LLVMValueRef Then = LLVMGetOperand(Src, 2);
|
|
|
|
LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
|
|
|
|
Dst = LLVMBuildCondBr(Builder, Cond, ThenBB, ElseBB);
|
2016-02-10 06:36:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMSwitch:
|
|
|
|
case LLVMIndirectBr:
|
|
|
|
break;
|
2016-02-19 04:38:32 +08:00
|
|
|
case LLVMInvoke: {
|
|
|
|
SmallVector<LLVMValueRef, 8> Args;
|
|
|
|
int ArgCount = LLVMGetNumArgOperands(Src);
|
|
|
|
for (int i = 0; i < ArgCount; i++)
|
|
|
|
Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
|
|
|
|
LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
|
|
|
|
LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
|
|
|
|
LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
|
|
|
|
Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
|
|
|
|
Then, Unwind, Name);
|
2016-06-15 13:14:29 +08:00
|
|
|
CloneAttrs(Src, Dst);
|
2016-02-19 04:38:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-02-10 06:36:41 +08:00
|
|
|
case LLVMUnreachable:
|
|
|
|
Dst = LLVMBuildUnreachable(Builder);
|
|
|
|
break;
|
|
|
|
case LLVMAdd: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMSub: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMMul: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMUDiv: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMSDiv: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMURem: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMSRem: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMShl: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMLShr: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMAShr: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMAnd: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMOr: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMXor: {
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMAlloca: {
|
2016-02-10 08:38:50 +08:00
|
|
|
LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildAlloca(Builder, Ty, Name);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-18 06:51:03 +08:00
|
|
|
case LLVMLoad: {
|
|
|
|
LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
Dst = LLVMBuildLoad(Builder, Ptr, Name);
|
|
|
|
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMStore: {
|
|
|
|
LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
|
|
|
|
Dst = LLVMBuildStore(Builder, Val, Ptr);
|
|
|
|
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMGetElementPtr: {
|
|
|
|
LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
SmallVector<LLVMValueRef, 8> Idx;
|
|
|
|
int NumIdx = LLVMGetNumIndices(Src);
|
|
|
|
for (int i = 1; i <= NumIdx; i++)
|
|
|
|
Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
|
|
|
|
if (LLVMIsInBounds(Src))
|
|
|
|
Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
|
|
|
|
else
|
|
|
|
Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
|
|
|
|
break;
|
|
|
|
}
|
2016-03-20 05:28:28 +08:00
|
|
|
case LLVMAtomicCmpXchg: {
|
|
|
|
LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
|
|
|
|
LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
|
|
|
|
LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
|
|
|
|
LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
|
|
|
|
LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
|
|
|
|
|
|
|
|
Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
|
|
|
|
SingleThread);
|
|
|
|
} break;
|
2016-02-18 07:55:59 +08:00
|
|
|
case LLVMBitCast: {
|
|
|
|
LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-10 07:15:02 +08:00
|
|
|
case LLVMICmp: {
|
|
|
|
LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
|
2016-02-10 07:41:20 +08:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 07:15:02 +08:00
|
|
|
Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-12 05:37:54 +08:00
|
|
|
case LLVMPHI: {
|
2017-03-30 20:59:53 +08:00
|
|
|
// We need to aggressively set things here because of loops.
|
2016-02-12 05:37:54 +08:00
|
|
|
VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
|
|
|
|
|
|
|
|
SmallVector<LLVMValueRef, 8> Values;
|
|
|
|
SmallVector<LLVMBasicBlockRef, 8> Blocks;
|
|
|
|
|
|
|
|
unsigned IncomingCount = LLVMCountIncoming(Src);
|
|
|
|
for (unsigned i = 0; i < IncomingCount; ++i) {
|
|
|
|
Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
|
|
|
|
Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
|
|
|
|
return Dst;
|
|
|
|
}
|
2016-02-10 06:36:41 +08:00
|
|
|
case LLVMCall: {
|
|
|
|
SmallVector<LLVMValueRef, 8> Args;
|
2016-02-10 08:09:37 +08:00
|
|
|
int ArgCount = LLVMGetNumArgOperands(Src);
|
2016-02-10 06:36:41 +08:00
|
|
|
for (int i = 0; i < ArgCount; i++)
|
2016-02-10 07:41:20 +08:00
|
|
|
Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
|
2016-02-10 08:09:37 +08:00
|
|
|
LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
|
2016-02-10 06:36:41 +08:00
|
|
|
Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
|
2016-02-19 04:38:32 +08:00
|
|
|
LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
|
2016-06-15 13:14:29 +08:00
|
|
|
CloneAttrs(Src, Dst);
|
2016-02-19 04:38:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMResume: {
|
|
|
|
Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMLandingPad: {
|
|
|
|
// The landing pad API is a bit screwed up for historical reasons.
|
|
|
|
Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
|
|
|
|
unsigned NumClauses = LLVMGetNumClauses(Src);
|
|
|
|
for (unsigned i = 0; i < NumClauses; ++i)
|
|
|
|
LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
|
|
|
|
LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
|
2016-02-10 06:36:41 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-02-10 08:38:50 +08:00
|
|
|
case LLVMExtractValue: {
|
|
|
|
LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
if (LLVMGetNumIndices(Src) != 1)
|
|
|
|
report_fatal_error("Expected only one indice");
|
|
|
|
auto I = LLVMGetIndices(Src)[0];
|
|
|
|
Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMInsertValue: {
|
|
|
|
LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
|
|
|
|
if (LLVMGetNumIndices(Src) != 1)
|
|
|
|
report_fatal_error("Expected only one indice");
|
|
|
|
auto I = LLVMGetIndices(Src)[0];
|
|
|
|
Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-10 06:36:41 +08:00
|
|
|
default:
|
|
|
|
break;
|
2016-02-05 07:26:19 +08:00
|
|
|
}
|
2016-02-10 06:36:41 +08:00
|
|
|
|
|
|
|
if (Dst == nullptr) {
|
|
|
|
fprintf(stderr, "%d is not a supported opcode\n", Op);
|
|
|
|
exit(-1);
|
2016-02-05 07:26:19 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 13:56:20 +08:00
|
|
|
check_value_kind(Dst, LLVMInstructionValueKind);
|
2016-02-10 06:36:41 +08:00
|
|
|
return VMap[Src] = Dst;
|
2016-02-05 07:26:19 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
|
|
|
|
// Check if this is something we already computed.
|
|
|
|
{
|
|
|
|
auto i = BBMap.find(Src);
|
|
|
|
if (i != BBMap.end()) {
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
LLVMValueRef V = LLVMBasicBlockAsValue(Src);
|
|
|
|
if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
|
2016-02-10 07:41:20 +08:00
|
|
|
report_fatal_error("Basic block is not a basic block");
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-14 18:06:34 +08:00
|
|
|
const char *Name = LLVMGetBasicBlockName(Src);
|
2016-02-10 06:36:41 +08:00
|
|
|
const char *VName = LLVMGetValueName(V);
|
|
|
|
if (Name != VName)
|
|
|
|
report_fatal_error("Basic block name mismatch");
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
|
|
|
|
return BBMap[Src] = BB;
|
2016-02-05 07:26:19 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
|
|
|
|
LLVMBasicBlockRef BB = DeclareBB(Src);
|
|
|
|
|
|
|
|
// Make sure ordering is correct.
|
|
|
|
LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
|
|
|
|
if (Prev)
|
|
|
|
LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
|
|
|
|
|
|
|
|
LLVMValueRef First = LLVMGetFirstInstruction(Src);
|
|
|
|
LLVMValueRef Last = LLVMGetLastInstruction(Src);
|
|
|
|
|
|
|
|
if (First == nullptr) {
|
2016-02-14 18:06:34 +08:00
|
|
|
if (Last != nullptr)
|
|
|
|
report_fatal_error("Has no first instruction, but last one");
|
2016-02-10 06:36:41 +08:00
|
|
|
return BB;
|
2016-02-05 07:26:19 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 08:38:50 +08:00
|
|
|
auto Ctx = LLVMGetModuleContext(M);
|
2016-02-10 06:36:41 +08:00
|
|
|
LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
|
|
|
|
LLVMPositionBuilderAtEnd(Builder, BB);
|
|
|
|
|
|
|
|
LLVMValueRef Cur = First;
|
|
|
|
LLVMValueRef Next = nullptr;
|
|
|
|
while(true) {
|
2016-02-10 07:41:20 +08:00
|
|
|
CloneInstruction(Cur, Builder);
|
2016-02-10 06:36:41 +08:00
|
|
|
Next = LLVMGetNextInstruction(Cur);
|
|
|
|
if (Next == nullptr) {
|
2016-02-14 18:06:34 +08:00
|
|
|
if (Cur != Last)
|
|
|
|
report_fatal_error("Final instruction does not match Last");
|
2016-02-10 06:36:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
|
2016-02-14 18:06:34 +08:00
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous instruction is not Current");
|
2016-02-10 06:36:41 +08:00
|
|
|
|
|
|
|
Cur = Next;
|
2016-02-05 07:26:19 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
LLVMDisposeBuilder(Builder);
|
|
|
|
return BB;
|
2016-02-05 07:26:19 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
void CloneBBs(LLVMValueRef Src) {
|
|
|
|
unsigned Count = LLVMCountBasicBlocks(Src);
|
|
|
|
if (Count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
|
|
|
|
LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
|
|
|
|
|
|
|
|
LLVMBasicBlockRef Cur = First;
|
|
|
|
LLVMBasicBlockRef Next = nullptr;
|
|
|
|
while(true) {
|
|
|
|
CloneBB(Cur);
|
|
|
|
Count--;
|
|
|
|
Next = LLVMGetNextBasicBlock(Cur);
|
|
|
|
if (Next == nullptr) {
|
2016-02-14 18:06:34 +08:00
|
|
|
if (Cur != Last)
|
|
|
|
report_fatal_error("Final basic block does not match Last");
|
2016-02-10 06:36:41 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
|
2016-02-14 18:06:34 +08:00
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous basic bloc is not Current");
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-10 06:36:41 +08:00
|
|
|
Cur = Next;
|
2016-02-05 07:26:19 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 18:06:34 +08:00
|
|
|
if (Count != 0)
|
|
|
|
report_fatal_error("Basic block count does not match iterration");
|
2016-02-05 07:26:19 +08:00
|
|
|
}
|
2016-02-10 06:36:41 +08:00
|
|
|
};
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
|
2016-02-18 06:13:33 +08:00
|
|
|
LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
|
|
|
|
LLVMValueRef End = LLVMGetLastGlobal(Src);
|
2016-02-18 06:30:05 +08:00
|
|
|
|
|
|
|
LLVMValueRef Cur = Begin;
|
|
|
|
LLVMValueRef Next = nullptr;
|
2016-02-18 06:13:33 +08:00
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
2017-07-13 14:48:39 +08:00
|
|
|
report_fatal_error("Range has an end but no beginning");
|
2016-02-18 06:30:05 +08:00
|
|
|
goto FunDecl;
|
2016-02-18 06:13:33 +08:00
|
|
|
}
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-18 06:13:33 +08:00
|
|
|
while (true) {
|
|
|
|
const char *Name = LLVMGetValueName(Cur);
|
|
|
|
if (LLVMGetNamedGlobal(M, Name))
|
|
|
|
report_fatal_error("GlobalVariable already cloned");
|
|
|
|
LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
|
2016-02-16 15:08:49 +08:00
|
|
|
|
2016-02-18 06:13:33 +08:00
|
|
|
Next = LLVMGetNextGlobal(Cur);
|
|
|
|
if (Next == nullptr) {
|
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("");
|
|
|
|
break;
|
|
|
|
}
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-18 06:13:33 +08:00
|
|
|
LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
|
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous global is not Current");
|
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-18 06:30:05 +08:00
|
|
|
FunDecl:
|
2016-02-18 06:13:33 +08:00
|
|
|
Begin = LLVMGetFirstFunction(Src);
|
|
|
|
End = LLVMGetLastFunction(Src);
|
2016-02-16 16:37:01 +08:00
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
2017-07-13 14:48:39 +08:00
|
|
|
report_fatal_error("Range has an end but no beginning");
|
2016-02-16 16:37:01 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-06-12 14:17:24 +08:00
|
|
|
auto Ctx = LLVMGetModuleContext(M);
|
|
|
|
|
2016-02-18 06:13:33 +08:00
|
|
|
Cur = Begin;
|
|
|
|
Next = nullptr;
|
2016-02-05 07:26:19 +08:00
|
|
|
while (true) {
|
2016-02-18 06:13:33 +08:00
|
|
|
const char *Name = LLVMGetValueName(Cur);
|
|
|
|
if (LLVMGetNamedFunction(M, Name))
|
|
|
|
report_fatal_error("Function already cloned");
|
2016-06-12 14:17:24 +08:00
|
|
|
auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur));
|
|
|
|
auto F = LLVMAddFunction(M, Name, Ty);
|
|
|
|
|
|
|
|
// Copy attributes
|
|
|
|
for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F);
|
|
|
|
i <= c; ++i) {
|
|
|
|
for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
|
|
|
|
if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) {
|
|
|
|
auto Val = LLVMGetEnumAttributeValue(SrcA);
|
|
|
|
auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val);
|
|
|
|
LLVMAddAttributeAtIndex(F, i, DstA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 06:13:33 +08:00
|
|
|
|
2016-02-16 15:08:49 +08:00
|
|
|
Next = LLVMGetNextFunction(Cur);
|
|
|
|
if (Next == nullptr) {
|
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("Last function does not match End");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
|
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous function is not Current");
|
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
2016-02-18 06:13:33 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
|
2016-02-18 06:13:33 +08:00
|
|
|
LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
|
|
|
|
LLVMValueRef End = LLVMGetLastGlobal(Src);
|
2016-02-18 06:30:05 +08:00
|
|
|
|
|
|
|
LLVMValueRef Cur = Begin;
|
|
|
|
LLVMValueRef Next = nullptr;
|
2016-02-18 06:13:33 +08:00
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
2017-07-13 14:48:39 +08:00
|
|
|
report_fatal_error("Range has an end but no beginning");
|
2016-02-18 06:30:05 +08:00
|
|
|
goto FunClone;
|
2016-02-18 06:13:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const char *Name = LLVMGetValueName(Cur);
|
|
|
|
LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
|
|
|
|
if (!G)
|
|
|
|
report_fatal_error("GlobalVariable must have been declared already");
|
|
|
|
|
|
|
|
if (auto I = LLVMGetInitializer(Cur))
|
|
|
|
LLVMSetInitializer(G, clone_constant(I, M));
|
|
|
|
|
|
|
|
LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
|
|
|
|
LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
|
|
|
|
LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
|
|
|
|
LLVMSetLinkage(G, LLVMGetLinkage(Cur));
|
|
|
|
LLVMSetSection(G, LLVMGetSection(Cur));
|
|
|
|
LLVMSetVisibility(G, LLVMGetVisibility(Cur));
|
|
|
|
LLVMSetUnnamedAddr(G, LLVMHasUnnamedAddr(Cur));
|
|
|
|
LLVMSetAlignment(G, LLVMGetAlignment(Cur));
|
|
|
|
|
|
|
|
Next = LLVMGetNextGlobal(Cur);
|
|
|
|
if (Next == nullptr) {
|
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
|
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous global is not Current");
|
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
|
|
|
|
2016-02-18 06:30:05 +08:00
|
|
|
FunClone:
|
2016-02-18 06:13:33 +08:00
|
|
|
Begin = LLVMGetFirstFunction(Src);
|
|
|
|
End = LLVMGetLastFunction(Src);
|
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
2017-07-13 14:48:39 +08:00
|
|
|
report_fatal_error("Range has an end but no beginning");
|
2016-02-18 06:13:33 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-02-16 15:08:49 +08:00
|
|
|
|
|
|
|
Cur = Begin;
|
|
|
|
Next = nullptr;
|
|
|
|
while (true) {
|
2016-02-18 06:13:33 +08:00
|
|
|
const char *Name = LLVMGetValueName(Cur);
|
|
|
|
LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
|
|
|
|
if (!Fun)
|
|
|
|
report_fatal_error("Function must have been declared already");
|
2016-02-19 04:38:32 +08:00
|
|
|
|
|
|
|
if (LLVMHasPersonalityFn(Cur)) {
|
|
|
|
const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur));
|
|
|
|
LLVMValueRef P = LLVMGetNamedFunction(M, FName);
|
|
|
|
if (!P)
|
|
|
|
report_fatal_error("Could not find personality function");
|
|
|
|
LLVMSetPersonalityFn(Fun, P);
|
|
|
|
}
|
|
|
|
|
2016-02-18 06:13:33 +08:00
|
|
|
FunCloner FC(Cur, Fun);
|
|
|
|
FC.CloneBBs(Cur);
|
|
|
|
|
2016-02-05 07:26:19 +08:00
|
|
|
Next = LLVMGetNextFunction(Cur);
|
|
|
|
if (Next == nullptr) {
|
2016-02-14 18:06:34 +08:00
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("Last function does not match End");
|
2016-02-05 07:26:19 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
|
2016-02-14 18:06:34 +08:00
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous function is not Current");
|
2016-02-05 07:26:19 +08:00
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 21:31:14 +08:00
|
|
|
int llvm_echo(void) {
|
2016-02-05 07:26:19 +08:00
|
|
|
LLVMEnablePrettyStackTrace();
|
|
|
|
|
2016-02-05 21:31:14 +08:00
|
|
|
LLVMModuleRef Src = llvm_load_module(false, true);
|
2016-04-05 21:56:59 +08:00
|
|
|
size_t Len;
|
|
|
|
const char *ModuleName = LLVMGetModuleIdentifier(Src, &Len);
|
2016-02-05 07:26:19 +08:00
|
|
|
LLVMContextRef Ctx = LLVMContextCreate();
|
2016-04-05 21:56:59 +08:00
|
|
|
LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
|
|
|
|
|
|
|
|
// This whole switcharound is done because the C API has no way to
|
|
|
|
// set the source_filename
|
|
|
|
LLVMSetModuleIdentifier(M, "", 0);
|
|
|
|
LLVMGetModuleIdentifier(M, &Len);
|
|
|
|
if (Len != 0)
|
|
|
|
report_fatal_error("LLVM{Set,Get}ModuleIdentifier failed");
|
|
|
|
LLVMSetModuleIdentifier(M, ModuleName, strlen(ModuleName));
|
2016-02-05 07:26:19 +08:00
|
|
|
|
2016-02-16 13:11:24 +08:00
|
|
|
LLVMSetTarget(M, LLVMGetTarget(Src));
|
|
|
|
LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
|
|
|
|
if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
|
|
|
|
report_fatal_error("Inconsistent DataLayout string representation");
|
|
|
|
|
2016-02-18 06:13:33 +08:00
|
|
|
declare_symbols(Src, M);
|
|
|
|
clone_symbols(Src, M);
|
2016-02-14 17:30:42 +08:00
|
|
|
char *Str = LLVMPrintModuleToString(M);
|
2016-02-05 07:26:19 +08:00
|
|
|
fputs(Str, stdout);
|
|
|
|
|
|
|
|
LLVMDisposeMessage(Str);
|
2016-02-14 17:30:42 +08:00
|
|
|
LLVMDisposeModule(M);
|
2016-02-05 07:26:19 +08:00
|
|
|
LLVMContextDispose(Ctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|