comparison graal/com.oracle.jvmci.asm.sparc/src/com/oracle/jvmci/asm/sparc/SPARCInstructionCounter.java @ 21708:6df25b1418be

moved com.oracle.asm.** to jvmci-util.jar (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Wed, 03 Jun 2015 18:06:44 +0200
parents graal/com.oracle.graal.asm.sparc/src/com/oracle/graal/asm/sparc/SPARCInstructionCounter.java@422e60a2f4b9
children
comparison
equal deleted inserted replaced
21707:e0f311284930 21708:6df25b1418be
1 /*
2 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.jvmci.asm.sparc;
24
25 import java.util.*;
26
27 import com.oracle.jvmci.asm.Assembler.*;
28
29 public class SPARCInstructionCounter implements InstructionCounter {
30 // Use a treemap to keep the order in the output
31 private static final TreeMap<String, SPARCInstructionMatch> INSTRUCTION_MATCHER = new TreeMap<>();
32 static {
33 // @formatter:off
34 INSTRUCTION_MATCHER.put("nop", new SPARCInstructionMatch(0xFFFF_FFFF, 0x0100_0000));
35 INSTRUCTION_MATCHER.put("st", new OP3LowBitsMatcher(0b11, 0x4, 0x5, 0x6, 0x7, 0xe, 0xf));
36 INSTRUCTION_MATCHER.put("ld", new OP3LowBitsMatcher(0b11, 0x0, 0x1, 0x2, 0x3, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd));
37 INSTRUCTION_MATCHER.put("all", new SPARCInstructionMatch(0x0, 0x0));
38 // @formatter:on
39 }
40 private final SPARCAssembler asm;
41
42 public SPARCInstructionCounter(SPARCAssembler asm) {
43 super();
44 this.asm = asm;
45 }
46
47 @Override
48 public int[] countInstructions(String[] instructionTypes, int beginPc, int endPc) {
49 SPARCInstructionMatch[] matchers = new SPARCInstructionMatch[instructionTypes.length];
50 for (int i = 0; i < instructionTypes.length; i++) {
51 String typeName = instructionTypes[i];
52 matchers[i] = INSTRUCTION_MATCHER.get(typeName);
53 if (matchers[i] == null) {
54 throw new IllegalArgumentException(String.format("Unknown instruction class %s, supported types are: %s", typeName, INSTRUCTION_MATCHER.keySet()));
55 }
56 }
57 return countBetween(matchers, beginPc, endPc);
58 }
59
60 private int[] countBetween(SPARCInstructionMatch[] matchers, int startPc, int endPc) {
61 int[] counts = new int[matchers.length];
62 for (int p = startPc; p < endPc; p += 4) {
63 int instr = asm.getInt(p);
64 for (int i = 0; i < matchers.length; i++) {
65 SPARCInstructionMatch matcher = matchers[i];
66 if (matcher.matches(instr)) {
67 counts[i]++;
68 }
69 }
70 }
71 return counts;
72 }
73
74 @Override
75 public String[] getSupportedInstructionTypes() {
76 return INSTRUCTION_MATCHER.keySet().toArray(new String[0]);
77 }
78
79 /**
80 * Tests the lower 3 bits of the op3 field.
81 */
82 private static class OP3LowBitsMatcher extends SPARCInstructionMatch {
83 private final int[] op3b03;
84 private final int op;
85
86 public OP3LowBitsMatcher(int op, int... op3b03) {
87 super(0, 0);
88 this.op = op;
89 this.op3b03 = op3b03;
90 }
91
92 @Override
93 public boolean matches(int instruction) {
94 if (instruction >>> 30 != op) {
95 return false;
96 }
97 int op3lo = (instruction >> 19) & ((1 << 4) - 1);
98 for (int op3Part : op3b03) {
99 if (op3Part == op3lo) {
100 return true;
101 }
102 }
103 return false;
104 }
105 }
106
107 private static class SPARCInstructionMatch {
108 private final int mask;
109 private final int[] patterns;
110
111 public SPARCInstructionMatch(int mask, int... patterns) {
112 super();
113 this.mask = mask;
114 this.patterns = patterns;
115 }
116
117 public boolean matches(int instruction) {
118 for (int pattern : patterns) {
119 if ((instruction & mask) == pattern) {
120 return true;
121 }
122 }
123 return false;
124 }
125 }
126 }