LLDB mainline
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
IRForTarget Class Reference

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.
 
std::unique_ptr< llvm::DataLayout > m_target_data
 The target data for the module being processed, or NULL if there is no module.
 
lldb_private::ClangExpressionDeclMapm_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::Streamm_error_stream
 The stream on which errors should be printed.
 
lldb_private::IRExecutionUnitm_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
 

Detailed Description

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.

Member Enumeration Documentation

◆ LookupResult

enum class IRForTarget::LookupResult
strong
Enumerator
Success 
Fail 
Ignore 

Definition at line 63 of file IRForTarget.h.

Constructor & Destructor Documentation

◆ IRForTarget()

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.

Parameters
[in]decl_mapThe 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_varsTrue if the external variable references (including persistent variables) should be resolved. If not, only external functions are resolved.
[in]execution_unitThe holder for raw data associated with the expression.
[in]error_streamIf non-NULL, a stream on which errors can be printed.
[in]func_nameThe name of the function to prepare for execution in the target.

Definition at line 72 of file IRForTarget.cpp.

Member Function Documentation

◆ CreateResultVariable()

bool IRForTarget::CreateResultVariable ( llvm::Function &  llvm_function)
private

◆ DeclForGlobal() [1/2]

static clang::NamedDecl * IRForTarget::DeclForGlobal ( const llvm::GlobalValue *  global_val,
llvm::Module *  module 
)
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.

Parameters
[in]global_valThe global entity to search for
[in]moduleThe module containing metadata to search
Returns
The corresponding variable declaration

Referenced by CreateResultVariable(), MaybeHandleVariable(), and ResolveExternals().

◆ DeclForGlobal() [2/2]

clang::NamedDecl * IRForTarget::DeclForGlobal ( llvm::GlobalValue *  global)
private

◆ FixFunctionLinkage()

bool IRForTarget::FixFunctionLinkage ( llvm::Function &  llvm_function)
private

Ensures that the current function's linkage is set to external.

Otherwise the JIT may not return an address for it.

Parameters
[in]llvm_functionThe function whose linkage is to be fixed.
Returns
True on success; false otherwise.

Definition at line 106 of file IRForTarget.cpp.

Referenced by runOnModule().

◆ HandleObjCClass()

bool IRForTarget::HandleObjCClass ( llvm::Value *  classlist_reference)
private

Handle a single externally-defined Objective-C class.

Parameters
[in]classlist_referenceThe reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n" where n (if present) is an index.
Returns
True on success; false otherwise

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().

◆ HandleSymbol()

bool IRForTarget::HandleSymbol ( llvm::Value *  symbol)
private

Handle a single externally-defined symbol.

Parameters
[in]symbolThe symbol.
Returns
True on success; false otherwise

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().

◆ MaybeHandleCallArguments()

bool IRForTarget::MaybeHandleCallArguments ( llvm::CallInst *  call_inst)
private

Handle all the arguments to a function call.

Parameters
[in]call_instThe call instruction.
Returns
True on success; false otherwise

Definition at line 1100 of file IRForTarget.cpp.

References LLDB_LOG, m_error_stream, MaybeHandleVariable(), lldb_private::Stream::Printf(), and PrintValue().

Referenced by ResolveCalls().

◆ MaybeHandleVariable()

bool IRForTarget::MaybeHandleVariable ( llvm::Value *  value)
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

Parameters
[in]valueThe variable.
Returns
True on success; false otherwise

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().

◆ RemoveCXAAtExit()

bool IRForTarget::RemoveCXAAtExit ( llvm::BasicBlock &  basic_block)
private

Remove calls to __cxa_atexit, which should never be generated by expressions.

Parameters
[in]basic_blockThe basic block currently being processed.
Returns
True if the scan was successful; false if some operation failed

Definition at line 1173 of file IRForTarget.cpp.

Referenced by runOnModule().

◆ RemoveGuards()

bool IRForTarget::RemoveGuards ( llvm::BasicBlock &  basic_block)
private

The top-level pass implementation.

Parameters
[in]basic_blockThe basic block currently being processed.
Returns
True on success; false otherwise

Definition at line 1283 of file IRForTarget.cpp.

References ExciseGuardStore(), isGuardVariableRef(), and TurnGuardLoadIntoZero().

Referenced by runOnModule().

◆ ReplaceVariables()

bool IRForTarget::ReplaceVariables ( llvm::Function &  llvm_function)
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

Parameters
[in]llvm_functionThe function currently being processed.
Returns
True on success; false otherwise

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().

◆ ResolveCalls()

bool IRForTarget::ResolveCalls ( llvm::BasicBlock &  basic_block)
private

Resolve variable references in calls to external functions.

Parameters
[in]basic_blockThe basic block currently being processed.
Returns
True on success; false otherwise

Definition at line 1205 of file IRForTarget.cpp.

References MaybeHandleCallArguments().

Referenced by runOnModule().

◆ ResolveExternals()

bool IRForTarget::ResolveExternals ( llvm::Function &  llvm_function)
private

The top-level pass implementation.

Parameters
[in]llvm_functionThe function currently being processed.
Returns
True on success; false otherwise

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().

◆ RewriteObjCConstString()

bool IRForTarget::RewriteObjCConstString ( llvm::GlobalVariable *  NSStr,
llvm::GlobalVariable *  CStr 
)
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.

