1 //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Part of the IRObjectFile class implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Mangler.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Object/IRObjectFile.h"
19 #include "llvm/Support/raw_ostream.h"
21 using namespace object;
23 IRObjectFile::IRObjectFile(MemoryBuffer *Object, std::error_code &EC,
24 LLVMContext &Context, bool BufferOwned)
25 : SymbolicFile(Binary::ID_IR, Object, BufferOwned) {
26 ErrorOr<Module*> MOrErr = parseBitcodeFile(Object, Context);
27 if ((EC = MOrErr.getError()))
30 M.reset(MOrErr.get());
32 // If we have a DataLayout, setup a mangler.
33 const DataLayout *DL = M->getDataLayout();
37 Mang.reset(new Mangler(DL));
40 static const GlobalValue &getGV(DataRefImpl &Symb) {
41 return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
44 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
45 if (I == M.alias_end())
47 const GlobalValue *GV = &*I;
48 return reinterpret_cast<uintptr_t>(GV) | 2;
51 static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
52 if (I == M.global_end())
53 return skipEmpty(M.alias_begin(), M);
54 const GlobalValue *GV = &*I;
55 return reinterpret_cast<uintptr_t>(GV) | 1;
58 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
60 return skipEmpty(M.global_begin(), M);
61 const GlobalValue *GV = &*I;
62 return reinterpret_cast<uintptr_t>(GV) | 0;
65 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
66 const GlobalValue *GV = &getGV(Symb);
67 const Module &M = *GV->getParent();
71 Module::const_iterator Iter(static_cast<const Function*>(GV));
73 Res = skipEmpty(Iter, M);
77 Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
79 Res = skipEmpty(Iter, M);
83 Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
85 Res = skipEmpty(Iter, M);
89 llvm_unreachable("Invalid symbol reference");
95 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
96 DataRefImpl Symb) const {
97 const GlobalValue &GV = getGV(Symb);
100 Mang->getNameWithPrefix(OS, &GV, false);
104 return object_error::success;
107 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
108 const GlobalValue &GV = getGV(Symb);
110 uint32_t Res = BasicSymbolRef::SF_None;
111 if (GV.isDeclaration() || GV.hasAvailableExternallyLinkage())
112 Res |= BasicSymbolRef::SF_Undefined;
113 if (GV.hasPrivateLinkage())
114 Res |= BasicSymbolRef::SF_FormatSpecific;
115 if (!GV.hasLocalLinkage())
116 Res |= BasicSymbolRef::SF_Global;
117 if (GV.hasCommonLinkage())
118 Res |= BasicSymbolRef::SF_Common;
119 if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
120 Res |= BasicSymbolRef::SF_Weak;
125 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
126 const GlobalValue &GV = getGV(Symb);
130 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
131 Module::const_iterator I = M->begin();
133 Ret.p = skipEmpty(I, *M);
134 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
137 basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
140 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
143 ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
144 MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
146 std::unique_ptr<IRObjectFile> Ret(
147 new IRObjectFile(Object, EC, Context, BufferOwned));
150 return Ret.release();