Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1 | //===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Jeroen Ketema | ad659c3 | 2016-04-08 09:19:02 +0000 | [diff] [blame] | 9 | // This file implements the --echo command in llvm-c-test. |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 10 | // |
| 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. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm-c-test.h" |
Robert Widmann | 9cba4ec | 2018-09-28 15:35:18 +0000 | [diff] [blame] | 18 | #include "llvm-c/DebugInfo.h" |
Amaury Sechet | 5590967 | 2016-02-16 05:11:24 +0000 | [diff] [blame] | 19 | #include "llvm-c/Target.h" |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 22 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | // Provide DenseMapInfo for C API opaque types. |
| 29 | template<typename T> |
| 30 | struct CAPIDenseMap {}; |
| 31 | |
| 32 | // The default DenseMapInfo require to know about pointer alignement. |
| 33 | // Because the C API uses opaques pointer types, their alignement is unknown. |
| 34 | // As a result, we need to roll out our own implementation. |
| 35 | template<typename T> |
| 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); |
| 41 | } |
| 42 | static inline T* getTombstoneKey() { |
| 43 | uintptr_t Val = static_cast<uintptr_t>(-2); |
| 44 | return reinterpret_cast<T*>(Val); |
| 45 | } |
| 46 | static unsigned getHashValue(const T *PtrVal) { |
| 47 | return hash_value(PtrVal); |
| 48 | } |
| 49 | static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; } |
| 50 | }; |
| 51 | |
| 52 | typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map; |
| 53 | }; |
| 54 | |
| 55 | typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap; |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 56 | typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 57 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 58 | struct TypeCloner { |
| 59 | LLVMModuleRef M; |
| 60 | LLVMContextRef Ctx; |
| 61 | |
| 62 | TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {} |
| 63 | |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 64 | LLVMTypeRef Clone(LLVMValueRef Src) { |
| 65 | return Clone(LLVMTypeOf(Src)); |
| 66 | } |
| 67 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 68 | LLVMTypeRef Clone(LLVMTypeRef Src) { |
| 69 | LLVMTypeKind Kind = LLVMGetTypeKind(Src); |
| 70 | switch (Kind) { |
| 71 | case LLVMVoidTypeKind: |
| 72 | return LLVMVoidTypeInContext(Ctx); |
| 73 | case LLVMHalfTypeKind: |
| 74 | return LLVMHalfTypeInContext(Ctx); |
| 75 | case LLVMFloatTypeKind: |
| 76 | return LLVMFloatTypeInContext(Ctx); |
| 77 | case LLVMDoubleTypeKind: |
| 78 | return LLVMDoubleTypeInContext(Ctx); |
| 79 | case LLVMX86_FP80TypeKind: |
| 80 | return LLVMX86FP80TypeInContext(Ctx); |
| 81 | case LLVMFP128TypeKind: |
| 82 | return LLVMFP128TypeInContext(Ctx); |
| 83 | case LLVMPPC_FP128TypeKind: |
| 84 | return LLVMPPCFP128TypeInContext(Ctx); |
| 85 | case LLVMLabelTypeKind: |
| 86 | return LLVMLabelTypeInContext(Ctx); |
| 87 | case LLVMIntegerTypeKind: |
| 88 | return LLVMIntTypeInContext(Ctx, LLVMGetIntTypeWidth(Src)); |
| 89 | case LLVMFunctionTypeKind: { |
| 90 | unsigned ParamCount = LLVMCountParamTypes(Src); |
| 91 | LLVMTypeRef* Params = nullptr; |
| 92 | if (ParamCount > 0) { |
Serge Pavlov | 76d8cce | 2018-02-20 05:41:26 +0000 | [diff] [blame] | 93 | Params = static_cast<LLVMTypeRef*>( |
| 94 | safe_malloc(ParamCount * sizeof(LLVMTypeRef))); |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 95 | LLVMGetParamTypes(Src, Params); |
| 96 | for (unsigned i = 0; i < ParamCount; i++) |
| 97 | Params[i] = Clone(Params[i]); |
| 98 | } |
| 99 | |
| 100 | LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)), |
| 101 | Params, ParamCount, |
| 102 | LLVMIsFunctionVarArg(Src)); |
| 103 | if (ParamCount > 0) |
| 104 | free(Params); |
| 105 | return FunTy; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 106 | } |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 107 | case LLVMStructTypeKind: { |
| 108 | LLVMTypeRef S = nullptr; |
| 109 | const char *Name = LLVMGetStructName(Src); |
| 110 | if (Name) { |
| 111 | S = LLVMGetTypeByName(M, Name); |
| 112 | if (S) |
| 113 | return S; |
| 114 | S = LLVMStructCreateNamed(Ctx, Name); |
| 115 | if (LLVMIsOpaqueStruct(Src)) |
| 116 | return S; |
| 117 | } |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 118 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 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))); |
| 123 | if (Name) |
| 124 | LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src)); |
| 125 | else |
| 126 | S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount, |
| 127 | LLVMIsPackedStruct(Src)); |
| 128 | return S; |
| 129 | } |
| 130 | case LLVMArrayTypeKind: |
| 131 | return LLVMArrayType( |
| 132 | Clone(LLVMGetElementType(Src)), |
| 133 | LLVMGetArrayLength(Src) |
| 134 | ); |
| 135 | case LLVMPointerTypeKind: |
| 136 | return LLVMPointerType( |
| 137 | Clone(LLVMGetElementType(Src)), |
| 138 | LLVMGetPointerAddressSpace(Src) |
| 139 | ); |
| 140 | case LLVMVectorTypeKind: |
| 141 | return LLVMVectorType( |
| 142 | Clone(LLVMGetElementType(Src)), |
| 143 | LLVMGetVectorSize(Src) |
| 144 | ); |
| 145 | case LLVMMetadataTypeKind: |
whitequark | 131f98f | 2017-10-27 11:51:40 +0000 | [diff] [blame] | 146 | return LLVMMetadataTypeInContext(Ctx); |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 147 | case LLVMX86_MMXTypeKind: |
| 148 | return LLVMX86MMXTypeInContext(Ctx); |
Robert Widmann | 478fce9 | 2018-03-30 17:49:53 +0000 | [diff] [blame] | 149 | case LLVMTokenTypeKind: |
| 150 | return LLVMTokenTypeInContext(Ctx); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 151 | } |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 152 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 153 | fprintf(stderr, "%d is not a supported typekind\n", Kind); |
| 154 | exit(-1); |
| 155 | } |
| 156 | }; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 157 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 158 | static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) { |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 159 | unsigned Count = LLVMCountParams(Src); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 160 | if (Count != LLVMCountParams(Dst)) |
| 161 | report_fatal_error("Parameter count mismatch"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 162 | |
| 163 | ValueMap VMap; |
| 164 | if (Count == 0) |
| 165 | return VMap; |
| 166 | |
| 167 | LLVMValueRef SrcFirst = LLVMGetFirstParam(Src); |
| 168 | LLVMValueRef DstFirst = LLVMGetFirstParam(Dst); |
| 169 | LLVMValueRef SrcLast = LLVMGetLastParam(Src); |
| 170 | LLVMValueRef DstLast = LLVMGetLastParam(Dst); |
| 171 | |
| 172 | LLVMValueRef SrcCur = SrcFirst; |
| 173 | LLVMValueRef DstCur = DstFirst; |
| 174 | LLVMValueRef SrcNext = nullptr; |
| 175 | LLVMValueRef DstNext = nullptr; |
| 176 | while (true) { |
Robert Widmann | 025c78f | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 177 | size_t NameLen; |
| 178 | const char *Name = LLVMGetValueName2(SrcCur, &NameLen); |
| 179 | LLVMSetValueName2(DstCur, Name, NameLen); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 180 | |
| 181 | VMap[SrcCur] = DstCur; |
| 182 | |
| 183 | Count--; |
| 184 | SrcNext = LLVMGetNextParam(SrcCur); |
| 185 | DstNext = LLVMGetNextParam(DstCur); |
| 186 | if (SrcNext == nullptr && DstNext == nullptr) { |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 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"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 194 | if (SrcNext == nullptr) |
| 195 | report_fatal_error("SrcNext was unexpectedly null"); |
| 196 | if (DstNext == nullptr) |
| 197 | report_fatal_error("DstNext was unexpectedly null"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 198 | |
| 199 | LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 200 | if (SrcPrev != SrcCur) |
| 201 | report_fatal_error("SrcNext.Previous param is not Current"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 202 | |
| 203 | LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 204 | if (DstPrev != DstCur) |
| 205 | report_fatal_error("DstNext.Previous param is not Current"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 206 | |
| 207 | SrcCur = SrcNext; |
| 208 | DstCur = DstNext; |
| 209 | } |
| 210 | |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 211 | if (Count != 0) |
| 212 | report_fatal_error("Parameter count does not match iteration"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 213 | |
| 214 | return VMap; |
| 215 | } |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 216 | |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 217 | static void check_value_kind(LLVMValueRef V, LLVMValueKind K) { |
| 218 | if (LLVMGetValueKind(V) != K) |
| 219 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
| 220 | } |
| 221 | |
| 222 | static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M); |
| 223 | |
| 224 | static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) { |
| 225 | LLVMValueRef Ret = clone_constant_impl(Cst, M); |
| 226 | check_value_kind(Ret, LLVMGetValueKind(Cst)); |
| 227 | return Ret; |
| 228 | } |
| 229 | |
| 230 | static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) { |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 231 | if (!LLVMIsAConstant(Cst)) |
| 232 | report_fatal_error("Expected a constant"); |
| 233 | |
| 234 | // Maybe it is a symbol |
| 235 | if (LLVMIsAGlobalValue(Cst)) { |
Robert Widmann | 025c78f | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 236 | size_t NameLen; |
| 237 | const char *Name = LLVMGetValueName2(Cst, &NameLen); |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 238 | |
| 239 | // Try function |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 240 | if (LLVMIsAFunction(Cst)) { |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 241 | check_value_kind(Cst, LLVMFunctionValueKind); |
Robert Widmann | d36f3b0 | 2018-11-06 01:38:14 +0000 | [diff] [blame] | 242 | |
| 243 | LLVMValueRef Dst = nullptr; |
| 244 | // Try an intrinsic |
| 245 | unsigned ID = LLVMGetIntrinsicID(Cst); |
| 246 | if (ID > 0 && !LLVMIntrinsicIsOverloaded(ID)) { |
| 247 | Dst = LLVMGetIntrinsicDeclaration(M, ID, nullptr, 0); |
| 248 | } else { |
| 249 | // Try a normal function |
| 250 | Dst = LLVMGetNamedFunction(M, Name); |
| 251 | } |
| 252 | |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 253 | if (Dst) |
| 254 | return Dst; |
| 255 | report_fatal_error("Could not find function"); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 256 | } |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 257 | |
| 258 | // Try global variable |
| 259 | if (LLVMIsAGlobalVariable(Cst)) { |
| 260 | check_value_kind(Cst, LLVMGlobalVariableValueKind); |
Robert Widmann | 360d6e3 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 261 | LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name); |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 262 | if (Dst) |
| 263 | return Dst; |
Robert Widmann | 360d6e3 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 264 | report_fatal_error("Could not find variable"); |
| 265 | } |
| 266 | |
| 267 | // Try global alias |
| 268 | if (LLVMIsAGlobalAlias(Cst)) { |
| 269 | check_value_kind(Cst, LLVMGlobalAliasValueKind); |
| 270 | LLVMValueRef Dst = LLVMGetNamedGlobalAlias(M, Name, NameLen); |
| 271 | if (Dst) |
| 272 | return Dst; |
| 273 | report_fatal_error("Could not find alias"); |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | fprintf(stderr, "Could not find @%s\n", Name); |
| 277 | exit(-1); |
| 278 | } |
| 279 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 280 | // Try integer literal |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 281 | if (LLVMIsAConstantInt(Cst)) { |
| 282 | check_value_kind(Cst, LLVMConstantIntValueKind); |
| 283 | return LLVMConstInt(TypeCloner(M).Clone(Cst), |
| 284 | LLVMConstIntGetZExtValue(Cst), false); |
| 285 | } |
| 286 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 287 | // Try zeroinitializer |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 288 | if (LLVMIsAConstantAggregateZero(Cst)) { |
| 289 | check_value_kind(Cst, LLVMConstantAggregateZeroValueKind); |
| 290 | return LLVMConstNull(TypeCloner(M).Clone(Cst)); |
| 291 | } |
| 292 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 293 | // Try constant array |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 294 | if (LLVMIsAConstantArray(Cst)) { |
| 295 | check_value_kind(Cst, LLVMConstantArrayValueKind); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 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)); |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 301 | return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount); |
| 302 | } |
| 303 | |
Amaury Sechet | b325686 | 2016-03-13 00:58:25 +0000 | [diff] [blame] | 304 | // Try contant data array |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 305 | if (LLVMIsAConstantDataArray(Cst)) { |
| 306 | check_value_kind(Cst, LLVMConstantDataArrayValueKind); |
Amaury Sechet | b325686 | 2016-03-13 00:58:25 +0000 | [diff] [blame] | 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)); |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 312 | return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount); |
| 313 | } |
| 314 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 315 | // Try constant struct |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 316 | if (LLVMIsAConstantStruct(Cst)) { |
| 317 | check_value_kind(Cst, LLVMConstantStructValueKind); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 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)) |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 324 | return LLVMConstNamedStruct(Ty, Elts.data(), EltCount); |
| 325 | return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(), |
| 326 | EltCount, LLVMIsPackedStruct(Ty)); |
| 327 | } |
| 328 | |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 329 | // Try undef |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 330 | if (LLVMIsUndef(Cst)) { |
| 331 | check_value_kind(Cst, LLVMUndefValueValueKind); |
| 332 | return LLVMGetUndef(TypeCloner(M).Clone(Cst)); |
| 333 | } |
| 334 | |
Robert Widmann | 478fce9 | 2018-03-30 17:49:53 +0000 | [diff] [blame] | 335 | // Try null |
| 336 | if (LLVMIsNull(Cst)) { |
| 337 | check_value_kind(Cst, LLVMConstantTokenNoneValueKind); |
| 338 | LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); |
| 339 | return LLVMConstNull(Ty); |
| 340 | } |
| 341 | |
Amaury Sechet | 40bbe51 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 342 | // Try float literal |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 343 | if (LLVMIsAConstantFP(Cst)) { |
| 344 | check_value_kind(Cst, LLVMConstantFPValueKind); |
Amaury Sechet | 40bbe51 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 345 | report_fatal_error("ConstantFP is not supported"); |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Amaury Sechet | 6b16c23 | 2016-02-16 07:33:23 +0000 | [diff] [blame] | 348 | // This kind of constant is not supported |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 349 | if (!LLVMIsAConstantExpr(Cst)) |
Amaury Sechet | 6b16c23 | 2016-02-16 07:33:23 +0000 | [diff] [blame] | 350 | report_fatal_error("Expected a constant expression"); |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 351 | |
| 352 | // At this point, it must be a constant expression |
| 353 | check_value_kind(Cst, LLVMConstantExprValueKind); |
| 354 | |
| 355 | LLVMOpcode Op = LLVMGetConstOpcode(Cst); |
| 356 | switch(Op) { |
| 357 | case LLVMBitCast: |
| 358 | return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M), |
| 359 | TypeCloner(M).Clone(Cst)); |
| 360 | default: |
| 361 | fprintf(stderr, "%d is not a supported opcode\n", Op); |
| 362 | exit(-1); |
Amaury Sechet | 40bbe51 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 363 | } |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 366 | struct FunCloner { |
| 367 | LLVMValueRef Fun; |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 368 | LLVMModuleRef M; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 369 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 370 | ValueMap VMap; |
| 371 | BasicBlockMap BBMap; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 372 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 373 | FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst), |
| 374 | M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {} |
| 375 | |
| 376 | LLVMTypeRef CloneType(LLVMTypeRef Src) { |
| 377 | return TypeCloner(M).Clone(Src); |
| 378 | } |
| 379 | |
| 380 | LLVMTypeRef CloneType(LLVMValueRef Src) { |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 381 | return TypeCloner(M).Clone(Src); |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 382 | } |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 383 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 384 | // Try to clone everything in the llvm::Value hierarchy. |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 385 | LLVMValueRef CloneValue(LLVMValueRef Src) { |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 386 | // First, the value may be constant. |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 387 | if (LLVMIsAConstant(Src)) |
| 388 | return clone_constant(Src, M); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 389 | |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 390 | // Function argument should always be in the map already. |
Amaury Sechet | 2f43208 | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 391 | auto i = VMap.find(Src); |
| 392 | if (i != VMap.end()) |
| 393 | return i->second; |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 394 | |
Amaury Sechet | 2f43208 | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 395 | if (!LLVMIsAInstruction(Src)) |
| 396 | report_fatal_error("Expected an instruction"); |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 397 | |
Amaury Sechet | 2f43208 | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 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); |
| 404 | return Dst; |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Amaury Sechet | a65a237 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 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); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 421 | LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) { |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 422 | check_value_kind(Src, LLVMInstructionValueKind); |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 423 | if (!LLVMIsAInstruction(Src)) |
| 424 | report_fatal_error("Expected an instruction"); |
| 425 | |
Robert Widmann | 025c78f | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 426 | size_t NameLen; |
| 427 | const char *Name = LLVMGetValueName2(Src, &NameLen); |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame] | 428 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 429 | // Check if this is something we already computed. |
| 430 | { |
| 431 | auto i = VMap.find(Src); |
Amaury Sechet | 2f43208 | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 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. |
| 436 | auto I = i->second; |
| 437 | LLVMInstructionRemoveFromParent(I); |
| 438 | LLVMInsertIntoBuilderWithName(Builder, I, Name); |
| 439 | return I; |
| 440 | } |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // We tried everything, it must be an instruction |
| 444 | // that hasn't been generated already. |
| 445 | LLVMValueRef Dst = nullptr; |
| 446 | |
| 447 | LLVMOpcode Op = LLVMGetInstructionOpcode(Src); |
| 448 | switch(Op) { |
| 449 | case LLVMRet: { |
| 450 | int OpCount = LLVMGetNumOperands(Src); |
| 451 | if (OpCount == 0) |
| 452 | Dst = LLVMBuildRetVoid(Builder); |
| 453 | else |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 454 | Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0))); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | case LLVMBr: { |
Amaury Sechet | e7e6217 | 2016-02-09 23:15:02 +0000 | [diff] [blame] | 458 | if (!LLVMIsConditional(Src)) { |
| 459 | LLVMValueRef SrcOp = LLVMGetOperand(Src, 0); |
| 460 | LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp); |
| 461 | Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB)); |
| 462 | break; |
| 463 | } |
| 464 | |
| 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)); |
Rafael Espindola | 92f3578 | 2018-02-21 19:55:11 +0000 | [diff] [blame] | 470 | Dst = LLVMBuildCondBr(Builder, CloneValue(Cond), ThenBB, ElseBB); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 471 | break; |
| 472 | } |
| 473 | case LLVMSwitch: |
| 474 | case LLVMIndirectBr: |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 475 | break; |
Amaury Sechet | e39e853 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 476 | case LLVMInvoke: { |
| 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, |
| 485 | Then, Unwind, Name); |
Amaury Sechet | a65a237 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 486 | CloneAttrs(Src, Dst); |
Amaury Sechet | e39e853 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 487 | break; |
| 488 | } |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 489 | case LLVMUnreachable: |
| 490 | Dst = LLVMBuildUnreachable(Builder); |
| 491 | break; |
| 492 | case LLVMAdd: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 493 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 494 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 495 | Dst = LLVMBuildAdd(Builder, LHS, RHS, Name); |
| 496 | break; |
| 497 | } |
| 498 | case LLVMSub: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 499 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 500 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 501 | Dst = LLVMBuildSub(Builder, LHS, RHS, Name); |
| 502 | break; |
| 503 | } |
| 504 | case LLVMMul: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 505 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 506 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 507 | Dst = LLVMBuildMul(Builder, LHS, RHS, Name); |
| 508 | break; |
| 509 | } |
| 510 | case LLVMUDiv: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 511 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 512 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 513 | Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name); |
| 514 | break; |
| 515 | } |
| 516 | case LLVMSDiv: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 517 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 518 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 519 | Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name); |
| 520 | break; |
| 521 | } |
| 522 | case LLVMURem: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 523 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 524 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 525 | Dst = LLVMBuildURem(Builder, LHS, RHS, Name); |
| 526 | break; |
| 527 | } |
| 528 | case LLVMSRem: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 529 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 530 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 531 | Dst = LLVMBuildSRem(Builder, LHS, RHS, Name); |
| 532 | break; |
| 533 | } |
| 534 | case LLVMShl: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 535 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 536 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 537 | Dst = LLVMBuildShl(Builder, LHS, RHS, Name); |
| 538 | break; |
| 539 | } |
| 540 | case LLVMLShr: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 541 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 542 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 543 | Dst = LLVMBuildLShr(Builder, LHS, RHS, Name); |
| 544 | break; |
| 545 | } |
| 546 | case LLVMAShr: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 547 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 548 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 549 | Dst = LLVMBuildAShr(Builder, LHS, RHS, Name); |
| 550 | break; |
| 551 | } |
| 552 | case LLVMAnd: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 553 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 554 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 555 | Dst = LLVMBuildAnd(Builder, LHS, RHS, Name); |
| 556 | break; |
| 557 | } |
| 558 | case LLVMOr: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 559 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 560 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 561 | Dst = LLVMBuildOr(Builder, LHS, RHS, Name); |
| 562 | break; |
| 563 | } |
| 564 | case LLVMXor: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 565 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 566 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 567 | Dst = LLVMBuildXor(Builder, LHS, RHS, Name); |
| 568 | break; |
| 569 | } |
| 570 | case LLVMAlloca: { |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 571 | LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 572 | Dst = LLVMBuildAlloca(Builder, Ty, Name); |
| 573 | break; |
| 574 | } |
Amaury Sechet | 053ac45 | 2016-02-17 22:51:03 +0000 | [diff] [blame] | 575 | case LLVMLoad: { |
| 576 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); |
| 577 | Dst = LLVMBuildLoad(Builder, Ptr, Name); |
| 578 | LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); |
| 579 | break; |
| 580 | } |
| 581 | case LLVMStore: { |
| 582 | LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0)); |
| 583 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1)); |
| 584 | Dst = LLVMBuildStore(Builder, Val, Ptr); |
| 585 | LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); |
| 586 | break; |
| 587 | } |
| 588 | case LLVMGetElementPtr: { |
| 589 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); |
| 590 | SmallVector<LLVMValueRef, 8> Idx; |
| 591 | int NumIdx = LLVMGetNumIndices(Src); |
| 592 | for (int i = 1; i <= NumIdx; i++) |
| 593 | Idx.push_back(CloneValue(LLVMGetOperand(Src, i))); |
| 594 | if (LLVMIsInBounds(Src)) |
| 595 | Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name); |
| 596 | else |
| 597 | Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name); |
| 598 | break; |
| 599 | } |
Mehdi Amini | 43165d9 | 2016-03-19 21:28:28 +0000 | [diff] [blame] | 600 | case LLVMAtomicCmpXchg: { |
| 601 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); |
| 602 | LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1)); |
| 603 | LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2)); |
| 604 | LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src); |
| 605 | LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src); |
| 606 | LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src); |
| 607 | |
| 608 | Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail, |
| 609 | SingleThread); |
| 610 | } break; |
Amaury Sechet | 40bbe51 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 611 | case LLVMBitCast: { |
| 612 | LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0)); |
| 613 | Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name); |
| 614 | break; |
| 615 | } |
Amaury Sechet | e7e6217 | 2016-02-09 23:15:02 +0000 | [diff] [blame] | 616 | case LLVMICmp: { |
| 617 | LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src); |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 618 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 619 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | e7e6217 | 2016-02-09 23:15:02 +0000 | [diff] [blame] | 620 | Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name); |
| 621 | break; |
| 622 | } |
Amaury Sechet | 2f43208 | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 623 | case LLVMPHI: { |
Simon Pilgrim | 68168d1 | 2017-03-30 12:59:53 +0000 | [diff] [blame] | 624 | // We need to aggressively set things here because of loops. |
Amaury Sechet | 2f43208 | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 625 | VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name); |
| 626 | |
| 627 | SmallVector<LLVMValueRef, 8> Values; |
| 628 | SmallVector<LLVMBasicBlockRef, 8> Blocks; |
| 629 | |
| 630 | unsigned IncomingCount = LLVMCountIncoming(Src); |
| 631 | for (unsigned i = 0; i < IncomingCount; ++i) { |
| 632 | Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i))); |
| 633 | Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i))); |
| 634 | } |
| 635 | |
| 636 | LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount); |
| 637 | return Dst; |
| 638 | } |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 639 | case LLVMCall: { |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 640 | SmallVector<LLVMValueRef, 8> Args; |
Amaury Sechet | 5c7b3af | 2016-02-10 00:09:37 +0000 | [diff] [blame] | 641 | int ArgCount = LLVMGetNumArgOperands(Src); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 642 | for (int i = 0; i < ArgCount; i++) |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 643 | Args.push_back(CloneValue(LLVMGetOperand(Src, i))); |
Amaury Sechet | 5c7b3af | 2016-02-10 00:09:37 +0000 | [diff] [blame] | 644 | LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 645 | Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name); |
Amaury Sechet | e39e853 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 646 | LLVMSetTailCall(Dst, LLVMIsTailCall(Src)); |
Amaury Sechet | a65a237 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 647 | CloneAttrs(Src, Dst); |
Amaury Sechet | e39e853 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 648 | break; |
| 649 | } |
| 650 | case LLVMResume: { |
| 651 | Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0))); |
| 652 | break; |
| 653 | } |
| 654 | case LLVMLandingPad: { |
| 655 | // The landing pad API is a bit screwed up for historical reasons. |
| 656 | Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name); |
| 657 | unsigned NumClauses = LLVMGetNumClauses(Src); |
| 658 | for (unsigned i = 0; i < NumClauses; ++i) |
| 659 | LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i))); |
| 660 | LLVMSetCleanup(Dst, LLVMIsCleanup(Src)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 661 | break; |
| 662 | } |
Robert Widmann | 478fce9 | 2018-03-30 17:49:53 +0000 | [diff] [blame] | 663 | case LLVMCleanupRet: { |
| 664 | LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0)); |
| 665 | LLVMBasicBlockRef Unwind = nullptr; |
| 666 | if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) |
| 667 | Unwind = DeclareBB(UDest); |
| 668 | Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind); |
| 669 | break; |
| 670 | } |
| 671 | case LLVMCatchRet: { |
| 672 | LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0)); |
| 673 | LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0)); |
| 674 | Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB); |
| 675 | break; |
| 676 | } |
| 677 | case LLVMCatchPad: { |
| 678 | LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src)); |
| 679 | SmallVector<LLVMValueRef, 8> Args; |
| 680 | int ArgCount = LLVMGetNumArgOperands(Src); |
| 681 | for (int i = 0; i < ArgCount; i++) |
| 682 | Args.push_back(CloneValue(LLVMGetOperand(Src, i))); |
| 683 | Dst = LLVMBuildCatchPad(Builder, ParentPad, |
| 684 | Args.data(), ArgCount, Name); |
| 685 | break; |
| 686 | } |
| 687 | case LLVMCleanupPad: { |
| 688 | LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0)); |
| 689 | SmallVector<LLVMValueRef, 8> Args; |
| 690 | int ArgCount = LLVMGetNumArgOperands(Src); |
| 691 | for (int i = 0; i < ArgCount; i++) |
| 692 | Args.push_back(CloneValue(LLVMGetArgOperand(Src, i))); |
| 693 | Dst = LLVMBuildCleanupPad(Builder, ParentPad, |
| 694 | Args.data(), ArgCount, Name); |
| 695 | break; |
| 696 | } |
| 697 | case LLVMCatchSwitch: { |
| 698 | LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0)); |
| 699 | LLVMBasicBlockRef UnwindBB = nullptr; |
| 700 | if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) { |
| 701 | UnwindBB = DeclareBB(UDest); |
| 702 | } |
| 703 | unsigned NumHandlers = LLVMGetNumHandlers(Src); |
| 704 | Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name); |
| 705 | if (NumHandlers > 0) { |
| 706 | LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>( |
| 707 | safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef))); |
| 708 | LLVMGetHandlers(Src, Handlers); |
| 709 | for (unsigned i = 0; i < NumHandlers; i++) |
| 710 | LLVMAddHandler(Dst, DeclareBB(Handlers[i])); |
| 711 | free(Handlers); |
| 712 | } |
| 713 | break; |
| 714 | } |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 715 | case LLVMExtractValue: { |
| 716 | LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); |
| 717 | if (LLVMGetNumIndices(Src) != 1) |
| 718 | report_fatal_error("Expected only one indice"); |
| 719 | auto I = LLVMGetIndices(Src)[0]; |
| 720 | Dst = LLVMBuildExtractValue(Builder, Agg, I, Name); |
| 721 | break; |
| 722 | } |
| 723 | case LLVMInsertValue: { |
| 724 | LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); |
| 725 | LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1)); |
| 726 | if (LLVMGetNumIndices(Src) != 1) |
| 727 | report_fatal_error("Expected only one indice"); |
| 728 | auto I = LLVMGetIndices(Src)[0]; |
| 729 | Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name); |
| 730 | break; |
| 731 | } |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 732 | default: |
| 733 | break; |
| 734 | } |
| 735 | |
| 736 | if (Dst == nullptr) { |
| 737 | fprintf(stderr, "%d is not a supported opcode\n", Op); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 738 | exit(-1); |
| 739 | } |
| 740 | |
Robert Widmann | 9cba4ec | 2018-09-28 15:35:18 +0000 | [diff] [blame] | 741 | auto Ctx = LLVMGetModuleContext(M); |
| 742 | size_t NumMetadataEntries; |
| 743 | auto *AllMetadata = |
| 744 | LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src, |
| 745 | &NumMetadataEntries); |
| 746 | for (unsigned i = 0; i < NumMetadataEntries; ++i) { |
| 747 | unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i); |
| 748 | LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i); |
| 749 | LLVMSetMetadata(Dst, Kind, LLVMMetadataAsValue(Ctx, MD)); |
| 750 | } |
| 751 | LLVMDisposeValueMetadataEntries(AllMetadata); |
| 752 | LLVMSetInstDebugLocation(Builder, Dst); |
| 753 | |
Amaury Sechet | 4949131 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 754 | check_value_kind(Dst, LLVMInstructionValueKind); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 755 | return VMap[Src] = Dst; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 758 | LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) { |
| 759 | // Check if this is something we already computed. |
| 760 | { |
| 761 | auto i = BBMap.find(Src); |
| 762 | if (i != BBMap.end()) { |
| 763 | return i->second; |
| 764 | } |
| 765 | } |
| 766 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 767 | LLVMValueRef V = LLVMBasicBlockAsValue(Src); |
| 768 | if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src) |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 769 | report_fatal_error("Basic block is not a basic block"); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 770 | |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 771 | const char *Name = LLVMGetBasicBlockName(Src); |
Robert Widmann | 025c78f | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 772 | size_t NameLen; |
| 773 | const char *VName = LLVMGetValueName2(V, &NameLen); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 774 | if (Name != VName) |
| 775 | report_fatal_error("Basic block name mismatch"); |
| 776 | |
| 777 | LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name); |
| 778 | return BBMap[Src] = BB; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 781 | LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) { |
| 782 | LLVMBasicBlockRef BB = DeclareBB(Src); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 783 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 784 | // Make sure ordering is correct. |
| 785 | LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src); |
| 786 | if (Prev) |
| 787 | LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev)); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 788 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 789 | LLVMValueRef First = LLVMGetFirstInstruction(Src); |
| 790 | LLVMValueRef Last = LLVMGetLastInstruction(Src); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 791 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 792 | if (First == nullptr) { |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 793 | if (Last != nullptr) |
| 794 | report_fatal_error("Has no first instruction, but last one"); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 795 | return BB; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 798 | auto Ctx = LLVMGetModuleContext(M); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 799 | LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx); |
| 800 | LLVMPositionBuilderAtEnd(Builder, BB); |
| 801 | |
| 802 | LLVMValueRef Cur = First; |
| 803 | LLVMValueRef Next = nullptr; |
| 804 | while(true) { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 805 | CloneInstruction(Cur, Builder); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 806 | Next = LLVMGetNextInstruction(Cur); |
| 807 | if (Next == nullptr) { |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 808 | if (Cur != Last) |
| 809 | report_fatal_error("Final instruction does not match Last"); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 810 | break; |
| 811 | } |
| 812 | |
| 813 | LLVMValueRef Prev = LLVMGetPreviousInstruction(Next); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 814 | if (Prev != Cur) |
| 815 | report_fatal_error("Next.Previous instruction is not Current"); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 816 | |
| 817 | Cur = Next; |
| 818 | } |
| 819 | |
| 820 | LLVMDisposeBuilder(Builder); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 821 | return BB; |
| 822 | } |
| 823 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 824 | void CloneBBs(LLVMValueRef Src) { |
| 825 | unsigned Count = LLVMCountBasicBlocks(Src); |
| 826 | if (Count == 0) |
| 827 | return; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 828 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 829 | LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src); |
| 830 | LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src); |
| 831 | |
| 832 | LLVMBasicBlockRef Cur = First; |
| 833 | LLVMBasicBlockRef Next = nullptr; |
| 834 | while(true) { |
| 835 | CloneBB(Cur); |
| 836 | Count--; |
| 837 | Next = LLVMGetNextBasicBlock(Cur); |
| 838 | if (Next == nullptr) { |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 839 | if (Cur != Last) |
| 840 | report_fatal_error("Final basic block does not match Last"); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 841 | break; |
| 842 | } |
| 843 | |
| 844 | LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 845 | if (Prev != Cur) |
| 846 | report_fatal_error("Next.Previous basic bloc is not Current"); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 847 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 848 | Cur = Next; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 851 | if (Count != 0) |
| 852 | report_fatal_error("Basic block count does not match iterration"); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 853 | } |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 854 | }; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 855 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 856 | static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) { |
Robert Widmann | 360d6e3 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 857 | auto Ctx = LLVMGetModuleContext(M); |
| 858 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 859 | LLVMValueRef Begin = LLVMGetFirstGlobal(Src); |
| 860 | LLVMValueRef End = LLVMGetLastGlobal(Src); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 861 | |
| 862 | LLVMValueRef Cur = Begin; |
| 863 | LLVMValueRef Next = nullptr; |
Amaury Sechet | 58946a9 | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 864 | if (!Begin) { |
| 865 | if (End != nullptr) |
Hiroshi Inoue | e9dea6e | 2017-07-13 06:48:39 +0000 | [diff] [blame] | 866 | report_fatal_error("Range has an end but no beginning"); |
Amaury Sechet | 58946a9 | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 867 | goto FunDecl; |
| 868 | } |
| 869 | |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 870 | while (true) { |
Robert Widmann | 025c78f | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 871 | size_t NameLen; |
| 872 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 873 | if (LLVMGetNamedGlobal(M, Name)) |
| 874 | report_fatal_error("GlobalVariable already cloned"); |
| 875 | LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name); |
| 876 | |
| 877 | Next = LLVMGetNextGlobal(Cur); |
| 878 | if (Next == nullptr) { |
| 879 | if (Cur != End) |
| 880 | report_fatal_error(""); |
| 881 | break; |
| 882 | } |
| 883 | |
| 884 | LLVMValueRef Prev = LLVMGetPreviousGlobal(Next); |
| 885 | if (Prev != Cur) |
| 886 | report_fatal_error("Next.Previous global is not Current"); |
| 887 | |
| 888 | Cur = Next; |
| 889 | } |
| 890 | |
Amaury Sechet | 58946a9 | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 891 | FunDecl: |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 892 | Begin = LLVMGetFirstFunction(Src); |
| 893 | End = LLVMGetLastFunction(Src); |
| 894 | if (!Begin) { |
| 895 | if (End != nullptr) |
Hiroshi Inoue | e9dea6e | 2017-07-13 06:48:39 +0000 | [diff] [blame] | 896 | report_fatal_error("Range has an end but no beginning"); |
Robert Widmann | 360d6e3 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 897 | goto AliasDecl; |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | Cur = Begin; |
| 901 | Next = nullptr; |
| 902 | while (true) { |
Robert Widmann | 025c78f | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 903 | size_t NameLen; |
| 904 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 905 | if (LLVMGetNamedFunction(M, Name)) |
| 906 | report_fatal_error("Function already cloned"); |
Amaury Sechet | 5db224e | 2016-06-12 06:17:24 +0000 | [diff] [blame] | 907 | auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur)); |
| 908 | auto F = LLVMAddFunction(M, Name, Ty); |
| 909 | |
| 910 | // Copy attributes |
| 911 | for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F); |
| 912 | i <= c; ++i) { |
| 913 | for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { |
| 914 | if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) { |
| 915 | auto Val = LLVMGetEnumAttributeValue(SrcA); |
| 916 | auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val); |
| 917 | LLVMAddAttributeAtIndex(F, i, DstA); |
| 918 | } |
| 919 | } |
| 920 | } |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 921 | |
Amaury Sechet | f64e834 | 2016-02-16 07:08:49 +0000 | [diff] [blame] | 922 | Next = LLVMGetNextFunction(Cur); |
| 923 | if (Next == nullptr) { |
| 924 | if (Cur != End) |
| 925 | report_fatal_error("Last function does not match End"); |
| 926 | break; |
| 927 | } |
| 928 | |
| 929 | LLVMValueRef Prev = LLVMGetPreviousFunction(Next); |
| 930 | if (Prev != Cur) |
| 931 | report_fatal_error("Next.Previous function is not Current"); |
| 932 | |
| 933 | Cur = Next; |
| 934 | } |
Robert Widmann | 360d6e3 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 935 | |
| 936 | AliasDecl: |
| 937 | Begin = LLVMGetFirstGlobalAlias(Src); |
| 938 | End = LLVMGetLastGlobalAlias(Src); |
| 939 | if (!Begin) { |
| 940 | if (End != nullptr) |
| 941 | report_fatal_error("Range has an end but no beginning"); |
Robert Widmann | d5444cc | 2019-02-05 18:05:44 +0000 | [diff] [blame^] | 942 | goto GlobalIFuncDecl; |
Robert Widmann | 360d6e3 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | Cur = Begin; |
| 946 | Next = nullptr; |
| 947 | while (true) { |
| 948 | size_t NameLen; |
| 949 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
| 950 | if (LLVMGetNamedGlobalAlias(M, Name, NameLen)) |
| 951 | report_fatal_error("Global alias already cloned"); |
| 952 | LLVMTypeRef CurType = TypeCloner(M).Clone(Cur); |
| 953 | // FIXME: Allow NULL aliasee. |
| 954 | LLVMAddAlias(M, CurType, LLVMGetUndef(CurType), Name); |
| 955 | |
| 956 | Next = LLVMGetNextGlobalAlias(Cur); |
| 957 | if (Next == nullptr) { |
| 958 | if (Cur != End) |
| 959 | report_fatal_error(""); |
| 960 | break; |
| 961 | } |
| 962 | |
| 963 | LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next); |
| 964 | if (Prev != Cur) |
| 965 | report_fatal_error("Next.Previous global is not Current"); |
| 966 | |
| 967 | Cur = Next; |
| 968 | } |
Robert Widmann | 0a35b76 | 2018-08-30 17:09:43 +0000 | [diff] [blame] | 969 | |
Robert Widmann | d5444cc | 2019-02-05 18:05:44 +0000 | [diff] [blame^] | 970 | GlobalIFuncDecl: |
| 971 | Begin = LLVMGetFirstGlobalIFunc(Src); |
| 972 | End = LLVMGetLastGlobalIFunc(Src); |
| 973 | if (!Begin) { |
| 974 | if (End != nullptr) |
| 975 | report_fatal_error("Range has an end but no beginning"); |
| 976 | goto NamedMDDecl; |
| 977 | } |
| 978 | |
| 979 | Cur = Begin; |
| 980 | Next = nullptr; |
| 981 | while (true) { |
| 982 | size_t NameLen; |
| 983 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
| 984 | if (LLVMGetNamedGlobalIFunc(M, Name, NameLen)) |
| 985 | report_fatal_error("Global ifunc already cloned"); |
| 986 | LLVMTypeRef CurType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur)); |
| 987 | // FIXME: Allow NULL resolver. |
| 988 | LLVMAddGlobalIFunc(M, Name, NameLen, |
| 989 | CurType, /*addressSpace*/ 0, LLVMGetUndef(CurType)); |
| 990 | |
| 991 | Next = LLVMGetNextGlobalIFunc(Cur); |
| 992 | if (Next == nullptr) { |
| 993 | if (Cur != End) |
| 994 | report_fatal_error(""); |
| 995 | break; |
| 996 | } |
| 997 | |
| 998 | LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next); |
| 999 | if (Prev != Cur) |
| 1000 | report_fatal_error("Next.Previous global is not Current"); |
| 1001 | |
| 1002 | Cur = Next; |
| 1003 | } |
| 1004 | |
Robert Widmann | 0a35b76 | 2018-08-30 17:09:43 +0000 | [diff] [blame] | 1005 | NamedMDDecl: |
| 1006 | LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src); |
| 1007 | LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src); |
| 1008 | if (!BeginMD) { |
| 1009 | if (EndMD != nullptr) |
| 1010 | report_fatal_error("Range has an end but no beginning"); |
| 1011 | return; |
| 1012 | } |
| 1013 | |
| 1014 | LLVMNamedMDNodeRef CurMD = BeginMD; |
| 1015 | LLVMNamedMDNodeRef NextMD = nullptr; |
| 1016 | while (true) { |
| 1017 | size_t NameLen; |
| 1018 | const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen); |
| 1019 | if (LLVMGetNamedMetadata(M, Name, NameLen)) |
| 1020 | report_fatal_error("Named Metadata Node already cloned"); |
| 1021 | LLVMGetOrInsertNamedMetadata(M, Name, NameLen); |
| 1022 | |
| 1023 | NextMD = LLVMGetNextNamedMetadata(CurMD); |
| 1024 | if (NextMD == nullptr) { |
| 1025 | if (CurMD != EndMD) |
| 1026 | report_fatal_error(""); |
| 1027 | break; |
| 1028 | } |
| 1029 | |
| 1030 | LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD); |
| 1031 | if (PrevMD != CurMD) |
| 1032 | report_fatal_error("Next.Previous global is not Current"); |
| 1033 | |
| 1034 | CurMD = NextMD; |
| 1035 | } |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1036 | } |
Amaury Sechet | f64e834 | 2016-02-16 07:08:49 +0000 | [diff] [blame] | 1037 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1038 | static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) { |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1039 | LLVMValueRef Begin = LLVMGetFirstGlobal(Src); |
| 1040 | LLVMValueRef End = LLVMGetLastGlobal(Src); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1041 | |
| 1042 | LLVMValueRef Cur = Begin; |
| 1043 | LLVMValueRef Next = nullptr; |
Amaury Sechet | 58946a9 | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 1044 | if (!Begin) { |
| 1045 | if (End != nullptr) |
Hiroshi Inoue | e9dea6e | 2017-07-13 06:48:39 +0000 | [diff] [blame] | 1046 | report_fatal_error("Range has an end but no beginning"); |
Amaury Sechet | 58946a9 | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 1047 | goto FunClone; |
| 1048 | } |
| 1049 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1050 | while (true) { |
Robert Widmann | 025c78f | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 1051 | size_t NameLen; |
| 1052 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1053 | LLVMValueRef G = LLVMGetNamedGlobal(M, Name); |
| 1054 | if (!G) |
| 1055 | report_fatal_error("GlobalVariable must have been declared already"); |
| 1056 | |
| 1057 | if (auto I = LLVMGetInitializer(Cur)) |
| 1058 | LLVMSetInitializer(G, clone_constant(I, M)); |
| 1059 | |
Robert Widmann | 9cba4ec | 2018-09-28 15:35:18 +0000 | [diff] [blame] | 1060 | size_t NumMetadataEntries; |
| 1061 | auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries); |
| 1062 | for (unsigned i = 0; i < NumMetadataEntries; ++i) { |
| 1063 | unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i); |
| 1064 | LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i); |
| 1065 | LLVMGlobalSetMetadata(G, Kind, MD); |
| 1066 | } |
| 1067 | LLVMDisposeValueMetadataEntries(AllMetadata); |
| 1068 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1069 | LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur)); |
| 1070 | LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur)); |
| 1071 | LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur)); |
| 1072 | LLVMSetLinkage(G, LLVMGetLinkage(Cur)); |
| 1073 | LLVMSetSection(G, LLVMGetSection(Cur)); |
| 1074 | LLVMSetVisibility(G, LLVMGetVisibility(Cur)); |
Robert Widmann | 4bb481b | 2018-03-14 06:45:51 +0000 | [diff] [blame] | 1075 | LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur)); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1076 | LLVMSetAlignment(G, LLVMGetAlignment(Cur)); |
| 1077 | |
| 1078 | Next = LLVMGetNextGlobal(Cur); |
| 1079 | if (Next == nullptr) { |
| 1080 | if (Cur != End) |
| 1081 | report_fatal_error(""); |
| 1082 | break; |
| 1083 | } |
| 1084 | |
| 1085 | LLVMValueRef Prev = LLVMGetPreviousGlobal(Next); |
| 1086 | if (Prev != Cur) |
| 1087 | report_fatal_error("Next.Previous global is not Current"); |
| 1088 | |
| 1089 | Cur = Next; |
| 1090 | } |
| 1091 | |
Amaury Sechet | 58946a9 | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 1092 | FunClone: |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1093 | Begin = LLVMGetFirstFunction(Src); |
| 1094 | End = LLVMGetLastFunction(Src); |
| 1095 | if (!Begin) { |
| 1096 | if (End != nullptr) |
Hiroshi Inoue | e9dea6e | 2017-07-13 06:48:39 +0000 | [diff] [blame] | 1097 | report_fatal_error("Range has an end but no beginning"); |
Robert Widmann | 360d6e3 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 1098 | goto AliasClone; |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Amaury Sechet | f64e834 | 2016-02-16 07:08:49 +0000 | [diff] [blame] | 1101 | Cur = Begin; |
| 1102 | Next = nullptr; |
| 1103 | while (true) { |
Robert Widmann | 025c78f | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 1104 | size_t NameLen; |
| 1105 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1106 | LLVMValueRef Fun = LLVMGetNamedFunction(M, Name); |
| 1107 | if (!Fun) |
| 1108 | report_fatal_error("Function must have been declared already"); |
Amaury Sechet | e39e853 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 1109 | |
| 1110 | if (LLVMHasPersonalityFn(Cur)) { |
Robert Widmann | 025c78f | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 1111 | size_t FNameLen; |
| 1112 | const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur), |
| 1113 | &FNameLen); |
Amaury Sechet | e39e853 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 1114 | LLVMValueRef P = LLVMGetNamedFunction(M, FName); |
| 1115 | if (!P) |
| 1116 | report_fatal_error("Could not find personality function"); |
| 1117 | LLVMSetPersonalityFn(Fun, P); |
| 1118 | } |
| 1119 | |
Robert Widmann | 9cba4ec | 2018-09-28 15:35:18 +0000 | [diff] [blame] | 1120 | size_t NumMetadataEntries; |
| 1121 | auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries); |
| 1122 | for (unsigned i = 0; i < NumMetadataEntries; ++i) { |
| 1123 | unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i); |
| 1124 | LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i); |
| 1125 | LLVMGlobalSetMetadata(Fun, Kind, MD); |
| 1126 | } |
| 1127 | LLVMDisposeValueMetadataEntries(AllMetadata); |
| 1128 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1129 | FunCloner FC(Cur, Fun); |
| 1130 | FC.CloneBBs(Cur); |
| 1131 | |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1132 | Next = LLVMGetNextFunction(Cur); |
| 1133 | if (Next == nullptr) { |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 1134 | if (Cur != End) |
| 1135 | report_fatal_error("Last function does not match End"); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1136 | break; |
| 1137 | } |
| 1138 | |
| 1139 | LLVMValueRef Prev = LLVMGetPreviousFunction(Next); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 1140 | if (Prev != Cur) |
| 1141 | report_fatal_error("Next.Previous function is not Current"); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1142 | |
| 1143 | Cur = Next; |
| 1144 | } |
Robert Widmann | 360d6e3 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 1145 | |
| 1146 | AliasClone: |
| 1147 | Begin = LLVMGetFirstGlobalAlias(Src); |
| 1148 | End = LLVMGetLastGlobalAlias(Src); |
| 1149 | if (!Begin) { |
| 1150 | if (End != nullptr) |
| 1151 | report_fatal_error("Range has an end but no beginning"); |
Robert Widmann | d5444cc | 2019-02-05 18:05:44 +0000 | [diff] [blame^] | 1152 | goto GlobalIFuncClone; |
Robert Widmann | 360d6e3 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | Cur = Begin; |
| 1156 | Next = nullptr; |
| 1157 | while (true) { |
| 1158 | size_t NameLen; |
| 1159 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
| 1160 | LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen); |
| 1161 | if (!Alias) |
| 1162 | report_fatal_error("Global alias must have been declared already"); |
| 1163 | |
| 1164 | if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Cur)) { |
| 1165 | LLVMAliasSetAliasee(Alias, clone_constant(Aliasee, M)); |
| 1166 | } |
| 1167 | |
| 1168 | LLVMSetLinkage(Alias, LLVMGetLinkage(Cur)); |
| 1169 | LLVMSetUnnamedAddress(Alias, LLVMGetUnnamedAddress(Cur)); |
| 1170 | |
| 1171 | Next = LLVMGetNextGlobalAlias(Cur); |
| 1172 | if (Next == nullptr) { |
| 1173 | if (Cur != End) |
| 1174 | report_fatal_error("Last global alias does not match End"); |
| 1175 | break; |
| 1176 | } |
| 1177 | |
| 1178 | LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next); |
| 1179 | if (Prev != Cur) |
| 1180 | report_fatal_error("Next.Previous global alias is not Current"); |
| 1181 | |
| 1182 | Cur = Next; |
| 1183 | } |
Robert Widmann | 0a35b76 | 2018-08-30 17:09:43 +0000 | [diff] [blame] | 1184 | |
Robert Widmann | d5444cc | 2019-02-05 18:05:44 +0000 | [diff] [blame^] | 1185 | GlobalIFuncClone: |
| 1186 | Begin = LLVMGetFirstGlobalIFunc(Src); |
| 1187 | End = LLVMGetLastGlobalIFunc(Src); |
| 1188 | if (!Begin) { |
| 1189 | if (End != nullptr) |
| 1190 | report_fatal_error("Range has an end but no beginning"); |
| 1191 | goto NamedMDClone; |
| 1192 | } |
| 1193 | |
| 1194 | Cur = Begin; |
| 1195 | Next = nullptr; |
| 1196 | while (true) { |
| 1197 | size_t NameLen; |
| 1198 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
| 1199 | LLVMValueRef IFunc = LLVMGetNamedGlobalIFunc(M, Name, NameLen); |
| 1200 | if (!IFunc) |
| 1201 | report_fatal_error("Global ifunc must have been declared already"); |
| 1202 | |
| 1203 | if (LLVMValueRef Resolver = LLVMGetGlobalIFuncResolver(Cur)) { |
| 1204 | LLVMSetGlobalIFuncResolver(IFunc, clone_constant(Resolver, M)); |
| 1205 | } |
| 1206 | |
| 1207 | LLVMSetLinkage(IFunc, LLVMGetLinkage(Cur)); |
| 1208 | LLVMSetUnnamedAddress(IFunc, LLVMGetUnnamedAddress(Cur)); |
| 1209 | |
| 1210 | Next = LLVMGetNextGlobalIFunc(Cur); |
| 1211 | if (Next == nullptr) { |
| 1212 | if (Cur != End) |
| 1213 | report_fatal_error("Last global alias does not match End"); |
| 1214 | break; |
| 1215 | } |
| 1216 | |
| 1217 | LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next); |
| 1218 | if (Prev != Cur) |
| 1219 | report_fatal_error("Next.Previous global alias is not Current"); |
| 1220 | |
| 1221 | Cur = Next; |
| 1222 | } |
| 1223 | |
Robert Widmann | 0a35b76 | 2018-08-30 17:09:43 +0000 | [diff] [blame] | 1224 | NamedMDClone: |
| 1225 | LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src); |
| 1226 | LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src); |
| 1227 | if (!BeginMD) { |
| 1228 | if (EndMD != nullptr) |
| 1229 | report_fatal_error("Range has an end but no beginning"); |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | LLVMNamedMDNodeRef CurMD = BeginMD; |
| 1234 | LLVMNamedMDNodeRef NextMD = nullptr; |
| 1235 | while (true) { |
| 1236 | size_t NameLen; |
| 1237 | const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen); |
| 1238 | LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen); |
| 1239 | if (!NamedMD) |
| 1240 | report_fatal_error("Named MD Node must have been declared already"); |
| 1241 | |
| 1242 | unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name); |
| 1243 | LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>( |
| 1244 | safe_malloc(OperandCount * sizeof(LLVMValueRef))); |
| 1245 | LLVMGetNamedMetadataOperands(Src, Name, OperandBuf); |
| 1246 | for (unsigned i = 0, e = OperandCount; i != e; ++i) { |
| 1247 | LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]); |
| 1248 | } |
| 1249 | free(OperandBuf); |
| 1250 | |
| 1251 | NextMD = LLVMGetNextNamedMetadata(CurMD); |
| 1252 | if (NextMD == nullptr) { |
| 1253 | if (CurMD != EndMD) |
| 1254 | report_fatal_error("Last Named MD Node does not match End"); |
| 1255 | break; |
| 1256 | } |
| 1257 | |
| 1258 | LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD); |
| 1259 | if (PrevMD != CurMD) |
| 1260 | report_fatal_error("Next.Previous Named MD Node is not Current"); |
| 1261 | |
| 1262 | CurMD = NextMD; |
| 1263 | } |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Benjamin Kramer | 9a3bd23 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 1266 | int llvm_echo(void) { |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1267 | LLVMEnablePrettyStackTrace(); |
| 1268 | |
Benjamin Kramer | 9a3bd23 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 1269 | LLVMModuleRef Src = llvm_load_module(false, true); |
Robert Widmann | 490a580 | 2018-01-30 21:34:29 +0000 | [diff] [blame] | 1270 | size_t SourceFileLen; |
| 1271 | const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen); |
| 1272 | size_t ModuleIdentLen; |
| 1273 | const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1274 | LLVMContextRef Ctx = LLVMContextCreate(); |
Peter Zotov | 0a2fa0a | 2016-04-05 13:56:59 +0000 | [diff] [blame] | 1275 | LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx); |
| 1276 | |
Robert Widmann | 490a580 | 2018-01-30 21:34:29 +0000 | [diff] [blame] | 1277 | LLVMSetSourceFileName(M, SourceFileName, SourceFileLen); |
| 1278 | LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1279 | |
Amaury Sechet | 5590967 | 2016-02-16 05:11:24 +0000 | [diff] [blame] | 1280 | LLVMSetTarget(M, LLVMGetTarget(Src)); |
| 1281 | LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src)); |
| 1282 | if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src))) |
| 1283 | report_fatal_error("Inconsistent DataLayout string representation"); |
| 1284 | |
Robert Widmann | f108d57 | 2018-04-06 02:31:29 +0000 | [diff] [blame] | 1285 | size_t ModuleInlineAsmLen; |
| 1286 | const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen); |
| 1287 | LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen); |
| 1288 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1289 | declare_symbols(Src, M); |
| 1290 | clone_symbols(Src, M); |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 1291 | char *Str = LLVMPrintModuleToString(M); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1292 | fputs(Str, stdout); |
| 1293 | |
| 1294 | LLVMDisposeMessage(Str); |
Rafael Espindola | 92f3578 | 2018-02-21 19:55:11 +0000 | [diff] [blame] | 1295 | LLVMDisposeModule(Src); |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 1296 | LLVMDisposeModule(M); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1297 | LLVMContextDispose(Ctx); |
| 1298 | |
| 1299 | return 0; |
| 1300 | } |