LLDB  mainline
Scalar.cpp
Go to the documentation of this file.
1 //===-- Scalar.cpp ----------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Utility/Scalar.h"
10 
12 #include "lldb/Utility/Endian.h"
13 #include "lldb/Utility/Status.h"
14 #include "lldb/Utility/Stream.h"
16 #include "lldb/lldb-types.h"
17 
18 #include "llvm/ADT/SmallString.h"
19 
20 #include <cinttypes>
21 #include <cstdio>
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 // Promote to max type currently follows the ANSI C rule for type promotion in
27 // expressions.
29  const Scalar &lhs, // The const left hand side object
30  const Scalar &rhs, // The const right hand side object
31  Scalar &temp_value, // A modifiable temp value than can be used to hold
32  // either the promoted lhs or rhs object
33  const Scalar *&promoted_lhs_ptr, // Pointer to the resulting possibly
34  // promoted value of lhs (at most one of
35  // lhs/rhs will get promoted)
36  const Scalar *&promoted_rhs_ptr // Pointer to the resulting possibly
37  // promoted value of rhs (at most one of
38  // lhs/rhs will get promoted)
39 ) {
40  Scalar result;
41  // Initialize the promoted values for both the right and left hand side
42  // values to be the objects themselves. If no promotion is needed (both right
43  // and left have the same type), then the temp_value will not get used.
44  promoted_lhs_ptr = &lhs;
45  promoted_rhs_ptr = &rhs;
46  // Extract the types of both the right and left hand side values
47  Scalar::Type lhs_type = lhs.GetType();
48  Scalar::Type rhs_type = rhs.GetType();
49 
50  if (lhs_type > rhs_type) {
51  // Right hand side need to be promoted
52  temp_value = rhs; // Copy right hand side into the temp value
53  if (temp_value.Promote(lhs_type)) // Promote it
54  promoted_rhs_ptr =
55  &temp_value; // Update the pointer for the promoted right hand side
56  } else if (lhs_type < rhs_type) {
57  // Left hand side need to be promoted
58  temp_value = lhs; // Copy left hand side value into the temp value
59  if (temp_value.Promote(rhs_type)) // Promote it
60  promoted_lhs_ptr =
61  &temp_value; // Update the pointer for the promoted left hand side
62  }
63 
64  // Make sure our type promotion worked as expected
65  if (promoted_lhs_ptr->GetType() == promoted_rhs_ptr->GetType())
66  return promoted_lhs_ptr->GetType(); // Return the resulting max type
67 
68  // Return the void type (zero) if we fail to promote either of the values.
69  return Scalar::e_void;
70 }
71 
72 Scalar::Scalar() : m_type(e_void), m_float((float)0) {}
73 
75  : m_type(rhs.m_type), m_integer(rhs.m_integer), m_float(rhs.m_float) {}
76 
77 bool Scalar::GetData(DataExtractor &data, size_t limit_byte_size) const {
78  size_t byte_size = GetByteSize();
79  if (byte_size > 0) {
80  const uint8_t *bytes = reinterpret_cast<const uint8_t *>(GetBytes());
81 
82  if (limit_byte_size < byte_size) {
84  // On little endian systems if we want fewer bytes from the current
85  // type we just specify fewer bytes since the LSByte is first...
86  byte_size = limit_byte_size;
87  } else if (endian::InlHostByteOrder() == eByteOrderBig) {
88  // On big endian systems if we want fewer bytes from the current type
89  // have to advance our initial byte pointer and trim down the number of
90  // bytes since the MSByte is first
91  bytes += byte_size - limit_byte_size;
92  byte_size = limit_byte_size;
93  }
94  }
95 
96  data.SetData(bytes, byte_size, endian::InlHostByteOrder());
97  return true;
98  }
99  data.Clear();
100  return false;
101 }
102 
103 const void *Scalar::GetBytes() const {
104  const uint64_t *apint_words;
105  const uint8_t *bytes;
106  static float_t flt_val;
107  static double_t dbl_val;
108  static uint64_t swapped_words[8];
109  switch (m_type) {
110  case e_void:
111  break;
112  case e_sint:
113  case e_uint:
114  case e_slong:
115  case e_ulong:
116  case e_slonglong:
117  case e_ulonglong:
118  bytes = reinterpret_cast<const uint8_t *>(m_integer.getRawData());
119  // getRawData always returns a pointer to an uint64_t. If we have a
120  // smaller type, we need to update the pointer on big-endian systems.
122  size_t byte_size = m_integer.getBitWidth() / 8;
123  if (byte_size < 8)
124  bytes += 8 - byte_size;
125  }
126  return bytes;
127  // getRawData always returns a pointer to an array of uint64_t values,
128  // where the least-significant word always comes first. On big-endian
129  // systems we need to swap the words.
130  case e_sint128:
131  case e_uint128:
132  apint_words = m_integer.getRawData();
134  swapped_words[0] = apint_words[1];
135  swapped_words[1] = apint_words[0];
136  apint_words = swapped_words;
137  }
138  return reinterpret_cast<const void *>(apint_words);
139  case e_sint256:
140  case e_uint256:
141  apint_words = m_integer.getRawData();
143  swapped_words[0] = apint_words[3];
144  swapped_words[1] = apint_words[2];
145  swapped_words[2] = apint_words[1];
146  swapped_words[3] = apint_words[0];
147  apint_words = swapped_words;
148  }
149  return reinterpret_cast<const void *>(apint_words);
150  case e_sint512:
151  case e_uint512:
152  apint_words = m_integer.getRawData();
154  swapped_words[0] = apint_words[7];
155  swapped_words[1] = apint_words[6];
156  swapped_words[2] = apint_words[5];
157  swapped_words[3] = apint_words[4];
158  swapped_words[4] = apint_words[3];
159  swapped_words[5] = apint_words[2];
160  swapped_words[6] = apint_words[1];
161  swapped_words[7] = apint_words[0];
162  apint_words = swapped_words;
163  }
164  return reinterpret_cast<const void *>(apint_words);
165  case e_float:
166  flt_val = m_float.convertToFloat();
167  return reinterpret_cast<const void *>(&flt_val);
168  case e_double:
169  dbl_val = m_float.convertToDouble();
170  return reinterpret_cast<const void *>(&dbl_val);
171  case e_long_double:
172  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
173  apint_words = ldbl_val.getRawData();
174  // getRawData always returns a pointer to an array of two uint64_t values,
175  // where the least-significant word always comes first. On big-endian
176  // systems we need to swap the two words.
178  swapped_words[0] = apint_words[1];
179  swapped_words[1] = apint_words[0];
180  apint_words = swapped_words;
181  }
182  return reinterpret_cast<const void *>(apint_words);
183  }
184  return nullptr;
185 }
186 
187 size_t Scalar::GetByteSize() const {
188  switch (m_type) {
189  case e_void:
190  break;
191  case e_sint:
192  case e_uint:
193  case e_slong:
194  case e_ulong:
195  case e_slonglong:
196  case e_ulonglong:
197  case e_sint128:
198  case e_uint128:
199  case e_sint256:
200  case e_uint256:
201  case e_sint512:
202  case e_uint512:
203  return (m_integer.getBitWidth() / 8);
204  case e_float:
205  return sizeof(float_t);
206  case e_double:
207  return sizeof(double_t);
208  case e_long_double:
209  return sizeof(long_double_t);
210  }
211  return 0;
212 }
213 
214 bool Scalar::IsZero() const {
215  llvm::APInt zero_int = llvm::APInt::getNullValue(m_integer.getBitWidth() / 8);
216  switch (m_type) {
217  case e_void:
218  break;
219  case e_sint:
220  case e_uint:
221  case e_slong:
222  case e_ulong:
223  case e_slonglong:
224  case e_ulonglong:
225  case e_sint128:
226  case e_uint128:
227  case e_sint256:
228  case e_uint256:
229  case e_uint512:
230  case e_sint512:
231  return llvm::APInt::isSameValue(zero_int, m_integer);
232  case e_float:
233  case e_double:
234  case e_long_double:
235  return m_float.isZero();
236  }
237  return false;
238 }
239 
240 void Scalar::GetValue(Stream *s, bool show_type) const {
241  if (show_type)
242  s->Printf("(%s) ", GetTypeAsCString());
243 
244  switch (m_type) {
245  case e_void:
246  break;
247  case e_sint:
248  case e_slong:
249  case e_slonglong:
250  case e_sint128:
251  case e_sint256:
252  case e_sint512:
253  s->PutCString(m_integer.toString(10, true));
254  break;
255  case e_uint:
256  case e_ulong:
257  case e_ulonglong:
258  case e_uint128:
259  case e_uint256:
260  case e_uint512:
261  s->PutCString(m_integer.toString(10, false));
262  break;
263  case e_float:
264  case e_double:
265  case e_long_double:
266  llvm::SmallString<24> string;
267  m_float.toString(string);
268  s->Printf("%s", string.c_str());
269  break;
270  }
271 }
272 
273 const char *Scalar::GetTypeAsCString() const {
274  switch (m_type) {
275  case e_void:
276  return "void";
277  case e_sint:
278  return "int";
279  case e_uint:
280  return "unsigned int";
281  case e_slong:
282  return "long";
283  case e_ulong:
284  return "unsigned long";
285  case e_slonglong:
286  return "long long";
287  case e_ulonglong:
288  return "unsigned long long";
289  case e_sint128:
290  return "int128_t";
291  case e_uint128:
292  return "unsigned int128_t";
293  case e_sint256:
294  return "int256_t";
295  case e_uint256:
296  return "unsigned int256_t";
297  case e_sint512:
298  return "int512_t";
299  case e_uint512:
300  return "unsigned int512_t";
301  case e_float:
302  return "float";
303  case e_double:
304  return "double";
305  case e_long_double:
306  return "long double";
307  }
308  return "<invalid Scalar type>";
309 }
310 
312  if (this != &rhs) {
313  m_type = rhs.m_type;
314  m_integer = llvm::APInt(rhs.m_integer);
315  m_float = rhs.m_float;
316  }
317  return *this;
318 }
319 
320 Scalar &Scalar::operator=(const int v) {
321  m_type = e_sint;
322  m_integer = llvm::APInt(sizeof(int) * 8, v, true);
323  return *this;
324 }
325 
326 Scalar &Scalar::operator=(unsigned int v) {
327  m_type = e_uint;
328  m_integer = llvm::APInt(sizeof(int) * 8, v);
329  return *this;
330 }
331 
333  m_type = e_slong;
334  m_integer = llvm::APInt(sizeof(long) * 8, v, true);
335  return *this;
336 }
337 
338 Scalar &Scalar::operator=(unsigned long v) {
339  m_type = e_ulong;
340  m_integer = llvm::APInt(sizeof(long) * 8, v);
341  return *this;
342 }
343 
344 Scalar &Scalar::operator=(long long v) {
346  m_integer = llvm::APInt(sizeof(long) * 8, v, true);
347  return *this;
348 }
349 
350 Scalar &Scalar::operator=(unsigned long long v) {
352  m_integer = llvm::APInt(sizeof(long long) * 8, v);
353  return *this;
354 }
355 
357  m_type = e_float;
358  m_float = llvm::APFloat(v);
359  return *this;
360 }
361 
363  m_type = e_double;
364  m_float = llvm::APFloat(v);
365  return *this;
366 }
367 
368 Scalar &Scalar::operator=(long double v) {
370  if (m_ieee_quad)
371  m_float = llvm::APFloat(
372  llvm::APFloat::IEEEquad(),
373  llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, ((type128 *)&v)->x));
374  else
375  m_float = llvm::APFloat(
376  llvm::APFloat::x87DoubleExtended(),
377  llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, ((type128 *)&v)->x));
378  return *this;
379 }
380 
381 Scalar &Scalar::operator=(llvm::APInt rhs) {
382  m_integer = llvm::APInt(rhs);
383  switch (m_integer.getBitWidth()) {
384  case 8:
385  case 16:
386  case 32:
387  if (m_integer.isSignedIntN(sizeof(sint_t) * 8))
388  m_type = e_sint;
389  else
390  m_type = e_uint;
391  break;
392  case 64:
393  if (m_integer.isSignedIntN(sizeof(slonglong_t) * 8))
395  else
397  break;
398  case 128:
399  if (m_integer.isSignedIntN(BITWIDTH_INT128))
400  m_type = e_sint128;
401  else
402  m_type = e_uint128;
403  break;
404  case 256:
405  if (m_integer.isSignedIntN(BITWIDTH_INT256))
406  m_type = e_sint256;
407  else
408  m_type = e_uint256;
409  break;
410  case 512:
411  if (m_integer.isSignedIntN(BITWIDTH_INT512))
412  m_type = e_sint512;
413  else
414  m_type = e_uint512;
415  break;
416  }
417  return *this;
418 }
419 
420 Scalar::~Scalar() = default;
421 
423  bool success = false;
424  switch (m_type) {
425  case e_void:
426  break;
427 
428  case e_sint:
429  switch (type) {
430  case e_void:
431  break;
432  case e_sint:
433  success = true;
434  break;
435  case e_uint:
436  m_integer = m_integer.sextOrTrunc(sizeof(uint_t) * 8);
437  success = true;
438  break;
439 
440  case e_slong:
441  m_integer = m_integer.sextOrTrunc(sizeof(slong_t) * 8);
442  success = true;
443  break;
444 
445  case e_ulong:
446  m_integer = m_integer.sextOrTrunc(sizeof(ulong_t) * 8);
447  success = true;
448  break;
449 
450  case e_slonglong:
451  m_integer = m_integer.sextOrTrunc(sizeof(slonglong_t) * 8);
452  success = true;
453  break;
454 
455  case e_ulonglong:
456  m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8);
457  success = true;
458  break;
459 
460  case e_sint128:
461  case e_uint128:
462  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128);
463  success = true;
464  break;
465 
466  case e_sint256:
467  case e_uint256:
468  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256);
469  success = true;
470  break;
471 
472  case e_sint512:
473  case e_uint512:
474  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT512);
475  success = true;
476  break;
477 
478  case e_float:
479  m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
480  m_float.convertFromAPInt(m_integer, true,
481  llvm::APFloat::rmNearestTiesToEven);
482  success = true;
483  break;
484 
485  case e_double:
486  m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
487  m_float.convertFromAPInt(m_integer, true,
488  llvm::APFloat::rmNearestTiesToEven);
489  success = true;
490  break;
491 
492  case e_long_double:
493  m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
494  : llvm::APFloat::x87DoubleExtended());
495  m_float.convertFromAPInt(m_integer, true,
496  llvm::APFloat::rmNearestTiesToEven);
497  success = true;
498  break;
499  }
500  break;
501 
502  case e_uint:
503  switch (type) {
504  case e_void:
505  case e_sint:
506  break;
507  case e_uint:
508  success = true;
509  break;
510  case e_slong:
511  m_integer = m_integer.zextOrTrunc(sizeof(slong_t) * 8);
512  success = true;
513  break;
514 
515  case e_ulong:
516  m_integer = m_integer.zextOrTrunc(sizeof(ulong_t) * 8);
517  success = true;
518  break;
519 
520  case e_slonglong:
521  m_integer = m_integer.zextOrTrunc(sizeof(slonglong_t) * 8);
522  success = true;
523  break;
524 
525  case e_ulonglong:
526  m_integer = m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8);
527  success = true;
528  break;
529 
530  case e_sint128:
531  case e_uint128:
532  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128);
533  success = true;
534  break;
535 
536  case e_sint256:
537  case e_uint256:
538  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256);
539  success = true;
540  break;
541 
542  case e_sint512:
543  case e_uint512:
544  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
545  success = true;
546  break;
547 
548  case e_float:
549  m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
550  m_float.convertFromAPInt(m_integer, false,
551  llvm::APFloat::rmNearestTiesToEven);
552  success = true;
553  break;
554 
555  case e_double:
556  m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
557  m_float.convertFromAPInt(m_integer, false,
558  llvm::APFloat::rmNearestTiesToEven);
559  success = true;
560  break;
561 
562  case e_long_double:
563  m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
564  : llvm::APFloat::x87DoubleExtended());
565  m_float.convertFromAPInt(m_integer, false,
566  llvm::APFloat::rmNearestTiesToEven);
567  success = true;
568  break;
569  }
570  break;
571 
572  case e_slong:
573  switch (type) {
574  case e_void:
575  case e_sint:
576  case e_uint:
577  break;
578  case e_slong:
579  success = true;
580  break;
581  case e_ulong:
582  m_integer = m_integer.sextOrTrunc(sizeof(ulong_t) * 8);
583  success = true;
584  break;
585 
586  case e_slonglong:
587  m_integer = m_integer.sextOrTrunc(sizeof(slonglong_t) * 8);
588  success = true;
589  break;
590 
591  case e_ulonglong:
592  m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8);
593  success = true;
594  break;
595 
596  case e_sint128:
597  case e_uint128:
598  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128);
599  success = true;
600  break;
601 
602  case e_sint256:
603  case e_uint256:
604  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256);
605  success = true;
606  break;
607 
608  case e_sint512:
609  case e_uint512:
610  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT512);
611  success = true;
612  break;
613 
614  case e_float:
615  m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
616  m_float.convertFromAPInt(m_integer, true,
617  llvm::APFloat::rmNearestTiesToEven);
618  success = true;
619  break;
620 
621  case e_double:
622  m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
623  m_float.convertFromAPInt(m_integer, true,
624  llvm::APFloat::rmNearestTiesToEven);
625  success = true;
626  break;
627 
628  case e_long_double:
629  m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
630  : llvm::APFloat::x87DoubleExtended());
631  m_float.convertFromAPInt(m_integer, true,
632  llvm::APFloat::rmNearestTiesToEven);
633  success = true;
634  break;
635  }
636  break;
637 
638  case e_ulong:
639  switch (type) {
640  case e_void:
641  case e_sint:
642  case e_uint:
643  case e_slong:
644  break;
645  case e_ulong:
646  success = true;
647  break;
648  case e_slonglong:
649  m_integer = m_integer.zextOrTrunc(sizeof(slonglong_t) * 8);
650  success = true;
651  break;
652 
653  case e_ulonglong:
654  m_integer = m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8);
655  success = true;
656  break;
657 
658  case e_sint128:
659  case e_uint128:
660  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128);
661  success = true;
662  break;
663 
664  case e_sint256:
665  case e_uint256:
666  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256);
667  success = true;
668  break;
669 
670  case e_sint512:
671  case e_uint512:
672  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
673  success = true;
674  break;
675 
676  case e_float:
677  m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
678  m_float.convertFromAPInt(m_integer, false,
679  llvm::APFloat::rmNearestTiesToEven);
680  success = true;
681  break;
682 
683  case e_double:
684  m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
685  m_float.convertFromAPInt(m_integer, false,
686  llvm::APFloat::rmNearestTiesToEven);
687  success = true;
688  break;
689 
690  case e_long_double:
691  m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
692  : llvm::APFloat::x87DoubleExtended());
693  m_float.convertFromAPInt(m_integer, false,
694  llvm::APFloat::rmNearestTiesToEven);
695  success = true;
696  break;
697  }
698  break;
699 
700  case e_slonglong:
701  switch (type) {
702  case e_void:
703  case e_sint:
704  case e_uint:
705  case e_slong:
706  case e_ulong:
707  break;
708  case e_slonglong:
709  success = true;
710  break;
711  case e_ulonglong:
712  m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8);
713  success = true;
714  break;
715 
716  case e_sint128:
717  case e_uint128:
718  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128);
719  success = true;
720  break;
721 
722  case e_sint256:
723  case e_uint256:
724  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256);
725  success = true;
726  break;
727 
728  case e_sint512:
729  case e_uint512:
730  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT512);
731  success = true;
732  break;
733 
734  case e_float:
735  m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
736  m_float.convertFromAPInt(m_integer, true,
737  llvm::APFloat::rmNearestTiesToEven);
738  success = true;
739  break;
740 
741  case e_double:
742  m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
743  m_float.convertFromAPInt(m_integer, true,
744  llvm::APFloat::rmNearestTiesToEven);
745  success = true;
746  break;
747 
748  case e_long_double:
749  m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
750  : llvm::APFloat::x87DoubleExtended());
751  m_float.convertFromAPInt(m_integer, true,
752  llvm::APFloat::rmNearestTiesToEven);
753  success = true;
754  break;
755  }
756  break;
757 
758  case e_ulonglong:
759  switch (type) {
760  case e_void:
761  case e_sint:
762  case e_uint:
763  case e_slong:
764  case e_ulong:
765  case e_slonglong:
766  break;
767  case e_ulonglong:
768  success = true;
769  break;
770  case e_sint128:
771  case e_uint128:
772  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128);
773  success = true;
774  break;
775 
776  case e_sint256:
777  case e_uint256:
778  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256);
779  success = true;
780  break;
781 
782  case e_sint512:
783  case e_uint512:
784  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
785  success = true;
786  break;
787 
788  case e_float:
789  m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
790  m_float.convertFromAPInt(m_integer, false,
791  llvm::APFloat::rmNearestTiesToEven);
792  success = true;
793  break;
794 
795  case e_double:
796  m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
797  m_float.convertFromAPInt(m_integer, false,
798  llvm::APFloat::rmNearestTiesToEven);
799  success = true;
800  break;
801 
802  case e_long_double:
803  m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
804  : llvm::APFloat::x87DoubleExtended());
805  m_float.convertFromAPInt(m_integer, false,
806  llvm::APFloat::rmNearestTiesToEven);
807  success = true;
808  break;
809  }
810  break;
811 
812  case e_sint128:
813  switch (type) {
814  case e_void:
815  case e_sint:
816  case e_uint:
817  case e_slong:
818  case e_ulong:
819  case e_slonglong:
820  case e_ulonglong:
821  break;
822  case e_sint128:
823  success = true;
824  break;
825  case e_uint128:
826  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128);
827  success = true;
828  break;
829 
830  case e_sint256:
831  case e_uint256:
832  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256);
833  success = true;
834  break;
835 
836  case e_sint512:
837  case e_uint512:
838  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT512);
839  success = true;
840  break;
841 
842  case e_float:
843  m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
844  m_float.convertFromAPInt(m_integer, true,
845  llvm::APFloat::rmNearestTiesToEven);
846  success = true;
847  break;
848 
849  case e_double:
850  m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
851  m_float.convertFromAPInt(m_integer, true,
852  llvm::APFloat::rmNearestTiesToEven);
853  success = true;
854  break;
855 
856  case e_long_double:
857  m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
858  : llvm::APFloat::x87DoubleExtended());
859  m_float.convertFromAPInt(m_integer, true,
860  llvm::APFloat::rmNearestTiesToEven);
861  success = true;
862  break;
863  }
864  break;
865 
866  case e_uint128:
867  switch (type) {
868  case e_void:
869  case e_sint:
870  case e_uint:
871  case e_slong:
872  case e_ulong:
873  case e_slonglong:
874  case e_ulonglong:
875  case e_sint128:
876  break;
877  case e_uint128:
878  success = true;
879  break;
880  case e_sint256:
881  case e_uint256:
882  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256);
883  success = true;
884  break;
885 
886  case e_sint512:
887  case e_uint512:
888  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
889  success = true;
890  break;
891 
892  case e_float:
893  m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
894  m_float.convertFromAPInt(m_integer, false,
895  llvm::APFloat::rmNearestTiesToEven);
896  success = true;
897  break;
898 
899  case e_double:
900  m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
901  m_float.convertFromAPInt(m_integer, false,
902  llvm::APFloat::rmNearestTiesToEven);
903  success = true;
904  break;
905 
906  case e_long_double:
907  m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
908  : llvm::APFloat::x87DoubleExtended());
909  m_float.convertFromAPInt(m_integer, false,
910  llvm::APFloat::rmNearestTiesToEven);
911  success = true;
912  break;
913  }
914  break;
915 
916  case e_sint256:
917  switch (type) {
918  case e_void:
919  case e_sint:
920  case e_uint:
921  case e_slong:
922  case e_ulong:
923  case e_slonglong:
924  case e_ulonglong:
925  case e_sint128:
926  case e_uint128:
927  break;
928  case e_sint256:
929  success = true;
930  break;
931  case e_uint256:
932  m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256);
933  success = true;
934  break;
935 
936  case e_sint512:
937  case e_uint512:
938  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
939  success = true;
940  break;
941 
942  case e_float:
943  m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
944  m_float.convertFromAPInt(m_integer, true,
945  llvm::APFloat::rmNearestTiesToEven);
946  success = true;
947  break;
948 
949  case e_double:
950  m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
951  m_float.convertFromAPInt(m_integer, true,
952  llvm::APFloat::rmNearestTiesToEven);
953  success = true;
954  break;
955 
956  case e_long_double:
957  m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
958  : llvm::APFloat::x87DoubleExtended());
959  m_float.convertFromAPInt(m_integer, true,
960  llvm::APFloat::rmNearestTiesToEven);
961  success = true;
962  break;
963  }
964  break;
965 
966  case e_uint256:
967  switch (type) {
968  case e_void:
969  case e_sint:
970  case e_uint:
971  case e_slong:
972  case e_ulong:
973  case e_slonglong:
974  case e_ulonglong:
975  case e_sint128:
976  case e_uint128:
977  case e_sint256:
978  break;
979  case e_uint256:
980  success = true;
981  break;
982 
983  case e_sint512:
984  case e_uint512:
985  m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
986  success = true;
987  break;
988 
989  case e_float:
990  m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
991  m_float.convertFromAPInt(m_integer, false,
992  llvm::APFloat::rmNearestTiesToEven);
993  success = true;
994  break;
995 
996  case e_double:
997  m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
998  m_float.convertFromAPInt(m_integer, false,
999  llvm::APFloat::rmNearestTiesToEven);
1000  success = true;
1001  break;
1002 
1003  case e_long_double:
1004  m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
1005  : llvm::APFloat::x87DoubleExtended());
1006  m_float.convertFromAPInt(m_integer, false,
1007  llvm::APFloat::rmNearestTiesToEven);
1008  success = true;
1009  break;
1010  }
1011  break;
1012 
1013  case e_sint512:
1014  case e_uint512:
1015  lldbassert(false && "unimplemented");
1016  break;
1017 
1018  case e_float:
1019  switch (type) {
1020  case e_void:
1021  case e_sint:
1022  case e_uint:
1023  case e_slong:
1024  case e_ulong:
1025  case e_slonglong:
1026  case e_ulonglong:
1027  case e_sint128:
1028  case e_uint128:
1029  case e_sint256:
1030  case e_uint256:
1031  case e_uint512:
1032  case e_sint512:
1033  break;
1034  case e_float:
1035  success = true;
1036  break;
1037  case e_double:
1038  m_float = llvm::APFloat((double_t)m_float.convertToFloat());
1039  success = true;
1040  break;
1041 
1042  case e_long_double: {
1043  bool ignore;
1044  m_float.convert(m_ieee_quad ? llvm::APFloat::IEEEquad()
1045  : llvm::APFloat::x87DoubleExtended(),
1046  llvm::APFloat::rmNearestTiesToEven, &ignore);
1047  success = true;
1048  break;
1049  }
1050  }
1051  break;
1052 
1053  case e_double:
1054  switch (type) {
1055  case e_void:
1056  case e_sint:
1057  case e_uint:
1058  case e_slong:
1059  case e_ulong:
1060  case e_slonglong:
1061  case e_ulonglong:
1062  case e_sint128:
1063  case e_uint128:
1064  case e_sint256:
1065  case e_uint256:
1066  case e_sint512:
1067  case e_uint512:
1068  case e_float:
1069  break;
1070  case e_double:
1071  success = true;
1072  break;
1073  case e_long_double: {
1074  bool ignore;
1075  m_float.convert(m_ieee_quad ? llvm::APFloat::IEEEquad()
1076  : llvm::APFloat::x87DoubleExtended(),
1077  llvm::APFloat::rmNearestTiesToEven, &ignore);
1078  success = true;
1079  break;
1080  }
1081  }
1082  break;
1083 
1084  case e_long_double:
1085  switch (type) {
1086  case e_void:
1087  case e_sint:
1088  case e_uint:
1089  case e_slong:
1090  case e_ulong:
1091  case e_slonglong:
1092  case e_ulonglong:
1093  case e_sint128:
1094  case e_uint128:
1095  case e_sint256:
1096  case e_uint256:
1097  case e_sint512:
1098  case e_uint512:
1099  case e_float:
1100  case e_double:
1101  break;
1102  case e_long_double:
1103  success = true;
1104  break;
1105  }
1106  break;
1107  }
1108 
1109  if (success)
1110  m_type = type;
1111  return success;
1112 }
1113 
1115  switch (type) {
1116  case e_void:
1117  return "void";
1118  case e_sint:
1119  return "int";
1120  case e_uint:
1121  return "unsigned int";
1122  case e_slong:
1123  return "long";
1124  case e_ulong:
1125  return "unsigned long";
1126  case e_slonglong:
1127  return "long long";
1128  case e_ulonglong:
1129  return "unsigned long long";
1130  case e_float:
1131  return "float";
1132  case e_double:
1133  return "double";
1134  case e_long_double:
1135  return "long double";
1136  case e_sint128:
1137  return "int128_t";
1138  case e_uint128:
1139  return "uint128_t";
1140  case e_sint256:
1141  return "int256_t";
1142  case e_uint256:
1143  return "uint256_t";
1144  case e_sint512:
1145  return "int512_t";
1146  case e_uint512:
1147  return "uint512_t";
1148  }
1149  return "???";
1150 }
1151 
1154  if (byte_size <= sizeof(sint_t))
1155  return e_sint;
1156  if (byte_size <= sizeof(slong_t))
1157  return e_slong;
1158  if (byte_size <= sizeof(slonglong_t))
1159  return e_slonglong;
1160  return e_void;
1161 }
1162 
1165  if (byte_size <= sizeof(uint_t))
1166  return e_uint;
1167  if (byte_size <= sizeof(ulong_t))
1168  return e_ulong;
1169  if (byte_size <= sizeof(ulonglong_t))
1170  return e_ulonglong;
1171  return e_void;
1172 }
1173 
1175  if (byte_size == sizeof(float_t))
1176  return e_float;
1177  if (byte_size == sizeof(double_t))
1178  return e_double;
1179  if (byte_size == sizeof(long_double_t))
1180  return e_long_double;
1181  return e_void;
1182 }
1183 
1185  bool success = false;
1186 
1187  switch (m_type) {
1188  case e_void:
1189  break;
1190  case e_sint:
1191  success = true;
1192  break;
1193  case e_uint:
1194  m_type = e_sint;
1195  success = true;
1196  break;
1197  case e_slong:
1198  success = true;
1199  break;
1200  case e_ulong:
1201  m_type = e_slong;
1202  success = true;
1203  break;
1204  case e_slonglong:
1205  success = true;
1206  break;
1207  case e_ulonglong:
1208  m_type = e_slonglong;
1209  success = true;
1210  break;
1211  case e_sint128:
1212  success = true;
1213  break;
1214  case e_uint128:
1215  m_type = e_sint128;
1216  success = true;
1217  break;
1218  case e_sint256:
1219  success = true;
1220  break;
1221  case e_uint256:
1222  m_type = e_sint256;
1223  success = true;
1224  break;
1225  case e_sint512:
1226  success = true;
1227  break;
1228  case e_uint512:
1229  m_type = e_sint512;
1230  success = true;
1231  break;
1232  case e_float:
1233  success = true;
1234  break;
1235  case e_double:
1236  success = true;
1237  break;
1238  case e_long_double:
1239  success = true;
1240  break;
1241  }
1242 
1243  return success;
1244 }
1245 
1247  bool success = false;
1248 
1249  switch (m_type) {
1250  case e_void:
1251  break;
1252  case e_sint:
1253  m_type = e_uint;
1254  success = true;
1255  break;
1256  case e_uint:
1257  success = true;
1258  break;
1259  case e_slong:
1260  m_type = e_ulong;
1261  success = true;
1262  break;
1263  case e_ulong:
1264  success = true;
1265  break;
1266  case e_slonglong:
1267  m_type = e_ulonglong;
1268  success = true;
1269  break;
1270  case e_ulonglong:
1271  success = true;
1272  break;
1273  case e_sint128:
1274  m_type = e_uint128;
1275  success = true;
1276  break;
1277  case e_uint128:
1278  success = true;
1279  break;
1280  case e_sint256:
1281  m_type = e_uint256;
1282  success = true;
1283  break;
1284  case e_uint256:
1285  success = true;
1286  break;
1287  case e_sint512:
1288  m_type = e_uint512;
1289  success = true;
1290  break;
1291  case e_uint512:
1292  success = true;
1293  break;
1294  case e_float:
1295  success = true;
1296  break;
1297  case e_double:
1298  success = true;
1299  break;
1300  case e_long_double:
1301  success = true;
1302  break;
1303  }
1304 
1305  return success;
1306 }
1307 
1308 signed char Scalar::SChar(char fail_value) const {
1309  switch (m_type) {
1310  case e_void:
1311  break;
1312  case e_sint:
1313  case e_uint:
1314  case e_slong:
1315  case e_ulong:
1316  case e_slonglong:
1317  case e_ulonglong:
1318  case e_sint128:
1319  case e_uint128:
1320  case e_sint256:
1321  case e_uint256:
1322  case e_sint512:
1323  case e_uint512:
1324  return (schar_t)(m_integer.sextOrTrunc(sizeof(schar_t) * 8)).getSExtValue();
1325  case e_float:
1326  return (schar_t)m_float.convertToFloat();
1327  case e_double:
1328  return (schar_t)m_float.convertToDouble();
1329  case e_long_double:
1330  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1331  return (schar_t)(ldbl_val.sextOrTrunc(sizeof(schar_t) * 8)).getSExtValue();
1332  }
1333  return fail_value;
1334 }
1335 
1336 unsigned char Scalar::UChar(unsigned char fail_value) const {
1337  switch (m_type) {
1338  case e_void:
1339  break;
1340  case e_sint:
1341  case e_uint:
1342  case e_slong:
1343  case e_ulong:
1344  case e_slonglong:
1345  case e_ulonglong:
1346  case e_sint128:
1347  case e_uint128:
1348  case e_sint256:
1349  case e_uint256:
1350  case e_sint512:
1351  case e_uint512:
1352  return (uchar_t)(m_integer.zextOrTrunc(sizeof(uchar_t) * 8)).getZExtValue();
1353  case e_float:
1354  return (uchar_t)m_float.convertToFloat();
1355  case e_double:
1356  return (uchar_t)m_float.convertToDouble();
1357  case e_long_double:
1358  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1359  return (uchar_t)(ldbl_val.zextOrTrunc(sizeof(uchar_t) * 8)).getZExtValue();
1360  }
1361  return fail_value;
1362 }
1363 
1364 short Scalar::SShort(short fail_value) const {
1365  switch (m_type) {
1366  case e_void:
1367  break;
1368  case e_sint:
1369  case e_uint:
1370  case e_slong:
1371  case e_ulong:
1372  case e_slonglong:
1373  case e_ulonglong:
1374  case e_sint128:
1375  case e_uint128:
1376  case e_sint256:
1377  case e_uint256:
1378  case e_sint512:
1379  case e_uint512:
1380  return (sshort_t)(m_integer.sextOrTrunc(sizeof(sshort_t) * 8))
1381  .getSExtValue();
1382  case e_float:
1383  return (sshort_t)m_float.convertToFloat();
1384  case e_double:
1385  return (sshort_t)m_float.convertToDouble();
1386  case e_long_double:
1387  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1388  return (sshort_t)(ldbl_val.sextOrTrunc(sizeof(sshort_t) * 8))
1389  .getSExtValue();
1390  }
1391  return fail_value;
1392 }
1393 
1394 unsigned short Scalar::UShort(unsigned short fail_value) const {
1395  switch (m_type) {
1396  case e_void:
1397  break;
1398  case e_sint:
1399  case e_uint:
1400  case e_slong:
1401  case e_ulong:
1402  case e_slonglong:
1403  case e_ulonglong:
1404  case e_sint128:
1405  case e_uint128:
1406  case e_sint256:
1407  case e_uint256:
1408  case e_sint512:
1409  case e_uint512:
1410  return (ushort_t)(m_integer.zextOrTrunc(sizeof(ushort_t) * 8))
1411  .getZExtValue();
1412  case e_float:
1413  return (ushort_t)m_float.convertToFloat();
1414  case e_double:
1415  return (ushort_t)m_float.convertToDouble();
1416  case e_long_double:
1417  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1418  return (ushort_t)(ldbl_val.zextOrTrunc(sizeof(ushort_t) * 8))
1419  .getZExtValue();
1420  }
1421  return fail_value;
1422 }
1423 
1424 int Scalar::SInt(int fail_value) const {
1425  switch (m_type) {
1426  case e_void:
1427  break;
1428  case e_sint:
1429  case e_uint:
1430  case e_slong:
1431  case e_ulong:
1432  case e_slonglong:
1433  case e_ulonglong:
1434  case e_sint128:
1435  case e_uint128:
1436  case e_sint256:
1437  case e_uint256:
1438  case e_sint512:
1439  case e_uint512:
1440  return (sint_t)(m_integer.sextOrTrunc(sizeof(sint_t) * 8)).getSExtValue();
1441  case e_float:
1442  return (sint_t)m_float.convertToFloat();
1443  case e_double:
1444  return (sint_t)m_float.convertToDouble();
1445  case e_long_double:
1446  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1447  return (sint_t)(ldbl_val.sextOrTrunc(sizeof(sint_t) * 8)).getSExtValue();
1448  }
1449  return fail_value;
1450 }
1451 
1452 unsigned int Scalar::UInt(unsigned int fail_value) const {
1453  switch (m_type) {
1454  case e_void:
1455  break;
1456  case e_sint:
1457  case e_uint:
1458  case e_slong:
1459  case e_ulong:
1460  case e_slonglong:
1461  case e_ulonglong:
1462  case e_sint128:
1463  case e_uint128:
1464  case e_sint256:
1465  case e_uint256:
1466  case e_sint512:
1467  case e_uint512:
1468  return (uint_t)(m_integer.zextOrTrunc(sizeof(uint_t) * 8)).getZExtValue();
1469  case e_float:
1470  return (uint_t)m_float.convertToFloat();
1471  case e_double:
1472  return (uint_t)m_float.convertToDouble();
1473  case e_long_double:
1474  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1475  return (uint_t)(ldbl_val.zextOrTrunc(sizeof(uint_t) * 8)).getZExtValue();
1476  }
1477  return fail_value;
1478 }
1479 
1480 long Scalar::SLong(long fail_value) const {
1481  switch (m_type) {
1482  case e_void:
1483  break;
1484  case e_sint:
1485  case e_uint:
1486  case e_slong:
1487  case e_ulong:
1488  case e_slonglong:
1489  case e_ulonglong:
1490  case e_sint128:
1491  case e_uint128:
1492  case e_sint256:
1493  case e_uint256:
1494  case e_sint512:
1495  case e_uint512:
1496  return (slong_t)(m_integer.sextOrTrunc(sizeof(slong_t) * 8)).getSExtValue();
1497  case e_float:
1498  return (slong_t)m_float.convertToFloat();
1499  case e_double:
1500  return (slong_t)m_float.convertToDouble();
1501  case e_long_double:
1502  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1503  return (slong_t)(ldbl_val.sextOrTrunc(sizeof(slong_t) * 8)).getSExtValue();
1504  }
1505  return fail_value;
1506 }
1507 
1508 unsigned long Scalar::ULong(unsigned long fail_value) const {
1509  switch (m_type) {
1510  case e_void:
1511  break;
1512  case e_sint:
1513  case e_uint:
1514  case e_slong:
1515  case e_ulong:
1516  case e_slonglong:
1517  case e_ulonglong:
1518  case e_sint128:
1519  case e_uint128:
1520  case e_sint256:
1521  case e_uint256:
1522  case e_sint512:
1523  case e_uint512:
1524  return (ulong_t)(m_integer.zextOrTrunc(sizeof(ulong_t) * 8)).getZExtValue();
1525  case e_float:
1526  return (ulong_t)m_float.convertToFloat();
1527  case e_double:
1528  return (ulong_t)m_float.convertToDouble();
1529  case e_long_double:
1530  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1531  return (ulong_t)(ldbl_val.zextOrTrunc(sizeof(ulong_t) * 8)).getZExtValue();
1532  }
1533  return fail_value;
1534 }
1535 
1536 long long Scalar::SLongLong(long long fail_value) const {
1537  switch (m_type) {
1538  case e_void:
1539  break;
1540  case e_sint:
1541  case e_uint:
1542  case e_slong:
1543  case e_ulong:
1544  case e_slonglong:
1545  case e_ulonglong:
1546  case e_sint128:
1547  case e_uint128:
1548  case e_sint256:
1549  case e_uint256:
1550  case e_sint512:
1551  case e_uint512:
1552  return (slonglong_t)(m_integer.sextOrTrunc(sizeof(slonglong_t) * 8))
1553  .getSExtValue();
1554  case e_float:
1555  return (slonglong_t)m_float.convertToFloat();
1556  case e_double:
1557  return (slonglong_t)m_float.convertToDouble();
1558  case e_long_double:
1559  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1560  return (slonglong_t)(ldbl_val.sextOrTrunc(sizeof(slonglong_t) * 8))
1561  .getSExtValue();
1562  }
1563  return fail_value;
1564 }
1565 
1566 unsigned long long Scalar::ULongLong(unsigned long long fail_value) const {
1567  switch (m_type) {
1568  case e_void:
1569  break;
1570  case e_sint:
1571  case e_uint:
1572  case e_slong:
1573  case e_ulong:
1574  case e_slonglong:
1575  case e_ulonglong:
1576  case e_sint128:
1577  case e_uint128:
1578  case e_sint256:
1579  case e_uint256:
1580  case e_sint512:
1581  case e_uint512:
1582  return (ulonglong_t)(m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8))
1583  .getZExtValue();
1584  case e_float:
1585  return (ulonglong_t)m_float.convertToFloat();
1586  case e_double: {
1587  double d_val = m_float.convertToDouble();
1588  llvm::APInt rounded_double =
1589  llvm::APIntOps::RoundDoubleToAPInt(d_val, sizeof(ulonglong_t) * 8);
1590  return (ulonglong_t)(rounded_double.zextOrTrunc(sizeof(ulonglong_t) * 8))
1591  .getZExtValue();
1592  }
1593  case e_long_double:
1594  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1595  return (ulonglong_t)(ldbl_val.zextOrTrunc(sizeof(ulonglong_t) * 8))
1596  .getZExtValue();
1597  }
1598  return fail_value;
1599 }
1600 
1601 llvm::APInt Scalar::SInt128(llvm::APInt &fail_value) const {
1602  switch (m_type) {
1603  case e_void:
1604  break;
1605  case e_sint:
1606  case e_uint:
1607  case e_slong:
1608  case e_ulong:
1609  case e_slonglong:
1610  case e_ulonglong:
1611  case e_sint128:
1612  case e_uint128:
1613  case e_sint256:
1614  case e_uint256:
1615  case e_sint512:
1616  case e_uint512:
1617  return m_integer;
1618  case e_float:
1619  case e_double:
1620  case e_long_double:
1621  return m_float.bitcastToAPInt();
1622  }
1623  return fail_value;
1624 }
1625 
1626 llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const {
1627  switch (m_type) {
1628  case e_void:
1629  break;
1630  case e_sint:
1631  case e_uint:
1632  case e_slong:
1633  case e_ulong:
1634  case e_slonglong:
1635  case e_ulonglong:
1636  case e_sint128:
1637  case e_uint128:
1638  case e_sint256:
1639  case e_uint256:
1640  case e_sint512:
1641  case e_uint512:
1642  return m_integer;
1643  case e_float:
1644  case e_double:
1645  case e_long_double:
1646  return m_float.bitcastToAPInt();
1647  }
1648  return fail_value;
1649 }
1650 
1651 float Scalar::Float(float fail_value) const {
1652  switch (m_type) {
1653  case e_void:
1654  break;
1655  case e_sint:
1656  case e_uint:
1657  case e_slong:
1658  case e_ulong:
1659  case e_slonglong:
1660  case e_ulonglong:
1661  case e_sint128:
1662  case e_uint128:
1663  case e_sint256:
1664  case e_uint256:
1665  case e_sint512:
1666  case e_uint512:
1667  return llvm::APIntOps::RoundAPIntToFloat(m_integer);
1668  case e_float:
1669  return m_float.convertToFloat();
1670  case e_double:
1671  return (float_t)m_float.convertToDouble();
1672  case e_long_double:
1673  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1674  return ldbl_val.bitsToFloat();
1675  }
1676  return fail_value;
1677 }
1678 
1679 double Scalar::Double(double fail_value) const {
1680  switch (m_type) {
1681  case e_void:
1682  break;
1683  case e_sint:
1684  case e_uint:
1685  case e_slong:
1686  case e_ulong:
1687  case e_slonglong:
1688  case e_ulonglong:
1689  case e_sint128:
1690  case e_uint128:
1691  case e_sint256:
1692  case e_uint256:
1693  case e_sint512:
1694  case e_uint512:
1695  return llvm::APIntOps::RoundAPIntToDouble(m_integer);
1696  case e_float:
1697  return (double_t)m_float.convertToFloat();
1698  case e_double:
1699  return m_float.convertToDouble();
1700  case e_long_double:
1701  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1702  return ldbl_val.bitsToFloat();
1703  }
1704  return fail_value;
1705 }
1706 
1707 long double Scalar::LongDouble(long double fail_value) const {
1708  switch (m_type) {
1709  case e_void:
1710  break;
1711  case e_sint:
1712  case e_uint:
1713  case e_slong:
1714  case e_ulong:
1715  case e_slonglong:
1716  case e_ulonglong:
1717  case e_sint128:
1718  case e_uint128:
1719  case e_sint256:
1720  case e_uint256:
1721  case e_sint512:
1722  case e_uint512:
1723  return (long_double_t)llvm::APIntOps::RoundAPIntToDouble(m_integer);
1724  case e_float:
1725  return (long_double_t)m_float.convertToFloat();
1726  case e_double:
1727  return (long_double_t)m_float.convertToDouble();
1728  case e_long_double:
1729  llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1730  return (long_double_t)ldbl_val.bitsToDouble();
1731  }
1732  return fail_value;
1733 }
1734 
1736  Scalar temp_value;
1737  const Scalar *a;
1738  const Scalar *b;
1739  if ((m_type = PromoteToMaxType(*this, rhs, temp_value, a, b)) !=
1740  Scalar::e_void) {
1741  switch (m_type) {
1742  case e_void:
1743  break;
1744  case e_sint:
1745  case e_uint:
1746  case e_slong:
1747  case e_ulong:
1748  case e_slonglong:
1749  case e_ulonglong:
1750  case e_sint128:
1751  case e_uint128:
1752  case e_sint256:
1753  case e_uint256:
1754  case e_sint512:
1755  case e_uint512:
1756  m_integer = a->m_integer + b->m_integer;
1757  break;
1758 
1759  case e_float:
1760  case e_double:
1761  case e_long_double:
1762  m_float = a->m_float + b->m_float;
1763  break;
1764  }
1765  }
1766  return *this;
1767 }
1768 
1770  switch (m_type) {
1771  case e_void:
1772  case e_float:
1773  case e_double:
1774  case e_long_double:
1775  m_type = e_void;
1776  break;
1777 
1778  case e_sint:
1779  case e_uint:
1780  case e_slong:
1781  case e_ulong:
1782  case e_slonglong:
1783  case e_ulonglong:
1784  case e_sint128:
1785  case e_uint128:
1786  case e_sint256:
1787  case e_uint256:
1788  case e_sint512:
1789  case e_uint512:
1790  switch (rhs.m_type) {
1791  case e_void:
1792  case e_float:
1793  case e_double:
1794  case e_long_double:
1795  m_type = e_void;
1796  break;
1797  case e_sint:
1798  case e_uint:
1799  case e_slong:
1800  case e_ulong:
1801  case e_slonglong:
1802  case e_ulonglong:
1803  case e_sint128:
1804  case e_uint128:
1805  case e_sint256:
1806  case e_uint256:
1807  case e_sint512:
1808  case e_uint512:
1809  m_integer = m_integer << rhs.m_integer;
1810  break;
1811  }
1812  break;
1813  }
1814  return *this;
1815 }
1816 
1818  switch (m_type) {
1819  case e_void:
1820  case e_float:
1821  case e_double:
1822  case e_long_double:
1823  m_type = e_void;
1824  break;
1825 
1826  case e_sint:
1827  case e_uint:
1828  case e_slong:
1829  case e_ulong:
1830  case e_slonglong:
1831  case e_ulonglong:
1832  case e_sint128:
1833  case e_uint128:
1834  case e_sint256:
1835  case e_uint256:
1836  case e_sint512:
1837  case e_uint512:
1838  switch (rhs.m_type) {
1839  case e_void:
1840  case e_float:
1841  case e_double:
1842  case e_long_double:
1843  m_type = e_void;
1844  break;
1845  case e_sint:
1846  case e_uint:
1847  case e_slong:
1848  case e_ulong:
1849  case e_slonglong:
1850  case e_ulonglong:
1851  case e_sint128:
1852  case e_uint128:
1853  case e_sint256:
1854  case e_uint256:
1855  case e_sint512:
1856  case e_uint512:
1857  m_integer = m_integer.lshr(rhs.m_integer);
1858  break;
1859  }
1860  break;
1861  }
1862  return m_type != e_void;
1863 }
1864 
1866  switch (m_type) {
1867  case e_void:
1868  case e_float:
1869  case e_double:
1870  case e_long_double:
1871  m_type = e_void;
1872  break;
1873 
1874  case e_sint:
1875  case e_uint:
1876  case e_slong:
1877  case e_ulong:
1878  case e_slonglong:
1879  case e_ulonglong:
1880  case e_sint128:
1881  case e_uint128:
1882  case e_sint256:
1883  case e_uint256:
1884  case e_sint512:
1885  case e_uint512:
1886  switch (rhs.m_type) {
1887  case e_void:
1888  case e_float:
1889  case e_double:
1890  case e_long_double:
1891  m_type = e_void;
1892  break;
1893  case e_sint:
1894  case e_uint:
1895  case e_slong:
1896  case e_ulong:
1897  case e_slonglong:
1898  case e_ulonglong:
1899  case e_sint128:
1900  case e_uint128:
1901  case e_sint256:
1902  case e_uint256:
1903  case e_sint512:
1904  case e_uint512:
1905  m_integer = m_integer.ashr(rhs.m_integer);
1906  break;
1907  }
1908  break;
1909  }
1910  return *this;
1911 }
1912 
1913 Scalar &Scalar::operator&=(const Scalar &rhs) {
1914  switch (m_type) {
1915  case e_void:
1916  case e_float:
1917  case e_double:
1918  case e_long_double:
1919  m_type = e_void;
1920  break;
1921 
1922  case e_sint:
1923  case e_uint:
1924  case e_slong:
1925  case e_ulong:
1926  case e_slonglong:
1927  case e_ulonglong:
1928  case e_sint128:
1929  case e_uint128:
1930  case e_sint256:
1931  case e_uint256:
1932  case e_sint512:
1933  case e_uint512:
1934  switch (rhs.m_type) {
1935  case e_void:
1936  case e_float:
1937  case e_double:
1938  case e_long_double:
1939  m_type = e_void;
1940  break;
1941  case e_sint:
1942  case e_uint:
1943  case e_slong:
1944  case e_ulong:
1945  case e_slonglong:
1946  case e_ulonglong:
1947  case e_sint128:
1948  case e_uint128:
1949  case e_sint256:
1950  case e_uint256:
1951  case e_sint512:
1952  case e_uint512:
1953  m_integer &= rhs.m_integer;
1954  break;
1955  }
1956  break;
1957  }
1958  return *this;
1959 }
1960 
1962  switch (m_type) {
1963  case e_void:
1964  break;
1965 
1966  case e_sint:
1967  case e_slong:
1968  case e_slonglong:
1969  case e_sint128:
1970  case e_sint256:
1971  case e_sint512:
1972  if (m_integer.isNegative())
1973  m_integer = -m_integer;
1974  return true;
1975 
1976  case e_uint:
1977  case e_ulong:
1978  case e_ulonglong:
1979  return true;
1980  case e_uint128:
1981  case e_uint256:
1982  case e_uint512:
1983  case e_float:
1984  case e_double:
1985  case e_long_double:
1986  m_float.clearSign();
1987  return true;
1988  }
1989  return false;
1990 }
1991 
1993  switch (m_type) {
1994  case e_void:
1995  break;
1996  case e_sint:
1997  case e_uint:
1998  case e_slong:
1999  case e_ulong:
2000  case e_slonglong:
2001  case e_ulonglong:
2002  case e_sint128:
2003  case e_uint128:
2004  case e_sint256:
2005  case e_uint256:
2006  case e_sint512:
2007  case e_uint512:
2008  m_integer = -m_integer;
2009  return true;
2010  case e_float:
2011  case e_double:
2012  case e_long_double:
2013  m_float.changeSign();
2014  return true;
2015  }
2016  return false;
2017 }
2018 
2020  switch (m_type) {
2021  case e_sint:
2022  case e_uint:
2023  case e_slong:
2024  case e_ulong:
2025  case e_slonglong:
2026  case e_ulonglong:
2027  case e_sint128:
2028  case e_uint128:
2029  case e_sint256:
2030  case e_uint256:
2031  case e_sint512:
2032  case e_uint512:
2033  m_integer = ~m_integer;
2034  return true;
2035 
2036  case e_void:
2037  case e_float:
2038  case e_double:
2039  case e_long_double:
2040  break;
2041  }
2042  return false;
2043 }
2044 
2045 const Scalar lldb_private::operator+(const Scalar &lhs, const Scalar &rhs) {
2046  Scalar result;
2047  Scalar temp_value;
2048  const Scalar *a;
2049  const Scalar *b;
2050  if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2051  Scalar::e_void) {
2052  switch (result.m_type) {
2053  case Scalar::e_void:
2054  break;
2055  case Scalar::e_sint:
2056  case Scalar::e_uint:
2057  case Scalar::e_slong:
2058  case Scalar::e_ulong:
2059  case Scalar::e_slonglong:
2060  case Scalar::e_ulonglong:
2061  case Scalar::e_sint128:
2062  case Scalar::e_uint128:
2063  case Scalar::e_sint256:
2064  case Scalar::e_uint256:
2065  case Scalar::e_sint512:
2066  case Scalar::e_uint512:
2067  result.m_integer = a->m_integer + b->m_integer;
2068  break;
2069  case Scalar::e_float:
2070  case Scalar::e_double:
2071  case Scalar::e_long_double:
2072  result.m_float = a->m_float + b->m_float;
2073  break;
2074  }
2075  }
2076  return result;
2077 }
2078 
2079 const Scalar lldb_private::operator-(const Scalar &lhs, const Scalar &rhs) {
2080  Scalar result;
2081  Scalar temp_value;
2082  const Scalar *a;
2083  const Scalar *b;
2084  if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2085  Scalar::e_void) {
2086  switch (result.m_type) {
2087  case Scalar::e_void:
2088  break;
2089  case Scalar::e_sint:
2090  case Scalar::e_uint:
2091  case Scalar::e_slong:
2092  case Scalar::e_ulong:
2093  case Scalar::e_slonglong:
2094  case Scalar::e_ulonglong:
2095  case Scalar::e_sint128:
2096  case Scalar::e_uint128:
2097  case Scalar::e_sint256:
2098  case Scalar::e_uint256:
2099  case Scalar::e_sint512:
2100  case Scalar::e_uint512:
2101  result.m_integer = a->m_integer - b->m_integer;
2102  break;
2103  case Scalar::e_float:
2104  case Scalar::e_double:
2105  case Scalar::e_long_double:
2106  result.m_float = a->m_float - b->m_float;
2107  break;
2108  }
2109  }
2110  return result;
2111 }
2112 
2113 const Scalar lldb_private::operator/(const Scalar &lhs, const Scalar &rhs) {
2114  Scalar result;
2115  Scalar temp_value;
2116  const Scalar *a;
2117  const Scalar *b;
2118  if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2119  Scalar::e_void) {
2120  switch (result.m_type) {
2121  case Scalar::e_void:
2122  break;
2123  case Scalar::e_sint:
2124  case Scalar::e_slong:
2125  case Scalar::e_slonglong:
2126  case Scalar::e_sint128:
2127  case Scalar::e_sint256:
2128  case Scalar::e_sint512:
2129  if (b->m_integer != 0) {
2130  result.m_integer = a->m_integer.sdiv(b->m_integer);
2131  return result;
2132  }
2133  break;
2134  case Scalar::e_uint:
2135  case Scalar::e_ulong:
2136  case Scalar::e_ulonglong:
2137  case Scalar::e_uint128:
2138  case Scalar::e_uint256:
2139  case Scalar::e_uint512:
2140  if (b->m_integer != 0) {
2141  result.m_integer = a->m_integer.udiv(b->m_integer);
2142  return result;
2143  }
2144  break;
2145  case Scalar::e_float:
2146  case Scalar::e_double:
2147  case Scalar::e_long_double:
2148  if (!b->m_float.isZero()) {
2149  result.m_float = a->m_float / b->m_float;
2150  return result;
2151  }
2152  break;
2153  }
2154  }
2155  // For division only, the only way it should make it here is if a promotion
2156  // failed, or if we are trying to do a divide by zero.
2157  result.m_type = Scalar::e_void;
2158  return result;
2159 }
2160 
2161 const Scalar lldb_private::operator*(const Scalar &lhs, const Scalar &rhs) {
2162  Scalar result;
2163  Scalar temp_value;
2164  const Scalar *a;
2165  const Scalar *b;
2166  if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2167  Scalar::e_void) {
2168  switch (result.m_type) {
2169  case Scalar::e_void:
2170  break;
2171  case Scalar::e_sint:
2172  case Scalar::e_uint:
2173  case Scalar::e_slong:
2174  case Scalar::e_ulong:
2175  case Scalar::e_slonglong:
2176  case Scalar::e_ulonglong:
2177  case Scalar::e_sint128:
2178  case Scalar::e_uint128:
2179  case Scalar::e_sint256:
2180  case Scalar::e_uint256:
2181  case Scalar::e_sint512:
2182  case Scalar::e_uint512:
2183  result.m_integer = a->m_integer * b->m_integer;
2184  break;
2185  case Scalar::e_float:
2186  case Scalar::e_double:
2187  case Scalar::e_long_double:
2188  result.m_float = a->m_float * b->m_float;
2189  break;
2190  }
2191  }
2192  return result;
2193 }
2194 
2195 const Scalar lldb_private::operator&(const Scalar &lhs, const Scalar &rhs) {
2196  Scalar result;
2197  Scalar temp_value;
2198  const Scalar *a;
2199  const Scalar *b;
2200  if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2201  Scalar::e_void) {
2202  switch (result.m_type) {
2203  case Scalar::e_sint:
2204  case Scalar::e_uint:
2205  case Scalar::e_slong:
2206  case Scalar::e_ulong:
2207  case Scalar::e_slonglong:
2208  case Scalar::e_ulonglong:
2209  case Scalar::e_sint128:
2210  case Scalar::e_uint128:
2211  case Scalar::e_sint256:
2212  case Scalar::e_uint256:
2213  case Scalar::e_sint512:
2214  case Scalar::e_uint512:
2215  result.m_integer = a->m_integer & b->m_integer;
2216  break;
2217  case Scalar::e_void:
2218  case Scalar::e_float:
2219  case Scalar::e_double:
2220  case Scalar::e_long_double:
2221  // No bitwise AND on floats, doubles of long doubles
2222  result.m_type = Scalar::e_void;
2223  break;
2224  }
2225  }
2226  return result;
2227 }
2228 
2229 const Scalar lldb_private::operator|(const Scalar &lhs, const Scalar &rhs) {
2230  Scalar result;
2231  Scalar temp_value;
2232  const Scalar *a;
2233  const Scalar *b;
2234  if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2235  Scalar::e_void) {
2236  switch (result.m_type) {
2237  case Scalar::e_sint:
2238  case Scalar::e_uint:
2239  case Scalar::e_slong:
2240  case Scalar::e_ulong:
2241  case Scalar::e_slonglong:
2242  case Scalar::e_ulonglong:
2243  case Scalar::e_sint128:
2244  case Scalar::e_uint128:
2245  case Scalar::e_sint256:
2246  case Scalar::e_uint256:
2247  case Scalar::e_sint512:
2248  case Scalar::e_uint512:
2249  result.m_integer = a->m_integer | b->m_integer;
2250  break;
2251 
2252  case Scalar::e_void:
2253  case Scalar::e_float:
2254  case Scalar::e_double:
2255  case Scalar::e_long_double:
2256  // No bitwise AND on floats, doubles of long doubles
2257  result.m_type = Scalar::e_void;
2258  break;
2259  }
2260  }
2261  return result;
2262 }
2263 
2264 const Scalar lldb_private::operator%(const Scalar &lhs, const Scalar &rhs) {
2265  Scalar result;
2266  Scalar temp_value;
2267  const Scalar *a;
2268  const Scalar *b;
2269  if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2270  Scalar::e_void) {
2271  switch (result.m_type) {
2272  default:
2273  break;
2274  case Scalar::e_void:
2275  break;
2276  case Scalar::e_sint:
2277  case Scalar::e_slong:
2278  case Scalar::e_slonglong:
2279  case Scalar::e_sint128:
2280  case Scalar::e_sint256:
2281  case Scalar::e_sint512:
2282  if (b->m_integer != 0) {
2283  result.m_integer = a->m_integer.srem(b->m_integer);
2284  return result;
2285  }
2286  break;
2287  case Scalar::e_uint:
2288  case Scalar::e_ulong:
2289  case Scalar::e_ulonglong:
2290  case Scalar::e_uint128:
2291  case Scalar::e_uint256:
2292  case Scalar::e_uint512:
2293  if (b->m_integer != 0) {
2294  result.m_integer = a->m_integer.urem(b->m_integer);
2295  return result;
2296  }
2297  break;
2298  }
2299  }
2300  result.m_type = Scalar::e_void;
2301  return result;
2302 }
2303 
2304 const Scalar lldb_private::operator^(const Scalar &lhs, const Scalar &rhs) {
2305  Scalar result;
2306  Scalar temp_value;
2307  const Scalar *a;
2308  const Scalar *b;
2309  if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2310  Scalar::e_void) {
2311  switch (result.m_type) {
2312  case Scalar::e_sint:
2313  case Scalar::e_uint:
2314  case Scalar::e_slong:
2315  case Scalar::e_ulong:
2316  case Scalar::e_slonglong:
2317  case Scalar::e_ulonglong:
2318  case Scalar::e_sint128:
2319  case Scalar::e_uint128:
2320  case Scalar::e_sint256:
2321  case Scalar::e_uint256:
2322  case Scalar::e_sint512:
2323  case Scalar::e_uint512:
2324  result.m_integer = a->m_integer ^ b->m_integer;
2325  break;
2326 
2327  case Scalar::e_void:
2328  case Scalar::e_float:
2329  case Scalar::e_double:
2330  case Scalar::e_long_double:
2331  // No bitwise AND on floats, doubles of long doubles
2332  result.m_type = Scalar::e_void;
2333  break;
2334  }
2335  }
2336  return result;
2337 }
2338 
2339 const Scalar lldb_private::operator<<(const Scalar &lhs, const Scalar &rhs) {
2340  Scalar result = lhs;
2341  result <<= rhs;
2342  return result;
2343 }
2344 
2345 const Scalar lldb_private::operator>>(const Scalar &lhs, const Scalar &rhs) {
2346  Scalar result = lhs;
2347  result >>= rhs;
2348  return result;
2349 }
2350 
2351 Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding,
2352  size_t byte_size) {
2353  Status error;
2354  if (value_str == nullptr || value_str[0] == '\0') {
2355  error.SetErrorString("Invalid c-string value string.");
2356  return error;
2357  }
2358  switch (encoding) {
2359  case eEncodingInvalid:
2360  error.SetErrorString("Invalid encoding.");
2361  break;
2362 
2363  case eEncodingUint:
2364  if (byte_size <= sizeof(uint64_t)) {
2365  uint64_t uval64;
2366  if (!llvm::to_integer(value_str, uval64))
2368  "'%s' is not a valid unsigned integer string value", value_str);
2369  else if (!UIntValueIsValidForSize(uval64, byte_size))
2370  error.SetErrorStringWithFormat("value 0x%" PRIx64
2371  " is too large to fit in a %" PRIu64
2372  " byte unsigned integer value",
2373  uval64, (uint64_t)byte_size);
2374  else {
2376  switch (m_type) {
2377  case e_uint:
2378  m_integer = llvm::APInt(sizeof(uint_t) * 8, uval64, false);
2379  break;
2380  case e_ulong:
2381  m_integer = llvm::APInt(sizeof(ulong_t) * 8, uval64, false);
2382  break;
2383  case e_ulonglong:
2384  m_integer = llvm::APInt(sizeof(ulonglong_t) * 8, uval64, false);
2385  break;
2386  default:
2388  "unsupported unsigned integer byte size: %" PRIu64 "",
2389  (uint64_t)byte_size);
2390  break;
2391  }
2392  }
2393  } else {
2395  "unsupported unsigned integer byte size: %" PRIu64 "",
2396  (uint64_t)byte_size);
2397  return error;
2398  }
2399  break;
2400 
2401  case eEncodingSint:
2402  if (byte_size <= sizeof(int64_t)) {
2403  int64_t sval64;
2404  if (!llvm::to_integer(value_str, sval64))
2406  "'%s' is not a valid signed integer string value", value_str);
2407  else if (!SIntValueIsValidForSize(sval64, byte_size))
2408  error.SetErrorStringWithFormat("value 0x%" PRIx64
2409  " is too large to fit in a %" PRIu64
2410  " byte signed integer value",
2411  sval64, (uint64_t)byte_size);
2412  else {
2414  switch (m_type) {
2415  case e_sint:
2416  m_integer = llvm::APInt(sizeof(sint_t) * 8, sval64, true);
2417  break;
2418  case e_slong:
2419  m_integer = llvm::APInt(sizeof(slong_t) * 8, sval64, true);
2420  break;
2421  case e_slonglong:
2422  m_integer = llvm::APInt(sizeof(slonglong_t) * 8, sval64, true);
2423  break;
2424  default:
2426  "unsupported signed integer byte size: %" PRIu64 "",
2427  (uint64_t)byte_size);
2428  break;
2429  }
2430  }
2431  } else {
2433  "unsupported signed integer byte size: %" PRIu64 "",
2434  (uint64_t)byte_size);
2435  return error;
2436  }
2437  break;
2438 
2439  case eEncodingIEEE754:
2440  static float f_val;
2441  static double d_val;
2442  static long double l_val;
2443  if (byte_size == sizeof(float)) {
2444  if (::sscanf(value_str, "%f", &f_val) == 1) {
2445  m_float = llvm::APFloat(f_val);
2446  m_type = e_float;
2447  } else
2448  error.SetErrorStringWithFormat("'%s' is not a valid float string value",
2449  value_str);
2450  } else if (byte_size == sizeof(double)) {
2451  if (::sscanf(value_str, "%lf", &d_val) == 1) {
2452  m_float = llvm::APFloat(d_val);
2453  m_type = e_double;
2454  } else
2455  error.SetErrorStringWithFormat("'%s' is not a valid float string value",
2456  value_str);
2457  } else if (byte_size == sizeof(long double)) {
2458  if (::sscanf(value_str, "%Lf", &l_val) == 1) {
2459  m_float =
2460  llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
2461  llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
2462  ((type128 *)&l_val)->x));
2464  } else
2465  error.SetErrorStringWithFormat("'%s' is not a valid float string value",
2466  value_str);
2467  } else {
2468  error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
2469  (uint64_t)byte_size);
2470  return error;
2471  }
2472  break;
2473 
2474  case eEncodingVector:
2475  error.SetErrorString("vector encoding unsupported.");
2476  break;
2477  }
2478  if (error.Fail())
2479  m_type = e_void;
2480 
2481  return error;
2482 }
2483 
2485  size_t byte_size) {
2486  Status error;
2487 
2488  type128 int128;
2489  type256 int256;
2490  switch (encoding) {
2492  error.SetErrorString("invalid encoding");
2493  break;
2494  case lldb::eEncodingVector:
2495  error.SetErrorString("vector encoding unsupported");
2496  break;
2497  case lldb::eEncodingUint: {
2498  lldb::offset_t offset = 0;
2499 
2500  switch (byte_size) {
2501  case 1:
2502  operator=((uint8_t)data.GetU8(&offset));
2503  break;
2504  case 2:
2505  operator=((uint16_t)data.GetU16(&offset));
2506  break;
2507  case 4:
2508  operator=((uint32_t)data.GetU32(&offset));
2509  break;
2510  case 8:
2511  operator=((uint64_t)data.GetU64(&offset));
2512  break;
2513  case 16:
2514  if (data.GetByteOrder() == eByteOrderBig) {
2515  int128.x[1] = (uint64_t)data.GetU64(&offset);
2516  int128.x[0] = (uint64_t)data.GetU64(&offset);
2517  } else {
2518  int128.x[0] = (uint64_t)data.GetU64(&offset);
2519  int128.x[1] = (uint64_t)data.GetU64(&offset);
2520  }
2521  operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
2522  break;
2523  case 32:
2524  if (data.GetByteOrder() == eByteOrderBig) {
2525  int256.x[3] = (uint64_t)data.GetU64(&offset);
2526  int256.x[2] = (uint64_t)data.GetU64(&offset);
2527  int256.x[1] = (uint64_t)data.GetU64(&offset);
2528  int256.x[0] = (uint64_t)data.GetU64(&offset);
2529  } else {
2530  int256.x[0] = (uint64_t)data.GetU64(&offset);
2531  int256.x[1] = (uint64_t)data.GetU64(&offset);
2532  int256.x[2] = (uint64_t)data.GetU64(&offset);
2533  int256.x[3] = (uint64_t)data.GetU64(&offset);
2534  }
2535  operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
2536  break;
2537  default:
2539  "unsupported unsigned integer byte size: %" PRIu64 "",
2540  (uint64_t)byte_size);
2541  break;
2542  }
2543  } break;
2544  case lldb::eEncodingSint: {
2545  lldb::offset_t offset = 0;
2546 
2547  switch (byte_size) {
2548  case 1:
2549  operator=((int8_t)data.GetU8(&offset));
2550  break;
2551  case 2:
2552  operator=((int16_t)data.GetU16(&offset));
2553  break;
2554  case 4:
2555  operator=((int32_t)data.GetU32(&offset));
2556  break;
2557  case 8:
2558  operator=((int64_t)data.GetU64(&offset));
2559  break;
2560  case 16:
2561  if (data.GetByteOrder() == eByteOrderBig) {
2562  int128.x[1] = (uint64_t)data.GetU64(&offset);
2563  int128.x[0] = (uint64_t)data.GetU64(&offset);
2564  } else {
2565  int128.x[0] = (uint64_t)data.GetU64(&offset);
2566  int128.x[1] = (uint64_t)data.GetU64(&offset);
2567  }
2568  operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
2569  break;
2570  case 32:
2571  if (data.GetByteOrder() == eByteOrderBig) {
2572  int256.x[3] = (uint64_t)data.GetU64(&offset);
2573  int256.x[2] = (uint64_t)data.GetU64(&offset);
2574  int256.x[1] = (uint64_t)data.GetU64(&offset);
2575  int256.x[0] = (uint64_t)data.GetU64(&offset);
2576  } else {
2577  int256.x[0] = (uint64_t)data.GetU64(&offset);
2578  int256.x[1] = (uint64_t)data.GetU64(&offset);
2579  int256.x[2] = (uint64_t)data.GetU64(&offset);
2580  int256.x[3] = (uint64_t)data.GetU64(&offset);
2581  }
2582  operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
2583  break;
2584  default:
2586  "unsupported signed integer byte size: %" PRIu64 "",
2587  (uint64_t)byte_size);
2588  break;
2589  }
2590  } break;
2591  case lldb::eEncodingIEEE754: {
2592  lldb::offset_t offset = 0;
2593 
2594  if (byte_size == sizeof(float))
2595  operator=((float)data.GetFloat(&offset));
2596  else if (byte_size == sizeof(double))
2597  operator=((double)data.GetDouble(&offset));
2598  else if (byte_size == sizeof(long double))
2599  operator=((long double)data.GetLongDouble(&offset));
2600  else
2601  error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
2602  (uint64_t)byte_size);
2603  } break;
2604  }
2605 
2606  return error;
2607 }
2608 
2609 bool Scalar::SignExtend(uint32_t sign_bit_pos) {
2610  const uint32_t max_bit_pos = GetByteSize() * 8;
2611 
2612  if (sign_bit_pos < max_bit_pos) {
2613  switch (m_type) {
2614  case Scalar::e_void:
2615  case Scalar::e_float:
2616  case Scalar::e_double:
2617  case Scalar::e_long_double:
2618  return false;
2619 
2620  case Scalar::e_sint:
2621  case Scalar::e_uint:
2622  case Scalar::e_slong:
2623  case Scalar::e_ulong:
2624  case Scalar::e_slonglong:
2625  case Scalar::e_ulonglong:
2626  case Scalar::e_sint128:
2627  case Scalar::e_uint128:
2628  case Scalar::e_sint256:
2629  case Scalar::e_uint256:
2630  case Scalar::e_sint512:
2631  case Scalar::e_uint512:
2632  if (max_bit_pos == sign_bit_pos)
2633  return true;
2634  else if (sign_bit_pos < (max_bit_pos - 1)) {
2635  llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
2636  llvm::APInt bitwize_and = m_integer & sign_bit;
2637  if (bitwize_and.getBoolValue()) {
2638  const llvm::APInt mask =
2639  ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
2640  m_integer |= mask;
2641  }
2642  return true;
2643  }
2644  break;
2645  }
2646  }
2647  return false;
2648 }
2649 
2650 size_t Scalar::GetAsMemoryData(void *dst, size_t dst_len,
2651  lldb::ByteOrder dst_byte_order,
2652  Status &error) const {
2653  // Get a data extractor that points to the native scalar data
2654  DataExtractor data;
2655  if (!GetData(data)) {
2656  error.SetErrorString("invalid scalar value");
2657  return 0;
2658  }
2659 
2660  const size_t src_len = data.GetByteSize();
2661 
2662  // Prepare a memory buffer that contains some or all of the register value
2663  const size_t bytes_copied =
2664  data.CopyByteOrderedData(0, // src offset
2665  src_len, // src length
2666  dst, // dst buffer
2667  dst_len, // dst length
2668  dst_byte_order); // dst byte order
2669  if (bytes_copied == 0)
2670  error.SetErrorString("failed to copy data");
2671 
2672  return bytes_copied;
2673 }
2674 
2675 bool Scalar::ExtractBitfield(uint32_t bit_size, uint32_t bit_offset) {
2676  if (bit_size == 0)
2677  return true;
2678 
2679  switch (m_type) {
2680  case Scalar::e_void:
2681  case Scalar::e_float:
2682  case Scalar::e_double:
2683  case Scalar::e_long_double:
2684  break;
2685 
2686  case Scalar::e_sint:
2687  case Scalar::e_slong:
2688  case Scalar::e_slonglong:
2689  case Scalar::e_sint128:
2690  case Scalar::e_sint256:
2691  case Scalar::e_sint512:
2692  m_integer = m_integer.ashr(bit_offset)
2693  .sextOrTrunc(bit_size)
2694  .sextOrSelf(8 * GetByteSize());
2695  return true;
2696 
2697  case Scalar::e_uint:
2698  case Scalar::e_ulong:
2699  case Scalar::e_ulonglong:
2700  case Scalar::e_uint128:
2701  case Scalar::e_uint256:
2702  case Scalar::e_uint512:
2703  m_integer = m_integer.lshr(bit_offset)
2704  .zextOrTrunc(bit_size)
2705  .zextOrSelf(8 * GetByteSize());
2706  return true;
2707  }
2708  return false;
2709 }
2710 
2711 bool lldb_private::operator==(const Scalar &lhs, const Scalar &rhs) {
2712  // If either entry is void then we can just compare the types
2713  if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
2714  return lhs.m_type == rhs.m_type;
2715 
2716  Scalar temp_value;
2717  const Scalar *a;
2718  const Scalar *b;
2719  llvm::APFloat::cmpResult result;
2720  switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
2721  case Scalar::e_void:
2722  break;
2723  case Scalar::e_sint:
2724  case Scalar::e_uint:
2725  case Scalar::e_slong:
2726  case Scalar::e_ulong:
2727  case Scalar::e_slonglong:
2728  case Scalar::e_ulonglong:
2729  case Scalar::e_sint128:
2730  case Scalar::e_uint128:
2731  case Scalar::e_sint256:
2732  case Scalar::e_uint256:
2733  case Scalar::e_sint512:
2734  case Scalar::e_uint512:
2735  return a->m_integer == b->m_integer;
2736  case Scalar::e_float:
2737  case Scalar::e_double:
2738  case Scalar::e_long_double:
2739  result = a->m_float.compare(b->m_float);
2740  if (result == llvm::APFloat::cmpEqual)
2741  return true;
2742  }
2743  return false;
2744 }
2745 
2746 bool lldb_private::operator!=(const Scalar &lhs, const Scalar &rhs) {
2747  return !(lhs == rhs);
2748 }
2749 
2750 bool lldb_private::operator<(const Scalar &lhs, const Scalar &rhs) {
2751  if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
2752  return false;
2753 
2754  Scalar temp_value;
2755  const Scalar *a;
2756  const Scalar *b;
2757  llvm::APFloat::cmpResult result;
2758  switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
2759  case Scalar::e_void:
2760  break;
2761  case Scalar::e_sint:
2762  case Scalar::e_slong:
2763  case Scalar::e_slonglong:
2764  case Scalar::e_sint128:
2765  case Scalar::e_sint256:
2766  case Scalar::e_sint512:
2767  case Scalar::e_uint512:
2768  return a->m_integer.slt(b->m_integer);
2769  case Scalar::e_uint:
2770  case Scalar::e_ulong:
2771  case Scalar::e_ulonglong:
2772  case Scalar::e_uint128:
2773  case Scalar::e_uint256:
2774  return a->m_integer.ult(b->m_integer);
2775  case Scalar::e_float:
2776  case Scalar::e_double:
2777  case Scalar::e_long_double:
2778  result = a->m_float.compare(b->m_float);
2779  if (result == llvm::APFloat::cmpLessThan)
2780  return true;
2781  }
2782  return false;
2783 }
2784 
2785 bool lldb_private::operator<=(const Scalar &lhs, const Scalar &rhs) {
2786  return !(rhs < lhs);
2787 }
2788 
2789 bool lldb_private::operator>(const Scalar &lhs, const Scalar &rhs) {
2790  return rhs < lhs;
2791 }
2792 
2793 bool lldb_private::operator>=(const Scalar &lhs, const Scalar &rhs) {
2794  return !(lhs < rhs);
2795 }
2796 
2798  switch (m_type) {
2799  case e_void:
2800  break;
2801  case e_sint:
2802  case e_uint:
2803  case e_slong:
2804  case e_ulong:
2805  case e_slonglong:
2806  case e_ulonglong:
2807  case e_sint128:
2808  case e_uint128:
2809  case e_sint256:
2810  case e_uint256:
2811  case e_sint512:
2812  case e_uint512:
2813  m_integer.clearBit(bit);
2814  return true;
2815  case e_float:
2816  case e_double:
2817  case e_long_double:
2818  break;
2819  }
2820  return false;
2821 }
2822 
2824  switch (m_type) {
2825  case e_void:
2826  break;
2827  case e_sint:
2828  case e_uint:
2829  case e_slong:
2830  case e_ulong:
2831  case e_slonglong:
2832  case e_ulonglong:
2833  case e_sint128:
2834  case e_uint128:
2835  case e_sint256:
2836  case e_uint256:
2837  case e_sint512:
2838  case e_uint512:
2839  m_integer.setBit(bit);
2840  return true;
2841  case e_float:
2842  case e_double:
2843  case e_long_double:
2844  break;
2845  }
2846  return false;
2847 }
2848 
2849 llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &os, const Scalar &scalar) {
2850  StreamString s;
2851  scalar.GetValue(&s, /*show_type*/ true);
2852  return os << s.GetString();
2853 }
#define BITWIDTH_INT512
Definition: Scalar.h:31
static Scalar::Type GetValueTypeForFloatWithByteSize(size_t byte_size)
Definition: Scalar.cpp:1174
Scalar & operator &=(const Scalar &rhs)
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
bool operator<=(const Scalar &lhs, const Scalar &rhs)
Definition: Scalar.cpp:2785
An data extractor class.
Definition: DataExtractor.h:47
const char * GetTypeAsCString() const
Definition: Scalar.cpp:273
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
size_t GetByteSize() const
Definition: Scalar.cpp:187
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
const void * GetBytes() const
Definition: Scalar.cpp:103
#define lldbassert(x)
Definition: LLDBAssert.h:15
unsigned long ulong_t
Definition: Scalar.h:280
const Scalar operator%(const Scalar &lhs, const Scalar &rhs)
Definition: Scalar.cpp:2264
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
static Scalar::Type GetValueTypeForSignedIntegerWithByteSize(size_t byte_size)
Definition: Scalar.cpp:1153
bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset)
Definition: Scalar.cpp:2675
size_t GetAsMemoryData(void *dst, size_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
Definition: Scalar.cpp:2650
unsigned short UShort(unsigned short fail_value=0) const
Definition: Scalar.cpp:1394
bool IsZero() const
Definition: Scalar.cpp:214
#define NUM_OF_WORDS_INT128
Definition: Scalar.h:26
unsigned long long ulonglong_t
Definition: Scalar.h:282
bool GetData(DataExtractor &data, size_t limit_byte_size=UINT32_MAX) const
Definition: Scalar.cpp:77
Status SetValueFromData(DataExtractor &data, lldb::Encoding encoding, size_t byte_size)
Definition: Scalar.cpp:2484
Scalar & operator=(const int i)
Definition: Scalar.cpp:320
static bool UIntValueIsValidForSize(uint64_t uval64, size_t total_byte_size)
Definition: Scalar.h:249
float GetFloat(lldb::offset_t *offset_ptr) const
Extract a float from *offset_ptr.
const Scalar operator*(const Scalar &lhs, const Scalar &rhs)
Definition: Scalar.cpp:2161
void GetValue(Stream *s, bool show_type) const
Definition: Scalar.cpp:240
signed char SChar(char fail_value=0) const
Definition: Scalar.cpp:1308
static Scalar::Type GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size)
Definition: Scalar.cpp:1164
long double GetLongDouble(lldb::offset_t *offset_ptr) const
llvm::APFloat m_float
Definition: Scalar.h:290
bool SignExtend(uint32_t bit_pos)
Definition: Scalar.cpp:2609
AdaptedConstIterator< C, E, A >::BackingIterator::difference_type operator-(AdaptedConstIterator< C, E, A > &lhs, AdaptedConstIterator< C, E, A > &rhs)
Definition: Iterable.h:141
bool operator==(const Address &lhs, const Address &rhs)
Definition: Address.cpp:973
#define BITWIDTH_INT256
Definition: Scalar.h:29
static uint32_t bit(const uint32_t val, const uint32_t msbit)
Definition: ARMUtils.h:269
const Scalar operator &(const Scalar &lhs, const Scalar &rhs)
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
Scalar & operator+=(const Scalar &rhs)
Definition: Scalar.cpp:1735
uint64_t offset_t
Definition: lldb-types.h:87
void Clear()
Clears the object state.
const Scalar operator>>(const Scalar &lhs, const Scalar &rhs)
Definition: Scalar.cpp:2345
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
Scalar & operator>>=(const Scalar &rhs)
Definition: Scalar.cpp:1865
long long slonglong_t
Definition: Scalar.h:281
double Double(double fail_value=0.0) const
Definition: Scalar.cpp:1679
unsigned char uchar_t
Definition: Scalar.h:274
AdaptedConstIterator< C, E, A > operator+(typename AdaptedConstIterator< C, E, A >::BackingIterator::difference_type offset, AdaptedConstIterator< C, E, A > &rhs)
Definition: Iterable.h:132
int SInt(int fail_value=0) const
Definition: Scalar.cpp:1424
bool SetBit(uint32_t bit)
Definition: Scalar.cpp:2823
llvm::StringRef GetString() const
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition: Scalar.cpp:1566
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
static Scalar::Type PromoteToMaxType(const Scalar &lhs, const Scalar &rhs, Scalar &temp_value, const Scalar *&promoted_lhs_ptr, const Scalar *&promoted_rhs_ptr)
Definition: Scalar.cpp:28
double GetDouble(lldb::offset_t *offset_ptr) const
bool Promote(Scalar::Type type)
Definition: Scalar.cpp:422
float Float(float fail_value=0.0f) const
Definition: Scalar.cpp:1651
static const char * GetValueTypeAsCString(Scalar::Type value_type)
Definition: Scalar.cpp:1114
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
bool ShiftRightLogical(const Scalar &rhs)
Definition: Scalar.cpp:1817
llvm::APInt SInt128(llvm::APInt &fail_value) const
Definition: Scalar.cpp:1601
long double LongDouble(long double fail_value=0.0) const
Definition: Scalar.cpp:1707
const Scalar operator/(const Scalar &lhs, const Scalar &rhs)
Definition: Scalar.cpp:2113
unsigned int UInt(unsigned int fail_value=0) const
Definition: Scalar.cpp:1452
bool operator!=(const Address &lhs, const Address &rhs)
Definition: Address.cpp:979
const Scalar operator^(const Scalar &lhs, const Scalar &rhs)
Definition: Scalar.cpp:2304
bool ClearBit(uint32_t bit)
Definition: Scalar.cpp:2797
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
long SLong(long fail_value=0) const
Definition: Scalar.cpp:1480
unsigned int uint_t
Definition: Scalar.h:278
llvm::APInt m_integer
Definition: Scalar.h:289
short SShort(short fail_value=0) const
Definition: Scalar.cpp:1364
lldb::offset_t CopyByteOrderedData(lldb::offset_t src_offset, lldb::offset_t src_len, void *dst, lldb::offset_t dst_len, lldb::ByteOrder dst_byte_order) const
Copy dst_len bytes from *offset_ptr and ensure the copied data is treated as a value that can be swap...
unsigned char UChar(unsigned char fail_value=0) const
Definition: Scalar.cpp:1336
Scalar::Type m_type
Definition: Scalar.h:288
bool operator>(const Address &lhs, const Address &rhs)
Definition: Address.cpp:957
Scalar::Type GetType() const
Definition: Scalar.h:208
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
Definition: SBAddress.h:15
Scalar & operator<<=(const Scalar &rhs)
Definition: Scalar.cpp:1769
bool operator>=(const Scalar &lhs, const Scalar &rhs)
Definition: Scalar.cpp:2793
long long SLongLong(long long fail_value=0) const
Definition: Scalar.cpp:1536
static bool SIntValueIsValidForSize(int64_t sval64, size_t total_byte_size)
Definition: Scalar.h:260
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
Stream & operator<<(Stream &s, ConstString str)
Stream the string value str to the stream s.
unsigned short ushort_t
Definition: Scalar.h:276
long double long_double_t
Definition: Scalar.h:285
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
#define BITWIDTH_INT128
Definition: Scalar.h:27
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
llvm::APInt UInt128(const llvm::APInt &fail_value) const
Definition: Scalar.cpp:1626
bool operator<(const Address &lhs, const Address &rhs)
Definition: Address.cpp:942
#define NUM_OF_WORDS_INT256
Definition: Scalar.h:28
unsigned long ULong(unsigned long fail_value=0) const
Definition: Scalar.cpp:1508
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
const Scalar operator|(const Scalar &lhs, const Scalar &rhs)
Definition: Scalar.cpp:2229
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
An error handling class.
Definition: Status.h:44
Status SetValueFromCString(const char *s, lldb::Encoding encoding, size_t byte_size)
Definition: Scalar.cpp:2351