aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Commands/CommandObjectExpression.cpp
diff options
context:
space:
mode:
authorSean Callanan <scallanan@apple.com>2010-08-27 01:01:44 +0000
committerSean Callanan <scallanan@apple.com>2010-08-27 01:01:44 +0000
commit1a8d40935d1e545e02149345b24ccbec19ed7db1 (patch)
tree8d91e53e6fc808bc1c4e17afdf55359b26080898 /lldb/source/Commands/CommandObjectExpression.cpp
parent09b263e05cfa6ee42e7213c48114cc1458bccd09 (diff)
downloadllvm-1a8d40935d1e545e02149345b24ccbec19ed7db1.zip
llvm-1a8d40935d1e545e02149345b24ccbec19ed7db1.tar.gz
llvm-1a8d40935d1e545e02149345b24ccbec19ed7db1.tar.bz2
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
Diffstat (limited to 'lldb/source/Commands/CommandObjectExpression.cpp')
-rw-r--r--lldb/source/Commands/CommandObjectExpression.cpp186
1 files changed, 7 insertions, 179 deletions
diff --git a/lldb/source/Commands/CommandObjectExpression.cpp b/lldb/source/Commands/CommandObjectExpression.cpp
index af10410..b29c0c7 100644
--- a/lldb/source/Commands/CommandObjectExpression.cpp
+++ b/lldb/source/Commands/CommandObjectExpression.cpp
@@ -16,10 +16,8 @@
#include "lldb/Interpreter/Args.h"
#include "lldb/Core/Value.h"
#include "lldb/Core/InputReader.h"
-#include "lldb/Expression/ClangExpression.h"
-#include "lldb/Expression/ClangExpressionDeclMap.h"
#include "lldb/Expression/ClangExpressionVariable.h"
-#include "lldb/Expression/ClangPersistentVariables.h"
+#include "lldb/Expression/ClangUserExpression.h"
#include "lldb/Expression/ClangFunction.h"
#include "lldb/Expression/DWARFExpression.h"
#include "lldb/Host/Host.h"
@@ -192,192 +190,22 @@ bool
CommandObjectExpression::EvaluateExpression (const char *expr, bool bare, Stream &output_stream, Stream &error_stream,
CommandReturnObject *result)
{
- Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
-
- ////////////////////////////////////
- // Set up the target and compiler
- //
-
- Target *target = m_exe_ctx.target;
-
- if (!target)
- {
- error_stream.PutCString ("error: invalid target\n");
- return false;
- }
+ ClangUserExpression user_expression (expr);
- ConstString target_triple;
-
- target->GetTargetTriple (target_triple);
-
- if (!target_triple)
- target_triple = Host::GetTargetTriple ();
-
- if (!target_triple)
+ if (!user_expression.Parse (error_stream, m_exe_ctx))
{
- error_stream.PutCString ("error: invalid target triple\n");
+ error_stream.Printf ("Couldn't parse the expresssion");
return false;
}
- ClangExpressionDeclMap expr_decl_map (&m_exe_ctx);
- ClangExpression clang_expr (target_triple.AsCString (), &expr_decl_map);
-
- //////////////////////////
- // Parse the expression
- //
-
- unsigned num_errors;
-
- if (bare)
- num_errors = clang_expr.ParseBareExpression (llvm::StringRef (expr), error_stream);
- else
- num_errors = clang_expr.ParseExpression (expr, error_stream, true);
-
- if (num_errors)
- {
- error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
- return false;
- }
-
- ///////////////////////////////////////////////
- // Convert the output of the parser to DWARF
- //
-
- StreamString dwarf_opcodes;
- dwarf_opcodes.SetByteOrder (eByteOrderHost);
- dwarf_opcodes.GetFlags ().Set (Stream::eBinary);
-
- ClangExpressionVariableList expr_local_vars;
-
- bool success;
- bool canInterpret = false;
-
- ClangExpressionVariable *expr_result = 0;
- Error expr_error;
-
- canInterpret = clang_expr.ConvertIRToDWARF (expr_local_vars, dwarf_opcodes);
-
- if (canInterpret)
- {
- if (log)
- log->Printf("Code can be interpreted.");
- success = true;
- }
- else
- {
- if (log)
- log->Printf("Code cannot be interpreted and must be run in the target.");
- success = clang_expr.PrepareIRForTarget ();
- }
-
- if (!success)
- {
- error_stream.PutCString ("error: expression couldn't be converted to IR\n");
- return false;
- }
+ ClangExpressionVariable *expr_result;
- if (canInterpret)
+ if (!user_expression.Execute (error_stream, m_exe_ctx, expr_result))
{
- // TODO interpret IR
+ error_stream.Printf ("Couldn't execute the expresssion");
return false;
}
- else
- {
- if (!clang_expr.JITFunction ())
- {
- error_stream.PutCString ("error: IR could not be JIT compiled\n");
- return false;
- }
- if (!clang_expr.WriteJITCode (m_exe_ctx))
- {
- error_stream.PutCString ("error: JIT code could not be written to the target\n");
- return false;
- }
-
- lldb::addr_t function_address(clang_expr.GetFunctionAddress ());
-
- if (function_address == LLDB_INVALID_ADDRESS)
- {
- error_stream.PutCString ("JIT compiled code's address couldn't be found\n");
- return false;
- }
-
- lldb::addr_t struct_address;
-
- if (!expr_decl_map.Materialize(&m_exe_ctx, struct_address, expr_error))
- {
- error_stream.Printf ("Couldn't materialize struct: %s\n", expr_error.AsCString("unknown error"));
- return false;
- }
-
- if (log)
- {
- log->Printf("Function address : 0x%llx", (uint64_t)function_address);
- log->Printf("Structure address : 0x%llx", (uint64_t)struct_address);
-
- StreamString insns;
-
- Error err = clang_expr.DisassembleFunction(insns, m_exe_ctx);
-
- if (!err.Success())
- {
- log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
- }
- else
- {
- log->Printf("Function disassembly:\n%s", insns.GetData());
- }
-
- StreamString args;
-
- if (!expr_decl_map.DumpMaterializedStruct(&m_exe_ctx, args, err))
- {
- log->Printf("Couldn't extract variable values : %s", err.AsCString("unknown error"));
- }
- else
- {
- log->Printf("Structure contents:\n%s", args.GetData());
- }
- }
-
- ClangFunction::ExecutionResults execution_result =
- ClangFunction::ExecuteFunction (m_exe_ctx, function_address, struct_address, true, true, 10000, error_stream);
-
- if (execution_result != ClangFunction::eExecutionCompleted)
- {
- const char *result_name;
-
- switch (execution_result)
- {
- case ClangFunction::eExecutionCompleted:
- result_name = "eExecutionCompleted";
- break;
- case ClangFunction::eExecutionDiscarded:
- result_name = "eExecutionDiscarded";
- break;
- case ClangFunction::eExecutionInterrupted:
- result_name = "eExecutionInterrupted";
- break;
- case ClangFunction::eExecutionSetupError:
- result_name = "eExecutionSetupError";
- break;
- case ClangFunction::eExecutionTimedOut:
- result_name = "eExecutionTimedOut";
- break;
- }
-
- error_stream.Printf ("Couldn't execute function; result was %s\n", result_name);
- return false;
- }
-
- if (!expr_decl_map.Dematerialize(&m_exe_ctx, expr_result, expr_error))
- {
- error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
- return false;
- }
- }
-
if (expr_result)
{
StreamString ss;