1 //===-- ABISysV_arc.cpp ---------------------------------------*- 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 #include "ABISysV_arc.h"
15 #include <type_traits>
17 // Other libraries and framework includes
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/Support/MathExtras.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/PluginManager.h"
24 #include "lldb/Core/Value.h"
25 #include "lldb/Core/ValueObjectConstResult.h"
26 #include "lldb/Core/ValueObjectMemory.h"
27 #include "lldb/Core/ValueObjectRegister.h"
28 #include "lldb/Symbol/UnwindPlan.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/RegisterContext.h"
31 #include "lldb/Target/StackFrame.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Target/Thread.h"
34 #include "lldb/Utility/ConstString.h"
35 #include "lldb/Utility/RegisterValue.h"
36 #include "lldb/Utility/Status.h"
38 #define DEFINE_REG_NAME(reg_num) ConstString(#reg_num).GetCString()
39 #define DEFINE_REG_NAME_STR(reg_name) ConstString(reg_name).GetCString()
41 // The ABI is not a source of such information as size, offset, encoding, etc.
42 // of a register. Just provides correct dwarf and eh_frame numbers.
44 #define DEFINE_GENERIC_REGISTER_STUB(dwarf_num, str_name, generic_num) \
46 DEFINE_REG_NAME(dwarf_num), DEFINE_REG_NAME_STR(str_name), \
47 0, 0, eEncodingInvalid, eFormatDefault, \
48 { dwarf_num, dwarf_num, generic_num, LLDB_INVALID_REGNUM, dwarf_num }, \
49 nullptr, nullptr, nullptr, 0 \
52 #define DEFINE_REGISTER_STUB(dwarf_num, str_name) \
53 DEFINE_GENERIC_REGISTER_STUB(dwarf_num, str_name, LLDB_INVALID_REGNUM)
56 using namespace lldb_private;
61 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16,
62 r17, r18, r19, r20, r21, r22, r23, r24, r25, r26,
63 r27, fp = r27, r28, sp = r28, r29, r30, r31, blink = r31,
64 r32, r33, r34, r35, r36, r37, r38, r39, r40, r41, r42, r43, r44, r45, r46,
65 r47, r48, r49, r50, r51, r52, r53, r54, r55, r56, r57, r58, r59, r60,
66 /*reserved,*/ /*limm indicator,*/ r63 = 63, pc = 70, status32 = 74
69 static const std::array<RegisterInfo, 64> g_register_infos = { {
70 DEFINE_GENERIC_REGISTER_STUB(r0, nullptr, LLDB_REGNUM_GENERIC_ARG1),
71 DEFINE_GENERIC_REGISTER_STUB(r1, nullptr, LLDB_REGNUM_GENERIC_ARG2),
72 DEFINE_GENERIC_REGISTER_STUB(r2, nullptr, LLDB_REGNUM_GENERIC_ARG3),
73 DEFINE_GENERIC_REGISTER_STUB(r3, nullptr, LLDB_REGNUM_GENERIC_ARG4),
74 DEFINE_GENERIC_REGISTER_STUB(r4, nullptr, LLDB_REGNUM_GENERIC_ARG5),
75 DEFINE_GENERIC_REGISTER_STUB(r5, nullptr, LLDB_REGNUM_GENERIC_ARG6),
76 DEFINE_GENERIC_REGISTER_STUB(r6, nullptr, LLDB_REGNUM_GENERIC_ARG7),
77 DEFINE_GENERIC_REGISTER_STUB(r7, nullptr, LLDB_REGNUM_GENERIC_ARG8),
78 DEFINE_REGISTER_STUB(r8, nullptr),
79 DEFINE_REGISTER_STUB(r9, nullptr),
80 DEFINE_REGISTER_STUB(r10, nullptr),
81 DEFINE_REGISTER_STUB(r11, nullptr),
82 DEFINE_REGISTER_STUB(r12, nullptr),
83 DEFINE_REGISTER_STUB(r13, nullptr),
84 DEFINE_REGISTER_STUB(r14, nullptr),
85 DEFINE_REGISTER_STUB(r15, nullptr),
86 DEFINE_REGISTER_STUB(r16, nullptr),
87 DEFINE_REGISTER_STUB(r17, nullptr),
88 DEFINE_REGISTER_STUB(r18, nullptr),
89 DEFINE_REGISTER_STUB(r19, nullptr),
90 DEFINE_REGISTER_STUB(r20, nullptr),
91 DEFINE_REGISTER_STUB(r21, nullptr),
92 DEFINE_REGISTER_STUB(r22, nullptr),
93 DEFINE_REGISTER_STUB(r23, nullptr),
94 DEFINE_REGISTER_STUB(r24, nullptr),
95 DEFINE_REGISTER_STUB(r25, nullptr),
96 DEFINE_REGISTER_STUB(r26, "gp"),
97 DEFINE_GENERIC_REGISTER_STUB(r27, "fp", LLDB_REGNUM_GENERIC_FP),
98 DEFINE_GENERIC_REGISTER_STUB(r28, "sp", LLDB_REGNUM_GENERIC_SP),
99 DEFINE_REGISTER_STUB(r29, "ilink"),
100 DEFINE_REGISTER_STUB(r30, nullptr),
101 DEFINE_GENERIC_REGISTER_STUB(r31, "blink", LLDB_REGNUM_GENERIC_RA),
102 DEFINE_REGISTER_STUB(r32, nullptr),
103 DEFINE_REGISTER_STUB(r33, nullptr),
104 DEFINE_REGISTER_STUB(r34, nullptr),
105 DEFINE_REGISTER_STUB(r35, nullptr),
106 DEFINE_REGISTER_STUB(r36, nullptr),
107 DEFINE_REGISTER_STUB(r37, nullptr),
108 DEFINE_REGISTER_STUB(r38, nullptr),
109 DEFINE_REGISTER_STUB(r39, nullptr),
110 DEFINE_REGISTER_STUB(r40, nullptr),
111 DEFINE_REGISTER_STUB(r41, nullptr),
112 DEFINE_REGISTER_STUB(r42, nullptr),
113 DEFINE_REGISTER_STUB(r43, nullptr),
114 DEFINE_REGISTER_STUB(r44, nullptr),
115 DEFINE_REGISTER_STUB(r45, nullptr),
116 DEFINE_REGISTER_STUB(r46, nullptr),
117 DEFINE_REGISTER_STUB(r47, nullptr),
118 DEFINE_REGISTER_STUB(r48, nullptr),
119 DEFINE_REGISTER_STUB(r49, nullptr),
120 DEFINE_REGISTER_STUB(r50, nullptr),
121 DEFINE_REGISTER_STUB(r51, nullptr),
122 DEFINE_REGISTER_STUB(r52, nullptr),
123 DEFINE_REGISTER_STUB(r53, nullptr),
124 DEFINE_REGISTER_STUB(r54, nullptr),
125 DEFINE_REGISTER_STUB(r55, nullptr),
126 DEFINE_REGISTER_STUB(r56, nullptr),
127 DEFINE_REGISTER_STUB(r57, nullptr),
128 DEFINE_REGISTER_STUB(r58, "accl"),
129 DEFINE_REGISTER_STUB(r59, "acch"),
130 DEFINE_REGISTER_STUB(r60, "lp_count"),
131 DEFINE_REGISTER_STUB(r63, "pcl"),
132 DEFINE_GENERIC_REGISTER_STUB(pc, nullptr, LLDB_REGNUM_GENERIC_PC),
133 DEFINE_GENERIC_REGISTER_STUB(status32, nullptr, LLDB_REGNUM_GENERIC_FLAGS)} };
137 const RegisterInfo *ABISysV_arc::GetRegisterInfoArray(uint32_t &count) {
138 count = dwarf::g_register_infos.size();
139 return dwarf::g_register_infos.data();
142 size_t ABISysV_arc::GetRedZoneSize() const { return 0; }
144 bool ABISysV_arc::IsRegisterFileReduced(RegisterContext ®_ctx) const {
145 if (!m_is_reg_file_reduced) {
146 const auto *const rf_build_reg = reg_ctx.GetRegisterInfoByName("rf_build");
148 const auto reg_value = reg_ctx.ReadRegisterAsUnsigned(rf_build_reg,
150 // RF_BUILD "Number of Entries" bit.
151 const uint32_t rf_entries_bit = 1U << 9U;
152 m_is_reg_file_reduced = (reg_value | rf_entries_bit) != 0;
155 return m_is_reg_file_reduced.getValueOr(false);
158 //------------------------------------------------------------------
160 //------------------------------------------------------------------
162 ABISP ABISysV_arc::CreateInstance(ProcessSP process_sp, const ArchSpec &arch) {
163 return llvm::Triple::arc == arch.GetTriple().getArch() ?
164 ABISP(new ABISysV_arc(std::move(process_sp), MakeMCRegisterInfo(arch))) :
169 const size_t word_size = 4U;
170 const size_t reg_size = word_size;
172 inline size_t AugmentArgSize(size_t size_in_bytes) {
173 return llvm::alignTo(size_in_bytes, word_size);
176 size_t TotalArgsSizeInWords(const llvm::ArrayRef<ABI::CallArgument> &args) {
177 size_t total_size = 0;
178 for (const auto &arg : args)
180 (ABI::CallArgument::TargetValue == arg.type ? AugmentArgSize(arg.size)
188 bool ABISysV_arc::PrepareTrivialCall(Thread &thread, addr_t sp,
189 addr_t func_addr, addr_t return_addr,
190 llvm::ArrayRef<addr_t> args) const {
191 // We don't use the traditional trivial call specialized for jit.
195 bool ABISysV_arc::PrepareTrivialCall(Thread &thread, addr_t sp, addr_t pc,
196 addr_t ra, llvm::Type &prototype,
197 llvm::ArrayRef<ABI::CallArgument> args) const {
198 auto reg_ctx = thread.GetRegisterContext();
202 uint32_t pc_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
203 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
204 if (pc_reg == LLDB_INVALID_REGNUM)
207 uint32_t ra_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
208 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA);
209 if (ra_reg == LLDB_INVALID_REGNUM)
212 uint32_t sp_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
213 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
214 if (sp_reg == LLDB_INVALID_REGNUM)
218 ProcessSP process = thread.GetProcess();
222 // Push host data onto target.
223 for (const auto &arg : args) {
224 // Skip over target values.
225 if (arg.type == ABI::CallArgument::TargetValue)
228 // Create space on the stack for this data 4-byte aligned.
229 sp -= AugmentArgSize(arg.size);
231 if (process->WriteMemory(sp, arg.data_up.get(), arg.size, error) < arg.size
235 // Update the argument with the target pointer.
236 *const_cast<addr_t *>(&arg.value) = sp;
239 // Make sure number of parameters matches prototype.
240 assert(!prototype.isFunctionVarArg());
241 assert(prototype.getFunctionNumParams() == args.size());
243 const size_t regs_for_args_count = IsRegisterFileReduced(*reg_ctx) ? 4U : 8U;
245 // Number of arguments passed on stack.
246 auto args_size = TotalArgsSizeInWords(args);
248 args_size <= regs_for_args_count ? 0 : args_size - regs_for_args_count;
249 auto offset = on_stack * word_size;
251 uint8_t reg_value[reg_size];
252 size_t reg_index = LLDB_REGNUM_GENERIC_ARG1;
254 for (const auto &arg : args) {
255 auto value = reinterpret_cast<const uint8_t *>(&arg.value);
257 ABI::CallArgument::TargetValue == arg.type ? arg.size : reg_size;
259 // Pass arguments via registers.
260 while (size > 0 && reg_index < regs_for_args_count) {
261 size_t byte_index = 0;
262 auto end = size < reg_size ? size : reg_size;
264 while (byte_index < end) {
265 reg_value[byte_index++] = *(value++);
269 while (byte_index < reg_size) {
270 reg_value[byte_index++] = 0;
273 RegisterValue reg_val_obj(reg_value, reg_size, eByteOrderLittle);
274 if (!reg_ctx->WriteRegister(
275 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, reg_index),
279 // NOTE: It's unsafe to iterate through LLDB_REGNUM_GENERICs.
283 if (reg_index < regs_for_args_count || size == 0)
286 // Remaining arguments are passed on the stack.
287 if (process->WriteMemory(sp - offset, value, size, error) < size ||
291 offset -= AugmentArgSize(size);
294 // Set stack pointer immediately below arguments.
295 sp -= on_stack * word_size;
297 // Update registers with current function call state.
298 reg_ctx->WriteRegisterFromUnsigned(pc_reg, pc);
299 reg_ctx->WriteRegisterFromUnsigned(ra_reg, ra);
300 reg_ctx->WriteRegisterFromUnsigned(sp_reg, sp);
305 bool ABISysV_arc::GetArgumentValues(Thread &thread, ValueList &values) const {
309 Status ABISysV_arc::SetReturnValueObject(StackFrameSP &frame_sp,
310 ValueObjectSP &new_value_sp) {
313 result.SetErrorString("Empty value object for return value.");
317 CompilerType compiler_type = new_value_sp->GetCompilerType();
318 if (!compiler_type) {
319 result.SetErrorString("Null clang type for return value.");
323 auto ®_ctx = *frame_sp->GetThread()->GetRegisterContext();
325 bool is_signed = false;
326 if (!compiler_type.IsIntegerOrEnumerationType(is_signed) &&
327 !compiler_type.IsPointerType()) {
328 result.SetErrorString("We don't support returning other types at present");
333 size_t num_bytes = new_value_sp->GetData(data, result);
336 result.SetErrorStringWithFormat(
337 "Couldn't convert return value to raw data: %s", result.AsCString());
341 if (num_bytes <= 2 * reg_size) {
343 uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
346 reg_ctx.GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);
347 if (!reg_ctx.WriteRegisterFromUnsigned(reg_info, raw_value)) {
348 result.SetErrorStringWithFormat("Couldn't write value to register %s",
353 if (num_bytes <= reg_size)
354 return result; // Successfully written.
358 reg_ctx.GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2);
359 if (!reg_ctx.WriteRegisterFromUnsigned(reg_info, raw_value)) {
360 result.SetErrorStringWithFormat("Couldn't write value to register %s",
367 result.SetErrorString(
368 "We don't support returning large integer values at present.");
373 template <typename T>
374 void SetInteger(Scalar &scalar, uint64_t raw_value, bool is_signed) {
375 raw_value &= std::numeric_limits<T>::max();
377 scalar = static_cast<typename std::make_signed<T>::type>(raw_value);
379 scalar = static_cast<T>(raw_value);
382 bool SetSizedInteger(Scalar &scalar, uint64_t raw_value, uint8_t size_in_bytes,
384 switch (size_in_bytes) {
388 case sizeof(uint64_t):
389 SetInteger<uint64_t>(scalar, raw_value, is_signed);
392 case sizeof(uint32_t):
393 SetInteger<uint32_t>(scalar, raw_value, is_signed);
396 case sizeof(uint16_t):
397 SetInteger<uint16_t>(scalar, raw_value, is_signed);
400 case sizeof(uint8_t):
401 SetInteger<uint8_t>(scalar, raw_value, is_signed);
408 bool SetSizedFloat(Scalar &scalar, uint64_t raw_value, uint8_t size_in_bytes) {
409 switch (size_in_bytes) {
413 case sizeof(uint64_t):
414 scalar = *reinterpret_cast<double *>(&raw_value);
417 case sizeof(uint32_t):
418 scalar = *reinterpret_cast<float *>(&raw_value);
425 uint64_t ReadRawValue(const RegisterContextSP ®_ctx, uint8_t size_in_bytes) {
427 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);
429 // Extract the register context so we can read arguments from registers.
431 reg_ctx->ReadRegisterAsUnsigned(reg_info_r0, 0) & UINT32_MAX;
433 if (sizeof(uint64_t) == size_in_bytes)
434 raw_value |= (reg_ctx->ReadRegisterAsUnsigned(
435 reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
436 LLDB_REGNUM_GENERIC_ARG2), 0) &
444 ABISysV_arc::GetReturnValueObjectSimple(Thread &thread,
445 CompilerType &compiler_type) const {
447 return ValueObjectSP();
449 auto reg_ctx = thread.GetRegisterContext();
451 return ValueObjectSP();
454 value.SetCompilerType(compiler_type);
456 const uint32_t type_flags = compiler_type.GetTypeInfo();
457 // Integer return type.
458 if (type_flags & eTypeIsInteger) {
459 const size_t byte_size = compiler_type.GetByteSize(nullptr).getValueOr(0);
460 auto raw_value = ReadRawValue(reg_ctx, byte_size);
462 const bool is_signed = (type_flags & eTypeIsSigned) != 0;
463 if (!SetSizedInteger(value.GetScalar(), raw_value, byte_size, is_signed))
464 return ValueObjectSP();
466 value.SetValueType(Value::eValueTypeScalar);
468 // Pointer return type.
469 else if (type_flags & eTypeIsPointer) {
470 auto reg_info_r0 = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
471 LLDB_REGNUM_GENERIC_ARG1);
472 value.GetScalar() = reg_ctx->ReadRegisterAsUnsigned(reg_info_r0, 0);
474 value.SetValueType(Value::eValueTypeScalar);
476 // Floating point return type.
477 else if (type_flags & eTypeIsFloat) {
478 uint32_t float_count = 0;
479 bool is_complex = false;
481 if (compiler_type.IsFloatingPointType(float_count, is_complex) &&
482 1 == float_count && !is_complex) {
483 const size_t byte_size = compiler_type.GetByteSize(nullptr).getValueOr(0);
484 auto raw_value = ReadRawValue(reg_ctx, byte_size);
486 if (!SetSizedFloat(value.GetScalar(), raw_value, byte_size))
487 return ValueObjectSP();
490 // Unsupported return type.
492 return ValueObjectSP();
494 return ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),
495 value, ConstString(""));
498 ValueObjectSP ABISysV_arc::GetReturnValueObjectImpl(
499 Thread &thread, CompilerType &return_compiler_type) const {
500 ValueObjectSP return_valobj_sp;
502 if (!return_compiler_type)
503 return return_valobj_sp;
505 ExecutionContext exe_ctx(thread.shared_from_this());
506 return GetReturnValueObjectSimple(thread, return_compiler_type);
509 ValueObjectSP ABISysV_arc::GetReturnValueObjectImpl(Thread &thread,
510 llvm::Type &retType) const {
511 auto reg_ctx = thread.GetRegisterContext();
513 return ValueObjectSP();
517 if (retType.isVoidTy()) {
518 value.GetScalar() = 0;
520 // Integer return type.
521 else if (retType.isIntegerTy()) {
522 size_t byte_size = retType.getPrimitiveSizeInBits();
523 if (1 != byte_size) // For boolian type.
524 byte_size /= CHAR_BIT;
526 auto raw_value = ReadRawValue(reg_ctx, byte_size);
528 const bool is_signed = false; // IR Type doesn't provide this info.
529 if (!SetSizedInteger(value.GetScalar(), raw_value, byte_size, is_signed))
530 return ValueObjectSP();
532 // Pointer return type.
533 else if (retType.isPointerTy()) {
534 auto reg_info_r0 = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
535 LLDB_REGNUM_GENERIC_ARG1);
536 value.GetScalar() = reg_ctx->ReadRegisterAsUnsigned(reg_info_r0, 0);
537 value.SetValueType(Value::eValueTypeScalar);
539 // Floating point return type.
540 else if (retType.isFloatingPointTy()) {
541 const size_t byte_size = retType.getPrimitiveSizeInBits() / CHAR_BIT;
542 auto raw_value = ReadRawValue(reg_ctx, byte_size);
544 if (!SetSizedFloat(value.GetScalar(), raw_value, byte_size))
545 return ValueObjectSP();
547 // Unsupported return type.
549 return ValueObjectSP();
551 return ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),
552 value, ConstString(""));
555 bool ABISysV_arc::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
557 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
559 UnwindPlan::RowSP row(new UnwindPlan::Row);
561 // Our Call Frame Address is the stack pointer value.
562 row->GetCFAValue().SetIsRegisterPlusOffset(dwarf::sp, 0);
564 // The previous PC is in the BLINK.
565 row->SetRegisterLocationToRegister(dwarf::pc, dwarf::blink, true);
566 unwind_plan.AppendRow(row);
568 // All other registers are the same.
569 unwind_plan.SetSourceName("arc at-func-entry default");
570 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
575 bool ABISysV_arc::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
579 bool ABISysV_arc::RegisterIsVolatile(const RegisterInfo *reg_info) {
580 if (nullptr == reg_info)
583 // Volatile registers are: r0..r12.
584 uint32_t regnum = reg_info->kinds[eRegisterKindDWARF];
588 static const std::string ra_reg_name = "blink";
589 return ra_reg_name == reg_info->name;
592 void ABISysV_arc::Initialize() {
593 PluginManager::RegisterPlugin(GetPluginNameStatic(),
594 "System V ABI for ARC targets", CreateInstance);
597 void ABISysV_arc::Terminate() {
598 PluginManager::UnregisterPlugin(CreateInstance);
601 ConstString ABISysV_arc::GetPluginNameStatic() {
602 static ConstString g_name("sysv-arc");
606 //------------------------------------------------------------------
607 // PluginInterface protocol
608 //------------------------------------------------------------------
610 ConstString ABISysV_arc::GetPluginName() {
611 return GetPluginNameStatic();
614 uint32_t ABISysV_arc::GetPluginVersion() { return 1; }