]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/ABI/SysV-s390x/ABISysV_s390x.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / ABI / SysV-s390x / ABISysV_s390x.cpp
1 //===-- ABISysV_s390x.cpp ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "ABISysV_s390x.h"
10
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/Triple.h"
13
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"
31
32 using namespace lldb;
33 using namespace lldb_private;
34
35 enum dwarf_regnums {
36   // General Purpose Registers
37   dwarf_r0_s390x = 0,
38   dwarf_r1_s390x,
39   dwarf_r2_s390x,
40   dwarf_r3_s390x,
41   dwarf_r4_s390x,
42   dwarf_r5_s390x,
43   dwarf_r6_s390x,
44   dwarf_r7_s390x,
45   dwarf_r8_s390x,
46   dwarf_r9_s390x,
47   dwarf_r10_s390x,
48   dwarf_r11_s390x,
49   dwarf_r12_s390x,
50   dwarf_r13_s390x,
51   dwarf_r14_s390x,
52   dwarf_r15_s390x,
53   // Floating Point Registers / Vector Registers 0-15
54   dwarf_f0_s390x = 16,
55   dwarf_f2_s390x,
56   dwarf_f4_s390x,
57   dwarf_f6_s390x,
58   dwarf_f1_s390x,
59   dwarf_f3_s390x,
60   dwarf_f5_s390x,
61   dwarf_f7_s390x,
62   dwarf_f8_s390x,
63   dwarf_f10_s390x,
64   dwarf_f12_s390x,
65   dwarf_f14_s390x,
66   dwarf_f9_s390x,
67   dwarf_f11_s390x,
68   dwarf_f13_s390x,
69   dwarf_f15_s390x,
70   // Access Registers
71   dwarf_acr0_s390x = 48,
72   dwarf_acr1_s390x,
73   dwarf_acr2_s390x,
74   dwarf_acr3_s390x,
75   dwarf_acr4_s390x,
76   dwarf_acr5_s390x,
77   dwarf_acr6_s390x,
78   dwarf_acr7_s390x,
79   dwarf_acr8_s390x,
80   dwarf_acr9_s390x,
81   dwarf_acr10_s390x,
82   dwarf_acr11_s390x,
83   dwarf_acr12_s390x,
84   dwarf_acr13_s390x,
85   dwarf_acr14_s390x,
86   dwarf_acr15_s390x,
87   // Program Status Word
88   dwarf_pswm_s390x = 64,
89   dwarf_pswa_s390x,
90   // Vector Registers 16-31
91   dwarf_v16_s390x = 68,
92   dwarf_v18_s390x,
93   dwarf_v20_s390x,
94   dwarf_v22_s390x,
95   dwarf_v17_s390x,
96   dwarf_v19_s390x,
97   dwarf_v21_s390x,
98   dwarf_v23_s390x,
99   dwarf_v24_s390x,
100   dwarf_v26_s390x,
101   dwarf_v28_s390x,
102   dwarf_v30_s390x,
103   dwarf_v25_s390x,
104   dwarf_v27_s390x,
105   dwarf_v29_s390x,
106   dwarf_v31_s390x,
107 };
108
109 // RegisterKind: EHFrame, DWARF, Generic, Process Plugin, LLDB
110
111 #define DEFINE_REG(name, size, alt, generic)                                   \
112   {                                                                            \
113     #name, alt, size, 0, eEncodingUint, eFormatHex,                            \
114         {dwarf_##name##_s390x, dwarf_##name##_s390x, generic,                  \
115          LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM },                           \
116          nullptr, nullptr, nullptr, 0                                          \
117   }
118
119 static RegisterInfo g_register_infos[] = {
120     DEFINE_REG(r0, 8, nullptr, LLDB_INVALID_REGNUM),
121     DEFINE_REG(r1, 8, nullptr, LLDB_INVALID_REGNUM),
122     DEFINE_REG(r2, 8, "arg1", LLDB_REGNUM_GENERIC_ARG1),
123     DEFINE_REG(r3, 8, "arg2", LLDB_REGNUM_GENERIC_ARG2),
124     DEFINE_REG(r4, 8, "arg3", LLDB_REGNUM_GENERIC_ARG3),
125     DEFINE_REG(r5, 8, "arg4", LLDB_REGNUM_GENERIC_ARG4),
126     DEFINE_REG(r6, 8, "arg5", LLDB_REGNUM_GENERIC_ARG5),
127     DEFINE_REG(r7, 8, nullptr, LLDB_INVALID_REGNUM),
128     DEFINE_REG(r8, 8, nullptr, LLDB_INVALID_REGNUM),
129     DEFINE_REG(r9, 8, nullptr, LLDB_INVALID_REGNUM),
130     DEFINE_REG(r10, 8, nullptr, LLDB_INVALID_REGNUM),
131     DEFINE_REG(r11, 8, "fp", LLDB_REGNUM_GENERIC_FP),
132     DEFINE_REG(r12, 8, nullptr, LLDB_INVALID_REGNUM),
133     DEFINE_REG(r13, 8, nullptr, LLDB_INVALID_REGNUM),
134     DEFINE_REG(r14, 8, nullptr, LLDB_INVALID_REGNUM),
135     DEFINE_REG(r15, 8, "sp", LLDB_REGNUM_GENERIC_SP),
136     DEFINE_REG(acr0, 4, nullptr, LLDB_INVALID_REGNUM),
137     DEFINE_REG(acr1, 4, nullptr, LLDB_INVALID_REGNUM),
138     DEFINE_REG(acr2, 4, nullptr, LLDB_INVALID_REGNUM),
139     DEFINE_REG(acr3, 4, nullptr, LLDB_INVALID_REGNUM),
140     DEFINE_REG(acr4, 4, nullptr, LLDB_INVALID_REGNUM),
141     DEFINE_REG(acr5, 4, nullptr, LLDB_INVALID_REGNUM),
142     DEFINE_REG(acr6, 4, nullptr, LLDB_INVALID_REGNUM),
143     DEFINE_REG(acr7, 4, nullptr, LLDB_INVALID_REGNUM),
144     DEFINE_REG(acr8, 4, nullptr, LLDB_INVALID_REGNUM),
145     DEFINE_REG(acr9, 4, nullptr, LLDB_INVALID_REGNUM),
146     DEFINE_REG(acr10, 4, nullptr, LLDB_INVALID_REGNUM),
147     DEFINE_REG(acr11, 4, nullptr, LLDB_INVALID_REGNUM),
148     DEFINE_REG(acr12, 4, nullptr, LLDB_INVALID_REGNUM),
149     DEFINE_REG(acr13, 4, nullptr, LLDB_INVALID_REGNUM),
150     DEFINE_REG(acr14, 4, nullptr, LLDB_INVALID_REGNUM),
151     DEFINE_REG(acr15, 4, nullptr, LLDB_INVALID_REGNUM),
152     DEFINE_REG(pswm, 8, "flags", LLDB_REGNUM_GENERIC_FLAGS),
153     DEFINE_REG(pswa, 8, "pc", LLDB_REGNUM_GENERIC_PC),
154     DEFINE_REG(f0, 8, nullptr, LLDB_INVALID_REGNUM),
155     DEFINE_REG(f1, 8, nullptr, LLDB_INVALID_REGNUM),
156     DEFINE_REG(f2, 8, nullptr, LLDB_INVALID_REGNUM),
157     DEFINE_REG(f3, 8, nullptr, LLDB_INVALID_REGNUM),
158     DEFINE_REG(f4, 8, nullptr, LLDB_INVALID_REGNUM),
159     DEFINE_REG(f5, 8, nullptr, LLDB_INVALID_REGNUM),
160     DEFINE_REG(f6, 8, nullptr, LLDB_INVALID_REGNUM),
161     DEFINE_REG(f7, 8, nullptr, LLDB_INVALID_REGNUM),
162     DEFINE_REG(f8, 8, nullptr, LLDB_INVALID_REGNUM),
163     DEFINE_REG(f9, 8, nullptr, LLDB_INVALID_REGNUM),
164     DEFINE_REG(f10, 8, nullptr, LLDB_INVALID_REGNUM),
165     DEFINE_REG(f11, 8, nullptr, LLDB_INVALID_REGNUM),
166     DEFINE_REG(f12, 8, nullptr, LLDB_INVALID_REGNUM),
167     DEFINE_REG(f13, 8, nullptr, LLDB_INVALID_REGNUM),
168     DEFINE_REG(f14, 8, nullptr, LLDB_INVALID_REGNUM),
169     DEFINE_REG(f15, 8, nullptr, LLDB_INVALID_REGNUM),
170 };
171
172 static const uint32_t k_num_register_infos =
173     llvm::array_lengthof(g_register_infos);
174 static bool g_register_info_names_constified = false;
175
176 const lldb_private::RegisterInfo *
177 ABISysV_s390x::GetRegisterInfoArray(uint32_t &count) {
178   // Make the C-string names and alt_names for the register infos into const
179   // C-string values by having the ConstString unique the names in the global
180   // constant C-string pool.
181   if (!g_register_info_names_constified) {
182     g_register_info_names_constified = true;
183     for (uint32_t i = 0; i < k_num_register_infos; ++i) {
184       if (g_register_infos[i].name)
185         g_register_infos[i].name =
186             ConstString(g_register_infos[i].name).GetCString();
187       if (g_register_infos[i].alt_name)
188         g_register_infos[i].alt_name =
189             ConstString(g_register_infos[i].alt_name).GetCString();
190     }
191   }
192   count = k_num_register_infos;
193   return g_register_infos;
194 }
195
196 size_t ABISysV_s390x::GetRedZoneSize() const { return 0; }
197
198 // Static Functions
199
200 ABISP
201 ABISysV_s390x::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) {
202   if (arch.GetTriple().getArch() == llvm::Triple::systemz) {
203     return ABISP(new ABISysV_s390x(process_sp));
204   }
205   return ABISP();
206 }
207
208 bool ABISysV_s390x::PrepareTrivialCall(Thread &thread, addr_t sp,
209                                        addr_t func_addr, addr_t return_addr,
210                                        llvm::ArrayRef<addr_t> args) const {
211   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
212
213   if (log) {
214     StreamString s;
215     s.Printf("ABISysV_s390x::PrepareTrivialCall (tid = 0x%" PRIx64
216              ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
217              ", return_addr = 0x%" PRIx64,
218              thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
219              (uint64_t)return_addr);
220
221     for (size_t i = 0; i < args.size(); ++i)
222       s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
223                args[i]);
224     s.PutCString(")");
225     log->PutString(s.GetString());
226   }
227
228   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
229   if (!reg_ctx)
230     return false;
231
232   const RegisterInfo *pc_reg_info =
233       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
234   const RegisterInfo *sp_reg_info =
235       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
236   const RegisterInfo *ra_reg_info = reg_ctx->GetRegisterInfoByName("r14", 0);
237   ProcessSP process_sp(thread.GetProcess());
238
239   // Allocate a new stack frame and space for stack arguments if necessary
240
241   addr_t arg_pos = 0;
242   if (args.size() > 5) {
243     sp -= 8 * (args.size() - 5);
244     arg_pos = sp;
245   }
246
247   sp -= 160;
248
249   // Process arguments
250
251   for (size_t i = 0; i < args.size(); ++i) {
252     if (i < 5) {
253       const RegisterInfo *reg_info = reg_ctx->GetRegisterInfo(
254           eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1 + i);
255       if (log)
256         log->Printf("About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s",
257                     static_cast<uint64_t>(i + 1), args[i], reg_info->name);
258       if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i]))
259         return false;
260     } else {
261       Status error;
262       if (log)
263         log->Printf("About to write arg%" PRIu64 " (0x%" PRIx64 ") onto stack",
264                     static_cast<uint64_t>(i + 1), args[i]);
265       if (!process_sp->WritePointerToMemory(arg_pos, args[i], error))
266         return false;
267       arg_pos += 8;
268     }
269   }
270
271   // %r14 is set to the return address
272
273   if (log)
274     log->Printf("Writing RA: 0x%" PRIx64, (uint64_t)return_addr);
275
276   if (!reg_ctx->WriteRegisterFromUnsigned(ra_reg_info, return_addr))
277     return false;
278
279   // %r15 is set to the actual stack value.
280
281   if (log)
282     log->Printf("Writing SP: 0x%" PRIx64, (uint64_t)sp);
283
284   if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))
285     return false;
286
287   // %pc is set to the address of the called function.
288
289   if (log)
290     log->Printf("Writing PC: 0x%" PRIx64, (uint64_t)func_addr);
291
292   if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))
293     return false;
294
295   return true;
296 }
297
298 static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
299                                 bool is_signed, Thread &thread,
300                                 uint32_t *argument_register_ids,
301                                 unsigned int &current_argument_register,
302                                 addr_t &current_stack_argument) {
303   if (bit_width > 64)
304     return false; // Scalar can't hold large integer arguments
305
306   if (current_argument_register < 5) {
307     scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
308         argument_register_ids[current_argument_register], 0);
309     current_argument_register++;
310     if (is_signed)
311       scalar.SignExtend(bit_width);
312   } else {
313     uint32_t byte_size = (bit_width + (8 - 1)) / 8;
314     Status error;
315     if (thread.GetProcess()->ReadScalarIntegerFromMemory(
316             current_stack_argument + 8 - byte_size, byte_size, is_signed,
317             scalar, error)) {
318       current_stack_argument += 8;
319       return true;
320     }
321     return false;
322   }
323   return true;
324 }
325
326 bool ABISysV_s390x::GetArgumentValues(Thread &thread, ValueList &values) const {
327   unsigned int num_values = values.GetSize();
328   unsigned int value_index;
329
330   // Extract the register context so we can read arguments from registers
331
332   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
333
334   if (!reg_ctx)
335     return false;
336
337   // Get the pointer to the first stack argument so we have a place to start
338   // when reading data
339
340   addr_t sp = reg_ctx->GetSP(0);
341
342   if (!sp)
343     return false;
344
345   addr_t current_stack_argument = sp + 160;
346
347   uint32_t argument_register_ids[5];
348
349   argument_register_ids[0] =
350       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1)
351           ->kinds[eRegisterKindLLDB];
352   argument_register_ids[1] =
353       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2)
354           ->kinds[eRegisterKindLLDB];
355   argument_register_ids[2] =
356       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3)
357           ->kinds[eRegisterKindLLDB];
358   argument_register_ids[3] =
359       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4)
360           ->kinds[eRegisterKindLLDB];
361   argument_register_ids[4] =
362       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG5)
363           ->kinds[eRegisterKindLLDB];
364
365   unsigned int current_argument_register = 0;
366
367   for (value_index = 0; value_index < num_values; ++value_index) {
368     Value *value = values.GetValueAtIndex(value_index);
369
370     if (!value)
371       return false;
372
373     // We currently only support extracting values with Clang QualTypes. Do we
374     // care about others?
375     CompilerType compiler_type = value->GetCompilerType();
376     llvm::Optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread);
377     if (!bit_size)
378       return false;
379     bool is_signed;
380
381     if (compiler_type.IsIntegerOrEnumerationType(is_signed)) {
382       ReadIntegerArgument(value->GetScalar(), *bit_size, is_signed, thread,
383                           argument_register_ids, current_argument_register,
384                           current_stack_argument);
385     } else if (compiler_type.IsPointerType()) {
386       ReadIntegerArgument(value->GetScalar(), *bit_size, false, thread,
387                           argument_register_ids, current_argument_register,
388                           current_stack_argument);
389     }
390   }
391
392   return true;
393 }
394
395 Status ABISysV_s390x::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
396                                            lldb::ValueObjectSP &new_value_sp) {
397   Status error;
398   if (!new_value_sp) {
399     error.SetErrorString("Empty value object for return value.");
400     return error;
401   }
402
403   CompilerType compiler_type = new_value_sp->GetCompilerType();
404   if (!compiler_type) {
405     error.SetErrorString("Null clang type for return value.");
406     return error;
407   }
408
409   Thread *thread = frame_sp->GetThread().get();
410
411   bool is_signed;
412   uint32_t count;
413   bool is_complex;
414
415   RegisterContext *reg_ctx = thread->GetRegisterContext().get();
416
417   bool set_it_simple = false;
418   if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
419       compiler_type.IsPointerType()) {
420     const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("r2", 0);
421
422     DataExtractor data;
423     Status data_error;
424     size_t num_bytes = new_value_sp->GetData(data, data_error);
425     if (data_error.Fail()) {
426       error.SetErrorStringWithFormat(
427           "Couldn't convert return value to raw data: %s",
428           data_error.AsCString());
429       return error;
430     }
431     lldb::offset_t offset = 0;
432     if (num_bytes <= 8) {
433       uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
434
435       if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))
436         set_it_simple = true;
437     } else {
438       error.SetErrorString("We don't support returning longer than 64 bit "
439                            "integer values at present.");
440     }
441   } else if (compiler_type.IsFloatingPointType(count, is_complex)) {
442     if (is_complex)
443       error.SetErrorString(
444           "We don't support returning complex values at present");
445     else {
446       llvm::Optional<uint64_t> bit_width =
447           compiler_type.GetBitSize(frame_sp.get());
448       if (!bit_width) {
449         error.SetErrorString("can't get type size");
450         return error;
451       }
452       if (*bit_width <= 64) {
453         const RegisterInfo *f0_info = reg_ctx->GetRegisterInfoByName("f0", 0);
454         RegisterValue f0_value;
455         DataExtractor data;
456         Status data_error;
457         size_t num_bytes = new_value_sp->GetData(data, data_error);
458         if (data_error.Fail()) {
459           error.SetErrorStringWithFormat(
460               "Couldn't convert return value to raw data: %s",
461               data_error.AsCString());
462           return error;
463         }
464
465         unsigned char buffer[8];
466         ByteOrder byte_order = data.GetByteOrder();
467
468         data.CopyByteOrderedData(0, num_bytes, buffer, 8, byte_order);
469         f0_value.SetBytes(buffer, 8, byte_order);
470         reg_ctx->WriteRegister(f0_info, f0_value);
471         set_it_simple = true;
472       } else {
473         // FIXME - don't know how to do long doubles yet.
474         error.SetErrorString(
475             "We don't support returning float values > 64 bits at present");
476       }
477     }
478   }
479
480   if (!set_it_simple) {
481     // Okay we've got a structure or something that doesn't fit in a simple
482     // register. We should figure out where it really goes, but we don't
483     // support this yet.
484     error.SetErrorString("We only support setting simple integer and float "
485                          "return types at present.");
486   }
487
488   return error;
489 }
490
491 ValueObjectSP ABISysV_s390x::GetReturnValueObjectSimple(
492     Thread &thread, CompilerType &return_compiler_type) const {
493   ValueObjectSP return_valobj_sp;
494   Value value;
495
496   if (!return_compiler_type)
497     return return_valobj_sp;
498
499   // value.SetContext (Value::eContextTypeClangType, return_value_type);
500   value.SetCompilerType(return_compiler_type);
501
502   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
503   if (!reg_ctx)
504     return return_valobj_sp;
505
506   const uint32_t type_flags = return_compiler_type.GetTypeInfo();
507   if (type_flags & eTypeIsScalar) {
508     value.SetValueType(Value::eValueTypeScalar);
509
510     bool success = false;
511     if (type_flags & eTypeIsInteger) {
512       // Extract the register context so we can read arguments from registers.
513       llvm::Optional<uint64_t> byte_size =
514           return_compiler_type.GetByteSize(nullptr);
515       if (!byte_size)
516         return return_valobj_sp;
517       uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
518           reg_ctx->GetRegisterInfoByName("r2", 0), 0);
519       const bool is_signed = (type_flags & eTypeIsSigned) != 0;
520       switch (*byte_size) {
521       default:
522         break;
523
524       case sizeof(uint64_t):
525         if (is_signed)
526           value.GetScalar() = (int64_t)(raw_value);
527         else
528           value.GetScalar() = (uint64_t)(raw_value);
529         success = true;
530         break;
531
532       case sizeof(uint32_t):
533         if (is_signed)
534           value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
535         else
536           value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
537         success = true;
538         break;
539
540       case sizeof(uint16_t):
541         if (is_signed)
542           value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
543         else
544           value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
545         success = true;
546         break;
547
548       case sizeof(uint8_t):
549         if (is_signed)
550           value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
551         else
552           value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
553         success = true;
554         break;
555       }
556     } else if (type_flags & eTypeIsFloat) {
557       if (type_flags & eTypeIsComplex) {
558         // Don't handle complex yet.
559       } else {
560         llvm::Optional<uint64_t> byte_size =
561             return_compiler_type.GetByteSize(nullptr);
562         if (byte_size && *byte_size <= sizeof(long double)) {
563           const RegisterInfo *f0_info = reg_ctx->GetRegisterInfoByName("f0", 0);
564           RegisterValue f0_value;
565           if (reg_ctx->ReadRegister(f0_info, f0_value)) {
566             DataExtractor data;
567             if (f0_value.GetData(data)) {
568               lldb::offset_t offset = 0;
569               if (*byte_size == sizeof(float)) {
570                 value.GetScalar() = (float)data.GetFloat(&offset);
571                 success = true;
572               } else if (*byte_size == sizeof(double)) {
573                 value.GetScalar() = (double)data.GetDouble(&offset);
574                 success = true;
575               } else if (*byte_size == sizeof(long double)) {
576                 // Don't handle long double yet.
577               }
578             }
579           }
580         }
581       }
582     }
583
584     if (success)
585       return_valobj_sp = ValueObjectConstResult::Create(
586           thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
587   } else if (type_flags & eTypeIsPointer) {
588     unsigned r2_id =
589         reg_ctx->GetRegisterInfoByName("r2", 0)->kinds[eRegisterKindLLDB];
590     value.GetScalar() =
591         (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(r2_id, 0);
592     value.SetValueType(Value::eValueTypeScalar);
593     return_valobj_sp = ValueObjectConstResult::Create(
594         thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
595   }
596
597   return return_valobj_sp;
598 }
599
600 ValueObjectSP ABISysV_s390x::GetReturnValueObjectImpl(
601     Thread &thread, CompilerType &return_compiler_type) const {
602   ValueObjectSP return_valobj_sp;
603
604   if (!return_compiler_type)
605     return return_valobj_sp;
606
607   ExecutionContext exe_ctx(thread.shared_from_this());
608   return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type);
609   if (return_valobj_sp)
610     return return_valobj_sp;
611
612   RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
613   if (!reg_ctx_sp)
614     return return_valobj_sp;
615
616   if (return_compiler_type.IsAggregateType()) {
617     // FIXME: This is just taking a guess, r2 may very well no longer hold the
618     // return storage location.
619     // If we are going to do this right, when we make a new frame we should
620     // check to see if it uses a memory return, and if we are at the first
621     // instruction and if so stash away the return location.  Then we would
622     // only return the memory return value if we know it is valid.
623
624     unsigned r2_id =
625         reg_ctx_sp->GetRegisterInfoByName("r2", 0)->kinds[eRegisterKindLLDB];
626     lldb::addr_t storage_addr =
627         (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(r2_id, 0);
628     return_valobj_sp = ValueObjectMemory::Create(
629         &thread, "", Address(storage_addr, nullptr), return_compiler_type);
630   }
631
632   return return_valobj_sp;
633 }
634
635 bool ABISysV_s390x::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
636   unwind_plan.Clear();
637   unwind_plan.SetRegisterKind(eRegisterKindDWARF);
638
639   UnwindPlan::RowSP row(new UnwindPlan::Row);
640
641   // Our Call Frame Address is the stack pointer value + 160
642   row->GetCFAValue().SetIsRegisterPlusOffset(dwarf_r15_s390x, 160);
643
644   // The previous PC is in r14
645   row->SetRegisterLocationToRegister(dwarf_pswa_s390x, dwarf_r14_s390x, true);
646
647   // All other registers are the same.
648   unwind_plan.AppendRow(row);
649   unwind_plan.SetSourceName("s390x at-func-entry default");
650   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
651   return true;
652 }
653
654 bool ABISysV_s390x::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
655   // There's really no default way to unwind on s390x. Trust the .eh_frame CFI,
656   // which should always be good.
657   return false;
658 }
659
660 bool ABISysV_s390x::GetFallbackRegisterLocation(
661     const RegisterInfo *reg_info,
662     UnwindPlan::Row::RegisterLocation &unwind_regloc) {
663   // If a volatile register is being requested, we don't want to forward the
664   // next frame's register contents up the stack -- the register is not
665   // retrievable at this frame.
666   if (RegisterIsVolatile(reg_info)) {
667     unwind_regloc.SetUndefined();
668     return true;
669   }
670
671   return false;
672 }
673
674 bool ABISysV_s390x::RegisterIsVolatile(const RegisterInfo *reg_info) {
675   return !RegisterIsCalleeSaved(reg_info);
676 }
677
678 bool ABISysV_s390x::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {
679   if (reg_info) {
680     // Preserved registers are :
681     //    r6-r13, r15
682     //    f8-f15
683
684     const char *name = reg_info->name;
685     if (name[0] == 'r') {
686       switch (name[1]) {
687       case '6': // r6
688       case '7': // r7
689       case '8': // r8
690       case '9': // r9
691         return name[2] == '\0';
692
693       case '1': // r10, r11, r12, r13, r15
694         if ((name[2] >= '0' && name[2] <= '3') || name[2] == '5')
695           return name[3] == '\0';
696         break;
697
698       default:
699         break;
700       }
701     }
702     if (name[0] == 'f') {
703       switch (name[1]) {
704       case '8': // r8
705       case '9': // r9
706         return name[2] == '\0';
707
708       case '1': // r10, r11, r12, r13, r14, r15
709         if (name[2] >= '0' && name[2] <= '5')
710           return name[3] == '\0';
711         break;
712
713       default:
714         break;
715       }
716     }
717
718     // Accept shorter-variant versions
719     if (name[0] == 's' && name[1] == 'p' && name[2] == '\0') // sp
720       return true;
721     if (name[0] == 'f' && name[1] == 'p' && name[2] == '\0') // fp
722       return true;
723     if (name[0] == 'p' && name[1] == 'c' && name[2] == '\0') // pc
724       return true;
725   }
726   return false;
727 }
728
729 void ABISysV_s390x::Initialize() {
730   PluginManager::RegisterPlugin(
731       GetPluginNameStatic(), "System V ABI for s390x targets", CreateInstance);
732 }
733
734 void ABISysV_s390x::Terminate() {
735   PluginManager::UnregisterPlugin(CreateInstance);
736 }
737
738 lldb_private::ConstString ABISysV_s390x::GetPluginNameStatic() {
739   static ConstString g_name("sysv-s390x");
740   return g_name;
741 }
742
743 // PluginInterface protocol
744
745 lldb_private::ConstString ABISysV_s390x::GetPluginName() {
746   return GetPluginNameStatic();
747 }
748
749 uint32_t ABISysV_s390x::GetPluginVersion() { return 1; }