]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Plugins/ABI/SysV-ppc/ABISysV_ppc.cpp
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / source / Plugins / ABI / SysV-ppc / ABISysV_ppc.cpp
1 //===-- ABISysV_ppc.cpp -----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "ABISysV_ppc.h"
11
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/Triple.h"
17
18 // Project includes
19 #include "lldb/Core/ConstString.h"
20 #include "lldb/Core/DataExtractor.h"
21 #include "lldb/Core/Error.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Core/Module.h"
24 #include "lldb/Core/PluginManager.h"
25 #include "lldb/Core/RegisterValue.h"
26 #include "lldb/Core/Value.h"
27 #include "lldb/Core/ValueObjectConstResult.h"
28 #include "lldb/Core/ValueObjectMemory.h"
29 #include "lldb/Core/ValueObjectRegister.h"
30 #include "lldb/Symbol/UnwindPlan.h"
31 #include "lldb/Target/Process.h"
32 #include "lldb/Target/RegisterContext.h"
33 #include "lldb/Target/StackFrame.h"
34 #include "lldb/Target/Target.h"
35 #include "lldb/Target/Thread.h"
36
37 using namespace lldb;
38 using namespace lldb_private;
39
40 enum dwarf_regnums {
41   dwarf_r0 = 0,
42   dwarf_r1,
43   dwarf_r2,
44   dwarf_r3,
45   dwarf_r4,
46   dwarf_r5,
47   dwarf_r6,
48   dwarf_r7,
49   dwarf_r8,
50   dwarf_r9,
51   dwarf_r10,
52   dwarf_r11,
53   dwarf_r12,
54   dwarf_r13,
55   dwarf_r14,
56   dwarf_r15,
57   dwarf_r16,
58   dwarf_r17,
59   dwarf_r18,
60   dwarf_r19,
61   dwarf_r20,
62   dwarf_r21,
63   dwarf_r22,
64   dwarf_r23,
65   dwarf_r24,
66   dwarf_r25,
67   dwarf_r26,
68   dwarf_r27,
69   dwarf_r28,
70   dwarf_r29,
71   dwarf_r30,
72   dwarf_r31,
73   dwarf_f0,
74   dwarf_f1,
75   dwarf_f2,
76   dwarf_f3,
77   dwarf_f4,
78   dwarf_f5,
79   dwarf_f6,
80   dwarf_f7,
81   dwarf_f8,
82   dwarf_f9,
83   dwarf_f10,
84   dwarf_f11,
85   dwarf_f12,
86   dwarf_f13,
87   dwarf_f14,
88   dwarf_f15,
89   dwarf_f16,
90   dwarf_f17,
91   dwarf_f18,
92   dwarf_f19,
93   dwarf_f20,
94   dwarf_f21,
95   dwarf_f22,
96   dwarf_f23,
97   dwarf_f24,
98   dwarf_f25,
99   dwarf_f26,
100   dwarf_f27,
101   dwarf_f28,
102   dwarf_f29,
103   dwarf_f30,
104   dwarf_f31,
105   dwarf_cr,
106   dwarf_fpscr,
107   dwarf_xer = 101,
108   dwarf_lr = 108,
109   dwarf_ctr,
110   dwarf_pc,
111   dwarf_cfa,
112 };
113
114 // Note that the size and offset will be updated by platform-specific classes.
115 #define DEFINE_GPR(reg, alt, kind1, kind2, kind3, kind4)                       \
116   {                                                                            \
117     #reg, alt, 8, 0, eEncodingUint, eFormatHex, {kind1, kind2, kind3, kind4 }, \
118                                                  nullptr, nullptr, nullptr, 0  \
119   }
120
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),
198     {nullptr,
199      nullptr,
200      8,
201      0,
202      eEncodingUint,
203      eFormatHex,
204      {dwarf_cfa, dwarf_cfa, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
205      nullptr,
206      nullptr,
207      nullptr,
208      0}};
209
210 static const uint32_t k_num_register_infos =
211     llvm::array_lengthof(g_register_infos);
212
213 const lldb_private::RegisterInfo *
214 ABISysV_ppc::GetRegisterInfoArray(uint32_t &count) {
215   count = k_num_register_infos;
216   return g_register_infos;
217 }
218
219 size_t ABISysV_ppc::GetRedZoneSize() const { return 224; }
220
221 //------------------------------------------------------------------
222 // Static Functions
223 //------------------------------------------------------------------
224
225 ABISP
226 ABISysV_ppc::CreateInstance(const ArchSpec &arch) {
227   static ABISP g_abi_sp;
228   if (arch.GetTriple().getArch() == llvm::Triple::ppc) {
229     if (!g_abi_sp)
230       g_abi_sp.reset(new ABISysV_ppc);
231     return g_abi_sp;
232   }
233   return ABISP();
234 }
235
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));
240
241   if (log) {
242     StreamString s;
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);
248
249     for (size_t i = 0; i < args.size(); ++i)
250       s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
251                args[i]);
252     s.PutCString(")");
253     log->PutString(s.GetString());
254   }
255
256   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
257   if (!reg_ctx)
258     return false;
259
260   const RegisterInfo *reg_info = nullptr;
261
262   if (args.size() > 8) // TODO handle more than 8 arguments
263     return false;
264
265   for (size_t i = 0; i < args.size(); ++i) {
266     reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
267                                         LLDB_REGNUM_GENERIC_ARG1 + i);
268     if (log)
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]))
272       return false;
273   }
274
275   // First, align the SP
276
277   if (log)
278     log->Printf("16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,
279                 (uint64_t)sp, (uint64_t)(sp & ~0xfull));
280
281   sp &= ~(0xfull); // 16-byte alignment
282
283   sp -= 8;
284
285   Error error;
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());
291
292   RegisterValue reg_value;
293
294 #if 0
295     // This code adds an extra frame so that we don't lose the function that we came from
296     // by pushing the PC and the FP and then writing the current FP to point to the FP value
297     // we just pushed. It is disabled for now until the stack backtracing code can be debugged.
298
299     // Save current PC
300     const RegisterInfo *fp_reg_info = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP);
301     if (reg_ctx->ReadRegister(pc_reg_info, reg_value))
302     {
303         if (log)
304             log->Printf("Pushing the current PC onto the stack: 0x%" PRIx64 ": 0x%" PRIx64, (uint64_t)sp, reg_value.GetAsUInt64());
305
306         if (!process_sp->WritePointerToMemory(sp, reg_value.GetAsUInt64(), error))
307             return false;
308
309         sp -= 8;
310
311         // Save current FP
312         if (reg_ctx->ReadRegister(fp_reg_info, reg_value))
313         {
314             if (log)
315                 log->Printf("Pushing the current FP onto the stack: 0x%" PRIx64 ": 0x%" PRIx64, (uint64_t)sp, reg_value.GetAsUInt64());
316
317             if (!process_sp->WritePointerToMemory(sp, reg_value.GetAsUInt64(), error))
318                 return false;
319         }
320         // Setup FP backchain
321         reg_value.SetUInt64 (sp);
322
323         if (log)
324             log->Printf("Writing FP:  0x%" PRIx64 " (for FP backchain)", reg_value.GetAsUInt64());
325
326         if (!reg_ctx->WriteRegister(fp_reg_info, reg_value))
327         {
328             return false;
329         }
330
331         sp -= 8;
332     }
333 #endif
334
335   if (log)
336     log->Printf("Pushing the return address onto the stack: 0x%" PRIx64
337                 ": 0x%" PRIx64,
338                 (uint64_t)sp, (uint64_t)return_addr);
339
340   // Save return address onto the stack
341   if (!process_sp->WritePointerToMemory(sp, return_addr, error))
342     return false;
343
344   // %r1 is set to the actual stack value.
345
346   if (log)
347     log->Printf("Writing SP: 0x%" PRIx64, (uint64_t)sp);
348
349   if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))
350     return false;
351
352   // %pc is set to the address of the called function.
353
354   if (log)
355     log->Printf("Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
356
357   if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))
358     return false;
359
360   return true;
361 }
362
363 static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
364                                 bool is_signed, Thread &thread,
365                                 uint32_t *argument_register_ids,
366                                 unsigned int &current_argument_register,
367                                 addr_t &current_stack_argument) {
368   if (bit_width > 64)
369     return false; // Scalar can't hold large integer arguments
370
371   if (current_argument_register < 6) {
372     scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
373         argument_register_ids[current_argument_register], 0);
374     current_argument_register++;
375     if (is_signed)
376       scalar.SignExtend(bit_width);
377   } else {
378     uint32_t byte_size = (bit_width + (8 - 1)) / 8;
379     Error error;
380     if (thread.GetProcess()->ReadScalarIntegerFromMemory(
381             current_stack_argument, byte_size, is_signed, scalar, error)) {
382       current_stack_argument += byte_size;
383       return true;
384     }
385     return false;
386   }
387   return true;
388 }
389
390 bool ABISysV_ppc::GetArgumentValues(Thread &thread, ValueList &values) const {
391   unsigned int num_values = values.GetSize();
392   unsigned int value_index;
393
394   // Extract the register context so we can read arguments from registers
395
396   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
397
398   if (!reg_ctx)
399     return false;
400
401   // Get the pointer to the first stack argument so we have a place to start
402   // when reading data
403
404   addr_t sp = reg_ctx->GetSP(0);
405
406   if (!sp)
407     return false;
408
409   addr_t current_stack_argument = sp + 48; // jump over return address
410
411   uint32_t argument_register_ids[8];
412
413   argument_register_ids[0] =
414       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1)
415           ->kinds[eRegisterKindLLDB];
416   argument_register_ids[1] =
417       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2)
418           ->kinds[eRegisterKindLLDB];
419   argument_register_ids[2] =
420       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3)
421           ->kinds[eRegisterKindLLDB];
422   argument_register_ids[3] =
423       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4)
424           ->kinds[eRegisterKindLLDB];
425   argument_register_ids[4] =
426       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG5)
427           ->kinds[eRegisterKindLLDB];
428   argument_register_ids[5] =
429       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG6)
430           ->kinds[eRegisterKindLLDB];
431   argument_register_ids[6] =
432       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG7)
433           ->kinds[eRegisterKindLLDB];
434   argument_register_ids[7] =
435       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG8)
436           ->kinds[eRegisterKindLLDB];
437
438   unsigned int current_argument_register = 0;
439
440   for (value_index = 0; value_index < num_values; ++value_index) {
441     Value *value = values.GetValueAtIndex(value_index);
442
443     if (!value)
444       return false;
445
446     // We currently only support extracting values with Clang QualTypes.
447     // Do we care about others?
448     CompilerType compiler_type = value->GetCompilerType();
449     if (!compiler_type)
450       return false;
451     bool is_signed;
452
453     if (compiler_type.IsIntegerOrEnumerationType(is_signed)) {
454       ReadIntegerArgument(value->GetScalar(), compiler_type.GetBitSize(&thread),
455                           is_signed, thread, argument_register_ids,
456                           current_argument_register, current_stack_argument);
457     } else if (compiler_type.IsPointerType()) {
458       ReadIntegerArgument(value->GetScalar(), compiler_type.GetBitSize(&thread),
459                           false, thread, argument_register_ids,
460                           current_argument_register, current_stack_argument);
461     }
462   }
463
464   return true;
465 }
466
467 Error ABISysV_ppc::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
468                                         lldb::ValueObjectSP &new_value_sp) {
469   Error error;
470   if (!new_value_sp) {
471     error.SetErrorString("Empty value object for return value.");
472     return error;
473   }
474
475   CompilerType compiler_type = new_value_sp->GetCompilerType();
476   if (!compiler_type) {
477     error.SetErrorString("Null clang type for return value.");
478     return error;
479   }
480
481   Thread *thread = frame_sp->GetThread().get();
482
483   bool is_signed;
484   uint32_t count;
485   bool is_complex;
486
487   RegisterContext *reg_ctx = thread->GetRegisterContext().get();
488
489   bool set_it_simple = false;
490   if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
491       compiler_type.IsPointerType()) {
492     const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("r3", 0);
493
494     DataExtractor data;
495     Error data_error;
496     size_t num_bytes = new_value_sp->GetData(data, data_error);
497     if (data_error.Fail()) {
498       error.SetErrorStringWithFormat(
499           "Couldn't convert return value to raw data: %s",
500           data_error.AsCString());
501       return error;
502     }
503     lldb::offset_t offset = 0;
504     if (num_bytes <= 8) {
505       uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
506
507       if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))
508         set_it_simple = true;
509     } else {
510       error.SetErrorString("We don't support returning longer than 64 bit "
511                            "integer values at present.");
512     }
513   } else if (compiler_type.IsFloatingPointType(count, is_complex)) {
514     if (is_complex)
515       error.SetErrorString(
516           "We don't support returning complex values at present");
517     else {
518       size_t bit_width = compiler_type.GetBitSize(frame_sp.get());
519       if (bit_width <= 64) {
520         DataExtractor data;
521         Error data_error;
522         size_t num_bytes = new_value_sp->GetData(data, data_error);
523         if (data_error.Fail()) {
524           error.SetErrorStringWithFormat(
525               "Couldn't convert return value to raw data: %s",
526               data_error.AsCString());
527           return error;
528         }
529
530         unsigned char buffer[16];
531         ByteOrder byte_order = data.GetByteOrder();
532
533         data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order);
534         set_it_simple = true;
535       } else {
536         // FIXME - don't know how to do 80 bit long doubles yet.
537         error.SetErrorString(
538             "We don't support returning float values > 64 bits at present");
539       }
540     }
541   }
542
543   if (!set_it_simple) {
544     // Okay we've got a structure or something that doesn't fit in a simple
545     // register.
546     // We should figure out where it really goes, but we don't support this yet.
547     error.SetErrorString("We only support setting simple integer and float "
548                          "return types at present.");
549   }
550
551   return error;
552 }
553
554 ValueObjectSP ABISysV_ppc::GetReturnValueObjectSimple(
555     Thread &thread, CompilerType &return_compiler_type) const {
556   ValueObjectSP return_valobj_sp;
557   Value value;
558
559   if (!return_compiler_type)
560     return return_valobj_sp;
561
562   // value.SetContext (Value::eContextTypeClangType, return_value_type);
563   value.SetCompilerType(return_compiler_type);
564
565   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
566   if (!reg_ctx)
567     return return_valobj_sp;
568
569   const uint32_t type_flags = return_compiler_type.GetTypeInfo();
570   if (type_flags & eTypeIsScalar) {
571     value.SetValueType(Value::eValueTypeScalar);
572
573     bool success = false;
574     if (type_flags & eTypeIsInteger) {
575       // Extract the register context so we can read arguments from registers
576
577       const size_t byte_size = return_compiler_type.GetByteSize(nullptr);
578       uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
579           reg_ctx->GetRegisterInfoByName("r3", 0), 0);
580       const bool is_signed = (type_flags & eTypeIsSigned) != 0;
581       switch (byte_size) {
582       default:
583         break;
584
585       case sizeof(uint64_t):
586         if (is_signed)
587           value.GetScalar() = (int64_t)(raw_value);
588         else
589           value.GetScalar() = (uint64_t)(raw_value);
590         success = true;
591         break;
592
593       case sizeof(uint32_t):
594         if (is_signed)
595           value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
596         else
597           value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
598         success = true;
599         break;
600
601       case sizeof(uint16_t):
602         if (is_signed)
603           value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
604         else
605           value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
606         success = true;
607         break;
608
609       case sizeof(uint8_t):
610         if (is_signed)
611           value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
612         else
613           value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
614         success = true;
615         break;
616       }
617     } else if (type_flags & eTypeIsFloat) {
618       if (type_flags & eTypeIsComplex) {
619         // Don't handle complex yet.
620       } else {
621         const size_t byte_size = return_compiler_type.GetByteSize(nullptr);
622         if (byte_size <= sizeof(long double)) {
623           const RegisterInfo *f1_info = reg_ctx->GetRegisterInfoByName("f1", 0);
624           RegisterValue f1_value;
625           if (reg_ctx->ReadRegister(f1_info, f1_value)) {
626             DataExtractor data;
627             if (f1_value.GetData(data)) {
628               lldb::offset_t offset = 0;
629               if (byte_size == sizeof(float)) {
630                 value.GetScalar() = (float)data.GetFloat(&offset);
631                 success = true;
632               } else if (byte_size == sizeof(double)) {
633                 value.GetScalar() = (double)data.GetDouble(&offset);
634                 success = true;
635               }
636             }
637           }
638         }
639       }
640     }
641
642     if (success)
643       return_valobj_sp = ValueObjectConstResult::Create(
644           thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
645   } else if (type_flags & eTypeIsPointer) {
646     unsigned r3_id =
647         reg_ctx->GetRegisterInfoByName("r3", 0)->kinds[eRegisterKindLLDB];
648     value.GetScalar() =
649         (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(r3_id, 0);
650     value.SetValueType(Value::eValueTypeScalar);
651     return_valobj_sp = ValueObjectConstResult::Create(
652         thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
653   } else if (type_flags & eTypeIsVector) {
654     const size_t byte_size = return_compiler_type.GetByteSize(nullptr);
655     if (byte_size > 0) {
656       const RegisterInfo *altivec_reg = reg_ctx->GetRegisterInfoByName("v2", 0);
657       if (altivec_reg) {
658         if (byte_size <= altivec_reg->byte_size) {
659           ProcessSP process_sp(thread.GetProcess());
660           if (process_sp) {
661             std::unique_ptr<DataBufferHeap> heap_data_ap(
662                 new DataBufferHeap(byte_size, 0));
663             const ByteOrder byte_order = process_sp->GetByteOrder();
664             RegisterValue reg_value;
665             if (reg_ctx->ReadRegister(altivec_reg, reg_value)) {
666               Error error;
667               if (reg_value.GetAsMemoryData(
668                       altivec_reg, heap_data_ap->GetBytes(),
669                       heap_data_ap->GetByteSize(), byte_order, error)) {
670                 DataExtractor data(DataBufferSP(heap_data_ap.release()),
671                                    byte_order, process_sp->GetTarget()
672                                                    .GetArchitecture()
673                                                    .GetAddressByteSize());
674                 return_valobj_sp = ValueObjectConstResult::Create(
675                     &thread, return_compiler_type, ConstString(""), data);
676               }
677             }
678           }
679         }
680       }
681     }
682   }
683
684   return return_valobj_sp;
685 }
686
687 ValueObjectSP ABISysV_ppc::GetReturnValueObjectImpl(
688     Thread &thread, CompilerType &return_compiler_type) const {
689   ValueObjectSP return_valobj_sp;
690
691   if (!return_compiler_type)
692     return return_valobj_sp;
693
694   ExecutionContext exe_ctx(thread.shared_from_this());
695   return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type);
696   if (return_valobj_sp)
697     return return_valobj_sp;
698
699   RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
700   if (!reg_ctx_sp)
701     return return_valobj_sp;
702
703   const size_t bit_width = return_compiler_type.GetBitSize(&thread);
704   if (return_compiler_type.IsAggregateType()) {
705     Target *target = exe_ctx.GetTargetPtr();
706     bool is_memory = true;
707     if (bit_width <= 128) {
708       ByteOrder target_byte_order = target->GetArchitecture().GetByteOrder();
709       DataBufferSP data_sp(new DataBufferHeap(16, 0));
710       DataExtractor return_ext(data_sp, target_byte_order,
711                                target->GetArchitecture().GetAddressByteSize());
712
713       const RegisterInfo *r3_info = reg_ctx_sp->GetRegisterInfoByName("r3", 0);
714       const RegisterInfo *rdx_info =
715           reg_ctx_sp->GetRegisterInfoByName("rdx", 0);
716
717       RegisterValue r3_value, rdx_value;
718       reg_ctx_sp->ReadRegister(r3_info, r3_value);
719       reg_ctx_sp->ReadRegister(rdx_info, rdx_value);
720
721       DataExtractor r3_data, rdx_data;
722
723       r3_value.GetData(r3_data);
724       rdx_value.GetData(rdx_data);
725
726       uint32_t fp_bytes =
727           0; // Tracks how much of the xmm registers we've consumed so far
728       uint32_t integer_bytes =
729           0; // Tracks how much of the r3/rds registers we've consumed so far
730
731       const uint32_t num_children = return_compiler_type.GetNumFields();
732
733       // Since we are in the small struct regime, assume we are not in memory.
734       is_memory = false;
735
736       for (uint32_t idx = 0; idx < num_children; idx++) {
737         std::string name;
738         uint64_t field_bit_offset = 0;
739         bool is_signed;
740         bool is_complex;
741         uint32_t count;
742
743         CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex(
744             idx, name, &field_bit_offset, nullptr, nullptr);
745         const size_t field_bit_width = field_compiler_type.GetBitSize(&thread);
746
747         // If there are any unaligned fields, this is stored in memory.
748         if (field_bit_offset % field_bit_width != 0) {
749           is_memory = true;
750           break;
751         }
752
753         uint32_t field_byte_width = field_bit_width / 8;
754         uint32_t field_byte_offset = field_bit_offset / 8;
755
756         DataExtractor *copy_from_extractor = nullptr;
757         uint32_t copy_from_offset = 0;
758
759         if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
760             field_compiler_type.IsPointerType()) {
761           if (integer_bytes < 8) {
762             if (integer_bytes + field_byte_width <= 8) {
763               // This is in RAX, copy from register to our result structure:
764               copy_from_extractor = &r3_data;
765               copy_from_offset = integer_bytes;
766               integer_bytes += field_byte_width;
767             } else {
768               // The next field wouldn't fit in the remaining space, so we
769               // pushed it to rdx.
770               copy_from_extractor = &rdx_data;
771               copy_from_offset = 0;
772               integer_bytes = 8 + field_byte_width;
773             }
774           } else if (integer_bytes + field_byte_width <= 16) {
775             copy_from_extractor = &rdx_data;
776             copy_from_offset = integer_bytes - 8;
777             integer_bytes += field_byte_width;
778           } else {
779             // The last field didn't fit.  I can't see how that would happen w/o
780             // the overall size being
781             // greater than 16 bytes.  For now, return a nullptr return value
782             // object.
783             return return_valobj_sp;
784           }
785         } else if (field_compiler_type.IsFloatingPointType(count, is_complex)) {
786           // Structs with long doubles are always passed in memory.
787           if (field_bit_width == 128) {
788             is_memory = true;
789             break;
790           } else if (field_bit_width == 64) {
791             copy_from_offset = 0;
792             fp_bytes += field_byte_width;
793           } else if (field_bit_width == 32) {
794             // This one is kind of complicated.  If we are in an "eightbyte"
795             // with another float, we'll
796             // be stuffed into an xmm register with it.  If we are in an
797             // "eightbyte" with one or more ints,
798             // then we will be stuffed into the appropriate GPR with them.
799             bool in_gpr;
800             if (field_byte_offset % 8 == 0) {
801               // We are at the beginning of one of the eightbytes, so check the
802               // next element (if any)
803               if (idx == num_children - 1)
804                 in_gpr = false;
805               else {
806                 uint64_t next_field_bit_offset = 0;
807                 CompilerType next_field_compiler_type =
808                     return_compiler_type.GetFieldAtIndex(idx + 1, name,
809                                                          &next_field_bit_offset,
810                                                          nullptr, nullptr);
811                 if (next_field_compiler_type.IsIntegerOrEnumerationType(
812                         is_signed))
813                   in_gpr = true;
814                 else {
815                   copy_from_offset = 0;
816                   in_gpr = false;
817                 }
818               }
819             } else if (field_byte_offset % 4 == 0) {
820               // We are inside of an eightbyte, so see if the field before us is
821               // floating point:
822               // This could happen if somebody put padding in the structure.
823               if (idx == 0)
824                 in_gpr = false;
825               else {
826                 uint64_t prev_field_bit_offset = 0;
827                 CompilerType prev_field_compiler_type =
828                     return_compiler_type.GetFieldAtIndex(idx - 1, name,
829                                                          &prev_field_bit_offset,
830                                                          nullptr, nullptr);
831                 if (prev_field_compiler_type.IsIntegerOrEnumerationType(
832                         is_signed))
833                   in_gpr = true;
834                 else {
835                   copy_from_offset = 4;
836                   in_gpr = false;
837                 }
838               }
839             } else {
840               is_memory = true;
841               continue;
842             }
843
844             // Okay, we've figured out whether we are in GPR or XMM, now figure
845             // out which one.
846             if (in_gpr) {
847               if (integer_bytes < 8) {
848                 // This is in RAX, copy from register to our result structure:
849                 copy_from_extractor = &r3_data;
850                 copy_from_offset = integer_bytes;
851                 integer_bytes += field_byte_width;
852               } else {
853                 copy_from_extractor = &rdx_data;
854                 copy_from_offset = integer_bytes - 8;
855                 integer_bytes += field_byte_width;
856               }
857             } else {
858               fp_bytes += field_byte_width;
859             }
860           }
861         }
862
863         // These two tests are just sanity checks.  If I somehow get the
864         // type calculation wrong above it is better to just return nothing
865         // than to assert or crash.
866         if (!copy_from_extractor)
867           return return_valobj_sp;
868         if (copy_from_offset + field_byte_width >
869             copy_from_extractor->GetByteSize())
870           return return_valobj_sp;
871
872         copy_from_extractor->CopyByteOrderedData(
873             copy_from_offset, field_byte_width,
874             data_sp->GetBytes() + field_byte_offset, field_byte_width,
875             target_byte_order);
876       }
877
878       if (!is_memory) {
879         // The result is in our data buffer.  Let's make a variable object out
880         // of it:
881         return_valobj_sp = ValueObjectConstResult::Create(
882             &thread, return_compiler_type, ConstString(""), return_ext);
883       }
884     }
885
886     // FIXME: This is just taking a guess, r3 may very well no longer hold the
887     // return storage location.
888     // If we are going to do this right, when we make a new frame we should
889     // check to see if it uses a memory
890     // return, and if we are at the first instruction and if so stash away the
891     // return location.  Then we would
892     // only return the memory return value if we know it is valid.
893
894     if (is_memory) {
895       unsigned r3_id =
896           reg_ctx_sp->GetRegisterInfoByName("r3", 0)->kinds[eRegisterKindLLDB];
897       lldb::addr_t storage_addr =
898           (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(r3_id,
899                                                                         0);
900       return_valobj_sp = ValueObjectMemory::Create(
901           &thread, "", Address(storage_addr, nullptr), return_compiler_type);
902     }
903   }
904
905   return return_valobj_sp;
906 }
907
908 bool ABISysV_ppc::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
909   unwind_plan.Clear();
910   unwind_plan.SetRegisterKind(eRegisterKindDWARF);
911
912   uint32_t lr_reg_num = dwarf_lr;
913   uint32_t sp_reg_num = dwarf_r1;
914   uint32_t pc_reg_num = dwarf_pc;
915
916   UnwindPlan::RowSP row(new UnwindPlan::Row);
917
918   // Our Call Frame Address is the stack pointer value
919   row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 0);
920
921   // The previous PC is in the LR
922   row->SetRegisterLocationToRegister(pc_reg_num, lr_reg_num, true);
923   unwind_plan.AppendRow(row);
924
925   // All other registers are the same.
926
927   unwind_plan.SetSourceName("ppc at-func-entry default");
928   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
929
930   return true;
931 }
932
933 bool ABISysV_ppc::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
934   unwind_plan.Clear();
935   unwind_plan.SetRegisterKind(eRegisterKindDWARF);
936
937   uint32_t sp_reg_num = dwarf_r1;
938   uint32_t pc_reg_num = dwarf_lr;
939
940   UnwindPlan::RowSP row(new UnwindPlan::Row);
941
942   const int32_t ptr_size = 4;
943   row->GetCFAValue().SetIsRegisterDereferenced(sp_reg_num);
944
945   row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * 1, true);
946   row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
947
948   unwind_plan.AppendRow(row);
949   unwind_plan.SetSourceName("ppc default unwind plan");
950   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
951   unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
952   unwind_plan.SetReturnAddressRegister(dwarf_lr);
953   return true;
954 }
955
956 bool ABISysV_ppc::RegisterIsVolatile(const RegisterInfo *reg_info) {
957   return !RegisterIsCalleeSaved(reg_info);
958 }
959
960 // See "Register Usage" in the
961 // "System V Application Binary Interface"
962 // "64-bit PowerPC ELF Application Binary Interface Supplement"
963 // current version is 1.9 released 2004 at
964 // http://refspecs.linuxfoundation.org/ELF/ppc/PPC-elf64abi-1.9.pdf
965
966 bool ABISysV_ppc::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {
967   if (reg_info) {
968     // Preserved registers are :
969     //    r1,r2,r13-r31
970     //    f14-f31 (not yet)
971     //    v20-v31 (not yet)
972     //    vrsave (not yet)
973
974     const char *name = reg_info->name;
975     if (name[0] == 'r') {
976       if ((name[1] == '1' || name[1] == '2') && name[2] == '\0')
977         return true;
978       if (name[1] == '1' && name[2] > '2')
979         return true;
980       if ((name[1] == '2' || name[1] == '3') && name[2] != '\0')
981         return true;
982     }
983
984     if (name[0] == 'f' && name[1] >= '0' && name[1] <= '9') {
985       if (name[3] == '1' && name[4] >= '4')
986         return true;
987       if ((name[3] == '2' || name[3] == '3') && name[4] != '\0')
988         return true;
989     }
990
991     if (name[0] == 's' && name[1] == 'p' && name[2] == '\0') // sp
992       return true;
993     if (name[0] == 'f' && name[1] == 'p' && name[2] == '\0') // fp
994       return true;
995     if (name[0] == 'p' && name[1] == 'c' && name[2] == '\0') // pc
996       return true;
997   }
998   return false;
999 }
1000
1001 void ABISysV_ppc::Initialize() {
1002   PluginManager::RegisterPlugin(GetPluginNameStatic(),
1003                                 "System V ABI for ppc targets", CreateInstance);
1004 }
1005
1006 void ABISysV_ppc::Terminate() {
1007   PluginManager::UnregisterPlugin(CreateInstance);
1008 }
1009
1010 lldb_private::ConstString ABISysV_ppc::GetPluginNameStatic() {
1011   static ConstString g_name("sysv-ppc");
1012   return g_name;
1013 }
1014
1015 //------------------------------------------------------------------
1016 // PluginInterface protocol
1017 //------------------------------------------------------------------
1018
1019 lldb_private::ConstString ABISysV_ppc::GetPluginName() {
1020   return GetPluginNameStatic();
1021 }
1022
1023 uint32_t ABISysV_ppc::GetPluginVersion() { return 1; }