LLDB mainline
|
Transforms the IR for a function to run in the target. More...
#include "lldb/Expression/IRForTarget.h"
Classes | |
class | FunctionValueCache |
Public Types | |
enum class | LookupResult { Success , Fail , Ignore } |
Public Member Functions | |
IRForTarget (lldb_private::ClangExpressionDeclMap *decl_map, bool resolve_vars, lldb_private::IRExecutionUnit &execution_unit, lldb_private::Stream &error_stream, const char *func_name="$__lldb_expr") | |
Constructor. | |
bool | runOnModule (llvm::Module &llvm_module) |
Run this IR transformer on a single module. | |
Static Public Member Functions | |
static clang::NamedDecl * | DeclForGlobal (const llvm::GlobalValue *global_val, llvm::Module *module) |
A function-level pass to take the generated global value $__lldb_expr_result and make it into a persistent variable. | |
Private Member Functions | |
bool | FixFunctionLinkage (llvm::Function &llvm_function) |
Ensures that the current function's linkage is set to external. | |
clang::NamedDecl * | DeclForGlobal (llvm::GlobalValue *global) |
bool | CreateResultVariable (llvm::Function &llvm_function) |
The top-level pass implementation. | |
bool | RewriteObjCConstString (llvm::GlobalVariable *NSStr, llvm::GlobalVariable *CStr) |
A module-level pass to find Objective-C constant strings and transform them to calls to CFStringCreateWithBytes. | |
bool | RewriteObjCConstStrings () |
The top-level pass implementation. | |
bool | RewriteObjCSelector (llvm::Instruction *selector_load) |
A basic block-level pass to find all Objective-C method calls and rewrite them to use sel_registerName instead of statically allocated selectors. | |
bool | RewriteObjCSelectors (llvm::BasicBlock &basic_block) |
The top-level pass implementation. | |
bool | RewritePersistentAlloc (llvm::Instruction *persistent_alloc) |
A basic block-level pass to find all newly-declared persistent variables and register them with the ClangExprDeclMap. | |
bool | RewritePersistentAllocs (llvm::BasicBlock &basic_block) |
The top-level pass implementation. | |
bool | MaybeHandleVariable (llvm::Value *value) |
A function-level pass to find all external variables and functions used in the IR. | |
bool | HandleSymbol (llvm::Value *symbol) |
Handle a single externally-defined symbol. | |
bool | HandleObjCClass (llvm::Value *classlist_reference) |
Handle a single externally-defined Objective-C class. | |
bool | MaybeHandleCallArguments (llvm::CallInst *call_inst) |
Handle all the arguments to a function call. | |
bool | ResolveCalls (llvm::BasicBlock &basic_block) |
Resolve variable references in calls to external functions. | |
bool | RemoveCXAAtExit (llvm::BasicBlock &basic_block) |
Remove calls to __cxa_atexit, which should never be generated by expressions. | |
bool | ResolveExternals (llvm::Function &llvm_function) |
The top-level pass implementation. | |
void | TurnGuardLoadIntoZero (llvm::Instruction *guard_load) |
A basic block-level pass to excise guard variables from the code. | |
bool | RemoveGuards (llvm::BasicBlock &basic_block) |
The top-level pass implementation. | |
bool | ReplaceVariables (llvm::Function &llvm_function) |
A function-level pass to make all external variable references point at the correct offsets from the void* passed into the function. | |
Static Private Member Functions | |
static bool | UnfoldConstant (llvm::Constant *old_constant, llvm::Function *llvm_function, FunctionValueCache &value_maker, FunctionValueCache &entry_instruction_finder, lldb_private::Stream &error_stream) |
UnfoldConstant operates on a constant [Old] which has just been replaced with a value [New]. | |
Private Attributes | |
bool | m_resolve_vars |
True if external variable references and persistent variable references should be resolved. | |
lldb_private::ConstString | m_func_name |
The name of the function to translate. | |
lldb_private::ConstString | m_result_name |
The name of the result variable ($0, $1, ...) | |
lldb_private::TypeFromParser | m_result_type |
The type of the result variable. | |
llvm::Module * | m_module = nullptr |
The module being processed, or NULL if that has not been determined yet. | |
const llvm::DataLayout * | m_target_data = nullptr |
The target data for the module being processed, or nullptr if there is no module. | |
lldb_private::ClangExpressionDeclMap * | m_decl_map |
The DeclMap containing the Decls. | |
llvm::FunctionCallee | m_CFStringCreateWithBytes |
The address of the function CFStringCreateWithBytes, cast to the appropriate function pointer type. | |
llvm::FunctionCallee | m_sel_registerName |
The address of the function sel_registerName, cast to the appropriate function pointer type. | |
llvm::IntegerType * | m_intptr_ty = nullptr |
The type of an integer large enough to hold a pointer. | |
lldb_private::Stream & | m_error_stream |
The stream on which errors should be printed. | |
lldb_private::IRExecutionUnit & | m_execution_unit |
The execution unit containing the IR being created. | |
bool | m_result_is_pointer = false |
True if the function's result in the AST is a pointer (see comments in ASTResultSynthesizer::SynthesizeBodyResult) | |
FunctionValueCache | m_entry_instruction_finder |
Transforms the IR for a function to run in the target.
Once an expression has been parsed and converted to IR, it can run in two contexts: interpreted by LLDB as a DWARF location expression, or compiled by the JIT and inserted into the target process for execution.
IRForTarget makes the second possible, by applying a series of transformations to the IR which make it relocatable. These transformations are discussed in more detail next to their relevant functions.
Definition at line 61 of file IRForTarget.h.
|
strong |
Enumerator | |
---|---|
Success | |
Fail | |
Ignore |
Definition at line 63 of file IRForTarget.h.
IRForTarget::IRForTarget | ( | lldb_private::ClangExpressionDeclMap * | decl_map, |
bool | resolve_vars, | ||
lldb_private::IRExecutionUnit & | execution_unit, | ||
lldb_private::Stream & | error_stream, | ||
const char * | func_name = "$__lldb_expr" |
||
) |
Constructor.
[in] | decl_map | The list of externally-referenced variables for the expression, for use in looking up globals and allocating the argument struct. See the documentation for ClangExpressionDeclMap. |
[in] | resolve_vars | True if the external variable references (including persistent variables) should be resolved. If not, only external functions are resolved. |
[in] | execution_unit | The holder for raw data associated with the expression. |
[in] | error_stream | If non-NULL, a stream on which errors can be printed. |
[in] | func_name | The name of the function to prepare for execution in the target. |
Definition at line 72 of file IRForTarget.cpp.
|
private |
The top-level pass implementation.
[in] | llvm_function | The function currently being processed. |
Definition at line 164 of file IRForTarget.cpp.
References lldb_private::ClangExpressionDeclMap::AddPersistentVariable(), DeclForGlobal(), lldb_private::Stream::Format(), lldb_private::ConstString::GetCString(), lldb_private::StreamString::GetData(), lldb_private::IRMemoryMap::GetTarget(), lldb_private::TypeSystemClang::GetType(), lldb_private::ClangASTSource::GetTypeSystem(), isGuardVariableSymbol(), LLDB_LOG, m_decl_map, m_error_stream, m_execution_unit, m_module, m_resolve_vars, m_result_is_pointer, m_result_name, m_result_type, lldb_private::Stream::Printf(), and PrintValue().
Referenced by runOnModule().
|
static |
A function-level pass to take the generated global value $__lldb_expr_result and make it into a persistent variable.
Also see ASTResultSynthesizer. Find the NamedDecl corresponding to a Value. This interface is exposed for the IR interpreter.
[in] | global_val | The global entity to search for |
[in] | module | The module containing metadata to search |
Referenced by CreateResultVariable(), MaybeHandleVariable(), and ResolveExternals().
|
private |
|
private |
Ensures that the current function's linkage is set to external.
Otherwise the JIT may not return an address for it.
[in] | llvm_function | The function whose linkage is to be fixed. |
Definition at line 106 of file IRForTarget.cpp.
Referenced by runOnModule().
|
private |
Handle a single externally-defined Objective-C class.
[in] | classlist_reference | The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n" where n (if present) is an index. |
Definition at line 1118 of file IRForTarget.cpp.
References lldb::eSymbolTypeObjCClass, lldb_private::ClangExpressionDeclMap::GetSymbolAddress(), LLDB_INVALID_ADDRESS, LLDB_LOG, m_decl_map, and m_intptr_ty.
Referenced by ResolveExternals().
|
private |
Handle a single externally-defined symbol.
[in] | symbol | The symbol. |
Definition at line 1069 of file IRForTarget.cpp.
References lldb::eSymbolTypeAny, lldb_private::ClangExpressionDeclMap::GetSymbolAddress(), LLDB_INVALID_ADDRESS, LLDB_LOG, m_decl_map, m_intptr_ty, and PrintValue().
Referenced by ResolveExternals().
|
private |
Handle all the arguments to a function call.
[in] | call_inst | The call instruction. |
Definition at line 1100 of file IRForTarget.cpp.
References LLDB_LOG, m_error_stream, MaybeHandleVariable(), lldb_private::Stream::Printf(), and PrintValue().
Referenced by ResolveCalls().
|
private |
A function-level pass to find all external variables and functions used in the IR.
Each found external variable is added to the struct, and each external function is resolved in place, its call replaced with a call to a function pointer whose value is the address of the function in the target process. Handle a single externally-defined variable
[in] | value | The variable. |
Definition at line 976 of file IRForTarget.cpp.
References lldb_private::ClangExpressionDeclMap::AddValueToStruct(), DeclForGlobal(), lldb_private::CompilerType::GetByteSize(), lldb_private::CompilerType::GetPointerType(), lldb_private::ClangUtil::GetQualType(), lldb_private::IRMemoryMap::GetTarget(), lldb_private::TypeSystemClang::GetType(), lldb_private::CompilerType::GetTypeBitAlign(), lldb_private::ClangASTSource::GetTypeSystem(), IsObjCSelectorRef(), LLDB_LOG, m_decl_map, m_execution_unit, MaybeHandleVariable(), PrintType(), and PrintValue().
Referenced by MaybeHandleCallArguments(), MaybeHandleVariable(), and ResolveExternals().
|
private |
Remove calls to __cxa_atexit, which should never be generated by expressions.
[in] | basic_block | The basic block currently being processed. |
Definition at line 1173 of file IRForTarget.cpp.
Referenced by runOnModule().
|
private |
The top-level pass implementation.
[in] | basic_block | The basic block currently being processed. |
Definition at line 1283 of file IRForTarget.cpp.
References ExciseGuardStore(), isGuardVariableRef(), and TurnGuardLoadIntoZero().
Referenced by runOnModule().
|
private |
A function-level pass to make all external variable references point at the correct offsets from the void* passed into the function.
ClangExpressionDeclMap::DoStructLayout() must be called beforehand, so that the offsets are valid. The top-level pass implementation
[in] | llvm_function | The function currently being processed. |
Definition at line 1422 of file IRForTarget.cpp.
References lldb_private::ClangExpressionDeclMap::DoStructLayout(), lldb_private::Stream::Format(), lldb_private::ClangExpressionDeclMap::GetStructElement(), lldb_private::ClangExpressionDeclMap::GetStructInfo(), IRForTarget::FunctionValueCache::GetValue(), LLDB_LOG, m_decl_map, m_entry_instruction_finder, m_error_stream, m_module, m_resolve_vars, m_result_is_pointer, m_result_name, lldb_private::Stream::Printf(), PrintValue(), lldb_private::Stream::PutCString(), and UnfoldConstant().
Referenced by runOnModule().
|
private |
Resolve variable references in calls to external functions.
[in] | basic_block | The basic block currently being processed. |
Definition at line 1205 of file IRForTarget.cpp.
References MaybeHandleCallArguments().
Referenced by runOnModule().
|
private |
The top-level pass implementation.
[in] | llvm_function | The function currently being processed. |
Definition at line 1219 of file IRForTarget.cpp.
References DeclForGlobal(), lldb_private::Stream::Format(), HandleObjCClass(), HandleSymbol(), LLDB_LOG, m_error_stream, m_module, MaybeHandleVariable(), and lldb_private::Stream::Printf().
Referenced by runOnModule().
|
private |
A module-level pass to find Objective-C constant strings and transform them to calls to CFStringCreateWithBytes.
Rewrite a single Objective-C constant string.
[in] | NSStr | The constant NSString to be transformed |
[in] | CStr | The constant C string inside the NSString. This will be passed as the bytes argument to CFStringCreateWithBytes. |
Definition at line 402 of file IRForTarget.cpp.
References lldb_private::IRExecutionUnit::FindSymbol(), IRForTarget::FunctionValueCache::GetValue(), LLDB_INVALID_ADDRESS, LLDB_LOG, m_CFStringCreateWithBytes, m_entry_instruction_finder, m_error_stream, m_execution_unit, m_intptr_ty, m_module, lldb_private::Stream::Printf(), and UnfoldConstant().
Referenced by RewriteObjCConstStrings().
|
private |
The top-level pass implementation.
Definition at line 539 of file IRForTarget.cpp.
References LLDB_LOG, m_error_stream, m_module, lldb_private::Stream::Printf(), and RewriteObjCConstString().
Referenced by runOnModule().
|
private |
A basic block-level pass to find all Objective-C method calls and rewrite them to use sel_registerName instead of statically allocated selectors.
The reason is that the selectors are created on the assumption that the Objective-C runtime will scan the appropriate section and prepare them. This doesn't happen when code is copied into the target, though, and there's no easy way to induce the runtime to scan them. So instead we get our selectors from sel_registerName. Replace a single selector reference
[in] | selector_load | The load of the statically-allocated selector. |
Definition at line 730 of file IRForTarget.cpp.
References lldb_private::IRExecutionUnit::FindSymbol(), LLDB_INVALID_ADDRESS, LLDB_LOG, m_execution_unit, m_intptr_ty, m_module, and m_sel_registerName.
Referenced by RewriteObjCSelectors().
|
private |
The top-level pass implementation.
[in] | basic_block | The basic block currently being processed. |
Definition at line 838 of file IRForTarget.cpp.
References IsObjCSelectorRef(), LLDB_LOG, m_error_stream, lldb_private::Stream::Printf(), and RewriteObjCSelector().
Referenced by runOnModule().
|
private |
A basic block-level pass to find all newly-declared persistent variables and register them with the ClangExprDeclMap.
This allows them to be materialized and dematerialized like normal external variables. Before transformation, these persistent variables look like normal locals, so they have an allocation. This pass excises these allocations and makes references look like external references where they will be resolved – like all other external references – by ResolveExternals(). Handle a single allocation of a persistent variable
[in] | persistent_alloc | The allocation of the persistent variable. |
Definition at line 865 of file IRForTarget.cpp.
References lldb_private::ClangExpressionDeclMap::AddPersistentVariable(), lldb_private::TypeSystemClang::GetType(), lldb_private::ClangASTSource::GetTypeSystem(), LLDB_LOG, m_decl_map, m_module, and PrintValue().
Referenced by RewritePersistentAllocs().
|
private |
The top-level pass implementation.
[in] | basic_block | The basic block currently being processed. |
Definition at line 932 of file IRForTarget.cpp.
References LLDB_LOG, m_error_stream, m_resolve_vars, lldb_private::Stream::Printf(), and RewritePersistentAlloc().
Referenced by runOnModule().
bool IRForTarget::runOnModule | ( | llvm::Module & | llvm_module | ) |
Run this IR transformer on a single module.
Implementation of the llvm::ModulePass::runOnModule() function.
[in] | llvm_module | The module to run on. This module is searched for the function $__lldb_expr, and that function is passed to the passes one by one. |
Definition at line 1605 of file IRForTarget.cpp.
References CreateResultVariable(), FixFunctionLinkage(), lldb_private::Stream::Format(), lldb_private::ConstString::GetStringRef(), lldb_private::Log::GetVerbose(), lldb_private::ConstString::IsEmpty(), LLDB_LOG, m_error_stream, m_func_name, m_intptr_ty, m_module, m_target_data, RemoveCXAAtExit(), RemoveGuards(), ReplaceVariables(), ResolveCalls(), ResolveExternals(), RewriteObjCConstStrings(), RewriteObjCSelectors(), and RewritePersistentAllocs().
Referenced by lldb_private::ClangExpressionParser::DoPrepareForExecution().
|
private |
A basic block-level pass to excise guard variables from the code.
The result for the function is passed through Clang as a static variable. Static variables normally have guard variables to ensure that they are only initialized once. Rewrite a load to a guard variable to return constant 0.
[in] | guard_load | The load instruction to zero out. |
Definition at line 1273 of file IRForTarget.cpp.
Referenced by RemoveGuards().
|
staticprivate |
UnfoldConstant operates on a constant [Old] which has just been replaced with a value [New].
We assume that new_value has been properly placed early in the function, in front of the first instruction in the entry basic block [FirstEntryInstruction].
UnfoldConstant reads through the uses of Old and replaces Old in those uses with New. Where those uses are constants, the function generates new instructions to compute the result of the new, non-constant expression and places them before FirstEntryInstruction. These instructions replace the constant uses, so UnfoldConstant calls itself recursively for those.
Definition at line 1310 of file IRForTarget.cpp.
References IRForTarget::FunctionValueCache::GetValue(), lldb_private::Stream::Printf(), PrintValue(), lldb_private::Stream::PutCString(), and UnfoldConstant().
Referenced by ReplaceVariables(), RewriteObjCConstString(), and UnfoldConstant().
|
private |
The address of the function CFStringCreateWithBytes, cast to the appropriate function pointer type.
Definition at line 341 of file IRForTarget.h.
Referenced by RewriteObjCConstString().
|
private |
The DeclMap containing the Decls.
Definition at line 338 of file IRForTarget.h.
Referenced by CreateResultVariable(), HandleObjCClass(), HandleSymbol(), MaybeHandleVariable(), ReplaceVariables(), and RewritePersistentAlloc().
|
private |
Definition at line 369 of file IRForTarget.h.
Referenced by ReplaceVariables(), and RewriteObjCConstString().
|
private |
The stream on which errors should be printed.
Definition at line 348 of file IRForTarget.h.
Referenced by CreateResultVariable(), MaybeHandleCallArguments(), ReplaceVariables(), ResolveExternals(), RewriteObjCConstString(), RewriteObjCConstStrings(), RewriteObjCSelectors(), RewritePersistentAllocs(), and runOnModule().
|
private |
The execution unit containing the IR being created.
Definition at line 350 of file IRForTarget.h.
Referenced by CreateResultVariable(), MaybeHandleVariable(), RewriteObjCConstString(), and RewriteObjCSelector().
|
private |
The name of the function to translate.
Definition at line 327 of file IRForTarget.h.
Referenced by runOnModule().
|
private |
The type of an integer large enough to hold a pointer.
Definition at line 346 of file IRForTarget.h.
Referenced by HandleObjCClass(), HandleSymbol(), RewriteObjCConstString(), RewriteObjCSelector(), and runOnModule().
|
private |
The module being processed, or NULL if that has not been determined yet.
Definition at line 333 of file IRForTarget.h.
Referenced by CreateResultVariable(), ReplaceVariables(), ResolveExternals(), RewriteObjCConstString(), RewriteObjCConstStrings(), RewriteObjCSelector(), RewritePersistentAlloc(), and runOnModule().
|
private |
True if external variable references and persistent variable references should be resolved.
Definition at line 325 of file IRForTarget.h.
Referenced by CreateResultVariable(), ReplaceVariables(), and RewritePersistentAllocs().
|
private |
True if the function's result in the AST is a pointer (see comments in ASTResultSynthesizer::SynthesizeBodyResult)
Definition at line 353 of file IRForTarget.h.
Referenced by CreateResultVariable(), and ReplaceVariables().
|
private |
The name of the result variable ($0, $1, ...)
Definition at line 329 of file IRForTarget.h.
Referenced by CreateResultVariable(), and ReplaceVariables().
|
private |
The type of the result variable.
Definition at line 331 of file IRForTarget.h.
Referenced by CreateResultVariable().
|
private |
The address of the function sel_registerName, cast to the appropriate function pointer type.
Definition at line 344 of file IRForTarget.h.
Referenced by RewriteObjCSelector().
|
private |
The target data for the module being processed, or nullptr if there is no module.
Definition at line 336 of file IRForTarget.h.
Referenced by runOnModule().