summaryrefslogtreecommitdiffstats
path: root/src/build/ifcompiler/initCompiler.C
blob: 1f3cc24d687a58b28668f8eff4c712beada59d4c (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
// IBM_PROLOG_BEGIN_TAG
// This is an automatically generated prolog.
//
// $Source: /afs/awd/projects/eclipz/KnowledgeBase/.cvsroot/eclipz/ifcompiler/initCompiler.C,v $
//
// IBM CONFIDENTIAL
//
// COPYRIGHT International Business Machines Corp. 2010,2010
//
//UNDEFINED 
//
// Origin: UNDEFINED
//
// IBM_PROLOG_END_TAG
// Change Log *************************************************************************************
//                                                                      
//  Flag   Track    Userid   Date     Description                
//  ----- -------- -------- -------- -------------------------------------------------------------
//         D754106 dgilbert 06/14/10 Create
//  dg002 SW039868 dgilbert 10/15/10 Add support to filter unneeded inits by EC
//  dg003  D779902 dgilbert 12/08/10 Add ability to specify ouput if file
//                 andrewg  05/24/11 Port over for VPL/PgP
//                 andrewg  09/19/11 Updates based on review
// End Change Log *********************************************************************************

/**
 * @file initCompiler.C
 * @brief Compile an initfile into bytecode.
 */
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <map>
#include <stdexcept>
#include <initCompiler.H>
#include <initRpn.H>
#include <initSymbols.H>
#include <initScom.H>
//#include <initSpy.H>

using namespace init;
using namespace std;

//Globals

int yyline = 1;
init::ScomList * yyscomlist = NULL;

ostringstream init::dbg;
ostringstream init::erros;
ostringstream init::stats;   // TODO move to Parser

// Main
int main(int narg, char ** argv)
{
    int rc = 0;

#if 0
    yyin = fopen("sample.initfile","r");
    if(!yyin)
    {
        std::cerr << "\nERROR: Failed to open sample.initfile! " << std::endl;
        exit(-1);
    }
    yyparse();
    fclose(yyin);
#endif

    try
    {
        // Parser: 
        //   - Parse args
        //   - Set up source location and source type
        //   - Load & parse Symbols & Spy/Array tables
        //   - Load & parse the initfile (if there is one)
        //
        Parser parsed(narg,argv);

        string initfile = parsed.source_fn();
        uint32_t type = parsed.get_source_type();

        BINSEQ bin_seq;
        bin_seq.reserve(0x38000);

        if(type == Parser::IF_TYPE)  // input is binary *.if file - build listing from it.
        {

            //for(SPY_LIST::iterator i = yyspylist->begin(); i != yyspylist->end(); ++i)
            //{
            //    cout << (*i)->listing() << endl;
            //}

            ifstream ifs(initfile.c_str(), ios_base::in | ios_base::binary);
            if(!ifs)
            {
                string msg("Can't open ");
                msg.append(initfile);
                throw invalid_argument(msg);
            }
            while(1)
            {
                int ch = ifs.get();
                if (!(ifs.good())) break;
                bin_seq.push_back(ch);
            }
            ifs.close();

            yyscomlist->listing(bin_seq, cout);

            erros << parsed.get_scomlist()->get_symbols()->not_found_listing();

        }
        else // normal initfile processing
        {
            // Already parsed
            yyscomlist->compile(bin_seq);


            std::cerr << "Compiled size = " << std::dec << bin_seq.size() << endl;

            // if there are missing symbols, SpyList::listing() will add duplicates
            // So get the listing now
            erros << parsed.get_scomlist()->get_symbols()->not_found_listing();

            string if_fn = parsed.binseq_fn();
            ofstream ofs(if_fn.c_str(), ios_base::out | ios_base::binary);
            if(!ofs)
            {
                erros << "ERROR - Could not open" << if_fn << endl;
                throw invalid_argument(if_fn);
            }
            else
            {
                for(BINSEQ::const_iterator bli = bin_seq.begin(); bli != bin_seq.end(); ++bli)
                    ofs.put((char)(*bli));

                ofs.close();
            }
            //cout << dbg << std::endl;
            printf("Generate Listing\n");
            // This builds a listing from the compiled binary sequence
            yyscomlist->listing(bin_seq, parsed.listing_ostream());

            // open if file and read in to new SpyList
            
            printf("Generate Stats\n");
            stats << "*********************************************************\n";

            cerr << stats.str() << endl;  // TODO -> cout

        }

        printf("Generate Debug\n");
        parsed.capture_dbg();
        //if(parsed.debug_mode()) cout << dbg.str() << endl;
    }
    catch(exception & e)
    {
        cerr << "ERROR! exception caught: " << e.what() << endl;
        rc = 2;
    }

    if(erros.str().size())
    {
        rc = 1;
        cerr << erros.str() << endl;
    }
    return rc;
}

// ------------------------------------------------------------------------------------------------
//  Parser:
//    Check the args and build the symbol table
//  -----------------------------------------------------------------------------------------------

Parser::Parser(int narg, char ** argv)
: iv_type(0), iv_scomlist(NULL), iv_dbg(false), iv_ec(0xFFFFFFFF)   //dg002c
{
    set<string> header_files;
    iv_prog_name = argv[0];

    stats << iv_prog_name << endl;
    --narg; ++argv;

    string type;

    pair<string,string> compare;

    for(int i = 0; i < narg; ++i)
    {
        string arg(argv[i]);
        if(arg.compare(0,5,"-init") == 0)  iv_source_path = argv[++i];
        else if (arg.compare(0,3,"-kw") == 0 ||
                 arg.compare(0,4,"-spy") == 0 ||
                 arg.compare(0,5,"-attr") == 0 || 
                 arg.compare(0,6,"-array") == 0 ) header_files.insert(string(argv[++i]));
        else if (arg.compare(0,7,"-outdir") == 0) iv_outdir = argv[++i];
        else if (arg.compare(0,2,"-o") == 0)      iv_outfile = argv[++i];              //dg003a
        else if (arg.compare(0,3,"-if") == 0)     iv_source_path = argv[++i];
        else if (arg.compare(0,3,"-ec") == 0)     iv_ec = strtoul(argv[++i],NULL,16);  //dg002a
        else if (arg.compare(0,9,"--compare") == 0)
        {
            compare.first = argv[++i];
            compare.second = argv[++i];
        }
        else if (arg.compare(0,7,"--debug") == 0) iv_dbg = true;

    }
    if(iv_source_path.size() == 0) iv_source_path = compare.first;

    if(!narg) // TEST MODE
    {
        iv_source_path = "p7.initfile";
        header_files.insert("p7_init_spies.h");
        header_files.insert("p7_init_arrays.h");
        header_files.insert("ciniIfSymbols.H");
    }

    size_t pos = iv_source_path.rfind('.');
    if(pos != string::npos)
    {
        string type = iv_source_path.substr(pos+1);
        if(type.compare(0,2,"if") == 0) iv_type = IF_TYPE;
        else if(type.compare(0,8,"initfile") == 0) iv_type = INITFILE_TYPE;

        size_t pos1 = iv_source_path.rfind('/',pos);
        if(pos1 == string::npos) pos1 = 0;
        else ++pos1;

        iv_initfile = iv_source_path.substr(pos1,pos-pos1);
    }

    if(iv_outdir.length() == 0) iv_outdir.push_back('.');
    if(iv_outdir.at(iv_outdir.size()-1) != '/') iv_outdir.push_back('/');

    if(iv_outfile.size() == 0) 
    {
        iv_outfile.append(iv_initfile);
        iv_outfile.append(".if");
    }

    iv_outfile.insert(0,iv_outdir);

    stats << "*********************************************************" << endl;
    stats << "* source:  " << iv_source_path << endl;
    stats << "* listing: " << listing_fn() << endl;
    stats << "* binary:  " << binseq_fn() << endl;

    iv_scomlist = new ScomList(iv_source_path, header_files, stats, iv_ec);   //dg002c
    if(compare.second.size())
    {
        ScomList cmplist(compare.second, header_files, stats, iv_ec);  //dg002c
        if(iv_scomlist->compare(cmplist))
        {
            cout << "Compare SUCCESS" << endl;
        }
        else
        {
            cout << stats;
        }
    }

    iv_list_ostream.open(listing_fn().c_str());
    if(!iv_list_ostream)
    {
        throw invalid_argument(string("ERROR! Could not open ") + listing_fn());
    }

}

Parser::~Parser()
{
    iv_list_ostream.close();
}

void Parser::capture_dbg()
{
    if(iv_dbg)
    {
        string fname(iv_outdir);
        fname.append(iv_initfile);
        fname.append(".dbg");
        ofstream dbgfs(fname.c_str());
        if(!dbgfs)
        {
            string msg("Can't open ");
            msg.append(fname);
            throw invalid_argument(msg);
        }
        dbgfs << dbg.str() << endl;
        dbgfs.close();
    }
}

// TODO
//  - Detect all errors down to a line # ?
//  - bad rows/cols check - have already?
//
OpenPOWER on IntegriCloud