1 //===-- ABISysV_ppc.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_ppc.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/Triple.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/PluginManager.h"
16 #include "lldb/Core/Value.h"
17 #include "lldb/Core/ValueObjectConstResult.h"
18 #include "lldb/Core/ValueObjectMemory.h"
19 #include "lldb/Core/ValueObjectRegister.h"
20 #include "lldb/Symbol/UnwindPlan.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/RegisterContext.h"
23 #include "lldb/Target/StackFrame.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/Thread.h"
26 #include "lldb/Utility/ConstString.h"
27 #include "lldb/Utility/DataExtractor.h"
28 #include "lldb/Utility/Log.h"
29 #include "lldb/Utility/RegisterValue.h"
30 #include "lldb/Utility/Status.h"
33 using namespace lldb_private;
109 // Note that the size and offset will be updated by platform-specific classes.
110 #define DEFINE_GPR(reg, alt, kind1, kind2, kind3, kind4) \
112 #reg, alt, 8, 0, eEncodingUint, eFormatHex, {kind1, kind2, kind3, kind4 }, \
113 nullptr, nullptr, nullptr, 0 \
116 static const RegisterInfo g_register_infos[] = {
117 // General purpose registers. eh_frame, DWARF,
118 // Generic, Process Plugin
119 DEFINE_GPR(r0, nullptr, dwarf_r0, dwarf_r0, LLDB_INVALID_REGNUM,
120 LLDB_INVALID_REGNUM),
121 DEFINE_GPR(r1, "sp", dwarf_r1, dwarf_r1, LLDB_REGNUM_GENERIC_SP,
122 LLDB_INVALID_REGNUM),
123 DEFINE_GPR(r2, nullptr, dwarf_r2, dwarf_r2, LLDB_INVALID_REGNUM,
124 LLDB_INVALID_REGNUM),
125 DEFINE_GPR(r3, "arg1", dwarf_r3, dwarf_r3, LLDB_REGNUM_GENERIC_ARG1,
126 LLDB_INVALID_REGNUM),
127 DEFINE_GPR(r4, "arg2", dwarf_r4, dwarf_r4, LLDB_REGNUM_GENERIC_ARG2,
128 LLDB_INVALID_REGNUM),
129 DEFINE_GPR(r5, "arg3", dwarf_r5, dwarf_r5, LLDB_REGNUM_GENERIC_ARG3,
130 LLDB_INVALID_REGNUM),
131 DEFINE_GPR(r6, "arg4", dwarf_r6, dwarf_r6, LLDB_REGNUM_GENERIC_ARG4,
132 LLDB_INVALID_REGNUM),
133 DEFINE_GPR(r7, "arg5", dwarf_r7, dwarf_r7, LLDB_REGNUM_GENERIC_ARG5,
134 LLDB_INVALID_REGNUM),
135 DEFINE_GPR(r8, "arg6", dwarf_r8, dwarf_r8, LLDB_REGNUM_GENERIC_ARG6,
136 LLDB_INVALID_REGNUM),
137 DEFINE_GPR(r9, "arg7", dwarf_r9, dwarf_r9, LLDB_REGNUM_GENERIC_ARG7,
138 LLDB_INVALID_REGNUM),
139 DEFINE_GPR(r10, "arg8", dwarf_r10, dwarf_r10, LLDB_REGNUM_GENERIC_ARG8,
140 LLDB_INVALID_REGNUM),
141 DEFINE_GPR(r11, nullptr, dwarf_r11, dwarf_r11, LLDB_INVALID_REGNUM,
142 LLDB_INVALID_REGNUM),
143 DEFINE_GPR(r12, nullptr, dwarf_r12, dwarf_r12, LLDB_INVALID_REGNUM,
144 LLDB_INVALID_REGNUM),
145 DEFINE_GPR(r13, nullptr, dwarf_r13, dwarf_r13, LLDB_INVALID_REGNUM,
146 LLDB_INVALID_REGNUM),
147 DEFINE_GPR(r14, nullptr, dwarf_r14, dwarf_r14, LLDB_INVALID_REGNUM,
148 LLDB_INVALID_REGNUM),
149 DEFINE_GPR(r15, nullptr, dwarf_r15, dwarf_r15, LLDB_INVALID_REGNUM,
150 LLDB_INVALID_REGNUM),
151 DEFINE_GPR(r16, nullptr, dwarf_r16, dwarf_r16, LLDB_INVALID_REGNUM,
152 LLDB_INVALID_REGNUM),
153 DEFINE_GPR(r17, nullptr, dwarf_r17, dwarf_r17, LLDB_INVALID_REGNUM,
154 LLDB_INVALID_REGNUM),
155 DEFINE_GPR(r18, nullptr, dwarf_r18, dwarf_r18, LLDB_INVALID_REGNUM,
156 LLDB_INVALID_REGNUM),
157 DEFINE_GPR(r19, nullptr, dwarf_r19, dwarf_r19, LLDB_INVALID_REGNUM,
158 LLDB_INVALID_REGNUM),
159 DEFINE_GPR(r20, nullptr, dwarf_r20, dwarf_r20, LLDB_INVALID_REGNUM,
160 LLDB_INVALID_REGNUM),
161 DEFINE_GPR(r21, nullptr, dwarf_r21, dwarf_r21, LLDB_INVALID_REGNUM,
162 LLDB_INVALID_REGNUM),
163 DEFINE_GPR(r22, nullptr, dwarf_r22, dwarf_r22, LLDB_INVALID_REGNUM,
164 LLDB_INVALID_REGNUM),
165 DEFINE_GPR(r23, nullptr, dwarf_r23, dwarf_r23, LLDB_INVALID_REGNUM,
166 LLDB_INVALID_REGNUM),
167 DEFINE_GPR(r24, nullptr, dwarf_r24, dwarf_r24, LLDB_INVALID_REGNUM,
168 LLDB_INVALID_REGNUM),
169 DEFINE_GPR(r25, nullptr, dwarf_r25, dwarf_r25, LLDB_INVALID_REGNUM,
170 LLDB_INVALID_REGNUM),
171 DEFINE_GPR(r26, nullptr, dwarf_r26, dwarf_r26, LLDB_INVALID_REGNUM,
172 LLDB_INVALID_REGNUM),
173 DEFINE_GPR(r27, nullptr, dwarf_r27, dwarf_r27, LLDB_INVALID_REGNUM,
174 LLDB_INVALID_REGNUM),
175 DEFINE_GPR(r28, nullptr, dwarf_r28, dwarf_r28, LLDB_INVALID_REGNUM,
176 LLDB_INVALID_REGNUM),
177 DEFINE_GPR(r29, nullptr, dwarf_r29, dwarf_r29, LLDB_INVALID_REGNUM,
178 LLDB_INVALID_REGNUM),
179 DEFINE_GPR(r30, nullptr, dwarf_r30, dwarf_r30, LLDB_INVALID_REGNUM,
180 LLDB_INVALID_REGNUM),
181 DEFINE_GPR(r31, nullptr, dwarf_r31, dwarf_r31, LLDB_INVALID_REGNUM,
182 LLDB_INVALID_REGNUM),
183 DEFINE_GPR(lr, "lr", dwarf_lr, dwarf_lr, LLDB_REGNUM_GENERIC_RA,
184 LLDB_INVALID_REGNUM),
185 DEFINE_GPR(cr, "cr", dwarf_cr, dwarf_cr, LLDB_REGNUM_GENERIC_FLAGS,
186 LLDB_INVALID_REGNUM),
187 DEFINE_GPR(xer, "xer", dwarf_xer, dwarf_xer, LLDB_INVALID_REGNUM,
188 LLDB_INVALID_REGNUM),
189 DEFINE_GPR(ctr, "ctr", dwarf_ctr, dwarf_ctr, LLDB_INVALID_REGNUM,
190 LLDB_INVALID_REGNUM),
191 DEFINE_GPR(pc, "pc", dwarf_pc, dwarf_pc, LLDB_REGNUM_GENERIC_PC,
192 LLDB_INVALID_REGNUM),
199 {dwarf_cfa, dwarf_cfa, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
205 static const uint32_t k_num_register_infos =
206 llvm::array_lengthof(g_register_infos);
208 const lldb_private::RegisterInfo *
209 ABISysV_ppc::GetRegisterInfoArray(uint32_t &count) {
210 count = k_num_register_infos;
211 return g_register_infos;
214 size_t ABISysV_ppc::GetRedZoneSize() const { return 224; }
219 ABISysV_ppc::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) {
220 if (arch.GetTriple().getArch() == llvm::Triple::ppc) {
221 return ABISP(new ABISysV_ppc(process_sp));
226 bool ABISysV_ppc::PrepareTrivialCall(Thread &thread, addr_t sp,
227 addr_t func_addr, addr_t return_addr,
228 llvm::ArrayRef<addr_t> args) const {
229 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
233 s.Printf("ABISysV_ppc::PrepareTrivialCall (tid = 0x%" PRIx64
234 ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
235 ", return_addr = 0x%" PRIx64,
236 thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
237 (uint64_t)return_addr);
239 for (size_t i = 0; i < args.size(); ++i)
240 s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
243 log->PutString(s.GetString());
246 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
250 const RegisterInfo *reg_info = nullptr;
252 if (args.size() > 8) // TODO handle more than 8 arguments
255 for (size_t i = 0; i < args.size(); ++i) {
256 reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
257 LLDB_REGNUM_GENERIC_ARG1 + i);
259 log->Printf("About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s",
260 static_cast<uint64_t>(i + 1), args[i], reg_info->name);
261 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i]))
265 // First, align the SP
268 log->Printf("16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,
269 (uint64_t)sp, (uint64_t)(sp & ~0xfull));
271 sp &= ~(0xfull); // 16-byte alignment
276 const RegisterInfo *pc_reg_info =
277 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
278 const RegisterInfo *sp_reg_info =
279 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
280 ProcessSP process_sp(thread.GetProcess());
282 RegisterValue reg_value;
285 log->Printf("Pushing the return address onto the stack: 0x%" PRIx64
287 (uint64_t)sp, (uint64_t)return_addr);
289 // Save return address onto the stack
290 if (!process_sp->WritePointerToMemory(sp, return_addr, error))
293 // %r1 is set to the actual stack value.
296 log->Printf("Writing SP: 0x%" PRIx64, (uint64_t)sp);
298 if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))
301 // %pc is set to the address of the called function.
304 log->Printf("Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
306 if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))
312 static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
313 bool is_signed, Thread &thread,
314 uint32_t *argument_register_ids,
315 unsigned int ¤t_argument_register,
316 addr_t ¤t_stack_argument) {
318 return false; // Scalar can't hold large integer arguments
320 if (current_argument_register < 6) {
321 scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
322 argument_register_ids[current_argument_register], 0);
323 current_argument_register++;
325 scalar.SignExtend(bit_width);
327 uint32_t byte_size = (bit_width + (8 - 1)) / 8;
329 if (thread.GetProcess()->ReadScalarIntegerFromMemory(
330 current_stack_argument, byte_size, is_signed, scalar, error)) {
331 current_stack_argument += byte_size;
339 bool ABISysV_ppc::GetArgumentValues(Thread &thread, ValueList &values) const {
340 unsigned int num_values = values.GetSize();
341 unsigned int value_index;
343 // Extract the register context so we can read arguments from registers
345 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
350 // Get the pointer to the first stack argument so we have a place to start
353 addr_t sp = reg_ctx->GetSP(0);
358 addr_t current_stack_argument = sp + 48; // jump over return address
360 uint32_t argument_register_ids[8];
362 argument_register_ids[0] =
363 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1)
364 ->kinds[eRegisterKindLLDB];
365 argument_register_ids[1] =
366 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2)
367 ->kinds[eRegisterKindLLDB];
368 argument_register_ids[2] =
369 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3)
370 ->kinds[eRegisterKindLLDB];
371 argument_register_ids[3] =
372 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4)
373 ->kinds[eRegisterKindLLDB];
374 argument_register_ids[4] =
375 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG5)
376 ->kinds[eRegisterKindLLDB];
377 argument_register_ids[5] =
378 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG6)
379 ->kinds[eRegisterKindLLDB];
380 argument_register_ids[6] =
381 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG7)
382 ->kinds[eRegisterKindLLDB];
383 argument_register_ids[7] =
384 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG8)
385 ->kinds[eRegisterKindLLDB];
387 unsigned int current_argument_register = 0;
389 for (value_index = 0; value_index < num_values; ++value_index) {
390 Value *value = values.GetValueAtIndex(value_index);
395 // We currently only support extracting values with Clang QualTypes. Do we
396 // care about others?
397 CompilerType compiler_type = value->GetCompilerType();
398 llvm::Optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread);
402 if (compiler_type.IsIntegerOrEnumerationType(is_signed))
403 ReadIntegerArgument(value->GetScalar(), *bit_size, is_signed, thread,
404 argument_register_ids, current_argument_register,
405 current_stack_argument);
406 else if (compiler_type.IsPointerType())
407 ReadIntegerArgument(value->GetScalar(), *bit_size, false, thread,
408 argument_register_ids, current_argument_register,
409 current_stack_argument);
415 Status ABISysV_ppc::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
416 lldb::ValueObjectSP &new_value_sp) {
419 error.SetErrorString("Empty value object for return value.");
423 CompilerType compiler_type = new_value_sp->GetCompilerType();
424 if (!compiler_type) {
425 error.SetErrorString("Null clang type for return value.");
429 Thread *thread = frame_sp->GetThread().get();
435 RegisterContext *reg_ctx = thread->GetRegisterContext().get();
437 bool set_it_simple = false;
438 if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
439 compiler_type.IsPointerType()) {
440 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("r3", 0);
444 size_t num_bytes = new_value_sp->GetData(data, data_error);
445 if (data_error.Fail()) {
446 error.SetErrorStringWithFormat(
447 "Couldn't convert return value to raw data: %s",
448 data_error.AsCString());
451 lldb::offset_t offset = 0;
452 if (num_bytes <= 8) {
453 uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
455 if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))
456 set_it_simple = true;
458 error.SetErrorString("We don't support returning longer than 64 bit "
459 "integer values at present.");
461 } else if (compiler_type.IsFloatingPointType(count, is_complex)) {
463 error.SetErrorString(
464 "We don't support returning complex values at present");
466 llvm::Optional<uint64_t> bit_width =
467 compiler_type.GetBitSize(frame_sp.get());
469 error.SetErrorString("can't get type size");
472 if (*bit_width <= 64) {
475 size_t num_bytes = new_value_sp->GetData(data, data_error);
476 if (data_error.Fail()) {
477 error.SetErrorStringWithFormat(
478 "Couldn't convert return value to raw data: %s",
479 data_error.AsCString());
483 unsigned char buffer[16];
484 ByteOrder byte_order = data.GetByteOrder();
486 data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order);
487 set_it_simple = true;
489 // FIXME - don't know how to do 80 bit long doubles yet.
490 error.SetErrorString(
491 "We don't support returning float values > 64 bits at present");
496 if (!set_it_simple) {
497 // Okay we've got a structure or something that doesn't fit in a simple
498 // register. We should figure out where it really goes, but we don't
500 error.SetErrorString("We only support setting simple integer and float "
501 "return types at present.");
507 ValueObjectSP ABISysV_ppc::GetReturnValueObjectSimple(
508 Thread &thread, CompilerType &return_compiler_type) const {
509 ValueObjectSP return_valobj_sp;
512 if (!return_compiler_type)
513 return return_valobj_sp;
515 // value.SetContext (Value::eContextTypeClangType, return_value_type);
516 value.SetCompilerType(return_compiler_type);
518 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
520 return return_valobj_sp;
522 const uint32_t type_flags = return_compiler_type.GetTypeInfo();
523 if (type_flags & eTypeIsScalar) {
524 value.SetValueType(Value::eValueTypeScalar);
526 bool success = false;
527 if (type_flags & eTypeIsInteger) {
528 // Extract the register context so we can read arguments from registers
530 llvm::Optional<uint64_t> byte_size =
531 return_compiler_type.GetByteSize(nullptr);
533 return return_valobj_sp;
534 uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
535 reg_ctx->GetRegisterInfoByName("r3", 0), 0);
536 const bool is_signed = (type_flags & eTypeIsSigned) != 0;
537 switch (*byte_size) {
541 case sizeof(uint64_t):
543 value.GetScalar() = (int64_t)(raw_value);
545 value.GetScalar() = (uint64_t)(raw_value);
549 case sizeof(uint32_t):
551 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
553 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
557 case sizeof(uint16_t):
559 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
561 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
565 case sizeof(uint8_t):
567 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
569 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
573 } else if (type_flags & eTypeIsFloat) {
574 if (type_flags & eTypeIsComplex) {
575 // Don't handle complex yet.
577 llvm::Optional<uint64_t> byte_size =
578 return_compiler_type.GetByteSize(nullptr);
579 if (byte_size && *byte_size <= sizeof(long double)) {
580 const RegisterInfo *f1_info = reg_ctx->GetRegisterInfoByName("f1", 0);
581 RegisterValue f1_value;
582 if (reg_ctx->ReadRegister(f1_info, f1_value)) {
584 if (f1_value.GetData(data)) {
585 lldb::offset_t offset = 0;
586 if (*byte_size == sizeof(float)) {
587 value.GetScalar() = (float)data.GetFloat(&offset);
589 } else if (*byte_size == sizeof(double)) {
590 value.GetScalar() = (double)data.GetDouble(&offset);
600 return_valobj_sp = ValueObjectConstResult::Create(
601 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
602 } else if (type_flags & eTypeIsPointer) {
604 reg_ctx->GetRegisterInfoByName("r3", 0)->kinds[eRegisterKindLLDB];
606 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(r3_id, 0);
607 value.SetValueType(Value::eValueTypeScalar);
608 return_valobj_sp = ValueObjectConstResult::Create(
609 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
610 } else if (type_flags & eTypeIsVector) {
611 llvm::Optional<uint64_t> byte_size =
612 return_compiler_type.GetByteSize(nullptr);
613 if (byte_size && *byte_size > 0) {
614 const RegisterInfo *altivec_reg = reg_ctx->GetRegisterInfoByName("v2", 0);
616 if (*byte_size <= altivec_reg->byte_size) {
617 ProcessSP process_sp(thread.GetProcess());
619 std::unique_ptr<DataBufferHeap> heap_data_up(
620 new DataBufferHeap(*byte_size, 0));
621 const ByteOrder byte_order = process_sp->GetByteOrder();
622 RegisterValue reg_value;
623 if (reg_ctx->ReadRegister(altivec_reg, reg_value)) {
625 if (reg_value.GetAsMemoryData(
626 altivec_reg, heap_data_up->GetBytes(),
627 heap_data_up->GetByteSize(), byte_order, error)) {
628 DataExtractor data(DataBufferSP(heap_data_up.release()),
630 process_sp->GetTarget()
632 .GetAddressByteSize());
633 return_valobj_sp = ValueObjectConstResult::Create(
634 &thread, return_compiler_type, ConstString(""), data);
643 return return_valobj_sp;
646 ValueObjectSP ABISysV_ppc::GetReturnValueObjectImpl(
647 Thread &thread, CompilerType &return_compiler_type) const {
648 ValueObjectSP return_valobj_sp;
650 if (!return_compiler_type)
651 return return_valobj_sp;
653 ExecutionContext exe_ctx(thread.shared_from_this());
654 return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type);
655 if (return_valobj_sp)
656 return return_valobj_sp;
658 RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
660 return return_valobj_sp;
662 llvm::Optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread);
664 return return_valobj_sp;
665 if (return_compiler_type.IsAggregateType()) {
666 Target *target = exe_ctx.GetTargetPtr();
667 bool is_memory = true;
668 if (*bit_width <= 128) {
669 ByteOrder target_byte_order = target->GetArchitecture().GetByteOrder();
670 DataBufferSP data_sp(new DataBufferHeap(16, 0));
671 DataExtractor return_ext(data_sp, target_byte_order,
672 target->GetArchitecture().GetAddressByteSize());
674 const RegisterInfo *r3_info = reg_ctx_sp->GetRegisterInfoByName("r3", 0);
675 const RegisterInfo *rdx_info =
676 reg_ctx_sp->GetRegisterInfoByName("rdx", 0);
678 RegisterValue r3_value, rdx_value;
679 reg_ctx_sp->ReadRegister(r3_info, r3_value);
680 reg_ctx_sp->ReadRegister(rdx_info, rdx_value);
682 DataExtractor r3_data, rdx_data;
684 r3_value.GetData(r3_data);
685 rdx_value.GetData(rdx_data);
688 0; // Tracks how much of the xmm registers we've consumed so far
689 uint32_t integer_bytes =
690 0; // Tracks how much of the r3/rds registers we've consumed so far
692 const uint32_t num_children = return_compiler_type.GetNumFields();
694 // Since we are in the small struct regime, assume we are not in memory.
697 for (uint32_t idx = 0; idx < num_children; idx++) {
699 uint64_t field_bit_offset = 0;
704 CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex(
705 idx, name, &field_bit_offset, nullptr, nullptr);
706 llvm::Optional<uint64_t> field_bit_width =
707 field_compiler_type.GetBitSize(&thread);
708 if (!field_bit_width)
709 return return_valobj_sp;
711 // If there are any unaligned fields, this is stored in memory.
712 if (field_bit_offset % *field_bit_width != 0) {
717 uint32_t field_byte_width = *field_bit_width / 8;
718 uint32_t field_byte_offset = field_bit_offset / 8;
720 DataExtractor *copy_from_extractor = nullptr;
721 uint32_t copy_from_offset = 0;
723 if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
724 field_compiler_type.IsPointerType()) {
725 if (integer_bytes < 8) {
726 if (integer_bytes + field_byte_width <= 8) {
727 // This is in RAX, copy from register to our result structure:
728 copy_from_extractor = &r3_data;
729 copy_from_offset = integer_bytes;
730 integer_bytes += field_byte_width;
732 // The next field wouldn't fit in the remaining space, so we
734 copy_from_extractor = &rdx_data;
735 copy_from_offset = 0;
736 integer_bytes = 8 + field_byte_width;
738 } else if (integer_bytes + field_byte_width <= 16) {
739 copy_from_extractor = &rdx_data;
740 copy_from_offset = integer_bytes - 8;
741 integer_bytes += field_byte_width;
743 // The last field didn't fit. I can't see how that would happen
744 // w/o the overall size being greater than 16 bytes. For now,
745 // return a nullptr return value object.
746 return return_valobj_sp;
748 } else if (field_compiler_type.IsFloatingPointType(count, is_complex)) {
749 // Structs with long doubles are always passed in memory.
750 if (*field_bit_width == 128) {
753 } else if (*field_bit_width == 64) {
754 copy_from_offset = 0;
755 fp_bytes += field_byte_width;
756 } else if (*field_bit_width == 32) {
757 // This one is kind of complicated. If we are in an "eightbyte"
758 // with another float, we'll be stuffed into an xmm register with
759 // it. If we are in an "eightbyte" with one or more ints, then we
760 // will be stuffed into the appropriate GPR with them.
762 if (field_byte_offset % 8 == 0) {
763 // We are at the beginning of one of the eightbytes, so check the
764 // next element (if any)
765 if (idx == num_children - 1)
768 uint64_t next_field_bit_offset = 0;
769 CompilerType next_field_compiler_type =
770 return_compiler_type.GetFieldAtIndex(idx + 1, name,
771 &next_field_bit_offset,
773 if (next_field_compiler_type.IsIntegerOrEnumerationType(
777 copy_from_offset = 0;
781 } else if (field_byte_offset % 4 == 0) {
782 // We are inside of an eightbyte, so see if the field before us
783 // is floating point: This could happen if somebody put padding
788 uint64_t prev_field_bit_offset = 0;
789 CompilerType prev_field_compiler_type =
790 return_compiler_type.GetFieldAtIndex(idx - 1, name,
791 &prev_field_bit_offset,
793 if (prev_field_compiler_type.IsIntegerOrEnumerationType(
797 copy_from_offset = 4;
806 // Okay, we've figured out whether we are in GPR or XMM, now figure
809 if (integer_bytes < 8) {
810 // This is in RAX, copy from register to our result structure:
811 copy_from_extractor = &r3_data;
812 copy_from_offset = integer_bytes;
813 integer_bytes += field_byte_width;
815 copy_from_extractor = &rdx_data;
816 copy_from_offset = integer_bytes - 8;
817 integer_bytes += field_byte_width;
820 fp_bytes += field_byte_width;
825 // These two tests are just sanity checks. If I somehow get the type
826 // calculation wrong above it is better to just return nothing than to
828 if (!copy_from_extractor)
829 return return_valobj_sp;
830 if (copy_from_offset + field_byte_width >
831 copy_from_extractor->GetByteSize())
832 return return_valobj_sp;
834 copy_from_extractor->CopyByteOrderedData(
835 copy_from_offset, field_byte_width,
836 data_sp->GetBytes() + field_byte_offset, field_byte_width,
841 // The result is in our data buffer. Let's make a variable object out
843 return_valobj_sp = ValueObjectConstResult::Create(
844 &thread, return_compiler_type, ConstString(""), return_ext);
848 // FIXME: This is just taking a guess, r3 may very well no longer hold the
849 // return storage location.
850 // If we are going to do this right, when we make a new frame we should
851 // check to see if it uses a memory return, and if we are at the first
852 // instruction and if so stash away the return location. Then we would
853 // only return the memory return value if we know it is valid.
857 reg_ctx_sp->GetRegisterInfoByName("r3", 0)->kinds[eRegisterKindLLDB];
858 lldb::addr_t storage_addr =
859 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(r3_id,
861 return_valobj_sp = ValueObjectMemory::Create(
862 &thread, "", Address(storage_addr, nullptr), return_compiler_type);
866 return return_valobj_sp;
869 bool ABISysV_ppc::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
871 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
873 uint32_t lr_reg_num = dwarf_lr;
874 uint32_t sp_reg_num = dwarf_r1;
875 uint32_t pc_reg_num = dwarf_pc;
877 UnwindPlan::RowSP row(new UnwindPlan::Row);
879 // Our Call Frame Address is the stack pointer value
880 row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 0);
882 // The previous PC is in the LR
883 row->SetRegisterLocationToRegister(pc_reg_num, lr_reg_num, true);
884 unwind_plan.AppendRow(row);
886 // All other registers are the same.
888 unwind_plan.SetSourceName("ppc at-func-entry default");
889 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
894 bool ABISysV_ppc::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
896 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
898 uint32_t sp_reg_num = dwarf_r1;
899 uint32_t pc_reg_num = dwarf_lr;
901 UnwindPlan::RowSP row(new UnwindPlan::Row);
903 const int32_t ptr_size = 4;
904 row->GetCFAValue().SetIsRegisterDereferenced(sp_reg_num);
906 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * 1, true);
907 row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
909 unwind_plan.AppendRow(row);
910 unwind_plan.SetSourceName("ppc default unwind plan");
911 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
912 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
913 unwind_plan.SetReturnAddressRegister(dwarf_lr);
917 bool ABISysV_ppc::RegisterIsVolatile(const RegisterInfo *reg_info) {
918 return !RegisterIsCalleeSaved(reg_info);
921 // See "Register Usage" in the
922 // "System V Application Binary Interface"
923 // "64-bit PowerPC ELF Application Binary Interface Supplement" current version
924 // is 1.9 released 2004 at http://refspecs.linuxfoundation.org/ELF/ppc/PPC-
927 bool ABISysV_ppc::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {
929 // Preserved registers are :
935 const char *name = reg_info->name;
936 if (name[0] == 'r') {
937 if ((name[1] == '1' || name[1] == '2') && name[2] == '\0')
939 if (name[1] == '1' && name[2] > '2')
941 if ((name[1] == '2' || name[1] == '3') && name[2] != '\0')
945 if (name[0] == 'f' && name[1] >= '0' && name[1] <= '9') {
946 if (name[3] == '1' && name[4] >= '4')
948 if ((name[3] == '2' || name[3] == '3') && name[4] != '\0')
952 if (name[0] == 's' && name[1] == 'p' && name[2] == '\0') // sp
954 if (name[0] == 'f' && name[1] == 'p' && name[2] == '\0') // fp
956 if (name[0] == 'p' && name[1] == 'c' && name[2] == '\0') // pc
962 void ABISysV_ppc::Initialize() {
963 PluginManager::RegisterPlugin(GetPluginNameStatic(),
964 "System V ABI for ppc targets", CreateInstance);
967 void ABISysV_ppc::Terminate() {
968 PluginManager::UnregisterPlugin(CreateInstance);
971 lldb_private::ConstString ABISysV_ppc::GetPluginNameStatic() {
972 static ConstString g_name("sysv-ppc");
976 // PluginInterface protocol
978 lldb_private::ConstString ABISysV_ppc::GetPluginName() {
979 return GetPluginNameStatic();
982 uint32_t ABISysV_ppc::GetPluginVersion() { return 1; }