001/* 002 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.asm.sparc; 024 025import java.util.*; 026 027import com.oracle.graal.asm.Assembler.*; 028 029public class SPARCInstructionCounter implements InstructionCounter { 030 // Use a treemap to keep the order in the output 031 private static final TreeMap<String, SPARCInstructionMatch> INSTRUCTION_MATCHER = new TreeMap<>(); 032 static { 033 // @formatter:off 034 INSTRUCTION_MATCHER.put("nop", new SPARCInstructionMatch(0xFFFF_FFFF, 0x0100_0000)); 035 INSTRUCTION_MATCHER.put("st", new OP3LowBitsMatcher(0b11, 0x4, 0x5, 0x6, 0x7, 0xe, 0xf)); 036 INSTRUCTION_MATCHER.put("ld", new OP3LowBitsMatcher(0b11, 0x0, 0x1, 0x2, 0x3, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd)); 037 INSTRUCTION_MATCHER.put("all", new SPARCInstructionMatch(0x0, 0x0)); 038 // @formatter:on 039 } 040 private final SPARCAssembler asm; 041 042 public SPARCInstructionCounter(SPARCAssembler asm) { 043 super(); 044 this.asm = asm; 045 } 046 047 @Override 048 public int[] countInstructions(String[] instructionTypes, int beginPc, int endPc) { 049 SPARCInstructionMatch[] matchers = new SPARCInstructionMatch[instructionTypes.length]; 050 for (int i = 0; i < instructionTypes.length; i++) { 051 String typeName = instructionTypes[i]; 052 matchers[i] = INSTRUCTION_MATCHER.get(typeName); 053 if (matchers[i] == null) { 054 throw new IllegalArgumentException(String.format("Unknown instruction class %s, supported types are: %s", typeName, INSTRUCTION_MATCHER.keySet())); 055 } 056 } 057 return countBetween(matchers, beginPc, endPc); 058 } 059 060 private int[] countBetween(SPARCInstructionMatch[] matchers, int startPc, int endPc) { 061 int[] counts = new int[matchers.length]; 062 for (int p = startPc; p < endPc; p += 4) { 063 int instr = asm.getInt(p); 064 for (int i = 0; i < matchers.length; i++) { 065 SPARCInstructionMatch matcher = matchers[i]; 066 if (matcher.matches(instr)) { 067 counts[i]++; 068 } 069 } 070 } 071 return counts; 072 } 073 074 @Override 075 public String[] getSupportedInstructionTypes() { 076 return INSTRUCTION_MATCHER.keySet().toArray(new String[0]); 077 } 078 079 /** 080 * Tests the lower 3 bits of the op3 field. 081 */ 082 private static class OP3LowBitsMatcher extends SPARCInstructionMatch { 083 private final int[] op3b03; 084 private final int op; 085 086 public OP3LowBitsMatcher(int op, int... op3b03) { 087 super(0, 0); 088 this.op = op; 089 this.op3b03 = op3b03; 090 } 091 092 @Override 093 public boolean matches(int instruction) { 094 if (instruction >>> 30 != op) { 095 return false; 096 } 097 int op3lo = (instruction >> 19) & ((1 << 4) - 1); 098 for (int op3Part : op3b03) { 099 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}