summaryrefslogtreecommitdiffstats
path: root/lldb/source/Core/RegisterValue.cpp
blob: 5f2e2e599ab3f066f82b822e696c1d4d5dc27a84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
//===-- RegisterValue.cpp ----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Core/RegisterValue.h"

// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/Error.h"
#include "lldb/Core/Scalar.h"
#include "lldb/Core/Stream.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Interpreter/Args.h"

using namespace lldb;
using namespace lldb_private;


bool
RegisterValue::Dump (Stream *s, 
                     const RegisterInfo *reg_info, 
                     bool prefix_with_name, 
                     bool prefix_with_alt_name, 
                     Format format,
                     uint32_t reg_name_right_align_at) const
{
    DataExtractor data;
    if (GetData (data))
    {
        bool name_printed = false;
        // For simplicity, alignment of the register name printing applies only
        // in the most common case where:
        // 
        //     prefix_with_name^prefix_with_alt_name is true
        //
        StreamString format_string;
        if (reg_name_right_align_at && (prefix_with_name^prefix_with_alt_name))
            format_string.Printf("%%%us", reg_name_right_align_at);
        else
            format_string.Printf("%%s");
        const char *fmt = format_string.GetData();
        if (prefix_with_name)
        {
            if (reg_info->name)
            {
                s->Printf (fmt, reg_info->name);
                name_printed = true;
            }
            else if (reg_info->alt_name)
            {
                s->Printf (fmt, reg_info->alt_name);
                prefix_with_alt_name = false;
                name_printed = true;
            }
        }
        if (prefix_with_alt_name)
        {
            if (name_printed)
                s->PutChar ('/');
            if (reg_info->alt_name)
            {
                s->Printf (fmt, reg_info->alt_name);
                name_printed = true;
            }
            else if (!name_printed)
            {
                // No alternate name but we were asked to display a name, so show the main name
                s->Printf (fmt, reg_info->name);
                name_printed = true;
            }
        }
        if (name_printed)
            s->PutCString (" = ");

        if (format == eFormatDefault)
            format = reg_info->format;

        data.Dump (s, 
                   0,                       // Offset in "data"
                   format,                  // Format to use when dumping
                   reg_info->byte_size,     // item_byte_size
                   1,                       // item_count
                   UINT32_MAX,              // num_per_line
                   LLDB_INVALID_ADDRESS,    // base_addr
                   0,                       // item_bit_size
                   0);                      // item_bit_offset
        return true;
    }
    return false;
}


bool
RegisterValue::GetData (DataExtractor &data) const
{
    return data.SetData(GetBytes(), GetByteSize(), GetByteOrder()) > 0;
}


uint32_t
RegisterValue::GetAsMemoryData (const RegisterInfo *reg_info,
                                void *dst,
                                uint32_t dst_len, 
                                lldb::ByteOrder dst_byte_order,
                                Error &error) const
{    
    if (reg_info == NULL)
    {
        error.SetErrorString ("invalid register info argument.");
        return 0;
    }
    
    // ReadRegister should have already been called on tgus object prior to 
    // calling this.
    if (GetType() == eTypeInvalid)
    {
        // No value has been read into this object...
        error.SetErrorStringWithFormat("invalid register value type for register %s", reg_info->name);
        return 0;
    }
    
    if (dst_len > kMaxRegisterByteSize)
    {
        error.SetErrorString ("destination is too big");
        return 0;
    }
    
    const uint32_t src_len = reg_info->byte_size;
    
    // Extract the register data into a data extractor
    DataExtractor reg_data;
    if (!GetData(reg_data))
    {
        error.SetErrorString ("invalid register value to copy into");
        return 0;
    }
    
    // Prepare a memory buffer that contains some or all of the register value
    const uint32_t bytes_copied = reg_data.CopyByteOrderedData (0,                  // src offset
                                                                src_len,            // src length
                                                                dst,                // dst buffer
                                                                dst_len,            // dst length
                                                                dst_byte_order);    // dst byte order
    if (bytes_copied == 0) 
        error.SetErrorStringWithFormat("failed to copy data for register write of %s", reg_info->name);
    
    return bytes_copied;
}

