Some coding style fixes
[jpcrr.git] / org / jpc / emulator / memory / codeblock / optimised / OpcodeLogger.java
blob54842609fe98f2066a3aadd3e679ff919c11a171
1 /*
2 JPC-RR: A x86 PC Hardware Emulator
3 Release 1
5 Copyright (C) 2007-2009 Isis Innovation Limited
6 Copyright (C) 2009 H. Ilari Liusvaara
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Based on JPC x86 PC Hardware emulator,
22 A project from the Physics Dept, The University of Oxford
24 Details about original JPC can be found at:
26 www-jpc.physics.ox.ac.uk
30 package org.jpc.emulator.memory.codeblock.optimised;
32 import java.lang.reflect.Field;
33 import java.lang.reflect.Modifier;
34 import java.util.Hashtable;
36 /**Logs frequencies of opcodes and periodically prints the results
38 * @author Ian Preston
40 public class OpcodeLogger {
42 int[] opcodeCounts = new int[MicrocodeSet.MICROCODE_LIMIT];
43 int count = 0;
44 int MAX = 5000000;
45 private String name;
47 OpcodeLogger(String name) {
48 this.name = name;
51 public boolean hasImmediate(int opcode)
53 if((opcode == 3) | (opcode == 8) | (opcode == 13)
54 | (opcode == 26) | (opcode == 27) | (opcode == 49)
55 | (opcode == 168) | (opcode == 229) | (opcode == 255))
56 return true;
57 else
58 return false;
61 public void addBlock(int[] microcodes)
63 boolean IM = false;
64 for(int j=0; j < microcodes.length; j++) {
65 if(!IM) {
66 addOpcode(microcodes[j]);
67 if(hasImmediate(microcodes[j]))
68 IM = true;
69 } else
70 IM = false;
74 public void addOpcode(int opcode)
76 opcodeCounts[opcode]++;
77 count++;
78 if(count >= MAX) {
79 printStats();
80 count = 0;
84 public void printStats()
86 System.out.println("*******************************");
87 System.out.println(name);
88 for(int i=0; i < opcodeCounts.length; i++)
89 if(opcodeCounts[i] > 0)
90 System.out.println(reflectedNameCache.get(String.valueOf(i)) + ": " + opcodeCounts[i]);
93 private static Hashtable<String, String> reflectedNameCache = new Hashtable<String, String>();
95 static
97 try {
98 Class<MicrocodeSet> cls = MicrocodeSet.class;
99 Field[] flds = cls.getDeclaredFields();
100 int count = 0;
101 for(int i=0; i<flds.length; i++) {
102 Field f = flds[i];
103 int mods = f.getModifiers();
104 if(!Modifier.isPublic(mods) || !Modifier.isStatic(mods) || !Modifier.isFinal(mods))
105 continue;
106 if(f.getType() != int.class)
107 continue;
109 int value = f.getInt(null);
110 count++;
111 reflectedNameCache.put(String.valueOf(value), f.getName());
113 } catch (Throwable t) {