[OpenACC] Create AST nodes for 'data' constructs
[llvm-project.git] / lldb / source / Plugins / ABI / AArch64 / ABIMacOSX_arm64.cpp
blobcb121c14048f08c7d883112e6789ec77bda3240b
1 //===-- ABIMacOSX_arm64.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 "ABIMacOSX_arm64.h"
11 #include <optional>
12 #include <vector>
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/TargetParser/Triple.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/PluginManager.h"
19 #include "lldb/Core/Value.h"
20 #include "lldb/Symbol/UnwindPlan.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/RegisterContext.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Target/Thread.h"
25 #include "lldb/Utility/ConstString.h"
26 #include "lldb/Utility/LLDBLog.h"
27 #include "lldb/Utility/Log.h"
28 #include "lldb/Utility/RegisterValue.h"
29 #include "lldb/Utility/Scalar.h"
30 #include "lldb/Utility/Status.h"
31 #include "lldb/ValueObject/ValueObjectConstResult.h"
33 #include "Utility/ARM64_DWARF_Registers.h"
35 using namespace lldb;
36 using namespace lldb_private;
38 static const char *pluginDesc = "Mac OS X ABI for arm64 targets";
40 size_t ABIMacOSX_arm64::GetRedZoneSize() const { return 128; }
42 // Static Functions
44 ABISP
45 ABIMacOSX_arm64::CreateInstance(ProcessSP process_sp, const ArchSpec &arch) {
46 const llvm::Triple::ArchType arch_type = arch.GetTriple().getArch();
47 const llvm::Triple::VendorType vendor_type = arch.GetTriple().getVendor();
49 if (vendor_type == llvm::Triple::Apple) {
50 if (arch_type == llvm::Triple::aarch64 ||
51 arch_type == llvm::Triple::aarch64_32) {
52 return ABISP(
53 new ABIMacOSX_arm64(std::move(process_sp), MakeMCRegisterInfo(arch)));
57 return ABISP();
60 bool ABIMacOSX_arm64::PrepareTrivialCall(
61 Thread &thread, lldb::addr_t sp, lldb::addr_t func_addr,
62 lldb::addr_t return_addr, llvm::ArrayRef<lldb::addr_t> args) const {
63 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
64 if (!reg_ctx)
65 return false;
67 Log *log = GetLog(LLDBLog::Expressions);
69 if (log) {
70 StreamString s;
71 s.Printf("ABIMacOSX_arm64::PrepareTrivialCall (tid = 0x%" PRIx64
72 ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
73 ", return_addr = 0x%" PRIx64,
74 thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
75 (uint64_t)return_addr);
77 for (size_t i = 0; i < args.size(); ++i)
78 s.Printf(", arg%d = 0x%" PRIx64, static_cast<int>(i + 1), args[i]);
79 s.PutCString(")");
80 log->PutString(s.GetString());
83 const uint32_t pc_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
84 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
85 const uint32_t sp_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
86 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
87 const uint32_t ra_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
88 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA);
90 // x0 - x7 contain first 8 simple args
91 if (args.size() > 8) // TODO handle more than 8 arguments
92 return false;
94 for (size_t i = 0; i < args.size(); ++i) {
95 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfo(
96 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + i);
97 LLDB_LOGF(log, "About to write arg%d (0x%" PRIx64 ") into %s",
98 static_cast<int>(i + 1), args[i], reg_info->name);
99 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i]))
100 return false;
103 // Set "lr" to the return address
104 if (!reg_ctx->WriteRegisterFromUnsigned(
105 reg_ctx->GetRegisterInfoAtIndex(ra_reg_num), return_addr))
106 return false;
108 // Set "sp" to the requested value
109 if (!reg_ctx->WriteRegisterFromUnsigned(
110 reg_ctx->GetRegisterInfoAtIndex(sp_reg_num), sp))
111 return false;
113 // Set "pc" to the address requested
114 if (!reg_ctx->WriteRegisterFromUnsigned(
115 reg_ctx->GetRegisterInfoAtIndex(pc_reg_num), func_addr))
116 return false;
118 return true;
121 bool ABIMacOSX_arm64::GetArgumentValues(Thread &thread,
122 ValueList &values) const {
123 uint32_t num_values = values.GetSize();
125 ExecutionContext exe_ctx(thread.shared_from_this());
127 // Extract the register context so we can read arguments from registers
129 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
131 if (!reg_ctx)
132 return false;
134 addr_t sp = 0;
136 for (uint32_t value_idx = 0; value_idx < num_values; ++value_idx) {
137 // We currently only support extracting values with Clang QualTypes. Do we
138 // care about others?
139 Value *value = values.GetValueAtIndex(value_idx);
141 if (!value)
142 return false;
144 CompilerType value_type = value->GetCompilerType();
145 std::optional<uint64_t> bit_size = value_type.GetBitSize(&thread);
146 if (!bit_size)
147 return false;
149 bool is_signed = false;
150 size_t bit_width = 0;
151 if (value_type.IsIntegerOrEnumerationType(is_signed)) {
152 bit_width = *bit_size;
153 } else if (value_type.IsPointerOrReferenceType()) {
154 bit_width = *bit_size;
155 } else {
156 // We only handle integer, pointer and reference types currently...
157 return false;
160 if (bit_width <= (exe_ctx.GetProcessRef().GetAddressByteSize() * 8)) {
161 if (value_idx < 8) {
162 // Arguments 1-6 are in x0-x5...
163 const RegisterInfo *reg_info = nullptr;
164 // Search by generic ID first, then fall back to by name
165 uint32_t arg_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
166 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + value_idx);
167 if (arg_reg_num != LLDB_INVALID_REGNUM) {
168 reg_info = reg_ctx->GetRegisterInfoAtIndex(arg_reg_num);
169 } else {
170 switch (value_idx) {
171 case 0:
172 reg_info = reg_ctx->GetRegisterInfoByName("x0");
173 break;
174 case 1:
175 reg_info = reg_ctx->GetRegisterInfoByName("x1");
176 break;
177 case 2:
178 reg_info = reg_ctx->GetRegisterInfoByName("x2");
179 break;
180 case 3:
181 reg_info = reg_ctx->GetRegisterInfoByName("x3");
182 break;
183 case 4:
184 reg_info = reg_ctx->GetRegisterInfoByName("x4");
185 break;
186 case 5:
187 reg_info = reg_ctx->GetRegisterInfoByName("x5");
188 break;
189 case 6:
190 reg_info = reg_ctx->GetRegisterInfoByName("x6");
191 break;
192 case 7:
193 reg_info = reg_ctx->GetRegisterInfoByName("x7");
194 break;
198 if (reg_info) {
199 RegisterValue reg_value;
201 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
202 if (is_signed)
203 reg_value.SignExtend(bit_width);
204 if (!reg_value.GetScalarValue(value->GetScalar()))
205 return false;
206 continue;
209 return false;
210 } else {
211 if (sp == 0) {
212 // Read the stack pointer if we already haven't read it
213 sp = reg_ctx->GetSP(0);
214 if (sp == 0)
215 return false;
218 // Arguments 5 on up are on the stack
219 const uint32_t arg_byte_size = (bit_width + (8 - 1)) / 8;
220 Status error;
221 if (!exe_ctx.GetProcessRef().ReadScalarIntegerFromMemory(
222 sp, arg_byte_size, is_signed, value->GetScalar(), error))
223 return false;
225 sp += arg_byte_size;
226 // Align up to the next 8 byte boundary if needed
227 if (sp % 8) {
228 sp >>= 3;
229 sp += 1;
230 sp <<= 3;
235 return true;
238 Status
239 ABIMacOSX_arm64::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
240 lldb::ValueObjectSP &new_value_sp) {
241 Status error;
242 if (!new_value_sp) {
243 error = Status::FromErrorString("Empty value object for return value.");
244 return error;
247 CompilerType return_value_type = new_value_sp->GetCompilerType();
248 if (!return_value_type) {
249 error = Status::FromErrorString("Null clang type for return value.");
250 return error;
253 Thread *thread = frame_sp->GetThread().get();
255 RegisterContext *reg_ctx = thread->GetRegisterContext().get();
257 if (reg_ctx) {
258 DataExtractor data;
259 Status data_error;
260 const uint64_t byte_size = new_value_sp->GetData(data, data_error);
261 if (data_error.Fail()) {
262 error = Status::FromErrorStringWithFormat(
263 "Couldn't convert return value to raw data: %s",
264 data_error.AsCString());
265 return error;
268 const uint32_t type_flags = return_value_type.GetTypeInfo(nullptr);
269 if (type_flags & eTypeIsScalar || type_flags & eTypeIsPointer) {
270 if (type_flags & eTypeIsInteger || type_flags & eTypeIsPointer) {
271 // Extract the register context so we can read arguments from registers
272 lldb::offset_t offset = 0;
273 if (byte_size <= 16) {
274 const RegisterInfo *x0_info = reg_ctx->GetRegisterInfoByName("x0", 0);
275 if (byte_size <= 8) {
276 uint64_t raw_value = data.GetMaxU64(&offset, byte_size);
278 if (!reg_ctx->WriteRegisterFromUnsigned(x0_info, raw_value))
279 error = Status::FromErrorString("failed to write register x0");
280 } else {
281 uint64_t raw_value = data.GetMaxU64(&offset, 8);
283 if (reg_ctx->WriteRegisterFromUnsigned(x0_info, raw_value)) {
284 const RegisterInfo *x1_info =
285 reg_ctx->GetRegisterInfoByName("x1", 0);
286 raw_value = data.GetMaxU64(&offset, byte_size - offset);
288 if (!reg_ctx->WriteRegisterFromUnsigned(x1_info, raw_value))
289 error = Status::FromErrorString("failed to write register x1");
292 } else {
293 error = Status::FromErrorString(
294 "We don't support returning longer than 128 bit "
295 "integer values at present.");
297 } else if (type_flags & eTypeIsFloat) {
298 if (type_flags & eTypeIsComplex) {
299 // Don't handle complex yet.
300 error = Status::FromErrorString(
301 "returning complex float values are not supported");
302 } else {
303 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName("v0", 0);
305 if (v0_info) {
306 if (byte_size <= 16) {
307 RegisterValue reg_value;
308 error = reg_value.SetValueFromData(*v0_info, data, 0, true);
309 if (error.Success())
310 if (!reg_ctx->WriteRegister(v0_info, reg_value))
311 error =
312 Status::FromErrorString("failed to write register v0");
313 } else {
314 error = Status::FromErrorString(
315 "returning float values longer than 128 "
316 "bits are not supported");
318 } else
319 error = Status::FromErrorString(
320 "v0 register is not available on this target");
323 } else if (type_flags & eTypeIsVector) {
324 if (byte_size > 0) {
325 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName("v0", 0);
327 if (v0_info) {
328 if (byte_size <= v0_info->byte_size) {
329 RegisterValue reg_value;
330 error = reg_value.SetValueFromData(*v0_info, data, 0, true);
331 if (error.Success()) {
332 if (!reg_ctx->WriteRegister(v0_info, reg_value))
333 error = Status::FromErrorString("failed to write register v0");
339 } else {
340 error = Status::FromErrorString("no registers are available");
343 return error;
346 bool ABIMacOSX_arm64::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
347 unwind_plan.Clear();
348 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
350 uint32_t lr_reg_num = arm64_dwarf::lr;
351 uint32_t sp_reg_num = arm64_dwarf::sp;
352 uint32_t pc_reg_num = arm64_dwarf::pc;
354 UnwindPlan::RowSP row(new UnwindPlan::Row);
356 // Our previous Call Frame Address is the stack pointer
357 row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 0);
359 // Our previous PC is in the LR
360 row->SetRegisterLocationToRegister(pc_reg_num, lr_reg_num, true);
362 unwind_plan.AppendRow(row);
364 // All other registers are the same.
366 unwind_plan.SetSourceName("arm64 at-func-entry default");
367 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
369 return true;
372 bool ABIMacOSX_arm64::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
373 unwind_plan.Clear();
374 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
376 uint32_t fp_reg_num = arm64_dwarf::fp;
377 uint32_t pc_reg_num = arm64_dwarf::pc;
379 UnwindPlan::RowSP row(new UnwindPlan::Row);
380 const int32_t ptr_size = 8;
382 row->GetCFAValue().SetIsRegisterPlusOffset(fp_reg_num, 2 * ptr_size);
383 row->SetOffset(0);
384 row->SetUnspecifiedRegistersAreUndefined(true);
386 row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, ptr_size * -2, true);
387 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * -1, true);
389 unwind_plan.AppendRow(row);
390 unwind_plan.SetSourceName("arm64-apple-darwin default unwind plan");
391 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
392 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
393 unwind_plan.SetUnwindPlanForSignalTrap(eLazyBoolNo);
394 return true;
397 // AAPCS64 (Procedure Call Standard for the ARM 64-bit Architecture) says
398 // registers x19 through x28 and sp are callee preserved. v8-v15 are non-
399 // volatile (and specifically only the lower 8 bytes of these regs), the rest
400 // of the fp/SIMD registers are volatile.
402 // v. https://github.com/ARM-software/abi-aa/blob/main/aapcs64/
404 // We treat x29 as callee preserved also, else the unwinder won't try to
405 // retrieve fp saves.
407 bool ABIMacOSX_arm64::RegisterIsVolatile(const RegisterInfo *reg_info) {
408 if (reg_info) {
409 const char *name = reg_info->name;
411 // Sometimes we'll be called with the "alternate" name for these registers;
412 // recognize them as non-volatile.
414 if (name[0] == 'p' && name[1] == 'c') // pc
415 return false;
416 if (name[0] == 'f' && name[1] == 'p') // fp
417 return false;
418 if (name[0] == 's' && name[1] == 'p') // sp
419 return false;
420 if (name[0] == 'l' && name[1] == 'r') // lr
421 return false;
423 if (name[0] == 'x') {
424 // Volatile registers: x0-x18, x30 (lr)
425 // Return false for the non-volatile gpr regs, true for everything else
426 switch (name[1]) {
427 case '1':
428 switch (name[2]) {
429 case '9':
430 return false; // x19 is non-volatile
431 default:
432 return true;
434 break;
435 case '2':
436 switch (name[2]) {
437 case '0':
438 case '1':
439 case '2':
440 case '3':
441 case '4':
442 case '5':
443 case '6':
444 case '7':
445 case '8':
446 return false; // x20 - 28 are non-volatile
447 case '9':
448 return false; // x29 aka fp treat as non-volatile on Darwin
449 default:
450 return true;
452 case '3': // x30 aka lr treat as non-volatile
453 if (name[2] == '0')
454 return false;
455 break;
456 default:
457 return true;
459 } else if (name[0] == 'v' || name[0] == 's' || name[0] == 'd') {
460 // Volatile registers: v0-7, v16-v31
461 // Return false for non-volatile fp/SIMD regs, true for everything else
462 switch (name[1]) {
463 case '8':
464 case '9':
465 return false; // v8-v9 are non-volatile
466 case '1':
467 switch (name[2]) {
468 case '0':
469 case '1':
470 case '2':
471 case '3':
472 case '4':
473 case '5':
474 return false; // v10-v15 are non-volatile
475 default:
476 return true;
478 default:
479 return true;
483 return true;
486 static bool LoadValueFromConsecutiveGPRRegisters(
487 ExecutionContext &exe_ctx, RegisterContext *reg_ctx,
488 const CompilerType &value_type,
489 bool is_return_value, // false => parameter, true => return value
490 uint32_t &NGRN, // NGRN (see ABI documentation)
491 uint32_t &NSRN, // NSRN (see ABI documentation)
492 DataExtractor &data) {
493 std::optional<uint64_t> byte_size =
494 value_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
495 if (!byte_size || *byte_size == 0)
496 return false;
498 std::unique_ptr<DataBufferHeap> heap_data_up(
499 new DataBufferHeap(*byte_size, 0));
500 const ByteOrder byte_order = exe_ctx.GetProcessRef().GetByteOrder();
501 Status error;
503 CompilerType base_type;
504 const uint32_t homogeneous_count =
505 value_type.IsHomogeneousAggregate(&base_type);
506 if (homogeneous_count > 0 && homogeneous_count <= 8) {
507 // Make sure we have enough registers
508 if (NSRN < 8 && (8 - NSRN) >= homogeneous_count) {
509 if (!base_type)
510 return false;
511 std::optional<uint64_t> base_byte_size =
512 base_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
513 if (!base_byte_size)
514 return false;
515 uint32_t data_offset = 0;
517 for (uint32_t i = 0; i < homogeneous_count; ++i) {
518 char v_name[8];
519 ::snprintf(v_name, sizeof(v_name), "v%u", NSRN);
520 const RegisterInfo *reg_info =
521 reg_ctx->GetRegisterInfoByName(v_name, 0);
522 if (reg_info == nullptr)
523 return false;
525 if (*base_byte_size > reg_info->byte_size)
526 return false;
528 RegisterValue reg_value;
530 if (!reg_ctx->ReadRegister(reg_info, reg_value))
531 return false;
533 // Make sure we have enough room in "heap_data_up"
534 if ((data_offset + *base_byte_size) <= heap_data_up->GetByteSize()) {
535 const size_t bytes_copied = reg_value.GetAsMemoryData(
536 *reg_info, heap_data_up->GetBytes() + data_offset,
537 *base_byte_size, byte_order, error);
538 if (bytes_copied != *base_byte_size)
539 return false;
540 data_offset += bytes_copied;
541 ++NSRN;
542 } else
543 return false;
545 data.SetByteOrder(byte_order);
546 data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize());
547 data.SetData(DataBufferSP(heap_data_up.release()));
548 return true;
552 const size_t max_reg_byte_size = 16;
553 if (*byte_size <= max_reg_byte_size) {
554 size_t bytes_left = *byte_size;
555 uint32_t data_offset = 0;
556 while (data_offset < *byte_size) {
557 if (NGRN >= 8)
558 return false;
560 uint32_t reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
561 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + NGRN);
562 if (reg_num == LLDB_INVALID_REGNUM)
563 return false;
565 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
566 if (reg_info == nullptr)
567 return false;
569 RegisterValue reg_value;
571 if (!reg_ctx->ReadRegister(reg_info, reg_value))
572 return false;
574 const size_t curr_byte_size = std::min<size_t>(8, bytes_left);
575 const size_t bytes_copied = reg_value.GetAsMemoryData(
576 *reg_info, heap_data_up->GetBytes() + data_offset, curr_byte_size,
577 byte_order, error);
578 if (bytes_copied == 0)
579 return false;
580 if (bytes_copied >= bytes_left)
581 break;
582 data_offset += bytes_copied;
583 bytes_left -= bytes_copied;
584 ++NGRN;
586 } else {
587 const RegisterInfo *reg_info = nullptr;
588 if (is_return_value) {
589 // The Darwin arm64 ABI doesn't write the return location back to x8
590 // before returning from the function the way the x86_64 ABI does. So
591 // we can't reconstruct stack based returns on exit from the function:
592 return false;
593 } else {
594 // We are assuming we are stopped at the first instruction in a function
595 // and that the ABI is being respected so all parameters appear where
596 // they should be (functions with no external linkage can legally violate
597 // the ABI).
598 if (NGRN >= 8)
599 return false;
601 uint32_t reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(
602 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + NGRN);
603 if (reg_num == LLDB_INVALID_REGNUM)
604 return false;
605 reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
606 if (reg_info == nullptr)
607 return false;
608 ++NGRN;
611 const lldb::addr_t value_addr =
612 reg_ctx->ReadRegisterAsUnsigned(reg_info, LLDB_INVALID_ADDRESS);
614 if (value_addr == LLDB_INVALID_ADDRESS)
615 return false;
617 if (exe_ctx.GetProcessRef().ReadMemory(
618 value_addr, heap_data_up->GetBytes(), heap_data_up->GetByteSize(),
619 error) != heap_data_up->GetByteSize()) {
620 return false;
624 data.SetByteOrder(byte_order);
625 data.SetAddressByteSize(exe_ctx.GetProcessRef().GetAddressByteSize());
626 data.SetData(DataBufferSP(heap_data_up.release()));
627 return true;
630 ValueObjectSP ABIMacOSX_arm64::GetReturnValueObjectImpl(
631 Thread &thread, CompilerType &return_compiler_type) const {
632 ValueObjectSP return_valobj_sp;
633 Value value;
635 ExecutionContext exe_ctx(thread.shared_from_this());
636 if (exe_ctx.GetTargetPtr() == nullptr || exe_ctx.GetProcessPtr() == nullptr)
637 return return_valobj_sp;
639 // value.SetContext (Value::eContextTypeClangType, return_compiler_type);
640 value.SetCompilerType(return_compiler_type);
642 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
643 if (!reg_ctx)
644 return return_valobj_sp;
646 std::optional<uint64_t> byte_size = return_compiler_type.GetByteSize(&thread);
647 if (!byte_size)
648 return return_valobj_sp;
650 const uint32_t type_flags = return_compiler_type.GetTypeInfo(nullptr);
651 if (type_flags & eTypeIsScalar || type_flags & eTypeIsPointer) {
652 value.SetValueType(Value::ValueType::Scalar);
654 bool success = false;
655 if (type_flags & eTypeIsInteger || type_flags & eTypeIsPointer) {
656 // Extract the register context so we can read arguments from registers
657 if (*byte_size <= 8) {
658 const RegisterInfo *x0_reg_info =
659 reg_ctx->GetRegisterInfoByName("x0", 0);
660 if (x0_reg_info) {
661 uint64_t raw_value =
662 thread.GetRegisterContext()->ReadRegisterAsUnsigned(x0_reg_info,
664 const bool is_signed = (type_flags & eTypeIsSigned) != 0;
665 switch (*byte_size) {
666 default:
667 break;
668 case 16: // uint128_t
669 // In register x0 and x1
671 const RegisterInfo *x1_reg_info =
672 reg_ctx->GetRegisterInfoByName("x1", 0);
674 if (x1_reg_info) {
675 if (*byte_size <=
676 x0_reg_info->byte_size + x1_reg_info->byte_size) {
677 std::unique_ptr<DataBufferHeap> heap_data_up(
678 new DataBufferHeap(*byte_size, 0));
679 const ByteOrder byte_order =
680 exe_ctx.GetProcessRef().GetByteOrder();
681 RegisterValue x0_reg_value;
682 RegisterValue x1_reg_value;
683 if (reg_ctx->ReadRegister(x0_reg_info, x0_reg_value) &&
684 reg_ctx->ReadRegister(x1_reg_info, x1_reg_value)) {
685 Status error;
686 if (x0_reg_value.GetAsMemoryData(
687 *x0_reg_info, heap_data_up->GetBytes() + 0, 8,
688 byte_order, error) &&
689 x1_reg_value.GetAsMemoryData(
690 *x1_reg_info, heap_data_up->GetBytes() + 8, 8,
691 byte_order, error)) {
692 DataExtractor data(
693 DataBufferSP(heap_data_up.release()), byte_order,
694 exe_ctx.GetProcessRef().GetAddressByteSize());
696 return_valobj_sp = ValueObjectConstResult::Create(
697 &thread, return_compiler_type, ConstString(""), data);
698 return return_valobj_sp;
704 break;
705 case sizeof(uint64_t):
706 if (is_signed)
707 value.GetScalar() = (int64_t)(raw_value);
708 else
709 value.GetScalar() = (uint64_t)(raw_value);
710 success = true;
711 break;
713 case sizeof(uint32_t):
714 if (is_signed)
715 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
716 else
717 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
718 success = true;
719 break;
721 case sizeof(uint16_t):
722 if (is_signed)
723 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
724 else
725 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
726 success = true;
727 break;
729 case sizeof(uint8_t):
730 if (is_signed)
731 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
732 else
733 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
734 success = true;
735 break;
739 } else if (type_flags & eTypeIsFloat) {
740 if (type_flags & eTypeIsComplex) {
741 // Don't handle complex yet.
742 } else {
743 if (*byte_size <= sizeof(long double)) {
744 const RegisterInfo *v0_reg_info =
745 reg_ctx->GetRegisterInfoByName("v0", 0);
746 RegisterValue v0_value;
747 if (reg_ctx->ReadRegister(v0_reg_info, v0_value)) {
748 DataExtractor data;
749 if (v0_value.GetData(data)) {
750 lldb::offset_t offset = 0;
751 if (*byte_size == sizeof(float)) {
752 value.GetScalar() = data.GetFloat(&offset);
753 success = true;
754 } else if (*byte_size == sizeof(double)) {
755 value.GetScalar() = data.GetDouble(&offset);
756 success = true;
757 } else if (*byte_size == sizeof(long double)) {
758 value.GetScalar() = data.GetLongDouble(&offset);
759 success = true;
767 if (success)
768 return_valobj_sp = ValueObjectConstResult::Create(
769 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
770 } else if (type_flags & eTypeIsVector) {
771 if (*byte_size > 0) {
773 const RegisterInfo *v0_info = reg_ctx->GetRegisterInfoByName("v0", 0);
775 if (v0_info) {
776 if (*byte_size <= v0_info->byte_size) {
777 std::unique_ptr<DataBufferHeap> heap_data_up(
778 new DataBufferHeap(*byte_size, 0));
779 const ByteOrder byte_order = exe_ctx.GetProcessRef().GetByteOrder();
780 RegisterValue reg_value;
781 if (reg_ctx->ReadRegister(v0_info, reg_value)) {
782 Status error;
783 if (reg_value.GetAsMemoryData(*v0_info, heap_data_up->GetBytes(),
784 heap_data_up->GetByteSize(),
785 byte_order, error)) {
786 DataExtractor data(DataBufferSP(heap_data_up.release()),
787 byte_order,
788 exe_ctx.GetProcessRef().GetAddressByteSize());
789 return_valobj_sp = ValueObjectConstResult::Create(
790 &thread, return_compiler_type, ConstString(""), data);
796 } else if (type_flags & eTypeIsStructUnion || type_flags & eTypeIsClass) {
797 DataExtractor data;
799 uint32_t NGRN = 0; // Search ABI docs for NGRN
800 uint32_t NSRN = 0; // Search ABI docs for NSRN
801 const bool is_return_value = true;
802 if (LoadValueFromConsecutiveGPRRegisters(
803 exe_ctx, reg_ctx, return_compiler_type, is_return_value, NGRN, NSRN,
804 data)) {
805 return_valobj_sp = ValueObjectConstResult::Create(
806 &thread, return_compiler_type, ConstString(""), data);
809 return return_valobj_sp;
812 addr_t ABIMacOSX_arm64::FixCodeAddress(addr_t pc) {
813 addr_t pac_sign_extension = 0x0080000000000000ULL;
814 addr_t tbi_mask = 0xff80000000000000ULL;
815 addr_t mask = 0;
817 if (ProcessSP process_sp = GetProcessSP()) {
818 mask = process_sp->GetCodeAddressMask();
819 if (pc & pac_sign_extension) {
820 addr_t highmem_mask = process_sp->GetHighmemCodeAddressMask();
821 if (highmem_mask != LLDB_INVALID_ADDRESS_MASK)
822 mask = highmem_mask;
825 if (mask == LLDB_INVALID_ADDRESS_MASK)
826 mask = tbi_mask;
828 return (pc & pac_sign_extension) ? pc | mask : pc & (~mask);
831 addr_t ABIMacOSX_arm64::FixDataAddress(addr_t pc) {
832 addr_t pac_sign_extension = 0x0080000000000000ULL;
833 addr_t tbi_mask = 0xff80000000000000ULL;
834 addr_t mask = 0;
836 if (ProcessSP process_sp = GetProcessSP()) {
837 mask = process_sp->GetDataAddressMask();
838 if (pc & pac_sign_extension) {
839 addr_t highmem_mask = process_sp->GetHighmemDataAddressMask();
840 if (highmem_mask != LLDB_INVALID_ADDRESS_MASK)
841 mask = highmem_mask;
844 if (mask == LLDB_INVALID_ADDRESS_MASK)
845 mask = tbi_mask;
847 return (pc & pac_sign_extension) ? pc | mask : pc & (~mask);
850 void ABIMacOSX_arm64::Initialize() {
851 PluginManager::RegisterPlugin(GetPluginNameStatic(), pluginDesc,
852 CreateInstance);
855 void ABIMacOSX_arm64::Terminate() {
856 PluginManager::UnregisterPlugin(CreateInstance);