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