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 */
23package org.graalvm.compiler.asm.sparc;
24
25import java.util.TreeMap;
26
27import org.graalvm.compiler.asm.Assembler.InstructionCounter;
28
29public 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
33    static {
34        // @formatter:off
35        INSTRUCTION_MATCHER.put("nop", new SPARCInstructionMatch(0xFFFF_FFFF, 0x0100_0000));
36        INSTRUCTION_MATCHER.put("st", new OP3LowBitsMatcher(0b11, 0x4, 0x5, 0x6, 0x7, 0xe, 0xf));
37        INSTRUCTION_MATCHER.put("ld", new OP3LowBitsMatcher(0b11, 0x0, 0x1, 0x2, 0x3, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd));
38        INSTRUCTION_MATCHER.put("all", new SPARCInstructionMatch(0x0, 0x0));
39        // @formatter:on
40    }
41
42    private final SPARCAssembler asm;
43
44    public SPARCInstructionCounter(SPARCAssembler asm) {
45        super();
46        this.asm = asm;
47    }
48
49    @Override
50    public int[] countInstructions(String[] instructionTypes, int beginPc, int endPc) {
51        SPARCInstructionMatch[] matchers = new SPARCInstructionMatch[instructionTypes.length];
52        for (int i = 0; i < instructionTypes.length; i++) {
53            String typeName = instructionTypes[i];
54            matchers[i] = INSTRUCTION_MATCHER.get(typeName);
55            if (matchers[i] == null) {
56                throw new IllegalArgumentException(String.format("Unknown instruction class %s, supported types are: %s", typeName, INSTRUCTION_MATCHER.keySet()));
57            }
58        }
59        return countBetween(matchers, beginPc, endPc);
60    }
61
62    private int[] countBetween(SPARCInstructionMatch[] matchers, int startPc, int endPc) {
63        int[] counts = new int[matchers.length];
64        for (int p = startPc; p < endPc; p += 4) {
65            int instr = asm.getInt(p);
66            for (int i = 0; i < matchers.length; i++) {
67                SPARCInstructionMatch matcher = matchers[i];
68                if (matcher.matches(instr)) {
69                    counts[i]++;
70                }
71            }
72        }
73        return counts;
74    }
75
76    @Override
77    public String[] getSupportedInstructionTypes() {
78        return INSTRUCTION_MATCHER.keySet().toArray(new String[0]);
79    }
80
81    /**
82     * Tests the lower 3 bits of the op3 field.
83     */
84    private static class OP3LowBitsMatcher extends SPARCInstructionMatch {
85        private final int[] op3b03;
86        private final int op;
87
88        OP3LowBitsMatcher(int op, int... op3b03) {
89            super(0, 0);
90            this.op = op;
91            this.op3b03 = op3b03;
92        }
93
94        @Override
95        public boolean matches(int instruction) {
96            if (instruction >>> 30 != op) {
97                return false;
98            }
99            int op3lo = (instruction >> 19) & ((1 << 4) - 1);
100            for (int op3Part : op3b03) {
101                if (op3Part == op3lo) {
102                    return true;
103                }
104            }
105            return false;
106        }
107    }
108
109    private static class SPARCInstructionMatch {
110        private final int mask;
111        private final int[] patterns;
112
113        SPARCInstructionMatch(int mask, int... patterns) {
114            super();
115            this.mask = mask;
116            this.patterns = patterns;
117        }
118
119        public boolean matches(int instruction) {
120            for (int pattern : patterns) {
121                if ((instruction & mask) == pattern) {
122                    return true;
123                }
124            }
125            return false;
126        }
127    }
128}
129