-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analysis.java
442 lines (400 loc) · 18.9 KB
/
Analysis.java
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
import java.util.*;
import java.lang.Math;
import static java.util.stream.Collectors.*;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.misc.Pair;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.tree.xpath.XPath;
import org.antlr.v4.runtime.tree.ParseTreeProperty;
import org.antlr.v4.runtime.tree.TerminalNode.*;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.ParserRuleContext;
// FIXME: You should limit your implementation to this class. You are free to add new auxilliary classes. You do not need to touch the LoopNext.g4 file.
class Analysis extends LoopNestBaseListener {
// Possible types
enum Types {
Byte, Short, Int, Long, Char, Float, Double, Boolean, String
}
// Type of variable declaration
enum VariableType {
Primitive, Array, Literal
}
// Types of caches supported
enum CacheTypes {
DirectMapped, SetAssociative, FullyAssociative,
}
// auxilliary data-structure for converting strings
// to types, ignoring strings because string is not a
// valid type for loop bounds
final Map<String, Types> stringToType = Collections.unmodifiableMap(new HashMap<String, Types>() {
private static final long serialVersionUID = 1L;
{
put("byte", Types.Byte);
put("short", Types.Short);
put("int", Types.Int);
put("long", Types.Long);
put("char", Types.Char);
put("float", Types.Float);
put("double", Types.Double);
put("boolean", Types.Boolean);
}
});
// Map of cache type string to value of CacheTypes
final Map<String, CacheTypes> stringToCacheType = Collections.unmodifiableMap(new HashMap<String, CacheTypes>() {
private static final long serialVersionUID = 1L;
{
put("FullyAssociative", CacheTypes.FullyAssociative);
put("SetAssociative", CacheTypes.SetAssociative);
put("DirectMapped", CacheTypes.DirectMapped);
}
});
ArrayList<HashMap<String, Long>> TestCases;
TestCase currentTestCase;
boolean debugEnable;
public Analysis(boolean IsDebugEnable) {
debugEnable = IsDebugEnable;
}
/******************************** LOCAL VARIABLE CREATION BEGIN ********************************/
@Override
public void enterLocalVariableDeclaration(LoopNestParser.LocalVariableDeclarationContext ctx) {
currentTestCase.currVarDeclaration = true;
currentTestCase.currVarName = ctx.getChild(1).getChild(0).getText();
}
@Override
public void exitUnannArrayType(LoopNestParser.UnannArrayTypeContext ctx) {
if (currentTestCase.currVarDeclaration){
currentTestCase.currVarIsArr = true;
currentTestCase.currVarDims = (ctx.getChild(1).getChildCount())/2;
}
}
@Override
public void exitUnannStringType(LoopNestParser.UnannStringTypeContext ctx) {
if (currentTestCase.currVarDeclaration){
currentTestCase.currVarType = Types.String;
}
}
@Override
public void exitUnannPrimitiveType(LoopNestParser.UnannPrimitiveTypeContext ctx) {
if (currentTestCase.currVarDeclaration){
currentTestCase.currVarType = stringToType.get(ctx.getText());
if (currentTestCase.currVarType == Types.Short || currentTestCase.currVarType == Types.Int ||
currentTestCase.currVarType == Types.Long){
currentTestCase.isImpVar = true;
}
}
}
@Override
public void exitVariableDeclarator(LoopNestParser.VariableDeclaratorContext ctx) {
if (currentTestCase.currVarDeclaration && ! currentTestCase.currVarIsArr){
String VarVal = ctx.getChild(2).getText();
if (currentTestCase.currVarType == Types.String){
VarVal = VarVal.substring(1, VarVal.length()-1);;
}
if (currentTestCase.currVarType == Types.String){
currentTestCase.CacheType = stringToCacheType.get(VarVal);
if (debugEnable){
System.out.println("CacheType is: " + currentTestCase.CacheType);
}
}
else if (currentTestCase.isImpVar){
currentTestCase.currVarVal = Long.parseLong(VarVal);
}
}
}
@Override
public void exitLocalVariableDeclaration(LoopNestParser.LocalVariableDeclarationContext ctx) {
if (currentTestCase.forInit){
currentTestCase.Loops.get(currentTestCase.forLoopDepth).GoverningVar = currentTestCase.currVarName;
currentTestCase.Loops.get(currentTestCase.forLoopDepth).LowerBound = currentTestCase.currVarVal;
}
if (currentTestCase.currVarIsArr){
ArrayInfo currArrInfo = new ArrayInfo(currentTestCase.currVarDims, currentTestCase.currVarType,
currentTestCase.currVarDimSize);
currentTestCase.Arrays.put(currentTestCase.currVarName, currArrInfo);
if (debugEnable){
System.out.println("Imp Var: " + currentTestCase.currVarName + ", Params: " + currArrInfo);
}
}
else if (currentTestCase.isImpVar){
currentTestCase.SymbolTable.put(currentTestCase.currVarName, currentTestCase.currVarVal);
if (debugEnable){
System.out.println("Imp Var: " + currentTestCase.currVarName + ", Val: " + currentTestCase.currVarVal);
}
}
else if (currentTestCase.currVarType != Types.String){
currentTestCase.NonImpVars.put(currentTestCase.currVarName, currentTestCase.currVarType);
if (debugEnable){
System.out.println("Non Imp Var: " + currentTestCase.currVarName + ", Val: " + currentTestCase.currVarType);
}
}
currentTestCase.currVarReset();
}
/******************************** LOCAL VARIABLE CREATION END ********************************/
@Override public void enterBlockStatements(LoopNestParser.BlockStatementsContext ctx) {
currentTestCase.beginCompute = true;
for (int i = 0; i < ctx.getChildCount(); i++){
RuleContext rc = (RuleContext)ctx.getChild(i).getChild(0).getChild(0).getPayload();
if (rc.getRuleIndex() == LoopNestParser.RULE_forStatement){
currentTestCase.beginCompute = false;
break;
}
}
if (currentTestCase.beginCompute){
currentTestCase.cacheSize = (long)Math.pow(2, currentTestCase.SymbolTable.get("cachePower"));
currentTestCase.blockSize = (long)Math.pow(2, currentTestCase.SymbolTable.get("blockPower"));
currentTestCase.numBlocks = currentTestCase.cacheSize/currentTestCase.blockSize;
if (currentTestCase.CacheType == CacheTypes.SetAssociative){
currentTestCase.numWays = currentTestCase.SymbolTable.get("setSize");
currentTestCase.numSets = currentTestCase.cacheSize/(currentTestCase.blockSize*currentTestCase.numWays);
}
else if (currentTestCase.CacheType == CacheTypes.DirectMapped){
currentTestCase.numWays = 1;
currentTestCase.numSets = currentTestCase.numBlocks;
}
else {
currentTestCase.numWays = currentTestCase.numBlocks;
currentTestCase.numSets = 1;
}
if (debugEnable){
System.out.println("Compute enabled, cacheSize: " + currentTestCase.cacheSize + ", numBlocks: " +
currentTestCase.numBlocks + ", numSets: " + currentTestCase.numSets + ", numWays: " + currentTestCase.numWays);
}
}
}
/******************************** FOR STATEMENT BLOCK BEGIN ********************************/
@Override public void enterForStatement(LoopNestParser.ForStatementContext ctx) {
currentTestCase.Loops.add(new ForLoop());
currentTestCase.forLoopDepth++;
}
@Override
public void enterForInit(LoopNestParser.ForInitContext ctx) {
currentTestCase.forInit = true;
}
@Override
public void exitForInit(LoopNestParser.ForInitContext ctx) {
currentTestCase.forInit = false;
}
@Override
public void enterForCondition(LoopNestParser.ForConditionContext ctx) {
currentTestCase.forCond = true;
}
@Override public void exitRelationalExpression(LoopNestParser.RelationalExpressionContext ctx) {
if (currentTestCase.forCond){
String UpperVal = ctx.getChild(2).getText();
if (UpperVal.charAt(0) >= '0' && UpperVal.charAt(0) <= '9'){
currentTestCase.Loops.get(currentTestCase.forLoopDepth).UpperBound = Long.parseLong(UpperVal);
}
else {
currentTestCase.Loops.get(currentTestCase.forLoopDepth).UpperBound = currentTestCase.ResolveSymbolicAssignment(UpperVal);
}
}
}
@Override
public void exitForCondition(LoopNestParser.ForConditionContext ctx) {
currentTestCase.forCond = false;
}
@Override
public void exitSimplifiedAssignment(LoopNestParser.SimplifiedAssignmentContext ctx) {
String stride = ctx.getChild(2).getText();
if (stride.charAt(0) >= '0' && stride.charAt(0) <= '9'){
currentTestCase.Loops.get(currentTestCase.forLoopDepth).Stride = Long.parseLong(stride);
}
else {
currentTestCase.Loops.get(currentTestCase.forLoopDepth).Stride = currentTestCase.ResolveSymbolicAssignment(stride);
}
if (debugEnable){
System.out.println("For Loop: " + currentTestCase.Loops.get(currentTestCase.forLoopDepth));
}
}
@Override public void exitForStatement(LoopNestParser.ForStatementContext ctx) {
if (currentTestCase.beginCompute){
ForLoop currLoop = currentTestCase.Loops.get(currentTestCase.forLoopDepth);
long stride = currLoop.Stride;
long numElAccessed = (currLoop.UpperBound)/stride;
///////////////// AUGMENT MISSES FOR ARRAYS THAT RELY ON THIS LOOP ////////////////
for (int i = 0; i < currLoop.ArrTouched.size(); i++){
ArrayInfo arr = currentTestCase.Arrays.get(currLoop.ArrTouched.get(i));
long byteJump = stride * arr.ElSize;
int accDim = -1;
for (int j = arr.ArrayDims - 1; j >= 0; j--){
if (arr.forLoopVar[j].equals(currLoop.GoverningVar)){
accDim = j;
break;
}
else {
byteJump *= arr.ArrayDim[j];
}
}
if (byteJump < currentTestCase.blockSize && arr.cacheFitsBlocks){
arr.Misses *= ((byteJump * numElAccessed)/currentTestCase.blockSize < 1) ? 1
: (byteJump * numElAccessed)/currentTestCase.blockSize;
}
else {
arr.Misses *= numElAccessed;
}
if (debugEnable){
System.out.println("Computing step for array: " + currLoop.ArrTouched.get(i) + " byteJump: " + byteJump + " elAcc: " +
numElAccessed + " cacheFitsBlocks: " + arr.cacheFitsBlocks + " Misses: " + arr.Misses);
}
if (arr.cacheFitsBlocks){
if (arr.Misses > currentTestCase.numBlocks){
arr.cacheFitsBlocks = false;
}
else if (byteJump/currentTestCase.blockSize >= currentTestCase.numSets){
if (numElAccessed > currentTestCase.numWays){
arr.cacheFitsBlocks = false;
}
}
else if ((numElAccessed * byteJump)/currentTestCase.blockSize > currentTestCase.numBlocks){
arr.cacheFitsBlocks = false;
}
else {
arr.cacheFitsBlocks = true;
}
}
}
///////////////// AUGMENT MISSES FOR ARRAYS THAT DO NOT RELY ON THIS LOOP ////////////////
if (currentTestCase.forLoopDepth + 1 < currentTestCase.Loops.size()){
ForLoop nextLoop = currentTestCase.Loops.get(currentTestCase.forLoopDepth + 1);
for (int k = 0; k < nextLoop.ArrTouched.size(); k++){
if (!currLoop.ArrTouched.contains(nextLoop.ArrTouched.get(k))){
currLoop.ArrTouched.add(nextLoop.ArrTouched.get(k));
if (!currentTestCase.Arrays.get(nextLoop.ArrTouched.get(k)).cacheFitsBlocks){
if (debugEnable){
System.out.println("Extra computing step for array: " + nextLoop.ArrTouched.get(k) +
" elements accessed: " + numElAccessed);
}
currentTestCase.Arrays.get(nextLoop.ArrTouched.get(k)).Misses *= numElAccessed;
}
}
}
}
}
currentTestCase.forLoopDepth--;
}
/******************************** FOR STATEMENT BLOCK END ********************************/
/******************************** ASSIGNMENT STATEMENT BEGIN ********************************/
@Override
public void exitArrayAccess(LoopNestParser.ArrayAccessContext ctx) {
if (currentTestCase.beginCompute){
String ArrName = ctx.getChild(0).getText();
ArrayInfo arrI = currentTestCase.Arrays.get(ArrName);
for (int i = 2; i < 3*arrI.ArrayDims + 2; i += 3){
arrI.forLoopVar[(i-2)/3] = ctx.getChild(i).getText();
for (int j = 0; j < currentTestCase.Loops.size(); j++){
if (currentTestCase.Loops.get(j).GoverningVar.equals(arrI.forLoopVar[(i-2)/3])){
if (!currentTestCase.Loops.get(j).ArrTouched.contains(ArrName)){
currentTestCase.Loops.get(j).ArrTouched.add(ArrName);
}
if (debugEnable){
System.out.println(ArrName + " is indexed by loop var "
+ currentTestCase.Loops.get(j).GoverningVar);
}
}
}
}
}
}
@Override
public void exitArrayAccess_lfno_primary(LoopNestParser.ArrayAccess_lfno_primaryContext ctx) {
if (currentTestCase.beginCompute){
String ArrName = ctx.getChild(0).getText();
ArrayInfo arrI = currentTestCase.Arrays.get(ArrName);
for (int i = 2; i < 3*arrI.ArrayDims + 2; i += 3){
arrI.forLoopVar[(i-2)/3] = ctx.getChild(i).getText();
for (int j = 0; j < currentTestCase.Loops.size(); j++){
if (currentTestCase.Loops.get(j).GoverningVar.equals(arrI.forLoopVar[(i-2)/3])){
if (!currentTestCase.Loops.get(j).ArrTouched.contains(ArrName)){
currentTestCase.Loops.get(j).ArrTouched.add(ArrName);
}
if (debugEnable){
System.out.println(ArrName + " is indexed by loop var " +
currentTestCase.Loops.get(j).GoverningVar);
}
}
}
}
}
}
/******************************** ASSIGNMENT STATEMENT END ********************************/
@Override
public void exitDimExpr(LoopNestParser.DimExprContext ctx) {
if (currentTestCase.currVarDeclaration && currentTestCase.currVarIsArr &&
currentTestCase.currVarArrDim < currentTestCase.currVarDims){
String DimSize = ctx.getChild(1).getText();
if (DimSize.charAt(0) >= '0' && DimSize.charAt(0) <= '9'){
currentTestCase.currVarDimSize[currentTestCase.currVarArrDim] = Long.parseLong(DimSize);
}
else{
currentTestCase.currVarDimSize[currentTestCase.currVarArrDim] = currentTestCase.ResolveSymbolicAssignment(DimSize);
}
currentTestCase.currVarArrDim++;
}
}
/******************************** TEST CASE ACCOUNTKEEPING BEGIN ********************************/
@Override
public void enterTests(LoopNestParser.TestsContext ctx) {
System.out.println("Tests begin");
if (debugEnable)
{
System.out.println("DEBUGGING IS ENABLED");
System.out.println("*************************************************");
}
else{
System.out.println("DEBUGGING IS DISABLED");
}
TestCases = new ArrayList<HashMap<String, Long>>(ctx.getChildCount());
currentTestCase = new TestCase();
}
@Override
public void enterMethodDeclarator(LoopNestParser.MethodDeclaratorContext ctx) {
currentTestCase.TestCaseName = ctx.getChild(0).getText();
}
// End of testcase
@Override
public void exitMethodDeclaration(LoopNestParser.MethodDeclarationContext ctx) {
Iterator it = currentTestCase.Arrays.entrySet().iterator();
while (it.hasNext()){
Map.Entry pair = (Map.Entry)it.next();
ArrayInfo arr = (ArrayInfo)pair.getValue();
currentTestCase.ComputedMisses.put((String)pair.getKey(), arr.Misses);
}
TestCases.add(currentTestCase.ComputedMisses);
if (debugEnable){
System.out.println("*************************************************");
}
currentTestCase.CleanUp();
}
@Override
public void exitTests(LoopNestParser.TestsContext ctx) {
System.out.println("Tests end");
try {
FileOutputStream fos = new FileOutputStream("Results.obj");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// FIXME: Serialize your data to a file
oos.writeObject(TestCases);
oos.close();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
if (debugEnable){
try {
FileInputStream fis = new FileInputStream("Results.obj");
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println("Results.obj: ");
System.out.println((List)ois.readObject());
ois.close();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}
/******************************** TEST CASE ACCOUNTKEEPING END ********************************/
}