forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trace.h
354 lines (314 loc) · 9.2 KB
/
Trace.h
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
// Copyright (c) 2011, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#ifndef _Trace_h_
#define _Trace_h_
#include "SymbolInfo.h"
#include "Thread.h"
#include "TerminalColours.h"
#include <iostream>
#include <sstream>
#include <string>
#include <memory>
class EventableResource;
class SystemState;
class Node;
inline std::ostream &operator<<(std::ostream &out, const Register::Reg &r) {
return out << getRegisterName(r);
}
class SrcRegister {
Register::Reg reg;
public:
SrcRegister(Register::Reg r) : reg(r) {}
SrcRegister(unsigned r) : reg((Register::Reg)r) {}
Register::Reg getRegister() const { return reg; }
};
class DestRegister {
Register::Reg reg;
public:
DestRegister(Register::Reg r) : reg(r) {}
DestRegister(unsigned r) : reg((Register::Reg)r) {}
Register::Reg getRegister() const { return reg; }
};
class SrcDestRegister {
Register::Reg reg;
public:
SrcDestRegister(Register::Reg r) : reg(r) {}
SrcDestRegister(unsigned r) : reg((Register::Reg)r) {}
Register::Reg getRegister() const { return reg; }
};
class CPRelOffset {
uint32_t offset;
public:
CPRelOffset(uint32_t o) : offset(o) {}
uint32_t getOffset() const { return offset; }
};
class DPRelOffset {
uint32_t offset;
public:
DPRelOffset(uint32_t o) : offset(o) {}
uint32_t getOffset() const { return offset; }
};
class Tracer {
private:
Tracer() :
tracingEnabled(false),
line(std::cout, buf, pendingBuf),
colours(TerminalColours::null) {}
struct LineState {
LineState(std::ostringstream *b) :
thread(0),
out(0),
buf(b),
pending(0) {}
LineState(std::ostream &o, std::ostringstream &b, std::ostringstream &pendingBuf) :
thread(0),
out(&o),
buf(&b),
pending(&pendingBuf) {}
const Thread *thread;
bool hadRegWrite;
size_t numEscapeChars;
std::ostream *out;
std::ostringstream *buf;
std::ostringstream *pending;
};
class PushLineState {
private:
bool needRestore;
LineState line;
public:
PushLineState();
~PushLineState();
bool getRestore() const { return needRestore; }
};
bool tracingEnabled;
std::ostringstream buf;
std::ostringstream pendingBuf;
LineState line;
std::auto_ptr<SymbolInfo> symInfo;
TerminalColours colours;
static Tracer instance;
void escapeCode(const char *s);
void reset() { escapeCode(colours.reset); }
void red() { escapeCode(colours.red); }
void green() { escapeCode(colours.green); }
void printThreadName();
void printCommonStart();
void printCommonStart(const Node &n);
void printCommonStart(const Thread &t);
void printCommonEnd();
void printThreadPC();
void printInstructionStart(const Thread &t);
template <typename T>
void printOperand(const T &op)
{
*line.buf << op;
}
void printOperand(SrcRegister op);
void printOperand(DestRegister op);
void printOperand(SrcDestRegister op);
void printOperand(CPRelOffset op);
void printOperand(DPRelOffset op);
void dumpThreadSummary(const Core &core);
void dumpThreadSummary(const SystemState &system);
public:
void setTracingEnabled(bool enable) { tracingEnabled = enable; }
bool getTracingEnabled() const { return tracingEnabled; }
void setSymbolInfo(std::auto_ptr<SymbolInfo> &si);
SymbolInfo *getSymbolInfo() { return symInfo.get(); }
void setColour(bool enable);
template<typename T0>
void trace(const Thread &t, T0 op0)
{
printInstructionStart(t);
printOperand(op0);
}
template<typename T0, typename T1>
void trace(const Thread &t, T0 op0, T1 op1)
{
printInstructionStart(t);
printOperand(op0);
printOperand(op1);
}
template<typename T0, typename T1, typename T2>
void trace(const Thread &t, T0 op0, T1 op1, T2 op2)
{
printInstructionStart(t);
printOperand(op0);
printOperand(op1);
printOperand(op2);
}
template<typename T0, typename T1, typename T2, typename T3>
void trace(const Thread &t, T0 op0, T1 op1, T2 op2,
T3 op3)
{
printInstructionStart(t);
printOperand(op0);
printOperand(op1);
printOperand(op2);
printOperand(op3);
}
template<typename T0, typename T1, typename T2, typename T3, typename T4>
void trace(const Thread &t, T0 op0, T1 op1, T2 op2, T3 op3, T4 op4)
{
printInstructionStart(t);
printOperand(op0);
printOperand(op1);
printOperand(op2);
printOperand(op3);
printOperand(op4);
}
template<typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5>
void trace(const Thread &t, T0 op0, T1 op1, T2 op2, T3 op3, T4 op4,
T5 op5)
{
printInstructionStart(t);
printOperand(op0);
printOperand(op1);
printOperand(op2);
printOperand(op3);
printOperand(op4);
printOperand(op5);
}
template<typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6>
void trace(const Thread &t, T0 op0, T1 op1, T2 op2, T3 op3, T4 op4,
T5 op5, T6 op6)
{
printInstructionStart(t);
printOperand(op0);
printOperand(op1);
printOperand(op2);
printOperand(op3);
printOperand(op4);
printOperand(op5);
printOperand(op6);
}
template<typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7>
void trace(const Thread &t, T0 op0, T1 op1, T2 op2, T3 op3, T4 op4,
T5 op5, T6 op6, T7 op7)
{
printInstructionStart(t);
printOperand(op0);
printOperand(op1);
printOperand(op2);
printOperand(op3);
printOperand(op4);
printOperand(op5);
printOperand(op6);
printOperand(op7);
}
template<typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7, typename T8>
void trace(const Thread &t, T0 op0, T1 op1, T2 op2, T3 op3, T4 op4,
T5 op5, T6 op6, T7 op7, T8 op8)
{
printInstructionStart(t);
printOperand(op0);
printOperand(op1);
printOperand(op2);
printOperand(op3);
printOperand(op4);
printOperand(op5);
printOperand(op6);
printOperand(op7);
printOperand(op8);
}
template<typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7, typename T8, typename T9>
void trace(const Thread &t, T0 op0, T1 op1, T2 op2, T3 op3, T4 op4,
T5 op5, T6 op6, T7 op7, T8 op8, T9 op9)
{
printInstructionStart(t);
printOperand(op0);
printOperand(op1);
printOperand(op2);
printOperand(op3);
printOperand(op4);
printOperand(op5);
printOperand(op6);
printOperand(op7);
printOperand(op8);
printOperand(op9);
}
template<typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7, typename T8, typename T9, typename T10>
void trace(const Thread &t, T0 op0, T1 op1, T2 op2, T3 op3, T4 op4,
T5 op5, T6 op6, T7 op7, T8 op8, T9 op9, T10 op10)
{
printInstructionStart(t);
printOperand(op0);
printOperand(op1);
printOperand(op2);
printOperand(op3);
printOperand(op4);
printOperand(op5);
printOperand(op6);
printOperand(op7);
printOperand(op8);
printOperand(op9);
printOperand(op10);
}
template<typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7, typename T8, typename T9, typename T10,
typename T11>
void trace(const Thread &t, T0 op0, T1 op1, T2 op2, T3 op3, T4 op4,
T5 op5, T6 op6, T7 op7, T8 op8, T9 op9, T10 op10, T11 op11)
{
printInstructionStart(t);
printOperand(op0);
printOperand(op1);
printOperand(op2);
printOperand(op3);
printOperand(op4);
printOperand(op5);
printOperand(op6);
printOperand(op7);
printOperand(op8);
printOperand(op9);
printOperand(op10);
printOperand(op11);
}
void regWrite(Register::Reg reg, uint32_t value);
void traceEnd() {
printCommonEnd();
}
void SSwitchRead(const Node &node, uint32_t retAddress, uint16_t regNum);
void SSwitchWrite(const Node &node, uint32_t retAddress, uint16_t regNum,
uint32_t value);
void SSwitchNack(const Node &node, uint32_t dest);
void SSwitchAck(const Node &node, uint32_t dest);
void SSwitchAck(const Node &node, uint32_t data, uint32_t dest);
void exception(const Thread &t, uint32_t et, uint32_t ed,
uint32_t sed, uint32_t ssr, uint32_t spc);
void event(const Thread &t, const EventableResource &res, uint32_t pc,
uint32_t ev);
void interrupt(const Thread &t, const EventableResource &res, uint32_t pc,
uint32_t ssr, uint32_t spc, uint32_t sed, uint32_t ed);
void syscallBegin(const Thread &t);
void syscall(const Thread &t, const std::string &s) {
syscallBegin(t);
*line.buf << s << "()";
reset();
}
template<typename T0>
void syscall(const Thread &t, const std::string &s,
T0 op0) {
syscallBegin(t);
*line.buf << s << '(' << op0 << ')';
reset();
}
void syscallEnd() {
printCommonEnd();
}
void noRunnableThreads(const SystemState &system);
static Tracer &get()
{
return instance;
}
};
#endif //_Trace_h_