uint32_t
RegisterValue::SetFromMemoryData (const RegisterInfo *reg_info,
                                  const void *src,
                                  uint32_t src_len,
                                  lldb::ByteOrder src_byte_order,
                                  Error &error)
{
    if (reg_info == NULL)
    {
        error.SetErrorString ("invalid register info argument.");
        return 0;
    }
    
    // Moving from addr into a register
    //
    // Case 1: src_len == dst_len
    //
    //   |AABBCCDD| Address contents
    //   |AABBCCDD| Register contents
    //
    // Case 2: src_len > dst_len
    //
    //   Error!  (The register should always be big enough to hold the data)
    //
    // Case 3: src_len < dst_len
    //
    //   |AABB| Address contents
    //   |AABB0000| Register contents [on little-endian hardware]
    //   |0000AABB| Register contents [on big-endian hardware]
    if (src_len > kMaxRegisterByteSize)
    {
        error.SetErrorStringWithFormat ("register buffer is too small to receive %u bytes of data.", src_len);
        return 0;
    }
    
    const uint32_t dst_len = reg_info->byte_size;
    
    if (src_len > dst_len)
    {
        error.SetErrorStringWithFormat("%u bytes is too big to store in register %s (%u bytes)", src_len, reg_info->name, dst_len);
        return 0;
    }

    // Use a data extractor to correctly copy and pad the bytes read into the
    // register value
    DataExtractor src_data (src, src_len, src_byte_order, 4);
    
    // Given the register info, set the value type of this RegisterValue object
    SetType (reg_info);
    // And make sure we were able to figure out what that register value was
    RegisterValue::Type value_type = GetType();
    if (value_type == eTypeInvalid)        
    {
        // No value has been read into this object...
        error.SetErrorStringWithFormat("invalid register value type for register %s", reg_info->name);
        return 0;
    }
    else if (value_type == eTypeBytes)
    {
        m_data.buffer.byte_order = src_byte_order;
        // Make sure to set the buffer length of the destination buffer to avoid
        // problems due to uninitalized variables.
        m_data.buffer.length = src_len;
    }

    const uint32_t bytes_copied = src_data.CopyByteOrderedData (0,               // src offset
                                                                src_len,         // src length
                                                                GetBytes(),      // dst buffer
                                                                GetByteSize(),   // dst length
                                                                GetByteOrder()); // dst byte order
    if (bytes_copied == 0)
        error.SetErrorStringWithFormat("failed to copy data for register write of %s", reg_info->name);

    return bytes_copied;
}

bool
RegisterValue::GetScalarValue (Scalar &scalar) const
{
    switch (m_type)
    {
        case eTypeInvalid:      break;
        case eTypeBytes:        break;
        case eTypeUInt8:        scalar = m_data.uint8; return true;
        case eTypeUInt16:       scalar = m_data.uint16; return true;
        case eTypeUInt32:       scalar = m_data.uint32; return true;
        case eTypeUInt64:       scalar = m_data.uint64; return true;
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:      break;
#endif
        case eTypeFloat:        scalar = m_data.ieee_float; return true;
        case eTypeDouble:       scalar = m_data.ieee_double; return true;
        case eTypeLongDouble:   scalar = m_data.ieee_long_double; return true;
    }
    return false;
}

void
RegisterValue::Clear()
{
    m_type = eTypeInvalid;
}

RegisterValue::Type
RegisterValue::SetType (const RegisterInfo *reg_info)
{
    m_type = eTypeInvalid;
    const uint32_t byte_size = reg_info->byte_size;
    switch (reg_info->encoding)
    {
        case eEncodingInvalid:
            break;
            
        case eEncodingUint:
        case eEncodingSint:
            if (byte_size == 1)
                m_type = eTypeUInt8;
            else if (byte_size <= 2)
                m_type = eTypeUInt16;
            else if (byte_size <= 4)
                m_type = eTypeUInt32;
            else if (byte_size <= 8)
                m_type = eTypeUInt64;
#if defined (ENABLE_128_BIT_SUPPORT)
            else if (byte_size <= 16)
                m_type = eTypeUInt128;
#endif
            break;

        case eEncodingIEEE754:
            if (byte_size == sizeof(float))
                m_type = eTypeFloat;
            else if (byte_size == sizeof(double))
                m_type = eTypeDouble;
            else if (byte_size == sizeof(long double))
                m_type = eTypeLongDouble;
            break;

        case eEncodingVector:
            m_type = eTypeBytes;
            break;
    }
    return m_type;
}

