aboutsummaryrefslogtreecommitdiffstats
path: root/source/Plugins/ABI/SysV-arc/ABISysV_arc.cpp
blob: 715b5e5d2b95b0c6081dacaac9b9b673590dd791 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//===-- ABISysV_arc.cpp ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "ABISysV_arc.h"

// C Includes
// C++ Includes
#include <array>
#include <limits>
#include <type_traits>

// Other libraries and framework includes
#include "llvm/ADT/Triple.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/Support/MathExtras.h"

#include "lldb/Core/Module.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Value.h"
#include "lldb/Core/ValueObjectConstResult.h"
#include "lldb/Core/ValueObjectMemory.h"
#include "lldb/Core/ValueObjectRegister.h"
#include "lldb/Symbol/UnwindPlan.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/RegisterValue.h"
#include "lldb/Utility/Status.h"

#define DEFINE_REG_NAME(reg_num)      ConstString(#reg_num).GetCString()
#define DEFINE_REG_NAME_STR(reg_name) ConstString(reg_name).GetCString()

// The ABI is not a source of such information as size, offset, encoding, etc.
// of a register. Just provides correct dwarf and eh_frame numbers.

#define DEFINE_GENERIC_REGISTER_STUB(dwarf_num, str_name, generic_num)        \
  {                                                                           \
    DEFINE_REG_NAME(dwarf_num), DEFINE_REG_NAME_STR(str_name),                \
    0, 0, eEncodingInvalid, eFormatDefault,                                   \
    { dwarf_num, dwarf_num, generic_num, LLDB_INVALID_REGNUM, dwarf_num },    \
    nullptr, nullptr, nullptr, 0                                              \
  }

#define DEFINE_REGISTER_STUB(dwarf_num, str_name) \
  DEFINE_GENERIC_REGISTER_STUB(dwarf_num, str_name, LLDB_INVALID_REGNUM)

using namespace lldb;
using namespace lldb_private;

