summaryrefslogtreecommitdiffstats
path: root/src/usr/hwpf/ifcompiler/initSymbols.C
blob: 7967c06d37a8e7aa68299694f860cede27b11d47 (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
/*  IBM_PROLOG_BEGIN_TAG
 *  This is an automatically generated prolog.
 *
 *  $Source: src/usr/hwpf/ifcompiler/initSymbols.C $
 *
 *  IBM CONFIDENTIAL
 *
 *  COPYRIGHT International Business Machines Corp. 2010-2012
 *
 *  p1
 *
 *  Object Code Only (OCO) source materials
 *  Licensed Internal Code Source Materials
 *  IBM HostBoot Licensed Internal Code
 *
 *  The source code for this program is not published or other-
 *  wise divested of its trade secrets, irrespective of what has
 *  been deposited with the U.S. Copyright Office.
 *
 *  Origin: 30
 *
 *  IBM_PROLOG_END_TAG
 */
// Change Log *************************************************************************************
//                                                                      
//  Flag Track     Userid   Date     Description                
//  ---- --------  -------- -------- -------------------------------------------------------------
//       D754106   dgilbert 06/14/10 Create
//                 dgilbert 10/22/10 Add spies_are_in()
//                 andrewg  09/19/11 Updates based on review
//                 camvanng 11/08/11 Added support for attribute enums
//                 andrewg  11/09/11 Multi-dimensional array and move to common fapi include
//                 mjjones  11/17/11 Output attribute listing
//                 camvanng 11/17/11 Support for system & target attributes
//                 camvanng 01/07/12 Support for writing an attribute to a SCOM register
//                 camvanng 04/10/12 Support fixed attribute enum value
//                 camvanng 04/16/12 Support defines for SCOM address
//                                   Support defines for bits, scom_data and attribute columns
//                                   Delete obsolete code for defines support
//                 camvanng 05/07/12 Support for associated target attributes
//                 camvanng 06/27/12 Improve error and debug tracing
//                                   Add get_numeric_array_data()
// End Change Log *********************************************************************************

/**
 * @file initSymbols.C
 * @brief Definition of the initSymbols class. Handles all symbols for initfiles
 */

#include <initSymbols.H>
#include <initRpn.H>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <stdlib.h>

using namespace init;

ostringstream errss;

#define SYM_EXPR 0x10000000
const string SYS_ATTR = "SYS.";

// ------------------------------------------------------------------------------------------------

Symbols::Symbols(FILELIST & i_filenames)
    : iv_used_var_count(1), iv_used_lit_count(1), iv_rpn_id(1)
{
    string fileline;

    for(FILELIST::iterator fn = i_filenames.begin(); fn != i_filenames.end(); ++fn)
    {
        printf("Parsing file %s\n",fn->c_str());
        uint32_t lineno = 0;
        ifstream infs(fn->c_str());
        if(!infs)
        {
            errss.str("");
            errss << "ERROR! Symbols::Symbols: Could not open "
                  << *fn << endl;
            throw invalid_argument(errss.str());
        }
        while(getline(infs,fileline))
        {
            ++lineno;
            if(fileline.size() == 0) continue;
            if(fileline[0] == '/') continue;
            istringstream iss(fileline);
            string def;
            iss >> def;
            //printf("def: %s\n",def.c_str());
            if(def == "enum")
            {
                // Make sure it's the AttributeId enum
                iss >> def;
                if(def == "AttributeId")
                {
                    // We've now found the beginning of the attribute enum definition
                    // Read and skip the '{' on the next line
                    getline(infs,fileline);
                    getline(infs,fileline);

                    while(fileline[0] != '}')
                    {
                        istringstream attr_stream(fileline);
                        string attr;
                        char tempChar = 0;
                        uint32_t attrId = 0;

                        // Read the attribute name
                        attr_stream >> attr;

                        // Read and ignore the '=' '0' 'x' characters
                        attr_stream >> tempChar;
                        attr_stream >> tempChar;
                        attr_stream >> tempChar;

                        // Read the attribute ID
                        attr_stream >> hex >> attrId;

                        // Store the value
                        iv_symbols[attr] = MAP_DATA(attrId,NOT_USED);

                        getline(infs,fileline);
                    }
                }
                else
                {
                    // Make sure it's an attribute enum

                    string attribute_enum_name;
                    string find_enum = "_Enum";

                    // Check for _Enum in the name
                    size_t pos = def.find(find_enum);
                    if(pos != string::npos)
                    {
                        // We've now found the beginning of the attribute enum definition
                        // Read and skip the '{' on the next line
                        getline(infs,fileline);
                        getline(infs,fileline);

                        // We're just parsing the enum in order so values start
                        // at 0 and increment by 1 after that unless they are
                        // explicitly assigned.
                        uint64_t value = 0;

                        while(fileline[0] != '}')
                        {
                            istringstream attr_enum_stream(fileline);
                            string attr_enum;
                            string tmp;
                         
                            // Get the attribute enum name
                            attr_enum_stream >> attr_enum;

                            // Strip off the "," at the end.
                            pos = attr_enum.find(',');
                            if(pos != string::npos)
                            {
                                attr_enum = attr_enum.substr(0,attr_enum.length()-1);
                            }
                            else
                            {
                                // Is a value for the enum specified?
                                attr_enum_stream >> tmp;

                                if (!attr_enum_stream.eof())
                                {
                                    //Make sure it's an '='
                                    if ("=" != tmp)
                                    {
                                        printf ("ERROR: Unknown attribute enum! %s\n",attr_enum.c_str());
                                        exit(1);
                                    }
                                    else
                                    {
                                        attr_enum_stream >> tmp;
                                        value = strtoull(tmp.c_str(), NULL, 0);
                                    }
                                }
                            }

                            //printf("Attribute Enum String:%s Value %u\n",attr_enum.c_str(), value);

                            // Get a value for the string
                            iv_attr_enum[attr_enum] = value;
                            value++;
                            getline(infs,fileline);
                        }
                    }
                }
            }
            else if(def == "typedef")
            {
                 string type;
                 string attribute_name;
                 string find_type = "_Type";
                 string find_array = "[";
                 uint32_t array = 0;
                 iss >> type;
                 iss >> attribute_name;
                 if(attribute_name == "*")
                 {
                     // It's a pointer type so read in the next string
                     iss >> attribute_name;
                     type = type + "*";
                 }
                 //printf("typedef: type:%s  attribute_name:%s\n",type.c_str(),attribute_name.c_str());

                 // Determine how many (if any) dimensions this array has
                 size_t pos = attribute_name.find(find_array);
                 while(pos != string::npos)
                 {
                     array++;
                     pos = attribute_name.find(find_array,pos+1);
                 }

                 // Now strip off the _type in the name
                 pos = attribute_name.find(find_type);
                 if(pos != string::npos)
                 {
                     attribute_name = attribute_name.substr(0,pos);
                 }
                 else
                 {
                     printf ("ERROR: Unknown attribute type! %s\n",attribute_name.c_str());
                     exit(1);
                 }

                 iv_attr_type[attribute_name] = get_attr_type(type,array);
                 //printf("Attribute %s Type  with array dimension %u for %s is %u\n",attribute_name.c_str(),array,
                 //       type.c_str(),get_attr_type(type,array));
            }
        }
        infs.close();
    }
    iv_rpn_map[Rpn::SYMBOL | INIT_EXPR_VAR] = RPN_DATA("EXPR",SYM_EXPR);
    iv_rpn_map[Rpn::SYMBOL | INIT_ANY_LIT]  = RPN_DATA("ANY",CINI_LIT_MASK);
}

uint32_t Symbols::get_attr_type(const string &i_type, const uint32_t i_array)
{
    uint32_t l_type = 0;

    if (i_type == "uint8_t")
    {
        l_type = SYM_ATTR_UINT8_TYPE;
    }
    else if(i_type == "uint32_t")
    {
        l_type = SYM_ATTR_UINT32_TYPE;   
    }
    else if(i_type == "uint64_t")
    {
        l_type = SYM_ATTR_UINT64_TYPE;
    }
    else
    {
        printf("Unknown data type: %s\n",i_type.c_str());
        exit(-1);
    }

    if(i_array > MAX_ATTRIBUTE_ARRAY_DIMENSION)
    {
        printf("Array dimension size for %s %u exceeded maximum dimension of %u\n",
               i_type.c_str(),i_array,MAX_ATTRIBUTE_ARRAY_DIMENSION);
        exit(-1);
    }
    // See enum definition on why this works
    l_type += (i_array*ATTR_DIMENSION_SIZE_MULT)+i_array;

    return(l_type);
}

uint32_t Symbols::get_attr_type(const uint32_t i_rpn_id)
{
    string l_attr_name = find_name(i_rpn_id);
    return(iv_attr_type[l_attr_name]);
}

// ------------------------------------------------------------------------------------------------

uint32_t Symbols::use_symbol(string & i_symbol)
{
    uint32_t rpn_id = Rpn::SYMBOL | NOT_FOUND;
    string l_symbol = i_symbol;

    if(i_symbol == "ANY") rpn_id = INIT_ANY_LIT | Rpn::SYMBOL;
    else if(i_symbol == "EXPR") rpn_id = INIT_EXPR_VAR | Rpn::SYMBOL;
    else
    {
        SYMBOL_MAP::iterator i = iv_symbols.find(i_symbol);
        if(i != iv_symbols.end())
        {
            if(i->second.second == NOT_USED)
            {
                rpn_id = Rpn::SYMBOL | iv_rpn_id++;
                i->second.second = rpn_id;
                iv_rpn_map[rpn_id] = RPN_DATA(i_symbol,i->second.first);

                //printf ("Symbols::use_symbol: Just added %s symbol, rpn_id:0x%8X to iv_rpn_map\n",i_symbol.c_str(),rpn_id);

                if(i->second.first & CINI_LIT_MASK)     ++iv_used_lit_count;
                else                                    ++iv_used_var_count;
            }
            else
            {
                rpn_id = i->second.second;
            }
        }
        else 
        {
            //Strip off any prefix (i.e. "SYS." or "TGT<#>.")
            size_t pos = i_symbol.find('.');
            if(pos != string::npos)
            {
                //Find the attribute without the prefix.
                //If found, then add this system or assoc target attribute
                //to our containers.
                l_symbol = i_symbol.substr(pos+1);
                SYMBOL_MAP::iterator i = iv_symbols.find(l_symbol);
                if(i != iv_symbols.end())
                {
                    //Add the new attribute

                    rpn_id = Rpn::SYMBOL | iv_rpn_id++;
                    uint32_t attrId = iv_symbols[l_symbol].first;

                    iv_rpn_map[rpn_id] = RPN_DATA(i_symbol,attrId);
                    iv_symbols[i_symbol] = MAP_DATA(attrId, rpn_id);
                    iv_attr_type[i_symbol] = iv_attr_type[l_symbol];

                    ++iv_used_var_count;

                    //printf ("Symbols::use_symbol: Just added %s symbol, rpn_id:0x%8X\n",i_symbol.c_str(),rpn_id);
                }
                else
                {
                    rpn_id = add_undefined(i_symbol);
                }
            }
            else
            {
                rpn_id = add_undefined(i_symbol);
            }
        }
    }

    return rpn_id;
}

// ------------------------------------------------------------------------------------------------

uint32_t Symbols::add_undefined(const string & i_symbol)
{
    uint32_t rpn_id = 0;

    SYMBOL_MAP::iterator i = iv_not_found.find(i_symbol);
    if(i != iv_not_found.end())
    {
        rpn_id = i->second.second;
    }
    else
    {
        rpn_id = Rpn::SYMBOL | iv_rpn_id++;
        iv_not_found[i_symbol] = MAP_DATA(NOT_FOUND,rpn_id);
        iv_rpn_map[rpn_id] = RPN_DATA(i_symbol,CINI_ID_NOT_FOUND);
    }
    return rpn_id;
}

// ------------------------------------------------------------------------------------------------

uint16_t Symbols::get_tag(uint32_t i_rpn_id)
{
    uint16_t tag = NOT_FOUND;

    // Set up tag table if not already setup
    if(iv_used_var.size() == 0)
    {
        iv_used_var.reserve(iv_used_var_count); // makes if faster
        iv_used_lit.reserve(iv_used_lit_count);

        //To differentiate between system, target, and associated target attributes
        //which have the same attribute id, save the attribute name also.
        iv_used_var.push_back(RPN_DATA("EXPR",SYM_EXPR));                    // EXPR var always first
        iv_used_lit.push_back(iv_rpn_map[Rpn::SYMBOL|INIT_ANY_LIT].second);  // ANY lit always first

        for(SYMBOL_MAP::iterator i = iv_symbols.begin(); i != iv_symbols.end(); ++i)
        {
            if(i->second.second != NOT_USED)
            {
                //printf("Symbols::get_tag adding rpn_id[0x%x]\n", i->second.second);

                if((i->second.first & CINI_LIT_MASK) ==  CINI_LIT_MASK)
                {
                    iv_used_lit.push_back(i->second.first);
                    //printf("Symbols::get_tag added to iv_used_lit[0x%x]\n", iv_used_lit.back());
                }
                else  //VAR
                {
                    iv_used_var.push_back(RPN_DATA(i->first, i->second.first));
                    //printf("Symbols::get_tag added to iv_used_var[%s, 0x%x]\n",
                    //       iv_used_var.back().first.c_str(), iv_used_var.back().second);
                }
            }
        }
    }

    do
    {
        string name = find_name(i_rpn_id);
        if ("NOT_FOUND" == name)
        {
            //SYMBOL_MAP::iterator sm = iv_not_found.begin();
            //for(; sm != iv_not_found.end(); ++sm)
            //{
            //    if((sm->second).second == i_rpn_id)
            //    {
            //        break;
            //    }
            //}

            //if(sm == iv_not_found.end())
            //{
                ostringstream err;
                err << hex;
                err << "ERROR! Symbols::get_tag() bad arg rpn_id = " << i_rpn_id << endl;
                throw invalid_argument(err.str());
            //}
            break;
        }

        uint32_t offset = 0;
        for(VAR_SYMBOL_USED::iterator i = iv_used_var.begin(); i != iv_used_var.end(); ++i,++offset)
        {
            if (name == (*i).first)
            {
                if (name.compare(0, ASSOC_TGT_ATTR.length(), ASSOC_TGT_ATTR) == 0)
                {
                    tag = (uint16_t) (offset | IF_ASSOC_TGT_ATTR_TYPE);
                }
                else if (name.compare(0, SYS_ATTR.length(), SYS_ATTR) == 0)
                {
                    tag = (uint16_t) (offset | IF_SYS_ATTR_TYPE);
                }
                else
                {
                    tag = (uint16_t) (offset | IF_ATTR_TYPE);
                }

                //printf ("get tag: %s tag 0x%x\n", name.c_str(), tag);
                break;
            }
        }
        
    } while(0);

    return tag;
}




// ------------------------------------------------------------------------------------------------

string Symbols::find_name(uint32_t i_rpn_id)
{
    string name;
    RPN_MAP::iterator rm = iv_rpn_map.find(i_rpn_id);
    if(rm != iv_rpn_map.end())
    {
        name = (rm->second).first;
    }
    else name = "NOT FOUND";
    return name;
}

// ------------------------------------------------------------------------------------------------

uint32_t Symbols::find_numeric_lit(uint64_t i_data, int32_t byte_size)
{
    uint32_t offset = 0;
    LIT_LIST::iterator i = iv_lits.begin();
    for(; i != iv_lits.end(); ++i,++offset)
    {
        if(i_data == i->first && (uint32_t)byte_size == i->second)
            break;
    }
    if(i == iv_lits.end())
    {
        iv_lits.push_back(LIT_DATA(i_data,byte_size));
    }
    //printf("Symbols::find_numeric_lit: i_data:0x%llX byte_size:%d Tag:0x%X\n",
    //      i_data,byte_size, offset | Rpn::NUMBER);
    return offset | Rpn::NUMBER;
}

uint32_t Symbols::find_numeric_array_lit(uint64_t i_data, int32_t byte_size)
{
    uint32_t offset = 0;
    LIT_LIST::iterator i = iv_lits.begin();
    for(; i != iv_lits.end(); ++i,++offset)
    {
        if(i_data == i->first && (uint32_t)byte_size == i->second)
            break;
    }
    if(i == iv_lits.end())
    {
        iv_lits.push_back(LIT_DATA(i_data,byte_size));
    }
    //printf("Symbols::find_numeric_lit: i_data:0x%llX byte_size:%d Tag:0x%X\n",
    //      i_data,byte_size, offset | Rpn::NUMBER);
    return offset | Rpn::ARRAY_INDEX;
}

// ------------------------------------------------------------------------------------------------

uint16_t Symbols::get_numeric_tag(uint32_t i_rpn_id)
{
    uint32_t tag = NOT_FOUND;
    uint32_t offset = i_rpn_id - Rpn::NUMBER;
    string any("ANY");
    if(iv_used_lit.size() == 0) get_tag(use_symbol(any));
    if(offset < iv_lits.size())
    {
        // numeric lits are numbered after enum lits, but with different TYPE
        tag = (iv_used_lit.size() + offset) | IF_NUM_TYPE;
    }
    else
    {
        ostringstream err;
        err << hex;
        err << "ERROR! - Symbols::get_numeric_tag() invalid arg rpn_id = " << i_rpn_id << endl;
        throw invalid_argument(err.str());
    }
    return  (uint16_t)tag;
}

// ------------------------------------------------------------------------------------------------

uint16_t Symbols::get_numeric_array_tag(uint32_t i_rpn_id)
{
    uint32_t tag = NOT_FOUND;
    uint32_t offset = i_rpn_id - Rpn::ARRAY_INDEX;
    string any("ANY");
    if(iv_used_lit.size() == 0) get_tag(use_symbol(any));
    if(offset < iv_lits.size())
    {
        // numeric lits are numbered after enum lits, but with different TYPE
        tag = (iv_used_lit.size() + offset) | IF_NUM_TYPE;
    }
    else
    {
        ostringstream err;
        err << hex;
        err << "ERROR! - Symbols::get_numeric_array_tag() invalid arg rpn_id = " << i_rpn_id << endl;
        throw invalid_argument(err.str());
    }
    return  (uint16_t)tag;
}

// ------------------------------------------------------------------------------------------------

uint64_t Symbols::get_numeric_data(uint32_t i_rpn_id, uint32_t & o_size)
{
    uint64_t data = 0;
    o_size = 0;
    uint32_t offset = i_rpn_id - Rpn::NUMBER;
    if(offset < iv_lits.size())
    {
        LIT_DATA d = iv_lits[offset];
        data = d.first;
        o_size = d.second;
    }
    else
    {
        ostringstream err;
        err << hex;
        err << "ERROR! - Symbols::get_numeric_data() invalid arg rpn_id = " << i_rpn_id << endl;
        throw invalid_argument(err.str());
    }
    return data;
}

// ------------------------------------------------------------------------------------------------

uint64_t Symbols::get_numeric_array_data(uint32_t i_rpn_id, uint32_t & o_size)
{
    uint64_t data = 0;
    o_size = 0;
    uint32_t offset = i_rpn_id - Rpn::ARRAY_INDEX;
    if(offset < iv_lits.size())
    {
        LIT_DATA d = iv_lits[offset];
        data = d.first;
        o_size = d.second;
    }
    else
    {
        ostringstream err;
        err << hex;
        err << "ERROR! - Symbols::get_numeric_array_data() invalid arg rpn_id = " << i_rpn_id << endl;
        throw invalid_argument(err.str());
    }
    return data;
}

// ------------------------------------------------------------------------------------------------
uint64_t Symbols::get_attr_enum_val(string & i_attr_enum)
{
    return iv_attr_enum[i_attr_enum];
}

// ------------------------------------------------------------------------------------------------

string Symbols::find_text(uint32_t i_cini_id)
{
    string name = "NOT FOUND";
    if(i_cini_id == CINI_LIT_MASK) name = "ANY";
    else if(i_cini_id == SYM_EXPR) name = "EXPR";
    else
    {
        //NOTE:  if there are multiple elements of the same cini_id, then this function will return the
        //first element found
        for(SYMBOL_MAP::const_iterator i = iv_symbols.begin(); i != iv_symbols.end(); ++i)
        {
            //printf("SYMBOL:%s\n",i->first.c_str());
            if((i->second).first == i_cini_id)
            {
                name = i->first;
                break;
            }
        }
//        for(RPN_MAP::iterator i = iv_rpn_map.begin(); i != iv_rpn_map.end(); ++i)
//        {
//            if((i->second).second == i_cini_id) 
//            {
//                name = (i->second).first;
//                break;
//            }
//        }
    }
    return name;
}


// ------------------------------------------------------------------------------------------------

uint32_t Symbols::get_spy_id(const string & spyname)
{
    uint32_t id = NOT_FOUND;
    string s = spyname;
    translate_spyname(s);
    SPY_MAP::iterator sm = iv_spymap.find(s);
    if(sm != iv_spymap.end())
    {
        id = sm->second;
    }
    else
    {
        size_t pos = s.find('-');
        if(pos == string::npos) s.insert(0,"SPY_");
        else
        {
            s[pos] = '_';
            s.insert(pos+1,"SPY_");
            s.insert(0,"ENUM_");
        }
        iv_not_found[s] = MAP_DATA(0,0);
        //cerr << "ERROR! Symbols::get_spy_id() Spyname not found " << '[' << s << ']' << endl;
    }
    return id;
}

// ------------------------------------------------------------------------------------------------

uint32_t Symbols::use_enum(const string & enumname)
{
    uint32_t rpn_id = NOT_FOUND;
    string s = enumname;
    translate_spyname(s);
    SPY_MAP::iterator sm = iv_enums.find(s);
    if(sm != iv_enums.end())
    {
        rpn_id = sm->second;
    }
    else
    {
        printf("Error!\n");
        exit(0);
    }
    return rpn_id;
}

// ------------------------------------------------------------------------------------------------

uint32_t Symbols::get_spy_enum_id(uint32_t i_rpn_id, const string & spyname)
{
    //uint32_t id = NOT_FOUND;
    string enumname = get_enum_name(i_rpn_id);
    enumname.append("-");
    enumname.append(spyname);
    return get_spy_id(enumname);
}

// ------------------------------------------------------------------------------------------------

string Symbols::get_enum_name(uint32_t i_rpn_id)
{
    string name("SPY ENUM NOT FOUND");
    for(SPY_MAP::iterator i = iv_enums.begin(); i != iv_enums.end(); ++i)
    {
        if(i->second == i_rpn_id)
        {
            name = i->first;
            break;
        }
    }
    return name;
}

// ------------------------------------------------------------------------------------------------

string Symbols::full_listing()
{
    uint32_t count = 0;
    ostringstream oss;
    oss << hex << setfill('0');

    for(SYMBOL_MAP::iterator i = iv_symbols.begin(); i != iv_symbols.end(); ++i)
    {
        if(i->second.first < CINI_LIT_MASK) //VAR
        {
            ++count;
            oss << "0x" << setw(8) << i->second.first << '\t'
                << '[' << i->first << ']' << endl;
        }
    }

    ostringstream title;
    title << "\n--------------- Attribute Symbol Table ---------------\n\n"
          << "0x" << hex << setfill('0') << setw(8) << count << '\t' 
          << "Number of variables\n" << oss.str();

    oss.str("");
    count = 0;

    for(SYMBOL_MAP::iterator i = iv_symbols.begin(); i != iv_symbols.end(); ++i)
    {
        if((i->second.first & CINI_LIT_MASK) == CINI_LIT_MASK) //LIT
        {
            ++count;
            oss << "0x" << setw(8) << i->second.first << '\t'
                << '[' << i->first << ']' << endl;
        }
    }

    title << "\n--------------- Literal Symbol Table -----------------\n\n"
        << setw(8) << count << '\t' << "Number of enumerated literals\n"
        << oss.str();
    
    oss.str("");
    title << "\n-------------------- Spies and arrays ----------------------\n\n"
          << "0x" << setw(8) << iv_spymap.size() << '\t' << "Number of spies and array symbols\n";

    for(SPY_MAP::iterator i = iv_spymap.begin(); i != iv_spymap.end(); ++i)
    {
        oss << "0x" << setw(8) << i->second << '\t' << '[' << i->first << ']' << endl;
    }

    title << oss.str();

    return title.str();
}

// ------------------------------------------------------------------------------------------------

string Symbols::listing()
{
    uint32_t count = 0;
    ostringstream oss;

    // Set up tag table if not already setup
    string any = "ANY";
    get_tag(use_symbol(any));

    oss << hex << setfill('0');

    oss << "\n--------------- Attribute Symbol Table ---------------\n\n"
        << "0x" << setw(4) << iv_used_var.size()-1 << '\t' << "Number of variables\n";

    for(VAR_SYMBOL_USED::iterator i = iv_used_var.begin() + 1; i != iv_used_var.end(); ++i)
    {
        ++count;
        uint32_t id = count;

        string name = (*i).first;
        uint32_t attrId = (*i).second;
        if(name.compare(0, ASSOC_TGT_ATTR.length(), ASSOC_TGT_ATTR) == 0)
        {
            id |= IF_ASSOC_TGT_ATTR_TYPE;
        }
        else if(name.compare(0, SYS_ATTR.length(), SYS_ATTR) == 0)
        {
            id |= IF_SYS_ATTR_TYPE;
        }
        else
        {
            id |= IF_ATTR_TYPE;
        }

        oss << "Type:" << setw(2) << iv_attr_type[name] << " Value:0x" << setw(8) << attrId << '\t' << "ID 0X" << setw(4) << id
            << '\t' << '[' << name << ']' << endl;

        //printf("ATTRIBUTE: %s  Value:0x%02X\n",name,iv_attr_type[name]);
    }

    oss << "\n--------------- Literal Symbol Table -----------------\n\n";

    oss << "\n0x" << setw(4) << iv_lits.size() << '\t' << "Number of numeric literals\n";

    count = 0;
    for(LIT_LIST::iterator i = iv_lits.begin(); i != iv_lits.end(); ++i,++count)
    {
        oss << "0x" << setw(2) << i->second << endl
            << "0x" << setw(2 * i->second) << i->first;
        if(i->second < 6)        oss << "\t\t";
        else                    oss<< '\t';
        oss << "ID 0x" << setw(4) << get_numeric_tag(count | Rpn::NUMBER) << endl;
    }

    oss << not_found_listing();

    return oss.str();
}

// ------------------------------------------------------------------------------------------------

string Symbols::attr_listing()
{
    ostringstream oss;

    // Set up tag table if not already setup
    string any = "ANY";
    get_tag(use_symbol(any));

    for(VAR_SYMBOL_USED::iterator i = iv_used_var.begin() + 1; i != iv_used_var.end(); ++i)
    {
        string name = (*i).first;

        //Strip off any prefix (i.e. "SYS." or "TGT<#>.")
        size_t pos = name.find('.');
        if(pos != string::npos)
        {
            name = name.substr(pos+1);
        }

        oss << name << endl;
    }

    return oss.str();
}

// ------------------------------------------------------------------------------------------------

string Symbols::not_found_listing()
{
    ostringstream oss;
    if(iv_not_found.size())
    {
        oss << "\n------------- Symbols requested that were NOT FOUND ---------------\n\n";
        for(SYMBOL_MAP::iterator i = iv_not_found.begin(); i != iv_not_found.end(); ++i)
        {
            //if(i->first == "SPY_NOTFOUND" || (i->first).compare(0,13,"ENUM_NOTFOUND") == 0) continue;
            oss << '[' << i->first << ']' << endl;
        }
    }
    return oss.str();
}

// ------------------------------------------------------------------------------------------------

uint32_t Symbols::bin_vars(BINSEQ & blist)
{
    // Set up tag table if not already setup
    string any = "ANY";
    get_tag(use_symbol(any));

    uint32_t count = iv_used_var.size() - 1; // first VAR is 'EXPR' and is not stored

    Rpn::set16(blist,(uint16_t)count);

    for(VAR_SYMBOL_USED::iterator i = iv_used_var.begin() + 1; i != iv_used_var.end(); ++i)
    {
        // Write out the attribute type (i.e. uint8_t, uint16_t, ...) and it's attribute id number

        string name = (*i).first;
        uint32_t attrId = (*i).second;

        Rpn::set8(blist,iv_attr_type[name]);

        Rpn::set32(blist,attrId);
        //printf("Symbols::bin_vars: Just wrote out type:%u attrId:0x%x\n",iv_attr_type[name],attrId);
    }
    return count;
}

//-------------------------------------------------------------------------------------------------

uint32_t Symbols::bin_lits(BINSEQ & blist)
{
    // Set up tag table if not already setup
    string any = "ANY";
    get_tag(use_symbol(any));

    uint32_t count = iv_lits.size();
    uint32_t total = count;

    Rpn::set16(blist,(uint16_t)count);

    for(LIT_LIST::iterator i = iv_lits.begin(); i != iv_lits.end(); ++i)
    {
        uint32_t size = i->second;
        uint64_t data = i->first;
        blist.push_back( (uint8_t)size );
        uint32_t shift_count = size * 8;
        while(shift_count)
        {
            shift_count -= 8;
            blist.push_back( (uint8_t)(data >> shift_count) );
        }
    }
    total += count;

    return total;
}

//-------------------------------------------------------------------------------------------------

uint32_t Symbols::restore_var_bseq(BINSEQ::const_iterator & bli)
{
    uint32_t count = Rpn::extract16(bli);

    for(uint32_t i = 0; i < count; ++i)
    {
        uint8_t type = Rpn::extract8(bli);
        uint32_t attrId = Rpn::extract32(bli);

        string name = find_text(attrId);
        string used_var_name = iv_used_var.at(i+1).first;

        // Account for system & associated target attributes
        if (name != used_var_name)
        {
            size_t pos = used_var_name.find(name);
            if(pos != string::npos)
            {
                attrId = iv_symbols[used_var_name].first;

            }
            else
            {
                ostringstream errs;
                errs << "ERROR! Symbols::restore_var_bseq().  Invalid attribute id ["
                    << attrId << ']' << endl;
                throw invalid_argument(errs.str());
            }

        }

        iv_attr_type[used_var_name] = type;
        iv_used_var.at(i+1).second = attrId;
    }

    return count;
}

//-------------------------------------------------------------------------------------------------

uint32_t Symbols::restore_lit_bseq(BINSEQ::const_iterator & bli)
{
    iv_used_lit.clear();
    iv_used_lit.push_back(iv_rpn_map[Rpn::SYMBOL|INIT_ANY_LIT].second);  // ANY lit always first

    iv_lits.clear();
    uint32_t num_count = Rpn::extract16(bli);

    for(uint32_t i = 0; i < num_count; ++i)
    {
        uint8_t size = *bli++;
        uint64_t data = 0;
        switch (size)
        {
            case 1: data = *bli++;  break;
            case 2: data = Rpn::extract16(bli); break;
            case 4: data = Rpn::extract32(bli); break;
            case 8: data = Rpn::extract64(bli); break;
            default:
                    {
                        ostringstream errs;
                        errs << "ERROR! Symbols::restore_lit_bseq().  Invalid literal data size ["
                            << size << ']' << endl;
                        throw invalid_argument(errs.str());
                    }
                    break;
        }

        iv_lits.push_back( LIT_DATA(data, size) );
    }

    return num_count;
}

//-------------------------------------------------------------------------------------------------

string Symbols::get_spyname(uint32_t spy_id)
{
    string name;

    for(SPY_MAP::const_iterator i = iv_spymap.begin(); i != iv_spymap.end(); ++i)
    {
        if (i->second == spy_id) 
        {
            name = i->first;
            break;
        }
    }
    if(name.length() == 0)
    {
        ostringstream oss;
        oss << hex << setfill('0');
        oss << "[0x" << setw(8) << spy_id << ']';
        name = oss.str();
    }
    return name;
}
//-------------------------------------------------------------------------------------------------

string Symbols::get_enumname(uint32_t spy_id)
{
    string name = get_spyname(spy_id);
    size_t pos = name.find('-');
    if(pos != string::npos)
    {
        name = name.substr(0,pos);
    }
    return name;
}

//-------------------------------------------------------------------------------------------------

uint32_t Symbols::get_rpn_id(uint32_t bin_tag)
{
    uint32_t rpn_id = NOT_FOUND;
    uint32_t type = bin_tag & IF_TYPE_MASK;
    uint32_t offset = bin_tag & ~IF_TYPE_MASK;
    switch(type)
    {
        case IF_ATTR_TYPE:
        case IF_SYS_ATTR_TYPE:
        case IF_ASSOC_TGT_ATTR_TYPE:
                        {
                            string name = iv_used_var[offset].first;
                            rpn_id = use_symbol(name);
                        }
                        break;

        case IF_NUM_TYPE:  {
                            offset -= iv_used_lit.size();
                            if(offset >= iv_lits.size())
                            {
                                ostringstream erros;
                                erros << hex;
                                erros << "ERROR! Symbols::get_rpn_id() Invalid NUM_TYPE 0x" 
                                    << bin_tag;
                                throw range_error(erros.str());
                            }
                            LIT_DATA d = iv_lits[offset];
                            rpn_id = find_numeric_lit(d.first,d.second);
                        }
                        break;

        default:
                        {
                            ostringstream erros;
                            erros << hex
                                  << "ERROR! Symbols::get_rpn_id()  Invalid bin_tag = 0x" 
                                  << bin_tag << endl;
                            throw range_error(erros.str());
                        }
                        break;
    }
    return rpn_id;
}
//-------------------------------------------------------------------------------------------------

string Symbols::spies_are_in(Symbols & i_full_list, const set<string> & i_ignore_spies)
{
    ostringstream result;
    result << hex;

    for(SPY_MAP::iterator i = iv_spymap.begin(); i != iv_spymap.end(); ++i)
    {
        // enums in the reduced file will soon no longer contain the spyname as part of the enum name <enum name>-<spy name>
        // At that time we will just need to check the enum part of the name  <enum name> in both the ignore file and the full list
        //  When initfile processing AND spyhunter both use only enum names for enum spies then we can simplify all this
        string spyname = i->first;
        size_t pos = spyname.find('-');  // check for enum - if so just use the spy part of the name
        if(pos != string::npos)
        {
            spyname = spyname.substr(pos+1);
        }

        if(i_ignore_spies.find(spyname) != i_ignore_spies.end()) //don't check this spy or any of it's enums
        {
            cout << "Will not check spy: " << i->first << endl;
            continue;
        }

        uint32_t hash = 0;
        if(pos != string::npos)  // old enum style name - check by hash - only check enumname
        {
            // just compare enum names based on hash
            string enumname1 = (i->first).substr(0,pos);
            string enumname2 = i_full_list.get_enumname(i->second);
            if(enumname1 != enumname2)
            {
                result << "ERROR! Enum not found for spy " << i->first << '\n'
                       << "       Enum: " << enumname1 << "!=" << enumname2 << endl;
            }
        }
        else   // check spyname by name or new-style enum by name
        {
            hash = i_full_list.get_spy_id(i->first);
            if(hash != NOT_FOUND)
            {
                if(hash != i->second)
                {
                    result << "ERROR! HASH not the same for spy " 
                        << i->first << ' ' << hash << ' ' << i->second << endl;
                }
                // else cout << "Found " << i->first << ' ' << i->second << endl;
            }
        }
    }
    result << i_full_list.not_found_listing();
    

    return result.str();
}





OpenPOWER on IntegriCloud