Error
RegisterValue::SetValueFromData (const RegisterInfo *reg_info, DataExtractor &src, uint32_t src_offset, bool partial_data_ok)
{
    Error error;
    
    if (src.GetByteSize() == 0)
    {
        error.SetErrorString ("empty data.");
        return error;
    }

    if (reg_info->byte_size == 0)
    {
        error.SetErrorString ("invalid register info.");
        return error;
    }

    uint32_t src_len = src.GetByteSize() - src_offset;
    
    if (!partial_data_ok && (src_len < reg_info->byte_size))
    {
        error.SetErrorString ("not enough data.");
        return error;
    }
        
    // Cap the data length if there is more than enough bytes for this register
    // value
    if (src_len > reg_info->byte_size)
        src_len = reg_info->byte_size;

    // Zero out the value in case we get partial data...
    memset (m_data.buffer.bytes, 0, sizeof (m_data.buffer.bytes));
    
    switch (SetType (reg_info))
    {
        case eTypeInvalid:
            error.SetErrorString("");
            break;
        case eTypeUInt8:    SetUInt8  (src.GetMaxU32 (&src_offset, src_len)); break;
        case eTypeUInt16:   SetUInt16 (src.GetMaxU32 (&src_offset, src_len)); break;
        case eTypeUInt32:   SetUInt32 (src.GetMaxU32 (&src_offset, src_len)); break;
        case eTypeUInt64:   SetUInt64 (src.GetMaxU64 (&src_offset, src_len)); break;
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:
            {
                __uint128_t data1 = src.GetU64 (&src_offset);
                __uint128_t data2 = src.GetU64 (&src_offset);
                if (src.GetByteSize() == eByteOrderBig)
                    SetUInt128 (data1 << 64 + data2);
                else
                    SetUInt128 (data2 << 64 + data1);
            }
            break;
#endif
        case eTypeFloat:        SetFloat (src.GetFloat (&src_offset));      break;
        case eTypeDouble:       SetDouble(src.GetDouble (&src_offset));     break;
        case eTypeLongDouble:   SetFloat (src.GetLongDouble (&src_offset)); break;
        case eTypeBytes:
        {
            m_data.buffer.length = reg_info->byte_size;
            m_data.buffer.byte_order = src.GetByteOrder();
            assert (m_data.buffer.length <= kMaxRegisterByteSize);
            if (m_data.buffer.length > kMaxRegisterByteSize)
                m_data.buffer.length = kMaxRegisterByteSize;
            if (src.CopyByteOrderedData (src_offset,                    // offset within "src" to start extracting data
                                         src_len,                       // src length
                                         m_data.buffer.bytes,           // dst buffer
                                         m_data.buffer.length,          // dst length
                                         m_data.buffer.byte_order) == 0)// dst byte order
            {
                error.SetErrorString ("data copy failed data.");
                return error;
            }
        }
    }
    
    return error;
}

