diff src/clojure/asm/commons/CodeSizeEvaluator.java @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/asm/commons/CodeSizeEvaluator.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,234 @@
     1.4 +/***
     1.5 + * ASM: a very small and fast Java bytecode manipulation framework
     1.6 + * Copyright (c) 2000-2005 INRIA, France Telecom
     1.7 + * All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions
    1.11 + * are met:
    1.12 + * 1. Redistributions of source code must retain the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer.
    1.14 + * 2. Redistributions in binary form must reproduce the above copyright
    1.15 + *    notice, this list of conditions and the following disclaimer in the
    1.16 + *    documentation and/or other materials provided with the distribution.
    1.17 + * 3. Neither the name of the copyright holders nor the names of its
    1.18 + *    contributors may be used to endorse or promote products derived from
    1.19 + *    this software without specific prior written permission.
    1.20 + *
    1.21 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.22 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.23 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.24 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    1.25 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.26 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.27 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.28 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.29 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.30 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
    1.31 + * THE POSSIBILITY OF SUCH DAMAGE.
    1.32 + */
    1.33 +package clojure.asm.commons;
    1.34 +
    1.35 +import clojure.asm.Label;
    1.36 +import clojure.asm.MethodAdapter;
    1.37 +import clojure.asm.MethodVisitor;
    1.38 +import clojure.asm.Opcodes;
    1.39 +
    1.40 +/**
    1.41 + * A {@link MethodAdapter} that can be used to approximate method size.
    1.42 + *
    1.43 + * @author Eugene Kuleshov
    1.44 + */
    1.45 +public class CodeSizeEvaluator extends MethodAdapter implements Opcodes{
    1.46 +
    1.47 +private int minSize;
    1.48 +
    1.49 +private int maxSize;
    1.50 +
    1.51 +public CodeSizeEvaluator(final MethodVisitor mv){
    1.52 +	super(mv);
    1.53 +}
    1.54 +
    1.55 +public int getMinSize(){
    1.56 +	return this.minSize;
    1.57 +}
    1.58 +
    1.59 +public int getMaxSize(){
    1.60 +	return this.maxSize;
    1.61 +}
    1.62 +
    1.63 +public void visitInsn(final int opcode){
    1.64 +	minSize += 1;
    1.65 +	maxSize += 1;
    1.66 +	if(mv != null)
    1.67 +		{
    1.68 +		mv.visitInsn(opcode);
    1.69 +		}
    1.70 +}
    1.71 +
    1.72 +public void visitIntInsn(final int opcode, final int operand){
    1.73 +	if(opcode == SIPUSH)
    1.74 +		{
    1.75 +		minSize += 3;
    1.76 +		maxSize += 3;
    1.77 +		}
    1.78 +	else
    1.79 +		{
    1.80 +		minSize += 2;
    1.81 +		maxSize += 2;
    1.82 +		}
    1.83 +	if(mv != null)
    1.84 +		{
    1.85 +		mv.visitIntInsn(opcode, operand);
    1.86 +		}
    1.87 +}
    1.88 +
    1.89 +public void visitVarInsn(final int opcode, final int var){
    1.90 +	if(var < 4 && opcode != Opcodes.RET)
    1.91 +		{
    1.92 +		minSize += 1;
    1.93 +		maxSize += 1;
    1.94 +		}
    1.95 +	else if(var >= 256)
    1.96 +		{
    1.97 +		minSize += 4;
    1.98 +		maxSize += 4;
    1.99 +		}
   1.100 +	else
   1.101 +		{
   1.102 +		minSize += 2;
   1.103 +		maxSize += 2;
   1.104 +		}
   1.105 +	if(mv != null)
   1.106 +		{
   1.107 +		mv.visitVarInsn(opcode, var);
   1.108 +		}
   1.109 +}
   1.110 +
   1.111 +public void visitTypeInsn(final int opcode, final String desc){
   1.112 +	minSize += 3;
   1.113 +	maxSize += 3;
   1.114 +	if(mv != null)
   1.115 +		{
   1.116 +		mv.visitTypeInsn(opcode, desc);
   1.117 +		}
   1.118 +}
   1.119 +
   1.120 +public void visitFieldInsn(
   1.121 +		final int opcode,
   1.122 +		final String owner,
   1.123 +		final String name,
   1.124 +		final String desc){
   1.125 +	minSize += 3;
   1.126 +	maxSize += 3;
   1.127 +	if(mv != null)
   1.128 +		{
   1.129 +		mv.visitFieldInsn(opcode, owner, name, desc);
   1.130 +		}
   1.131 +}
   1.132 +
   1.133 +public void visitMethodInsn(
   1.134 +		final int opcode,
   1.135 +		final String owner,
   1.136 +		final String name,
   1.137 +		final String desc){
   1.138 +	if(opcode == INVOKEINTERFACE)
   1.139 +		{
   1.140 +		minSize += 5;
   1.141 +		maxSize += 5;
   1.142 +		}
   1.143 +	else
   1.144 +		{
   1.145 +		minSize += 3;
   1.146 +		maxSize += 3;
   1.147 +		}
   1.148 +	if(mv != null)
   1.149 +		{
   1.150 +		mv.visitMethodInsn(opcode, owner, name, desc);
   1.151 +		}
   1.152 +}
   1.153 +
   1.154 +public void visitJumpInsn(final int opcode, final Label label){
   1.155 +	minSize += 3;
   1.156 +	if(opcode == GOTO || opcode == JSR)
   1.157 +		{
   1.158 +		maxSize += 5;
   1.159 +		}
   1.160 +	else
   1.161 +		{
   1.162 +		maxSize += 8;
   1.163 +		}
   1.164 +	if(mv != null)
   1.165 +		{
   1.166 +		mv.visitJumpInsn(opcode, label);
   1.167 +		}
   1.168 +}
   1.169 +
   1.170 +public void visitLdcInsn(final Object cst){
   1.171 +	if(cst instanceof Long || cst instanceof Double)
   1.172 +		{
   1.173 +		minSize += 3;
   1.174 +		maxSize += 3;
   1.175 +		}
   1.176 +	else
   1.177 +		{
   1.178 +		minSize += 2;
   1.179 +		maxSize += 3;
   1.180 +		}
   1.181 +	if(mv != null)
   1.182 +		{
   1.183 +		mv.visitLdcInsn(cst);
   1.184 +		}
   1.185 +}
   1.186 +
   1.187 +public void visitIincInsn(final int var, final int increment){
   1.188 +	if(var > 255 || increment > 127 || increment < -128)
   1.189 +		{
   1.190 +		minSize += 6;
   1.191 +		maxSize += 6;
   1.192 +		}
   1.193 +	else
   1.194 +		{
   1.195 +		minSize += 3;
   1.196 +		maxSize += 3;
   1.197 +		}
   1.198 +	if(mv != null)
   1.199 +		{
   1.200 +		mv.visitIincInsn(var, increment);
   1.201 +		}
   1.202 +}
   1.203 +
   1.204 +public void visitTableSwitchInsn(
   1.205 +		final int min,
   1.206 +		final int max,
   1.207 +		final Label dflt,
   1.208 +		final Label[] labels){
   1.209 +	minSize += 13 + labels.length * 4;
   1.210 +	maxSize += 16 + labels.length * 4;
   1.211 +	if(mv != null)
   1.212 +		{
   1.213 +		mv.visitTableSwitchInsn(min, max, dflt, labels);
   1.214 +		}
   1.215 +}
   1.216 +
   1.217 +public void visitLookupSwitchInsn(
   1.218 +		final Label dflt,
   1.219 +		final int[] keys,
   1.220 +		final Label[] labels){
   1.221 +	minSize += 9 + keys.length * 8;
   1.222 +	maxSize += 12 + keys.length * 8;
   1.223 +	if(mv != null)
   1.224 +		{
   1.225 +		mv.visitLookupSwitchInsn(dflt, keys, labels);
   1.226 +		}
   1.227 +}
   1.228 +
   1.229 +public void visitMultiANewArrayInsn(final String desc, final int dims){
   1.230 +	minSize += 4;
   1.231 +	maxSize += 4;
   1.232 +	if(mv != null)
   1.233 +		{
   1.234 +		mv.visitMultiANewArrayInsn(desc, dims);
   1.235 +		}
   1.236 +}
   1.237 +}