ClangExpressionParser.h revision 47a5c4c01066ece2d41cba56c3a065eeca12d47c
1//===-- 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
28class RecordingMemoryManager;
29
30//----------------------------------------------------------------------
31/// @class ClangExpressionParser ClangExpressionParser.h "lldb/Expression/ClangExpressionParser.h"
32/// @brief Encapsulates an instance of Clang that can parse expressions.
33///
34/// ClangExpressionParser is responsible for preparing an instance of
35/// ClangExpression for execution.  ClangExpressionParser uses ClangExpression
36/// as a glorified parameter list, performing the required parsing and
37/// conversion to formats (DWARF bytecode, or JIT compiled machine code)
38/// that can be executed.
39//----------------------------------------------------------------------
40class ClangExpressionParser
41{
42public:
43    //------------------------------------------------------------------
44    /// Constructor
45    ///
46    /// Initializes class variabes.
47    ///
48    /// @param[in] target_triple
49    ///     The LLVM-friendly target triple for use in initializing the
50    ///     compiler.
51    ///
52    /// @param[in] expr
53    ///     The expression to be parsed.
54    //------------------------------------------------------------------
55    ClangExpressionParser (const char *target_triple,
56                           ClangExpression &expr);
57
58    //------------------------------------------------------------------
59    /// Destructor
60    //------------------------------------------------------------------
61    ~ClangExpressionParser ();
62
63    //------------------------------------------------------------------
64    /// Parse a single expression and convert it to IR using Clang.  Don't
65    /// wrap the expression in anything at all.
66    ///
67    /// @param[in] stream
68    ///     The stream to print errors to.
69    ///
70    /// @return
71    ///     The number of errors encountered during parsing.  0 means
72    ///     success.
73    //------------------------------------------------------------------
74    unsigned
75    Parse (Stream &stream);
76
77    //------------------------------------------------------------------
78    /// Convert the IR for an already-parsed expression to DWARF if possible.
79    ///
80    /// @param[in] dwarf_opcode_strm
81    ///     The stream to place the resulting DWARF code into.
82    ///
83    /// @return
84    ///     An error code indicating the success or failure of the operation.
85    ///     Test with Success().
86    //------------------------------------------------------------------
87    Error
88    MakeDWARF ();
89
90    //------------------------------------------------------------------
91    /// JIT-compile the IR for an already-parsed expression.
92    ///
93    /// @param[out] func_addr
94    ///     The address to which the function has been written.
95    ///
96    /// @param[out] func_end
97    ///     The end of the function's allocated memory region.  (func_addr
98    ///     and func_end do not delimit an allocated region; the allocated
99    ///     region may begin before func_addr.)
100    ///
101    /// @param[in] exe_ctx
102    ///     The execution context to write the function into.
103    ///
104    /// @return
105    ///     An error code indicating the success or failure of the operation.
106    ///     Test with Success().
107    //------------------------------------------------------------------
108    Error
109    MakeJIT (lldb::addr_t &func_addr,
110             lldb::addr_t &func_end,
111             ExecutionContext &exe_ctx);
112
113    //------------------------------------------------------------------
114    /// Disassemble the machine code for a JITted function from the target
115    /// process's memory and print the result to a stream.
116    ///
117    /// @param[in] stream
118    ///     The stream to print disassembly to.
119    ///
120    /// @param[in] exc_context
121    ///     The execution context to get the machine code from.
122    ///
123    /// @param[in] func_name
124    ///     The name of the function to be disassembled.  By default, the
125    ///     function wrapped by ParseExpression().
126    ///
127    /// @return
128    ///     The error generated.  If .Success() is true, disassembly succeeded.
129    //------------------------------------------------------------------
130    Error
131    DisassembleFunction (Stream &stream, ExecutionContext &exc_context);
132
133private:
134    //----------------------------------------------------------------------
135    /// @class JittedFunction ClangExpressionParser.h "lldb/Expression/ClangExpressionParser.h"
136    /// @brief Encapsulates a single function that has been generated by the JIT.
137    ///
138    /// Functions that have been generated by the JIT are first resident in the
139    /// local process, and then placed in the target process.  JittedFunction
140    /// represents a function possibly resident in both.
141    //----------------------------------------------------------------------
142    struct JittedFunction {
143        std::string m_name;             ///< The function's name
144        lldb::addr_t m_local_addr;      ///< The address of the function in LLDB's memory
145        lldb::addr_t m_remote_addr;     ///< The address of the function in the target's memory
146
147        //------------------------------------------------------------------
148        /// Constructor
149        ///
150        /// Initializes class variabes.
151        ///
152        /// @param[in] name
153        ///     The name of the function.
154        ///
155        /// @param[in] local_addr
156        ///     The address of the function in LLDB, or LLDB_INVALID_ADDRESS if
157        ///     it is not present in LLDB's memory.
158        ///
159        /// @param[in] remote_addr
160        ///     The address of the function in the target, or LLDB_INVALID_ADDRESS
161        ///     if it is not present in the target's memory.
162        //------------------------------------------------------------------
163        JittedFunction (const char *name,
164                        lldb::addr_t local_addr = LLDB_INVALID_ADDRESS,
165                        lldb::addr_t remote_addr = LLDB_INVALID_ADDRESS) :
166            m_name (name),
167            m_local_addr (local_addr),
168            m_remote_addr (remote_addr)
169        {
170        }
171    };
172
173    ClangExpression                            &m_expr;                 ///< The expression to be parsed
174
175    std::string                                 m_target_triple;        ///< The target triple used to initialize LLVM
176    std::auto_ptr<clang::CompilerInstance>      m_compiler;             ///< The Clang compiler used to parse expressions into IR
177    std::auto_ptr<clang::Builtin::Context>      m_builtin_context;      ///< Context for Clang built-ins
178    std::auto_ptr<clang::ASTContext>            m_ast_context;          ///< The AST context used to hold types and names for the parser
179    std::auto_ptr<clang::CodeGenerator>         m_code_generator;       ///< [owned by the Execution Engine] The Clang object that generates IR
180    RecordingMemoryManager                     *m_jit_mm;               ///< The memory manager for the LLVM JIT
181    std::auto_ptr<llvm::ExecutionEngine>        m_execution_engine;     ///< The LLVM JIT
182    std::vector<JittedFunction>                 m_jitted_functions;     ///< A vector of all functions that have been JITted into machine code (just one, if ParseExpression() was called)
183};
184
185}
186
187#endif  // liblldb_ClangExpressionParser_h_
188