180814287SRaphael Isemann //===-- Scalar.cpp --------------------------------------------------------===//
2d821c997SPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d821c997SPavel Labath //
7d821c997SPavel Labath //===----------------------------------------------------------------------===//
8d821c997SPavel Labath 
9d821c997SPavel Labath #include "lldb/Utility/Scalar.h"
10d0fa52ccSPavel Labath #include "lldb/Utility/DataBufferHeap.h"
11d821c997SPavel Labath #include "lldb/Utility/DataExtractor.h"
12d821c997SPavel Labath #include "lldb/Utility/Endian.h"
13d821c997SPavel Labath #include "lldb/Utility/Status.h"
14d821c997SPavel Labath #include "lldb/Utility/Stream.h"
15b07a7997SPavel Labath #include "lldb/Utility/StreamString.h"
16672d2c12SJonas Devlieghere #include "lldb/lldb-types.h"
178270a903SPavel Labath #include "llvm/ADT/APSInt.h"
18d821c997SPavel Labath #include "llvm/ADT/SmallString.h"
19d821c997SPavel Labath 
20d821c997SPavel Labath #include <cinttypes>
21d821c997SPavel Labath #include <cstdio>
22d821c997SPavel Labath 
23d821c997SPavel Labath using namespace lldb;
24d821c997SPavel Labath using namespace lldb_private;
25d821c997SPavel Labath 
26d821c997SPavel Labath // Promote to max type currently follows the ANSI C rule for type promotion in
27d821c997SPavel Labath // expressions.
28d821c997SPavel Labath static Scalar::Type PromoteToMaxType(
29d821c997SPavel Labath     const Scalar &lhs,  // The const left hand side object
30d821c997SPavel Labath     const Scalar &rhs,  // The const right hand side object
31d821c997SPavel Labath     Scalar &temp_value, // A modifiable temp value than can be used to hold
32d821c997SPavel Labath                         // either the promoted lhs or rhs object
33d821c997SPavel Labath     const Scalar *&promoted_lhs_ptr, // Pointer to the resulting possibly
34d821c997SPavel Labath                                      // promoted value of lhs (at most one of
35d821c997SPavel Labath                                      // lhs/rhs will get promoted)
36d821c997SPavel Labath     const Scalar *&promoted_rhs_ptr  // Pointer to the resulting possibly
37d821c997SPavel Labath                                      // promoted value of rhs (at most one of
38d821c997SPavel Labath                                      // lhs/rhs will get promoted)
39d821c997SPavel Labath ) {
40d821c997SPavel Labath   Scalar result;
41d821c997SPavel Labath   // Initialize the promoted values for both the right and left hand side
42d821c997SPavel Labath   // values to be the objects themselves. If no promotion is needed (both right
43d821c997SPavel Labath   // and left have the same type), then the temp_value will not get used.
44d821c997SPavel Labath   promoted_lhs_ptr = &lhs;
45d821c997SPavel Labath   promoted_rhs_ptr = &rhs;
46d821c997SPavel Labath   // Extract the types of both the right and left hand side values
47d821c997SPavel Labath   Scalar::Type lhs_type = lhs.GetType();
48d821c997SPavel Labath   Scalar::Type rhs_type = rhs.GetType();
49d821c997SPavel Labath 
50d821c997SPavel Labath   if (lhs_type > rhs_type) {
51d821c997SPavel Labath     // Right hand side need to be promoted
52d821c997SPavel Labath     temp_value = rhs; // Copy right hand side into the temp value
53d821c997SPavel Labath     if (temp_value.Promote(lhs_type)) // Promote it
54d821c997SPavel Labath       promoted_rhs_ptr =
55d821c997SPavel Labath           &temp_value; // Update the pointer for the promoted right hand side
56d821c997SPavel Labath   } else if (lhs_type < rhs_type) {
57d821c997SPavel Labath     // Left hand side need to be promoted
58d821c997SPavel Labath     temp_value = lhs; // Copy left hand side value into the temp value
59d821c997SPavel Labath     if (temp_value.Promote(rhs_type)) // Promote it
60d821c997SPavel Labath       promoted_lhs_ptr =
61d821c997SPavel Labath           &temp_value; // Update the pointer for the promoted left hand side
62d821c997SPavel Labath   }
63d821c997SPavel Labath 
64d821c997SPavel Labath   // Make sure our type promotion worked as expected
65d821c997SPavel Labath   if (promoted_lhs_ptr->GetType() == promoted_rhs_ptr->GetType())
66d821c997SPavel Labath     return promoted_lhs_ptr->GetType(); // Return the resulting max type
67d821c997SPavel Labath 
68d821c997SPavel Labath   // Return the void type (zero) if we fail to promote either of the values.
69d821c997SPavel Labath   return Scalar::e_void;
70d821c997SPavel Labath }
71d821c997SPavel Labath 
7224374aefSJonas Devlieghere Scalar::Scalar() : m_type(e_void), m_float(static_cast<float>(0)) {}
73d821c997SPavel Labath 
74d821c997SPavel Labath bool Scalar::GetData(DataExtractor &data, size_t limit_byte_size) const {
75d821c997SPavel Labath   size_t byte_size = GetByteSize();
76d0fa52ccSPavel Labath   if (byte_size == 0) {
77d0fa52ccSPavel Labath     data.Clear();
78d0fa52ccSPavel Labath     return false;
79d0fa52ccSPavel Labath   }
80d0fa52ccSPavel Labath   auto buffer_up = std::make_unique<DataBufferHeap>(byte_size, 0);
81d0fa52ccSPavel Labath   GetBytes(buffer_up->GetData());
82d0fa52ccSPavel Labath   lldb::offset_t offset = 0;
83d821c997SPavel Labath 
84d821c997SPavel Labath   if (limit_byte_size < byte_size) {
85d821c997SPavel Labath     if (endian::InlHostByteOrder() == eByteOrderLittle) {
86d821c997SPavel Labath       // On little endian systems if we want fewer bytes from the current
87d821c997SPavel Labath       // type we just specify fewer bytes since the LSByte is first...
88d821c997SPavel Labath       byte_size = limit_byte_size;
89d821c997SPavel Labath     } else if (endian::InlHostByteOrder() == eByteOrderBig) {
90d821c997SPavel Labath       // On big endian systems if we want fewer bytes from the current type
91d821c997SPavel Labath       // have to advance our initial byte pointer and trim down the number of
92d821c997SPavel Labath       // bytes since the MSByte is first
93d0fa52ccSPavel Labath       offset = byte_size - limit_byte_size;
94d821c997SPavel Labath       byte_size = limit_byte_size;
95d821c997SPavel Labath     }
96d821c997SPavel Labath   }
97d821c997SPavel Labath 
98d0fa52ccSPavel Labath   data.SetData(std::move(buffer_up), offset, byte_size);
99d0fa52ccSPavel Labath   data.SetByteOrder(endian::InlHostByteOrder());
100d821c997SPavel Labath   return true;
101d821c997SPavel Labath }
102d821c997SPavel Labath 
103d0fa52ccSPavel Labath void Scalar::GetBytes(llvm::MutableArrayRef<uint8_t> storage) const {
104d0fa52ccSPavel Labath   assert(storage.size() >= GetByteSize());
105d0fa52ccSPavel Labath 
106d821c997SPavel Labath   switch (m_type) {
107d821c997SPavel Labath   case e_void:
108d821c997SPavel Labath     break;
109d821c997SPavel Labath   case e_sint:
110d821c997SPavel Labath   case e_uint:
111d821c997SPavel Labath   case e_slong:
112d821c997SPavel Labath   case e_ulong:
113d821c997SPavel Labath   case e_slonglong:
114d821c997SPavel Labath   case e_ulonglong:
115d821c997SPavel Labath   case e_sint128:
116d821c997SPavel Labath   case e_uint128:
117d821c997SPavel Labath   case e_sint256:
118d821c997SPavel Labath   case e_uint256:
11951d46bd4SDavide Italiano   case e_sint512:
12051d46bd4SDavide Italiano   case e_uint512:
121d0fa52ccSPavel Labath     StoreIntToMemory(m_integer, storage.data(),
122d0fa52ccSPavel Labath                      (m_integer.getBitWidth() + 7) / 8);
123d0fa52ccSPavel Labath     break;
124d0fa52ccSPavel Labath   case e_float: {
125d0fa52ccSPavel Labath     float val = m_float.convertToFloat();
126d0fa52ccSPavel Labath     memcpy(storage.data(), &val, sizeof(val));
127d0fa52ccSPavel Labath     break;
12851d46bd4SDavide Italiano   }
129d0fa52ccSPavel Labath   case e_double: {
130d0fa52ccSPavel Labath     double val = m_float.convertToDouble();
131d0fa52ccSPavel Labath     memcpy(storage.data(), &val, sizeof(double));
132d0fa52ccSPavel Labath     break;
133d821c997SPavel Labath   }
134d0fa52ccSPavel Labath   case e_long_double: {
135d0fa52ccSPavel Labath     llvm::APInt val = m_float.bitcastToAPInt();
136d0fa52ccSPavel Labath     StoreIntToMemory(val, storage.data(), storage.size());
137d0fa52ccSPavel Labath     break;
138d821c997SPavel Labath   }
139d0fa52ccSPavel Labath   }
140d821c997SPavel Labath }
141d821c997SPavel Labath 
142d821c997SPavel Labath size_t Scalar::GetByteSize() const {
143d821c997SPavel Labath   switch (m_type) {
144d821c997SPavel Labath   case e_void:
145d821c997SPavel Labath     break;
146d821c997SPavel Labath   case e_sint:
147d821c997SPavel Labath   case e_uint:
148d821c997SPavel Labath   case e_slong:
149d821c997SPavel Labath   case e_ulong:
150d821c997SPavel Labath   case e_slonglong:
151d821c997SPavel Labath   case e_ulonglong:
152d821c997SPavel Labath   case e_sint128:
153d821c997SPavel Labath   case e_uint128:
154d821c997SPavel Labath   case e_sint256:
155d821c997SPavel Labath   case e_uint256:
15651d46bd4SDavide Italiano   case e_sint512:
15751d46bd4SDavide Italiano   case e_uint512:
158d821c997SPavel Labath     return (m_integer.getBitWidth() / 8);
159d821c997SPavel Labath   case e_float:
160d821c997SPavel Labath     return sizeof(float_t);
161d821c997SPavel Labath   case e_double:
162d821c997SPavel Labath     return sizeof(double_t);
163d821c997SPavel Labath   case e_long_double:
164d821c997SPavel Labath     return sizeof(long_double_t);
165d821c997SPavel Labath   }
166d821c997SPavel Labath   return 0;
167d821c997SPavel Labath }
168d821c997SPavel Labath 
169d821c997SPavel Labath bool Scalar::IsZero() const {
170d821c997SPavel Labath   llvm::APInt zero_int = llvm::APInt::getNullValue(m_integer.getBitWidth() / 8);
171d821c997SPavel Labath   switch (m_type) {
172d821c997SPavel Labath   case e_void:
173d821c997SPavel Labath     break;
174d821c997SPavel Labath   case e_sint:
175d821c997SPavel Labath   case e_uint:
176d821c997SPavel Labath   case e_slong:
177d821c997SPavel Labath   case e_ulong:
178d821c997SPavel Labath   case e_slonglong:
179d821c997SPavel Labath   case e_ulonglong:
180d821c997SPavel Labath   case e_sint128:
181d821c997SPavel Labath   case e_uint128:
182d821c997SPavel Labath   case e_sint256:
183d821c997SPavel Labath   case e_uint256:
18451d46bd4SDavide Italiano   case e_uint512:
18551d46bd4SDavide Italiano   case e_sint512:
186d821c997SPavel Labath     return llvm::APInt::isSameValue(zero_int, m_integer);
187d821c997SPavel Labath   case e_float:
188d821c997SPavel Labath   case e_double:
189d821c997SPavel Labath   case e_long_double:
190d821c997SPavel Labath     return m_float.isZero();
191d821c997SPavel Labath   }
192d821c997SPavel Labath   return false;
193d821c997SPavel Labath }
194d821c997SPavel Labath 
195d821c997SPavel Labath void Scalar::GetValue(Stream *s, bool show_type) const {
196d821c997SPavel Labath   if (show_type)
197d821c997SPavel Labath     s->Printf("(%s) ", GetTypeAsCString());
198d821c997SPavel Labath 
199d821c997SPavel Labath   switch (m_type) {
200d821c997SPavel Labath   case e_void:
201d821c997SPavel Labath     break;
202d821c997SPavel Labath   case e_sint:
203d821c997SPavel Labath   case e_slong:
204d821c997SPavel Labath   case e_slonglong:
205d821c997SPavel Labath   case e_sint128:
206d821c997SPavel Labath   case e_sint256:
20751d46bd4SDavide Italiano   case e_sint512:
208d821c997SPavel Labath     s->PutCString(m_integer.toString(10, true));
209d821c997SPavel Labath     break;
210d821c997SPavel Labath   case e_uint:
211d821c997SPavel Labath   case e_ulong:
212d821c997SPavel Labath   case e_ulonglong:
213d821c997SPavel Labath   case e_uint128:
214d821c997SPavel Labath   case e_uint256:
21551d46bd4SDavide Italiano   case e_uint512:
216d821c997SPavel Labath     s->PutCString(m_integer.toString(10, false));
217d821c997SPavel Labath     break;
218d821c997SPavel Labath   case e_float:
219d821c997SPavel Labath   case e_double:
220d821c997SPavel Labath   case e_long_double:
221d821c997SPavel Labath     llvm::SmallString<24> string;
222d821c997SPavel Labath     m_float.toString(string);
223d821c997SPavel Labath     s->Printf("%s", string.c_str());
224d821c997SPavel Labath     break;
225d821c997SPavel Labath   }
226d821c997SPavel Labath }
227d821c997SPavel Labath 
228d821c997SPavel Labath const char *Scalar::GetTypeAsCString() const {
229d821c997SPavel Labath   switch (m_type) {
230d821c997SPavel Labath   case e_void:
231d821c997SPavel Labath     return "void";
232d821c997SPavel Labath   case e_sint:
233d821c997SPavel Labath     return "int";
234d821c997SPavel Labath   case e_uint:
235d821c997SPavel Labath     return "unsigned int";
236d821c997SPavel Labath   case e_slong:
237d821c997SPavel Labath     return "long";
238d821c997SPavel Labath   case e_ulong:
239d821c997SPavel Labath     return "unsigned long";
240d821c997SPavel Labath   case e_slonglong:
241d821c997SPavel Labath     return "long long";
242d821c997SPavel Labath   case e_ulonglong:
243d821c997SPavel Labath     return "unsigned long long";
244d821c997SPavel Labath   case e_sint128:
245d821c997SPavel Labath     return "int128_t";
246d821c997SPavel Labath   case e_uint128:
247d821c997SPavel Labath     return "unsigned int128_t";
248d821c997SPavel Labath   case e_sint256:
249d821c997SPavel Labath     return "int256_t";
250d821c997SPavel Labath   case e_uint256:
251d821c997SPavel Labath     return "unsigned int256_t";
25251d46bd4SDavide Italiano   case e_sint512:
25351d46bd4SDavide Italiano     return "int512_t";
25451d46bd4SDavide Italiano   case e_uint512:
25551d46bd4SDavide Italiano     return "unsigned int512_t";
256d821c997SPavel Labath   case e_float:
257d821c997SPavel Labath     return "float";
258d821c997SPavel Labath   case e_double:
259d821c997SPavel Labath     return "double";
260d821c997SPavel Labath   case e_long_double:
261d821c997SPavel Labath     return "long double";
262d821c997SPavel Labath   }
263d821c997SPavel Labath   return "<invalid Scalar type>";
264d821c997SPavel Labath }
265d821c997SPavel Labath 
266d821c997SPavel Labath Scalar::~Scalar() = default;
267d821c997SPavel Labath 
2689b23df63SAdrian Prantl Scalar::Type Scalar::GetBestTypeForBitSize(size_t bit_size, bool sign) {
2699b23df63SAdrian Prantl   // Scalar types are always host types, hence the sizeof().
2709b23df63SAdrian Prantl   if (sign) {
2719b23df63SAdrian Prantl     if (bit_size <= sizeof(int)*8) return Scalar::e_sint;
2729b23df63SAdrian Prantl     if (bit_size <= sizeof(long)*8) return Scalar::e_slong;
2739b23df63SAdrian Prantl     if (bit_size <= sizeof(long long)*8) return Scalar::e_slonglong;
2749b23df63SAdrian Prantl     if (bit_size <= 128) return Scalar::e_sint128;
2759b23df63SAdrian Prantl     if (bit_size <= 256) return Scalar::e_sint256;
2769b23df63SAdrian Prantl     if (bit_size <= 512) return Scalar::e_sint512;
2779b23df63SAdrian Prantl   } else {
2789b23df63SAdrian Prantl     if (bit_size <= sizeof(unsigned int)*8) return Scalar::e_uint;
2799b23df63SAdrian Prantl     if (bit_size <= sizeof(unsigned long)*8) return Scalar::e_ulong;
2809b23df63SAdrian Prantl     if (bit_size <= sizeof(unsigned long long)*8) return Scalar::e_ulonglong;
2819b23df63SAdrian Prantl     if (bit_size <= 128) return Scalar::e_uint128;
2829b23df63SAdrian Prantl     if (bit_size <= 256) return Scalar::e_uint256;
2839b23df63SAdrian Prantl     if (bit_size <= 512) return Scalar::e_uint512;
2849b23df63SAdrian Prantl   }
2859b23df63SAdrian Prantl   return Scalar::e_void;
286799d61f2SMartin Storsjo }
2879b23df63SAdrian Prantl 
28816e17ca1SPavel Labath void Scalar::TruncOrExtendTo(uint16_t bits, bool sign) {
28916e17ca1SPavel Labath   m_integer = sign ? m_integer.sextOrTrunc(bits) : m_integer.zextOrTrunc(bits);
29016e17ca1SPavel Labath   m_type = GetBestTypeForBitSize(bits, sign);
2919b23df63SAdrian Prantl }
2929b23df63SAdrian Prantl 
293ce275d30SPavel Labath static size_t GetBitSize(Scalar::Type type) {
294ce275d30SPavel Labath   switch (type) {
295ce275d30SPavel Labath   case Scalar::e_void:
296ce275d30SPavel Labath     return 0;
297ce275d30SPavel Labath   case Scalar::e_sint:
298ce275d30SPavel Labath     return 8 * sizeof(int);
299ce275d30SPavel Labath   case Scalar::e_uint:
300ce275d30SPavel Labath     return 8 * sizeof(unsigned int);
301ce275d30SPavel Labath   case Scalar::e_slong:
302ce275d30SPavel Labath     return 8 * sizeof(long);
303ce275d30SPavel Labath   case Scalar::e_ulong:
304ce275d30SPavel Labath     return 8 * sizeof(unsigned long);
305ce275d30SPavel Labath   case Scalar::e_slonglong:
306ce275d30SPavel Labath     return 8 * sizeof(long long);
307ce275d30SPavel Labath   case Scalar::e_ulonglong:
308ce275d30SPavel Labath     return 8 * sizeof(unsigned long long);
309ce275d30SPavel Labath   case Scalar::e_sint128:
310ce275d30SPavel Labath   case Scalar::e_uint128:
311ce275d30SPavel Labath     return BITWIDTH_INT128;
312ce275d30SPavel Labath   case Scalar::e_sint256:
313ce275d30SPavel Labath   case Scalar::e_uint256:
314ce275d30SPavel Labath     return BITWIDTH_INT256;
315ce275d30SPavel Labath   case Scalar::e_sint512:
316ce275d30SPavel Labath   case Scalar::e_uint512:
317ce275d30SPavel Labath     return BITWIDTH_INT512;
318ce275d30SPavel Labath   case Scalar::e_float:
319ce275d30SPavel Labath     return 8 * sizeof(float);
320ce275d30SPavel Labath   case Scalar::e_double:
321ce275d30SPavel Labath     return 8 * sizeof(double);
322ce275d30SPavel Labath   case Scalar::e_long_double:
323ce275d30SPavel Labath     return 8 * sizeof(long double);
324ce275d30SPavel Labath   }
325ce275d30SPavel Labath   llvm_unreachable("Unhandled type!");
326ce275d30SPavel Labath }
327ce275d30SPavel Labath 
328ce275d30SPavel Labath static bool IsSigned(Scalar::Type type) {
329ce275d30SPavel Labath   switch (type) {
330ce275d30SPavel Labath   case Scalar::e_void:
331ce275d30SPavel Labath   case Scalar::e_uint:
332ce275d30SPavel Labath   case Scalar::e_ulong:
333ce275d30SPavel Labath   case Scalar::e_ulonglong:
334ce275d30SPavel Labath   case Scalar::e_uint128:
335ce275d30SPavel Labath   case Scalar::e_uint256:
336ce275d30SPavel Labath   case Scalar::e_uint512:
337ce275d30SPavel Labath     return false;
338ce275d30SPavel Labath   case Scalar::e_sint:
339ce275d30SPavel Labath   case Scalar::e_slong:
340ce275d30SPavel Labath   case Scalar::e_slonglong:
341ce275d30SPavel Labath   case Scalar::e_sint128:
342ce275d30SPavel Labath   case Scalar::e_sint256:
343ce275d30SPavel Labath   case Scalar::e_sint512:
344ce275d30SPavel Labath   case Scalar::e_float:
345ce275d30SPavel Labath   case Scalar::e_double:
346ce275d30SPavel Labath   case Scalar::e_long_double:
347ce275d30SPavel Labath     return true;
348ce275d30SPavel Labath   }
349ce275d30SPavel Labath   llvm_unreachable("Unhandled type!");
350ce275d30SPavel Labath }
351ce275d30SPavel Labath 
352ce275d30SPavel Labath namespace {
353ce275d30SPavel Labath enum class Category { Void, Integral, Float };
354ce275d30SPavel Labath }
355ce275d30SPavel Labath 
356ce275d30SPavel Labath static Category GetCategory(Scalar::Type type) {
357ce275d30SPavel Labath   switch (type) {
358ce275d30SPavel Labath   case Scalar::e_void:
359ce275d30SPavel Labath     return Category::Void;
360ce275d30SPavel Labath   case Scalar::e_float:
361ce275d30SPavel Labath   case Scalar::e_double:
362ce275d30SPavel Labath   case Scalar::e_long_double:
363ce275d30SPavel Labath     return Category::Float;
364ce275d30SPavel Labath   case Scalar::e_sint:
365ce275d30SPavel Labath   case Scalar::e_slong:
366ce275d30SPavel Labath   case Scalar::e_slonglong:
367ce275d30SPavel Labath   case Scalar::e_sint128:
368ce275d30SPavel Labath   case Scalar::e_sint256:
369ce275d30SPavel Labath   case Scalar::e_sint512:
370ce275d30SPavel Labath   case Scalar::e_uint:
371ce275d30SPavel Labath   case Scalar::e_ulong:
372ce275d30SPavel Labath   case Scalar::e_ulonglong:
373ce275d30SPavel Labath   case Scalar::e_uint128:
374ce275d30SPavel Labath   case Scalar::e_uint256:
375ce275d30SPavel Labath   case Scalar::e_uint512:
376ce275d30SPavel Labath     return Category::Integral;
377ce275d30SPavel Labath   }
378ce275d30SPavel Labath   llvm_unreachable("Unhandled type!");
379ce275d30SPavel Labath }
380ce275d30SPavel Labath 
381ce275d30SPavel Labath static const llvm::fltSemantics &GetFltSemantics(Scalar::Type type) {
382ce275d30SPavel Labath   switch (type) {
383ce275d30SPavel Labath   case Scalar::e_void:
384ce275d30SPavel Labath   case Scalar::e_sint:
385ce275d30SPavel Labath   case Scalar::e_slong:
386ce275d30SPavel Labath   case Scalar::e_slonglong:
387ce275d30SPavel Labath   case Scalar::e_sint128:
388ce275d30SPavel Labath   case Scalar::e_sint256:
389ce275d30SPavel Labath   case Scalar::e_sint512:
390ce275d30SPavel Labath   case Scalar::e_uint:
391ce275d30SPavel Labath   case Scalar::e_ulong:
392ce275d30SPavel Labath   case Scalar::e_ulonglong:
393ce275d30SPavel Labath   case Scalar::e_uint128:
394ce275d30SPavel Labath   case Scalar::e_uint256:
395ce275d30SPavel Labath   case Scalar::e_uint512:
396ce275d30SPavel Labath     llvm_unreachable("Only floating point types supported!");
397ce275d30SPavel Labath   case Scalar::e_float:
398ce275d30SPavel Labath     return llvm::APFloat::IEEEsingle();
399ce275d30SPavel Labath   case Scalar::e_double:
400ce275d30SPavel Labath     return llvm::APFloat::IEEEdouble();
401ce275d30SPavel Labath   case Scalar::e_long_double:
402ce275d30SPavel Labath     return llvm::APFloat::x87DoubleExtended();
403ce275d30SPavel Labath   }
404ce275d30SPavel Labath   llvm_unreachable("Unhandled type!");
405ce275d30SPavel Labath }
406ce275d30SPavel Labath 
407d821c997SPavel Labath bool Scalar::Promote(Scalar::Type type) {
408d821c997SPavel Labath   bool success = false;
409ce275d30SPavel Labath   switch (GetCategory(m_type)) {
410ce275d30SPavel Labath   case Category::Void:
411d821c997SPavel Labath     break;
412ce275d30SPavel Labath   case Category::Integral:
413ce275d30SPavel Labath     switch (GetCategory(type)) {
414ce275d30SPavel Labath     case Category::Void:
415d821c997SPavel Labath       break;
416ce275d30SPavel Labath     case Category::Integral:
417ce275d30SPavel Labath       if (type < m_type)
418ce275d30SPavel Labath         break;
419d821c997SPavel Labath       success = true;
420ce275d30SPavel Labath       if (IsSigned(m_type))
421ce275d30SPavel Labath         m_integer = m_integer.sextOrTrunc(GetBitSize(type));
422ce275d30SPavel Labath       else
423ce275d30SPavel Labath         m_integer = m_integer.zextOrTrunc(GetBitSize(type));
424d821c997SPavel Labath       break;
425ce275d30SPavel Labath     case Category::Float:
426ce275d30SPavel Labath       m_float = llvm::APFloat(GetFltSemantics(type));
427ce275d30SPavel Labath       m_float.convertFromAPInt(m_integer, IsSigned(m_type),
428d821c997SPavel Labath                                llvm::APFloat::rmNearestTiesToEven);
429d821c997SPavel Labath       success = true;
430d821c997SPavel Labath       break;
431d821c997SPavel Labath     }
432d821c997SPavel Labath     break;
433ce275d30SPavel Labath   case Category::Float:
434ce275d30SPavel Labath     switch (GetCategory(type)) {
435ce275d30SPavel Labath     case Category::Void:
436ce275d30SPavel Labath     case Category::Integral:
437d821c997SPavel Labath       break;
438ce275d30SPavel Labath     case Category::Float:
439ce275d30SPavel Labath       if (type < m_type)
440d821c997SPavel Labath         break;
441d821c997SPavel Labath       bool ignore;
442d821c997SPavel Labath       success = true;
443ce275d30SPavel Labath       m_float.convert(GetFltSemantics(type), llvm::APFloat::rmNearestTiesToEven,
444ce275d30SPavel Labath                       &ignore);
445d821c997SPavel Labath     }
446d821c997SPavel Labath   }
447d821c997SPavel Labath 
448d821c997SPavel Labath   if (success)
449d821c997SPavel Labath     m_type = type;
450d821c997SPavel Labath   return success;
451d821c997SPavel Labath }
452d821c997SPavel Labath 
453d821c997SPavel Labath const char *Scalar::GetValueTypeAsCString(Scalar::Type type) {
454d821c997SPavel Labath   switch (type) {
455d821c997SPavel Labath   case e_void:
456d821c997SPavel Labath     return "void";
457d821c997SPavel Labath   case e_sint:
458d821c997SPavel Labath     return "int";
459d821c997SPavel Labath   case e_uint:
460d821c997SPavel Labath     return "unsigned int";
461d821c997SPavel Labath   case e_slong:
462d821c997SPavel Labath     return "long";
463d821c997SPavel Labath   case e_ulong:
464d821c997SPavel Labath     return "unsigned long";
465d821c997SPavel Labath   case e_slonglong:
466d821c997SPavel Labath     return "long long";
467d821c997SPavel Labath   case e_ulonglong:
468d821c997SPavel Labath     return "unsigned long long";
469d821c997SPavel Labath   case e_float:
470d821c997SPavel Labath     return "float";
471d821c997SPavel Labath   case e_double:
472d821c997SPavel Labath     return "double";
473d821c997SPavel Labath   case e_long_double:
474d821c997SPavel Labath     return "long double";
475d821c997SPavel Labath   case e_sint128:
476d821c997SPavel Labath     return "int128_t";
477d821c997SPavel Labath   case e_uint128:
478d821c997SPavel Labath     return "uint128_t";
479d821c997SPavel Labath   case e_sint256:
480d821c997SPavel Labath     return "int256_t";
481d821c997SPavel Labath   case e_uint256:
482d821c997SPavel Labath     return "uint256_t";
48351d46bd4SDavide Italiano   case e_sint512:
48451d46bd4SDavide Italiano     return "int512_t";
48551d46bd4SDavide Italiano   case e_uint512:
48651d46bd4SDavide Italiano     return "uint512_t";
487d821c997SPavel Labath   }
488d821c997SPavel Labath   return "???";
489d821c997SPavel Labath }
490d821c997SPavel Labath 
491d821c997SPavel Labath Scalar::Type
492d821c997SPavel Labath Scalar::GetValueTypeForSignedIntegerWithByteSize(size_t byte_size) {
493d821c997SPavel Labath   if (byte_size <= sizeof(sint_t))
494d821c997SPavel Labath     return e_sint;
495d821c997SPavel Labath   if (byte_size <= sizeof(slong_t))
496d821c997SPavel Labath     return e_slong;
497d821c997SPavel Labath   if (byte_size <= sizeof(slonglong_t))
498d821c997SPavel Labath     return e_slonglong;
499d821c997SPavel Labath   return e_void;
500d821c997SPavel Labath }
501d821c997SPavel Labath 
502d821c997SPavel Labath Scalar::Type
503d821c997SPavel Labath Scalar::GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size) {
504d821c997SPavel Labath   if (byte_size <= sizeof(uint_t))
505d821c997SPavel Labath     return e_uint;
506d821c997SPavel Labath   if (byte_size <= sizeof(ulong_t))
507d821c997SPavel Labath     return e_ulong;
508d821c997SPavel Labath   if (byte_size <= sizeof(ulonglong_t))
509d821c997SPavel Labath     return e_ulonglong;
510d821c997SPavel Labath   return e_void;
511d821c997SPavel Labath }
512d821c997SPavel Labath 
513d821c997SPavel Labath Scalar::Type Scalar::GetValueTypeForFloatWithByteSize(size_t byte_size) {
514d821c997SPavel Labath   if (byte_size == sizeof(float_t))
515d821c997SPavel Labath     return e_float;
516d821c997SPavel Labath   if (byte_size == sizeof(double_t))
517d821c997SPavel Labath     return e_double;
518d821c997SPavel Labath   if (byte_size == sizeof(long_double_t))
519d821c997SPavel Labath     return e_long_double;
520d821c997SPavel Labath   return e_void;
521d821c997SPavel Labath }
522d821c997SPavel Labath 
523d821c997SPavel Labath bool Scalar::MakeSigned() {
524d821c997SPavel Labath   bool success = false;
525d821c997SPavel Labath 
526d821c997SPavel Labath   switch (m_type) {
527d821c997SPavel Labath   case e_void:
528d821c997SPavel Labath     break;
529d821c997SPavel Labath   case e_sint:
530d821c997SPavel Labath     success = true;
531d821c997SPavel Labath     break;
532d821c997SPavel Labath   case e_uint:
533d821c997SPavel Labath     m_type = e_sint;
534d821c997SPavel Labath     success = true;
535d821c997SPavel Labath     break;
536d821c997SPavel Labath   case e_slong:
537d821c997SPavel Labath     success = true;
538d821c997SPavel Labath     break;
539d821c997SPavel Labath   case e_ulong:
540d821c997SPavel Labath     m_type = e_slong;
541d821c997SPavel Labath     success = true;
542d821c997SPavel Labath     break;
543d821c997SPavel Labath   case e_slonglong:
544d821c997SPavel Labath     success = true;
545d821c997SPavel Labath     break;
546d821c997SPavel Labath   case e_ulonglong:
547d821c997SPavel Labath     m_type = e_slonglong;
548d821c997SPavel Labath     success = true;
549d821c997SPavel Labath     break;
550d821c997SPavel Labath   case e_sint128:
551d821c997SPavel Labath     success = true;
552d821c997SPavel Labath     break;
553d821c997SPavel Labath   case e_uint128:
554d821c997SPavel Labath     m_type = e_sint128;
555d821c997SPavel Labath     success = true;
556d821c997SPavel Labath     break;
557d821c997SPavel Labath   case e_sint256:
558d821c997SPavel Labath     success = true;
559d821c997SPavel Labath     break;
560d821c997SPavel Labath   case e_uint256:
561d821c997SPavel Labath     m_type = e_sint256;
562d821c997SPavel Labath     success = true;
563d821c997SPavel Labath     break;
56451d46bd4SDavide Italiano   case e_sint512:
56551d46bd4SDavide Italiano     success = true;
56651d46bd4SDavide Italiano     break;
56751d46bd4SDavide Italiano   case e_uint512:
56851d46bd4SDavide Italiano     m_type = e_sint512;
56951d46bd4SDavide Italiano     success = true;
57051d46bd4SDavide Italiano     break;
571d821c997SPavel Labath   case e_float:
572d821c997SPavel Labath     success = true;
573d821c997SPavel Labath     break;
574d821c997SPavel Labath   case e_double:
575d821c997SPavel Labath     success = true;
576d821c997SPavel Labath     break;
577d821c997SPavel Labath   case e_long_double:
578d821c997SPavel Labath     success = true;
579d821c997SPavel Labath     break;
580d821c997SPavel Labath   }
581d821c997SPavel Labath 
582d821c997SPavel Labath   return success;
583d821c997SPavel Labath }
584d821c997SPavel Labath 
585d821c997SPavel Labath bool Scalar::MakeUnsigned() {
586d821c997SPavel Labath   bool success = false;
587d821c997SPavel Labath 
588d821c997SPavel Labath   switch (m_type) {
589d821c997SPavel Labath   case e_void:
590d821c997SPavel Labath     break;
591d821c997SPavel Labath   case e_sint:
592d821c997SPavel Labath     m_type = e_uint;
593d821c997SPavel Labath     success = true;
594d821c997SPavel Labath     break;
595d821c997SPavel Labath   case e_uint:
596d821c997SPavel Labath     success = true;
597d821c997SPavel Labath     break;
598d821c997SPavel Labath   case e_slong:
599d821c997SPavel Labath     m_type = e_ulong;
600d821c997SPavel Labath     success = true;
601d821c997SPavel Labath     break;
602d821c997SPavel Labath   case e_ulong:
603d821c997SPavel Labath     success = true;
604d821c997SPavel Labath     break;
605d821c997SPavel Labath   case e_slonglong:
606d821c997SPavel Labath     m_type = e_ulonglong;
607d821c997SPavel Labath     success = true;
608d821c997SPavel Labath     break;
609d821c997SPavel Labath   case e_ulonglong:
610d821c997SPavel Labath     success = true;
611d821c997SPavel Labath     break;
612d821c997SPavel Labath   case e_sint128:
613d821c997SPavel Labath     m_type = e_uint128;
614d821c997SPavel Labath     success = true;
615d821c997SPavel Labath     break;
616d821c997SPavel Labath   case e_uint128:
617d821c997SPavel Labath     success = true;
618d821c997SPavel Labath     break;
619d821c997SPavel Labath   case e_sint256:
620d821c997SPavel Labath     m_type = e_uint256;
621d821c997SPavel Labath     success = true;
622d821c997SPavel Labath     break;
623d821c997SPavel Labath   case e_uint256:
624d821c997SPavel Labath     success = true;
625d821c997SPavel Labath     break;
62651d46bd4SDavide Italiano   case e_sint512:
62751d46bd4SDavide Italiano     m_type = e_uint512;
62851d46bd4SDavide Italiano     success = true;
62951d46bd4SDavide Italiano     break;
63051d46bd4SDavide Italiano   case e_uint512:
63151d46bd4SDavide Italiano     success = true;
63251d46bd4SDavide Italiano     break;
633d821c997SPavel Labath   case e_float:
634d821c997SPavel Labath     success = true;
635d821c997SPavel Labath     break;
636d821c997SPavel Labath   case e_double:
637d821c997SPavel Labath     success = true;
638d821c997SPavel Labath     break;
639d821c997SPavel Labath   case e_long_double:
640d821c997SPavel Labath     success = true;
641d821c997SPavel Labath     break;
642d821c997SPavel Labath   }
643d821c997SPavel Labath 
644d821c997SPavel Labath   return success;
645d821c997SPavel Labath }
646d821c997SPavel Labath 
647*b725142cSPavel Labath template <typename T> T Scalar::GetAs(T fail_value) const {
6488270a903SPavel Labath   switch (GetCategory(m_type)) {
6498270a903SPavel Labath   case Category::Void:
650d821c997SPavel Labath     break;
6518270a903SPavel Labath   case Category::Integral:
652*b725142cSPavel Labath     if (IsSigned(m_type))
65348ca1559SPavel Labath       return m_integer.sextOrTrunc(sizeof(T) * 8).getSExtValue();
654*b725142cSPavel Labath     return m_integer.zextOrTrunc(sizeof(T) * 8).getZExtValue();
6558270a903SPavel Labath   case Category::Float: {
656*b725142cSPavel Labath     llvm::APSInt result(sizeof(T) * 8, std::is_unsigned<T>::value);
6578270a903SPavel Labath     bool isExact;
6588270a903SPavel Labath     m_float.convertToInteger(result, llvm::APFloat::rmTowardZero, &isExact);
6598270a903SPavel Labath     return result.getSExtValue();
6608270a903SPavel Labath   }
661d821c997SPavel Labath   }
662d821c997SPavel Labath   return fail_value;
663d821c997SPavel Labath }
664d821c997SPavel Labath 
66548ca1559SPavel Labath signed char Scalar::SChar(signed char fail_value) const {
666*b725142cSPavel Labath   return GetAs<signed char>(fail_value);
66748ca1559SPavel Labath }
66848ca1559SPavel Labath 
66948ca1559SPavel Labath unsigned char Scalar::UChar(unsigned char fail_value) const {
670*b725142cSPavel Labath   return GetAs<unsigned char>(fail_value);
67148ca1559SPavel Labath }
67248ca1559SPavel Labath 
673d821c997SPavel Labath short Scalar::SShort(short fail_value) const {
674*b725142cSPavel Labath   return GetAs<short>(fail_value);
675d821c997SPavel Labath }
676d821c997SPavel Labath 
677d821c997SPavel Labath unsigned short Scalar::UShort(unsigned short fail_value) const {
678*b725142cSPavel Labath   return GetAs<unsigned short>(fail_value);
679d821c997SPavel Labath }
680d821c997SPavel Labath 
681*b725142cSPavel Labath int Scalar::SInt(int fail_value) const { return GetAs<int>(fail_value); }
682d821c997SPavel Labath 
683d821c997SPavel Labath unsigned int Scalar::UInt(unsigned int fail_value) const {
684*b725142cSPavel Labath   return GetAs<unsigned int>(fail_value);
685d821c997SPavel Labath }
686d821c997SPavel Labath 
687*b725142cSPavel Labath long Scalar::SLong(long fail_value) const { return GetAs<long>(fail_value); }
688d821c997SPavel Labath 
689d821c997SPavel Labath unsigned long Scalar::ULong(unsigned long fail_value) const {
690*b725142cSPavel Labath   return GetAs<unsigned long>(fail_value);
691d821c997SPavel Labath }
692d821c997SPavel Labath 
693d821c997SPavel Labath long long Scalar::SLongLong(long long fail_value) const {
694*b725142cSPavel Labath   return GetAs<long long>(fail_value);
695d821c997SPavel Labath }
696d821c997SPavel Labath 
697d821c997SPavel Labath unsigned long long Scalar::ULongLong(unsigned long long fail_value) const {
698*b725142cSPavel Labath   return GetAs<unsigned long long>(fail_value);
699d821c997SPavel Labath }
700d821c997SPavel Labath 
70148ca1559SPavel Labath llvm::APInt Scalar::SInt128(const llvm::APInt &fail_value) const {
702d821c997SPavel Labath   switch (m_type) {
703d821c997SPavel Labath   case e_void:
704d821c997SPavel Labath     break;
705d821c997SPavel Labath   case e_sint:
706d821c997SPavel Labath   case e_uint:
707d821c997SPavel Labath   case e_slong:
708d821c997SPavel Labath   case e_ulong:
709d821c997SPavel Labath   case e_slonglong:
710d821c997SPavel Labath   case e_ulonglong:
711d821c997SPavel Labath   case e_sint128:
712d821c997SPavel Labath   case e_uint128:
713d821c997SPavel Labath   case e_sint256:
714d821c997SPavel Labath   case e_uint256:
71551d46bd4SDavide Italiano   case e_sint512:
71651d46bd4SDavide Italiano   case e_uint512:
717d821c997SPavel Labath     return m_integer;
718d821c997SPavel Labath   case e_float:
719d821c997SPavel Labath   case e_double:
720d821c997SPavel Labath   case e_long_double:
721d821c997SPavel Labath     return m_float.bitcastToAPInt();
722d821c997SPavel Labath   }
723d821c997SPavel Labath   return fail_value;
724d821c997SPavel Labath }
725d821c997SPavel Labath 
726d821c997SPavel Labath llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const {
727d821c997SPavel Labath   switch (m_type) {
728d821c997SPavel Labath   case e_void:
729d821c997SPavel Labath     break;
730d821c997SPavel Labath   case e_sint:
731d821c997SPavel Labath   case e_uint:
732d821c997SPavel Labath   case e_slong:
733d821c997SPavel Labath   case e_ulong:
734d821c997SPavel Labath   case e_slonglong:
735d821c997SPavel Labath   case e_ulonglong:
736d821c997SPavel Labath   case e_sint128:
737d821c997SPavel Labath   case e_uint128:
738d821c997SPavel Labath   case e_sint256:
739d821c997SPavel Labath   case e_uint256:
74051d46bd4SDavide Italiano   case e_sint512:
74151d46bd4SDavide Italiano   case e_uint512:
742d821c997SPavel Labath     return m_integer;
743d821c997SPavel Labath   case e_float:
744d821c997SPavel Labath   case e_double:
745d821c997SPavel Labath   case e_long_double:
746d821c997SPavel Labath     return m_float.bitcastToAPInt();
747d821c997SPavel Labath   }
748d821c997SPavel Labath   return fail_value;
749d821c997SPavel Labath }
750d821c997SPavel Labath 
751d821c997SPavel Labath float Scalar::Float(float fail_value) const {
752d821c997SPavel Labath   switch (m_type) {
753d821c997SPavel Labath   case e_void:
754d821c997SPavel Labath     break;
755d821c997SPavel Labath   case e_sint:
756d821c997SPavel Labath   case e_slong:
757d821c997SPavel Labath   case e_slonglong:
758d821c997SPavel Labath   case e_sint128:
759d821c997SPavel Labath   case e_sint256:
76051d46bd4SDavide Italiano   case e_sint512:
761*b725142cSPavel Labath     return llvm::APIntOps::RoundSignedAPIntToFloat(m_integer);
762*b725142cSPavel Labath 
763*b725142cSPavel Labath   case e_uint:
764*b725142cSPavel Labath   case e_ulong:
765*b725142cSPavel Labath   case e_ulonglong:
766*b725142cSPavel Labath   case e_uint128:
767*b725142cSPavel Labath   case e_uint256:
76851d46bd4SDavide Italiano   case e_uint512:
769d821c997SPavel Labath     return llvm::APIntOps::RoundAPIntToFloat(m_integer);
770*b725142cSPavel Labath 
771d821c997SPavel Labath   case e_float:
772d821c997SPavel Labath     return m_float.convertToFloat();
773d821c997SPavel Labath   case e_double:
77424374aefSJonas Devlieghere     return static_cast<float_t>(m_float.convertToDouble());
775d821c997SPavel Labath   case e_long_double:
776d821c997SPavel Labath     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
777d821c997SPavel Labath     return ldbl_val.bitsToFloat();
778d821c997SPavel Labath   }
779d821c997SPavel Labath   return fail_value;
780d821c997SPavel Labath }
781d821c997SPavel Labath 
782d821c997SPavel Labath double Scalar::Double(double fail_value) const {
783d821c997SPavel Labath   switch (m_type) {
784d821c997SPavel Labath   case e_void:
785d821c997SPavel Labath     break;
786d821c997SPavel Labath   case e_sint:
787d821c997SPavel Labath   case e_slong:
788d821c997SPavel Labath   case e_slonglong:
789d821c997SPavel Labath   case e_sint128:
790d821c997SPavel Labath   case e_sint256:
79151d46bd4SDavide Italiano   case e_sint512:
792*b725142cSPavel Labath     return llvm::APIntOps::RoundSignedAPIntToDouble(m_integer);
793*b725142cSPavel Labath 
794*b725142cSPavel Labath   case e_uint:
795*b725142cSPavel Labath   case e_ulong:
796*b725142cSPavel Labath   case e_ulonglong:
797*b725142cSPavel Labath   case e_uint128:
798*b725142cSPavel Labath   case e_uint256:
79951d46bd4SDavide Italiano   case e_uint512:
800d821c997SPavel Labath     return llvm::APIntOps::RoundAPIntToDouble(m_integer);
801*b725142cSPavel Labath 
802d821c997SPavel Labath   case e_float:
80324374aefSJonas Devlieghere     return static_cast<double_t>(m_float.convertToFloat());
804d821c997SPavel Labath   case e_double:
805d821c997SPavel Labath     return m_float.convertToDouble();
806d821c997SPavel Labath   case e_long_double:
807d821c997SPavel Labath     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
808d821c997SPavel Labath     return ldbl_val.bitsToFloat();
809d821c997SPavel Labath   }
810d821c997SPavel Labath   return fail_value;
811d821c997SPavel Labath }
812d821c997SPavel Labath 
813d821c997SPavel Labath long double Scalar::LongDouble(long double fail_value) const {
814d821c997SPavel Labath   switch (m_type) {
815d821c997SPavel Labath   case e_void:
816d821c997SPavel Labath     break;
817d821c997SPavel Labath   case e_sint:
818d821c997SPavel Labath   case e_slong:
819d821c997SPavel Labath   case e_slonglong:
820d821c997SPavel Labath   case e_sint128:
821d821c997SPavel Labath   case e_sint256:
82251d46bd4SDavide Italiano   case e_sint512:
823*b725142cSPavel Labath     return static_cast<long_double_t>(
824*b725142cSPavel Labath         llvm::APIntOps::RoundSignedAPIntToDouble(m_integer));
825*b725142cSPavel Labath 
826*b725142cSPavel Labath   case e_uint:
827*b725142cSPavel Labath   case e_ulong:
828*b725142cSPavel Labath   case e_ulonglong:
829*b725142cSPavel Labath   case e_uint128:
830*b725142cSPavel Labath   case e_uint256:
83151d46bd4SDavide Italiano   case e_uint512:
83224374aefSJonas Devlieghere     return static_cast<long_double_t>(
83324374aefSJonas Devlieghere         llvm::APIntOps::RoundAPIntToDouble(m_integer));
834*b725142cSPavel Labath 
835d821c997SPavel Labath   case e_float:
83624374aefSJonas Devlieghere     return static_cast<long_double_t>(m_float.convertToFloat());
837d821c997SPavel Labath   case e_double:
83824374aefSJonas Devlieghere     return static_cast<long_double_t>(m_float.convertToDouble());
839d821c997SPavel Labath   case e_long_double:
840d821c997SPavel Labath     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
84124374aefSJonas Devlieghere     return static_cast<long_double_t>(ldbl_val.bitsToDouble());
842d821c997SPavel Labath   }
843d821c997SPavel Labath   return fail_value;
844d821c997SPavel Labath }
845d821c997SPavel Labath 
846d821c997SPavel Labath Scalar &Scalar::operator+=(const Scalar &rhs) {
847d821c997SPavel Labath   Scalar temp_value;
848d821c997SPavel Labath   const Scalar *a;
849d821c997SPavel Labath   const Scalar *b;
850d821c997SPavel Labath   if ((m_type = PromoteToMaxType(*this, rhs, temp_value, a, b)) !=
851d821c997SPavel Labath       Scalar::e_void) {
852d821c997SPavel Labath     switch (m_type) {
853d821c997SPavel Labath     case e_void:
854d821c997SPavel Labath       break;
855d821c997SPavel Labath     case e_sint:
856d821c997SPavel Labath     case e_uint:
857d821c997SPavel Labath     case e_slong:
858d821c997SPavel Labath     case e_ulong:
859d821c997SPavel Labath     case e_slonglong:
860d821c997SPavel Labath     case e_ulonglong:
861d821c997SPavel Labath     case e_sint128:
862d821c997SPavel Labath     case e_uint128:
863d821c997SPavel Labath     case e_sint256:
864d821c997SPavel Labath     case e_uint256:
86551d46bd4SDavide Italiano     case e_sint512:
86651d46bd4SDavide Italiano     case e_uint512:
867d821c997SPavel Labath       m_integer = a->m_integer + b->m_integer;
868d821c997SPavel Labath       break;
869d821c997SPavel Labath 
870d821c997SPavel Labath     case e_float:
871d821c997SPavel Labath     case e_double:
872d821c997SPavel Labath     case e_long_double:
873d821c997SPavel Labath       m_float = a->m_float + b->m_float;
874d821c997SPavel Labath       break;
875d821c997SPavel Labath     }
876d821c997SPavel Labath   }
877d821c997SPavel Labath   return *this;
878d821c997SPavel Labath }
879d821c997SPavel Labath 
880d821c997SPavel Labath Scalar &Scalar::operator<<=(const Scalar &rhs) {
881d821c997SPavel Labath   switch (m_type) {
882d821c997SPavel Labath   case e_void:
883d821c997SPavel Labath   case e_float:
884d821c997SPavel Labath   case e_double:
885d821c997SPavel Labath   case e_long_double:
886d821c997SPavel Labath     m_type = e_void;
887d821c997SPavel Labath     break;
888d821c997SPavel Labath 
889d821c997SPavel Labath   case e_sint:
890d821c997SPavel Labath   case e_uint:
891d821c997SPavel Labath   case e_slong:
892d821c997SPavel Labath   case e_ulong:
893d821c997SPavel Labath   case e_slonglong:
894d821c997SPavel Labath   case e_ulonglong:
895d821c997SPavel Labath   case e_sint128:
896d821c997SPavel Labath   case e_uint128:
897d821c997SPavel Labath   case e_sint256:
898d821c997SPavel Labath   case e_uint256:
89951d46bd4SDavide Italiano   case e_sint512:
90051d46bd4SDavide Italiano   case e_uint512:
901d821c997SPavel Labath     switch (rhs.m_type) {
902d821c997SPavel Labath     case e_void:
903d821c997SPavel Labath     case e_float:
904d821c997SPavel Labath     case e_double:
905d821c997SPavel Labath     case e_long_double:
906d821c997SPavel Labath       m_type = e_void;
907d821c997SPavel Labath       break;
908d821c997SPavel Labath     case e_sint:
909d821c997SPavel Labath     case e_uint:
910d821c997SPavel Labath     case e_slong:
911d821c997SPavel Labath     case e_ulong:
912d821c997SPavel Labath     case e_slonglong:
913d821c997SPavel Labath     case e_ulonglong:
914d821c997SPavel Labath     case e_sint128:
915d821c997SPavel Labath     case e_uint128:
916d821c997SPavel Labath     case e_sint256:
917d821c997SPavel Labath     case e_uint256:
91851d46bd4SDavide Italiano     case e_sint512:
91951d46bd4SDavide Italiano     case e_uint512:
920d821c997SPavel Labath       m_integer = m_integer << rhs.m_integer;
921d821c997SPavel Labath       break;
922d821c997SPavel Labath     }
923d821c997SPavel Labath     break;
924d821c997SPavel Labath   }
925d821c997SPavel Labath   return *this;
926d821c997SPavel Labath }
927d821c997SPavel Labath 
928d821c997SPavel Labath bool Scalar::ShiftRightLogical(const Scalar &rhs) {
929d821c997SPavel Labath   switch (m_type) {
930d821c997SPavel Labath   case e_void:
931d821c997SPavel Labath   case e_float:
932d821c997SPavel Labath   case e_double:
933d821c997SPavel Labath   case e_long_double:
934d821c997SPavel Labath     m_type = e_void;
935d821c997SPavel Labath     break;
936d821c997SPavel Labath 
937d821c997SPavel Labath   case e_sint:
938d821c997SPavel Labath   case e_uint:
939d821c997SPavel Labath   case e_slong:
940d821c997SPavel Labath   case e_ulong:
941d821c997SPavel Labath   case e_slonglong:
942d821c997SPavel Labath   case e_ulonglong:
943d821c997SPavel Labath   case e_sint128:
944d821c997SPavel Labath   case e_uint128:
945d821c997SPavel Labath   case e_sint256:
946d821c997SPavel Labath   case e_uint256:
94751d46bd4SDavide Italiano   case e_sint512:
94851d46bd4SDavide Italiano   case e_uint512:
949d821c997SPavel Labath     switch (rhs.m_type) {
950d821c997SPavel Labath     case e_void:
951d821c997SPavel Labath     case e_float:
952d821c997SPavel Labath     case e_double:
953d821c997SPavel Labath     case e_long_double:
954d821c997SPavel Labath       m_type = e_void;
955d821c997SPavel Labath       break;
956d821c997SPavel Labath     case e_sint:
957d821c997SPavel Labath     case e_uint:
958d821c997SPavel Labath     case e_slong:
959d821c997SPavel Labath     case e_ulong:
960d821c997SPavel Labath     case e_slonglong:
961d821c997SPavel Labath     case e_ulonglong:
962d821c997SPavel Labath     case e_sint128:
963d821c997SPavel Labath     case e_uint128:
964d821c997SPavel Labath     case e_sint256:
965d821c997SPavel Labath     case e_uint256:
96651d46bd4SDavide Italiano     case e_sint512:
96751d46bd4SDavide Italiano     case e_uint512:
968d821c997SPavel Labath       m_integer = m_integer.lshr(rhs.m_integer);
969d821c997SPavel Labath       break;
970d821c997SPavel Labath     }
971d821c997SPavel Labath     break;
972d821c997SPavel Labath   }
973d821c997SPavel Labath   return m_type != e_void;
974d821c997SPavel Labath }
975d821c997SPavel Labath 
976d821c997SPavel Labath Scalar &Scalar::operator>>=(const Scalar &rhs) {
977d821c997SPavel Labath   switch (m_type) {
978d821c997SPavel Labath   case e_void:
979d821c997SPavel Labath   case e_float:
980d821c997SPavel Labath   case e_double:
981d821c997SPavel Labath   case e_long_double:
982d821c997SPavel Labath     m_type = e_void;
983d821c997SPavel Labath     break;
984d821c997SPavel Labath 
985d821c997SPavel Labath   case e_sint:
986d821c997SPavel Labath   case e_uint:
987d821c997SPavel Labath   case e_slong:
988d821c997SPavel Labath   case e_ulong:
989d821c997SPavel Labath   case e_slonglong:
990d821c997SPavel Labath   case e_ulonglong:
991d821c997SPavel Labath   case e_sint128:
992d821c997SPavel Labath   case e_uint128:
993d821c997SPavel Labath   case e_sint256:
994d821c997SPavel Labath   case e_uint256:
99551d46bd4SDavide Italiano   case e_sint512:
99651d46bd4SDavide Italiano   case e_uint512:
997d821c997SPavel Labath     switch (rhs.m_type) {
998d821c997SPavel Labath     case e_void:
999d821c997SPavel Labath     case e_float:
1000d821c997SPavel Labath     case e_double:
1001d821c997SPavel Labath     case e_long_double:
1002d821c997SPavel Labath       m_type = e_void;
1003d821c997SPavel Labath       break;
1004d821c997SPavel Labath     case e_sint:
1005d821c997SPavel Labath     case e_uint:
1006d821c997SPavel Labath     case e_slong:
1007d821c997SPavel Labath     case e_ulong:
1008d821c997SPavel Labath     case e_slonglong:
1009d821c997SPavel Labath     case e_ulonglong:
1010d821c997SPavel Labath     case e_sint128:
1011d821c997SPavel Labath     case e_uint128:
1012d821c997SPavel Labath     case e_sint256:
1013d821c997SPavel Labath     case e_uint256:
101451d46bd4SDavide Italiano     case e_sint512:
101551d46bd4SDavide Italiano     case e_uint512:
1016d821c997SPavel Labath       m_integer = m_integer.ashr(rhs.m_integer);
1017d821c997SPavel Labath       break;
1018d821c997SPavel Labath     }
1019d821c997SPavel Labath     break;
1020d821c997SPavel Labath   }
1021d821c997SPavel Labath   return *this;
1022d821c997SPavel Labath }
1023d821c997SPavel Labath 
1024d821c997SPavel Labath Scalar &Scalar::operator&=(const Scalar &rhs) {
1025d821c997SPavel Labath   switch (m_type) {
1026d821c997SPavel Labath   case e_void:
1027d821c997SPavel Labath   case e_float:
1028d821c997SPavel Labath   case e_double:
1029d821c997SPavel Labath   case e_long_double:
1030d821c997SPavel Labath     m_type = e_void;
1031d821c997SPavel Labath     break;
1032d821c997SPavel Labath 
1033d821c997SPavel Labath   case e_sint:
1034d821c997SPavel Labath   case e_uint:
1035d821c997SPavel Labath   case e_slong:
1036d821c997SPavel Labath   case e_ulong:
1037d821c997SPavel Labath   case e_slonglong:
1038d821c997SPavel Labath   case e_ulonglong:
1039d821c997SPavel Labath   case e_sint128:
1040d821c997SPavel Labath   case e_uint128:
1041d821c997SPavel Labath   case e_sint256:
1042d821c997SPavel Labath   case e_uint256:
104351d46bd4SDavide Italiano   case e_sint512:
104451d46bd4SDavide Italiano   case e_uint512:
1045d821c997SPavel Labath     switch (rhs.m_type) {
1046d821c997SPavel Labath     case e_void:
1047d821c997SPavel Labath     case e_float:
1048d821c997SPavel Labath     case e_double:
1049d821c997SPavel Labath     case e_long_double:
1050d821c997SPavel Labath       m_type = e_void;
1051d821c997SPavel Labath       break;
1052d821c997SPavel Labath     case e_sint:
1053d821c997SPavel Labath     case e_uint:
1054d821c997SPavel Labath     case e_slong:
1055d821c997SPavel Labath     case e_ulong:
1056d821c997SPavel Labath     case e_slonglong:
1057d821c997SPavel Labath     case e_ulonglong:
1058d821c997SPavel Labath     case e_sint128:
1059d821c997SPavel Labath     case e_uint128:
1060d821c997SPavel Labath     case e_sint256:
1061d821c997SPavel Labath     case e_uint256:
106251d46bd4SDavide Italiano     case e_sint512:
106351d46bd4SDavide Italiano     case e_uint512:
1064d821c997SPavel Labath       m_integer &= rhs.m_integer;
1065d821c997SPavel Labath       break;
1066d821c997SPavel Labath     }
1067d821c997SPavel Labath     break;
1068d821c997SPavel Labath   }
1069d821c997SPavel Labath   return *this;
1070d821c997SPavel Labath }
1071d821c997SPavel Labath 
1072d821c997SPavel Labath bool Scalar::AbsoluteValue() {
1073d821c997SPavel Labath   switch (m_type) {
1074d821c997SPavel Labath   case e_void:
1075d821c997SPavel Labath     break;
1076d821c997SPavel Labath 
1077d821c997SPavel Labath   case e_sint:
1078d821c997SPavel Labath   case e_slong:
1079d821c997SPavel Labath   case e_slonglong:
1080d821c997SPavel Labath   case e_sint128:
1081d821c997SPavel Labath   case e_sint256:
108251d46bd4SDavide Italiano   case e_sint512:
1083d821c997SPavel Labath     if (m_integer.isNegative())
1084d821c997SPavel Labath       m_integer = -m_integer;
1085d821c997SPavel Labath     return true;
1086d821c997SPavel Labath 
1087d821c997SPavel Labath   case e_uint:
1088d821c997SPavel Labath   case e_ulong:
1089d821c997SPavel Labath   case e_ulonglong:
1090d821c997SPavel Labath     return true;
1091d821c997SPavel Labath   case e_uint128:
1092d821c997SPavel Labath   case e_uint256:
109351d46bd4SDavide Italiano   case e_uint512:
1094d821c997SPavel Labath   case e_float:
1095d821c997SPavel Labath   case e_double:
1096d821c997SPavel Labath   case e_long_double:
1097d821c997SPavel Labath     m_float.clearSign();
1098d821c997SPavel Labath     return true;
1099d821c997SPavel Labath   }
1100d821c997SPavel Labath   return false;
1101d821c997SPavel Labath }
1102d821c997SPavel Labath 
1103d821c997SPavel Labath bool Scalar::UnaryNegate() {
1104d821c997SPavel Labath   switch (m_type) {
1105d821c997SPavel Labath   case e_void:
1106d821c997SPavel Labath     break;
1107d821c997SPavel Labath   case e_sint:
1108d821c997SPavel Labath   case e_uint:
1109d821c997SPavel Labath   case e_slong:
1110d821c997SPavel Labath   case e_ulong:
1111d821c997SPavel Labath   case e_slonglong:
1112d821c997SPavel Labath   case e_ulonglong:
1113d821c997SPavel Labath   case e_sint128:
1114d821c997SPavel Labath   case e_uint128:
1115d821c997SPavel Labath   case e_sint256:
1116d821c997SPavel Labath   case e_uint256:
111751d46bd4SDavide Italiano   case e_sint512:
111851d46bd4SDavide Italiano   case e_uint512:
1119d821c997SPavel Labath     m_integer = -m_integer;
1120d821c997SPavel Labath     return true;
1121d821c997SPavel Labath   case e_float:
1122d821c997SPavel Labath   case e_double:
1123d821c997SPavel Labath   case e_long_double:
1124d821c997SPavel Labath     m_float.changeSign();
1125d821c997SPavel Labath     return true;
1126d821c997SPavel Labath   }
1127d821c997SPavel Labath   return false;
1128d821c997SPavel Labath }
1129d821c997SPavel Labath 
1130d821c997SPavel Labath bool Scalar::OnesComplement() {
1131d821c997SPavel Labath   switch (m_type) {
1132d821c997SPavel Labath   case e_sint:
1133d821c997SPavel Labath   case e_uint:
1134d821c997SPavel Labath   case e_slong:
1135d821c997SPavel Labath   case e_ulong:
1136d821c997SPavel Labath   case e_slonglong:
1137d821c997SPavel Labath   case e_ulonglong:
1138d821c997SPavel Labath   case e_sint128:
1139d821c997SPavel Labath   case e_uint128:
1140d821c997SPavel Labath   case e_sint256:
1141d821c997SPavel Labath   case e_uint256:
114251d46bd4SDavide Italiano   case e_sint512:
114351d46bd4SDavide Italiano   case e_uint512:
1144d821c997SPavel Labath     m_integer = ~m_integer;
1145d821c997SPavel Labath     return true;
1146d821c997SPavel Labath 
1147d821c997SPavel Labath   case e_void:
1148d821c997SPavel Labath   case e_float:
1149d821c997SPavel Labath   case e_double:
1150d821c997SPavel Labath   case e_long_double:
1151d821c997SPavel Labath     break;
1152d821c997SPavel Labath   }
1153d821c997SPavel Labath   return false;
1154d821c997SPavel Labath }
1155d821c997SPavel Labath 
1156d821c997SPavel Labath const Scalar lldb_private::operator+(const Scalar &lhs, const Scalar &rhs) {
1157d821c997SPavel Labath   Scalar result;
1158d821c997SPavel Labath   Scalar temp_value;
1159d821c997SPavel Labath   const Scalar *a;
1160d821c997SPavel Labath   const Scalar *b;
1161d821c997SPavel Labath   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1162d821c997SPavel Labath       Scalar::e_void) {
1163d821c997SPavel Labath     switch (result.m_type) {
1164d821c997SPavel Labath     case Scalar::e_void:
1165d821c997SPavel Labath       break;
1166d821c997SPavel Labath     case Scalar::e_sint:
1167d821c997SPavel Labath     case Scalar::e_uint:
1168d821c997SPavel Labath     case Scalar::e_slong:
1169d821c997SPavel Labath     case Scalar::e_ulong:
1170d821c997SPavel Labath     case Scalar::e_slonglong:
1171d821c997SPavel Labath     case Scalar::e_ulonglong:
1172d821c997SPavel Labath     case Scalar::e_sint128:
1173d821c997SPavel Labath     case Scalar::e_uint128:
1174d821c997SPavel Labath     case Scalar::e_sint256:
1175d821c997SPavel Labath     case Scalar::e_uint256:
117651d46bd4SDavide Italiano     case Scalar::e_sint512:
117751d46bd4SDavide Italiano     case Scalar::e_uint512:
1178d821c997SPavel Labath       result.m_integer = a->m_integer + b->m_integer;
1179d821c997SPavel Labath       break;
1180d821c997SPavel Labath     case Scalar::e_float:
1181d821c997SPavel Labath     case Scalar::e_double:
1182d821c997SPavel Labath     case Scalar::e_long_double:
1183d821c997SPavel Labath       result.m_float = a->m_float + b->m_float;
1184d821c997SPavel Labath       break;
1185d821c997SPavel Labath     }
1186d821c997SPavel Labath   }
1187d821c997SPavel Labath   return result;
1188d821c997SPavel Labath }
1189d821c997SPavel Labath 
1190d821c997SPavel Labath const Scalar lldb_private::operator-(const Scalar &lhs, const Scalar &rhs) {
1191d821c997SPavel Labath   Scalar result;
1192d821c997SPavel Labath   Scalar temp_value;
1193d821c997SPavel Labath   const Scalar *a;
1194d821c997SPavel Labath   const Scalar *b;
1195d821c997SPavel Labath   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1196d821c997SPavel Labath       Scalar::e_void) {
1197d821c997SPavel Labath     switch (result.m_type) {
1198d821c997SPavel Labath     case Scalar::e_void:
1199d821c997SPavel Labath       break;
1200d821c997SPavel Labath     case Scalar::e_sint:
1201d821c997SPavel Labath     case Scalar::e_uint:
1202d821c997SPavel Labath     case Scalar::e_slong:
1203d821c997SPavel Labath     case Scalar::e_ulong:
1204d821c997SPavel Labath     case Scalar::e_slonglong:
1205d821c997SPavel Labath     case Scalar::e_ulonglong:
1206d821c997SPavel Labath     case Scalar::e_sint128:
1207d821c997SPavel Labath     case Scalar::e_uint128:
1208d821c997SPavel Labath     case Scalar::e_sint256:
1209d821c997SPavel Labath     case Scalar::e_uint256:
121051d46bd4SDavide Italiano     case Scalar::e_sint512:
121151d46bd4SDavide Italiano     case Scalar::e_uint512:
1212d821c997SPavel Labath       result.m_integer = a->m_integer - b->m_integer;
1213d821c997SPavel Labath       break;
1214d821c997SPavel Labath     case Scalar::e_float:
1215d821c997SPavel Labath     case Scalar::e_double:
1216d821c997SPavel Labath     case Scalar::e_long_double:
1217d821c997SPavel Labath       result.m_float = a->m_float - b->m_float;
1218d821c997SPavel Labath       break;
1219d821c997SPavel Labath     }
1220d821c997SPavel Labath   }
1221d821c997SPavel Labath   return result;
1222d821c997SPavel Labath }
1223d821c997SPavel Labath 
1224d821c997SPavel Labath const Scalar lldb_private::operator/(const Scalar &lhs, const Scalar &rhs) {
1225d821c997SPavel Labath   Scalar result;
1226d821c997SPavel Labath   Scalar temp_value;
1227d821c997SPavel Labath   const Scalar *a;
1228d821c997SPavel Labath   const Scalar *b;
1229d821c997SPavel Labath   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1230d821c997SPavel Labath       Scalar::e_void) {
1231d821c997SPavel Labath     switch (result.m_type) {
1232d821c997SPavel Labath     case Scalar::e_void:
1233d821c997SPavel Labath       break;
1234d821c997SPavel Labath     case Scalar::e_sint:
1235d821c997SPavel Labath     case Scalar::e_slong:
1236d821c997SPavel Labath     case Scalar::e_slonglong:
1237d821c997SPavel Labath     case Scalar::e_sint128:
1238d821c997SPavel Labath     case Scalar::e_sint256:
123951d46bd4SDavide Italiano     case Scalar::e_sint512:
1240d821c997SPavel Labath       if (b->m_integer != 0) {
1241d821c997SPavel Labath         result.m_integer = a->m_integer.sdiv(b->m_integer);
1242d821c997SPavel Labath         return result;
1243d821c997SPavel Labath       }
1244d821c997SPavel Labath       break;
1245d821c997SPavel Labath     case Scalar::e_uint:
1246d821c997SPavel Labath     case Scalar::e_ulong:
1247d821c997SPavel Labath     case Scalar::e_ulonglong:
1248d821c997SPavel Labath     case Scalar::e_uint128:
1249d821c997SPavel Labath     case Scalar::e_uint256:
125051d46bd4SDavide Italiano     case Scalar::e_uint512:
1251d821c997SPavel Labath       if (b->m_integer != 0) {
1252d821c997SPavel Labath         result.m_integer = a->m_integer.udiv(b->m_integer);
1253d821c997SPavel Labath         return result;
1254d821c997SPavel Labath       }
1255d821c997SPavel Labath       break;
1256d821c997SPavel Labath     case Scalar::e_float:
1257d821c997SPavel Labath     case Scalar::e_double:
1258d821c997SPavel Labath     case Scalar::e_long_double:
1259d821c997SPavel Labath       if (!b->m_float.isZero()) {
1260d821c997SPavel Labath         result.m_float = a->m_float / b->m_float;
1261d821c997SPavel Labath         return result;
1262d821c997SPavel Labath       }
1263d821c997SPavel Labath       break;
1264d821c997SPavel Labath     }
1265d821c997SPavel Labath   }
1266d821c997SPavel Labath   // For division only, the only way it should make it here is if a promotion
1267d821c997SPavel Labath   // failed, or if we are trying to do a divide by zero.
1268d821c997SPavel Labath   result.m_type = Scalar::e_void;
1269d821c997SPavel Labath   return result;
1270d821c997SPavel Labath }
1271d821c997SPavel Labath 
1272d821c997SPavel Labath const Scalar lldb_private::operator*(const Scalar &lhs, const Scalar &rhs) {
1273d821c997SPavel Labath   Scalar result;
1274d821c997SPavel Labath   Scalar temp_value;
1275d821c997SPavel Labath   const Scalar *a;
1276d821c997SPavel Labath   const Scalar *b;
1277d821c997SPavel Labath   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1278d821c997SPavel Labath       Scalar::e_void) {
1279d821c997SPavel Labath     switch (result.m_type) {
1280d821c997SPavel Labath     case Scalar::e_void:
1281d821c997SPavel Labath       break;
1282d821c997SPavel Labath     case Scalar::e_sint:
1283d821c997SPavel Labath     case Scalar::e_uint:
1284d821c997SPavel Labath     case Scalar::e_slong:
1285d821c997SPavel Labath     case Scalar::e_ulong:
1286d821c997SPavel Labath     case Scalar::e_slonglong:
1287d821c997SPavel Labath     case Scalar::e_ulonglong:
1288d821c997SPavel Labath     case Scalar::e_sint128:
1289d821c997SPavel Labath     case Scalar::e_uint128:
1290d821c997SPavel Labath     case Scalar::e_sint256:
1291d821c997SPavel Labath     case Scalar::e_uint256:
129251d46bd4SDavide Italiano     case Scalar::e_sint512:
129351d46bd4SDavide Italiano     case Scalar::e_uint512:
1294d821c997SPavel Labath       result.m_integer = a->m_integer * b->m_integer;
1295d821c997SPavel Labath       break;
1296d821c997SPavel Labath     case Scalar::e_float:
1297d821c997SPavel Labath     case Scalar::e_double:
1298d821c997SPavel Labath     case Scalar::e_long_double:
1299d821c997SPavel Labath       result.m_float = a->m_float * b->m_float;
1300d821c997SPavel Labath       break;
1301d821c997SPavel Labath     }
1302d821c997SPavel Labath   }
1303d821c997SPavel Labath   return result;
1304d821c997SPavel Labath }
1305d821c997SPavel Labath 
1306d821c997SPavel Labath const Scalar lldb_private::operator&(const Scalar &lhs, const Scalar &rhs) {
1307d821c997SPavel Labath   Scalar result;
1308d821c997SPavel Labath   Scalar temp_value;
1309d821c997SPavel Labath   const Scalar *a;
1310d821c997SPavel Labath   const Scalar *b;
1311d821c997SPavel Labath   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1312d821c997SPavel Labath       Scalar::e_void) {
1313d821c997SPavel Labath     switch (result.m_type) {
1314d821c997SPavel Labath     case Scalar::e_sint:
1315d821c997SPavel Labath     case Scalar::e_uint:
1316d821c997SPavel Labath     case Scalar::e_slong:
1317d821c997SPavel Labath     case Scalar::e_ulong:
1318d821c997SPavel Labath     case Scalar::e_slonglong:
1319d821c997SPavel Labath     case Scalar::e_ulonglong:
1320d821c997SPavel Labath     case Scalar::e_sint128:
1321d821c997SPavel Labath     case Scalar::e_uint128:
1322d821c997SPavel Labath     case Scalar::e_sint256:
1323d821c997SPavel Labath     case Scalar::e_uint256:
132451d46bd4SDavide Italiano     case Scalar::e_sint512:
132551d46bd4SDavide Italiano     case Scalar::e_uint512:
1326d821c997SPavel Labath       result.m_integer = a->m_integer & b->m_integer;
1327d821c997SPavel Labath       break;
1328d821c997SPavel Labath     case Scalar::e_void:
1329d821c997SPavel Labath     case Scalar::e_float:
1330d821c997SPavel Labath     case Scalar::e_double:
1331d821c997SPavel Labath     case Scalar::e_long_double:
1332d821c997SPavel Labath       // No bitwise AND on floats, doubles of long doubles
1333d821c997SPavel Labath       result.m_type = Scalar::e_void;
1334d821c997SPavel Labath       break;
1335d821c997SPavel Labath     }
1336d821c997SPavel Labath   }
1337d821c997SPavel Labath   return result;
1338d821c997SPavel Labath }
1339d821c997SPavel Labath 
1340d821c997SPavel Labath const Scalar lldb_private::operator|(const Scalar &lhs, const Scalar &rhs) {
1341d821c997SPavel Labath   Scalar result;
1342d821c997SPavel Labath   Scalar temp_value;
1343d821c997SPavel Labath   const Scalar *a;
1344d821c997SPavel Labath   const Scalar *b;
1345d821c997SPavel Labath   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1346d821c997SPavel Labath       Scalar::e_void) {
1347d821c997SPavel Labath     switch (result.m_type) {
1348d821c997SPavel Labath     case Scalar::e_sint:
1349d821c997SPavel Labath     case Scalar::e_uint:
1350d821c997SPavel Labath     case Scalar::e_slong:
1351d821c997SPavel Labath     case Scalar::e_ulong:
1352d821c997SPavel Labath     case Scalar::e_slonglong:
1353d821c997SPavel Labath     case Scalar::e_ulonglong:
1354d821c997SPavel Labath     case Scalar::e_sint128:
1355d821c997SPavel Labath     case Scalar::e_uint128:
1356d821c997SPavel Labath     case Scalar::e_sint256:
1357d821c997SPavel Labath     case Scalar::e_uint256:
135851d46bd4SDavide Italiano     case Scalar::e_sint512:
135951d46bd4SDavide Italiano     case Scalar::e_uint512:
1360d821c997SPavel Labath       result.m_integer = a->m_integer | b->m_integer;
1361d821c997SPavel Labath       break;
1362d821c997SPavel Labath 
1363d821c997SPavel Labath     case Scalar::e_void:
1364d821c997SPavel Labath     case Scalar::e_float:
1365d821c997SPavel Labath     case Scalar::e_double:
1366d821c997SPavel Labath     case Scalar::e_long_double:
1367d821c997SPavel Labath       // No bitwise AND on floats, doubles of long doubles
1368d821c997SPavel Labath       result.m_type = Scalar::e_void;
1369d821c997SPavel Labath       break;
1370d821c997SPavel Labath     }
1371d821c997SPavel Labath   }
1372d821c997SPavel Labath   return result;
1373d821c997SPavel Labath }
1374d821c997SPavel Labath 
1375d821c997SPavel Labath const Scalar lldb_private::operator%(const Scalar &lhs, const Scalar &rhs) {
1376d821c997SPavel Labath   Scalar result;
1377d821c997SPavel Labath   Scalar temp_value;
1378d821c997SPavel Labath   const Scalar *a;
1379d821c997SPavel Labath   const Scalar *b;
1380d821c997SPavel Labath   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1381d821c997SPavel Labath       Scalar::e_void) {
1382d821c997SPavel Labath     switch (result.m_type) {
1383d821c997SPavel Labath     default:
1384d821c997SPavel Labath       break;
1385d821c997SPavel Labath     case Scalar::e_void:
1386d821c997SPavel Labath       break;
1387d821c997SPavel Labath     case Scalar::e_sint:
1388d821c997SPavel Labath     case Scalar::e_slong:
1389d821c997SPavel Labath     case Scalar::e_slonglong:
1390d821c997SPavel Labath     case Scalar::e_sint128:
1391d821c997SPavel Labath     case Scalar::e_sint256:
139251d46bd4SDavide Italiano     case Scalar::e_sint512:
1393d821c997SPavel Labath       if (b->m_integer != 0) {
1394d821c997SPavel Labath         result.m_integer = a->m_integer.srem(b->m_integer);
1395d821c997SPavel Labath         return result;
1396d821c997SPavel Labath       }
1397d821c997SPavel Labath       break;
1398d821c997SPavel Labath     case Scalar::e_uint:
1399d821c997SPavel Labath     case Scalar::e_ulong:
1400d821c997SPavel Labath     case Scalar::e_ulonglong:
1401d821c997SPavel Labath     case Scalar::e_uint128:
1402d821c997SPavel Labath     case Scalar::e_uint256:
140351d46bd4SDavide Italiano     case Scalar::e_uint512:
1404d821c997SPavel Labath       if (b->m_integer != 0) {
1405d821c997SPavel Labath         result.m_integer = a->m_integer.urem(b->m_integer);
1406d821c997SPavel Labath         return result;
1407d821c997SPavel Labath       }
1408d821c997SPavel Labath       break;
1409d821c997SPavel Labath     }
1410d821c997SPavel Labath   }
1411d821c997SPavel Labath   result.m_type = Scalar::e_void;
1412d821c997SPavel Labath   return result;
1413d821c997SPavel Labath }
1414d821c997SPavel Labath 
1415d821c997SPavel Labath const Scalar lldb_private::operator^(const Scalar &lhs, const Scalar &rhs) {
1416d821c997SPavel Labath   Scalar result;
1417d821c997SPavel Labath   Scalar temp_value;
1418d821c997SPavel Labath   const Scalar *a;
1419d821c997SPavel Labath   const Scalar *b;
1420d821c997SPavel Labath   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1421d821c997SPavel Labath       Scalar::e_void) {
1422d821c997SPavel Labath     switch (result.m_type) {
1423d821c997SPavel Labath     case Scalar::e_sint:
1424d821c997SPavel Labath     case Scalar::e_uint:
1425d821c997SPavel Labath     case Scalar::e_slong:
1426d821c997SPavel Labath     case Scalar::e_ulong:
1427d821c997SPavel Labath     case Scalar::e_slonglong:
1428d821c997SPavel Labath     case Scalar::e_ulonglong:
1429d821c997SPavel Labath     case Scalar::e_sint128:
1430d821c997SPavel Labath     case Scalar::e_uint128:
1431d821c997SPavel Labath     case Scalar::e_sint256:
1432d821c997SPavel Labath     case Scalar::e_uint256:
143351d46bd4SDavide Italiano     case Scalar::e_sint512:
143451d46bd4SDavide Italiano     case Scalar::e_uint512:
1435d821c997SPavel Labath       result.m_integer = a->m_integer ^ b->m_integer;
1436d821c997SPavel Labath       break;
1437d821c997SPavel Labath 
1438d821c997SPavel Labath     case Scalar::e_void:
1439d821c997SPavel Labath     case Scalar::e_float:
1440d821c997SPavel Labath     case Scalar::e_double:
1441d821c997SPavel Labath     case Scalar::e_long_double:
1442d821c997SPavel Labath       // No bitwise AND on floats, doubles of long doubles
1443d821c997SPavel Labath       result.m_type = Scalar::e_void;
1444d821c997SPavel Labath       break;
1445d821c997SPavel Labath     }
1446d821c997SPavel Labath   }
1447d821c997SPavel Labath   return result;
1448d821c997SPavel Labath }
1449d821c997SPavel Labath 
1450d821c997SPavel Labath const Scalar lldb_private::operator<<(const Scalar &lhs, const Scalar &rhs) {
1451d821c997SPavel Labath   Scalar result = lhs;
1452d821c997SPavel Labath   result <<= rhs;
1453d821c997SPavel Labath   return result;
1454d821c997SPavel Labath }
1455d821c997SPavel Labath 
1456d821c997SPavel Labath const Scalar lldb_private::operator>>(const Scalar &lhs, const Scalar &rhs) {
1457d821c997SPavel Labath   Scalar result = lhs;
1458d821c997SPavel Labath   result >>= rhs;
1459d821c997SPavel Labath   return result;
1460d821c997SPavel Labath }
1461d821c997SPavel Labath 
1462d821c997SPavel Labath Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding,
1463d821c997SPavel Labath                                    size_t byte_size) {
1464d821c997SPavel Labath   Status error;
1465d821c997SPavel Labath   if (value_str == nullptr || value_str[0] == '\0') {
1466d821c997SPavel Labath     error.SetErrorString("Invalid c-string value string.");
1467d821c997SPavel Labath     return error;
1468d821c997SPavel Labath   }
1469d821c997SPavel Labath   switch (encoding) {
1470d821c997SPavel Labath   case eEncodingInvalid:
1471d821c997SPavel Labath     error.SetErrorString("Invalid encoding.");
1472d821c997SPavel Labath     break;
1473d821c997SPavel Labath 
1474d821c997SPavel Labath   case eEncodingUint:
1475d821c997SPavel Labath     if (byte_size <= sizeof(uint64_t)) {
1476d821c997SPavel Labath       uint64_t uval64;
1477d821c997SPavel Labath       if (!llvm::to_integer(value_str, uval64))
1478d821c997SPavel Labath         error.SetErrorStringWithFormat(
1479d821c997SPavel Labath             "'%s' is not a valid unsigned integer string value", value_str);
1480d821c997SPavel Labath       else if (!UIntValueIsValidForSize(uval64, byte_size))
148124374aefSJonas Devlieghere         error.SetErrorStringWithFormat(
148224374aefSJonas Devlieghere             "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
1483d821c997SPavel Labath             " byte unsigned integer value",
148424374aefSJonas Devlieghere             uval64, static_cast<uint64_t>(byte_size));
1485d821c997SPavel Labath       else {
1486d821c997SPavel Labath         m_type = Scalar::GetValueTypeForUnsignedIntegerWithByteSize(byte_size);
1487d821c997SPavel Labath         switch (m_type) {
1488d821c997SPavel Labath         case e_uint:
1489d821c997SPavel Labath           m_integer = llvm::APInt(sizeof(uint_t) * 8, uval64, false);
1490d821c997SPavel Labath           break;
1491d821c997SPavel Labath         case e_ulong:
1492d821c997SPavel Labath           m_integer = llvm::APInt(sizeof(ulong_t) * 8, uval64, false);
1493d821c997SPavel Labath           break;
1494d821c997SPavel Labath         case e_ulonglong:
1495d821c997SPavel Labath           m_integer = llvm::APInt(sizeof(ulonglong_t) * 8, uval64, false);
1496d821c997SPavel Labath           break;
1497d821c997SPavel Labath         default:
1498d821c997SPavel Labath           error.SetErrorStringWithFormat(
1499d821c997SPavel Labath               "unsupported unsigned integer byte size: %" PRIu64 "",
150024374aefSJonas Devlieghere               static_cast<uint64_t>(byte_size));
1501d821c997SPavel Labath           break;
1502d821c997SPavel Labath         }
1503d821c997SPavel Labath       }
1504d821c997SPavel Labath     } else {
1505d821c997SPavel Labath       error.SetErrorStringWithFormat(
1506d821c997SPavel Labath           "unsupported unsigned integer byte size: %" PRIu64 "",
150724374aefSJonas Devlieghere           static_cast<uint64_t>(byte_size));
1508d821c997SPavel Labath       return error;
1509d821c997SPavel Labath     }
1510d821c997SPavel Labath     break;
1511d821c997SPavel Labath 
1512d821c997SPavel Labath   case eEncodingSint:
1513d821c997SPavel Labath     if (byte_size <= sizeof(int64_t)) {
1514d821c997SPavel Labath       int64_t sval64;
1515d821c997SPavel Labath       if (!llvm::to_integer(value_str, sval64))
1516d821c997SPavel Labath         error.SetErrorStringWithFormat(
1517d821c997SPavel Labath             "'%s' is not a valid signed integer string value", value_str);
1518d821c997SPavel Labath       else if (!SIntValueIsValidForSize(sval64, byte_size))
151924374aefSJonas Devlieghere         error.SetErrorStringWithFormat(
152024374aefSJonas Devlieghere             "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
1521d821c997SPavel Labath             " byte signed integer value",
152224374aefSJonas Devlieghere             sval64, static_cast<uint64_t>(byte_size));
1523d821c997SPavel Labath       else {
1524d821c997SPavel Labath         m_type = Scalar::GetValueTypeForSignedIntegerWithByteSize(byte_size);
1525d821c997SPavel Labath         switch (m_type) {
1526d821c997SPavel Labath         case e_sint:
1527d821c997SPavel Labath           m_integer = llvm::APInt(sizeof(sint_t) * 8, sval64, true);
1528d821c997SPavel Labath           break;
1529d821c997SPavel Labath         case e_slong:
1530d821c997SPavel Labath           m_integer = llvm::APInt(sizeof(slong_t) * 8, sval64, true);
1531d821c997SPavel Labath           break;
1532d821c997SPavel Labath         case e_slonglong:
1533d821c997SPavel Labath           m_integer = llvm::APInt(sizeof(slonglong_t) * 8, sval64, true);
1534d821c997SPavel Labath           break;
1535d821c997SPavel Labath         default:
1536d821c997SPavel Labath           error.SetErrorStringWithFormat(
1537d821c997SPavel Labath               "unsupported signed integer byte size: %" PRIu64 "",
153824374aefSJonas Devlieghere               static_cast<uint64_t>(byte_size));
1539d821c997SPavel Labath           break;
1540d821c997SPavel Labath         }
1541d821c997SPavel Labath       }
1542d821c997SPavel Labath     } else {
1543d821c997SPavel Labath       error.SetErrorStringWithFormat(
1544d821c997SPavel Labath           "unsupported signed integer byte size: %" PRIu64 "",
154524374aefSJonas Devlieghere           static_cast<uint64_t>(byte_size));
1546d821c997SPavel Labath       return error;
1547d821c997SPavel Labath     }
1548d821c997SPavel Labath     break;
1549d821c997SPavel Labath 
1550d821c997SPavel Labath   case eEncodingIEEE754:
1551d821c997SPavel Labath     static float f_val;
1552d821c997SPavel Labath     static double d_val;
1553d821c997SPavel Labath     static long double l_val;
1554d821c997SPavel Labath     if (byte_size == sizeof(float)) {
1555d821c997SPavel Labath       if (::sscanf(value_str, "%f", &f_val) == 1) {
1556d821c997SPavel Labath         m_float = llvm::APFloat(f_val);
1557d821c997SPavel Labath         m_type = e_float;
1558d821c997SPavel Labath       } else
1559d821c997SPavel Labath         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1560d821c997SPavel Labath                                        value_str);
1561d821c997SPavel Labath     } else if (byte_size == sizeof(double)) {
1562d821c997SPavel Labath       if (::sscanf(value_str, "%lf", &d_val) == 1) {
1563d821c997SPavel Labath         m_float = llvm::APFloat(d_val);
1564d821c997SPavel Labath         m_type = e_double;
1565d821c997SPavel Labath       } else
1566d821c997SPavel Labath         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1567d821c997SPavel Labath                                        value_str);
1568d821c997SPavel Labath     } else if (byte_size == sizeof(long double)) {
1569d821c997SPavel Labath       if (::sscanf(value_str, "%Lf", &l_val) == 1) {
157024374aefSJonas Devlieghere         m_float = llvm::APFloat(
157124374aefSJonas Devlieghere             llvm::APFloat::x87DoubleExtended(),
1572d821c997SPavel Labath             llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
157324374aefSJonas Devlieghere                         (reinterpret_cast<type128 *>(&l_val))->x));
1574d821c997SPavel Labath         m_type = e_long_double;
1575d821c997SPavel Labath       } else
1576d821c997SPavel Labath         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1577d821c997SPavel Labath                                        value_str);
1578d821c997SPavel Labath     } else {
1579d821c997SPavel Labath       error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
158024374aefSJonas Devlieghere                                      static_cast<uint64_t>(byte_size));
1581d821c997SPavel Labath       return error;
1582d821c997SPavel Labath     }
1583d821c997SPavel Labath     break;
1584d821c997SPavel Labath 
1585d821c997SPavel Labath   case eEncodingVector:
1586d821c997SPavel Labath     error.SetErrorString("vector encoding unsupported.");
1587d821c997SPavel Labath     break;
1588d821c997SPavel Labath   }
1589d821c997SPavel Labath   if (error.Fail())
1590d821c997SPavel Labath     m_type = e_void;
1591d821c997SPavel Labath 
1592d821c997SPavel Labath   return error;
1593d821c997SPavel Labath }
1594d821c997SPavel Labath 
1595d821c997SPavel Labath Status Scalar::SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
1596d821c997SPavel Labath                                 size_t byte_size) {
1597d821c997SPavel Labath   Status error;
1598d821c997SPavel Labath 
1599d821c997SPavel Labath   type128 int128;
1600d821c997SPavel Labath   type256 int256;
1601d821c997SPavel Labath   switch (encoding) {
1602d821c997SPavel Labath   case lldb::eEncodingInvalid:
1603d821c997SPavel Labath     error.SetErrorString("invalid encoding");
1604d821c997SPavel Labath     break;
1605d821c997SPavel Labath   case lldb::eEncodingVector:
1606d821c997SPavel Labath     error.SetErrorString("vector encoding unsupported");
1607d821c997SPavel Labath     break;
1608d821c997SPavel Labath   case lldb::eEncodingUint: {
1609d821c997SPavel Labath     lldb::offset_t offset = 0;
1610d821c997SPavel Labath 
1611d821c997SPavel Labath     switch (byte_size) {
1612d821c997SPavel Labath     case 1:
161324374aefSJonas Devlieghere       operator=(data.GetU8(&offset));
1614d821c997SPavel Labath       break;
1615d821c997SPavel Labath     case 2:
161624374aefSJonas Devlieghere       operator=(data.GetU16(&offset));
1617d821c997SPavel Labath       break;
1618d821c997SPavel Labath     case 4:
161924374aefSJonas Devlieghere       operator=(data.GetU32(&offset));
1620d821c997SPavel Labath       break;
1621d821c997SPavel Labath     case 8:
162224374aefSJonas Devlieghere       operator=(data.GetU64(&offset));
1623d821c997SPavel Labath       break;
1624d821c997SPavel Labath     case 16:
1625d821c997SPavel Labath       if (data.GetByteOrder() == eByteOrderBig) {
162624374aefSJonas Devlieghere         int128.x[1] = data.GetU64(&offset);
162724374aefSJonas Devlieghere         int128.x[0] = data.GetU64(&offset);
1628d821c997SPavel Labath       } else {
162924374aefSJonas Devlieghere         int128.x[0] = data.GetU64(&offset);
163024374aefSJonas Devlieghere         int128.x[1] = data.GetU64(&offset);
1631d821c997SPavel Labath       }
1632d821c997SPavel Labath       operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
1633d821c997SPavel Labath       break;
1634d821c997SPavel Labath     case 32:
1635d821c997SPavel Labath       if (data.GetByteOrder() == eByteOrderBig) {
163624374aefSJonas Devlieghere         int256.x[3] = data.GetU64(&offset);
163724374aefSJonas Devlieghere         int256.x[2] = data.GetU64(&offset);
163824374aefSJonas Devlieghere         int256.x[1] = data.GetU64(&offset);
163924374aefSJonas Devlieghere         int256.x[0] = data.GetU64(&offset);
1640d821c997SPavel Labath       } else {
164124374aefSJonas Devlieghere         int256.x[0] = data.GetU64(&offset);
164224374aefSJonas Devlieghere         int256.x[1] = data.GetU64(&offset);
164324374aefSJonas Devlieghere         int256.x[2] = data.GetU64(&offset);
164424374aefSJonas Devlieghere         int256.x[3] = data.GetU64(&offset);
1645d821c997SPavel Labath       }
1646d821c997SPavel Labath       operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
1647d821c997SPavel Labath       break;
1648d821c997SPavel Labath     default:
1649d821c997SPavel Labath       error.SetErrorStringWithFormat(
1650d821c997SPavel Labath           "unsupported unsigned integer byte size: %" PRIu64 "",
165124374aefSJonas Devlieghere           static_cast<uint64_t>(byte_size));
1652d821c997SPavel Labath       break;
1653d821c997SPavel Labath     }
1654d821c997SPavel Labath   } break;
1655d821c997SPavel Labath   case lldb::eEncodingSint: {
1656d821c997SPavel Labath     lldb::offset_t offset = 0;
1657d821c997SPavel Labath 
1658d821c997SPavel Labath     switch (byte_size) {
1659d821c997SPavel Labath     case 1:
166024374aefSJonas Devlieghere       operator=(static_cast<int8_t>(data.GetU8(&offset)));
1661d821c997SPavel Labath       break;
1662d821c997SPavel Labath     case 2:
166324374aefSJonas Devlieghere       operator=(static_cast<int16_t>(data.GetU16(&offset)));
1664d821c997SPavel Labath       break;
1665d821c997SPavel Labath     case 4:
166624374aefSJonas Devlieghere       operator=(static_cast<int32_t>(data.GetU32(&offset)));
1667d821c997SPavel Labath       break;
1668d821c997SPavel Labath     case 8:
166924374aefSJonas Devlieghere       operator=(static_cast<int64_t>(data.GetU64(&offset)));
1670d821c997SPavel Labath       break;
1671d821c997SPavel Labath     case 16:
1672d821c997SPavel Labath       if (data.GetByteOrder() == eByteOrderBig) {
167324374aefSJonas Devlieghere         int128.x[1] = data.GetU64(&offset);
167424374aefSJonas Devlieghere         int128.x[0] = data.GetU64(&offset);
1675d821c997SPavel Labath       } else {
167624374aefSJonas Devlieghere         int128.x[0] = data.GetU64(&offset);
167724374aefSJonas Devlieghere         int128.x[1] = data.GetU64(&offset);
1678d821c997SPavel Labath       }
1679d821c997SPavel Labath       operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
1680d821c997SPavel Labath       break;
1681d821c997SPavel Labath     case 32:
1682d821c997SPavel Labath       if (data.GetByteOrder() == eByteOrderBig) {
168324374aefSJonas Devlieghere         int256.x[3] = data.GetU64(&offset);
168424374aefSJonas Devlieghere         int256.x[2] = data.GetU64(&offset);
168524374aefSJonas Devlieghere         int256.x[1] = data.GetU64(&offset);
168624374aefSJonas Devlieghere         int256.x[0] = data.GetU64(&offset);
1687d821c997SPavel Labath       } else {
168824374aefSJonas Devlieghere         int256.x[0] = data.GetU64(&offset);
168924374aefSJonas Devlieghere         int256.x[1] = data.GetU64(&offset);
169024374aefSJonas Devlieghere         int256.x[2] = data.GetU64(&offset);
169124374aefSJonas Devlieghere         int256.x[3] = data.GetU64(&offset);
1692d821c997SPavel Labath       }
1693d821c997SPavel Labath       operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
1694d821c997SPavel Labath       break;
1695d821c997SPavel Labath     default:
1696d821c997SPavel Labath       error.SetErrorStringWithFormat(
1697d821c997SPavel Labath           "unsupported signed integer byte size: %" PRIu64 "",
169824374aefSJonas Devlieghere           static_cast<uint64_t>(byte_size));
1699d821c997SPavel Labath       break;
1700d821c997SPavel Labath     }
1701d821c997SPavel Labath   } break;
1702d821c997SPavel Labath   case lldb::eEncodingIEEE754: {
1703d821c997SPavel Labath     lldb::offset_t offset = 0;
1704d821c997SPavel Labath 
1705d821c997SPavel Labath     if (byte_size == sizeof(float))
170624374aefSJonas Devlieghere       operator=(data.GetFloat(&offset));
1707d821c997SPavel Labath     else if (byte_size == sizeof(double))
170824374aefSJonas Devlieghere       operator=(data.GetDouble(&offset));
1709d821c997SPavel Labath     else if (byte_size == sizeof(long double))
171024374aefSJonas Devlieghere       operator=(data.GetLongDouble(&offset));
1711d821c997SPavel Labath     else
1712d821c997SPavel Labath       error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
171324374aefSJonas Devlieghere                                      static_cast<uint64_t>(byte_size));
1714d821c997SPavel Labath   } break;
1715d821c997SPavel Labath   }
1716d821c997SPavel Labath 
1717d821c997SPavel Labath   return error;
1718d821c997SPavel Labath }
1719d821c997SPavel Labath 
1720d821c997SPavel Labath bool Scalar::SignExtend(uint32_t sign_bit_pos) {
1721d821c997SPavel Labath   const uint32_t max_bit_pos = GetByteSize() * 8;
1722d821c997SPavel Labath 
1723d821c997SPavel Labath   if (sign_bit_pos < max_bit_pos) {
1724d821c997SPavel Labath     switch (m_type) {
1725d821c997SPavel Labath     case Scalar::e_void:
1726d821c997SPavel Labath     case Scalar::e_float:
1727d821c997SPavel Labath     case Scalar::e_double:
1728d821c997SPavel Labath     case Scalar::e_long_double:
1729d821c997SPavel Labath       return false;
1730d821c997SPavel Labath 
1731d821c997SPavel Labath     case Scalar::e_sint:
1732d821c997SPavel Labath     case Scalar::e_uint:
1733d821c997SPavel Labath     case Scalar::e_slong:
1734d821c997SPavel Labath     case Scalar::e_ulong:
1735d821c997SPavel Labath     case Scalar::e_slonglong:
1736d821c997SPavel Labath     case Scalar::e_ulonglong:
1737d821c997SPavel Labath     case Scalar::e_sint128:
1738d821c997SPavel Labath     case Scalar::e_uint128:
1739d821c997SPavel Labath     case Scalar::e_sint256:
1740d821c997SPavel Labath     case Scalar::e_uint256:
174151d46bd4SDavide Italiano     case Scalar::e_sint512:
174251d46bd4SDavide Italiano     case Scalar::e_uint512:
1743d821c997SPavel Labath       if (max_bit_pos == sign_bit_pos)
1744d821c997SPavel Labath         return true;
1745d821c997SPavel Labath       else if (sign_bit_pos < (max_bit_pos - 1)) {
1746d821c997SPavel Labath         llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
1747d821c997SPavel Labath         llvm::APInt bitwize_and = m_integer & sign_bit;
1748d821c997SPavel Labath         if (bitwize_and.getBoolValue()) {
1749d821c997SPavel Labath           const llvm::APInt mask =
1750d821c997SPavel Labath               ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
1751d821c997SPavel Labath           m_integer |= mask;
1752d821c997SPavel Labath         }
1753d821c997SPavel Labath         return true;
1754d821c997SPavel Labath       }
1755d821c997SPavel Labath       break;
1756d821c997SPavel Labath     }
1757d821c997SPavel Labath   }
1758d821c997SPavel Labath   return false;
1759d821c997SPavel Labath }
1760d821c997SPavel Labath 
1761d821c997SPavel Labath size_t Scalar::GetAsMemoryData(void *dst, size_t dst_len,
1762d821c997SPavel Labath                                lldb::ByteOrder dst_byte_order,
1763d821c997SPavel Labath                                Status &error) const {
1764d821c997SPavel Labath   // Get a data extractor that points to the native scalar data
1765d821c997SPavel Labath   DataExtractor data;
1766d821c997SPavel Labath   if (!GetData(data)) {
1767d821c997SPavel Labath     error.SetErrorString("invalid scalar value");
1768d821c997SPavel Labath     return 0;
1769d821c997SPavel Labath   }
1770d821c997SPavel Labath 
1771d821c997SPavel Labath   const size_t src_len = data.GetByteSize();
1772d821c997SPavel Labath 
1773d821c997SPavel Labath   // Prepare a memory buffer that contains some or all of the register value
1774d821c997SPavel Labath   const size_t bytes_copied =
1775d821c997SPavel Labath       data.CopyByteOrderedData(0,               // src offset
1776d821c997SPavel Labath                                src_len,         // src length
1777d821c997SPavel Labath                                dst,             // dst buffer
1778d821c997SPavel Labath                                dst_len,         // dst length
1779d821c997SPavel Labath                                dst_byte_order); // dst byte order
1780d821c997SPavel Labath   if (bytes_copied == 0)
1781d821c997SPavel Labath     error.SetErrorString("failed to copy data");
1782d821c997SPavel Labath 
1783d821c997SPavel Labath   return bytes_copied;
1784d821c997SPavel Labath }
1785d821c997SPavel Labath 
1786d821c997SPavel Labath bool Scalar::ExtractBitfield(uint32_t bit_size, uint32_t bit_offset) {
1787d821c997SPavel Labath   if (bit_size == 0)
1788d821c997SPavel Labath     return true;
1789d821c997SPavel Labath 
1790d821c997SPavel Labath   switch (m_type) {
1791d821c997SPavel Labath   case Scalar::e_void:
1792d821c997SPavel Labath   case Scalar::e_float:
1793d821c997SPavel Labath   case Scalar::e_double:
1794d821c997SPavel Labath   case Scalar::e_long_double:
1795d821c997SPavel Labath     break;
1796d821c997SPavel Labath 
1797d821c997SPavel Labath   case Scalar::e_sint:
1798d821c997SPavel Labath   case Scalar::e_slong:
1799d821c997SPavel Labath   case Scalar::e_slonglong:
1800d821c997SPavel Labath   case Scalar::e_sint128:
1801d821c997SPavel Labath   case Scalar::e_sint256:
180251d46bd4SDavide Italiano   case Scalar::e_sint512:
1803d821c997SPavel Labath     m_integer = m_integer.ashr(bit_offset)
1804d821c997SPavel Labath                     .sextOrTrunc(bit_size)
1805d821c997SPavel Labath                     .sextOrSelf(8 * GetByteSize());
1806d821c997SPavel Labath     return true;
1807d821c997SPavel Labath 
1808d821c997SPavel Labath   case Scalar::e_uint:
1809d821c997SPavel Labath   case Scalar::e_ulong:
1810d821c997SPavel Labath   case Scalar::e_ulonglong:
1811d821c997SPavel Labath   case Scalar::e_uint128:
1812d821c997SPavel Labath   case Scalar::e_uint256:
181351d46bd4SDavide Italiano   case Scalar::e_uint512:
1814d821c997SPavel Labath     m_integer = m_integer.lshr(bit_offset)
1815d821c997SPavel Labath                     .zextOrTrunc(bit_size)
1816d821c997SPavel Labath                     .zextOrSelf(8 * GetByteSize());
1817d821c997SPavel Labath     return true;
1818d821c997SPavel Labath   }
1819d821c997SPavel Labath   return false;
1820d821c997SPavel Labath }
1821d821c997SPavel Labath 
1822d821c997SPavel Labath bool lldb_private::operator==(const Scalar &lhs, const Scalar &rhs) {
1823d821c997SPavel Labath   // If either entry is void then we can just compare the types
1824d821c997SPavel Labath   if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
1825d821c997SPavel Labath     return lhs.m_type == rhs.m_type;
1826d821c997SPavel Labath 
1827d821c997SPavel Labath   Scalar temp_value;
1828d821c997SPavel Labath   const Scalar *a;
1829d821c997SPavel Labath   const Scalar *b;
1830d821c997SPavel Labath   llvm::APFloat::cmpResult result;
1831d821c997SPavel Labath   switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
1832d821c997SPavel Labath   case Scalar::e_void:
1833d821c997SPavel Labath     break;
1834d821c997SPavel Labath   case Scalar::e_sint:
1835d821c997SPavel Labath   case Scalar::e_uint:
1836d821c997SPavel Labath   case Scalar::e_slong:
1837d821c997SPavel Labath   case Scalar::e_ulong:
1838d821c997SPavel Labath   case Scalar::e_slonglong:
1839d821c997SPavel Labath   case Scalar::e_ulonglong:
1840d821c997SPavel Labath   case Scalar::e_sint128:
1841d821c997SPavel Labath   case Scalar::e_uint128:
1842d821c997SPavel Labath   case Scalar::e_sint256:
1843d821c997SPavel Labath   case Scalar::e_uint256:
184451d46bd4SDavide Italiano   case Scalar::e_sint512:
184551d46bd4SDavide Italiano   case Scalar::e_uint512:
1846d821c997SPavel Labath     return a->m_integer == b->m_integer;
1847d821c997SPavel Labath   case Scalar::e_float:
1848d821c997SPavel Labath   case Scalar::e_double:
1849d821c997SPavel Labath   case Scalar::e_long_double:
1850d821c997SPavel Labath     result = a->m_float.compare(b->m_float);
1851d821c997SPavel Labath     if (result == llvm::APFloat::cmpEqual)
1852d821c997SPavel Labath       return true;
1853d821c997SPavel Labath   }
1854d821c997SPavel Labath   return false;
1855d821c997SPavel Labath }
1856d821c997SPavel Labath 
1857d821c997SPavel Labath bool lldb_private::operator!=(const Scalar &lhs, const Scalar &rhs) {
185818a0ce98SDavide Italiano   return !(lhs == rhs);
1859d821c997SPavel Labath }
1860d821c997SPavel Labath 
1861d821c997SPavel Labath bool lldb_private::operator<(const Scalar &lhs, const Scalar &rhs) {
1862d821c997SPavel Labath   if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
1863d821c997SPavel Labath     return false;
1864d821c997SPavel Labath 
1865d821c997SPavel Labath   Scalar temp_value;
1866d821c997SPavel Labath   const Scalar *a;
1867d821c997SPavel Labath   const Scalar *b;
1868d821c997SPavel Labath   llvm::APFloat::cmpResult result;
1869d821c997SPavel Labath   switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
1870d821c997SPavel Labath   case Scalar::e_void:
1871d821c997SPavel Labath     break;
1872d821c997SPavel Labath   case Scalar::e_sint:
1873d821c997SPavel Labath   case Scalar::e_slong:
1874d821c997SPavel Labath   case Scalar::e_slonglong:
1875d821c997SPavel Labath   case Scalar::e_sint128:
1876d821c997SPavel Labath   case Scalar::e_sint256:
187751d46bd4SDavide Italiano   case Scalar::e_sint512:
187851d46bd4SDavide Italiano   case Scalar::e_uint512:
1879d821c997SPavel Labath     return a->m_integer.slt(b->m_integer);
1880d821c997SPavel Labath   case Scalar::e_uint:
1881d821c997SPavel Labath   case Scalar::e_ulong:
1882d821c997SPavel Labath   case Scalar::e_ulonglong:
1883d821c997SPavel Labath   case Scalar::e_uint128:
1884d821c997SPavel Labath   case Scalar::e_uint256:
1885d821c997SPavel Labath     return a->m_integer.ult(b->m_integer);
1886d821c997SPavel Labath   case Scalar::e_float:
1887d821c997SPavel Labath   case Scalar::e_double:
1888d821c997SPavel Labath   case Scalar::e_long_double:
1889d821c997SPavel Labath     result = a->m_float.compare(b->m_float);
1890d821c997SPavel Labath     if (result == llvm::APFloat::cmpLessThan)
1891d821c997SPavel Labath       return true;
1892d821c997SPavel Labath   }
1893d821c997SPavel Labath   return false;
1894d821c997SPavel Labath }
1895d821c997SPavel Labath 
1896d821c997SPavel Labath bool lldb_private::operator<=(const Scalar &lhs, const Scalar &rhs) {
1897ff92a1a7SDavide Italiano   return !(rhs < lhs);
1898d821c997SPavel Labath }
1899d821c997SPavel Labath 
1900d821c997SPavel Labath bool lldb_private::operator>(const Scalar &lhs, const Scalar &rhs) {
1901ff92a1a7SDavide Italiano   return rhs < lhs;
1902d821c997SPavel Labath }
1903d821c997SPavel Labath 
1904d821c997SPavel Labath bool lldb_private::operator>=(const Scalar &lhs, const Scalar &rhs) {
1905ff92a1a7SDavide Italiano   return !(lhs < rhs);
1906d821c997SPavel Labath }
1907d821c997SPavel Labath 
1908d821c997SPavel Labath bool Scalar::ClearBit(uint32_t bit) {
1909d821c997SPavel Labath   switch (m_type) {
1910d821c997SPavel Labath   case e_void:
1911d821c997SPavel Labath     break;
1912d821c997SPavel Labath   case e_sint:
1913d821c997SPavel Labath   case e_uint:
1914d821c997SPavel Labath   case e_slong:
1915d821c997SPavel Labath   case e_ulong:
1916d821c997SPavel Labath   case e_slonglong:
1917d821c997SPavel Labath   case e_ulonglong:
1918d821c997SPavel Labath   case e_sint128:
1919d821c997SPavel Labath   case e_uint128:
1920d821c997SPavel Labath   case e_sint256:
1921d821c997SPavel Labath   case e_uint256:
192251d46bd4SDavide Italiano   case e_sint512:
192351d46bd4SDavide Italiano   case e_uint512:
1924d821c997SPavel Labath     m_integer.clearBit(bit);
1925d821c997SPavel Labath     return true;
1926d821c997SPavel Labath   case e_float:
1927d821c997SPavel Labath   case e_double:
1928d821c997SPavel Labath   case e_long_double:
1929d821c997SPavel Labath     break;
1930d821c997SPavel Labath   }
1931d821c997SPavel Labath   return false;
1932d821c997SPavel Labath }
1933d821c997SPavel Labath 
1934d821c997SPavel Labath bool Scalar::SetBit(uint32_t bit) {
1935d821c997SPavel Labath   switch (m_type) {
1936d821c997SPavel Labath   case e_void:
1937d821c997SPavel Labath     break;
1938d821c997SPavel Labath   case e_sint:
1939d821c997SPavel Labath   case e_uint:
1940d821c997SPavel Labath   case e_slong:
1941d821c997SPavel Labath   case e_ulong:
1942d821c997SPavel Labath   case e_slonglong:
1943d821c997SPavel Labath   case e_ulonglong:
1944d821c997SPavel Labath   case e_sint128:
1945d821c997SPavel Labath   case e_uint128:
1946d821c997SPavel Labath   case e_sint256:
1947d821c997SPavel Labath   case e_uint256:
194851d46bd4SDavide Italiano   case e_sint512:
194951d46bd4SDavide Italiano   case e_uint512:
1950d821c997SPavel Labath     m_integer.setBit(bit);
1951d821c997SPavel Labath     return true;
1952d821c997SPavel Labath   case e_float:
1953d821c997SPavel Labath   case e_double:
1954d821c997SPavel Labath   case e_long_double:
1955d821c997SPavel Labath     break;
1956d821c997SPavel Labath   }
1957d821c997SPavel Labath   return false;
1958d821c997SPavel Labath }
1959b07a7997SPavel Labath 
1960b07a7997SPavel Labath llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &os, const Scalar &scalar) {
1961b07a7997SPavel Labath   StreamString s;
1962b07a7997SPavel Labath   scalar.GetValue(&s, /*show_type*/ true);
1963b07a7997SPavel Labath   return os << s.GetString();
1964b07a7997SPavel Labath }
1965