namespace {
namespace dwarf {
enum regnums {
  r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16,
  r17, r18, r19, r20, r21, r22, r23, r24, r25, r26,
  r27, fp = r27, r28, sp = r28, r29, r30, r31, blink = r31,
  r32, r33, r34, r35, r36, r37, r38, r39, r40, r41, r42, r43, r44, r45, r46,
  r47, r48, r49, r50, r51, r52, r53, r54, r55, r56, r57, r58, r59, r60,
  /*reserved,*/ /*limm indicator,*/ r63 = 63, pc = 70, status32 = 74
};

static const std::array<RegisterInfo, 64> g_register_infos = { {
    DEFINE_GENERIC_REGISTER_STUB(r0, nullptr, LLDB_REGNUM_GENERIC_ARG1),
    DEFINE_GENERIC_REGISTER_STUB(r1, nullptr, LLDB_REGNUM_GENERIC_ARG2),
    DEFINE_GENERIC_REGISTER_STUB(r2, nullptr, LLDB_REGNUM_GENERIC_ARG3),
    DEFINE_GENERIC_REGISTER_STUB(r3, nullptr, LLDB_REGNUM_GENERIC_ARG4),
    DEFINE_GENERIC_REGISTER_STUB(r4, nullptr, LLDB_REGNUM_GENERIC_ARG5),
    DEFINE_GENERIC_REGISTER_STUB(r5, nullptr, LLDB_REGNUM_GENERIC_ARG6),
    DEFINE_GENERIC_REGISTER_STUB(r6, nullptr, LLDB_REGNUM_GENERIC_ARG7),
    DEFINE_GENERIC_REGISTER_STUB(r7, nullptr, LLDB_REGNUM_GENERIC_ARG8),
    DEFINE_REGISTER_STUB(r8, nullptr),
    DEFINE_REGISTER_STUB(r9, nullptr),
    DEFINE_REGISTER_STUB(r10, nullptr),
    DEFINE_REGISTER_STUB(r11, nullptr),
    DEFINE_REGISTER_STUB(r12, nullptr),
    DEFINE_REGISTER_STUB(r13, nullptr),
    DEFINE_REGISTER_STUB(r14, nullptr),
    DEFINE_REGISTER_STUB(r15, nullptr),
    DEFINE_REGISTER_STUB(r16, nullptr),
    DEFINE_REGISTER_STUB(r17, nullptr),
    DEFINE_REGISTER_STUB(r18, nullptr),
    DEFINE_REGISTER_STUB(r19, nullptr),
    DEFINE_REGISTER_STUB(r20, nullptr),
    DEFINE_REGISTER_STUB(r21, nullptr),
    DEFINE_REGISTER_STUB(r22, nullptr),
    DEFINE_REGISTER_STUB(r23, nullptr),
    DEFINE_REGISTER_STUB(r24, nullptr),
    DEFINE_REGISTER_STUB(r25, nullptr),
    DEFINE_REGISTER_STUB(r26, "gp"),
    DEFINE_GENERIC_REGISTER_STUB(r27, "fp", LLDB_REGNUM_GENERIC_FP),
    DEFINE_GENERIC_REGISTER_STUB(r28, "sp", LLDB_REGNUM_GENERIC_SP),
    DEFINE_REGISTER_STUB(r29, "ilink"),
    DEFINE_REGISTER_STUB(r30, nullptr),
    DEFINE_GENERIC_REGISTER_STUB(r31, "blink", LLDB_REGNUM_GENERIC_RA),
    DEFINE_REGISTER_STUB(r32, nullptr),
    DEFINE_REGISTER_STUB(r33, nullptr),
    DEFINE_REGISTER_STUB(r34, nullptr),
    DEFINE_REGISTER_STUB(r35, nullptr),
    DEFINE_REGISTER_STUB(r36, nullptr),
    DEFINE_REGISTER_STUB(r37, nullptr),
    DEFINE_REGISTER_STUB(r38, nullptr),
    DEFINE_REGISTER_STUB(r39, nullptr),
    DEFINE_REGISTER_STUB(r40, nullptr),
    DEFINE_REGISTER_STUB(r41, nullptr),
    DEFINE_REGISTER_STUB(r42, nullptr),
    DEFINE_REGISTER_STUB(r43, nullptr),
    DEFINE_REGISTER_STUB(r44, nullptr),
    DEFINE_REGISTER_STUB(r45, nullptr),
    DEFINE_REGISTER_STUB(r46, nullptr),
    DEFINE_REGISTER_STUB(r47, nullptr),
    DEFINE_REGISTER_STUB(r48, nullptr),
    DEFINE_REGISTER_STUB(r49, nullptr),
    DEFINE_REGISTER_STUB(r50, nullptr),
    DEFINE_REGISTER_STUB(r51, nullptr),
    DEFINE_REGISTER_STUB(r52, nullptr),
    DEFINE_REGISTER_STUB(r53, nullptr),
    DEFINE_REGISTER_STUB(r54, nullptr),
    DEFINE_REGISTER_STUB(r55, nullptr),
    DEFINE_REGISTER_STUB(r56, nullptr),
    DEFINE_REGISTER_STUB(r57, nullptr),
    DEFINE_REGISTER_STUB(r58, "accl"),
    DEFINE_REGISTER_STUB(r59, "acch"),
    DEFINE_REGISTER_STUB(r60, "lp_count"),
    DEFINE_REGISTER_STUB(r63, "pcl"),
    DEFINE_GENERIC_REGISTER_STUB(pc, nullptr, LLDB_REGNUM_GENERIC_PC),
    DEFINE_GENERIC_REGISTER_STUB(status32, nullptr, LLDB_REGNUM_GENERIC_FLAGS)} };
} // namespace dwarf
} // namespace

