1 //===-- ABISysV_ppc.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ABISysV_ppc.h"
14 // Other libraries and framework includes
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/Triple.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Core/RegisterValue.h"
22 #include "lldb/Core/Value.h"
23 #include "lldb/Core/ValueObjectConstResult.h"
24 #include "lldb/Core/ValueObjectMemory.h"
25 #include "lldb/Core/ValueObjectRegister.h"
26 #include "lldb/Symbol/UnwindPlan.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/RegisterContext.h"
29 #include "lldb/Target/StackFrame.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
32 #include "lldb/Utility/ConstString.h"
33 #include "lldb/Utility/DataExtractor.h"
34 #include "lldb/Utility/Log.h"
35 #include "lldb/Utility/Status.h"
38 using namespace lldb_private;
114 // Note that the size and offset will be updated by platform-specific classes.
115 #define DEFINE_GPR(reg, alt, kind1, kind2, kind3, kind4) \
117 #reg, alt, 8, 0, eEncodingUint, eFormatHex, {kind1, kind2, kind3, kind4 }, \
118 nullptr, nullptr, nullptr, 0 \
121 static const RegisterInfo g_register_infos[] = {
122 // General purpose registers. eh_frame, DWARF,
123 // Generic, Process Plugin
124 DEFINE_GPR(r0, nullptr, dwarf_r0, dwarf_r0, LLDB_INVALID_REGNUM,
125 LLDB_INVALID_REGNUM),
126 DEFINE_GPR(r1, "sp", dwarf_r1, dwarf_r1, LLDB_REGNUM_GENERIC_SP,
127 LLDB_INVALID_REGNUM),
128 DEFINE_GPR(r2, nullptr, dwarf_r2, dwarf_r2, LLDB_INVALID_REGNUM,
129 LLDB_INVALID_REGNUM),
130 DEFINE_GPR(r3, "arg1", dwarf_r3, dwarf_r3, LLDB_REGNUM_GENERIC_ARG1,
131 LLDB_INVALID_REGNUM),
132 DEFINE_GPR(r4, "arg2", dwarf_r4, dwarf_r4, LLDB_REGNUM_GENERIC_ARG2,
133 LLDB_INVALID_REGNUM),
134 DEFINE_GPR(r5, "arg3", dwarf_r5, dwarf_r5, LLDB_REGNUM_GENERIC_ARG3,
135 LLDB_INVALID_REGNUM),
136 DEFINE_GPR(r6, "arg4", dwarf_r6, dwarf_r6, LLDB_REGNUM_GENERIC_ARG4,
137 LLDB_INVALID_REGNUM),
138 DEFINE_GPR(r7, "arg5", dwarf_r7, dwarf_r7, LLDB_REGNUM_GENERIC_ARG5,
139 LLDB_INVALID_REGNUM),
140 DEFINE_GPR(r8, "arg6", dwarf_r8, dwarf_r8, LLDB_REGNUM_GENERIC_ARG6,
141 LLDB_INVALID_REGNUM),
142 DEFINE_GPR(r9, "arg7", dwarf_r9, dwarf_r9, LLDB_REGNUM_GENERIC_ARG7,
143 LLDB_INVALID_REGNUM),
144 DEFINE_GPR(r10, "arg8", dwarf_r10, dwarf_r10, LLDB_REGNUM_GENERIC_ARG8,
145 LLDB_INVALID_REGNUM),
146 DEFINE_GPR(r11, nullptr, dwarf_r11, dwarf_r11, LLDB_INVALID_REGNUM,
147 LLDB_INVALID_REGNUM),
148 DEFINE_GPR(r12, nullptr, dwarf_r12, dwarf_r12, LLDB_INVALID_REGNUM,
149 LLDB_INVALID_REGNUM),
150 DEFINE_GPR(r13, nullptr, dwarf_r13, dwarf_r13, LLDB_INVALID_REGNUM,
151 LLDB_INVALID_REGNUM),
152 DEFINE_GPR(r14, nullptr, dwarf_r14, dwarf_r14, LLDB_INVALID_REGNUM,
153 LLDB_INVALID_REGNUM),
154 DEFINE_GPR(r15, nullptr, dwarf_r15, dwarf_r15, LLDB_INVALID_REGNUM,
155 LLDB_INVALID_REGNUM),
156 DEFINE_GPR(r16, nullptr, dwarf_r16, dwarf_r16, LLDB_INVALID_REGNUM,
157 LLDB_INVALID_REGNUM),
158 DEFINE_GPR(r17, nullptr, dwarf_r17, dwarf_r17, LLDB_INVALID_REGNUM,
159 LLDB_INVALID_REGNUM),
160 DEFINE_GPR(r18, nullptr, dwarf_r18, dwarf_r18, LLDB_INVALID_REGNUM,
161 LLDB_INVALID_REGNUM),
162 DEFINE_GPR(r19, nullptr, dwarf_r19, dwarf_r19, LLDB_INVALID_REGNUM,
163 LLDB_INVALID_REGNUM),
164 DEFINE_GPR(r20, nullptr, dwarf_r20, dwarf_r20, LLDB_INVALID_REGNUM,
165 LLDB_INVALID_REGNUM),
166 DEFINE_GPR(r21, nullptr, dwarf_r21, dwarf_r21, LLDB_INVALID_REGNUM,
167 LLDB_INVALID_REGNUM),
168 DEFINE_GPR(r22, nullptr, dwarf_r22, dwarf_r22, LLDB_INVALID_REGNUM,
169 LLDB_INVALID_REGNUM),
170 DEFINE_GPR(r23, nullptr, dwarf_r23, dwarf_r23, LLDB_INVALID_REGNUM,
171 LLDB_INVALID_REGNUM),
172 DEFINE_GPR(r24, nullptr, dwarf_r24, dwarf_r24, LLDB_INVALID_REGNUM,
173 LLDB_INVALID_REGNUM),
174 DEFINE_GPR(r25, nullptr, dwarf_r25, dwarf_r25, LLDB_INVALID_REGNUM,
175 LLDB_INVALID_REGNUM),
176 DEFINE_GPR(r26, nullptr, dwarf_r26, dwarf_r26, LLDB_INVALID_REGNUM,
177 LLDB_INVALID_REGNUM),
178 DEFINE_GPR(r27, nullptr, dwarf_r27, dwarf_r27, LLDB_INVALID_REGNUM,
179 LLDB_INVALID_REGNUM),
180 DEFINE_GPR(r28, nullptr, dwarf_r28, dwarf_r28, LLDB_INVALID_REGNUM,
181 LLDB_INVALID_REGNUM),
182 DEFINE_GPR(r29, nullptr, dwarf_r29, dwarf_r29, LLDB_INVALID_REGNUM,
183 LLDB_INVALID_REGNUM),
184 DEFINE_GPR(r30, nullptr, dwarf_r30, dwarf_r30, LLDB_INVALID_REGNUM,
185 LLDB_INVALID_REGNUM),
186 DEFINE_GPR(r31, nullptr, dwarf_r31, dwarf_r31, LLDB_INVALID_REGNUM,
187 LLDB_INVALID_REGNUM),
188 DEFINE_GPR(lr, "lr", dwarf_lr, dwarf_lr, LLDB_REGNUM_GENERIC_RA,
189 LLDB_INVALID_REGNUM),
190 DEFINE_GPR(cr, "cr", dwarf_cr, dwarf_cr, LLDB_REGNUM_GENERIC_FLAGS,
191 LLDB_INVALID_REGNUM),
192 DEFINE_GPR(xer, "xer", dwarf_xer, dwarf_xer, LLDB_INVALID_REGNUM,
193 LLDB_INVALID_REGNUM),
194 DEFINE_GPR(ctr, "ctr", dwarf_ctr, dwarf_ctr, LLDB_INVALID_REGNUM,
195 LLDB_INVALID_REGNUM),
196 DEFINE_GPR(pc, "pc", dwarf_pc, dwarf_pc, LLDB_REGNUM_GENERIC_PC,
197 LLDB_INVALID_REGNUM),
204 {dwarf_cfa, dwarf_cfa, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
210 static const uint32_t k_num_register_infos =
211 llvm::array_lengthof(g_register_infos);
213 const lldb_private::RegisterInfo *
214 ABISysV_ppc::GetRegisterInfoArray(uint32_t &count) {
215 count = k_num_register_infos;
216 return g_register_infos;
219 size_t ABISysV_ppc::GetRedZoneSize() const { return 224; }
221 //------------------------------------------------------------------
223 //------------------------------------------------------------------
226 ABISysV_ppc::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) {
227 static ABISP g_abi_sp;
228 if (arch.GetTriple().getArch() == llvm::Triple::ppc) {
230 g_abi_sp.reset(new ABISysV_ppc(process_sp));
236 bool ABISysV_ppc::PrepareTrivialCall(Thread &thread, addr_t sp,
237 addr_t func_addr, addr_t return_addr,
238 llvm::ArrayRef<addr_t> args) const {
239 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
243 s.Printf("ABISysV_ppc::PrepareTrivialCall (tid = 0x%" PRIx64
244 ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
245 ", return_addr = 0x%" PRIx64,
246 thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
247 (uint64_t)return_addr);
249 for (size_t i = 0; i < args.size(); ++i)
250 s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
253 log->PutString(s.GetString());
256 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
260 const RegisterInfo *reg_info = nullptr;
262 if (args.size() > 8) // TODO handle more than 8 arguments
265 for (size_t i = 0; i < args.size(); ++i) {
266 reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
267 LLDB_REGNUM_GENERIC_ARG1 + i);
269 log->Printf("About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s",
270 static_cast<uint64_t>(i + 1), args[i], reg_info->name);
271 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i]))
275 // First, align the SP
278 log->Printf("16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,
279 (uint64_t)sp, (uint64_t)(sp & ~0xfull));
281 sp &= ~(0xfull); // 16-byte alignment
286 const RegisterInfo *pc_reg_info =
287 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
288 const RegisterInfo *sp_reg_info =
289 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
290 ProcessSP process_sp(thread.GetProcess());
292 RegisterValue reg_value;
295 log->Printf("Pushing the return address onto the stack: 0x%" PRIx64
297 (uint64_t)sp, (uint64_t)return_addr);
299 // Save return address onto the stack
300 if (!process_sp->WritePointerToMemory(sp, return_addr, error))
303 // %r1 is set to the actual stack value.
306 log->Printf("Writing SP: 0x%" PRIx64, (uint64_t)sp);
308 if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))
311 // %pc is set to the address of the called function.
314 log->Printf("Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
316 if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))
322 static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
323 bool is_signed, Thread &thread,
324 uint32_t *argument_register_ids,
325 unsigned int ¤t_argument_register,
326 addr_t ¤t_stack_argument) {
328 return false; // Scalar can't hold large integer arguments
330 if (current_argument_register < 6) {
331 scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
332 argument_register_ids[current_argument_register], 0);
333 current_argument_register++;
335 scalar.SignExtend(bit_width);
337 uint32_t byte_size = (bit_width + (8 - 1)) / 8;
339 if (thread.GetProcess()->ReadScalarIntegerFromMemory(
340 current_stack_argument, byte_size, is_signed, scalar, error)) {
341 current_stack_argument += byte_size;
349 bool ABISysV_ppc::GetArgumentValues(Thread &thread, ValueList &values) const {
350 unsigned int num_values = values.GetSize();
351 unsigned int value_index;
353 // Extract the register context so we can read arguments from registers
355 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
360 // Get the pointer to the first stack argument so we have a place to start
363 addr_t sp = reg_ctx->GetSP(0);
368 addr_t current_stack_argument = sp + 48; // jump over return address
370 uint32_t argument_register_ids[8];
372 argument_register_ids[0] =
373 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1)
374 ->kinds[eRegisterKindLLDB];
375 argument_register_ids[1] =
376 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2)
377 ->kinds[eRegisterKindLLDB];
378 argument_register_ids[2] =
379 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3)
380 ->kinds[eRegisterKindLLDB];
381 argument_register_ids[3] =
382 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4)
383 ->kinds[eRegisterKindLLDB];
384 argument_register_ids[4] =
385 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG5)
386 ->kinds[eRegisterKindLLDB];
387 argument_register_ids[5] =
388 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG6)
389 ->kinds[eRegisterKindLLDB];
390 argument_register_ids[6] =
391 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG7)
392 ->kinds[eRegisterKindLLDB];
393 argument_register_ids[7] =
394 reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG8)
395 ->kinds[eRegisterKindLLDB];
397 unsigned int current_argument_register = 0;
399 for (value_index = 0; value_index < num_values; ++value_index) {
400 Value *value = values.GetValueAtIndex(value_index);
405 // We currently only support extracting values with Clang QualTypes. Do we
406 // care about others?
407 CompilerType compiler_type = value->GetCompilerType();
412 if (compiler_type.IsIntegerOrEnumerationType(is_signed)) {
413 ReadIntegerArgument(value->GetScalar(), compiler_type.GetBitSize(&thread),
414 is_signed, thread, argument_register_ids,
415 current_argument_register, current_stack_argument);
416 } else if (compiler_type.IsPointerType()) {
417 ReadIntegerArgument(value->GetScalar(), compiler_type.GetBitSize(&thread),
418 false, thread, argument_register_ids,
419 current_argument_register, current_stack_argument);
426 Status ABISysV_ppc::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
427 lldb::ValueObjectSP &new_value_sp) {
430 error.SetErrorString("Empty value object for return value.");
434 CompilerType compiler_type = new_value_sp->GetCompilerType();
435 if (!compiler_type) {
436 error.SetErrorString("Null clang type for return value.");
440 Thread *thread = frame_sp->GetThread().get();
446 RegisterContext *reg_ctx = thread->GetRegisterContext().get();
448 bool set_it_simple = false;
449 if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
450 compiler_type.IsPointerType()) {
451 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("r3", 0);
455 size_t num_bytes = new_value_sp->GetData(data, data_error);
456 if (data_error.Fail()) {
457 error.SetErrorStringWithFormat(
458 "Couldn't convert return value to raw data: %s",
459 data_error.AsCString());
462 lldb::offset_t offset = 0;
463 if (num_bytes <= 8) {
464 uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
466 if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))
467 set_it_simple = true;
469 error.SetErrorString("We don't support returning longer than 64 bit "
470 "integer values at present.");
472 } else if (compiler_type.IsFloatingPointType(count, is_complex)) {
474 error.SetErrorString(
475 "We don't support returning complex values at present");
477 size_t bit_width = compiler_type.GetBitSize(frame_sp.get());
478 if (bit_width <= 64) {
481 size_t num_bytes = new_value_sp->GetData(data, data_error);
482 if (data_error.Fail()) {
483 error.SetErrorStringWithFormat(
484 "Couldn't convert return value to raw data: %s",
485 data_error.AsCString());
489 unsigned char buffer[16];
490 ByteOrder byte_order = data.GetByteOrder();
492 data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order);
493 set_it_simple = true;
495 // FIXME - don't know how to do 80 bit long doubles yet.
496 error.SetErrorString(
497 "We don't support returning float values > 64 bits at present");
502 if (!set_it_simple) {
503 // Okay we've got a structure or something that doesn't fit in a simple
504 // register. We should figure out where it really goes, but we don't
506 error.SetErrorString("We only support setting simple integer and float "
507 "return types at present.");
513 ValueObjectSP ABISysV_ppc::GetReturnValueObjectSimple(
514 Thread &thread, CompilerType &return_compiler_type) const {
515 ValueObjectSP return_valobj_sp;
518 if (!return_compiler_type)
519 return return_valobj_sp;
521 // value.SetContext (Value::eContextTypeClangType, return_value_type);
522 value.SetCompilerType(return_compiler_type);
524 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
526 return return_valobj_sp;
528 const uint32_t type_flags = return_compiler_type.GetTypeInfo();
529 if (type_flags & eTypeIsScalar) {
530 value.SetValueType(Value::eValueTypeScalar);
532 bool success = false;
533 if (type_flags & eTypeIsInteger) {
534 // Extract the register context so we can read arguments from registers
536 const size_t byte_size = return_compiler_type.GetByteSize(nullptr);
537 uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
538 reg_ctx->GetRegisterInfoByName("r3", 0), 0);
539 const bool is_signed = (type_flags & eTypeIsSigned) != 0;
544 case sizeof(uint64_t):
546 value.GetScalar() = (int64_t)(raw_value);
548 value.GetScalar() = (uint64_t)(raw_value);
552 case sizeof(uint32_t):
554 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
556 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
560 case sizeof(uint16_t):
562 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
564 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
568 case sizeof(uint8_t):
570 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
572 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
576 } else if (type_flags & eTypeIsFloat) {
577 if (type_flags & eTypeIsComplex) {
578 // Don't handle complex yet.
580 const size_t byte_size = return_compiler_type.GetByteSize(nullptr);
581 if (byte_size <= sizeof(long double)) {
582 const RegisterInfo *f1_info = reg_ctx->GetRegisterInfoByName("f1", 0);
583 RegisterValue f1_value;
584 if (reg_ctx->ReadRegister(f1_info, f1_value)) {
586 if (f1_value.GetData(data)) {
587 lldb::offset_t offset = 0;
588 if (byte_size == sizeof(float)) {
589 value.GetScalar() = (float)data.GetFloat(&offset);
591 } else if (byte_size == sizeof(double)) {
592 value.GetScalar() = (double)data.GetDouble(&offset);
602 return_valobj_sp = ValueObjectConstResult::Create(
603 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
604 } else if (type_flags & eTypeIsPointer) {
606 reg_ctx->GetRegisterInfoByName("r3", 0)->kinds[eRegisterKindLLDB];
608 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(r3_id, 0);
609 value.SetValueType(Value::eValueTypeScalar);
610 return_valobj_sp = ValueObjectConstResult::Create(
611 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
612 } else if (type_flags & eTypeIsVector) {
613 const size_t byte_size = return_compiler_type.GetByteSize(nullptr);
615 const RegisterInfo *altivec_reg = reg_ctx->GetRegisterInfoByName("v2", 0);
617 if (byte_size <= altivec_reg->byte_size) {
618 ProcessSP process_sp(thread.GetProcess());
620 std::unique_ptr<DataBufferHeap> heap_data_ap(
621 new DataBufferHeap(byte_size, 0));
622 const ByteOrder byte_order = process_sp->GetByteOrder();
623 RegisterValue reg_value;
624 if (reg_ctx->ReadRegister(altivec_reg, reg_value)) {
626 if (reg_value.GetAsMemoryData(
627 altivec_reg, heap_data_ap->GetBytes(),
628 heap_data_ap->GetByteSize(), byte_order, error)) {
629 DataExtractor data(DataBufferSP(heap_data_ap.release()),
630 byte_order, 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 const size_t bit_width = return_compiler_type.GetBitSize(&thread);
663 if (return_compiler_type.IsAggregateType()) {
664 Target *target = exe_ctx.GetTargetPtr();
665 bool is_memory = true;
666 if (bit_width <= 128) {
667 ByteOrder target_byte_order = target->GetArchitecture().GetByteOrder();
668 DataBufferSP data_sp(new DataBufferHeap(16, 0));
669 DataExtractor return_ext(data_sp, target_byte_order,
670 target->GetArchitecture().GetAddressByteSize());
672 const RegisterInfo *r3_info = reg_ctx_sp->GetRegisterInfoByName("r3", 0);
673 const RegisterInfo *rdx_info =
674 reg_ctx_sp->GetRegisterInfoByName("rdx", 0);
676 RegisterValue r3_value, rdx_value;
677 reg_ctx_sp->ReadRegister(r3_info, r3_value);
678 reg_ctx_sp->ReadRegister(rdx_info, rdx_value);
680 DataExtractor r3_data, rdx_data;
682 r3_value.GetData(r3_data);
683 rdx_value.GetData(rdx_data);
686 0; // Tracks how much of the xmm registers we've consumed so far
687 uint32_t integer_bytes =
688 0; // Tracks how much of the r3/rds registers we've consumed so far
690 const uint32_t num_children = return_compiler_type.GetNumFields();
692 // Since we are in the small struct regime, assume we are not in memory.
695 for (uint32_t idx = 0; idx < num_children; idx++) {
697 uint64_t field_bit_offset = 0;
702 CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex(
703 idx, name, &field_bit_offset, nullptr, nullptr);
704 const size_t field_bit_width = field_compiler_type.GetBitSize(&thread);
706 // If there are any unaligned fields, this is stored in memory.
707 if (field_bit_offset % field_bit_width != 0) {
712 uint32_t field_byte_width = field_bit_width / 8;
713 uint32_t field_byte_offset = field_bit_offset / 8;
715 DataExtractor *copy_from_extractor = nullptr;
716 uint32_t copy_from_offset = 0;
718 if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
719 field_compiler_type.IsPointerType()) {
720 if (integer_bytes < 8) {
721 if (integer_bytes + field_byte_width <= 8) {
722 // This is in RAX, copy from register to our result structure:
723 copy_from_extractor = &r3_data;
724 copy_from_offset = integer_bytes;
725 integer_bytes += field_byte_width;
727 // The next field wouldn't fit in the remaining space, so we
729 copy_from_extractor = &rdx_data;
730 copy_from_offset = 0;
731 integer_bytes = 8 + field_byte_width;
733 } else if (integer_bytes + field_byte_width <= 16) {
734 copy_from_extractor = &rdx_data;
735 copy_from_offset = integer_bytes - 8;
736 integer_bytes += field_byte_width;
738 // The last field didn't fit. I can't see how that would happen
739 // w/o the overall size being greater than 16 bytes. For now,
740 // return a nullptr return value object.
741 return return_valobj_sp;
743 } else if (field_compiler_type.IsFloatingPointType(count, is_complex)) {
744 // Structs with long doubles are always passed in memory.
745 if (field_bit_width == 128) {
748 } else if (field_bit_width == 64) {
749 copy_from_offset = 0;
750 fp_bytes += field_byte_width;
751 } else if (field_bit_width == 32) {
752 // This one is kind of complicated. If we are in an "eightbyte"
753 // with another float, we'll be stuffed into an xmm register with
754 // it. If we are in an "eightbyte" with one or more ints, then we
755 // will be stuffed into the appropriate GPR with them.
757 if (field_byte_offset % 8 == 0) {
758 // We are at the beginning of one of the eightbytes, so check the
759 // next element (if any)
760 if (idx == num_children - 1)
763 uint64_t next_field_bit_offset = 0;
764 CompilerType next_field_compiler_type =
765 return_compiler_type.GetFieldAtIndex(idx + 1, name,
766 &next_field_bit_offset,
768 if (next_field_compiler_type.IsIntegerOrEnumerationType(
772 copy_from_offset = 0;
776 } else if (field_byte_offset % 4 == 0) {
777 // We are inside of an eightbyte, so see if the field before us
778 // is floating point: This could happen if somebody put padding
783 uint64_t prev_field_bit_offset = 0;
784 CompilerType prev_field_compiler_type =
785 return_compiler_type.GetFieldAtIndex(idx - 1, name,
786 &prev_field_bit_offset,
788 if (prev_field_compiler_type.IsIntegerOrEnumerationType(
792 copy_from_offset = 4;
801 // Okay, we've figured out whether we are in GPR or XMM, now figure
804 if (integer_bytes < 8) {
805 // This is in RAX, copy from register to our result structure:
806 copy_from_extractor = &r3_data;
807 copy_from_offset = integer_bytes;
808 integer_bytes += field_byte_width;
810 copy_from_extractor = &rdx_data;
811 copy_from_offset = integer_bytes - 8;
812 integer_bytes += field_byte_width;
815 fp_bytes += field_byte_width;
820 // These two tests are just sanity checks. If I somehow get the type
821 // calculation wrong above it is better to just return nothing than to
823 if (!copy_from_extractor)
824 return return_valobj_sp;
825 if (copy_from_offset + field_byte_width >
826 copy_from_extractor->GetByteSize())
827 return return_valobj_sp;
829 copy_from_extractor->CopyByteOrderedData(
830 copy_from_offset, field_byte_width,
831 data_sp->GetBytes() + field_byte_offset, field_byte_width,
836 // The result is in our data buffer. Let's make a variable object out
838 return_valobj_sp = ValueObjectConstResult::Create(
839 &thread, return_compiler_type, ConstString(""), return_ext);
843 // FIXME: This is just taking a guess, r3 may very well no longer hold the
844 // return storage location.
845 // If we are going to do this right, when we make a new frame we should
846 // check to see if it uses a memory return, and if we are at the first
847 // instruction and if so stash away the return location. Then we would
848 // only return the memory return value if we know it is valid.
852 reg_ctx_sp->GetRegisterInfoByName("r3", 0)->kinds[eRegisterKindLLDB];
853 lldb::addr_t storage_addr =
854 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(r3_id,
856 return_valobj_sp = ValueObjectMemory::Create(
857 &thread, "", Address(storage_addr, nullptr), return_compiler_type);
861 return return_valobj_sp;
864 bool ABISysV_ppc::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
866 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
868 uint32_t lr_reg_num = dwarf_lr;
869 uint32_t sp_reg_num = dwarf_r1;
870 uint32_t pc_reg_num = dwarf_pc;
872 UnwindPlan::RowSP row(new UnwindPlan::Row);
874 // Our Call Frame Address is the stack pointer value
875 row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 0);
877 // The previous PC is in the LR
878 row->SetRegisterLocationToRegister(pc_reg_num, lr_reg_num, true);
879 unwind_plan.AppendRow(row);
881 // All other registers are the same.
883 unwind_plan.SetSourceName("ppc at-func-entry default");
884 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
889 bool ABISysV_ppc::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
891 unwind_plan.SetRegisterKind(eRegisterKindDWARF);
893 uint32_t sp_reg_num = dwarf_r1;
894 uint32_t pc_reg_num = dwarf_lr;
896 UnwindPlan::RowSP row(new UnwindPlan::Row);
898 const int32_t ptr_size = 4;
899 row->GetCFAValue().SetIsRegisterDereferenced(sp_reg_num);
901 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * 1, true);
902 row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
904 unwind_plan.AppendRow(row);
905 unwind_plan.SetSourceName("ppc default unwind plan");
906 unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
907 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
908 unwind_plan.SetReturnAddressRegister(dwarf_lr);
912 bool ABISysV_ppc::RegisterIsVolatile(const RegisterInfo *reg_info) {
913 return !RegisterIsCalleeSaved(reg_info);
916 // See "Register Usage" in the
917 // "System V Application Binary Interface"
918 // "64-bit PowerPC ELF Application Binary Interface Supplement" current version
919 // is 1.9 released 2004 at http://refspecs.linuxfoundation.org/ELF/ppc/PPC-
922 bool ABISysV_ppc::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {
924 // Preserved registers are :
930 const char *name = reg_info->name;
931 if (name[0] == 'r') {
932 if ((name[1] == '1' || name[1] == '2') && name[2] == '\0')
934 if (name[1] == '1' && name[2] > '2')
936 if ((name[1] == '2' || name[1] == '3') && name[2] != '\0')
940 if (name[0] == 'f' && name[1] >= '0' && name[1] <= '9') {
941 if (name[3] == '1' && name[4] >= '4')
943 if ((name[3] == '2' || name[3] == '3') && name[4] != '\0')
947 if (name[0] == 's' && name[1] == 'p' && name[2] == '\0') // sp
949 if (name[0] == 'f' && name[1] == 'p' && name[2] == '\0') // fp
951 if (name[0] == 'p' && name[1] == 'c' && name[2] == '\0') // pc
957 void ABISysV_ppc::Initialize() {
958 PluginManager::RegisterPlugin(GetPluginNameStatic(),
959 "System V ABI for ppc targets", CreateInstance);
962 void ABISysV_ppc::Terminate() {
963 PluginManager::UnregisterPlugin(CreateInstance);
966 lldb_private::ConstString ABISysV_ppc::GetPluginNameStatic() {
967 static ConstString g_name("sysv-ppc");
971 //------------------------------------------------------------------
972 // PluginInterface protocol
973 //------------------------------------------------------------------
975 lldb_private::ConstString ABISysV_ppc::GetPluginName() {
976 return GetPluginNameStatic();
979 uint32_t ABISysV_ppc::GetPluginVersion() { return 1; }