1*bdd1243dSDimitry Andric //===-- EmulateInstructionRISCV.h -----------------------------------------===// 2*bdd1243dSDimitry Andric // 3*bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*bdd1243dSDimitry Andric // 7*bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 8*bdd1243dSDimitry Andric 9*bdd1243dSDimitry Andric #ifndef LLDB_SOURCE_PLUGINS_INSTRUCTION_RISCV_EMULATEINSTRUCTIONRISCV_H 10*bdd1243dSDimitry Andric #define LLDB_SOURCE_PLUGINS_INSTRUCTION_RISCV_EMULATEINSTRUCTIONRISCV_H 11*bdd1243dSDimitry Andric 12*bdd1243dSDimitry Andric #include "RISCVInstructions.h" 13*bdd1243dSDimitry Andric 14*bdd1243dSDimitry Andric #include "lldb/Core/EmulateInstruction.h" 15*bdd1243dSDimitry Andric #include "lldb/Interpreter/OptionValue.h" 16*bdd1243dSDimitry Andric #include "lldb/Utility/Log.h" 17*bdd1243dSDimitry Andric #include "lldb/Utility/RegisterValue.h" 18*bdd1243dSDimitry Andric #include "lldb/Utility/Status.h" 19*bdd1243dSDimitry Andric #include <optional> 20*bdd1243dSDimitry Andric 21*bdd1243dSDimitry Andric namespace lldb_private { 22*bdd1243dSDimitry Andric 23*bdd1243dSDimitry Andric class EmulateInstructionRISCV : public EmulateInstruction { 24*bdd1243dSDimitry Andric public: 25*bdd1243dSDimitry Andric static llvm::StringRef GetPluginNameStatic() { return "riscv"; } 26*bdd1243dSDimitry Andric 27*bdd1243dSDimitry Andric static llvm::StringRef GetPluginDescriptionStatic() { 28*bdd1243dSDimitry Andric return "Emulate instructions for the RISC-V architecture."; 29*bdd1243dSDimitry Andric } 30*bdd1243dSDimitry Andric 31*bdd1243dSDimitry Andric static bool SupportsThisInstructionType(InstructionType inst_type) { 32*bdd1243dSDimitry Andric switch (inst_type) { 33*bdd1243dSDimitry Andric case eInstructionTypeAny: 34*bdd1243dSDimitry Andric case eInstructionTypePCModifying: 35*bdd1243dSDimitry Andric return true; 36*bdd1243dSDimitry Andric case eInstructionTypePrologueEpilogue: 37*bdd1243dSDimitry Andric case eInstructionTypeAll: 38*bdd1243dSDimitry Andric return false; 39*bdd1243dSDimitry Andric } 40*bdd1243dSDimitry Andric llvm_unreachable("Fully covered switch above!"); 41*bdd1243dSDimitry Andric } 42*bdd1243dSDimitry Andric 43*bdd1243dSDimitry Andric static bool SupportsThisArch(const ArchSpec &arch); 44*bdd1243dSDimitry Andric 45*bdd1243dSDimitry Andric static lldb_private::EmulateInstruction * 46*bdd1243dSDimitry Andric CreateInstance(const lldb_private::ArchSpec &arch, InstructionType inst_type); 47*bdd1243dSDimitry Andric 48*bdd1243dSDimitry Andric static void Initialize(); 49*bdd1243dSDimitry Andric 50*bdd1243dSDimitry Andric static void Terminate(); 51*bdd1243dSDimitry Andric 52*bdd1243dSDimitry Andric public: 53*bdd1243dSDimitry Andric EmulateInstructionRISCV(const ArchSpec &arch) : EmulateInstruction(arch) {} 54*bdd1243dSDimitry Andric 55*bdd1243dSDimitry Andric llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); } 56*bdd1243dSDimitry Andric 57*bdd1243dSDimitry Andric bool SupportsEmulatingInstructionsOfType(InstructionType inst_type) override { 58*bdd1243dSDimitry Andric return SupportsThisInstructionType(inst_type); 59*bdd1243dSDimitry Andric } 60*bdd1243dSDimitry Andric 61*bdd1243dSDimitry Andric bool SetTargetTriple(const ArchSpec &arch) override; 62*bdd1243dSDimitry Andric bool ReadInstruction() override; 63*bdd1243dSDimitry Andric bool EvaluateInstruction(uint32_t options) override; 64*bdd1243dSDimitry Andric bool TestEmulation(Stream *out_stream, ArchSpec &arch, 65*bdd1243dSDimitry Andric OptionValueDictionary *test_data) override; 66*bdd1243dSDimitry Andric std::optional<RegisterInfo> GetRegisterInfo(lldb::RegisterKind reg_kind, 67*bdd1243dSDimitry Andric uint32_t reg_num) override; 68*bdd1243dSDimitry Andric 69*bdd1243dSDimitry Andric std::optional<lldb::addr_t> ReadPC(); 70*bdd1243dSDimitry Andric bool WritePC(lldb::addr_t pc); 71*bdd1243dSDimitry Andric 72*bdd1243dSDimitry Andric std::optional<DecodeResult> ReadInstructionAt(lldb::addr_t addr); 73*bdd1243dSDimitry Andric std::optional<DecodeResult> Decode(uint32_t inst); 74*bdd1243dSDimitry Andric bool Execute(DecodeResult inst, bool ignore_cond); 75*bdd1243dSDimitry Andric 76*bdd1243dSDimitry Andric template <typename T> 77*bdd1243dSDimitry Andric std::enable_if_t<std::is_integral_v<T>, std::optional<T>> 78*bdd1243dSDimitry Andric ReadMem(uint64_t addr) { 79*bdd1243dSDimitry Andric EmulateInstructionRISCV::Context ctx; 80*bdd1243dSDimitry Andric ctx.type = EmulateInstruction::eContextRegisterLoad; 81*bdd1243dSDimitry Andric ctx.SetNoArgs(); 82*bdd1243dSDimitry Andric bool success = false; 83*bdd1243dSDimitry Andric T result = ReadMemoryUnsigned(ctx, addr, sizeof(T), T(), &success); 84*bdd1243dSDimitry Andric if (!success) 85*bdd1243dSDimitry Andric return {}; // aka return false 86*bdd1243dSDimitry Andric return result; 87*bdd1243dSDimitry Andric } 88*bdd1243dSDimitry Andric 89*bdd1243dSDimitry Andric template <typename T> bool WriteMem(uint64_t addr, uint64_t value) { 90*bdd1243dSDimitry Andric EmulateInstructionRISCV::Context ctx; 91*bdd1243dSDimitry Andric ctx.type = EmulateInstruction::eContextRegisterStore; 92*bdd1243dSDimitry Andric ctx.SetNoArgs(); 93*bdd1243dSDimitry Andric return WriteMemoryUnsigned(ctx, addr, value, sizeof(T)); 94*bdd1243dSDimitry Andric } 95*bdd1243dSDimitry Andric 96*bdd1243dSDimitry Andric llvm::RoundingMode GetRoundingMode(); 97*bdd1243dSDimitry Andric bool SetAccruedExceptions(llvm::APFloatBase::opStatus); 98*bdd1243dSDimitry Andric 99*bdd1243dSDimitry Andric private: 100*bdd1243dSDimitry Andric /// Last decoded instruction from m_opcode 101*bdd1243dSDimitry Andric DecodeResult m_decoded; 102*bdd1243dSDimitry Andric }; 103*bdd1243dSDimitry Andric 104*bdd1243dSDimitry Andric } // namespace lldb_private 105*bdd1243dSDimitry Andric 106*bdd1243dSDimitry Andric #endif // LLDB_SOURCE_PLUGINS_INSTRUCTION_RISCV_EMULATEINSTRUCTIONRISCV_H 107