[Lldb-commits] [lldb] r249233 - Add PersistentVariableDelegate to handle language-specific dematerialization.
Sean Callanan via lldb-commits
lldb-commits at lists.llvm.org
Sat Oct 3 02:09:02 PDT 2015
Author: spyffe
Date: Sat Oct 3 04:09:01 2015
New Revision: 249233
URL: http://llvm.org/viewvc/llvm-project?rev=249233&view=rev
Log:
Add PersistentVariableDelegate to handle language-specific dematerialization.
The concept here is that languages may have different ways of communicating
results. In particular, languages may have different names for their result
variables and in fact may have multiple types of result variables (e.g.,
error results). Materializer was tied to one specific model of result handling.
Instead, now UserExpressions can register their own handlers for the result
variables they inject. This allows language-specific code in Materializer to
be moved into the expression parser plug-in, and it simplifies Materializer.
These delegates are subclasses of PersistentVariableDelegate.
PersistentVariableDelegate can provide the name of the result variable, and is
notified when the result variable is populated. It can also be used to touch
persistent variables if need be, updating language-specific state. The
UserExpression owns the delegate and can decide on its result based on
consulting all of its (potentially multiple) delegates.
The user expression itself now makes the determination of what the final result
of the expression is, rather than relying on the Materializer, and I've added a
virtual function to UserExpression to allow this.
Modified:
lldb/trunk/include/lldb/Expression/Materializer.h
lldb/trunk/include/lldb/Expression/UserExpression.h
lldb/trunk/source/Expression/Materializer.cpp
lldb/trunk/source/Expression/UserExpression.cpp
lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h
lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.h
lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
Modified: lldb/trunk/include/lldb/Expression/Materializer.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/Materializer.h?rev=249233&r1=249232&r2=249233&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/Materializer.h (original)
+++ lldb/trunk/include/lldb/Expression/Materializer.h Sat Oct 3 04:09:01 2015
@@ -44,7 +44,6 @@ public:
}
void Dematerialize (Error &err,
- lldb::ExpressionVariableSP &result_sp,
lldb::addr_t frame_top,
lldb::addr_t frame_bottom);
@@ -84,11 +83,28 @@ public:
DematerializerSP Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err);
- uint32_t AddPersistentVariable (lldb::ExpressionVariableSP &persistent_variable_sp, Error &err);
- uint32_t AddVariable (lldb::VariableSP &variable_sp, Error &err);
- uint32_t AddResultVariable (const CompilerType &type, bool is_lvalue, bool keep_in_memory, Error &err);
- uint32_t AddSymbol (const Symbol &symbol_sp, Error &err);
- uint32_t AddRegister (const RegisterInfo ®ister_info, Error &err);
+ class PersistentVariableDelegate
+ {
+ public:
+ virtual ~PersistentVariableDelegate();
+ virtual ConstString GetName() = 0;
+ virtual void DidDematerialize(lldb::ExpressionVariableSP &variable) = 0;
+ };
+
+ uint32_t AddPersistentVariable (lldb::ExpressionVariableSP &persistent_variable_sp,
+ PersistentVariableDelegate *delegate,
+ Error &err);
+ uint32_t AddVariable (lldb::VariableSP &variable_sp,
+ Error &err);
+ uint32_t AddResultVariable (const CompilerType &type,
+ bool is_lvalue,
+ bool keep_in_memory,
+ PersistentVariableDelegate *delegate,
+ Error &err);
+ uint32_t AddSymbol (const Symbol &symbol_sp,
+ Error &err);
+ uint32_t AddRegister (const RegisterInfo ®ister_info,
+ Error &err);
uint32_t GetStructAlignment ()
{
@@ -100,14 +116,6 @@ public:
return m_current_offset;
}
- uint32_t GetResultOffset ()
- {
- if (m_result_entity)
- return m_result_entity->GetOffset();
- else
- return UINT32_MAX;
- }
-
class Entity
{
public:
@@ -163,7 +171,6 @@ private:
DematerializerWP m_dematerializer_wp;
EntityVector m_entities;
- Entity *m_result_entity;
uint32_t m_current_offset;
uint32_t m_struct_alignment;
};
Modified: lldb/trunk/include/lldb/Expression/UserExpression.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/UserExpression.h?rev=249233&r1=249232&r2=249233&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/UserExpression.h (original)
+++ lldb/trunk/include/lldb/Expression/UserExpression.h Sat Oct 3 04:09:01 2015
@@ -249,6 +249,12 @@ public:
{
return true;
}
+
+ virtual lldb::ExpressionVariableSP
+ GetResultAfterDematerialization(ExecutionContextScope *exe_scope)
+ {
+ return lldb::ExpressionVariableSP();
+ }
//------------------------------------------------------------------
/// Evaluate one expression in the scratch context of the
Modified: lldb/trunk/source/Expression/Materializer.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/Materializer.cpp?rev=249233&r1=249232&r2=249233&view=diff
==============================================================================
--- lldb/trunk/source/Expression/Materializer.cpp (original)
+++ lldb/trunk/source/Expression/Materializer.cpp Sat Oct 3 04:09:01 2015
@@ -65,9 +65,11 @@ Materializer::Entity::SetSizeAndAlignmen
class EntityPersistentVariable : public Materializer::Entity
{
public:
- EntityPersistentVariable (lldb::ExpressionVariableSP &persistent_variable_sp) :
+ EntityPersistentVariable (lldb::ExpressionVariableSP &persistent_variable_sp,
+ Materializer::PersistentVariableDelegate *delegate) :
Entity(),
- m_persistent_variable_sp(persistent_variable_sp)
+ m_persistent_variable_sp(persistent_variable_sp),
+ m_delegate(delegate)
{
// Hard-coding to maximum size of a pointer since persistent variables are materialized by reference
m_size = 8;
@@ -210,6 +212,11 @@ public:
m_persistent_variable_sp->m_flags);
}
+ if (m_delegate)
+ {
+ m_delegate->DidDematerialize(m_persistent_variable_sp);
+ }
+
if ((m_persistent_variable_sp->m_flags & ExpressionVariable::EVIsLLDBAllocated) ||
(m_persistent_variable_sp->m_flags & ExpressionVariable::EVIsProgramReference))
{
@@ -390,13 +397,16 @@ public:
}
private:
lldb::ExpressionVariableSP m_persistent_variable_sp;
+ Materializer::PersistentVariableDelegate *m_delegate;
};
uint32_t
-Materializer::AddPersistentVariable (lldb::ExpressionVariableSP &persistent_variable_sp, Error &err)
+Materializer::AddPersistentVariable (lldb::ExpressionVariableSP &persistent_variable_sp,
+ PersistentVariableDelegate *delegate,
+ Error &err)
{
EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
- iter->reset (new EntityPersistentVariable (persistent_variable_sp));
+ iter->reset (new EntityPersistentVariable (persistent_variable_sp, delegate));
uint32_t ret = AddStructMember(**iter);
(*iter)->SetOffset(ret);
return ret;
@@ -755,13 +765,17 @@ Materializer::AddVariable (lldb::Variabl
class EntityResultVariable : public Materializer::Entity
{
public:
- EntityResultVariable (const CompilerType &type, bool is_program_reference, bool keep_in_memory) :
+ EntityResultVariable (const CompilerType &type,
+ bool is_program_reference,
+ bool keep_in_memory,
+ Materializer::PersistentVariableDelegate *delegate) :
Entity(),
m_type(type),
m_is_program_reference(is_program_reference),
m_keep_in_memory(keep_in_memory),
m_temporary_allocation(LLDB_INVALID_ADDRESS),
- m_temporary_allocation_size(0)
+ m_temporary_allocation_size(0),
+ m_delegate(delegate)
{
// Hard-coding to maximum size of a pointer since all results are materialized by reference
m_size = 8;
@@ -816,17 +830,6 @@ public:
lldb::addr_t frame_bottom,
Error &err)
{
- err.SetErrorString("Tried to detmaterialize a result variable with the normal Dematerialize method");
- }
-
- void Dematerialize (lldb::ExpressionVariableSP &result_variable_sp,
- lldb::StackFrameSP &frame_sp,
- IRMemoryMap &map,
- lldb::addr_t process_address,
- lldb::addr_t frame_top,
- lldb::addr_t frame_bottom,
- Error &err)
- {
err.Clear();
ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
@@ -874,7 +877,7 @@ public:
return;
}
- ConstString name = persistent_state->GetNextPersistentVariableName();
+ ConstString name = m_delegate ? m_delegate->GetName() : persistent_state->GetNextPersistentVariableName();
lldb::ExpressionVariableSP ret = persistent_state->CreatePersistentVariable(exe_scope,
name,
@@ -890,6 +893,11 @@ public:
lldb::ProcessSP process_sp = map.GetBestExecutionContextScope()->CalculateProcess();
+ if (m_delegate)
+ {
+ m_delegate->DidDematerialize(ret);
+ }
+
bool can_persist = (m_is_program_reference && process_sp && process_sp->CanJIT() && !(address >= frame_bottom && address < frame_top));
if (can_persist && m_keep_in_memory)
@@ -914,8 +922,6 @@ public:
err.SetErrorString("Couldn't dematerialize a result variable: couldn't read its memory");
return;
}
-
- result_variable_sp = persistent_state->GetVariable(name);
if (!can_persist || !m_keep_in_memory)
{
@@ -1028,16 +1034,20 @@ private:
lldb::addr_t m_temporary_allocation;
size_t m_temporary_allocation_size;
+ Materializer::PersistentVariableDelegate *m_delegate;
};
uint32_t
-Materializer::AddResultVariable (const CompilerType &type, bool is_program_reference, bool keep_in_memory, Error &err)
+Materializer::AddResultVariable (const CompilerType &type,
+ bool is_program_reference,
+ bool keep_in_memory,
+ PersistentVariableDelegate *delegate,
+ Error &err)
{
EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
- iter->reset (new EntityResultVariable (type, is_program_reference, keep_in_memory));
+ iter->reset (new EntityResultVariable (type, is_program_reference, keep_in_memory, delegate));
uint32_t ret = AddStructMember(**iter);
(*iter)->SetOffset(ret);
- m_result_entity = iter->get();
return ret;
}
@@ -1349,7 +1359,6 @@ Materializer::AddRegister (const Registe
Materializer::Materializer () :
m_dematerializer_wp(),
- m_result_entity(NULL),
m_current_offset(0),
m_struct_alignment(8)
{
@@ -1409,7 +1418,9 @@ Materializer::Materialize (lldb::StackFr
}
void
-Materializer::Dematerializer::Dematerialize (Error &error, lldb::ExpressionVariableSP &result_sp, lldb::addr_t frame_bottom, lldb::addr_t frame_top)
+Materializer::Dematerializer::Dematerialize (Error &error,
+ lldb::addr_t frame_bottom,
+ lldb::addr_t frame_top)
{
lldb::StackFrameSP frame_sp;
@@ -1442,14 +1453,7 @@ Materializer::Dematerializer::Dematerial
for (EntityUP &entity_up : m_materializer->m_entities)
{
- if (entity_up.get() == m_materializer->m_result_entity)
- {
- static_cast<EntityResultVariable*>(m_materializer->m_result_entity)->Dematerialize (result_sp, frame_sp, *m_map, m_process_address, frame_top, frame_bottom, error);
- }
- else
- {
- entity_up->Dematerialize (frame_sp, *m_map, m_process_address, frame_top, frame_bottom, error);
- }
+ entity_up->Dematerialize (frame_sp, *m_map, m_process_address, frame_top, frame_bottom, error);
if (!error.Success())
break;
@@ -1474,3 +1478,8 @@ Materializer::Dematerializer::Wipe ()
m_map = NULL;
m_process_address = LLDB_INVALID_ADDRESS;
}
+
+Materializer::PersistentVariableDelegate::~PersistentVariableDelegate()
+{
+}
+
Modified: lldb/trunk/source/Expression/UserExpression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/UserExpression.cpp?rev=249233&r1=249232&r2=249233&view=diff
==============================================================================
--- lldb/trunk/source/Expression/UserExpression.cpp (original)
+++ lldb/trunk/source/Expression/UserExpression.cpp Sat Oct 3 04:09:01 2015
@@ -265,7 +265,7 @@ UserExpression::FinalizeJITExecution (St
Error dematerialize_error;
- m_dematerializer_sp->Dematerialize(dematerialize_error, result, function_stack_bottom, function_stack_top);
+ m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom, function_stack_top);
if (!dematerialize_error.Success())
{
@@ -273,6 +273,8 @@ UserExpression::FinalizeJITExecution (St
return false;
}
+ result = GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
+
if (result)
result->TransferAddress();
Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp?rev=249233&r1=249232&r2=249233&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp Sat Oct 3 04:09:01 2015
@@ -56,11 +56,14 @@ using namespace lldb;
using namespace lldb_private;
using namespace clang;
-ClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory, ExecutionContext &exe_ctx) :
+ClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory,
+ Materializer::PersistentVariableDelegate *result_delegate,
+ ExecutionContext &exe_ctx) :
ClangASTSource (exe_ctx.GetTargetSP()),
m_found_entities (),
m_struct_members (),
m_keep_result_in_memory (keep_result_in_memory),
+ m_result_delegate (result_delegate),
m_parser_vars (),
m_struct_vars ()
{
@@ -216,8 +219,12 @@ ClangExpressionDeclMap::AddPersistentVar
ast->getASTContext(),
parser_type.GetOpaqueQualType()),
context);
-
- uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type, is_lvalue, m_keep_result_in_memory, err);
+
+ uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type,
+ is_lvalue,
+ m_keep_result_in_memory,
+ m_result_delegate,
+ err);
ClangExpressionVariable *var = new ClangExpressionVariable(exe_ctx.GetBestExecutionContextScope(),
name,
@@ -381,7 +388,7 @@ ClangExpressionDeclMap::AddValueToStruct
if (is_persistent_variable)
{
ExpressionVariableSP var_sp(var->shared_from_this());
- offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, err);
+ offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, nullptr, err);
}
else
{
Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h?rev=249233&r1=249232&r2=249233&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h Sat Oct 3 04:09:01 2015
@@ -71,10 +71,15 @@ public:
/// the result persistent variable, and instead marks the variable
/// as persisting.
///
+ /// @param[in] delegate
+ /// If non-NULL, use this delegate to report result values. This
+ /// allows the client ClangUserExpression to report a result.
+ ///
/// @param[in] exe_ctx
/// The execution context to use when parsing.
//------------------------------------------------------------------
ClangExpressionDeclMap (bool keep_result_in_memory,
+ Materializer::PersistentVariableDelegate *result_delegate,
ExecutionContext &exe_ctx);
//------------------------------------------------------------------
@@ -391,6 +396,7 @@ private:
ExpressionVariableList m_found_entities; ///< All entities that were looked up for the parser.
ExpressionVariableList m_struct_members; ///< All entities that need to be placed in the struct.
bool m_keep_result_in_memory; ///< True if result persistent variables generated by this expression should stay in memory.
+ Materializer::PersistentVariableDelegate *m_result_delegate; ///< If non-NULL, used to report expression results to ClangUserExpression.
//----------------------------------------------------------------------
/// The following values should not live beyond parsing
Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp?rev=249233&r1=249232&r2=249233&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp Sat Oct 3 04:09:01 2015
@@ -336,6 +336,24 @@ ClangUserExpression::Parse (Stream &erro
Error err;
InstallContext(exe_ctx);
+
+ if (Target *target = exe_ctx.GetTargetPtr())
+ {
+ if (PersistentExpressionState *persistent_state = target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC))
+ {
+ m_result_delegate.RegisterPersistentState(persistent_state);
+ }
+ else
+ {
+ error_stream.PutCString ("error: couldn't start parsing (no persistent data)");
+ return false;
+ }
+ }
+ else
+ {
+ error_stream.PutCString ("error: couldn't start parsing (no target)");
+ return false;
+ }
ScanContext(exe_ctx, err);
@@ -424,7 +442,7 @@ ClangUserExpression::Parse (Stream &erro
m_materializer_ap.reset(new Materializer());
- ResetDeclMap(exe_ctx, keep_result_in_memory);
+ ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory);
class OnExit
{
@@ -598,10 +616,16 @@ ClangUserExpression::AddInitialArguments
return true;
}
+lldb::ExpressionVariableSP
+ClangUserExpression::GetResultAfterDematerialization(ExecutionContextScope *exe_scope)
+{
+ return m_result_delegate.GetVariable();
+}
+
void
-ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap(ExecutionContext &exe_ctx, bool keep_result_in_memory)
+ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap(ExecutionContext &exe_ctx, Materializer::PersistentVariableDelegate &delegate, bool keep_result_in_memory)
{
- m_expr_decl_map_up.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
+ m_expr_decl_map_up.reset(new ClangExpressionDeclMap(keep_result_in_memory, &delegate, exe_ctx));
}
clang::ASTConsumer *
@@ -613,3 +637,31 @@ ClangUserExpression::ClangUserExpression
return m_result_synthesizer_up.get();
}
+ClangUserExpression::ResultDelegate::ResultDelegate()
+{
+}
+
+ConstString
+ClangUserExpression::ResultDelegate::GetName()
+{
+ return m_persistent_state->GetNextPersistentVariableName();
+}
+
+void
+ClangUserExpression::ResultDelegate::DidDematerialize(lldb::ExpressionVariableSP &variable)
+{
+ m_variable = variable;
+}
+
+void
+ClangUserExpression::ResultDelegate::RegisterPersistentState(PersistentExpressionState *persistent_state)
+{
+ m_persistent_state = persistent_state;
+}
+
+lldb::ExpressionVariableSP &
+ClangUserExpression::ResultDelegate::GetVariable()
+{
+ return m_variable;
+}
+
Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.h?rev=249233&r1=249232&r2=249233&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.h (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.h Sat Oct 3 04:09:01 2015
@@ -81,7 +81,7 @@ public:
}
void
- ResetDeclMap (ExecutionContext & exe_ctx, bool keep_result_in_memory);
+ ResetDeclMap (ExecutionContext & exe_ctx, Materializer::PersistentVariableDelegate &result_delegate, bool keep_result_in_memory);
//------------------------------------------------------------------
/// Return the object that the parser should allow to access ASTs.
@@ -177,10 +177,13 @@ public:
}
void
- ResetDeclMap (ExecutionContext & exe_ctx, bool keep_result_in_memory)
+ ResetDeclMap (ExecutionContext & exe_ctx, Materializer::PersistentVariableDelegate &result_delegate, bool keep_result_in_memory)
{
- m_type_system_helper.ResetDeclMap(exe_ctx, keep_result_in_memory);
+ m_type_system_helper.ResetDeclMap(exe_ctx, result_delegate, keep_result_in_memory);
}
+
+ lldb::ExpressionVariableSP
+ GetResultAfterDematerialization(ExecutionContextScope *exe_scope) override;
private:
//------------------------------------------------------------------
@@ -197,6 +200,22 @@ private:
Stream &error_stream) override;
ClangUserExpressionHelper m_type_system_helper;
+
+ class ResultDelegate : public Materializer::PersistentVariableDelegate
+ {
+ public:
+ ResultDelegate();
+ ConstString GetName() override;
+ void DidDematerialize(lldb::ExpressionVariableSP &variable) override;
+
+ void RegisterPersistentState(PersistentExpressionState *persistent_state);
+ lldb::ExpressionVariableSP &GetVariable();
+ private:
+ PersistentExpressionState *m_persistent_state;
+ lldb::ExpressionVariableSP m_variable;
+ };
+
+ ResultDelegate m_result_delegate;
};
} // namespace lldb_private
Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp?rev=249233&r1=249232&r2=249233&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp Sat Oct 3 04:09:01 2015
@@ -185,5 +185,5 @@ ClangUtilityFunction::Install (Stream &e
void
ClangUtilityFunction::ClangUtilityFunctionHelper::ResetDeclMap(ExecutionContext &exe_ctx, bool keep_result_in_memory)
{
- m_expr_decl_map_up.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
+ m_expr_decl_map_up.reset(new ClangExpressionDeclMap(keep_result_in_memory, nullptr, exe_ctx));
}
More information about the lldb-commits
mailing list