Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 1 | //===-- LLVMUserExpression.h ------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef liblldb_LLVMUserExpression_h |
| 10 | #define liblldb_LLVMUserExpression_h |
| 11 | |
| 12 | #include <map> |
| 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "llvm/IR/LegacyPassManager.h" |
| 17 | |
| 18 | #include "lldb/Expression/UserExpression.h" |
| 19 | |
| 20 | namespace lldb_private { |
| 21 | |
| 22 | /// \class LLVMUserExpression LLVMUserExpression.h |
| 23 | /// "lldb/Expression/LLVMUserExpression.h" Encapsulates a one-time expression |
| 24 | /// for use in lldb. |
| 25 | /// |
| 26 | /// LLDB uses expressions for various purposes, notably to call functions |
| 27 | /// and as a backend for the expr command. LLVMUserExpression is a virtual |
| 28 | /// base class that encapsulates the objects needed to parse and JIT an |
| 29 | /// expression. The actual parsing part will be provided by the specific |
| 30 | /// implementations of LLVMUserExpression - which will be vended through the |
| 31 | /// appropriate TypeSystem. |
| 32 | class LLVMUserExpression : public UserExpression { |
| 33 | public: |
| 34 | /// LLVM-style RTTI support. |
| 35 | static bool classof(const Expression *E) { |
| 36 | return E->getKind() == eKindLLVMUserExpression; |
| 37 | } |
| 38 | |
| 39 | // The IRPasses struct is filled in by a runtime after an expression is |
| 40 | // compiled and can be used to to run fixups/analysis passes as required. |
| 41 | // EarlyPasses are run on the generated module before lldb runs its own IR |
| 42 | // fixups and inserts instrumentation code/pointer checks. LatePasses are run |
| 43 | // after the module has been processed by llvm, before the module is |
| 44 | // assembled and run in the ThreadPlan. |
| 45 | struct IRPasses { |
| 46 | IRPasses() : EarlyPasses(nullptr), LatePasses(nullptr){}; |
| 47 | std::shared_ptr<llvm::legacy::PassManager> EarlyPasses; |
| 48 | std::shared_ptr<llvm::legacy::PassManager> LatePasses; |
| 49 | }; |
| 50 | |
| 51 | LLVMUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr, |
| 52 | llvm::StringRef prefix, lldb::LanguageType language, |
| 53 | ResultType desired_type, |
| 54 | const EvaluateExpressionOptions &options, |
| 55 | ExpressionKind kind); |
| 56 | ~LLVMUserExpression() override; |
| 57 | |
| 58 | bool FinalizeJITExecution( |
| 59 | DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, |
| 60 | lldb::ExpressionVariableSP &result, |
| 61 | lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS, |
| 62 | lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS) override; |
| 63 | |
| 64 | bool CanInterpret() override { return m_can_interpret; } |
| 65 | |
| 66 | /// Return the string that the parser should parse. Must be a full |
| 67 | /// translation unit. |
| 68 | const char *Text() override { return m_transformed_text.c_str(); } |
| 69 | |
| 70 | lldb::ModuleSP GetJITModule() override; |
| 71 | |
| 72 | protected: |
| 73 | lldb::ExpressionResults |
| 74 | DoExecute(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, |
| 75 | const EvaluateExpressionOptions &options, |
| 76 | lldb::UserExpressionSP &shared_ptr_to_me, |
| 77 | lldb::ExpressionVariableSP &result) override; |
| 78 | |
| 79 | virtual void ScanContext(ExecutionContext &exe_ctx, |
| 80 | lldb_private::Status &err) = 0; |
| 81 | |
| 82 | bool PrepareToExecuteJITExpression(DiagnosticManager &diagnostic_manager, |
| 83 | ExecutionContext &exe_ctx, |
| 84 | lldb::addr_t &struct_address); |
| 85 | |
| 86 | virtual bool AddArguments(ExecutionContext &exe_ctx, |
| 87 | std::vector<lldb::addr_t> &args, |
| 88 | lldb::addr_t struct_address, |
| 89 | DiagnosticManager &diagnostic_manager) = 0; |
| 90 | |
| 91 | lldb::addr_t |
| 92 | m_stack_frame_bottom; ///< The bottom of the allocated stack frame. |
| 93 | lldb::addr_t m_stack_frame_top; ///< The top of the allocated stack frame. |
| 94 | |
| 95 | bool m_allow_cxx; ///< True if the language allows C++. |
| 96 | bool m_allow_objc; ///< True if the language allows Objective-C. |
| 97 | std::string |
| 98 | m_transformed_text; ///< The text of the expression, as send to the parser |
| 99 | |
| 100 | std::shared_ptr<IRExecutionUnit> |
| 101 | m_execution_unit_sp; ///< The execution unit the expression is stored in. |
| 102 | std::unique_ptr<Materializer> m_materializer_up; ///< The materializer to use |
| 103 | /// when running the |
| 104 | /// expression. |
| 105 | lldb::ModuleWP m_jit_module_wp; |
| 106 | bool m_enforce_valid_object; ///< True if the expression parser should enforce |
| 107 | ///the presence of a valid class pointer |
| 108 | /// in order to generate the expression as a method. |
| 109 | bool m_in_cplusplus_method; ///< True if the expression is compiled as a C++ |
| 110 | ///member function (true if it was parsed |
| 111 | /// when exe_ctx was in a C++ method). |
| 112 | bool m_in_objectivec_method; ///< True if the expression is compiled as an |
| 113 | ///Objective-C method (true if it was parsed |
| 114 | /// when exe_ctx was in an Objective-C method). |
| 115 | bool m_in_static_method; ///< True if the expression is compiled as a static |
| 116 | ///(or class) method (currently true if it |
| 117 | /// was parsed when exe_ctx was in an Objective-C class method). |
| 118 | bool m_needs_object_ptr; ///< True if "this" or "self" must be looked up and |
| 119 | ///passed in. False if the expression |
| 120 | /// doesn't really use them and they can be NULL. |
| 121 | bool m_const_object; ///< True if "this" is const. |
| 122 | Target *m_target; ///< The target for storing persistent data like types and |
| 123 | ///variables. |
| 124 | |
| 125 | bool m_can_interpret; ///< True if the expression could be evaluated |
| 126 | ///statically; false otherwise. |
| 127 | lldb::addr_t m_materialized_address; ///< The address at which the arguments |
| 128 | ///to the expression have been |
| 129 | ///materialized. |
| 130 | Materializer::DematerializerSP m_dematerializer_sp; ///< The dematerializer. |
| 131 | }; |
| 132 | |
| 133 | } // namespace lldb_private |
| 134 | #endif |