1 //===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the --echo command in llvm-c-test. 11 // 12 // This command uses the C API to read a module and output an exact copy of it 13 // as output. It is used to check that the resulting module matches the input 14 // to validate that the C API can read and write modules properly. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm-c-test.h" 19 #include "llvm-c/Target.h" 20 #include "llvm/ADT/DenseMap.h" 21 #include "llvm/Support/ErrorHandling.h" 22 23 #include <stdio.h> 24 #include <stdlib.h> 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; 56 typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap; 57 58 struct TypeCloner { 59 LLVMModuleRef M; 60 LLVMContextRef Ctx; 61 62 TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {} 63 64 LLVMTypeRef Clone(LLVMValueRef Src) { 65 return Clone(LLVMTypeOf(Src)); 66 } 67 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) { 93 Params = static_cast<LLVMTypeRef*>( 94 safe_malloc(ParamCount * sizeof(LLVMTypeRef))); 95 LLVMGetParamTypes(Src, Params); 96 for (unsigned i = 0; i < ParamCount; i++) 97 Params[i] = Clone(Params[i]); 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; 106 } 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 } 118 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: 146 return LLVMMetadataTypeInContext(Ctx); 147 case LLVMX86_MMXTypeKind: 148 return LLVMX86MMXTypeInContext(Ctx); 149 case LLVMTokenTypeKind: 150 return LLVMTokenTypeInContext(Ctx); 151 } 152 153 fprintf(stderr, "%d is not a supported typekind\n", Kind); 154 exit(-1); 155 } 156 }; 157 158 static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) { 159 unsigned Count = LLVMCountParams(Src); 160 if (Count != LLVMCountParams(Dst)) 161 report_fatal_error("Parameter count mismatch"); 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) { 177 const char *Name = LLVMGetValueName(SrcCur); 178 LLVMSetValueName(DstCur, Name); 179 180 VMap[SrcCur] = DstCur; 181 182 Count--; 183 SrcNext = LLVMGetNextParam(SrcCur); 184 DstNext = LLVMGetNextParam(DstCur); 185 if (SrcNext == nullptr && DstNext == nullptr) { 186 if (SrcCur != SrcLast) 187 report_fatal_error("SrcLast param does not match End"); 188 if (DstCur != DstLast) 189 report_fatal_error("DstLast param does not match End"); 190 break; 191 } 192 193 if (SrcNext == nullptr) 194 report_fatal_error("SrcNext was unexpectedly null"); 195 if (DstNext == nullptr) 196 report_fatal_error("DstNext was unexpectedly null"); 197 198 LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext); 199 if (SrcPrev != SrcCur) 200 report_fatal_error("SrcNext.Previous param is not Current"); 201 202 LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext); 203 if (DstPrev != DstCur) 204 report_fatal_error("DstNext.Previous param is not Current"); 205 206 SrcCur = SrcNext; 207 DstCur = DstNext; 208 } 209 210 if (Count != 0) 211 report_fatal_error("Parameter count does not match iteration"); 212 213 return VMap; 214 } 215 216 static void check_value_kind(LLVMValueRef V, LLVMValueKind K) { 217 if (LLVMGetValueKind(V) != K) 218 report_fatal_error("LLVMGetValueKind returned incorrect type"); 219 } 220 221 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M); 222 223 static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) { 224 LLVMValueRef Ret = clone_constant_impl(Cst, M); 225 check_value_kind(Ret, LLVMGetValueKind(Cst)); 226 return Ret; 227 } 228 229 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) { 230 if (!LLVMIsAConstant(Cst)) 231 report_fatal_error("Expected a constant"); 232 233 // Maybe it is a symbol 234 if (LLVMIsAGlobalValue(Cst)) { 235 const char *Name = LLVMGetValueName(Cst); 236 237 // Try function 238 if (LLVMIsAFunction(Cst)) { 239 check_value_kind(Cst, LLVMFunctionValueKind); 240 LLVMValueRef Dst = LLVMGetNamedFunction(M, Name); 241 if (Dst) 242 return Dst; 243 report_fatal_error("Could not find function"); 244 } 245 246 // Try global variable 247 if (LLVMIsAGlobalVariable(Cst)) { 248 check_value_kind(Cst, LLVMGlobalVariableValueKind); 249 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name); 250 if (Dst) 251 return Dst; 252 report_fatal_error("Could not find function"); 253 } 254 255 fprintf(stderr, "Could not find @%s\n", Name); 256 exit(-1); 257 } 258 259 // Try integer literal 260 if (LLVMIsAConstantInt(Cst)) { 261 check_value_kind(Cst, LLVMConstantIntValueKind); 262 return LLVMConstInt(TypeCloner(M).Clone(Cst), 263 LLVMConstIntGetZExtValue(Cst), false); 264 } 265 266 // Try zeroinitializer 267 if (LLVMIsAConstantAggregateZero(Cst)) { 268 check_value_kind(Cst, LLVMConstantAggregateZeroValueKind); 269 return LLVMConstNull(TypeCloner(M).Clone(Cst)); 270 } 271 272 // Try constant array 273 if (LLVMIsAConstantArray(Cst)) { 274 check_value_kind(Cst, LLVMConstantArrayValueKind); 275 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); 276 unsigned EltCount = LLVMGetArrayLength(Ty); 277 SmallVector<LLVMValueRef, 8> Elts; 278 for (unsigned i = 0; i < EltCount; i++) 279 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M)); 280 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount); 281 } 282 283 // Try contant data array 284 if (LLVMIsAConstantDataArray(Cst)) { 285 check_value_kind(Cst, LLVMConstantDataArrayValueKind); 286 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); 287 unsigned EltCount = LLVMGetArrayLength(Ty); 288 SmallVector<LLVMValueRef, 8> Elts; 289 for (unsigned i = 0; i < EltCount; i++) 290 Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M)); 291 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount); 292 } 293 294 // Try constant struct 295 if (LLVMIsAConstantStruct(Cst)) { 296 check_value_kind(Cst, LLVMConstantStructValueKind); 297 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); 298 unsigned EltCount = LLVMCountStructElementTypes(Ty); 299 SmallVector<LLVMValueRef, 8> Elts; 300 for (unsigned i = 0; i < EltCount; i++) 301 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M)); 302 if (LLVMGetStructName(Ty)) 303 return LLVMConstNamedStruct(Ty, Elts.data(), EltCount); 304 return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(), 305 EltCount, LLVMIsPackedStruct(Ty)); 306 } 307 308 // Try undef 309 if (LLVMIsUndef(Cst)) { 310 check_value_kind(Cst, LLVMUndefValueValueKind); 311 return LLVMGetUndef(TypeCloner(M).Clone(Cst)); 312 } 313 314 // Try null 315 if (LLVMIsNull(Cst)) { 316 check_value_kind(Cst, LLVMConstantTokenNoneValueKind); 317 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); 318 return LLVMConstNull(Ty); 319 } 320 321 // Try float literal 322 if (LLVMIsAConstantFP(Cst)) { 323 check_value_kind(Cst, LLVMConstantFPValueKind); 324 report_fatal_error("ConstantFP is not supported"); 325 } 326 327 // This kind of constant is not supported 328 if (!LLVMIsAConstantExpr(Cst)) 329 report_fatal_error("Expected a constant expression"); 330 331 // At this point, it must be a constant expression 332 check_value_kind(Cst, LLVMConstantExprValueKind); 333 334 LLVMOpcode Op = LLVMGetConstOpcode(Cst); 335 switch(Op) { 336 case LLVMBitCast: 337 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M), 338 TypeCloner(M).Clone(Cst)); 339 default: 340 fprintf(stderr, "%d is not a supported opcode\n", Op); 341 exit(-1); 342 } 343 } 344 345 struct FunCloner { 346 LLVMValueRef Fun; 347 LLVMModuleRef M; 348 349 ValueMap VMap; 350 BasicBlockMap BBMap; 351 352 FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst), 353 M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {} 354 355 LLVMTypeRef CloneType(LLVMTypeRef Src) { 356 return TypeCloner(M).Clone(Src); 357 } 358 359 LLVMTypeRef CloneType(LLVMValueRef Src) { 360 return TypeCloner(M).Clone(Src); 361 } 362 363 // Try to clone everything in the llvm::Value hierarchy. 364 LLVMValueRef CloneValue(LLVMValueRef Src) { 365 // First, the value may be constant. 366 if (LLVMIsAConstant(Src)) 367 return clone_constant(Src, M); 368 369 // Function argument should always be in the map already. 370 auto i = VMap.find(Src); 371 if (i != VMap.end()) 372 return i->second; 373 374 if (!LLVMIsAInstruction(Src)) 375 report_fatal_error("Expected an instruction"); 376 377 auto Ctx = LLVMGetModuleContext(M); 378 auto Builder = LLVMCreateBuilderInContext(Ctx); 379 auto BB = DeclareBB(LLVMGetInstructionParent(Src)); 380 LLVMPositionBuilderAtEnd(Builder, BB); 381 auto Dst = CloneInstruction(Src, Builder); 382 LLVMDisposeBuilder(Builder); 383 return Dst; 384 } 385 386 void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) { 387 auto Ctx = LLVMGetModuleContext(M); 388 int ArgCount = LLVMGetNumArgOperands(Src); 389 for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) { 390 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { 391 if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) { 392 auto Val = LLVMGetEnumAttributeValue(SrcA); 393 auto A = LLVMCreateEnumAttribute(Ctx, k, Val); 394 LLVMAddCallSiteAttribute(Dst, i, A); 395 } 396 } 397 } 398 } 399 400 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) { 401 check_value_kind(Src, LLVMInstructionValueKind); 402 if (!LLVMIsAInstruction(Src)) 403 report_fatal_error("Expected an instruction"); 404 405 const char *Name = LLVMGetValueName(Src); 406 407 // Check if this is something we already computed. 408 { 409 auto i = VMap.find(Src); 410 if (i != VMap.end()) { 411 // If we have a hit, it means we already generated the instruction 412 // as a dependancy to somethign else. We need to make sure 413 // it is ordered properly. 414 auto I = i->second; 415 LLVMInstructionRemoveFromParent(I); 416 LLVMInsertIntoBuilderWithName(Builder, I, Name); 417 return I; 418 } 419 } 420 421 // We tried everything, it must be an instruction 422 // that hasn't been generated already. 423 LLVMValueRef Dst = nullptr; 424 425 LLVMOpcode Op = LLVMGetInstructionOpcode(Src); 426 switch(Op) { 427 case LLVMRet: { 428 int OpCount = LLVMGetNumOperands(Src); 429 if (OpCount == 0) 430 Dst = LLVMBuildRetVoid(Builder); 431 else 432 Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0))); 433 break; 434 } 435 case LLVMBr: { 436 if (!LLVMIsConditional(Src)) { 437 LLVMValueRef SrcOp = LLVMGetOperand(Src, 0); 438 LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp); 439 Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB)); 440 break; 441 } 442 443 LLVMValueRef Cond = LLVMGetCondition(Src); 444 LLVMValueRef Else = LLVMGetOperand(Src, 1); 445 LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else)); 446 LLVMValueRef Then = LLVMGetOperand(Src, 2); 447 LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then)); 448 Dst = LLVMBuildCondBr(Builder, CloneValue(Cond), ThenBB, ElseBB); 449 break; 450 } 451 case LLVMSwitch: 452 case LLVMIndirectBr: 453 break; 454 case LLVMInvoke: { 455 SmallVector<LLVMValueRef, 8> Args; 456 int ArgCount = LLVMGetNumArgOperands(Src); 457 for (int i = 0; i < ArgCount; i++) 458 Args.push_back(CloneValue(LLVMGetOperand(Src, i))); 459 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); 460 LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src)); 461 LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src)); 462 Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount, 463 Then, Unwind, Name); 464 CloneAttrs(Src, Dst); 465 break; 466 } 467 case LLVMUnreachable: 468 Dst = LLVMBuildUnreachable(Builder); 469 break; 470 case LLVMAdd: { 471 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 472 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 473 Dst = LLVMBuildAdd(Builder, LHS, RHS, Name); 474 break; 475 } 476 case LLVMSub: { 477 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 478 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 479 Dst = LLVMBuildSub(Builder, LHS, RHS, Name); 480 break; 481 } 482 case LLVMMul: { 483 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 484 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 485 Dst = LLVMBuildMul(Builder, LHS, RHS, Name); 486 break; 487 } 488 case LLVMUDiv: { 489 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 490 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 491 Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name); 492 break; 493 } 494 case LLVMSDiv: { 495 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 496 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 497 Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name); 498 break; 499 } 500 case LLVMURem: { 501 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 502 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 503 Dst = LLVMBuildURem(Builder, LHS, RHS, Name); 504 break; 505 } 506 case LLVMSRem: { 507 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 508 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 509 Dst = LLVMBuildSRem(Builder, LHS, RHS, Name); 510 break; 511 } 512 case LLVMShl: { 513 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 514 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 515 Dst = LLVMBuildShl(Builder, LHS, RHS, Name); 516 break; 517 } 518 case LLVMLShr: { 519 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 520 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 521 Dst = LLVMBuildLShr(Builder, LHS, RHS, Name); 522 break; 523 } 524 case LLVMAShr: { 525 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 526 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 527 Dst = LLVMBuildAShr(Builder, LHS, RHS, Name); 528 break; 529 } 530 case LLVMAnd: { 531 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 532 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 533 Dst = LLVMBuildAnd(Builder, LHS, RHS, Name); 534 break; 535 } 536 case LLVMOr: { 537 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 538 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 539 Dst = LLVMBuildOr(Builder, LHS, RHS, Name); 540 break; 541 } 542 case LLVMXor: { 543 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 544 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 545 Dst = LLVMBuildXor(Builder, LHS, RHS, Name); 546 break; 547 } 548 case LLVMAlloca: { 549 LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src)); 550 Dst = LLVMBuildAlloca(Builder, Ty, Name); 551 break; 552 } 553 case LLVMLoad: { 554 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); 555 Dst = LLVMBuildLoad(Builder, Ptr, Name); 556 LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); 557 break; 558 } 559 case LLVMStore: { 560 LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0)); 561 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1)); 562 Dst = LLVMBuildStore(Builder, Val, Ptr); 563 LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); 564 break; 565 } 566 case LLVMGetElementPtr: { 567 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); 568 SmallVector<LLVMValueRef, 8> Idx; 569 int NumIdx = LLVMGetNumIndices(Src); 570 for (int i = 1; i <= NumIdx; i++) 571 Idx.push_back(CloneValue(LLVMGetOperand(Src, i))); 572 if (LLVMIsInBounds(Src)) 573 Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name); 574 else 575 Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name); 576 break; 577 } 578 case LLVMAtomicCmpXchg: { 579 LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); 580 LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1)); 581 LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2)); 582 LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src); 583 LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src); 584 LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src); 585 586 Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail, 587 SingleThread); 588 } break; 589 case LLVMBitCast: { 590 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0)); 591 Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name); 592 break; 593 } 594 case LLVMICmp: { 595 LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src); 596 LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); 597 LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); 598 Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name); 599 break; 600 } 601 case LLVMPHI: { 602 // We need to aggressively set things here because of loops. 603 VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name); 604 605 SmallVector<LLVMValueRef, 8> Values; 606 SmallVector<LLVMBasicBlockRef, 8> Blocks; 607 608 unsigned IncomingCount = LLVMCountIncoming(Src); 609 for (unsigned i = 0; i < IncomingCount; ++i) { 610 Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i))); 611 Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i))); 612 } 613 614 LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount); 615 return Dst; 616 } 617 case LLVMCall: { 618 SmallVector<LLVMValueRef, 8> Args; 619 int ArgCount = LLVMGetNumArgOperands(Src); 620 for (int i = 0; i < ArgCount; i++) 621 Args.push_back(CloneValue(LLVMGetOperand(Src, i))); 622 LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); 623 Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name); 624 LLVMSetTailCall(Dst, LLVMIsTailCall(Src)); 625 CloneAttrs(Src, Dst); 626 break; 627 } 628 case LLVMResume: { 629 Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0))); 630 break; 631 } 632 case LLVMLandingPad: { 633 // The landing pad API is a bit screwed up for historical reasons. 634 Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name); 635 unsigned NumClauses = LLVMGetNumClauses(Src); 636 for (unsigned i = 0; i < NumClauses; ++i) 637 LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i))); 638 LLVMSetCleanup(Dst, LLVMIsCleanup(Src)); 639 break; 640 } 641 case LLVMCleanupRet: { 642 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0)); 643 LLVMBasicBlockRef Unwind = nullptr; 644 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) 645 Unwind = DeclareBB(UDest); 646 Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind); 647 break; 648 } 649 case LLVMCatchRet: { 650 LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0)); 651 LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0)); 652 Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB); 653 break; 654 } 655 case LLVMCatchPad: { 656 LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src)); 657 SmallVector<LLVMValueRef, 8> Args; 658 int ArgCount = LLVMGetNumArgOperands(Src); 659 for (int i = 0; i < ArgCount; i++) 660 Args.push_back(CloneValue(LLVMGetOperand(Src, i))); 661 Dst = LLVMBuildCatchPad(Builder, ParentPad, 662 Args.data(), ArgCount, Name); 663 break; 664 } 665 case LLVMCleanupPad: { 666 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0)); 667 SmallVector<LLVMValueRef, 8> Args; 668 int ArgCount = LLVMGetNumArgOperands(Src); 669 for (int i = 0; i < ArgCount; i++) 670 Args.push_back(CloneValue(LLVMGetArgOperand(Src, i))); 671 Dst = LLVMBuildCleanupPad(Builder, ParentPad, 672 Args.data(), ArgCount, Name); 673 break; 674 } 675 case LLVMCatchSwitch: { 676 LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0)); 677 LLVMBasicBlockRef UnwindBB = nullptr; 678 if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) { 679 UnwindBB = DeclareBB(UDest); 680 } 681 unsigned NumHandlers = LLVMGetNumHandlers(Src); 682 Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name); 683 if (NumHandlers > 0) { 684 LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>( 685 safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef))); 686 LLVMGetHandlers(Src, Handlers); 687 for (unsigned i = 0; i < NumHandlers; i++) 688 LLVMAddHandler(Dst, DeclareBB(Handlers[i])); 689 free(Handlers); 690 } 691 break; 692 } 693 case LLVMExtractValue: { 694 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); 695 if (LLVMGetNumIndices(Src) != 1) 696 report_fatal_error("Expected only one indice"); 697 auto I = LLVMGetIndices(Src)[0]; 698 Dst = LLVMBuildExtractValue(Builder, Agg, I, Name); 699 break; 700 } 701 case LLVMInsertValue: { 702 LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); 703 LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1)); 704 if (LLVMGetNumIndices(Src) != 1) 705 report_fatal_error("Expected only one indice"); 706 auto I = LLVMGetIndices(Src)[0]; 707 Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name); 708 break; 709 } 710 default: 711 break; 712 } 713 714 if (Dst == nullptr) { 715 fprintf(stderr, "%d is not a supported opcode\n", Op); 716 exit(-1); 717 } 718 719 check_value_kind(Dst, LLVMInstructionValueKind); 720 return VMap[Src] = Dst; 721 } 722 723 LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) { 724 // Check if this is something we already computed. 725 { 726 auto i = BBMap.find(Src); 727 if (i != BBMap.end()) { 728 return i->second; 729 } 730 } 731 732 LLVMValueRef V = LLVMBasicBlockAsValue(Src); 733 if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src) 734 report_fatal_error("Basic block is not a basic block"); 735 736 const char *Name = LLVMGetBasicBlockName(Src); 737 const char *VName = LLVMGetValueName(V); 738 if (Name != VName) 739 report_fatal_error("Basic block name mismatch"); 740 741 LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name); 742 return BBMap[Src] = BB; 743 } 744 745 LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) { 746 LLVMBasicBlockRef BB = DeclareBB(Src); 747 748 // Make sure ordering is correct. 749 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src); 750 if (Prev) 751 LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev)); 752 753 LLVMValueRef First = LLVMGetFirstInstruction(Src); 754 LLVMValueRef Last = LLVMGetLastInstruction(Src); 755 756 if (First == nullptr) { 757 if (Last != nullptr) 758 report_fatal_error("Has no first instruction, but last one"); 759 return BB; 760 } 761 762 auto Ctx = LLVMGetModuleContext(M); 763 LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx); 764 LLVMPositionBuilderAtEnd(Builder, BB); 765 766 LLVMValueRef Cur = First; 767 LLVMValueRef Next = nullptr; 768 while(true) { 769 CloneInstruction(Cur, Builder); 770 Next = LLVMGetNextInstruction(Cur); 771 if (Next == nullptr) { 772 if (Cur != Last) 773 report_fatal_error("Final instruction does not match Last"); 774 break; 775 } 776 777 LLVMValueRef Prev = LLVMGetPreviousInstruction(Next); 778 if (Prev != Cur) 779 report_fatal_error("Next.Previous instruction is not Current"); 780 781 Cur = Next; 782 } 783 784 LLVMDisposeBuilder(Builder); 785 return BB; 786 } 787 788 void CloneBBs(LLVMValueRef Src) { 789 unsigned Count = LLVMCountBasicBlocks(Src); 790 if (Count == 0) 791 return; 792 793 LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src); 794 LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src); 795 796 LLVMBasicBlockRef Cur = First; 797 LLVMBasicBlockRef Next = nullptr; 798 while(true) { 799 CloneBB(Cur); 800 Count--; 801 Next = LLVMGetNextBasicBlock(Cur); 802 if (Next == nullptr) { 803 if (Cur != Last) 804 report_fatal_error("Final basic block does not match Last"); 805 break; 806 } 807 808 LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next); 809 if (Prev != Cur) 810 report_fatal_error("Next.Previous basic bloc is not Current"); 811 812 Cur = Next; 813 } 814 815 if (Count != 0) 816 report_fatal_error("Basic block count does not match iterration"); 817 } 818 }; 819 820 static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) { 821 LLVMValueRef Begin = LLVMGetFirstGlobal(Src); 822 LLVMValueRef End = LLVMGetLastGlobal(Src); 823 824 LLVMValueRef Cur = Begin; 825 LLVMValueRef Next = nullptr; 826 if (!Begin) { 827 if (End != nullptr) 828 report_fatal_error("Range has an end but no beginning"); 829 goto FunDecl; 830 } 831 832 while (true) { 833 const char *Name = LLVMGetValueName(Cur); 834 if (LLVMGetNamedGlobal(M, Name)) 835 report_fatal_error("GlobalVariable already cloned"); 836 LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name); 837 838 Next = LLVMGetNextGlobal(Cur); 839 if (Next == nullptr) { 840 if (Cur != End) 841 report_fatal_error(""); 842 break; 843 } 844 845 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next); 846 if (Prev != Cur) 847 report_fatal_error("Next.Previous global is not Current"); 848 849 Cur = Next; 850 } 851 852 FunDecl: 853 Begin = LLVMGetFirstFunction(Src); 854 End = LLVMGetLastFunction(Src); 855 if (!Begin) { 856 if (End != nullptr) 857 report_fatal_error("Range has an end but no beginning"); 858 return; 859 } 860 861 auto Ctx = LLVMGetModuleContext(M); 862 863 Cur = Begin; 864 Next = nullptr; 865 while (true) { 866 const char *Name = LLVMGetValueName(Cur); 867 if (LLVMGetNamedFunction(M, Name)) 868 report_fatal_error("Function already cloned"); 869 auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur)); 870 auto F = LLVMAddFunction(M, Name, Ty); 871 872 // Copy attributes 873 for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F); 874 i <= c; ++i) { 875 for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { 876 if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) { 877 auto Val = LLVMGetEnumAttributeValue(SrcA); 878 auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val); 879 LLVMAddAttributeAtIndex(F, i, DstA); 880 } 881 } 882 } 883 884 Next = LLVMGetNextFunction(Cur); 885 if (Next == nullptr) { 886 if (Cur != End) 887 report_fatal_error("Last function does not match End"); 888 break; 889 } 890 891 LLVMValueRef Prev = LLVMGetPreviousFunction(Next); 892 if (Prev != Cur) 893 report_fatal_error("Next.Previous function is not Current"); 894 895 Cur = Next; 896 } 897 } 898 899 static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) { 900 LLVMValueRef Begin = LLVMGetFirstGlobal(Src); 901 LLVMValueRef End = LLVMGetLastGlobal(Src); 902 903 LLVMValueRef Cur = Begin; 904 LLVMValueRef Next = nullptr; 905 if (!Begin) { 906 if (End != nullptr) 907 report_fatal_error("Range has an end but no beginning"); 908 goto FunClone; 909 } 910 911 while (true) { 912 const char *Name = LLVMGetValueName(Cur); 913 LLVMValueRef G = LLVMGetNamedGlobal(M, Name); 914 if (!G) 915 report_fatal_error("GlobalVariable must have been declared already"); 916 917 if (auto I = LLVMGetInitializer(Cur)) 918 LLVMSetInitializer(G, clone_constant(I, M)); 919 920 LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur)); 921 LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur)); 922 LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur)); 923 LLVMSetLinkage(G, LLVMGetLinkage(Cur)); 924 LLVMSetSection(G, LLVMGetSection(Cur)); 925 LLVMSetVisibility(G, LLVMGetVisibility(Cur)); 926 LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur)); 927 LLVMSetAlignment(G, LLVMGetAlignment(Cur)); 928 929 Next = LLVMGetNextGlobal(Cur); 930 if (Next == nullptr) { 931 if (Cur != End) 932 report_fatal_error(""); 933 break; 934 } 935 936 LLVMValueRef Prev = LLVMGetPreviousGlobal(Next); 937 if (Prev != Cur) 938 report_fatal_error("Next.Previous global is not Current"); 939 940 Cur = Next; 941 } 942 943 FunClone: 944 Begin = LLVMGetFirstFunction(Src); 945 End = LLVMGetLastFunction(Src); 946 if (!Begin) { 947 if (End != nullptr) 948 report_fatal_error("Range has an end but no beginning"); 949 return; 950 } 951 952 Cur = Begin; 953 Next = nullptr; 954 while (true) { 955 const char *Name = LLVMGetValueName(Cur); 956 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name); 957 if (!Fun) 958 report_fatal_error("Function must have been declared already"); 959 960 if (LLVMHasPersonalityFn(Cur)) { 961 const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur)); 962 LLVMValueRef P = LLVMGetNamedFunction(M, FName); 963 if (!P) 964 report_fatal_error("Could not find personality function"); 965 LLVMSetPersonalityFn(Fun, P); 966 } 967 968 FunCloner FC(Cur, Fun); 969 FC.CloneBBs(Cur); 970 971 Next = LLVMGetNextFunction(Cur); 972 if (Next == nullptr) { 973 if (Cur != End) 974 report_fatal_error("Last function does not match End"); 975 break; 976 } 977 978 LLVMValueRef Prev = LLVMGetPreviousFunction(Next); 979 if (Prev != Cur) 980 report_fatal_error("Next.Previous function is not Current"); 981 982 Cur = Next; 983 } 984 } 985 986 int llvm_echo(void) { 987 LLVMEnablePrettyStackTrace(); 988 989 LLVMModuleRef Src = llvm_load_module(false, true); 990 size_t SourceFileLen; 991 const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen); 992 size_t ModuleIdentLen; 993 const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen); 994 LLVMContextRef Ctx = LLVMContextCreate(); 995 LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx); 996 997 LLVMSetSourceFileName(M, SourceFileName, SourceFileLen); 998 LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen); 999 1000 LLVMSetTarget(M, LLVMGetTarget(Src)); 1001 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src)); 1002 if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src))) 1003 report_fatal_error("Inconsistent DataLayout string representation"); 1004 1005 size_t ModuleInlineAsmLen; 1006 const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen); 1007 LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen); 1008 1009 declare_symbols(Src, M); 1010 clone_symbols(Src, M); 1011 char *Str = LLVMPrintModuleToString(M); 1012 fputs(Str, stdout); 1013 1014 LLVMDisposeMessage(Str); 1015 LLVMDisposeModule(Src); 1016 LLVMDisposeModule(M); 1017 LLVMContextDispose(Ctx); 1018 1019 return 0; 1020 } 1021