#include "llvm/ADT/StringRef.h"
#include <vector>
static inline void StripSpaces(llvm::StringRef &Str)
{
    while (!Str.empty() && isspace(Str[0]))
        Str = Str.substr(1);
    while (!Str.empty() && isspace(Str.back()))
        Str = Str.substr(0, Str.size()-1);
}
static inline void LStrip(llvm::StringRef &Str, char c)
{
    if (!Str.empty() && Str.front() == c)
        Str = Str.substr(1);
}
static inline void RStrip(llvm::StringRef &Str, char c)
{
    if (!Str.empty() && Str.back() == c)
        Str = Str.substr(0, Str.size()-1);
}
// Helper function for RegisterValue::SetValueFromCString()
static bool
ParseVectorEncoding(const RegisterInfo *reg_info, const char *vector_str, const uint32_t byte_size, RegisterValue *reg_value)
{
    // Example: vector_str = "{0x2c 0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a 0x2a 0x3e 0x84 0x4f 0x2a 0x3e}".
    llvm::StringRef Str(vector_str);
    StripSpaces(Str);
    LStrip(Str, '{');
    RStrip(Str, '}');
    StripSpaces(Str);

    char Sep = ' ';

    // The first split should give us:
    // ('0x2c', '0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a 0x2a 0x3e 0x84 0x4f 0x2a 0x3e').
    std::pair<llvm::StringRef, llvm::StringRef> Pair = Str.split(Sep);
    std::vector<uint8_t> bytes;
    unsigned byte = 0;

    // Using radix auto-sensing by passing 0 as the radix.
    // Keep on processing the vector elements as long as the parsing succeeds and the vector size is < byte_size.
    while (!Pair.first.getAsInteger(0, byte) && bytes.size() < byte_size) {
        bytes.push_back(byte);
        Pair = Pair.second.split(Sep);
    }

    // Check for vector of exact byte_size elements.
    if (bytes.size() != byte_size)
        return false;

    reg_value->SetBytes(&(bytes.front()), byte_size, eByteOrderLittle);
    return true;
}
Error
RegisterValue::SetValueFromCString (const RegisterInfo *reg_info, const char *value_str)
{
    Error error;
    if (reg_info == NULL)
    {
        error.SetErrorString ("Invalid register info argument.");
        return error;
    }

    if (value_str == NULL || value_str[0] == '\0')
    {
        error.SetErrorString ("Invalid c-string value string.");
        return error;
    }
    bool success = false;
    const uint32_t byte_size = reg_info->byte_size;
    switch (reg_info->encoding)
    {
        default:
        case eEncodingInvalid:
            error.SetErrorString ("Invalid encoding.");
            break;
            
        case eEncodingUint:
            if (byte_size <= sizeof (uint64_t))
            {
                uint64_t uval64 = Args::StringToUInt64(value_str, UINT64_MAX, 0, &success);
                if (!success)
                    error.SetErrorStringWithFormat ("'%s' is not a valid unsigned integer string value", value_str);
                else if (!Args::UInt64ValueIsValidForByteSize (uval64, byte_size))
                    error.SetErrorStringWithFormat ("value 0x%llx is too large to fit in a %u byte unsigned integer value", uval64, byte_size);
                else
                {
                    if (!SetUInt (uval64, reg_info->byte_size))
                        error.SetErrorStringWithFormat ("unsupported unsigned integer byte size: %u", byte_size);
                }
            }
            else
            {
                error.SetErrorStringWithFormat ("unsupported unsigned integer byte size: %u", byte_size);
                return error;
            }
            break;
            
        case eEncodingSint:
            if (byte_size <= sizeof (long long))
            {
                uint64_t sval64 = Args::StringToSInt64(value_str, INT64_MAX, 0, &success);
                if (!success)
                    error.SetErrorStringWithFormat ("'%s' is not a valid signed integer string value", value_str);
                else if (!Args::SInt64ValueIsValidForByteSize (sval64, byte_size))
                    error.SetErrorStringWithFormat ("value 0x%llx is too large to fit in a %u byte signed integer value", sval64, byte_size);
                else
                {
                    if (!SetUInt (sval64, reg_info->byte_size))
                        error.SetErrorStringWithFormat ("unsupported signed integer byte size: %u", byte_size);
                }
            }
            else
            {
                error.SetErrorStringWithFormat ("unsupported signed integer byte size: %u", byte_size);
                return error;
            }
            break;
            
        case eEncodingIEEE754:
            if (byte_size == sizeof (float))
            {
                if (::sscanf (value_str, "%f", &m_data.ieee_float) == 1)
                    m_type = eTypeFloat;
                else
                    error.SetErrorStringWithFormat ("'%s' is not a valid float string value", value_str);
            }
            else if (byte_size == sizeof (double))
            {
                if (::sscanf (value_str, "%lf", &m_data.ieee_double) == 1)
                    m_type = eTypeDouble;
                else
                    error.SetErrorStringWithFormat ("'%s' is not a valid float string value", value_str);
            }
            else if (byte_size == sizeof (long double))
            {
                if (::sscanf (value_str, "%Lf", &m_data.ieee_long_double) == 1)
                    m_type = eTypeLongDouble;
                else
                    error.SetErrorStringWithFormat ("'%s' is not a valid float string value", value_str);
            }
            else
            {
                error.SetErrorStringWithFormat ("unsupported float byte size: %u", byte_size);
                return error;
            }
            break;
            
        case eEncodingVector:
            if (!ParseVectorEncoding(reg_info, value_str, byte_size, this))
                error.SetErrorString ("unrecognized vector encoding string value.");
            break;
    }
    if (error.Fail())
        m_type = eTypeInvalid;
    
    return error;
}


