summaryrefslogtreecommitdiffstats
path: root/lldb/source/Expression/IRToDWARF.cpp
blob: af6d1b17b4c532c3ec11dd63d56ae1fba24b880d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//===-- IRToDWARF.cpp -------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Expression/IRToDWARF.h"

#include "llvm/Support/raw_ostream.h"
#include "llvm/InstrTypes.h"
#include "llvm/Module.h"

#include "lldb/Core/dwarf.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/Scalar.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Expression/ClangExpressionDeclMap.h"
#include "lldb/Expression/ClangExpressionVariable.h"

#include <map>

using namespace llvm;

IRToDWARF::IRToDWARF(const void *pid,
                     lldb_private::ClangExpressionVariableList &variable_list, 
                     lldb_private::ClangExpressionDeclMap *decl_map,
                     lldb_private::StreamString &strm) :
    ModulePass(pid),
    m_variable_list(variable_list),
    m_decl_map(decl_map),
    m_strm(strm)
{
}

IRToDWARF::~IRToDWARF()
{
}

class Relocator
{
public:
    Relocator()
    {
    }
    
    ~Relocator()
    {
    }
    
    void MarkBasicBlock(BasicBlock *bb, uint16_t offset)
    {
        m_basic_blocks[bb] = offset;
    }
    
    bool BasicBlockIsMarked(BasicBlock *bb)
    {
        return m_basic_blocks.find(bb) != m_basic_blocks.end();
    }
    
    void MarkRelocation(BasicBlock *bb, uint16_t offset)
    {
        m_relocations[offset] = bb;
    }
    
    bool ResolveRelocations(lldb_private::StreamString &strm)
    {
        std::map<uint16_t, BasicBlock*>::const_iterator iter;
        
        lldb_private::StreamString swapper(0, 32, strm.GetByteOrder());
        
        // This array must be delete [] d at every exit
        size_t temporary_bufsize = strm.GetSize();
        uint8_t *temporary_buffer(new uint8_t[temporary_bufsize]);
        
        memcpy(temporary_buffer, strm.GetData(), temporary_bufsize);
                
        for (iter = m_relocations.begin();
             iter != m_relocations.end();
             ++iter)
        {
            const std::pair<uint16_t, BasicBlock*> &pair = *iter;
            
            uint16_t off = pair.first;
            BasicBlock *bb = pair.second;
            
            if (m_basic_blocks.find(bb) == m_basic_blocks.end())
            {
                delete [] temporary_buffer;
                return false;
            }
                
            uint16_t target_off = m_basic_blocks[bb];
            
            int16_t relative = (int16_t)target_off - (int16_t)off;
            
            swapper.Clear();
            swapper << relative;
            
            // off is intended to be the offset of the branch opcode (which is 
            // what the relative location is added to) so 
            // (temporary_buffer + off + 1) skips the opcode and writes to the
            // relative location
            memcpy(temporary_buffer + off + 1, swapper.GetData(), sizeof(uint16_t));
        }
        
        strm.Clear();
        strm.Write(temporary_buffer, temporary_bufsize);
        
        delete [] temporary_buffer;
        return true;
    }
private:
    std::map<BasicBlock*, uint16_t> m_basic_blocks;
    std::map<uint16_t, BasicBlock*> m_relocations;
};

bool
IRToDWARF::runOnBasicBlock(BasicBlock &BB, Relocator &R)
{
    lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
    
    ///////////////////////////////////////
    // Mark the current block as visited
    //
    
    size_t stream_size = m_strm.GetSize();
    
    if (stream_size > 0xffff)
        return false;
    
    uint16_t offset = stream_size & 0xffff;
    
    R.MarkBasicBlock(&BB, offset);
    
    ////////////////////////////////////////////////
    // Translate the current basic block to DWARF
    //
    
    if (log)
    {
        log->Printf("Translating basic block %s:",
                    BB.hasName() ? BB.getNameStr().c_str() : "[anonymous]");
    
        llvm::BasicBlock::iterator ii;
        
        for (ii = BB.begin();
             ii != BB.end();
             ++ii)
        {
            llvm::Instruction &inst = *ii;
            
            std::string s;
            raw_string_ostream os(s);
            
            inst.print(os);
            
            if (log)
                log->Printf("  %s", s.c_str());
        }
    }
    
    /////////////////////////////////////////////////
    // Visit all successors we haven't visited yet
    //
    
    TerminatorInst *arnold = BB.getTerminator();
    
    if (!arnold)
        return false;
    
    unsigned successor_index;
    unsigned num_successors = arnold->getNumSuccessors();
    
    for (successor_index = 0;
         successor_index < num_successors;
         ++successor_index)
    {
        BasicBlock *successor = arnold->getSuccessor(successor_index);
        
        if (!R.BasicBlockIsMarked(successor))
        {
            if (!runOnBasicBlock(*successor, R))
                return false;
        }
    }
    
    return true;
}

bool
IRToDWARF::runOnModule(Module &M)
{
    lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
    
    llvm::Function* function = M.getFunction(StringRef("___clang_expr"));
    
    if (!function)
    {
        if (log)
            log->Printf("Couldn't find ___clang_expr() in the module");
        
        return 1;
    }
    
    Relocator relocator;
    
    if (!runOnBasicBlock(function->getEntryBlock(), relocator))
        return false;
    
    // TEMPORARY: Fail in order to force execution in the target.
    return false;
    
    return relocator.ResolveRelocations(m_strm);    
}

void
IRToDWARF::assignPassManager(PMStack &PMS,
                                 PassManagerType T)
{
}

PassManagerType
IRToDWARF::getPotentialPassManagerType() const
{
    return PMT_ModulePassManager;
}
OpenPOWER on IntegriCloud