1 //===-- LLVMUserExpression.h ------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_LLVMUserExpression_h
10 #define liblldb_LLVMUserExpression_h
16 #include "llvm/IR/LegacyPassManager.h"
18 #include "lldb/Expression/UserExpression.h"
20 namespace lldb_private {
22 /// \class LLVMUserExpression LLVMUserExpression.h
23 /// "lldb/Expression/LLVMUserExpression.h" Encapsulates a one-time expression
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 {
37 bool isA(const void *ClassID) const override {
38 return ClassID == &ID || UserExpression::isA(ClassID);
40 static bool classof(const Expression *obj) { return obj->isA(&ID); }
42 // The IRPasses struct is filled in by a runtime after an expression is
43 // compiled and can be used to to run fixups/analysis passes as required.
44 // EarlyPasses are run on the generated module before lldb runs its own IR
45 // fixups and inserts instrumentation code/pointer checks. LatePasses are run
46 // after the module has been processed by llvm, before the module is
47 // assembled and run in the ThreadPlan.
49 IRPasses() : EarlyPasses(nullptr), LatePasses(nullptr){};
50 std::shared_ptr<llvm::legacy::PassManager> EarlyPasses;
51 std::shared_ptr<llvm::legacy::PassManager> LatePasses;
54 LLVMUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr,
55 llvm::StringRef prefix, lldb::LanguageType language,
56 ResultType desired_type,
57 const EvaluateExpressionOptions &options);
58 ~LLVMUserExpression() override;
60 bool FinalizeJITExecution(
61 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
62 lldb::ExpressionVariableSP &result,
63 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS,
64 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS) override;
66 bool CanInterpret() override { return m_can_interpret; }
68 Materializer *GetMaterializer() override { return m_materializer_up.get(); }
70 /// Return the string that the parser should parse. Must be a full
72 const char *Text() override { return m_transformed_text.c_str(); }
74 lldb::ModuleSP GetJITModule() override;
77 lldb::ExpressionResults
78 DoExecute(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
79 const EvaluateExpressionOptions &options,
80 lldb::UserExpressionSP &shared_ptr_to_me,
81 lldb::ExpressionVariableSP &result) override;
83 virtual void ScanContext(ExecutionContext &exe_ctx,
84 lldb_private::Status &err) = 0;
86 bool PrepareToExecuteJITExpression(DiagnosticManager &diagnostic_manager,
87 ExecutionContext &exe_ctx,
88 lldb::addr_t &struct_address);
90 virtual bool AddArguments(ExecutionContext &exe_ctx,
91 std::vector<lldb::addr_t> &args,
92 lldb::addr_t struct_address,
93 DiagnosticManager &diagnostic_manager) = 0;
96 m_stack_frame_bottom; ///< The bottom of the allocated stack frame.
97 lldb::addr_t m_stack_frame_top; ///< The top of the allocated stack frame.
99 bool m_allow_cxx; ///< True if the language allows C++.
100 bool m_allow_objc; ///< True if the language allows Objective-C.
102 m_transformed_text; ///< The text of the expression, as send to the parser
104 std::shared_ptr<IRExecutionUnit>
105 m_execution_unit_sp; ///< The execution unit the expression is stored in.
106 std::unique_ptr<Materializer> m_materializer_up; ///< The materializer to use
109 lldb::ModuleWP m_jit_module_wp;
110 Target *m_target; ///< The target for storing persistent data like types and
113 bool m_can_interpret; ///< True if the expression could be evaluated
114 ///statically; false otherwise.
115 lldb::addr_t m_materialized_address; ///< The address at which the arguments
116 ///to the expression have been
118 Materializer::DematerializerSP m_dematerializer_sp; ///< The dematerializer.
121 } // namespace lldb_private