Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ABI/RISCV/ABISysV_riscv.cpp
39645 views
//===-- ABISysV_riscv.cpp ------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===---------------------------------------------------------------------===//78#include "ABISysV_riscv.h"910#include <array>11#include <limits>1213#include "llvm/IR/DerivedTypes.h"1415#include "Utility/RISCV_DWARF_Registers.h"16#include "lldb/Core/PluginManager.h"17#include "lldb/Core/Value.h"18#include "lldb/Core/ValueObjectConstResult.h"19#include "lldb/Target/RegisterContext.h"20#include "lldb/Target/StackFrame.h"21#include "lldb/Target/Thread.h"22#include "lldb/Utility/RegisterValue.h"2324#define DEFINE_REG_NAME(reg_num) ConstString(#reg_num).GetCString()25#define DEFINE_REG_NAME_STR(reg_name) ConstString(reg_name).GetCString()2627// The ABI is not a source of such information as size, offset, encoding, etc.28// of a register. Just provides correct dwarf and eh_frame numbers.2930#define DEFINE_GENERIC_REGISTER_STUB(dwarf_num, str_name, generic_num) \31{ \32DEFINE_REG_NAME(dwarf_num), DEFINE_REG_NAME_STR(str_name), 0, 0, \33eEncodingInvalid, eFormatDefault, \34{dwarf_num, dwarf_num, generic_num, LLDB_INVALID_REGNUM, dwarf_num}, \35nullptr, nullptr, nullptr, \36}3738#define DEFINE_REGISTER_STUB(dwarf_num, str_name) \39DEFINE_GENERIC_REGISTER_STUB(dwarf_num, str_name, LLDB_INVALID_REGNUM)4041using namespace lldb;42using namespace lldb_private;4344LLDB_PLUGIN_DEFINE_ADV(ABISysV_riscv, ABIRISCV)4546namespace {47namespace dwarf {48enum regnums {49zero,50ra,51sp,52gp,53tp,54t0,55t1,56t2,57fp,58s0 = fp,59s1,60a0,61a1,62a2,63a3,64a4,65a5,66a6,67a7,68s2,69s3,70s4,71s5,72s6,73s7,74s8,75s9,76s10,77s11,78t3,79t4,80t5,81t6,82pc83};8485static const std::array<RegisterInfo, 33> g_register_infos = {86{DEFINE_REGISTER_STUB(zero, nullptr),87DEFINE_GENERIC_REGISTER_STUB(ra, nullptr, LLDB_REGNUM_GENERIC_RA),88DEFINE_GENERIC_REGISTER_STUB(sp, nullptr, LLDB_REGNUM_GENERIC_SP),89DEFINE_REGISTER_STUB(gp, nullptr),90DEFINE_REGISTER_STUB(tp, nullptr),91DEFINE_REGISTER_STUB(t0, nullptr),92DEFINE_REGISTER_STUB(t1, nullptr),93DEFINE_REGISTER_STUB(t2, nullptr),94DEFINE_GENERIC_REGISTER_STUB(fp, nullptr, LLDB_REGNUM_GENERIC_FP),95DEFINE_REGISTER_STUB(s1, nullptr),96DEFINE_GENERIC_REGISTER_STUB(a0, nullptr, LLDB_REGNUM_GENERIC_ARG1),97DEFINE_GENERIC_REGISTER_STUB(a1, nullptr, LLDB_REGNUM_GENERIC_ARG2),98DEFINE_GENERIC_REGISTER_STUB(a2, nullptr, LLDB_REGNUM_GENERIC_ARG3),99DEFINE_GENERIC_REGISTER_STUB(a3, nullptr, LLDB_REGNUM_GENERIC_ARG4),100DEFINE_GENERIC_REGISTER_STUB(a4, nullptr, LLDB_REGNUM_GENERIC_ARG5),101DEFINE_GENERIC_REGISTER_STUB(a5, nullptr, LLDB_REGNUM_GENERIC_ARG6),102DEFINE_GENERIC_REGISTER_STUB(a6, nullptr, LLDB_REGNUM_GENERIC_ARG7),103DEFINE_GENERIC_REGISTER_STUB(a7, nullptr, LLDB_REGNUM_GENERIC_ARG8),104DEFINE_REGISTER_STUB(s2, nullptr),105DEFINE_REGISTER_STUB(s3, nullptr),106DEFINE_REGISTER_STUB(s4, nullptr),107DEFINE_REGISTER_STUB(s5, nullptr),108DEFINE_REGISTER_STUB(s6, nullptr),109DEFINE_REGISTER_STUB(s7, nullptr),110DEFINE_REGISTER_STUB(s8, nullptr),111DEFINE_REGISTER_STUB(s9, nullptr),112DEFINE_REGISTER_STUB(s10, nullptr),113DEFINE_REGISTER_STUB(s11, nullptr),114DEFINE_REGISTER_STUB(t3, nullptr),115DEFINE_REGISTER_STUB(t4, nullptr),116DEFINE_REGISTER_STUB(t5, nullptr),117DEFINE_REGISTER_STUB(t6, nullptr),118DEFINE_GENERIC_REGISTER_STUB(pc, nullptr, LLDB_REGNUM_GENERIC_PC)}};119} // namespace dwarf120} // namespace121122const RegisterInfo *ABISysV_riscv::GetRegisterInfoArray(uint32_t &count) {123count = dwarf::g_register_infos.size();124return dwarf::g_register_infos.data();125}126127//------------------------------------------------------------------128// Static Functions129//------------------------------------------------------------------130131ABISP132ABISysV_riscv::CreateInstance(ProcessSP process_sp, const ArchSpec &arch) {133llvm::Triple::ArchType machine = arch.GetTriple().getArch();134135if (llvm::Triple::riscv32 != machine && llvm::Triple::riscv64 != machine)136return ABISP();137138ABISysV_riscv *abi = new ABISysV_riscv(std::move(process_sp),139MakeMCRegisterInfo(arch));140if (abi)141abi->SetIsRV64((llvm::Triple::riscv64 == machine) ? true : false);142return ABISP(abi);143}144145static inline size_t AugmentArgSize(bool is_rv64, size_t size_in_bytes) {146size_t word_size = is_rv64 ? 8 : 4;147return llvm::alignTo(size_in_bytes, word_size);148}149150static size_t151TotalArgsSizeInWords(bool is_rv64,152const llvm::ArrayRef<ABI::CallArgument> &args) {153size_t reg_size = is_rv64 ? 8 : 4;154size_t word_size = reg_size;155size_t total_size = 0;156for (const auto &arg : args)157total_size +=158(ABI::CallArgument::TargetValue == arg.type ? AugmentArgSize(is_rv64,159arg.size)160: reg_size) /161word_size;162163return total_size;164}165166bool ABISysV_riscv::PrepareTrivialCall(Thread &thread, addr_t sp,167addr_t func_addr, addr_t return_addr,168llvm::ArrayRef<addr_t> args) const {169// TODO: Implement170return false;171}172173bool ABISysV_riscv::PrepareTrivialCall(174Thread &thread, addr_t sp, addr_t pc, addr_t ra, llvm::Type &prototype,175llvm::ArrayRef<ABI::CallArgument> args) const {176auto reg_ctx = thread.GetRegisterContext();177if (!reg_ctx)178return false;179180uint32_t pc_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(181eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);182if (pc_reg == LLDB_INVALID_REGNUM)183return false;184185uint32_t ra_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(186eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA);187if (ra_reg == LLDB_INVALID_REGNUM)188return false;189190uint32_t sp_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(191eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);192if (sp_reg == LLDB_INVALID_REGNUM)193return false;194195Status error;196ProcessSP process = thread.GetProcess();197if (!process)198return false;199200size_t reg_size = m_is_rv64 ? 8 : 4;201size_t word_size = reg_size;202// Push host data onto target.203for (const auto &arg : args) {204// Skip over target values.205if (arg.type == ABI::CallArgument::TargetValue)206continue;207208// Create space on the host stack for this data 4-byte aligned.209sp -= AugmentArgSize(m_is_rv64, arg.size);210211if (process->WriteMemory(sp, arg.data_up.get(), arg.size, error) <212arg.size ||213error.Fail())214return false;215216// Update the argument with the target pointer.217*const_cast<addr_t *>(&arg.value) = sp;218}219220// Make sure number of parameters matches prototype.221assert(prototype.getFunctionNumParams() == args.size());222223const size_t num_args = args.size();224const size_t regs_for_args_count = 8U;225const size_t num_args_in_regs =226num_args > regs_for_args_count ? regs_for_args_count : num_args;227228// Number of arguments passed on stack.229size_t args_size = TotalArgsSizeInWords(m_is_rv64, args);230auto on_stack =231args_size <= regs_for_args_count ? 0 : args_size - regs_for_args_count;232auto offset = on_stack * word_size;233234uint8_t reg_value[8];235size_t reg_index = LLDB_REGNUM_GENERIC_ARG1;236237for (size_t i = 0; i < args_size; ++i) {238auto value = reinterpret_cast<const uint8_t *>(&args[i].value);239auto size =240ABI::CallArgument::TargetValue == args[i].type ? args[i].size : reg_size;241242// Pass arguments via registers.243if (i < num_args_in_regs) {244// copy value to register, padding if arg is smaller than register245auto end = size < reg_size ? size : reg_size;246memcpy(reg_value, value, end);247if (reg_size > end)248memset(reg_value + end, 0, reg_size - end);249250RegisterValue reg_val_obj(llvm::ArrayRef(reg_value, reg_size),251eByteOrderLittle);252if (!reg_ctx->WriteRegister(253reg_ctx->GetRegisterInfo(eRegisterKindGeneric, reg_index),254reg_val_obj))255return false;256257// NOTE: It's unsafe to iterate through LLDB_REGNUM_GENERICs258// But the "a" registers are sequential in the RISC-V register space259++reg_index;260}261262if (reg_index < regs_for_args_count || size == 0)263continue;264265// Remaining arguments are passed on the stack.266if (process->WriteMemory(sp - offset, value, size, error) < size ||267!error.Success())268return false;269270offset -= AugmentArgSize(m_is_rv64, size);271}272273// Set stack pointer immediately below arguments.274sp -= on_stack * word_size;275276// Update registers with current function call state.277reg_ctx->WriteRegisterFromUnsigned(pc_reg, pc);278reg_ctx->WriteRegisterFromUnsigned(ra_reg, ra);279reg_ctx->WriteRegisterFromUnsigned(sp_reg, sp);280281return true;282}283284bool ABISysV_riscv::GetArgumentValues(Thread &thread, ValueList &values) const {285// TODO: Implement286return false;287}288289Status ABISysV_riscv::SetReturnValueObject(StackFrameSP &frame_sp,290ValueObjectSP &new_value_sp) {291Status result;292if (!new_value_sp) {293result.SetErrorString("Empty value object for return value.");294return result;295}296297CompilerType compiler_type = new_value_sp->GetCompilerType();298if (!compiler_type) {299result.SetErrorString("Null clang type for return value.");300return result;301}302303auto ®_ctx = *frame_sp->GetThread()->GetRegisterContext();304305bool is_signed = false;306if (!compiler_type.IsIntegerOrEnumerationType(is_signed) &&307!compiler_type.IsPointerType()) {308result.SetErrorString("We don't support returning other types at present");309return result;310}311312DataExtractor data;313size_t num_bytes = new_value_sp->GetData(data, result);314315if (result.Fail()) {316result.SetErrorStringWithFormat(317"Couldn't convert return value to raw data: %s", result.AsCString());318return result;319}320321size_t reg_size = m_is_rv64 ? 8 : 4;322if (num_bytes <= 2 * reg_size) {323offset_t offset = 0;324uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);325326auto reg_info =327reg_ctx.GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);328if (!reg_ctx.WriteRegisterFromUnsigned(reg_info, raw_value)) {329result.SetErrorStringWithFormat("Couldn't write value to register %s",330reg_info->name);331return result;332}333334if (num_bytes <= reg_size)335return result; // Successfully written.336337// for riscv32, get the upper 32 bits from raw_value and write them338// for riscv64, get the next 64 bits from data and write them339if (4 == reg_size)340raw_value >>= 32;341else342raw_value = data.GetMaxU64(&offset, num_bytes - reg_size);343reg_info =344reg_ctx.GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2);345if (!reg_ctx.WriteRegisterFromUnsigned(reg_info, raw_value)) {346result.SetErrorStringWithFormat("Couldn't write value to register %s",347reg_info->name);348}349350return result;351}352353result.SetErrorString(354"We don't support returning large integer values at present.");355return result;356}357358template <typename T>359static void SetInteger(Scalar &scalar, uint64_t raw_value, bool is_signed) {360raw_value &= std::numeric_limits<T>::max();361if (is_signed)362scalar = static_cast<typename std::make_signed<T>::type>(raw_value);363else364scalar = static_cast<T>(raw_value);365}366367static bool SetSizedInteger(Scalar &scalar, uint64_t raw_value,368uint8_t size_in_bytes, bool is_signed) {369switch (size_in_bytes) {370default:371return false;372373case sizeof(uint64_t):374SetInteger<uint64_t>(scalar, raw_value, is_signed);375break;376377case sizeof(uint32_t):378SetInteger<uint32_t>(scalar, raw_value, is_signed);379break;380381case sizeof(uint16_t):382SetInteger<uint16_t>(scalar, raw_value, is_signed);383break;384385case sizeof(uint8_t):386SetInteger<uint8_t>(scalar, raw_value, is_signed);387break;388}389390return true;391}392393static bool SetSizedFloat(Scalar &scalar, uint64_t raw_value,394uint8_t size_in_bytes) {395switch (size_in_bytes) {396default:397return false;398399case sizeof(uint64_t):400scalar = *reinterpret_cast<double *>(&raw_value);401break;402403case sizeof(uint32_t):404scalar = *reinterpret_cast<float *>(&raw_value);405break;406}407408return true;409}410411static ValueObjectSP GetValObjFromIntRegs(Thread &thread,412const RegisterContextSP ®_ctx,413llvm::Triple::ArchType machine,414uint32_t type_flags,415uint32_t byte_size) {416Value value;417ValueObjectSP return_valobj_sp;418auto reg_info_a0 =419reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);420auto reg_info_a1 =421reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2);422uint64_t raw_value;423424switch (byte_size) {425case sizeof(uint32_t):426// Read a0 to get the arg427raw_value = reg_ctx->ReadRegisterAsUnsigned(reg_info_a0, 0) & UINT32_MAX;428break;429case sizeof(uint64_t):430// Read a0 to get the arg on riscv64, a0 and a1 on riscv32431if (llvm::Triple::riscv32 == machine) {432raw_value = reg_ctx->ReadRegisterAsUnsigned(reg_info_a0, 0) & UINT32_MAX;433raw_value |=434(reg_ctx->ReadRegisterAsUnsigned(reg_info_a1, 0) & UINT32_MAX) << 32U;435} else {436raw_value = reg_ctx->ReadRegisterAsUnsigned(reg_info_a0, 0);437}438break;439case 16: {440// Read a0 and a1 to get the arg on riscv64, not supported on riscv32441if (llvm::Triple::riscv32 == machine)442return return_valobj_sp;443444// Create the ValueObjectSP here and return445std::unique_ptr<DataBufferHeap> heap_data_up(446new DataBufferHeap(byte_size, 0));447const ByteOrder byte_order = thread.GetProcess()->GetByteOrder();448RegisterValue reg_value_a0, reg_value_a1;449if (reg_ctx->ReadRegister(reg_info_a0, reg_value_a0) &&450reg_ctx->ReadRegister(reg_info_a1, reg_value_a1)) {451Status error;452if (reg_value_a0.GetAsMemoryData(*reg_info_a0,453heap_data_up->GetBytes() + 0, 8,454byte_order, error) &&455reg_value_a1.GetAsMemoryData(*reg_info_a1,456heap_data_up->GetBytes() + 8, 8,457byte_order, error)) {458value.SetBytes(heap_data_up.release(), byte_size);459return ValueObjectConstResult::Create(460thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));461}462}463break;464}465default:466return return_valobj_sp;467}468469if (type_flags & eTypeIsInteger) {470const bool is_signed = (type_flags & eTypeIsSigned) != 0;471if (!SetSizedInteger(value.GetScalar(), raw_value, byte_size, is_signed))472return return_valobj_sp;473} else if (type_flags & eTypeIsFloat) {474if (!SetSizedFloat(value.GetScalar(), raw_value, byte_size))475return return_valobj_sp;476} else477return return_valobj_sp;478479value.SetValueType(Value::ValueType::Scalar);480return_valobj_sp = ValueObjectConstResult::Create(481thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));482return return_valobj_sp;483}484485static ValueObjectSP486GetValObjFromFPRegs(Thread &thread, const RegisterContextSP ®_ctx,487llvm::Triple::ArchType machine, uint32_t arch_fp_flags,488uint32_t type_flags, uint32_t byte_size) {489auto reg_info_fa0 = reg_ctx->GetRegisterInfoByName("fa0");490bool use_fp_regs = false;491ValueObjectSP return_valobj_sp;492493switch (arch_fp_flags) {494// fp return value in integer registers a0 and possibly a1495case ArchSpec::eRISCV_float_abi_soft:496return_valobj_sp =497GetValObjFromIntRegs(thread, reg_ctx, machine, type_flags, byte_size);498return return_valobj_sp;499// fp return value in fp register fa0 (only float)500case ArchSpec::eRISCV_float_abi_single:501if (byte_size <= 4)502use_fp_regs = true;503break;504// fp return value in fp registers fa0 (float, double)505case ArchSpec::eRISCV_float_abi_double:506[[fallthrough]];507// fp return value in fp registers fa0 (float, double, quad)508// not implemented; act like they're doubles509case ArchSpec::eRISCV_float_abi_quad:510if (byte_size <= 8)511use_fp_regs = true;512break;513}514515if (use_fp_regs) {516uint64_t raw_value;517Value value;518raw_value = reg_ctx->ReadRegisterAsUnsigned(reg_info_fa0, 0);519if (!SetSizedFloat(value.GetScalar(), raw_value, byte_size))520return return_valobj_sp;521value.SetValueType(Value::ValueType::Scalar);522return ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),523value, ConstString(""));524}525// we should never reach this, but if we do, use the integer registers526return GetValObjFromIntRegs(thread, reg_ctx, machine, type_flags, byte_size);527}528529ValueObjectSP530ABISysV_riscv::GetReturnValueObjectSimple(Thread &thread,531CompilerType &compiler_type) const {532ValueObjectSP return_valobj_sp;533534if (!compiler_type)535return return_valobj_sp;536537auto reg_ctx = thread.GetRegisterContext();538if (!reg_ctx)539return return_valobj_sp;540541Value value;542value.SetCompilerType(compiler_type);543544const uint32_t type_flags = compiler_type.GetTypeInfo();545const size_t byte_size = compiler_type.GetByteSize(&thread).value_or(0);546const ArchSpec arch = thread.GetProcess()->GetTarget().GetArchitecture();547const llvm::Triple::ArchType machine = arch.GetMachine();548549// Integer return type.550if (type_flags & eTypeIsInteger) {551return_valobj_sp =552GetValObjFromIntRegs(thread, reg_ctx, machine, type_flags, byte_size);553return return_valobj_sp;554}555// Pointer return type.556else if (type_flags & eTypeIsPointer) {557auto reg_info_a0 = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,558LLDB_REGNUM_GENERIC_ARG1);559value.GetScalar() = reg_ctx->ReadRegisterAsUnsigned(reg_info_a0, 0);560value.SetValueType(Value::ValueType::Scalar);561return ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),562value, ConstString(""));563}564// Floating point return type.565else if (type_flags & eTypeIsFloat) {566uint32_t float_count = 0;567bool is_complex = false;568569if (compiler_type.IsFloatingPointType(float_count, is_complex) &&570float_count == 1 && !is_complex) {571const uint32_t arch_fp_flags =572arch.GetFlags() & ArchSpec::eRISCV_float_abi_mask;573return_valobj_sp = GetValObjFromFPRegs(574thread, reg_ctx, machine, arch_fp_flags, type_flags, byte_size);575return return_valobj_sp;576}577}578// Unsupported return type.579return return_valobj_sp;580}581582ValueObjectSP583ABISysV_riscv::GetReturnValueObjectImpl(lldb_private::Thread &thread,584llvm::Type &type) const {585Value value;586ValueObjectSP return_valobj_sp;587588auto reg_ctx = thread.GetRegisterContext();589if (!reg_ctx)590return return_valobj_sp;591592uint32_t type_flags = 0;593if (type.isIntegerTy())594type_flags = eTypeIsInteger;595else if (type.isVoidTy())596type_flags = eTypeIsPointer;597else if (type.isFloatTy())598type_flags = eTypeIsFloat;599600const uint32_t byte_size = type.getPrimitiveSizeInBits() / CHAR_BIT;601const ArchSpec arch = thread.GetProcess()->GetTarget().GetArchitecture();602const llvm::Triple::ArchType machine = arch.GetMachine();603604// Integer return type.605if (type_flags & eTypeIsInteger) {606return_valobj_sp =607GetValObjFromIntRegs(thread, reg_ctx, machine, type_flags, byte_size);608return return_valobj_sp;609}610// Pointer return type.611else if (type_flags & eTypeIsPointer) {612auto reg_info_a0 = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,613LLDB_REGNUM_GENERIC_ARG1);614value.GetScalar() = reg_ctx->ReadRegisterAsUnsigned(reg_info_a0, 0);615value.SetValueType(Value::ValueType::Scalar);616return ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),617value, ConstString(""));618}619// Floating point return type.620else if (type_flags & eTypeIsFloat) {621const uint32_t arch_fp_flags =622arch.GetFlags() & ArchSpec::eRISCV_float_abi_mask;623return_valobj_sp = GetValObjFromFPRegs(624thread, reg_ctx, machine, arch_fp_flags, type_flags, byte_size);625return return_valobj_sp;626}627// Unsupported return type.628return return_valobj_sp;629}630631ValueObjectSP ABISysV_riscv::GetReturnValueObjectImpl(632Thread &thread, CompilerType &return_compiler_type) const {633ValueObjectSP return_valobj_sp;634635if (!return_compiler_type)636return return_valobj_sp;637638ExecutionContext exe_ctx(thread.shared_from_this());639return GetReturnValueObjectSimple(thread, return_compiler_type);640}641642bool ABISysV_riscv::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {643unwind_plan.Clear();644unwind_plan.SetRegisterKind(eRegisterKindDWARF);645646uint32_t pc_reg_num = riscv_dwarf::dwarf_gpr_pc;647uint32_t sp_reg_num = riscv_dwarf::dwarf_gpr_sp;648uint32_t ra_reg_num = riscv_dwarf::dwarf_gpr_ra;649650UnwindPlan::RowSP row(new UnwindPlan::Row);651652// Define CFA as the stack pointer653row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 0);654655// Previous frame's pc is in ra656657row->SetRegisterLocationToRegister(pc_reg_num, ra_reg_num, true);658unwind_plan.AppendRow(row);659unwind_plan.SetSourceName("riscv function-entry unwind plan");660unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);661662return true;663}664665bool ABISysV_riscv::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {666unwind_plan.Clear();667unwind_plan.SetRegisterKind(eRegisterKindGeneric);668669uint32_t pc_reg_num = LLDB_REGNUM_GENERIC_PC;670uint32_t fp_reg_num = LLDB_REGNUM_GENERIC_FP;671672UnwindPlan::RowSP row(new UnwindPlan::Row);673674// Define the CFA as the current frame pointer value.675row->GetCFAValue().SetIsRegisterPlusOffset(fp_reg_num, 0);676row->SetOffset(0);677678int reg_size = 4;679if (m_is_rv64)680reg_size = 8;681682// Assume the ra reg (return pc) and caller's frame pointer683// have been spilled to stack already.684row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, reg_size * -2, true);685row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, reg_size * -1, true);686687unwind_plan.AppendRow(row);688unwind_plan.SetSourceName("riscv default unwind plan");689unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);690unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);691return true;692}693694bool ABISysV_riscv::RegisterIsVolatile(const RegisterInfo *reg_info) {695return !RegisterIsCalleeSaved(reg_info);696}697698bool ABISysV_riscv::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {699if (!reg_info)700return false;701702const char *name = reg_info->name;703ArchSpec arch = GetProcessSP()->GetTarget().GetArchitecture();704uint32_t arch_flags = arch.GetFlags();705// floating point registers are only callee saved when using706// F, D or Q hardware floating point ABIs707bool is_hw_fp = (arch_flags & ArchSpec::eRISCV_float_abi_mask) != 0;708709bool is_callee_saved =710llvm::StringSwitch<bool>(name)711// integer ABI names712.Cases("ra", "sp", "fp", true)713.Cases("s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9",714true)715.Cases("s10", "s11", true)716// integer hardware names717.Cases("x1", "x2", "x8", "x9", "x18", "x19", "x20", "x21", "x22",718true)719.Cases("x23", "x24", "x25", "x26", "x27", true)720// floating point ABI names721.Cases("fs0", "fs1", "fs2", "fs3", "fs4", "fs5", "fs6", "fs7",722is_hw_fp)723.Cases("fs8", "fs9", "fs10", "fs11", is_hw_fp)724// floating point hardware names725.Cases("f8", "f9", "f18", "f19", "f20", "f21", "f22", "f23", is_hw_fp)726.Cases("f24", "f25", "f26", "f27", is_hw_fp)727.Default(false);728729return is_callee_saved;730}731732void ABISysV_riscv::Initialize() {733PluginManager::RegisterPlugin(734GetPluginNameStatic(), "System V ABI for RISCV targets", CreateInstance);735}736737void ABISysV_riscv::Terminate() {738PluginManager::UnregisterPlugin(CreateInstance);739}740741static uint32_t GetGenericNum(llvm::StringRef name) {742return llvm::StringSwitch<uint32_t>(name)743.Case("pc", LLDB_REGNUM_GENERIC_PC)744.Cases("ra", "x1", LLDB_REGNUM_GENERIC_RA)745.Cases("sp", "x2", LLDB_REGNUM_GENERIC_SP)746.Cases("fp", "s0", LLDB_REGNUM_GENERIC_FP)747.Case("a0", LLDB_REGNUM_GENERIC_ARG1)748.Case("a1", LLDB_REGNUM_GENERIC_ARG2)749.Case("a2", LLDB_REGNUM_GENERIC_ARG3)750.Case("a3", LLDB_REGNUM_GENERIC_ARG4)751.Case("a4", LLDB_REGNUM_GENERIC_ARG5)752.Case("a5", LLDB_REGNUM_GENERIC_ARG6)753.Case("a6", LLDB_REGNUM_GENERIC_ARG7)754.Case("a7", LLDB_REGNUM_GENERIC_ARG8)755.Default(LLDB_INVALID_REGNUM);756}757758void ABISysV_riscv::AugmentRegisterInfo(759std::vector<lldb_private::DynamicRegisterInfo::Register> ®s) {760lldb_private::RegInfoBasedABI::AugmentRegisterInfo(regs);761762for (auto it : llvm::enumerate(regs)) {763// Set alt name for certain registers for convenience764if (it.value().name == "zero")765it.value().alt_name.SetCString("x0");766else if (it.value().name == "ra")767it.value().alt_name.SetCString("x1");768else if (it.value().name == "sp")769it.value().alt_name.SetCString("x2");770else if (it.value().name == "gp")771it.value().alt_name.SetCString("x3");772else if (it.value().name == "fp")773it.value().alt_name.SetCString("s0");774else if (it.value().name == "s0")775it.value().alt_name.SetCString("x8");776777// Set generic regnum so lldb knows what the PC, etc is778it.value().regnum_generic = GetGenericNum(it.value().name.GetStringRef());779}780}781782783