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