blob: c7e3713e957a4544cd6c20965537296b94ee5050 [file] [log] [blame]
Sean Callanan1a8d4092010-08-27 01:01:44 +00001//===-- ClangExpressionParser.h ---------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_ClangExpressionParser_h_
11#define liblldb_ClangExpressionParser_h_
12
13#include "lldb/lldb-include.h"
14#include "lldb/Core/ClangForward.h"
15#include "lldb/Core/Error.h"
16
17#include <string>
18#include <vector>
19
20namespace llvm
21{
22 class ExecutionEngine;
23}
24
25namespace lldb_private
26{
27
Sean Callananc3a16002011-01-17 23:42:46 +000028class Process;
Sean Callanan1a8d4092010-08-27 01:01:44 +000029class RecordingMemoryManager;
30
31//----------------------------------------------------------------------
32/// @class ClangExpressionParser ClangExpressionParser.h "lldb/Expression/ClangExpressionParser.h"
33/// @brief Encapsulates an instance of Clang that can parse expressions.
34///
35/// ClangExpressionParser is responsible for preparing an instance of
36/// ClangExpression for execution. ClangExpressionParser uses ClangExpression
37/// as a glorified parameter list, performing the required parsing and
38/// conversion to formats (DWARF bytecode, or JIT compiled machine code)
39/// that can be executed.
40//----------------------------------------------------------------------
41class ClangExpressionParser
42{
43public:
44 //------------------------------------------------------------------
45 /// Constructor
46 ///
47 /// Initializes class variabes.
48 ///
49 /// @param[in] target_triple
50 /// The LLVM-friendly target triple for use in initializing the
51 /// compiler.
52 ///
Sean Callananc3a16002011-01-17 23:42:46 +000053 /// @param[in process
54 /// If non-NULL, the process to customize the expression for
55 /// (e.g., by tuning Objective-C runtime support). May be NULL.
56 ///
Sean Callanan1a8d4092010-08-27 01:01:44 +000057 /// @param[in] expr
58 /// The expression to be parsed.
59 //------------------------------------------------------------------
60 ClangExpressionParser (const char *target_triple,
Sean Callananc3a16002011-01-17 23:42:46 +000061 Process *process,
Sean Callanan1a8d4092010-08-27 01:01:44 +000062 ClangExpression &expr);
63
64 //------------------------------------------------------------------
65 /// Destructor
66 //------------------------------------------------------------------
67 ~ClangExpressionParser ();
68
69 //------------------------------------------------------------------
70 /// Parse a single expression and convert it to IR using Clang. Don't
71 /// wrap the expression in anything at all.
72 ///
73 /// @param[in] stream
74 /// The stream to print errors to.
75 ///
76 /// @return
77 /// The number of errors encountered during parsing. 0 means
78 /// success.
79 //------------------------------------------------------------------
80 unsigned
81 Parse (Stream &stream);
82
83 //------------------------------------------------------------------
84 /// Convert the IR for an already-parsed expression to DWARF if possible.
85 ///
86 /// @param[in] dwarf_opcode_strm
87 /// The stream to place the resulting DWARF code into.
88 ///
89 /// @return
90 /// An error code indicating the success or failure of the operation.
91 /// Test with Success().
92 //------------------------------------------------------------------
93 Error
94 MakeDWARF ();
95
96 //------------------------------------------------------------------
97 /// JIT-compile the IR for an already-parsed expression.
98 ///
99 /// @param[out] func_addr
100 /// The address to which the function has been written.
101 ///
Sean Callanane71d5532010-08-27 23:31:21 +0000102 /// @param[out] func_end
103 /// The end of the function's allocated memory region. (func_addr
104 /// and func_end do not delimit an allocated region; the allocated
105 /// region may begin before func_addr.)
106 ///
107 /// @param[in] exe_ctx
Sean Callanan1a8d4092010-08-27 01:01:44 +0000108 /// The execution context to write the function into.
109 ///
Sean Callanane4ec90e2010-12-16 03:17:46 +0000110 /// @param[out] const_result
111 /// If non-NULL, the result of the expression is constant, and the
112 /// expression has no side effects, this is set to the result of the
113 /// expression.
114 ///
Sean Callanan1a8d4092010-08-27 01:01:44 +0000115 /// @return
116 /// An error code indicating the success or failure of the operation.
117 /// Test with Success().
118 //------------------------------------------------------------------
119 Error
Sean Callanane71d5532010-08-27 23:31:21 +0000120 MakeJIT (lldb::addr_t &func_addr,
121 lldb::addr_t &func_end,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000122 ExecutionContext &exe_ctx,
123 lldb::ClangExpressionVariableSP *const_result = NULL);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000124
125 //------------------------------------------------------------------
126 /// Disassemble the machine code for a JITted function from the target
127 /// process's memory and print the result to a stream.
128 ///
129 /// @param[in] stream
130 /// The stream to print disassembly to.
131 ///
132 /// @param[in] exc_context
133 /// The execution context to get the machine code from.
134 ///
Sean Callanan1a8d4092010-08-27 01:01:44 +0000135 /// @return
136 /// The error generated. If .Success() is true, disassembly succeeded.
137 //------------------------------------------------------------------
138 Error
139 DisassembleFunction (Stream &stream, ExecutionContext &exc_context);
140
141private:
142 //----------------------------------------------------------------------
143 /// @class JittedFunction ClangExpressionParser.h "lldb/Expression/ClangExpressionParser.h"
144 /// @brief Encapsulates a single function that has been generated by the JIT.
145 ///
146 /// Functions that have been generated by the JIT are first resident in the
147 /// local process, and then placed in the target process. JittedFunction
148 /// represents a function possibly resident in both.
149 //----------------------------------------------------------------------
150 struct JittedFunction {
151 std::string m_name; ///< The function's name
152 lldb::addr_t m_local_addr; ///< The address of the function in LLDB's memory
153 lldb::addr_t m_remote_addr; ///< The address of the function in the target's memory
154
155 //------------------------------------------------------------------
156 /// Constructor
157 ///
158 /// Initializes class variabes.
159 ///
160 /// @param[in] name
161 /// The name of the function.
162 ///
163 /// @param[in] local_addr
164 /// The address of the function in LLDB, or LLDB_INVALID_ADDRESS if
165 /// it is not present in LLDB's memory.
166 ///
167 /// @param[in] remote_addr
168 /// The address of the function in the target, or LLDB_INVALID_ADDRESS
169 /// if it is not present in the target's memory.
170 //------------------------------------------------------------------
171 JittedFunction (const char *name,
172 lldb::addr_t local_addr = LLDB_INVALID_ADDRESS,
173 lldb::addr_t remote_addr = LLDB_INVALID_ADDRESS) :
174 m_name (name),
175 m_local_addr (local_addr),
176 m_remote_addr (remote_addr)
177 {
178 }
179 };
180
181 ClangExpression &m_expr; ///< The expression to be parsed
182
183 std::string m_target_triple; ///< The target triple used to initialize LLVM
Sean Callanan79439e82010-11-18 02:56:27 +0000184 std::auto_ptr<clang::FileManager> m_file_manager; ///< The Clang file manager object used by the compiler
Sean Callanan1a8d4092010-08-27 01:01:44 +0000185 std::auto_ptr<clang::CompilerInstance> m_compiler; ///< The Clang compiler used to parse expressions into IR
186 std::auto_ptr<clang::Builtin::Context> m_builtin_context; ///< Context for Clang built-ins
Sean Callanan6abfabf2010-11-19 20:20:02 +0000187 std::auto_ptr<clang::SelectorTable> m_selector_table; ///< Selector table for Objective-C methods
Sean Callanan1a8d4092010-08-27 01:01:44 +0000188 std::auto_ptr<clang::ASTContext> m_ast_context; ///< The AST context used to hold types and names for the parser
189 std::auto_ptr<clang::CodeGenerator> m_code_generator; ///< [owned by the Execution Engine] The Clang object that generates IR
190 RecordingMemoryManager *m_jit_mm; ///< The memory manager for the LLVM JIT
191 std::auto_ptr<llvm::ExecutionEngine> m_execution_engine; ///< The LLVM JIT
192 std::vector<JittedFunction> m_jitted_functions; ///< A vector of all functions that have been JITted into machine code (just one, if ParseExpression() was called)
193};
194
195}
196
197#endif // liblldb_ClangExpressionParser_h_