bool
RegisterValue::SignExtend (uint32_t sign_bitpos)
{
    switch (m_type)
    {
        case eTypeInvalid:
            break;

        case eTypeUInt8:        
            if (sign_bitpos == (8-1))
                return true;
            else if (sign_bitpos < (8-1))
            {
                uint8_t sign_bit = 1u << sign_bitpos;
                if (m_data.uint8 & sign_bit)
                {
                    const uint8_t mask = ~(sign_bit) + 1u;
                    m_data.uint8 |= mask;
                }
                return true;
            }
            break;

        case eTypeUInt16:
            if (sign_bitpos == (16-1))
                return true;
            else if (sign_bitpos < (16-1))
            {
                uint16_t sign_bit = 1u << sign_bitpos;
                if (m_data.uint16 & sign_bit)
                {
                    const uint16_t mask = ~(sign_bit) + 1u;
                    m_data.uint16 |= mask;
                }
                return true;
            }
            break;
        
        case eTypeUInt32:
            if (sign_bitpos == (32-1))
                return true;
            else if (sign_bitpos < (32-1))
            {
                uint32_t sign_bit = 1u << sign_bitpos;
                if (m_data.uint32 & sign_bit)
                {
                    const uint32_t mask = ~(sign_bit) + 1u;
                    m_data.uint32 |= mask;
                }
                return true;
            }
            break;

        case eTypeUInt64:
            if (sign_bitpos == (64-1))
                return true;
            else if (sign_bitpos < (64-1))
            {
                uint64_t sign_bit = 1ull << sign_bitpos;
                if (m_data.uint64 & sign_bit)
                {
                    const uint64_t mask = ~(sign_bit) + 1ull;
                    m_data.uint64 |= mask;
                }
                return true;
            }
            break;

#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:
            if (sign_bitpos == (128-1))
                return true;
            else if (sign_bitpos < (128-1))
            {
                __uint128_t sign_bit = (__uint128_t)1u << sign_bitpos;
                if (m_data.uint128 & sign_bit)
                {
                    const uint128_t mask = ~(sign_bit) + 1u;
                    m_data.uint128 |= mask;
                }
                return true;
            }
            break;
#endif
        case eTypeFloat:
        case eTypeDouble:
        case eTypeLongDouble:
        case eTypeBytes:
            break;
    }
    return false;
}

bool
RegisterValue::CopyValue (const RegisterValue &rhs)
{
    m_type = rhs.m_type;
    switch (m_type)
    {
        default:
        case eTypeInvalid: 
            return false;
        case eTypeUInt8:        m_data.uint8 = rhs.m_data.uint8; break;
        case eTypeUInt16:       m_data.uint16 = rhs.m_data.uint16; break;
        case eTypeUInt32:       m_data.uint32 = rhs.m_data.uint32; break;
        case eTypeUInt64:       m_data.uint64 = rhs.m_data.uint64; break;
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:      m_data.uint128 = rhs.m_data.uint128; break;
#endif
        case eTypeFloat:        m_data.ieee_float = rhs.m_data.ieee_float; break;
        case eTypeDouble:       m_data.ieee_double = rhs.m_data.ieee_double; break;
        case eTypeLongDouble:   m_data.ieee_long_double = rhs.m_data.ieee_long_double; break;
        case eTypeBytes:        
            assert (rhs.m_data.buffer.length <= kMaxRegisterByteSize);
            ::memcpy (m_data.buffer.bytes, rhs.m_data.buffer.bytes, kMaxRegisterByteSize);
            m_data.buffer.length = rhs.m_data.buffer.length;
            m_data.buffer.byte_order = rhs.m_data.buffer.byte_order;
            break;
    }
    return true;
}

uint16_t
RegisterValue::GetAsUInt16 (uint16_t fail_value, bool *success_ptr) const
{
    if (success_ptr)
        *success_ptr = true;
    
    switch (m_type)
    {
        default:            break;
        case eTypeUInt8:    return m_data.uint8;
        case eTypeUInt16:   return m_data.uint16;
    }
    if (success_ptr)
        *success_ptr = false;
    return fail_value;
}

uint32_t
RegisterValue::GetAsUInt32 (uint32_t fail_value, bool *success_ptr) const
{
    if (success_ptr)
        *success_ptr = true;
    switch (m_type)
    {
        default:            break;
        case eTypeUInt8:    return m_data.uint8;
        case eTypeUInt16:   return m_data.uint16;
        case eTypeUInt32:   return m_data.uint32;
        case eTypeFloat:
            if (sizeof(float) == sizeof(uint32_t))
                return m_data.uint32;
            break;
        case eTypeDouble:
            if (sizeof(double) == sizeof(uint32_t))
                return m_data.uint32;
            break;
        case eTypeLongDouble:
            if (sizeof(long double) == sizeof(uint32_t))
                return m_data.uint32;
            break;
    }
    if (success_ptr)
        *success_ptr = false;
    return fail_value;
}

uint64_t
RegisterValue::GetAsUInt64 (uint64_t fail_value, bool *success_ptr) const
{
    if (success_ptr)
        *success_ptr = true;
    switch (m_type)
    {
        default:            break;
        case eTypeUInt8:    return m_data.uint8;
        case eTypeUInt16:   return m_data.uint16;
        case eTypeUInt32:   return m_data.uint32;
        case eTypeUInt64:   return m_data.uint64;
        case eTypeFloat:
            if (sizeof(float) == sizeof(uint64_t))
                return m_data.uint64;
            break;
        case eTypeDouble:
            if (sizeof(double) == sizeof(uint64_t))
                return m_data.uint64;
            break;
        case eTypeLongDouble:
            if (sizeof(long double) == sizeof(uint64_t))
                return m_data.uint64;
            break;
    }
    if (success_ptr)
        *success_ptr = false;
    return fail_value;
}