Parameters
[in]NSStrThe constant NSString to be transformed
[in]CStrThe constant C string inside the NSString. This will be passed as the bytes argument to CFStringCreateWithBytes.
Returns
True on success; false otherwise

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().

◆ RewriteObjCConstStrings()

bool IRForTarget::RewriteObjCConstStrings ( )
private

The top-level pass implementation.

Returns
True on success; false otherwise

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().

◆ RewriteObjCSelector()

bool IRForTarget::RewriteObjCSelector ( llvm::Instruction *  selector_load)
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

Parameters
[in]selector_loadThe load of the statically-allocated selector.
Returns
True on success; false otherwise

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().

◆ RewriteObjCSelectors()

bool IRForTarget::RewriteObjCSelectors ( llvm::BasicBlock &  basic_block)
private

The top-level pass implementation.

Parameters
[in]basic_blockThe basic block currently being processed.
Returns
True on success; false otherwise

Definition at line 838 of file IRForTarget.cpp.

References IsObjCSelectorRef(), LLDB_LOG, m_error_stream, lldb_private::Stream::Printf(), and RewriteObjCSelector().

Referenced by runOnModule().

◆ RewritePersistentAlloc()

bool IRForTarget::RewritePersistentAlloc ( llvm::Instruction *  persistent_alloc)
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

Parameters
[in]persistent_allocThe allocation of the persistent variable.
Returns
True on success; false otherwise

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().

◆ RewritePersistentAllocs()

bool IRForTarget::RewritePersistentAllocs ( llvm::BasicBlock &  basic_block)
private

The top-level pass implementation.

Parameters
[in]basic_blockThe 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().

◆ runOnModule()

bool IRForTarget::runOnModule ( llvm::Module &  llvm_module)

Run this IR transformer on a single module.

Implementation of the llvm::ModulePass::runOnModule() function.

Parameters
[in]llvm_moduleThe module to run on. This module is searched for the function $__lldb_expr, and that function is passed to the passes one by one.
Returns
True on success; false otherwise

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::PrepareForExecution().

◆ TurnGuardLoadIntoZero()

void IRForTarget::TurnGuardLoadIntoZero ( llvm::Instruction *  guard_load)
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.

Parameters
[in]guard_loadThe load instruction to zero out.

Definition at line 1273 of file IRForTarget.cpp.

Referenced by RemoveGuards().

◆ UnfoldConstant()

bool IRForTarget::UnfoldConstant ( llvm::Constant *  old_constant,
llvm::Function *  llvm_function,
FunctionValueCache value_maker,
FunctionValueCache entry_instruction_finder,
lldb_private::Stream error_stream 
)
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.

Returns
True on success; false otherwise

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().

Member Data Documentation

◆ m_CFStringCreateWithBytes

llvm::FunctionCallee IRForTarget::m_CFStringCreateWithBytes
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().

◆ m_decl_map

lldb_private::ClangExpressionDeclMap* IRForTarget::m_decl_map
private

The DeclMap containing the Decls.

Definition at line 338 of file IRForTarget.h.

Referenced by CreateResultVariable(), HandleObjCClass(), HandleSymbol(), MaybeHandleVariable(), ReplaceVariables(), and RewritePersistentAlloc().

◆ m_entry_instruction_finder

FunctionValueCache IRForTarget::m_entry_instruction_finder
private

Definition at line 369 of file IRForTarget.h.

Referenced by ReplaceVariables(), and RewriteObjCConstString().

◆ m_error_stream

lldb_private::Stream& IRForTarget::m_error_stream
private

◆ m_execution_unit

lldb_private::IRExecutionUnit& IRForTarget::m_execution_unit
private

The execution unit containing the IR being created.

Definition at line 350 of file IRForTarget.h.

Referenced by CreateResultVariable(), MaybeHandleVariable(), RewriteObjCConstString(), and RewriteObjCSelector().

◆ m_func_name

lldb_private::ConstString IRForTarget::m_func_name
private

The name of the function to translate.

Definition at line 327 of file IRForTarget.h.

Referenced by runOnModule().

◆ m_intptr_ty

llvm::IntegerType* IRForTarget::m_intptr_ty = nullptr
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().

◆ m_module

llvm::Module* IRForTarget::m_module = nullptr
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().

◆ m_resolve_vars

bool IRForTarget::m_resolve_vars
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().

◆ m_result_is_pointer

bool IRForTarget::m_result_is_pointer = false
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().

◆ m_result_name

lldb_private::ConstString IRForTarget::m_result_name
private

The name of the result variable ($0, $1, ...)

Definition at line 329 of file IRForTarget.h.

Referenced by CreateResultVariable(), and ReplaceVariables().

◆ m_result_type

lldb_private::TypeFromParser IRForTarget::m_result_type
private

The type of the result variable.

Definition at line 331 of file IRForTarget.h.

Referenced by CreateResultVariable().

◆ m_sel_registerName

llvm::FunctionCallee IRForTarget::m_sel_registerName
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().

◆ m_target_data

std::unique_ptr<llvm::DataLayout> IRForTarget::m_target_data
private

The target data for the module being processed, or NULL if there is no module.

Definition at line 336 of file IRForTarget.h.

Referenced by runOnModule().


The documentation for this class was generated from the following files: