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