|
| 1 | +/*** |
| 2 | + * ASM: a very small and fast Java bytecode manipulation framework |
| 3 | + * Copyright (c) 2000-2011 INRIA, France Telecom |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * 1. Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * 3. Neither the name of the copyright holders nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 28 | + * THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package scala.tools.asm.commons; |
| 31 | + |
| 32 | +import scala.tools.asm.Handle; |
| 33 | +import scala.tools.asm.Label; |
| 34 | +import scala.tools.asm.MethodVisitor; |
| 35 | +import scala.tools.asm.Opcodes; |
| 36 | + |
| 37 | +/** |
| 38 | + * A {@link MethodVisitor} that can be used to approximate method size. |
| 39 | + * |
| 40 | + * @author Eugene Kuleshov |
| 41 | + */ |
| 42 | +public class CodeSizeEvaluator extends MethodVisitor implements Opcodes { |
| 43 | + |
| 44 | + private int minSize; |
| 45 | + |
| 46 | + private int maxSize; |
| 47 | + |
| 48 | + public CodeSizeEvaluator(final MethodVisitor mv) { |
| 49 | + this(Opcodes.ASM5, mv); |
| 50 | + } |
| 51 | + |
| 52 | + protected CodeSizeEvaluator(final int api, final MethodVisitor mv) { |
| 53 | + super(api, mv); |
| 54 | + } |
| 55 | + |
| 56 | + public int getMinSize() { |
| 57 | + return this.minSize; |
| 58 | + } |
| 59 | + |
| 60 | + public int getMaxSize() { |
| 61 | + return this.maxSize; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void visitInsn(final int opcode) { |
| 66 | + minSize += 1; |
| 67 | + maxSize += 1; |
| 68 | + if (mv != null) { |
| 69 | + mv.visitInsn(opcode); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void visitIntInsn(final int opcode, final int operand) { |
| 75 | + if (opcode == SIPUSH) { |
| 76 | + minSize += 3; |
| 77 | + maxSize += 3; |
| 78 | + } else { |
| 79 | + minSize += 2; |
| 80 | + maxSize += 2; |
| 81 | + } |
| 82 | + if (mv != null) { |
| 83 | + mv.visitIntInsn(opcode, operand); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void visitVarInsn(final int opcode, final int var) { |
| 89 | + if (var < 4 && opcode != RET) { |
| 90 | + minSize += 1; |
| 91 | + maxSize += 1; |
| 92 | + } else if (var >= 256) { |
| 93 | + minSize += 4; |
| 94 | + maxSize += 4; |
| 95 | + } else { |
| 96 | + minSize += 2; |
| 97 | + maxSize += 2; |
| 98 | + } |
| 99 | + if (mv != null) { |
| 100 | + mv.visitVarInsn(opcode, var); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void visitTypeInsn(final int opcode, final String type) { |
| 106 | + minSize += 3; |
| 107 | + maxSize += 3; |
| 108 | + if (mv != null) { |
| 109 | + mv.visitTypeInsn(opcode, type); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void visitFieldInsn(final int opcode, final String owner, |
| 115 | + final String name, final String desc) { |
| 116 | + minSize += 3; |
| 117 | + maxSize += 3; |
| 118 | + if (mv != null) { |
| 119 | + mv.visitFieldInsn(opcode, owner, name, desc); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Deprecated |
| 124 | + @Override |
| 125 | + public void visitMethodInsn(final int opcode, final String owner, |
| 126 | + final String name, final String desc) { |
| 127 | + if (api >= Opcodes.ASM5) { |
| 128 | + super.visitMethodInsn(opcode, owner, name, desc); |
| 129 | + return; |
| 130 | + } |
| 131 | + doVisitMethodInsn(opcode, owner, name, desc, |
| 132 | + opcode == Opcodes.INVOKEINTERFACE); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void visitMethodInsn(final int opcode, final String owner, |
| 137 | + final String name, final String desc, final boolean itf) { |
| 138 | + if (api < Opcodes.ASM5) { |
| 139 | + super.visitMethodInsn(opcode, owner, name, desc, itf); |
| 140 | + return; |
| 141 | + } |
| 142 | + doVisitMethodInsn(opcode, owner, name, desc, itf); |
| 143 | + } |
| 144 | + |
| 145 | + private void doVisitMethodInsn(int opcode, final String owner, |
| 146 | + final String name, final String desc, final boolean itf) { |
| 147 | + if (opcode == INVOKEINTERFACE) { |
| 148 | + minSize += 5; |
| 149 | + maxSize += 5; |
| 150 | + } else { |
| 151 | + minSize += 3; |
| 152 | + maxSize += 3; |
| 153 | + } |
| 154 | + if (mv != null) { |
| 155 | + mv.visitMethodInsn(opcode, owner, name, desc, itf); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, |
| 161 | + Object... bsmArgs) { |
| 162 | + minSize += 5; |
| 163 | + maxSize += 5; |
| 164 | + if (mv != null) { |
| 165 | + mv.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void visitJumpInsn(final int opcode, final Label label) { |
| 171 | + minSize += 3; |
| 172 | + if (opcode == GOTO || opcode == JSR) { |
| 173 | + maxSize += 5; |
| 174 | + } else { |
| 175 | + maxSize += 8; |
| 176 | + } |
| 177 | + if (mv != null) { |
| 178 | + mv.visitJumpInsn(opcode, label); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public void visitLdcInsn(final Object cst) { |
| 184 | + if (cst instanceof Long || cst instanceof Double) { |
| 185 | + minSize += 3; |
| 186 | + maxSize += 3; |
| 187 | + } else { |
| 188 | + minSize += 2; |
| 189 | + maxSize += 3; |
| 190 | + } |
| 191 | + if (mv != null) { |
| 192 | + mv.visitLdcInsn(cst); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public void visitIincInsn(final int var, final int increment) { |
| 198 | + if (var > 255 || increment > 127 || increment < -128) { |
| 199 | + minSize += 6; |
| 200 | + maxSize += 6; |
| 201 | + } else { |
| 202 | + minSize += 3; |
| 203 | + maxSize += 3; |
| 204 | + } |
| 205 | + if (mv != null) { |
| 206 | + mv.visitIincInsn(var, increment); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public void visitTableSwitchInsn(final int min, final int max, |
| 212 | + final Label dflt, final Label... labels) { |
| 213 | + minSize += 13 + labels.length * 4; |
| 214 | + maxSize += 16 + labels.length * 4; |
| 215 | + if (mv != null) { |
| 216 | + mv.visitTableSwitchInsn(min, max, dflt, labels); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public void visitLookupSwitchInsn(final Label dflt, final int[] keys, |
| 222 | + final Label[] labels) { |
| 223 | + minSize += 9 + keys.length * 8; |
| 224 | + maxSize += 12 + keys.length * 8; |
| 225 | + if (mv != null) { |
| 226 | + mv.visitLookupSwitchInsn(dflt, keys, labels); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public void visitMultiANewArrayInsn(final String desc, final int dims) { |
| 232 | + minSize += 4; |
| 233 | + maxSize += 4; |
| 234 | + if (mv != null) { |
| 235 | + mv.visitMultiANewArrayInsn(desc, dims); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments