1 //===-- ABISysV_arc.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 //===----------------------------------------------------------------------===//
8 
9 #include "ABISysV_arc.h"
10 
11 // C Includes
12 // C++ Includes
13 #include <array>
14 #include <limits>
15 #include <type_traits>
16 
17 // Other libraries and framework includes
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/Support/MathExtras.h"
21 
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"
37 
38 #define DEFINE_REG_NAME(reg_num)      ConstString(#reg_num).GetCString()
39 #define DEFINE_REG_NAME_STR(reg_name) ConstString(reg_name).GetCString()
40 
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.
43 
44 #define DEFINE_GENERIC_REGISTER_STUB(dwarf_num, str_name, generic_num)        \
45   {                                                                           \
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                                              \
50   }
51 
52 #define DEFINE_REGISTER_STUB(dwarf_num, str_name) \
53   DEFINE_GENERIC_REGISTER_STUB(dwarf_num, str_name, LLDB_INVALID_REGNUM)
54 
55 using namespace lldb;
56 using namespace lldb_private;
57 
58 LLDB_PLUGIN_DEFINE_ADV(ABISysV_arc, ABIARC)
59 
60 namespace {
61 namespace dwarf {
62 enum regnums {
63   r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16,
64   r17, r18, r19, r20, r21, r22, r23, r24, r25, r26,
65   r27, fp = r27, r28, sp = r28, r29, r30, r31, blink = r31,
66   r32, r33, r34, r35, r36, r37, r38, r39, r40, r41, r42, r43, r44, r45, r46,
67   r47, r48, r49, r50, r51, r52, r53, r54, r55, r56, r57, r58, r59, r60,
68   /*reserved,*/ /*limm indicator,*/ r63 = 63, pc = 70, status32 = 74
69 };
70 
71 static const std::array<RegisterInfo, 64> g_register_infos = { {
72     DEFINE_GENERIC_REGISTER_STUB(r0, nullptr, LLDB_REGNUM_GENERIC_ARG1),
73     DEFINE_GENERIC_REGISTER_STUB(r1, nullptr, LLDB_REGNUM_GENERIC_ARG2),
74     DEFINE_GENERIC_REGISTER_STUB(r2, nullptr, LLDB_REGNUM_GENERIC_ARG3),
75     DEFINE_GENERIC_REGISTER_STUB(r3, nullptr, LLDB_REGNUM_GENERIC_ARG4),
76     DEFINE_GENERIC_REGISTER_STUB(r4, nullptr, LLDB_REGNUM_GENERIC_ARG5),
77     DEFINE_GENERIC_REGISTER_STUB(r5, nullptr, LLDB_REGNUM_GENERIC_ARG6),
78     DEFINE_GENERIC_REGISTER_STUB(r6, nullptr, LLDB_REGNUM_GENERIC_ARG7),
79     DEFINE_GENERIC_REGISTER_STUB(r7, nullptr, LLDB_REGNUM_GENERIC_ARG8),
80     DEFINE_REGISTER_STUB(r8, nullptr),
81     DEFINE_REGISTER_STUB(r9, nullptr),
82     DEFINE_REGISTER_STUB(r10, nullptr),
83     DEFINE_REGISTER_STUB(r11, nullptr),
84     DEFINE_REGISTER_STUB(r12, nullptr),
85     DEFINE_REGISTER_STUB(r13, nullptr),
86     DEFINE_REGISTER_STUB(r14, nullptr),
87     DEFINE_REGISTER_STUB(r15, nullptr),
88     DEFINE_REGISTER_STUB(r16, nullptr),
89     DEFINE_REGISTER_STUB(r17, nullptr),
90     DEFINE_REGISTER_STUB(r18, nullptr),
91     DEFINE_REGISTER_STUB(r19, nullptr),
92     DEFINE_REGISTER_STUB(r20, nullptr),
93     DEFINE_REGISTER_STUB(r21, nullptr),
94     DEFINE_REGISTER_STUB(r22, nullptr),
95     DEFINE_REGISTER_STUB(r23, nullptr),
96     DEFINE_REGISTER_STUB(r24, nullptr),
97     DEFINE_REGISTER_STUB(r25, nullptr),
98     DEFINE_REGISTER_STUB(r26, "gp"),
99     DEFINE_GENERIC_REGISTER_STUB(r27, "fp", LLDB_REGNUM_GENERIC_FP),
100     DEFINE_GENERIC_REGISTER_STUB(r28, "sp", LLDB_REGNUM_GENERIC_SP),
101     DEFINE_REGISTER_STUB(r29, "ilink"),
102     DEFINE_REGISTER_STUB(r30, nullptr),
103     DEFINE_GENERIC_REGISTER_STUB(r31, "blink", LLDB_REGNUM_GENERIC_RA),
104     DEFINE_REGISTER_STUB(r32, nullptr),
105     DEFINE_REGISTER_STUB(r33, nullptr),
106     DEFINE_REGISTER_STUB(r34, nullptr),
107     DEFINE_REGISTER_STUB(r35, nullptr),
108     DEFINE_REGISTER_STUB(r36, nullptr),
109     DEFINE_REGISTER_STUB(r37, nullptr),
110     DEFINE_REGISTER_STUB(r38, nullptr),
111     DEFINE_REGISTER_STUB(r39, nullptr),
112     DEFINE_REGISTER_STUB(r40, nullptr),
113     DEFINE_REGISTER_STUB(r41, nullptr),
114     DEFINE_REGISTER_STUB(r42, nullptr),
115     DEFINE_REGISTER_STUB(r43, nullptr),
116     DEFINE_REGISTER_STUB(r44, nullptr),
117     DEFINE_REGISTER_STUB(r45, nullptr),
118     DEFINE_REGISTER_STUB(r46, nullptr),
119     DEFINE_REGISTER_STUB(r47, nullptr),
120     DEFINE_REGISTER_STUB(r48, nullptr),
121     DEFINE_REGISTER_STUB(r49, nullptr),
122     DEFINE_REGISTER_STUB(r50, nullptr),
123     DEFINE_REGISTER_STUB(r51, nullptr),
124     DEFINE_REGISTER_STUB(r52, nullptr),
125     DEFINE_REGISTER_STUB(r53, nullptr),
126     DEFINE_REGISTER_STUB(r54, nullptr),
127     DEFINE_REGISTER_STUB(r55, nullptr),
128     DEFINE_REGISTER_STUB(r56, nullptr),
129     DEFINE_REGISTER_STUB(r57, nullptr),
130     DEFINE_REGISTER_STUB(r58, "accl"),
131     DEFINE_REGISTER_STUB(r59, "acch"),
132     DEFINE_REGISTER_STUB(r60, "lp_count"),
133     DEFINE_REGISTER_STUB(r63, "pcl"),
134     DEFINE_GENERIC_REGISTER_STUB(pc, nullptr, LLDB_REGNUM_GENERIC_PC),
135     DEFINE_GENERIC_REGISTER_STUB(status32, nullptr, LLDB_REGNUM_GENERIC_FLAGS)} };
136 } // namespace dwarf
137 } // namespace
138 
GetRegisterInfoArray(uint32_t & count)139 const RegisterInfo *ABISysV_arc::GetRegisterInfoArray(uint32_t &count) {
140   count = dwarf::g_register_infos.size();
141   return dwarf::g_register_infos.data();
142 }
143 
GetRedZoneSize() const144 size_t ABISysV_arc::GetRedZoneSize() const { return 0; }
145 
IsRegisterFileReduced(RegisterContext & reg_ctx) const146 bool ABISysV_arc::IsRegisterFileReduced(RegisterContext &reg_ctx) const {
147   if (!m_is_reg_file_reduced) {
148     const auto *const rf_build_reg = reg_ctx.GetRegisterInfoByName("rf_build");
149 
150     const auto reg_value = reg_ctx.ReadRegisterAsUnsigned(rf_build_reg,
151                                                           /*fail_value*/ 0);
152     // RF_BUILD "Number of Entries" bit.
153     const uint32_t rf_entries_bit = 1U << 9U;
154     m_is_reg_file_reduced = (reg_value | rf_entries_bit) != 0;
155   }
156 
157   return m_is_reg_file_reduced.getValueOr(false);
158 }
159 
160 //------------------------------------------------------------------
161 // Static Functions
162 //------------------------------------------------------------------
163 
CreateInstance(ProcessSP process_sp,const ArchSpec & arch)164 ABISP ABISysV_arc::CreateInstance(ProcessSP process_sp, const ArchSpec &arch) {
165   return llvm::Triple::arc == arch.GetTriple().getArch() ?
166       ABISP(new ABISysV_arc(std::move(process_sp), MakeMCRegisterInfo(arch))) :
167       ABISP();
168 }
169 
170 namespace {
171 const size_t word_size = 4U;
172 const size_t reg_size = word_size;
173 
AugmentArgSize(size_t size_in_bytes)174 inline size_t AugmentArgSize(size_t size_in_bytes) {
175   return llvm::alignTo(size_in_bytes, word_size);
176 }
177 
TotalArgsSizeInWords(const llvm::ArrayRef<ABI::CallArgument> & args)178 size_t TotalArgsSizeInWords(const llvm::ArrayRef<ABI::CallArgument> &args) {
179   size_t total_size = 0;
180   for (const auto &arg : args)
181     total_size +=
182         (ABI::CallArgument::TargetValue == arg.type ? AugmentArgSize(arg.size)
183                                                     : reg_size) /
184         word_size;
185 
186   return total_size;
187 }
188 } // namespace
189 
PrepareTrivialCall(Thread & thread,addr_t sp,addr_t func_addr,addr_t return_addr,llvm::ArrayRef<addr_t> args) const190 bool ABISysV_arc::PrepareTrivialCall(Thread &thread, addr_t sp,
191                                      addr_t func_addr, addr_t return_addr,
192                                      llvm::ArrayRef<addr_t> args) const {
193   // We don't use the traditional trivial call specialized for jit.
194   return false;
195 }
196 
PrepareTrivialCall(Thread & thread,addr_t sp,addr_t pc,addr_t ra,llvm::Type & prototype,llvm::ArrayRef<ABI::CallArgument> args) const197 bool ABISysV_arc::PrepareTrivialCall(Thread &thread, addr_t sp, addr_t pc,
198     addr_t ra, llvm::Type &prototype,
199     llvm::ArrayRef<ABI::CallArgument> args) const {
200   auto reg_ctx = thread.GetRegisterContext();
201   if (!reg_ctx)
202     return false;
203 
204   uint32_t pc_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
205       eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
206   if (pc_reg == LLDB_INVALID_REGNUM)
207     return false;
208 
209   uint32_t ra_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
210       eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA);
211   if (ra_reg == LLDB_INVALID_REGNUM)
212     return false;
213 
214   uint32_t sp_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
215       eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
216   if (sp_reg == LLDB_INVALID_REGNUM)
217     return false;
218 
219   Status error;
220   ProcessSP process = thread.GetProcess();
221   if (!process)
222     return false;
223 
224   // Push host data onto target.
225   for (const auto &arg : args) {
226     // Skip over target values.
227     if (arg.type == ABI::CallArgument::TargetValue)
228       continue;
229 
230     // Create space on the stack for this data 4-byte aligned.
231     sp -= AugmentArgSize(arg.size);
232 
233     if (process->WriteMemory(sp, arg.data_up.get(), arg.size, error) < arg.size
234         || error.Fail())
235       return false;
236 
237     // Update the argument with the target pointer.
238     *const_cast<addr_t *>(&arg.value) = sp;
239   }
240 
241   // Make sure number of parameters matches prototype.
242   assert(!prototype.isFunctionVarArg());
243   assert(prototype.getFunctionNumParams() == args.size());
244 
245   const size_t regs_for_args_count = IsRegisterFileReduced(*reg_ctx) ? 4U : 8U;
246 
247   // Number of arguments passed on stack.
248   auto args_size = TotalArgsSizeInWords(args);
249   auto on_stack =
250       args_size <= regs_for_args_count ? 0 : args_size - regs_for_args_count;
251   auto offset = on_stack * word_size;
252 
253   uint8_t reg_value[reg_size];
254   size_t reg_index = LLDB_REGNUM_GENERIC_ARG1;
255 
256   for (const auto &arg : args) {
257     auto value = reinterpret_cast<const uint8_t *>(&arg.value);
258     auto size =
259         ABI::CallArgument::TargetValue == arg.type ? arg.size : reg_size;
260 
261     // Pass arguments via registers.
262     while (size > 0 && reg_index < regs_for_args_count) {
263       size_t byte_index = 0;
264       auto end = size < reg_size ? size : reg_size;
265 
266       while (byte_index < end) {
267         reg_value[byte_index++] = *(value++);
268         --size;
269       }
270 
271       while (byte_index < reg_size) {
272         reg_value[byte_index++] = 0;
273       }
274 
275       RegisterValue reg_val_obj(llvm::makeArrayRef(reg_value, reg_size),
276                                 eByteOrderLittle);
277       if (!reg_ctx->WriteRegister(
278             reg_ctx->GetRegisterInfo(eRegisterKindGeneric, reg_index),
279             reg_val_obj))
280         return false;
281 
282       // NOTE: It's unsafe to iterate through LLDB_REGNUM_GENERICs.
283       ++reg_index;
284     }
285 
286     if (reg_index < regs_for_args_count || size == 0)
287       continue;
288 
289     // Remaining arguments are passed on the stack.
290     if (process->WriteMemory(sp - offset, value, size, error) < size ||
291         !error.Success())
292       return false;
293 
294     offset -= AugmentArgSize(size);
295   }
296 
297   // Set stack pointer immediately below arguments.
298   sp -= on_stack * word_size;
299 
300   // Update registers with current function call state.
301   reg_ctx->WriteRegisterFromUnsigned(pc_reg, pc);
302   reg_ctx->WriteRegisterFromUnsigned(ra_reg, ra);
303   reg_ctx->WriteRegisterFromUnsigned(sp_reg, sp);
304 
305   return true;
306 }
307 
GetArgumentValues(Thread & thread,ValueList & values) const308 bool ABISysV_arc::GetArgumentValues(Thread &thread, ValueList &values) const {
309   return false;
310 }
311 
SetReturnValueObject(StackFrameSP & frame_sp,ValueObjectSP & new_value_sp)312 Status ABISysV_arc::SetReturnValueObject(StackFrameSP &frame_sp,
313                                          ValueObjectSP &new_value_sp) {
314   Status result;
315   if (!new_value_sp) {
316     result.SetErrorString("Empty value object for return value.");
317     return result;
318   }
319 
320   CompilerType compiler_type = new_value_sp->GetCompilerType();
321   if (!compiler_type) {
322     result.SetErrorString("Null clang type for return value.");
323     return result;
324   }
325 
326   auto &reg_ctx = *frame_sp->GetThread()->GetRegisterContext();
327 
328   bool is_signed = false;
329   if (!compiler_type.IsIntegerOrEnumerationType(is_signed) &&
330       !compiler_type.IsPointerType()) {
331     result.SetErrorString("We don't support returning other types at present");
332     return result;
333   }
334 
335   DataExtractor data;
336   size_t num_bytes = new_value_sp->GetData(data, result);
337 
338   if (result.Fail()) {
339     result.SetErrorStringWithFormat(
340         "Couldn't convert return value to raw data: %s", result.AsCString());
341     return result;
342   }
343 
344   if (num_bytes <= 2 * reg_size) {
345     offset_t offset = 0;
346     uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
347 
348     auto reg_info =
349         reg_ctx.GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);
350     if (!reg_ctx.WriteRegisterFromUnsigned(reg_info, raw_value)) {
351       result.SetErrorStringWithFormat("Couldn't write value to register %s",
352                                       reg_info->name);
353       return result;
354     }
355 
356     if (num_bytes <= reg_size)
357       return result; // Successfully written.
358 
359     raw_value >>= 32;
360     reg_info =
361         reg_ctx.GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2);
362     if (!reg_ctx.WriteRegisterFromUnsigned(reg_info, raw_value)) {
363       result.SetErrorStringWithFormat("Couldn't write value to register %s",
364                                       reg_info->name);
365     }
366 
367     return result;
368   }
369 
370   result.SetErrorString(
371       "We don't support returning large integer values at present.");
372   return result;
373 }
374 
375 namespace {
376 template <typename T>
SetInteger(Scalar & scalar,uint64_t raw_value,bool is_signed)377 void SetInteger(Scalar &scalar, uint64_t raw_value, bool is_signed) {
378   raw_value &= std::numeric_limits<T>::max();
379   if (is_signed)
380     scalar = static_cast<typename std::make_signed<T>::type>(raw_value);
381   else
382     scalar = static_cast<T>(raw_value);
383 }
384 
SetSizedInteger(Scalar & scalar,uint64_t raw_value,uint8_t size_in_bytes,bool is_signed)385 bool SetSizedInteger(Scalar &scalar, uint64_t raw_value, uint8_t size_in_bytes,
386                      bool is_signed) {
387   switch (size_in_bytes) {
388   default:
389     return false;
390 
391   case sizeof(uint64_t):
392     SetInteger<uint64_t>(scalar, raw_value, is_signed);
393     break;
394 
395   case sizeof(uint32_t):
396     SetInteger<uint32_t>(scalar, raw_value, is_signed);
397     break;
398 
399   case sizeof(uint16_t):
400     SetInteger<uint16_t>(scalar, raw_value, is_signed);
401     break;
402 
403   case sizeof(uint8_t):
404     SetInteger<uint8_t>(scalar, raw_value, is_signed);
405     break;
406   }
407 
408   return true;
409 }
410 
SetSizedFloat(Scalar & scalar,uint64_t raw_value,uint8_t size_in_bytes)411 bool SetSizedFloat(Scalar &scalar, uint64_t raw_value, uint8_t size_in_bytes) {
412   switch (size_in_bytes) {
413   default:
414     return false;
415 
416   case sizeof(uint64_t):
417     scalar = *reinterpret_cast<double *>(&raw_value);
418     break;
419 
420   case sizeof(uint32_t):
421     scalar = *reinterpret_cast<float *>(&raw_value);
422     break;
423   }
424 
425   return true;
426 }
427 
ReadRawValue(const RegisterContextSP & reg_ctx,uint8_t size_in_bytes)428 uint64_t ReadRawValue(const RegisterContextSP &reg_ctx, uint8_t size_in_bytes) {
429   auto reg_info_r0 =
430       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);
431 
432   // Extract the register context so we can read arguments from registers.
433   uint64_t raw_value =
434       reg_ctx->ReadRegisterAsUnsigned(reg_info_r0, 0) & UINT32_MAX;
435 
436   if (sizeof(uint64_t) == size_in_bytes)
437     raw_value |= (reg_ctx->ReadRegisterAsUnsigned(
438                       reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
439                                                LLDB_REGNUM_GENERIC_ARG2), 0) &
440                   UINT64_MAX) << 32U;
441 
442   return raw_value;
443 }
444 } // namespace
445 
446 ValueObjectSP
GetReturnValueObjectSimple(Thread & thread,CompilerType & compiler_type) const447 ABISysV_arc::GetReturnValueObjectSimple(Thread &thread,
448                                         CompilerType &compiler_type) const {
449   if (!compiler_type)
450     return ValueObjectSP();
451 
452   auto reg_ctx = thread.GetRegisterContext();
453   if (!reg_ctx)
454     return ValueObjectSP();
455 
456   Value value;
457   value.SetCompilerType(compiler_type);
458 
459   const uint32_t type_flags = compiler_type.GetTypeInfo();
460   // Integer return type.
461   if (type_flags & eTypeIsInteger) {
462     const size_t byte_size = compiler_type.GetByteSize(&thread).getValueOr(0);
463     auto raw_value = ReadRawValue(reg_ctx, byte_size);
464 
465     const bool is_signed = (type_flags & eTypeIsSigned) != 0;
466     if (!SetSizedInteger(value.GetScalar(), raw_value, byte_size, is_signed))
467       return ValueObjectSP();
468 
469     value.SetValueType(Value::eValueTypeScalar);
470   }
471   // Pointer return type.
472   else if (type_flags & eTypeIsPointer) {
473     auto reg_info_r0 = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
474                                                 LLDB_REGNUM_GENERIC_ARG1);
475     value.GetScalar() = reg_ctx->ReadRegisterAsUnsigned(reg_info_r0, 0);
476 
477     value.SetValueType(Value::eValueTypeScalar);
478   }
479   // Floating point return type.
480   else if (type_flags & eTypeIsFloat) {
481     uint32_t float_count = 0;
482     bool is_complex = false;
483 
484     if (compiler_type.IsFloatingPointType(float_count, is_complex) &&
485         1 == float_count && !is_complex) {
486       const size_t byte_size = compiler_type.GetByteSize(&thread).getValueOr(0);
487       auto raw_value = ReadRawValue(reg_ctx, byte_size);
488 
489       if (!SetSizedFloat(value.GetScalar(), raw_value, byte_size))
490         return ValueObjectSP();
491     }
492   }
493   // Unsupported return type.
494   else
495     return ValueObjectSP();
496 
497   return ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),
498                                         value, ConstString(""));
499 }
500 
GetReturnValueObjectImpl(Thread & thread,CompilerType & return_compiler_type) const501 ValueObjectSP ABISysV_arc::GetReturnValueObjectImpl(
502     Thread &thread, CompilerType &return_compiler_type) const {
503   ValueObjectSP return_valobj_sp;
504 
505   if (!return_compiler_type)
506     return return_valobj_sp;
507 
508   ExecutionContext exe_ctx(thread.shared_from_this());
509   return GetReturnValueObjectSimple(thread, return_compiler_type);
510 }
511 
GetReturnValueObjectImpl(Thread & thread,llvm::Type & retType) const512 ValueObjectSP ABISysV_arc::GetReturnValueObjectImpl(Thread &thread,
513                                                     llvm::Type &retType) const {
514   auto reg_ctx = thread.GetRegisterContext();
515   if (!reg_ctx)
516     return ValueObjectSP();
517 
518   Value value;
519   // Void return type.
520   if (retType.isVoidTy()) {
521     value.GetScalar() = 0;
522   }
523   // Integer return type.
524   else if (retType.isIntegerTy()) {
525     size_t byte_size = retType.getPrimitiveSizeInBits();
526     if (1 != byte_size) // For boolean type.
527       byte_size /= CHAR_BIT;
528 
529     auto raw_value = ReadRawValue(reg_ctx, byte_size);
530 
531     const bool is_signed = false; // IR Type doesn't provide this info.
532     if (!SetSizedInteger(value.GetScalar(), raw_value, byte_size, is_signed))
533       return ValueObjectSP();
534   }
535   // Pointer return type.
536   else if (retType.isPointerTy()) {
537     auto reg_info_r0 = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
538                                                 LLDB_REGNUM_GENERIC_ARG1);
539     value.GetScalar() = reg_ctx->ReadRegisterAsUnsigned(reg_info_r0, 0);
540     value.SetValueType(Value::eValueTypeScalar);
541   }
542   // Floating point return type.
543   else if (retType.isFloatingPointTy()) {
544     const size_t byte_size = retType.getPrimitiveSizeInBits() / CHAR_BIT;
545     auto raw_value = ReadRawValue(reg_ctx, byte_size);
546 
547     if (!SetSizedFloat(value.GetScalar(), raw_value, byte_size))
548       return ValueObjectSP();
549   }
550   // Unsupported return type.
551   else
552     return ValueObjectSP();
553 
554   return ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),
555                                         value, ConstString(""));
556 }
557 
CreateFunctionEntryUnwindPlan(UnwindPlan & unwind_plan)558 bool ABISysV_arc::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
559   unwind_plan.Clear();
560   unwind_plan.SetRegisterKind(eRegisterKindDWARF);
561 
562   UnwindPlan::RowSP row(new UnwindPlan::Row);
563 
564   // Our Call Frame Address is the stack pointer value.
565   row->GetCFAValue().SetIsRegisterPlusOffset(dwarf::sp, 0);
566 
567   // The previous PC is in the BLINK.
568   row->SetRegisterLocationToRegister(dwarf::pc, dwarf::blink, true);
569   unwind_plan.AppendRow(row);
570 
571   // All other registers are the same.
572   unwind_plan.SetSourceName("arc at-func-entry default");
573   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
574 
575   return true;
576 }
577 
CreateDefaultUnwindPlan(UnwindPlan & unwind_plan)578 bool ABISysV_arc::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
579   return false;
580 }
581 
RegisterIsVolatile(const RegisterInfo * reg_info)582 bool ABISysV_arc::RegisterIsVolatile(const RegisterInfo *reg_info) {
583   if (nullptr == reg_info)
584     return false;
585 
586   // Volatile registers are: r0..r12.
587   uint32_t regnum = reg_info->kinds[eRegisterKindDWARF];
588   if (regnum <= 12)
589     return true;
590 
591   static const std::string ra_reg_name = "blink";
592   return ra_reg_name == reg_info->name;
593 }
594 
Initialize()595 void ABISysV_arc::Initialize() {
596   PluginManager::RegisterPlugin(GetPluginNameStatic(),
597                                 "System V ABI for ARC targets", CreateInstance);
598 }
599 
Terminate()600 void ABISysV_arc::Terminate() {
601   PluginManager::UnregisterPlugin(CreateInstance);
602 }
603 
GetPluginNameStatic()604 ConstString ABISysV_arc::GetPluginNameStatic() {
605   static ConstString g_name("sysv-arc");
606   return g_name;
607 }
608 
609 //------------------------------------------------------------------
610 // PluginInterface protocol
611 //------------------------------------------------------------------
612 
GetPluginName()613 ConstString ABISysV_arc::GetPluginName() {
614   return GetPluginNameStatic();
615 }
616 
GetPluginVersion()617 uint32_t ABISysV_arc::GetPluginVersion() { return 1; }
618