const RegisterInfo *ABISysV_arc::GetRegisterInfoArray(uint32_t &count) {
  count = dwarf::g_register_infos.size();
  return dwarf::g_register_infos.data();
}

size_t ABISysV_arc::GetRedZoneSize() const { return 0; }

bool ABISysV_arc::IsRegisterFileReduced(RegisterContext &reg_ctx) const {
  if (!m_is_reg_file_reduced) {
    const auto *const rf_build_reg = reg_ctx.GetRegisterInfoByName("rf_build");
    
    const auto reg_value = reg_ctx.ReadRegisterAsUnsigned(rf_build_reg,
                                                          /*fail_value*/ 0);
    // RF_BUILD "Number of Entries" bit.
    const uint32_t rf_entries_bit = 1U << 9U;
    m_is_reg_file_reduced = (reg_value | rf_entries_bit) != 0;
  }

  return m_is_reg_file_reduced.getValueOr(false);
}

//------------------------------------------------------------------
// Static Functions
//------------------------------------------------------------------

ABISP ABISysV_arc::CreateInstance(ProcessSP process_sp, const ArchSpec &arch) {
  return llvm::Triple::arc == arch.GetTriple().getArch() ?
      ABISP(new ABISysV_arc(std::move(process_sp), MakeMCRegisterInfo(arch))) :
      ABISP();
}

namespace {
const size_t word_size = 4U;
const size_t reg_size = word_size;

inline size_t AugmentArgSize(size_t size_in_bytes) {
  return llvm::alignTo(size_in_bytes, word_size);
}

size_t TotalArgsSizeInWords(const llvm::ArrayRef<ABI::CallArgument> &args) {
  size_t total_size = 0;
  for (const auto &arg : args)
    total_size +=
        (ABI::CallArgument::TargetValue == arg.type ? AugmentArgSize(arg.size)
                                                    : reg_size) /
        word_size;

  return total_size;
}
} // namespace

bool ABISysV_arc::PrepareTrivialCall(Thread &thread, addr_t sp,
                                     addr_t func_addr, addr_t return_addr,
                                     llvm::ArrayRef<addr_t> args) const {
  // We don't use the traditional trivial call specialized for jit.
  return false;
}

bool ABISysV_arc::PrepareTrivialCall(Thread &thread, addr_t sp, addr_t pc,
    addr_t ra, llvm::Type &prototype,
    llvm::ArrayRef<ABI::CallArgument> args) const {
  auto reg_ctx = thread.GetRegisterContext();
  if (!reg_ctx)
    return false;

  uint32_t pc_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
      eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
  if (pc_reg == LLDB_INVALID_REGNUM)
    return false;

  uint32_t ra_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
      eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA);
  if (ra_reg == LLDB_INVALID_REGNUM)
    return false;

  uint32_t sp_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
      eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
  if (sp_reg == LLDB_INVALID_REGNUM)
    return false;

  Status error;
  ProcessSP process = thread.GetProcess();
  if (!process)
    return false;

  // Push host data onto target.
  for (const auto &arg : args) {
    // Skip over target values.
    if (arg.type == ABI::CallArgument::TargetValue)
      continue;

    // Create space on the stack for this data 4-byte aligned.
    sp -= AugmentArgSize(arg.size);

    if (process->WriteMemory(sp, arg.data_up.get(), arg.size, error) < arg.size
        || error.Fail())
      return false;

    // Update the argument with the target pointer.
    *const_cast<addr_t *>(&arg.value) = sp;
  }

  // Make sure number of parameters matches prototype.
  assert(!prototype.isFunctionVarArg());
  assert(prototype.getFunctionNumParams() == args.size());
  
  const size_t regs_for_args_count = IsRegisterFileReduced(*reg_ctx) ? 4U : 8U;

  // Number of arguments passed on stack.
  auto args_size = TotalArgsSizeInWords(args);
  auto on_stack =
      args_size <= regs_for_args_count ? 0 : args_size - regs_for_args_count;
  auto offset = on_stack * word_size;

  uint8_t reg_value[reg_size];
  size_t reg_index = LLDB_REGNUM_GENERIC_ARG1;

  for (const auto &arg : args) {
    auto value = reinterpret_cast<const uint8_t *>(&arg.value);
    auto size =
        ABI::CallArgument::TargetValue == arg.type ? arg.size : reg_size;

    // Pass arguments via registers.
    while (size > 0 && reg_index < regs_for_args_count) {
      size_t byte_index = 0;
      auto end = size < reg_size ? size : reg_size;

      while (byte_index < end) {
        reg_value[byte_index++] = *(value++);
        --size;
      }

      while (byte_index < reg_size) {
        reg_value[byte_index++] = 0;
      }

      RegisterValue reg_val_obj(reg_value, reg_size, eByteOrderLittle);
      if (!reg_ctx->WriteRegister(
            reg_ctx->GetRegisterInfo(eRegisterKindGeneric, reg_index),
            reg_val_obj))
        return false;

      // NOTE: It's unsafe to iterate through LLDB_REGNUM_GENERICs.
      ++reg_index;
    }

    if (reg_index < regs_for_args_count || size == 0)
      continue;

    // Remaining arguments are passed on the stack.
    if (process->WriteMemory(sp - offset, value, size, error) < size ||
        !error.Success())
      return false;

    offset -= AugmentArgSize(size);
  }

  // Set stack pointer immediately below arguments.
  sp -= on_stack * word_size;

  // Update registers with current function call state.
  reg_ctx->WriteRegisterFromUnsigned(pc_reg, pc);
  reg_ctx->WriteRegisterFromUnsigned(ra_reg, ra);
  reg_ctx->WriteRegisterFromUnsigned(sp_reg, sp);

  return true;
}

