blob: 0cca9aa136a40f49caf4c3c7c65ce2f226903f2a [file] [log] [blame]
Sean Callanan051052f2010-07-02 22:22:28 +00001//===-- IRToDWARF.cpp -------------------------------------------*- C++ -*-===//
Sean Callanandcb658b2010-07-02 21:09:36 +00002//
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#include "lldb/Expression/IRToDWARF.h"
11
12#include "llvm/Support/raw_ostream.h"
13#include "llvm/InstrTypes.h"
14#include "llvm/Module.h"
15
16#include "lldb/Core/dwarf.h"
17#include "lldb/Core/Log.h"
18#include "lldb/Core/Scalar.h"
19#include "lldb/Core/StreamString.h"
20#include "lldb/Expression/ClangExpressionDeclMap.h"
21#include "lldb/Expression/ClangExpressionVariable.h"
22
23#include <map>
24
25using namespace llvm;
26
27IRToDWARF::IRToDWARF(const void *pid,
28 lldb_private::ClangExpressionVariableList &variable_list,
29 lldb_private::ClangExpressionDeclMap *decl_map,
30 lldb_private::StreamString &strm) :
31 ModulePass(pid),
32 m_variable_list(variable_list),
33 m_decl_map(decl_map),
34 m_strm(strm)
35{
36}
37
38IRToDWARF::~IRToDWARF()
39{
40}
41
42class Relocator
43{
44public:
45 Relocator()
46 {
47 }
48
49 ~Relocator()
50 {
51 }
52
53 void MarkBasicBlock(BasicBlock *bb, uint16_t offset)
54 {
55 m_basic_blocks[bb] = offset;
56 }
57
58 bool BasicBlockIsMarked(BasicBlock *bb)
59 {
60 return m_basic_blocks.find(bb) != m_basic_blocks.end();
61 }
62
63 void MarkRelocation(BasicBlock *bb, uint16_t offset)
64 {
65 m_relocations[offset] = bb;
66 }
67
68 bool ResolveRelocations(lldb_private::StreamString &strm)
69 {
70 std::map<uint16_t, BasicBlock*>::const_iterator iter;
71
72 lldb_private::StreamString swapper(0, 32, strm.GetByteOrder());
73
74 // This array must be delete [] d at every exit
75 size_t temporary_bufsize = strm.GetSize();
76 uint8_t *temporary_buffer(new uint8_t[temporary_bufsize]);
77
78 memcpy(temporary_buffer, strm.GetData(), temporary_bufsize);
79
80 for (iter = m_relocations.begin();
81 iter != m_relocations.end();
82 ++iter)
83 {
84 const std::pair<uint16_t, BasicBlock*> &pair = *iter;
85
86 uint16_t off = pair.first;
87 BasicBlock *bb = pair.second;
88
89 if (m_basic_blocks.find(bb) == m_basic_blocks.end())
90 {
91 delete [] temporary_buffer;
92 return false;
93 }
94
95 uint16_t target_off = m_basic_blocks[bb];
96
97 int16_t relative = (int16_t)target_off - (int16_t)off;
98
99 swapper.Clear();
Sean Callanan702abd62010-07-02 21:28:35 +0000100 swapper << relative;
Sean Callanandcb658b2010-07-02 21:09:36 +0000101
Sean Callanan702abd62010-07-02 21:28:35 +0000102 // off is intended to be the offset of the branch opcode (which is
103 // what the relative location is added to) so
104 // (temporary_buffer + off + 1) skips the opcode and writes to the
105 // relative location
106 memcpy(temporary_buffer + off + 1, swapper.GetData(), sizeof(uint16_t));
Sean Callanandcb658b2010-07-02 21:09:36 +0000107 }
108
109 strm.Clear();
110 strm.Write(temporary_buffer, temporary_bufsize);
111
112 delete [] temporary_buffer;
113 return true;
114 }
115private:
116 std::map<BasicBlock*, uint16_t> m_basic_blocks;
117 std::map<uint16_t, BasicBlock*> m_relocations;
118};
119
120bool
121IRToDWARF::runOnBasicBlock(BasicBlock &BB, Relocator &R)
122{
123 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
124
125 ///////////////////////////////////////
126 // Mark the current block as visited
127 //
128
129 size_t stream_size = m_strm.GetSize();
130
131 if (stream_size > 0xffff)
132 return false;
133
134 uint16_t offset = stream_size & 0xffff;
135
136 R.MarkBasicBlock(&BB, offset);
137
138 ////////////////////////////////////////////////
139 // Translate the current basic block to DWARF
140 //
141
142 if (log)
143 {
Sean Callanan051052f2010-07-02 22:22:28 +0000144 log->Printf("Translating basic block %s:",
145 BB.hasName() ? BB.getNameStr().c_str() : "[anonymous]");
Sean Callanandcb658b2010-07-02 21:09:36 +0000146
147 llvm::BasicBlock::iterator ii;
148
149 for (ii = BB.begin();
150 ii != BB.end();
151 ++ii)
152 {
153 llvm::Instruction &inst = *ii;
154
155 std::string s;
156 raw_string_ostream os(s);
157
158 inst.print(os);
159
160 if (log)
161 log->Printf(" %s", s.c_str());
162 }
163 }
164
165 /////////////////////////////////////////////////
166 // Visit all successors we haven't visited yet
167 //
168
169 TerminatorInst *arnold = BB.getTerminator();
170
171 if (!arnold)
172 return false;
173
174 unsigned successor_index;
175 unsigned num_successors = arnold->getNumSuccessors();
176
177 for (successor_index = 0;
178 successor_index < num_successors;
179 ++successor_index)
180 {
181 BasicBlock *successor = arnold->getSuccessor(successor_index);
182
183 if (!R.BasicBlockIsMarked(successor))
184 {
185 if (!runOnBasicBlock(*successor, R))
186 return false;
187 }
188 }
189
190 return true;
191}
192
193bool
194IRToDWARF::runOnModule(Module &M)
195{
196 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
197
198 llvm::Function* function = M.getFunction(StringRef("___clang_expr"));
199
200 if (!function)
201 {
202 if (log)
203 log->Printf("Couldn't find ___clang_expr() in the module");
204
205 return 1;
206 }
207
208 Relocator relocator;
209
210 llvm::BasicBlock &currentBB = function->getEntryBlock();
211
212 runOnBasicBlock(currentBB, relocator);
213
214 return relocator.ResolveRelocations(m_strm);
215}
216
217void
218IRToDWARF::assignPassManager(PMStack &PMS,
219 PassManagerType T)
220{
221}
222
223PassManagerType
224IRToDWARF::getPotentialPassManagerType() const
225{
226 return PMT_ModulePassManager;
227}