[OpenACC] Implement 'device_type' for 'data' construct
[llvm-project.git] / lldb / source / Plugins / ABI / LoongArch / ABISysV_loongarch.cpp
blobdc7e9bba000676d91d981de68fb2e2dab801ea71
1 //===-- ABISysV_loongarch.cpp----------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "ABISysV_loongarch.h"
11 #include <array>
12 #include <limits>
13 #include <sstream>
15 #include "llvm/IR/DerivedTypes.h"
16 #include "llvm/Support/MathExtras.h"
18 #include "Utility/LoongArch_DWARF_Registers.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/Value.h"
21 #include "lldb/Target/RegisterContext.h"
22 #include "lldb/Target/StackFrame.h"
23 #include "lldb/Target/Thread.h"
24 #include "lldb/Utility/LLDBLog.h"
25 #include "lldb/Utility/RegisterValue.h"
26 #include "lldb/ValueObject/ValueObjectConstResult.h"
28 #define DEFINE_REG_NAME(reg_num) ConstString(#reg_num).GetCString()
29 #define DEFINE_REG_NAME_STR(reg_name) ConstString(reg_name).GetCString()
31 // The ABI is not a source of such information as size, offset, encoding, etc.
32 // of a register. Just provides correct dwarf and eh_frame numbers.
34 #define DEFINE_GENERIC_REGISTER_STUB(dwarf_num, generic_num) \
35 { \
36 DEFINE_REG_NAME(dwarf_num), \
37 DEFINE_REG_NAME_STR(nullptr), \
38 0, \
39 0, \
40 eEncodingInvalid, \
41 eFormatDefault, \
42 {dwarf_num, dwarf_num, generic_num, LLDB_INVALID_REGNUM, dwarf_num}, \
43 nullptr, \
44 nullptr, \
45 nullptr, \
48 #define DEFINE_REGISTER_STUB(dwarf_num) \
49 DEFINE_GENERIC_REGISTER_STUB(dwarf_num, LLDB_INVALID_REGNUM)
51 using namespace lldb;
52 using namespace lldb_private;
54 LLDB_PLUGIN_DEFINE_ADV(ABISysV_loongarch, ABILoongArch)
56 namespace {
57 namespace dwarf {
58 enum regnums {
59 r0,
60 r1,
61 ra = r1,
62 r2,
63 r3,
64 sp = r3,
65 r4,
66 r5,
67 r6,
68 r7,
69 r8,
70 r9,
71 r10,
72 r11,
73 r12,
74 r13,
75 r14,
76 r15,
77 r16,
78 r17,
79 r18,
80 r19,
81 r20,
82 r21,
83 r22,
84 fp = r22,
85 r23,
86 r24,
87 r25,
88 r26,
89 r27,
90 r28,
91 r29,
92 r30,
93 r31,
97 static const std::array<RegisterInfo, 33> g_register_infos = {
98 {DEFINE_REGISTER_STUB(r0),
99 DEFINE_GENERIC_REGISTER_STUB(r1, LLDB_REGNUM_GENERIC_RA),
100 DEFINE_REGISTER_STUB(r2),
101 DEFINE_GENERIC_REGISTER_STUB(r3, LLDB_REGNUM_GENERIC_SP),
102 DEFINE_GENERIC_REGISTER_STUB(r4, LLDB_REGNUM_GENERIC_ARG1),
103 DEFINE_GENERIC_REGISTER_STUB(r5, LLDB_REGNUM_GENERIC_ARG2),
104 DEFINE_GENERIC_REGISTER_STUB(r6, LLDB_REGNUM_GENERIC_ARG3),
105 DEFINE_GENERIC_REGISTER_STUB(r7, LLDB_REGNUM_GENERIC_ARG4),
106 DEFINE_GENERIC_REGISTER_STUB(r8, LLDB_REGNUM_GENERIC_ARG5),
107 DEFINE_GENERIC_REGISTER_STUB(r9, LLDB_REGNUM_GENERIC_ARG6),
108 DEFINE_GENERIC_REGISTER_STUB(r10, LLDB_REGNUM_GENERIC_ARG7),
109 DEFINE_GENERIC_REGISTER_STUB(r11, LLDB_REGNUM_GENERIC_ARG8),
110 DEFINE_REGISTER_STUB(r12),
111 DEFINE_REGISTER_STUB(r13),
112 DEFINE_REGISTER_STUB(r14),
113 DEFINE_REGISTER_STUB(r15),
114 DEFINE_REGISTER_STUB(r16),
115 DEFINE_REGISTER_STUB(r17),
116 DEFINE_REGISTER_STUB(r18),
117 DEFINE_REGISTER_STUB(r19),
118 DEFINE_REGISTER_STUB(r20),
119 DEFINE_REGISTER_STUB(r21),
120 DEFINE_GENERIC_REGISTER_STUB(r22, LLDB_REGNUM_GENERIC_FP),
121 DEFINE_REGISTER_STUB(r23),
122 DEFINE_REGISTER_STUB(r24),
123 DEFINE_REGISTER_STUB(r25),
124 DEFINE_REGISTER_STUB(r26),
125 DEFINE_REGISTER_STUB(r27),
126 DEFINE_REGISTER_STUB(r28),
127 DEFINE_REGISTER_STUB(r29),
128 DEFINE_REGISTER_STUB(r30),
129 DEFINE_REGISTER_STUB(r31),
130 DEFINE_GENERIC_REGISTER_STUB(pc, LLDB_REGNUM_GENERIC_PC)}};
131 } // namespace dwarf
132 } // namespace
134 // Number of argument registers (the base integer calling convention
135 // provides 8 argument registers, a0-a7)
136 static constexpr size_t g_regs_for_args_count = 8U;
138 const RegisterInfo *ABISysV_loongarch::GetRegisterInfoArray(uint32_t &count) {
139 count = dwarf::g_register_infos.size();
140 return dwarf::g_register_infos.data();
143 //------------------------------------------------------------------
144 // Static Functions
145 //------------------------------------------------------------------
147 ABISP
148 ABISysV_loongarch::CreateInstance(ProcessSP process_sp, const ArchSpec &arch) {
149 llvm::Triple::ArchType machine = arch.GetTriple().getArch();
151 if (llvm::Triple::loongarch32 != machine &&
152 llvm::Triple::loongarch64 != machine)
153 return ABISP();
155 ABISysV_loongarch *abi =
156 new ABISysV_loongarch(std::move(process_sp), MakeMCRegisterInfo(arch));
157 if (abi)
158 abi->SetIsLA64(llvm::Triple::loongarch64 == machine);
159 return ABISP(abi);
162 static bool UpdateRegister(RegisterContext *reg_ctx,
163 const lldb::RegisterKind reg_kind,
164 const uint32_t reg_num, const addr_t value) {
165 Log *log = GetLog(LLDBLog::Expressions);
167 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfo(reg_kind, reg_num);
169 LLDB_LOG(log, "Writing {0}: 0x{1:x}", reg_info->name,
170 static_cast<uint64_t>(value));
171 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, value)) {
172 LLDB_LOG(log, "Writing {0}: failed", reg_info->name);
173 return false;
175 return true;
178 static void LogInitInfo(Log &log, const Thread &thread, addr_t sp,
179 addr_t func_addr, addr_t return_addr,
180 const llvm::ArrayRef<addr_t> args) {
181 std::stringstream ss;
182 ss << "ABISysV_loongarch::PrepareTrivialCall"
183 << " (tid = 0x" << std::hex << thread.GetID() << ", sp = 0x" << sp
184 << ", func_addr = 0x" << func_addr << ", return_addr = 0x" << return_addr;
186 for (auto [idx, arg] : enumerate(args))
187 ss << ", arg" << std::dec << idx << " = 0x" << std::hex << arg;
188 ss << ")";
189 log.PutString(ss.str());
192 bool ABISysV_loongarch::PrepareTrivialCall(Thread &thread, addr_t sp,
193 addr_t func_addr, addr_t return_addr,
194 llvm::ArrayRef<addr_t> args) const {
195 Log *log = GetLog(LLDBLog::Expressions);
196 if (log)
197 LogInitInfo(*log, thread, sp, func_addr, return_addr, args);
199 const auto reg_ctx_sp = thread.GetRegisterContext();
200 if (!reg_ctx_sp) {
201 LLDB_LOG(log, "Failed to get RegisterContext");
202 return false;
205 if (args.size() > g_regs_for_args_count) {
206 LLDB_LOG(log, "Function has {0} arguments, but only {1} are allowed!",
207 args.size(), g_regs_for_args_count);
208 return false;
211 // Write arguments to registers
212 for (auto [idx, arg] : enumerate(args)) {
213 const RegisterInfo *reg_info = reg_ctx_sp->GetRegisterInfo(
214 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + idx);
215 LLDB_LOG(log, "About to write arg{0} ({1:x}) into {2}", idx, arg,
216 reg_info->name);
218 if (!reg_ctx_sp->WriteRegisterFromUnsigned(reg_info, arg)) {
219 LLDB_LOG(log, "Failed to write arg{0} ({1:x}) into {2}", idx, arg,
220 reg_info->name);
221 return false;
225 if (!UpdateRegister(reg_ctx_sp.get(), eRegisterKindGeneric,
226 LLDB_REGNUM_GENERIC_PC, func_addr))
227 return false;
228 if (!UpdateRegister(reg_ctx_sp.get(), eRegisterKindGeneric,
229 LLDB_REGNUM_GENERIC_SP, sp))
230 return false;
231 if (!UpdateRegister(reg_ctx_sp.get(), eRegisterKindGeneric,
232 LLDB_REGNUM_GENERIC_RA, return_addr))
233 return false;
235 LLDB_LOG(log, "ABISysV_loongarch::{0}() success", __FUNCTION__);
236 return true;
239 bool ABISysV_loongarch::GetArgumentValues(Thread &thread,
240 ValueList &values) const {
241 // TODO: Implement
242 return false;
245 Status ABISysV_loongarch::SetReturnValueObject(StackFrameSP &frame_sp,
246 ValueObjectSP &new_value_sp) {
247 Status result;
248 if (!new_value_sp) {
249 result = Status::FromErrorString("Empty value object for return value.");
250 return result;
253 CompilerType compiler_type = new_value_sp->GetCompilerType();
254 if (!compiler_type) {
255 result = Status::FromErrorString("Null clang type for return value.");
256 return result;
259 auto &reg_ctx = *frame_sp->GetThread()->GetRegisterContext();
261 bool is_signed = false;
262 if (!compiler_type.IsIntegerOrEnumerationType(is_signed) &&
263 !compiler_type.IsPointerType()) {
264 result = Status::FromErrorString(
265 "We don't support returning other types at present");
266 return result;
269 DataExtractor data;
270 size_t num_bytes = new_value_sp->GetData(data, result);
272 if (result.Fail()) {
273 result = Status::FromErrorStringWithFormat(
274 "Couldn't convert return value to raw data: %s", result.AsCString());
275 return result;
278 size_t reg_size = m_is_la64 ? 8 : 4;
279 // Currently, we only support sizeof(data) <= 2 * reg_size.
280 // 1. If the (`size` <= reg_size), the `data` will be returned through `ARG1`.
281 // 2. If the (`size` > reg_size && `size` <= 2 * reg_size), the `data` will be
282 // returned through a pair of registers (ARG1 and ARG2), and the lower-ordered
283 // bits in the `ARG1`.
284 if (num_bytes > 2 * reg_size) {
285 result = Status::FromErrorString(
286 "We don't support returning large integer values at present.");
287 return result;
290 offset_t offset = 0;
291 uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
292 // According to psABI, i32 (no matter signed or unsigned) should be
293 // sign-extended in register.
294 if (4 == num_bytes && m_is_la64)
295 raw_value = llvm::SignExtend64<32>(raw_value);
296 auto reg_info =
297 reg_ctx.GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);
298 if (!reg_ctx.WriteRegisterFromUnsigned(reg_info, raw_value)) {
299 result = Status::FromErrorStringWithFormat(
300 "Couldn't write value to register %s", reg_info->name);
301 return result;
304 if (num_bytes <= reg_size)
305 return result; // Successfully written.
307 // For loongarch32, get the upper 32 bits from raw_value and write them.
308 // For loongarch64, get the next 64 bits from data and write them.
309 if (4 == reg_size)
310 raw_value >>= 32;
311 else
312 raw_value = data.GetMaxU64(&offset, num_bytes - reg_size);
314 reg_info =
315 reg_ctx.GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2);
316 if (!reg_ctx.WriteRegisterFromUnsigned(reg_info, raw_value))
317 result = Status::FromErrorStringWithFormat(
318 "Couldn't write value to register %s", reg_info->name);
320 return result;
323 template <typename T>
324 static void SetInteger(Scalar &scalar, uint64_t raw_value, bool is_signed) {
325 static_assert(std::is_unsigned<T>::value, "T must be an unsigned type.");
326 raw_value &= std::numeric_limits<T>::max();
327 if (is_signed)
328 scalar = static_cast<typename std::make_signed<T>::type>(raw_value);
329 else
330 scalar = static_cast<T>(raw_value);
333 static bool SetSizedInteger(Scalar &scalar, uint64_t raw_value,
334 uint8_t size_in_bytes, bool is_signed) {
335 switch (size_in_bytes) {
336 default:
337 return false;
339 case sizeof(uint64_t):
340 SetInteger<uint64_t>(scalar, raw_value, is_signed);
341 break;
343 case sizeof(uint32_t):
344 SetInteger<uint32_t>(scalar, raw_value, is_signed);
345 break;
347 case sizeof(uint16_t):
348 SetInteger<uint16_t>(scalar, raw_value, is_signed);
349 break;
351 case sizeof(uint8_t):
352 SetInteger<uint8_t>(scalar, raw_value, is_signed);
353 break;
356 return true;
359 static bool SetSizedFloat(Scalar &scalar, uint64_t raw_value,
360 uint8_t size_in_bytes) {
361 switch (size_in_bytes) {
362 default:
363 return false;
365 case sizeof(uint64_t):
366 scalar = *reinterpret_cast<double *>(&raw_value);
367 break;
369 case sizeof(uint32_t):
370 scalar = *reinterpret_cast<float *>(&raw_value);
371 break;
374 return true;
377 static ValueObjectSP GetValObjFromIntRegs(Thread &thread,
378 const RegisterContextSP &reg_ctx,
379 llvm::Triple::ArchType machine,
380 uint32_t type_flags,
381 uint32_t byte_size) {
382 Value value;
383 ValueObjectSP return_valobj_sp;
384 auto *reg_info_a0 =
385 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);
386 auto *reg_info_a1 =
387 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2);
388 uint64_t raw_value = 0;
390 switch (byte_size) {
391 case sizeof(uint32_t):
392 // Read a0 to get the arg
393 raw_value = reg_ctx->ReadRegisterAsUnsigned(reg_info_a0, 0) & UINT32_MAX;
394 break;
395 case sizeof(uint64_t):
396 // Read a0 to get the arg on loongarch64, a0 and a1 on loongarch32
397 if (llvm::Triple::loongarch32 == machine) {
398 raw_value = reg_ctx->ReadRegisterAsUnsigned(reg_info_a0, 0) & UINT32_MAX;
399 raw_value |=
400 (reg_ctx->ReadRegisterAsUnsigned(reg_info_a1, 0) & UINT32_MAX) << 32U;
401 } else {
402 raw_value = reg_ctx->ReadRegisterAsUnsigned(reg_info_a0, 0);
404 break;
405 case 16: {
406 // Read a0 and a1 to get the arg on loongarch64, not supported on
407 // loongarch32
408 if (llvm::Triple::loongarch32 == machine)
409 return return_valobj_sp;
411 // Create the ValueObjectSP here and return
412 std::unique_ptr<DataBufferHeap> heap_data_up(
413 new DataBufferHeap(byte_size, 0));
414 const ByteOrder byte_order = thread.GetProcess()->GetByteOrder();
415 RegisterValue reg_value_a0, reg_value_a1;
416 if (reg_ctx->ReadRegister(reg_info_a0, reg_value_a0) &&
417 reg_ctx->ReadRegister(reg_info_a1, reg_value_a1)) {
418 Status error;
419 if (reg_value_a0.GetAsMemoryData(*reg_info_a0,
420 heap_data_up->GetBytes() + 0, 8,
421 byte_order, error) &&
422 reg_value_a1.GetAsMemoryData(*reg_info_a1,
423 heap_data_up->GetBytes() + 8, 8,
424 byte_order, error)) {
425 value.SetBytes(heap_data_up.release(), byte_size);
426 return ValueObjectConstResult::Create(
427 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
430 break;
432 default:
433 return return_valobj_sp;
436 if (type_flags & eTypeIsInteger) {
437 if (!SetSizedInteger(value.GetScalar(), raw_value, byte_size,
438 type_flags & eTypeIsSigned))
439 return return_valobj_sp;
440 } else if (type_flags & eTypeIsFloat) {
441 if (!SetSizedFloat(value.GetScalar(), raw_value, byte_size))
442 return return_valobj_sp;
443 } else
444 return return_valobj_sp;
446 value.SetValueType(Value::ValueType::Scalar);
447 return_valobj_sp = ValueObjectConstResult::Create(
448 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
449 return return_valobj_sp;
452 static ValueObjectSP GetValObjFromFPRegs(Thread &thread,
453 const RegisterContextSP &reg_ctx,
454 llvm::Triple::ArchType machine,
455 uint32_t type_flags,
456 uint32_t byte_size) {
457 auto *reg_info_fa0 = reg_ctx->GetRegisterInfoByName("f0");
458 bool use_fp_regs = false;
459 ValueObjectSP return_valobj_sp;
461 if (byte_size <= 8)
462 use_fp_regs = true;
464 if (use_fp_regs) {
465 uint64_t raw_value;
466 Value value;
467 raw_value = reg_ctx->ReadRegisterAsUnsigned(reg_info_fa0, 0);
468 if (!SetSizedFloat(value.GetScalar(), raw_value, byte_size))
469 return return_valobj_sp;
470 value.SetValueType(Value::ValueType::Scalar);
471 return ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),
472 value, ConstString(""));
474 // we should never reach this, but if we do, use the integer registers
475 return GetValObjFromIntRegs(thread, reg_ctx, machine, type_flags, byte_size);
478 ValueObjectSP ABISysV_loongarch::GetReturnValueObjectSimple(
479 Thread &thread, CompilerType &compiler_type) const {
480 ValueObjectSP return_valobj_sp;
482 if (!compiler_type)
483 return return_valobj_sp;
485 auto reg_ctx = thread.GetRegisterContext();
486 if (!reg_ctx)
487 return return_valobj_sp;
489 Value value;
490 value.SetCompilerType(compiler_type);
492 const uint32_t type_flags = compiler_type.GetTypeInfo();
493 const size_t byte_size = compiler_type.GetByteSize(&thread).value_or(0);
494 const ArchSpec arch = thread.GetProcess()->GetTarget().GetArchitecture();
495 const llvm::Triple::ArchType machine = arch.GetMachine();
497 if (type_flags & eTypeIsInteger) {
498 return_valobj_sp =
499 GetValObjFromIntRegs(thread, reg_ctx, machine, type_flags, byte_size);
500 return return_valobj_sp;
502 if (type_flags & eTypeIsPointer) {
503 const auto *reg_info_a0 = reg_ctx->GetRegisterInfo(
504 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);
505 value.GetScalar() = reg_ctx->ReadRegisterAsUnsigned(reg_info_a0, 0);
506 value.SetValueType(Value::ValueType::Scalar);
507 return ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),
508 value, ConstString(""));
510 if (type_flags & eTypeIsFloat) {
511 uint32_t float_count = 0;
512 bool is_complex = false;
514 if (compiler_type.IsFloatingPointType(float_count, is_complex) &&
515 float_count == 1 && !is_complex) {
516 return_valobj_sp =
517 GetValObjFromFPRegs(thread, reg_ctx, machine, type_flags, byte_size);
518 return return_valobj_sp;
521 return return_valobj_sp;
524 ValueObjectSP ABISysV_loongarch::GetReturnValueObjectImpl(
525 Thread &thread, CompilerType &return_compiler_type) const {
526 ValueObjectSP return_valobj_sp;
528 if (!return_compiler_type)
529 return return_valobj_sp;
531 ExecutionContext exe_ctx(thread.shared_from_this());
532 return GetReturnValueObjectSimple(thread, return_compiler_type);
535 bool ABISysV_loongarch::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
536 unwind_plan.Clear();
537 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
539 uint32_t pc_reg_num = loongarch_dwarf::dwarf_gpr_pc;
540 uint32_t sp_reg_num = loongarch_dwarf::dwarf_gpr_sp;
541 uint32_t ra_reg_num = loongarch_dwarf::dwarf_gpr_ra;
543 UnwindPlan::RowSP row(new UnwindPlan::Row);
545 // Define CFA as the stack pointer
546 row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 0);
548 // Previous frame's pc is in ra
550 row->SetRegisterLocationToRegister(pc_reg_num, ra_reg_num, true);
551 unwind_plan.AppendRow(row);
552 unwind_plan.SetSourceName("loongarch function-entry unwind plan");
553 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
555 return true;
558 bool ABISysV_loongarch::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
559 unwind_plan.Clear();
560 unwind_plan.SetRegisterKind(eRegisterKindGeneric);
562 uint32_t pc_reg_num = LLDB_REGNUM_GENERIC_PC;
563 uint32_t fp_reg_num = LLDB_REGNUM_GENERIC_FP;
565 UnwindPlan::RowSP row(new UnwindPlan::Row);
567 // Define the CFA as the current frame pointer value.
568 row->GetCFAValue().SetIsRegisterPlusOffset(fp_reg_num, 0);
569 row->SetOffset(0);
571 int reg_size = 4;
572 if (m_is_la64)
573 reg_size = 8;
575 // Assume the ra reg (return pc) and caller's frame pointer
576 // have been spilled to stack already.
577 row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, reg_size * -2, true);
578 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, reg_size * -1, true);
580 unwind_plan.AppendRow(row);
581 unwind_plan.SetSourceName("loongarch default unwind plan");
582 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
583 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
584 return true;
587 bool ABISysV_loongarch::RegisterIsVolatile(const RegisterInfo *reg_info) {
588 return !RegisterIsCalleeSaved(reg_info);
591 bool ABISysV_loongarch::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {
592 if (!reg_info)
593 return false;
595 const char *name = reg_info->name;
596 ArchSpec arch = GetProcessSP()->GetTarget().GetArchitecture();
597 uint32_t arch_flags = arch.GetFlags();
598 // Floating point registers are only callee saved when using
599 // F or D hardware floating point ABIs.
600 bool is_hw_fp = (arch_flags & ArchSpec::eLoongArch_abi_mask) != 0;
602 return llvm::StringSwitch<bool>(name)
603 // integer ABI names
604 .Cases("ra", "sp", "fp", true)
605 .Cases("s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", true)
606 // integer hardware names
607 .Cases("r1", "r3", "r22", true)
608 .Cases("r23", "r24", "r25", "r26", "r27", "r28", "r29", "r30", "31", true)
609 // floating point ABI names
610 .Cases("fs0", "fs1", "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", is_hw_fp)
611 // floating point hardware names
612 .Cases("f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", is_hw_fp)
613 .Default(false);
616 void ABISysV_loongarch::Initialize() {
617 PluginManager::RegisterPlugin(GetPluginNameStatic(),
618 "System V ABI for LoongArch targets",
619 CreateInstance);
622 void ABISysV_loongarch::Terminate() {
623 PluginManager::UnregisterPlugin(CreateInstance);
626 static uint32_t GetGenericNum(llvm::StringRef name) {
627 return llvm::StringSwitch<uint32_t>(name)
628 .Case("pc", LLDB_REGNUM_GENERIC_PC)
629 .Cases("ra", "r1", LLDB_REGNUM_GENERIC_RA)
630 .Cases("sp", "r3", LLDB_REGNUM_GENERIC_SP)
631 .Cases("fp", "r22", LLDB_REGNUM_GENERIC_FP)
632 .Cases("a0", "r4", LLDB_REGNUM_GENERIC_ARG1)
633 .Cases("a1", "r5", LLDB_REGNUM_GENERIC_ARG2)
634 .Cases("a2", "r6", LLDB_REGNUM_GENERIC_ARG3)
635 .Cases("a3", "r7", LLDB_REGNUM_GENERIC_ARG4)
636 .Cases("a4", "r8", LLDB_REGNUM_GENERIC_ARG5)
637 .Cases("a5", "r9", LLDB_REGNUM_GENERIC_ARG6)
638 .Cases("a6", "r10", LLDB_REGNUM_GENERIC_ARG7)
639 .Cases("a7", "r11", LLDB_REGNUM_GENERIC_ARG8)
640 .Default(LLDB_INVALID_REGNUM);
643 void ABISysV_loongarch::AugmentRegisterInfo(
644 std::vector<lldb_private::DynamicRegisterInfo::Register> &regs) {
645 lldb_private::RegInfoBasedABI::AugmentRegisterInfo(regs);
647 for (auto it : llvm::enumerate(regs)) {
648 // Set alt name for certain registers for convenience
649 if (it.value().name == "r0")
650 it.value().alt_name.SetCString("zero");
651 else if (it.value().name == "r1")
652 it.value().alt_name.SetCString("ra");
653 else if (it.value().name == "r3")
654 it.value().alt_name.SetCString("sp");
655 else if (it.value().name == "r22")
656 it.value().alt_name.SetCString("fp");
657 else if (it.value().name == "r4")
658 it.value().alt_name.SetCString("a0");
659 else if (it.value().name == "r5")
660 it.value().alt_name.SetCString("a1");
661 else if (it.value().name == "r6")
662 it.value().alt_name.SetCString("a2");
663 else if (it.value().name == "r7")
664 it.value().alt_name.SetCString("a3");
665 else if (it.value().name == "r8")
666 it.value().alt_name.SetCString("a4");
667 else if (it.value().name == "r9")
668 it.value().alt_name.SetCString("a5");
669 else if (it.value().name == "r10")
670 it.value().alt_name.SetCString("a6");
671 else if (it.value().name == "r11")
672 it.value().alt_name.SetCString("a7");
674 // Set generic regnum so lldb knows what the PC, etc is
675 it.value().regnum_generic = GetGenericNum(it.value().name.GetStringRef());