bool ABISysV_arc::GetArgumentValues(Thread &thread, ValueList &values) const {
  return false;
}

Status ABISysV_arc::SetReturnValueObject(StackFrameSP &frame_sp,
                                         ValueObjectSP &new_value_sp) {
  Status result;
  if (!new_value_sp) {
    result.SetErrorString("Empty value object for return value.");
    return result;
  }

  CompilerType compiler_type = new_value_sp->GetCompilerType();
  if (!compiler_type) {
    result.SetErrorString("Null clang type for return value.");
    return result;
  }

  auto &reg_ctx = *frame_sp->GetThread()->GetRegisterContext();

  bool is_signed = false;
  if (!compiler_type.IsIntegerOrEnumerationType(is_signed) &&
      !compiler_type.IsPointerType()) {
    result.SetErrorString("We don't support returning other types at present");
    return result;
  }

  DataExtractor data;
  size_t num_bytes = new_value_sp->GetData(data, result);

  if (result.Fail()) {
    result.SetErrorStringWithFormat(
        "Couldn't convert return value to raw data: %s", result.AsCString());
    return result;
  }

  if (num_bytes <= 2 * reg_size) {
    offset_t offset = 0;
    uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);

    auto reg_info =
        reg_ctx.GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);
    if (!reg_ctx.WriteRegisterFromUnsigned(reg_info, raw_value)) {
      result.SetErrorStringWithFormat("Couldn't write value to register %s",
                                      reg_info->name);
      return result;
    }

    if (num_bytes <= reg_size)
      return result; // Successfully written.

    raw_value >>= 32;
    reg_info =
        reg_ctx.GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2);
    if (!reg_ctx.WriteRegisterFromUnsigned(reg_info, raw_value)) {
      result.SetErrorStringWithFormat("Couldn't write value to register %s",
                                      reg_info->name);
    }

    return result;
  }

  result.SetErrorString(
      "We don't support returning large integer values at present.");
  return result;
}

