Pass a std::unique_ptr& to the create??? methods is lib/Object.
[oota-llvm.git] / lib / Object / IRObjectFile.cpp
1 //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
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 // Part of the IRObjectFile class implementation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/GVMaterializer.h"
17 #include "llvm/IR/Mangler.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Object/IRObjectFile.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22 using namespace object;
23
24 IRObjectFile::IRObjectFile(MemoryBuffer *Object, std::error_code &EC,
25                            LLVMContext &Context)
26     : SymbolicFile(Binary::ID_IR, Object) {
27   ErrorOr<Module *> MOrErr = getLazyBitcodeModule(Object, Context);
28   if ((EC = MOrErr.getError()))
29     return;
30
31   M.reset(MOrErr.get());
32
33   // If we have a DataLayout, setup a mangler.
34   const DataLayout *DL = M->getDataLayout();
35   if (!DL)
36     return;
37
38   Mang.reset(new Mangler(DL));
39 }
40
41 IRObjectFile::~IRObjectFile() { M->getMaterializer()->releaseBuffer(); }
42
43 static const GlobalValue &getGV(DataRefImpl &Symb) {
44   return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
45 }
46
47 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
48   if (I == M.alias_end())
49     return 3;
50   const GlobalValue *GV = &*I;
51   return reinterpret_cast<uintptr_t>(GV) | 2;
52 }
53
54 static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
55   if (I == M.global_end())
56     return skipEmpty(M.alias_begin(), M);
57   const GlobalValue *GV = &*I;
58   return reinterpret_cast<uintptr_t>(GV) | 1;
59 }
60
61 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
62   if (I == M.end())
63     return skipEmpty(M.global_begin(), M);
64   const GlobalValue *GV = &*I;
65   return reinterpret_cast<uintptr_t>(GV) | 0;
66 }
67
68 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
69   const GlobalValue *GV = &getGV(Symb);
70   const Module &M = *GV->getParent();
71   uintptr_t Res;
72   switch (Symb.p & 3) {
73   case 0: {
74     Module::const_iterator Iter(static_cast<const Function*>(GV));
75     ++Iter;
76     Res = skipEmpty(Iter, M);
77     break;
78   }
79   case 1: {
80     Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
81     ++Iter;
82     Res = skipEmpty(Iter, M);
83     break;
84   }
85   case 2: {
86     Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
87     ++Iter;
88     Res = skipEmpty(Iter, M);
89     break;
90   }
91   case 3:
92     llvm_unreachable("Invalid symbol reference");
93   }
94
95   Symb.p = Res;
96 }
97
98 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
99                                               DataRefImpl Symb) const {
100   const GlobalValue &GV = getGV(Symb);
101
102   if (Mang)
103     Mang->getNameWithPrefix(OS, &GV, false);
104   else
105     OS << GV.getName();
106
107   return object_error::success;
108 }
109
110 static bool isDeclaration(const GlobalValue &V) {
111   if (V.hasAvailableExternallyLinkage())
112     return true;
113
114   if (V.isMaterializable())
115     return false;
116
117   return V.isDeclaration();
118 }
119
120 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
121   const GlobalValue &GV = getGV(Symb);
122
123   uint32_t Res = BasicSymbolRef::SF_None;
124   if (isDeclaration(GV))
125     Res |= BasicSymbolRef::SF_Undefined;
126   if (GV.hasPrivateLinkage())
127     Res |= BasicSymbolRef::SF_FormatSpecific;
128   if (!GV.hasLocalLinkage())
129     Res |= BasicSymbolRef::SF_Global;
130   if (GV.hasCommonLinkage())
131     Res |= BasicSymbolRef::SF_Common;
132   if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
133     Res |= BasicSymbolRef::SF_Weak;
134
135   return Res;
136 }
137
138 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
139   const GlobalValue &GV = getGV(Symb);
140   return GV;
141 }
142
143 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
144   Module::const_iterator I = M->begin();
145   DataRefImpl Ret;
146   Ret.p = skipEmpty(I, *M);
147   return basic_symbol_iterator(BasicSymbolRef(Ret, this));
148 }
149
150 basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
151   DataRefImpl Ret;
152   Ret.p = 3;
153   return basic_symbol_iterator(BasicSymbolRef(Ret, this));
154 }
155
156 ErrorOr<SymbolicFile *>
157 llvm::object::SymbolicFile::createIRObjectFile(MemoryBuffer *Object,
158                                                LLVMContext &Context) {
159   std::error_code EC;
160   std::unique_ptr<IRObjectFile> Ret(new IRObjectFile(Object, EC, Context));
161   if (EC)
162     return EC;
163   return Ret.release();
164 }