2010-07-03 06:22:28 +08:00
|
|
|
//===-- IRToDWARF.cpp -------------------------------------------*- C++ -*-===//
|
2010-07-03 05:09:36 +08:00
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
|
2010-08-19 02:50:51 +08:00
|
|
|
static char ID;
|
|
|
|
|
2010-12-14 10:59:59 +08:00
|
|
|
IRToDWARF::IRToDWARF(lldb_private::ClangExpressionVariableList &local_vars,
|
2010-07-03 05:09:36 +08:00
|
|
|
lldb_private::ClangExpressionDeclMap *decl_map,
|
This is a major refactoring of the expression parser.
The goal is to separate the parser's data from the data
belonging to the parser's clients. This allows clients
to use the parser to obtain (for example) a JIT compiled
function or some DWARF code, and then discard the parser
state.
Previously, parser state was held in ClangExpression and
used liberally by ClangFunction, which inherited from
ClangExpression. The main effects of this refactoring
are:
- reducing ClangExpression to an abstract class that
declares methods that any client must expose to the
expression parser,
- moving the code specific to implementing the "expr"
command from ClangExpression and
CommandObjectExpression into ClangUserExpression,
a new class,
- moving the common parser interaction code from
ClangExpression into ClangExpressionParser, a new
class, and
- making ClangFunction rely only on
ClangExpressionParser and not depend on the
internal implementation of ClangExpression.
Side effects include:
- the compiler interaction code has been factored
out of ClangFunction and is now in an AST pass
(ASTStructExtractor),
- the header file for ClangFunction is now fully
documented,
- several bugs that only popped up when Clang was
deallocated (which never happened, since the
lifetime of the compiler was essentially infinite)
are now fixed, and
- the developer-only "call" command has been
disabled.
I have tested the expr command and the Objective-C
step-into code, which use ClangUserExpression and
ClangFunction, respectively, and verified that they
work. Please let me know if you encounter bugs or
poor documentation.
llvm-svn: 112249
2010-08-27 09:01:44 +08:00
|
|
|
lldb_private::StreamString &strm,
|
|
|
|
const char *func_name) :
|
2010-09-23 11:01:22 +08:00
|
|
|
ModulePass(ID),
|
2011-04-12 03:41:40 +08:00
|
|
|
m_func_name(func_name),
|
This is a major refactoring of the expression parser.
The goal is to separate the parser's data from the data
belonging to the parser's clients. This allows clients
to use the parser to obtain (for example) a JIT compiled
function or some DWARF code, and then discard the parser
state.
Previously, parser state was held in ClangExpression and
used liberally by ClangFunction, which inherited from
ClangExpression. The main effects of this refactoring
are:
- reducing ClangExpression to an abstract class that
declares methods that any client must expose to the
expression parser,
- moving the code specific to implementing the "expr"
command from ClangExpression and
CommandObjectExpression into ClangUserExpression,
a new class,
- moving the common parser interaction code from
ClangExpression into ClangExpressionParser, a new
class, and
- making ClangFunction rely only on
ClangExpressionParser and not depend on the
internal implementation of ClangExpression.
Side effects include:
- the compiler interaction code has been factored
out of ClangFunction and is now in an AST pass
(ASTStructExtractor),
- the header file for ClangFunction is now fully
documented,
- several bugs that only popped up when Clang was
deallocated (which never happened, since the
lifetime of the compiler was essentially infinite)
are now fixed, and
- the developer-only "call" command has been
disabled.
I have tested the expr command and the Objective-C
step-into code, which use ClangUserExpression and
ClangFunction, respectively, and verified that they
work. Please let me know if you encounter bugs or
poor documentation.
llvm-svn: 112249
2010-08-27 09:01:44 +08:00
|
|
|
m_local_vars(local_vars),
|
2010-07-03 05:09:36 +08:00
|
|
|
m_decl_map(decl_map),
|
2011-04-12 03:41:40 +08:00
|
|
|
m_strm(strm)
|
2010-07-03 05:09:36 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2010-07-03 05:28:35 +08:00
|
|
|
swapper << relative;
|
2010-07-03 05:09:36 +08:00
|
|
|
|
2010-07-03 05:28:35 +08:00
|
|
|
// 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));
|
2010-07-03 05:09:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2010-07-28 09:00:59 +08:00
|
|
|
{
|
2010-07-03 05:09:36 +08:00
|
|
|
///////////////////////////////////////
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
// 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)
|
|
|
|
{
|
2010-11-06 09:53:30 +08:00
|
|
|
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
|
2010-07-03 05:09:36 +08:00
|
|
|
|
This is a major refactoring of the expression parser.
The goal is to separate the parser's data from the data
belonging to the parser's clients. This allows clients
to use the parser to obtain (for example) a JIT compiled
function or some DWARF code, and then discard the parser
state.
Previously, parser state was held in ClangExpression and
used liberally by ClangFunction, which inherited from
ClangExpression. The main effects of this refactoring
are:
- reducing ClangExpression to an abstract class that
declares methods that any client must expose to the
expression parser,
- moving the code specific to implementing the "expr"
command from ClangExpression and
CommandObjectExpression into ClangUserExpression,
a new class,
- moving the common parser interaction code from
ClangExpression into ClangExpressionParser, a new
class, and
- making ClangFunction rely only on
ClangExpressionParser and not depend on the
internal implementation of ClangExpression.
Side effects include:
- the compiler interaction code has been factored
out of ClangFunction and is now in an AST pass
(ASTStructExtractor),
- the header file for ClangFunction is now fully
documented,
- several bugs that only popped up when Clang was
deallocated (which never happened, since the
lifetime of the compiler was essentially infinite)
are now fixed, and
- the developer-only "call" command has been
disabled.
I have tested the expr command and the Objective-C
step-into code, which use ClangUserExpression and
ClangFunction, respectively, and verified that they
work. Please let me know if you encounter bugs or
poor documentation.
llvm-svn: 112249
2010-08-27 09:01:44 +08:00
|
|
|
llvm::Function* function = M.getFunction(StringRef(m_func_name.c_str()));
|
2010-07-03 05:09:36 +08:00
|
|
|
|
|
|
|
if (!function)
|
|
|
|
{
|
|
|
|
if (log)
|
This is a major refactoring of the expression parser.
The goal is to separate the parser's data from the data
belonging to the parser's clients. This allows clients
to use the parser to obtain (for example) a JIT compiled
function or some DWARF code, and then discard the parser
state.
Previously, parser state was held in ClangExpression and
used liberally by ClangFunction, which inherited from
ClangExpression. The main effects of this refactoring
are:
- reducing ClangExpression to an abstract class that
declares methods that any client must expose to the
expression parser,
- moving the code specific to implementing the "expr"
command from ClangExpression and
CommandObjectExpression into ClangUserExpression,
a new class,
- moving the common parser interaction code from
ClangExpression into ClangExpressionParser, a new
class, and
- making ClangFunction rely only on
ClangExpressionParser and not depend on the
internal implementation of ClangExpression.
Side effects include:
- the compiler interaction code has been factored
out of ClangFunction and is now in an AST pass
(ASTStructExtractor),
- the header file for ClangFunction is now fully
documented,
- several bugs that only popped up when Clang was
deallocated (which never happened, since the
lifetime of the compiler was essentially infinite)
are now fixed, and
- the developer-only "call" command has been
disabled.
I have tested the expr command and the Objective-C
step-into code, which use ClangUserExpression and
ClangFunction, respectively, and verified that they
work. Please let me know if you encounter bugs or
poor documentation.
llvm-svn: 112249
2010-08-27 09:01:44 +08:00
|
|
|
log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
|
2010-07-03 05:09:36 +08:00
|
|
|
|
This is a major refactoring of the expression parser.
The goal is to separate the parser's data from the data
belonging to the parser's clients. This allows clients
to use the parser to obtain (for example) a JIT compiled
function or some DWARF code, and then discard the parser
state.
Previously, parser state was held in ClangExpression and
used liberally by ClangFunction, which inherited from
ClangExpression. The main effects of this refactoring
are:
- reducing ClangExpression to an abstract class that
declares methods that any client must expose to the
expression parser,
- moving the code specific to implementing the "expr"
command from ClangExpression and
CommandObjectExpression into ClangUserExpression,
a new class,
- moving the common parser interaction code from
ClangExpression into ClangExpressionParser, a new
class, and
- making ClangFunction rely only on
ClangExpressionParser and not depend on the
internal implementation of ClangExpression.
Side effects include:
- the compiler interaction code has been factored
out of ClangFunction and is now in an AST pass
(ASTStructExtractor),
- the header file for ClangFunction is now fully
documented,
- several bugs that only popped up when Clang was
deallocated (which never happened, since the
lifetime of the compiler was essentially infinite)
are now fixed, and
- the developer-only "call" command has been
disabled.
I have tested the expr command and the Objective-C
step-into code, which use ClangUserExpression and
ClangFunction, respectively, and verified that they
work. Please let me know if you encounter bugs or
poor documentation.
llvm-svn: 112249
2010-08-27 09:01:44 +08:00
|
|
|
return false;
|
2010-07-03 05:09:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Relocator relocator;
|
|
|
|
|
2010-07-03 09:35:46 +08:00
|
|
|
if (!runOnBasicBlock(function->getEntryBlock(), relocator))
|
|
|
|
return false;
|
2011-01-22 09:25:40 +08:00
|
|
|
|
|
|
|
if (log && log->GetVerbose())
|
2010-07-28 09:00:59 +08:00
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
raw_string_ostream oss(s);
|
|
|
|
|
|
|
|
M.print(oss, NULL);
|
|
|
|
|
|
|
|
oss.flush();
|
|
|
|
|
2011-01-22 09:25:40 +08:00
|
|
|
log->Printf ("Module being translated to DWARF: \n%s", s.c_str());
|
2010-07-28 09:00:59 +08:00
|
|
|
}
|
|
|
|
|
2010-07-03 09:35:46 +08:00
|
|
|
// TEMPORARY: Fail in order to force execution in the target.
|
|
|
|
return false;
|
2010-07-03 05:09:36 +08:00
|
|
|
|
|
|
|
return relocator.ResolveRelocations(m_strm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IRToDWARF::assignPassManager(PMStack &PMS,
|
|
|
|
PassManagerType T)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PassManagerType
|
|
|
|
IRToDWARF::getPotentialPassManagerType() const
|
|
|
|
{
|
|
|
|
return PMT_ModulePassManager;
|
|
|
|
}
|