#if defined (ENABLE_128_BIT_SUPPORT)
__uint128_t
RegisterValue::GetAsUInt128 (__uint128_t fail_value, bool *success_ptr) const
{
    if (success_ptr)
        *success_ptr = true;
    switch (m_type)
    {
        default:            break;
        case eTypeUInt8:    return m_data.uint8;
        case eTypeUInt16:   return m_data.uint16;
        case eTypeUInt32:   return m_data.uint32;
        case eTypeUInt64:   return m_data.uint64;
        case eTypeUInt128:  return m_data.uint128;
        case eTypeFloat:
            if (sizeof(float) == sizeof(__uint128_t))
                return m_data.uint128;
            break;
        case eTypeDouble:
            if (sizeof(double) == sizeof(__uint128_t))
                return m_data.uint128;
            break;
        case eTypeLongDouble:
            if (sizeof(long double) == sizeof(__uint128_t))
                return m_data.uint128;
            break;
    }
    if (success_ptr)
        *success_ptr = false;
    return fail_value;
}
#endif
float
RegisterValue::GetAsFloat (float fail_value, bool *success_ptr) const
{
    if (success_ptr)
        *success_ptr = true;
    switch (m_type)
    {
        default:            break;
        case eTypeUInt32:
            if (sizeof(float) == sizeof(m_data.uint32))
                return m_data.ieee_float;
            break;
        case eTypeUInt64:
            if (sizeof(float) == sizeof(m_data.uint64))
                return m_data.ieee_float;
            break;
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:
            if (sizeof(float) == sizeof(m_data.uint128))
                return m_data.ieee_float;
            break;
#endif
        case eTypeFloat:    return m_data.ieee_float;
        case eTypeDouble:
            if (sizeof(float) == sizeof(double))
                return m_data.ieee_float;
            break;
        case eTypeLongDouble:
            if (sizeof(float) == sizeof(long double))
                return m_data.ieee_float;
            break;
    }
    if (success_ptr)
        *success_ptr = false;
    return fail_value;
}

double
RegisterValue::GetAsDouble (double fail_value, bool *success_ptr) const
{
    if (success_ptr)
        *success_ptr = true;
    switch (m_type)
    {
        default:            
            break;
            
        case eTypeUInt32:
            if (sizeof(double) == sizeof(m_data.uint32))
                return m_data.ieee_double;
            break;
            
        case eTypeUInt64:
            if (sizeof(double) == sizeof(m_data.uint64))
                return m_data.ieee_double;
            break;
            
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:
            if (sizeof(double) == sizeof(m_data.uint128))
                return m_data.ieee_double;
#endif
        case eTypeFloat:    return m_data.ieee_float;
        case eTypeDouble:   return m_data.ieee_double;
            
        case eTypeLongDouble:
            if (sizeof(double) == sizeof(long double))
                return m_data.ieee_double;
            break;
    }
    if (success_ptr)
        *success_ptr = false;
    return fail_value;
}

long double
RegisterValue::GetAsLongDouble (long double fail_value, bool *success_ptr) const
{
    if (success_ptr)
        *success_ptr = true;
    switch (m_type)
    {
        default:
            break;
            
        case eTypeUInt32:
            if (sizeof(long double) == sizeof(m_data.uint32))
                return m_data.ieee_long_double;
            break;
            
        case eTypeUInt64:
            if (sizeof(long double) == sizeof(m_data.uint64))
                return m_data.ieee_long_double;
            break;
            
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:
            if (sizeof(long double) == sizeof(m_data.uint128))
                return m_data.ieee_long_double;
#endif
        case eTypeFloat:        return m_data.ieee_float;
        case eTypeDouble:       return m_data.ieee_double;
        case eTypeLongDouble:   return m_data.ieee_long_double;
            break;
    }
    if (success_ptr)
        *success_ptr = false;
    return fail_value;
}

const void *
RegisterValue::GetBytes () const
{
    switch (m_type)
    {
        case eTypeInvalid:      break;
        case eTypeUInt8:        return &m_data.uint8;
        case eTypeUInt16:       return &m_data.uint16;
        case eTypeUInt32:       return &m_data.uint32;
        case eTypeUInt64:       return &m_data.uint64;
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:      return &m_data.uint128;
#endif
        case eTypeFloat:        return &m_data.ieee_float;
        case eTypeDouble:       return &m_data.ieee_double;
        case eTypeLongDouble:   return &m_data.ieee_long_double;
        case eTypeBytes:        return m_data.buffer.bytes;
    }
    return NULL;
}

