xref: /openbsd-src/gnu/llvm/lldb/include/lldb/Expression/LLVMUserExpression.h (revision dda2819751e49c83612958492e38917049128b41)
1061da546Spatrick //===-- LLVMUserExpression.h ------------------------------------*- C++ -*-===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9*dda28197Spatrick #ifndef LLDB_EXPRESSION_LLVMUSEREXPRESSION_H
10*dda28197Spatrick #define LLDB_EXPRESSION_LLVMUSEREXPRESSION_H
11061da546Spatrick 
12061da546Spatrick #include <map>
13061da546Spatrick #include <string>
14061da546Spatrick #include <vector>
15061da546Spatrick 
16061da546Spatrick #include "llvm/IR/LegacyPassManager.h"
17061da546Spatrick 
18061da546Spatrick #include "lldb/Expression/UserExpression.h"
19061da546Spatrick 
20061da546Spatrick namespace lldb_private {
21061da546Spatrick 
22061da546Spatrick /// \class LLVMUserExpression LLVMUserExpression.h
23061da546Spatrick /// "lldb/Expression/LLVMUserExpression.h" Encapsulates a one-time expression
24061da546Spatrick /// for use in lldb.
25061da546Spatrick ///
26061da546Spatrick /// LLDB uses expressions for various purposes, notably to call functions
27061da546Spatrick /// and as a backend for the expr command.  LLVMUserExpression is a virtual
28061da546Spatrick /// base class that encapsulates the objects needed to parse and JIT an
29061da546Spatrick /// expression. The actual parsing part will be provided by the specific
30061da546Spatrick /// implementations of LLVMUserExpression - which will be vended through the
31061da546Spatrick /// appropriate TypeSystem.
32061da546Spatrick class LLVMUserExpression : public UserExpression {
33061da546Spatrick   // LLVM RTTI support
34061da546Spatrick   static char ID;
35061da546Spatrick 
36061da546Spatrick public:
isA(const void * ClassID)37061da546Spatrick   bool isA(const void *ClassID) const override {
38061da546Spatrick     return ClassID == &ID || UserExpression::isA(ClassID);
39061da546Spatrick   }
classof(const Expression * obj)40061da546Spatrick   static bool classof(const Expression *obj) { return obj->isA(&ID); }
41061da546Spatrick 
42061da546Spatrick   // The IRPasses struct is filled in by a runtime after an expression is
43061da546Spatrick   // compiled and can be used to to run fixups/analysis passes as required.
44061da546Spatrick   // EarlyPasses are run on the generated module before lldb runs its own IR
45061da546Spatrick   // fixups and inserts instrumentation code/pointer checks. LatePasses are run
46061da546Spatrick   // after the module has been processed by llvm, before the module is
47061da546Spatrick   // assembled and run in the ThreadPlan.
48061da546Spatrick   struct IRPasses {
IRPassesIRPasses49061da546Spatrick     IRPasses() : EarlyPasses(nullptr), LatePasses(nullptr){};
50061da546Spatrick     std::shared_ptr<llvm::legacy::PassManager> EarlyPasses;
51061da546Spatrick     std::shared_ptr<llvm::legacy::PassManager> LatePasses;
52061da546Spatrick   };
53061da546Spatrick 
54061da546Spatrick   LLVMUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr,
55061da546Spatrick                      llvm::StringRef prefix, lldb::LanguageType language,
56061da546Spatrick                      ResultType desired_type,
57061da546Spatrick                      const EvaluateExpressionOptions &options);
58061da546Spatrick   ~LLVMUserExpression() override;
59061da546Spatrick 
60061da546Spatrick   bool FinalizeJITExecution(
61061da546Spatrick       DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
62061da546Spatrick       lldb::ExpressionVariableSP &result,
63061da546Spatrick       lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS,
64061da546Spatrick       lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS) override;
65061da546Spatrick 
CanInterpret()66061da546Spatrick   bool CanInterpret() override { return m_can_interpret; }
67061da546Spatrick 
GetMaterializer()68061da546Spatrick   Materializer *GetMaterializer() override { return m_materializer_up.get(); }
69061da546Spatrick 
70061da546Spatrick   /// Return the string that the parser should parse.  Must be a full
71061da546Spatrick   /// translation unit.
Text()72061da546Spatrick   const char *Text() override { return m_transformed_text.c_str(); }
73061da546Spatrick 
74061da546Spatrick protected:
75061da546Spatrick   lldb::ExpressionResults
76061da546Spatrick   DoExecute(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
77061da546Spatrick             const EvaluateExpressionOptions &options,
78061da546Spatrick             lldb::UserExpressionSP &shared_ptr_to_me,
79061da546Spatrick             lldb::ExpressionVariableSP &result) override;
80061da546Spatrick 
81061da546Spatrick   virtual void ScanContext(ExecutionContext &exe_ctx,
82061da546Spatrick                            lldb_private::Status &err) = 0;
83061da546Spatrick 
84061da546Spatrick   bool PrepareToExecuteJITExpression(DiagnosticManager &diagnostic_manager,
85061da546Spatrick                                      ExecutionContext &exe_ctx,
86061da546Spatrick                                      lldb::addr_t &struct_address);
87061da546Spatrick 
88061da546Spatrick   virtual bool AddArguments(ExecutionContext &exe_ctx,
89061da546Spatrick                             std::vector<lldb::addr_t> &args,
90061da546Spatrick                             lldb::addr_t struct_address,
91061da546Spatrick                             DiagnosticManager &diagnostic_manager) = 0;
92061da546Spatrick 
93061da546Spatrick   lldb::addr_t
94061da546Spatrick       m_stack_frame_bottom;       ///< The bottom of the allocated stack frame.
95061da546Spatrick   lldb::addr_t m_stack_frame_top; ///< The top of the allocated stack frame.
96061da546Spatrick 
97061da546Spatrick   bool m_allow_cxx;  ///< True if the language allows C++.
98061da546Spatrick   bool m_allow_objc; ///< True if the language allows Objective-C.
99061da546Spatrick   std::string
100061da546Spatrick       m_transformed_text; ///< The text of the expression, as send to the parser
101061da546Spatrick 
102061da546Spatrick   std::shared_ptr<IRExecutionUnit>
103061da546Spatrick       m_execution_unit_sp; ///< The execution unit the expression is stored in.
104061da546Spatrick   std::unique_ptr<Materializer> m_materializer_up; ///< The materializer to use
105061da546Spatrick                                                    /// when running the
106061da546Spatrick                                                    /// expression.
107061da546Spatrick   lldb::ModuleWP m_jit_module_wp;
108061da546Spatrick   Target *m_target; ///< The target for storing persistent data like types and
109061da546Spatrick                     ///variables.
110061da546Spatrick 
111061da546Spatrick   bool m_can_interpret; ///< True if the expression could be evaluated
112061da546Spatrick                         ///statically; false otherwise.
113061da546Spatrick   lldb::addr_t m_materialized_address; ///< The address at which the arguments
114061da546Spatrick                                        ///to the expression have been
115061da546Spatrick                                        ///materialized.
116061da546Spatrick   Materializer::DematerializerSP m_dematerializer_sp; ///< The dematerializer.
117061da546Spatrick };
118061da546Spatrick 
119061da546Spatrick } // namespace lldb_private
120061da546Spatrick #endif
121