namespace {
template <typename T>
void SetInteger(Scalar &scalar, uint64_t raw_value, bool is_signed) {
  raw_value &= std::numeric_limits<T>::max();
  if (is_signed)
    scalar = static_cast<typename std::make_signed<T>::type>(raw_value);
  else
    scalar = static_cast<T>(raw_value);
}

bool SetSizedInteger(Scalar &scalar, uint64_t raw_value, uint8_t size_in_bytes,
                     bool is_signed) {
  switch (size_in_bytes) {
  default:
    return false;

  case sizeof(uint64_t):
    SetInteger<uint64_t>(scalar, raw_value, is_signed);
    break;

  case sizeof(uint32_t):
    SetInteger<uint32_t>(scalar, raw_value, is_signed);
    break;

  case sizeof(uint16_t):
    SetInteger<uint16_t>(scalar, raw_value, is_signed);
    break;

  case sizeof(uint8_t):
    SetInteger<uint8_t>(scalar, raw_value, is_signed);
    break;
  }

  return true;
}

bool SetSizedFloat(Scalar &scalar, uint64_t raw_value, uint8_t size_in_bytes) {
  switch (size_in_bytes) {
  default:
    return false;

  case sizeof(uint64_t):
    scalar = *reinterpret_cast<double *>(&raw_value);
    break;

  case sizeof(uint32_t):
    scalar = *reinterpret_cast<float *>(&raw_value);
    break;
  }

  return true;
}

uint64_t ReadRawValue(const RegisterContextSP &reg_ctx, uint8_t size_in_bytes) {
  auto reg_info_r0 =
      reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);

  // Extract the register context so we can read arguments from registers.
  uint64_t raw_value =
      reg_ctx->ReadRegisterAsUnsigned(reg_info_r0, 0) & UINT32_MAX;

  if (sizeof(uint64_t) == size_in_bytes)
    raw_value |= (reg_ctx->ReadRegisterAsUnsigned(
                      reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
                                               LLDB_REGNUM_GENERIC_ARG2), 0) &
                  UINT64_MAX) << 32U;

  return raw_value;
}
} // namespace

ValueObjectSP
ABISysV_arc::GetReturnValueObjectSimple(Thread &thread,
                                        CompilerType &compiler_type) const {
  if (!compiler_type)
    return ValueObjectSP();

  auto reg_ctx = thread.GetRegisterContext();
  if (!reg_ctx)
    return ValueObjectSP();

  Value value;
  value.SetCompilerType(compiler_type);

  const uint32_t type_flags = compiler_type.GetTypeInfo();
  // Integer return type.
  if (type_flags & eTypeIsInteger) {
    const size_t byte_size = compiler_type.GetByteSize(nullptr).getValueOr(0);
    auto raw_value = ReadRawValue(reg_ctx, byte_size);

    const bool is_signed = (type_flags & eTypeIsSigned) != 0;
    if (!SetSizedInteger(value.GetScalar(), raw_value, byte_size, is_signed))
      return ValueObjectSP();

    value.SetValueType(Value::eValueTypeScalar);
  }
  // Pointer return type.
  else if (type_flags & eTypeIsPointer) {
    auto reg_info_r0 = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
                                                LLDB_REGNUM_GENERIC_ARG1);
    value.GetScalar() = reg_ctx->ReadRegisterAsUnsigned(reg_info_r0, 0);

    value.SetValueType(Value::eValueTypeScalar);
  }
  // Floating point return type.
  else if (type_flags & eTypeIsFloat) {
    uint32_t float_count = 0;
    bool is_complex = false;

    if (compiler_type.IsFloatingPointType(float_count, is_complex) &&
        1 == float_count && !is_complex) {
      const size_t byte_size = compiler_type.GetByteSize(nullptr).getValueOr(0);
      auto raw_value = ReadRawValue(reg_ctx, byte_size);

      if (!SetSizedFloat(value.GetScalar(), raw_value, byte_size))
        return ValueObjectSP();
    }
  }
  // Unsupported return type.
  else
    return ValueObjectSP();

  return ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),
                                        value, ConstString(""));
}

