summaryrefslogtreecommitdiff
path: root/util/statetrace/printer.cc
blob: b14671f2c1a3ec7798e86b5c42127c79ef12f1c0 (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
/*
 * Copyright (c) 2006 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Gabe Black
 */

#include "tracechild.hh"
#include "printer.hh"

using namespace std;

//Types of printers. If none is found, or there is an error in the input,
//there are psuedo types to return.
enum PrinterType {PRINTER_NONE, PRINTER_ERROR, PRINTER_NESTING, PRINTER_REG};

int findEndOfRegPrinter(string, int);
int findEndOfNestingPrinter(string, int);
PrinterType findSub(string, int &, int &);

//This is pretty easy. Just find the closing parenthesis.
int findEndOfRegPrinter(string config, int startPos)
{
    int pos = config.find(")", startPos);
    if(pos == string::npos)
    {
        cerr << "Couldn't find the closing parenthesis for a reg printer" << endl;
        return 0;
    }
    return pos;
}

//This is a little harder. We need to make sure we don't
//grab an ending parenthesis that belongs to the nesting printer.
int findEndOfNestingPrinter(string config, int startPos)
{
    int length = config.length();
    int pos = startPos;
    int endPos = length;
    int parenPos = config.find(")", pos);
    //If we didn't find an ending parenthesis at all, we're in trouble
    if(parenPos == string::npos)
    {
        cerr << "Couldn't find the closing parenthesis for a nesting printer on the first try" << endl;
        return 0;
    }
    //Keep pulling out embedded stuff until we can't any more
    //we need to make sure we aren't skipping over the parenthesis
    //that ends -this- printer.
    PrinterType type = findSub(config, pos, endPos);
    if(type == PRINTER_ERROR)
        return 0;
    while(type != PRINTER_NONE && endPos >= parenPos)
    {
        //Find the next closest ending parenthesis since we passed
        //up the last one
        parenPos = config.find(")", endPos + 1);
        //If we didn't find one, we're in trouble
        if(parenPos == string::npos)
        {
            cerr << "Couldn't find the closing parenthesis for a nested printer on later tries" << endl;
            return 0;
        }
        //Start looking for the end of this printer and embedded
        //stuff past the one we just found
        pos = endPos + 1;
        //Reset endPos so we search to the end of config
        endPos = length;
        type = findSub(config, pos, endPos);
        if(type == PRINTER_ERROR)
            return 0;
    }
    //We ran out of embedded items, and we didn't pass up our last
    //closing paren. This must be the end of this printer.
    return parenPos;
}

//Find a sub printer. This looks for things which have a type defining
//character and then an opening parenthesis. The type is returned, and
//startPos and endPos are set to the beginning and end of the sub printer
//On entry, the search starts at index startPos and ends at either index
//endPos or a closing parenthesis, whichever comes first
PrinterType findSub(string config, int & startPos, int & endPos)
{
    int length = config.length();
    //Figure out where the different types of sub printers may start
    int regPos = config.find("%(", startPos);
    int nestingPos = config.find("~(", startPos);
    //If a type of printer wasn't found, say it was found too far away.
    //This simplifies things later
    if(regPos == string::npos)
        regPos = endPos;
    if(nestingPos == string::npos)
        nestingPos = endPos;
    //If we find a closing paren, that marks the
    //end of the region we're searching.
    int closingPos = config.find(")", startPos);
    if(closingPos != string::npos &&
            closingPos < regPos &&
            closingPos < nestingPos)
        return PRINTER_NONE;
    //If we didn't find anything close enough, say so.
    if(regPos >= endPos && nestingPos >= endPos)
        return PRINTER_NONE;
    //At this point, we know that one of the options starts legally
    //We need to find which one is first and return that
    if(regPos < nestingPos)
    {
        int regEnd = findEndOfRegPrinter(config, regPos + 2);
        //If we couldn't find the end...
        if(!regEnd)
        {
            cerr << "Couldn't find the end of the reg printer" << endl;
            return PRINTER_ERROR;
        }
        //Report the sub printer's vitals.
        startPos = regPos;
        endPos = regEnd;
        return PRINTER_REG;
    }
    else
    {
        int nestingEnd = findEndOfNestingPrinter(config, nestingPos + 2);
        //If we couldn't find the end...
        if(!nestingEnd)
        {
            cerr << "Couldn't find the end of the nesting printer" << endl;
            return PRINTER_ERROR;
        }
        //Report the sub printer's vitals.
        startPos = nestingPos;
        endPos = nestingEnd;
        return PRINTER_NESTING;
    }
    return PRINTER_NONE;
}

//Set up a nesting printer. This printer can contain sub printers
bool NestingPrinter::configure(string config)
{
    //Clear out any old stuff
    constStrings.clear();
    numPrinters = 0;
    printers.clear();
    int length = config.length();
    int startPos = 0, endPos = length;
    int lastEndPos = -1;
    //Try to find a sub printer
    PrinterType type = findSub(config, startPos, endPos);
    if(type == PRINTER_ERROR)
    {
        cerr << "Problem finding first sub printer" << endl;
        return false;
    }
    while(type != PRINTER_NONE)
    {
        string prefix = config.substr(lastEndPos + 1, startPos - lastEndPos - 1);
        lastEndPos = endPos;
        constStrings.push_back(prefix);
        string subConfig, subString;
        int commaPos, lastCommaPos, childSwitchVar;
        switch(type)
        {
            //If we found a plain register printer
          case PRINTER_REG:
            numPrinters++;
            //Get the register name
            subConfig = config.substr(startPos + 2, endPos - startPos - 2);
            //Set up the register printer
            RegPrinter * regPrinter = new RegPrinter(child);
            if(!regPrinter->configure(subConfig))
            {
                delete regPrinter;
                cerr << "Error configuring reg printer" << endl;
                return false;
            }
            printers.push_back(regPrinter);
            break;
            //If we found an embedded nesting printer
          case PRINTER_NESTING:
            numPrinters++;
            //Punt on reading in all the parameters of the nesting printer
            NestingPrinter * nestingPrinter = new NestingPrinter(child);
            subConfig = config.substr(startPos + 2, endPos - startPos - 2);
            lastCommaPos = string::npos;
            commaPos = subConfig.find(",");
            if(commaPos == string::npos)
                return false;
            childSwitchVar = child->getRegNum(subConfig.substr(0, commaPos));
            if(childSwitchVar == -1)
            {
                cerr << "Couldn't configure switching variable!" << endl;
                return false;
            }
            //Eat up remaining arguments
            while(commaPos != string::npos)
            {
                lastCommaPos = commaPos;
                commaPos = subConfig.find(",", commaPos + 1);
            }
            if(lastCommaPos != string::npos)
            {
                subConfig = subConfig.substr(lastCommaPos + 1, subConfig.length() - lastCommaPos - 1);
            }
            if(!nestingPrinter->configure(subConfig))
            {
                delete nestingPrinter;
                cerr << "Error configuring nesting printer" << endl;
                return false;
            }
            nestingPrinter->switchVar = childSwitchVar;
            printers.push_back(nestingPrinter);
            break;
          default:
            cerr << "Unrecognized printer type" << endl;
            return false;
        }
        //Move down past what we just parsed
        startPos = endPos + 1;
        endPos = length;
        type = findSub(config, startPos, endPos);
        if(type == PRINTER_ERROR)
        {
            cerr << "Unable to find subprinters on later tries" << endl;
            return false;
        }
    }
    //Put in the trailing stuff
    string trailer = config.substr(startPos, length - startPos);
    constStrings.push_back(trailer);
    return true;
}

bool RegPrinter::configure(string config)
{
    //Figure out what our register number is based on the name we're given
    int num = child->getRegNum(config);
    if(num == -1)
    {
        cerr << "Couldn't find register " << config << endl;
        return false;
    }
    regNum(num);
    return true;
}

ostream & NestingPrinter::writeOut(ostream & os)
{
    if(switchVar == -1 || child->diffSinceUpdate(switchVar))
    {
        int x;
        for(x = 0; x < numPrinters; x++)
        {
            os << constStrings[x];
            os << printers[x];
        }
        os << constStrings[x];
    }
    return os;
}

ostream & RegPrinter::writeOut(ostream & os)
{
    os << child->printReg(intRegNum);
    return os;
}