This commit changes the way LLDB executes user
expressions.
Previously, ClangUserExpression assumed that if
there was a constant result for an expression
then it could be determined during parsing. In
particular, the IRInterpreter ran while parser
state (in particular, ClangExpressionDeclMap)
was present. This approach is flawed, because
the IRInterpreter actually is capable of using
external variables, and hence the result might
be different each run. Until now, we papered
over this flaw by re-parsing the expression each
time we ran it.
I have rewritten the IRInterpreter to be
completely independent of the ClangExpressionDeclMap.
Instead of special-casing external variable lookup,
which ties the IRInterpreter closely to LLDB,
we now interpret the exact same IR that the JIT
would see. This IR assumes that materialization
has occurred; hence the recent implementation of the
Materializer, which does not require parser state
(in the form of ClangExpressionDeclMap) to be
present.
Materialization, interpretation, and dematerialization
are now all independent of parsing. This means that
in theory we can parse expressions once and run them
many times. I have three outstanding tasks before
shutting this down:
- First, I will ensure that all of this works with
core files. Core files have a Process but do not
allow allocating memory, which currently confuses
materialization.
- Second, I will make expression breakpoint
conditions remember their ClangUserExpression and
re-use it.
- Third, I will tear out all the redundant code
(for example, materialization logic in
ClangExpressionDeclMap) that is no longer used.
While implementing this fix, I also found a bug in
IRForTarget's handling of floating-point constants.
This should be fixed.
llvm-svn: 179801
diff --git a/lldb/source/Expression/ClangExpressionParser.cpp b/lldb/source/Expression/ClangExpressionParser.cpp
index 2922898..5e4b934 100644
--- a/lldb/source/Expression/ClangExpressionParser.cpp
+++ b/lldb/source/Expression/ClangExpressionParser.cpp
@@ -22,6 +22,7 @@
#include "lldb/Expression/ClangExpressionDeclMap.h"
#include "lldb/Expression/IRExecutionUnit.h"
#include "lldb/Expression/IRDynamicChecks.h"
+#include "lldb/Expression/IRInterpreter.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/ObjCLanguageRuntime.h"
#include "lldb/Target/Process.h"
@@ -464,8 +465,7 @@
lldb::addr_t &func_end,
STD_UNIQUE_PTR(IRExecutionUnit) &execution_unit_ap,
ExecutionContext &exe_ctx,
- bool &evaluated_statically,
- lldb::ClangExpressionVariableSP &const_result,
+ bool &can_interpret,
ExecutionPolicy execution_policy)
{
func_addr = LLDB_INVALID_ADDRESS;
@@ -518,50 +518,39 @@
IRForTarget ir_for_target(decl_map,
m_expr.NeedsVariableResolution(),
- execution_policy,
- const_result,
*m_execution_unit,
error_stream,
function_name.AsCString());
bool ir_can_run = ir_for_target.runOnModule(*m_execution_unit->GetModule());
- Error &interpreter_error(ir_for_target.getInterpreterError());
+ Error interpret_error;
- if (execution_policy != eExecutionPolicyAlways && interpreter_error.Success())
- {
- if (const_result)
- const_result->TransferAddress();
- evaluated_statically = true;
- err.Clear();
- return err;
- }
+ can_interpret = IRInterpreter::CanInterpret(*m_execution_unit->GetModule(), *m_execution_unit->GetFunction(), interpret_error);
Process *process = exe_ctx.GetProcessPtr();
-
- if (!process || execution_policy == eExecutionPolicyNever)
+
+ if (!ir_can_run)
{
- err.SetErrorToGenericError();
- if (execution_policy == eExecutionPolicyAlways)
- err.SetErrorString("Execution needed to run in the target, but the target can't be run");
- else
- err.SetErrorStringWithFormat("Interpreting the expression locally failed: %s", interpreter_error.AsCString());
-
- return err;
- }
- else if (!ir_can_run)
- {
- err.SetErrorToGenericError();
err.SetErrorString("The expression could not be prepared to run in the target");
-
return err;
}
- if (execution_policy != eExecutionPolicyNever &&
- m_expr.NeedsValidation() &&
- process)
+ if (!can_interpret && execution_policy == eExecutionPolicyNever)
{
- if (!process->GetDynamicCheckers())
+ err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
+ return err;
+ }
+
+ if (!process && execution_policy == eExecutionPolicyAlways)
+ {
+ err.SetErrorString("Expression needed to run in the target, but the target can't be run");
+ return err;
+ }
+
+ if (execution_policy == eExecutionPolicyAlways || !can_interpret)
+ {
+ if (!process->GetDynamicCheckers() && m_expr.NeedsValidation())
{
DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
@@ -581,20 +570,24 @@
if (log)
log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
+
+ IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
+
+ if (!ir_dynamic_checks.runOnModule(*m_execution_unit->GetModule()))
+ {
+ err.SetErrorToGenericError();
+ err.SetErrorString("Couldn't add dynamic checks to the expression");
+ return err;
+ }
}
- IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
-
- if (!ir_dynamic_checks.runOnModule(*m_execution_unit->GetModule()))
- {
- err.SetErrorToGenericError();
- err.SetErrorString("Couldn't add dynamic checks to the expression");
- return err;
- }
+ m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
}
}
-
- m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
+ else
+ {
+ m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
+ }
execution_unit_ap.reset (m_execution_unit.release());