1*f12b8282SRafael Espindola //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===// 2*f12b8282SRafael Espindola // 3*f12b8282SRafael Espindola // The LLVM Compiler Infrastructure 4*f12b8282SRafael Espindola // 5*f12b8282SRafael Espindola // This file is distributed under the University of Illinois Open Source 6*f12b8282SRafael Espindola // License. See LICENSE.TXT for details. 7*f12b8282SRafael Espindola // 8*f12b8282SRafael Espindola //===----------------------------------------------------------------------===// 9*f12b8282SRafael Espindola // 10*f12b8282SRafael Espindola // Part of the IRObjectFile class implementation. 11*f12b8282SRafael Espindola // 12*f12b8282SRafael Espindola //===----------------------------------------------------------------------===// 13*f12b8282SRafael Espindola 14*f12b8282SRafael Espindola #include "llvm/Bitcode/ReaderWriter.h" 15*f12b8282SRafael Espindola #include "llvm/IR/LLVMContext.h" 16*f12b8282SRafael Espindola #include "llvm/IR/Module.h" 17*f12b8282SRafael Espindola #include "llvm/Object/IRObjectFile.h" 18*f12b8282SRafael Espindola using namespace llvm; 19*f12b8282SRafael Espindola using namespace object; 20*f12b8282SRafael Espindola 21*f12b8282SRafael Espindola IRObjectFile::IRObjectFile(MemoryBuffer *Object, error_code &EC, 22*f12b8282SRafael Espindola LLVMContext &Context, bool BufferOwned) 23*f12b8282SRafael Espindola : SymbolicFile(Binary::ID_IR, Object, BufferOwned) { 24*f12b8282SRafael Espindola ErrorOr<Module*> MOrErr = parseBitcodeFile(Object, Context); 25*f12b8282SRafael Espindola if ((EC = MOrErr.getError())) 26*f12b8282SRafael Espindola return; 27*f12b8282SRafael Espindola 28*f12b8282SRafael Espindola M.reset(MOrErr.get()); 29*f12b8282SRafael Espindola } 30*f12b8282SRafael Espindola 31*f12b8282SRafael Espindola static const GlobalValue &getGV(DataRefImpl &Symb) { 32*f12b8282SRafael Espindola return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3)); 33*f12b8282SRafael Espindola } 34*f12b8282SRafael Espindola 35*f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) { 36*f12b8282SRafael Espindola if (I == M.alias_end()) 37*f12b8282SRafael Espindola return 3; 38*f12b8282SRafael Espindola const GlobalValue *GV = &*I; 39*f12b8282SRafael Espindola return reinterpret_cast<uintptr_t>(GV) | 2; 40*f12b8282SRafael Espindola } 41*f12b8282SRafael Espindola 42*f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) { 43*f12b8282SRafael Espindola if (I == M.global_end()) 44*f12b8282SRafael Espindola return skipEmpty(M.alias_begin(), M); 45*f12b8282SRafael Espindola const GlobalValue *GV = &*I; 46*f12b8282SRafael Espindola return reinterpret_cast<uintptr_t>(GV) | 1; 47*f12b8282SRafael Espindola } 48*f12b8282SRafael Espindola 49*f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) { 50*f12b8282SRafael Espindola if (I == M.end()) 51*f12b8282SRafael Espindola return skipEmpty(M.global_begin(), M); 52*f12b8282SRafael Espindola const GlobalValue *GV = &*I; 53*f12b8282SRafael Espindola return reinterpret_cast<uintptr_t>(GV) | 0; 54*f12b8282SRafael Espindola } 55*f12b8282SRafael Espindola 56*f12b8282SRafael Espindola void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const { 57*f12b8282SRafael Espindola const GlobalValue *GV = &getGV(Symb); 58*f12b8282SRafael Espindola const Module &M = *GV->getParent(); 59*f12b8282SRafael Espindola uintptr_t Res; 60*f12b8282SRafael Espindola switch (Symb.p & 3) { 61*f12b8282SRafael Espindola case 0: { 62*f12b8282SRafael Espindola Module::const_iterator Iter(static_cast<const Function*>(GV)); 63*f12b8282SRafael Espindola ++Iter; 64*f12b8282SRafael Espindola Res = skipEmpty(Iter, M); 65*f12b8282SRafael Espindola break; 66*f12b8282SRafael Espindola } 67*f12b8282SRafael Espindola case 1: { 68*f12b8282SRafael Espindola Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV)); 69*f12b8282SRafael Espindola ++Iter; 70*f12b8282SRafael Espindola Res = skipEmpty(Iter, M); 71*f12b8282SRafael Espindola break; 72*f12b8282SRafael Espindola } 73*f12b8282SRafael Espindola case 2: { 74*f12b8282SRafael Espindola Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV)); 75*f12b8282SRafael Espindola ++Iter; 76*f12b8282SRafael Espindola Res = skipEmpty(Iter, M); 77*f12b8282SRafael Espindola break; 78*f12b8282SRafael Espindola } 79*f12b8282SRafael Espindola case 3: 80*f12b8282SRafael Espindola llvm_unreachable("Invalid symbol reference"); 81*f12b8282SRafael Espindola } 82*f12b8282SRafael Espindola 83*f12b8282SRafael Espindola Symb.p = Res; 84*f12b8282SRafael Espindola } 85*f12b8282SRafael Espindola 86*f12b8282SRafael Espindola error_code IRObjectFile::printSymbolName(raw_ostream &OS, 87*f12b8282SRafael Espindola DataRefImpl Symb) const { 88*f12b8282SRafael Espindola // FIXME: This should use the Mangler. 89*f12b8282SRafael Espindola const GlobalValue &GV = getGV(Symb); 90*f12b8282SRafael Espindola OS << GV.getName(); 91*f12b8282SRafael Espindola return object_error::success; 92*f12b8282SRafael Espindola } 93*f12b8282SRafael Espindola 94*f12b8282SRafael Espindola uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { 95*f12b8282SRafael Espindola const GlobalValue &GV = getGV(Symb); 96*f12b8282SRafael Espindola 97*f12b8282SRafael Espindola uint32_t Res = BasicSymbolRef::SF_None; 98*f12b8282SRafael Espindola if (GV.isDeclaration() || GV.hasAvailableExternallyLinkage()) 99*f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Undefined; 100*f12b8282SRafael Espindola if (GV.hasPrivateLinkage() || GV.hasLinkerPrivateLinkage() || 101*f12b8282SRafael Espindola GV.hasLinkerPrivateWeakLinkage()) 102*f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_FormatSpecific; 103*f12b8282SRafael Espindola if (!GV.hasLocalLinkage()) 104*f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Global; 105*f12b8282SRafael Espindola if (GV.hasCommonLinkage()) 106*f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Common; 107*f12b8282SRafael Espindola if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage()) 108*f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Weak; 109*f12b8282SRafael Espindola 110*f12b8282SRafael Espindola return Res; 111*f12b8282SRafael Espindola } 112*f12b8282SRafael Espindola 113*f12b8282SRafael Espindola const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const { 114*f12b8282SRafael Espindola const GlobalValue &GV = getGV(Symb); 115*f12b8282SRafael Espindola return GV; 116*f12b8282SRafael Espindola } 117*f12b8282SRafael Espindola 118*f12b8282SRafael Espindola basic_symbol_iterator IRObjectFile::symbol_begin_impl() const { 119*f12b8282SRafael Espindola Module::const_iterator I = M->begin(); 120*f12b8282SRafael Espindola DataRefImpl Ret; 121*f12b8282SRafael Espindola Ret.p = skipEmpty(I, *M); 122*f12b8282SRafael Espindola return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 123*f12b8282SRafael Espindola } 124*f12b8282SRafael Espindola 125*f12b8282SRafael Espindola basic_symbol_iterator IRObjectFile::symbol_end_impl() const { 126*f12b8282SRafael Espindola DataRefImpl Ret; 127*f12b8282SRafael Espindola Ret.p = 3; 128*f12b8282SRafael Espindola return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 129*f12b8282SRafael Espindola } 130*f12b8282SRafael Espindola 131*f12b8282SRafael Espindola ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile( 132*f12b8282SRafael Espindola MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) { 133*f12b8282SRafael Espindola error_code EC; 134*f12b8282SRafael Espindola OwningPtr<IRObjectFile> Ret( 135*f12b8282SRafael Espindola new IRObjectFile(Object, EC, Context, BufferOwned)); 136*f12b8282SRafael Espindola if (EC) 137*f12b8282SRafael Espindola return EC; 138*f12b8282SRafael Espindola return Ret.take(); 139*f12b8282SRafael Espindola } 140