void *
RegisterValue::GetBytes ()
{
    switch (m_type)
    {
        case eTypeInvalid:      break;
        case eTypeUInt8:        return &m_data.uint8;
        case eTypeUInt16:       return &m_data.uint16;
        case eTypeUInt32:       return &m_data.uint32;
        case eTypeUInt64:       return &m_data.uint64;
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:      return &m_data.uint128;
#endif
        case eTypeFloat:        return &m_data.ieee_float;
        case eTypeDouble:       return &m_data.ieee_double;
        case eTypeLongDouble:   return &m_data.ieee_long_double;
        case eTypeBytes:        return m_data.buffer.bytes;
    }
    return NULL;
}

uint32_t
RegisterValue::GetByteSize () const
{
    switch (m_type)
    {
        case eTypeInvalid: break;
        case eTypeUInt8:        return sizeof(m_data.uint8);
        case eTypeUInt16:       return sizeof(m_data.uint16);
        case eTypeUInt32:       return sizeof(m_data.uint32);
        case eTypeUInt64:       return sizeof(m_data.uint64);
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:      return sizeof(m_data.uint128);
#endif
        case eTypeFloat:        return sizeof(m_data.ieee_float);
        case eTypeDouble:       return sizeof(m_data.ieee_double);
        case eTypeLongDouble:   return sizeof(m_data.ieee_long_double);
        case eTypeBytes: return m_data.buffer.length;
    }
    return 0;
}


bool
RegisterValue::SetUInt (uint64_t uint, uint32_t byte_size)
{
    if (byte_size == 0)
    {
        SetUInt64 (uint);
    }
    else if (byte_size == 1)
    {
        SetUInt8 (uint);
    }
    else if (byte_size <= 2)
    {
        SetUInt16 (uint);
    }
    else if (byte_size <= 4)
    {
        SetUInt32 (uint);
    }
    else if (byte_size <= 8)
    {
        SetUInt64 (uint);
    }
#if defined (ENABLE_128_BIT_SUPPORT)
    else if (byte_size <= 16)
    {
        SetUInt128 (uint);
    }
#endif
    else
        return false;
    return true;
}

void
RegisterValue::SetBytes (const void *bytes, size_t length, lldb::ByteOrder byte_order)
{
    // If this assertion fires off we need to increase the size of
    // m_data.buffer.bytes, or make it something that is allocated on
    // the heap. Since the data buffer is in a union, we can't make it
    // a collection class like SmallVector...
    assert (length <= sizeof (m_data.buffer.bytes));
    if (bytes && length > 0)
    {
        m_type = eTypeBytes;
        m_data.buffer.length = length;
        assert (length < sizeof (m_data.buffer.bytes));
        memcpy (m_data.buffer.bytes, bytes, length);
        m_data.buffer.byte_order = byte_order;
    }
    else
    {
        m_type = eTypeInvalid;
        m_data.buffer.length = 0;
    }
}


bool
RegisterValue::operator == (const RegisterValue &rhs) const
{
    if (m_type == rhs.m_type)
    {
        switch (m_type)
        {
            case eTypeInvalid:      return true;
            case eTypeUInt8:        return m_data.uint8 == rhs.m_data.uint8;
            case eTypeUInt16:       return m_data.uint16 == rhs.m_data.uint16;
            case eTypeUInt32:       return m_data.uint32 == rhs.m_data.uint32;
            case eTypeUInt64:       return m_data.uint64 == rhs.m_data.uint64;
#if defined (ENABLE_128_BIT_SUPPORT)
            case eTypeUInt128:      return m_data.uint128 == rhs.m_data.uint128;
#endif
            case eTypeFloat:        return m_data.ieee_float == rhs.m_data.ieee_float;
            case eTypeDouble:       return m_data.ieee_double == rhs.m_data.ieee_double;
            case eTypeLongDouble:   return m_data.ieee_long_double == rhs.m_data.ieee_long_double;
            case eTypeBytes:        
                if (m_data.buffer.length != rhs.m_data.buffer.length)
                    return false;
                else
                {
                    uint8_t length = m_data.buffer.length;
                    if (length > kMaxRegisterByteSize)
                        length = kMaxRegisterByteSize;
                    return memcmp (m_data.buffer.bytes, rhs.m_data.buffer.bytes, length) == 0;
                }
                break;
        }
    }
    return false;
}

