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