ValueObjectSP ABISysV_arc::GetReturnValueObjectImpl(
    Thread &thread, CompilerType &return_compiler_type) const {
  ValueObjectSP return_valobj_sp;

  if (!return_compiler_type)
    return return_valobj_sp;

  ExecutionContext exe_ctx(thread.shared_from_this());
  return GetReturnValueObjectSimple(thread, return_compiler_type);
}

ValueObjectSP ABISysV_arc::GetReturnValueObjectImpl(Thread &thread,
                                                    llvm::Type &retType) const {
  auto reg_ctx = thread.GetRegisterContext();
  if (!reg_ctx)
    return ValueObjectSP();

  Value value;
  // Void return type.
  if (retType.isVoidTy()) {
    value.GetScalar() = 0;
  }
  // Integer return type.
  else if (retType.isIntegerTy()) {
    size_t byte_size = retType.getPrimitiveSizeInBits();
    if (1 != byte_size) // For boolian type.
      byte_size /= CHAR_BIT;

    auto raw_value = ReadRawValue(reg_ctx, byte_size);

    const bool is_signed = false; // IR Type doesn't provide this info.
    if (!SetSizedInteger(value.GetScalar(), raw_value, byte_size, is_signed))
      return ValueObjectSP();
  }
  // Pointer return type.
  else if (retType.isPointerTy()) {
    auto reg_info_r0 = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
                                                LLDB_REGNUM_GENERIC_ARG1);
    value.GetScalar() = reg_ctx->ReadRegisterAsUnsigned(reg_info_r0, 0);
    value.SetValueType(Value::eValueTypeScalar);
  }
  // Floating point return type.
  else if (retType.isFloatingPointTy()) {
    const size_t byte_size = retType.getPrimitiveSizeInBits() / CHAR_BIT;
    auto raw_value = ReadRawValue(reg_ctx, byte_size);

    if (!SetSizedFloat(value.GetScalar(), raw_value, byte_size))
      return ValueObjectSP();
  }
  // Unsupported return type.
  else
    return ValueObjectSP();

  return ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),
                                        value, ConstString(""));
}

bool ABISysV_arc::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
  unwind_plan.Clear();
  unwind_plan.SetRegisterKind(eRegisterKindDWARF);

  UnwindPlan::RowSP row(new UnwindPlan::Row);

  // Our Call Frame Address is the stack pointer value.
  row->GetCFAValue().SetIsRegisterPlusOffset(dwarf::sp, 0);

  // The previous PC is in the BLINK.
  row->SetRegisterLocationToRegister(dwarf::pc, dwarf::blink, true);
  unwind_plan.AppendRow(row);

  // All other registers are the same.
  unwind_plan.SetSourceName("arc at-func-entry default");
  unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);

  return true;
}

bool ABISysV_arc::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
  return false;
}

bool ABISysV_arc::RegisterIsVolatile(const RegisterInfo *reg_info) {
  if (nullptr == reg_info)
    return false;

  // Volatile registers are: r0..r12.
  uint32_t regnum = reg_info->kinds[eRegisterKindDWARF];
  if (regnum <= 12)
    return true;

  static const std::string ra_reg_name = "blink";
  return ra_reg_name == reg_info->name;
}

void ABISysV_arc::Initialize() {
  PluginManager::RegisterPlugin(GetPluginNameStatic(),
                                "System V ABI for ARC targets", CreateInstance);
}

void ABISysV_arc::Terminate() {
  PluginManager::UnregisterPlugin(CreateInstance);
}

ConstString ABISysV_arc::GetPluginNameStatic() {
  static ConstString g_name("sysv-arc");
  return g_name;
}

//------------------------------------------------------------------
// PluginInterface protocol
//------------------------------------------------------------------

ConstString ABISysV_arc::GetPluginName() {
  return GetPluginNameStatic();
}

uint32_t ABISysV_arc::GetPluginVersion() { return 1; }