bool
RegisterValue::operator != (const RegisterValue &rhs) const
{
    if (m_type != rhs.m_type)
        return true;
    switch (m_type)
    {
        case eTypeInvalid:      return false;
        case eTypeUInt8:        return m_data.uint8 != rhs.m_data.uint8;
        case eTypeUInt16:       return m_data.uint16 != rhs.m_data.uint16;
        case eTypeUInt32:       return m_data.uint32 != rhs.m_data.uint32;
        case eTypeUInt64:       return m_data.uint64 != rhs.m_data.uint64;
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:      return m_data.uint128 != rhs.m_data.uint128;
#endif
        case eTypeFloat:        return m_data.ieee_float != rhs.m_data.ieee_float;
        case eTypeDouble:       return m_data.ieee_double != rhs.m_data.ieee_double;
        case eTypeLongDouble:   return m_data.ieee_long_double != rhs.m_data.ieee_long_double;
        case eTypeBytes:        
            if (m_data.buffer.length != rhs.m_data.buffer.length)
            {
                return true;
            }
            else
            {
                uint8_t length = m_data.buffer.length;
                if (length > kMaxRegisterByteSize)
                    length = kMaxRegisterByteSize;
                return memcmp (m_data.buffer.bytes, rhs.m_data.buffer.bytes, length) != 0;
            }
            break;
    }
    return true;
}

bool
RegisterValue::ClearBit (uint32_t bit)
{
    switch (m_type)
    {
        case eTypeInvalid:
            break;

        case eTypeUInt8:        
            if (bit < 8)
            {
                m_data.uint8 &= ~(1u << bit);
                return true;
            }
            break;
            
        case eTypeUInt16:
            if (bit < 16)
            {
                m_data.uint16 &= ~(1u << bit);
                return true;
            }
            break;

        case eTypeUInt32:
            if (bit < 32)
            {
                m_data.uint32 &= ~(1u << bit);
                return true;
            }
            break;
            
        case eTypeUInt64:
            if (bit < 64)
            {
                m_data.uint64 &= ~(1ull << (uint64_t)bit);
                return true;
            }
            break;
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:
            if (bit < 64)
            {
                m_data.uint128 &= ~((__uint128_t)1ull << (__uint128_t)bit);
                return true;
            }
#endif
        case eTypeFloat:
        case eTypeDouble:
        case eTypeLongDouble:
            break;

        case eTypeBytes:
            if (m_data.buffer.byte_order == eByteOrderBig || m_data.buffer.byte_order == eByteOrderLittle)
            {
                uint32_t byte_idx;
                if (m_data.buffer.byte_order == eByteOrderBig)
                    byte_idx = m_data.buffer.length - (bit / 8) - 1;
                else
                    byte_idx = bit / 8;

                const uint32_t byte_bit = bit % 8;
                if (byte_idx < m_data.buffer.length)
                {
                    m_data.buffer.bytes[byte_idx] &= ~(1u << byte_bit);
                    return true;
                }
            }
            break;
    }
    return false;
}


bool
RegisterValue::SetBit (uint32_t bit)
{
    switch (m_type)
    {
        case eTypeInvalid:
            break;
            
        case eTypeUInt8:        
            if (bit < 8)
            {
                m_data.uint8 |= (1u << bit);
                return true;
            }
            break;
            
        case eTypeUInt16:
            if (bit < 16)
            {
                m_data.uint16 |= (1u << bit);
                return true;
            }
            break;
            
        case eTypeUInt32:
            if (bit < 32)
            {
                m_data.uint32 |= (1u << bit);
                return true;
            }
            break;
            
        case eTypeUInt64:
            if (bit < 64)
            {
                m_data.uint64 |= (1ull << (uint64_t)bit);
                return true;
            }
            break;
#if defined (ENABLE_128_BIT_SUPPORT)
        case eTypeUInt128:
            if (bit < 64)
            {
                m_data.uint128 |= ((__uint128_t)1ull << (__uint128_t)bit);
                return true;
            }
#endif
        case eTypeFloat:
        case eTypeDouble:
        case eTypeLongDouble:
            break;
            
        case eTypeBytes:
            if (m_data.buffer.byte_order == eByteOrderBig || m_data.buffer.byte_order == eByteOrderLittle)
            {
                uint32_t byte_idx;
                if (m_data.buffer.byte_order == eByteOrderBig)
                    byte_idx = m_data.buffer.length - (bit / 8) - 1;
                else
                    byte_idx = bit / 8;
                
                const uint32_t byte_bit = bit % 8;
                if (byte_idx < m_data.buffer.length)
                {
                    m_data.buffer.bytes[byte_idx] |= (1u << byte_bit);
                    return true;
                }
            }
            break;
    }
    return false;
}

OpenPOWER on IntegriCloud