|
What this is
Other links
The source code
/***
* ASM: a very small and fast Java bytecode manipulation framework
* Copyright (C) 2000 INRIA, France Telecom
* Copyright (C) 2002 France Telecom
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact: Eric.Bruneton@rd.francetelecom.com
*
* Author: Eric Bruneton
*/
package org.objectweb.asm;
/**
* A {@link CodeVisitor CodeVisitor} that generates Java bytecode instructions.
* Each visit method of this class appends the bytecode corresponding to the
* visited instruction to a byte vector, in the order these methods are called.
*/
public class CodeWriter implements CodeVisitor {
/**
* true if preconditions must be checked at runtime or not.
*/
final static boolean CHECK = false;
/**
* Next code writer (see {@link ClassWriter#firstMethod firstMethod}).
*/
CodeWriter next;
/**
* The class writer to which this method must be added.
*/
private ClassWriter cw;
/**
* The constant pool item that contains the name of this method.
*/
private Item name;
/**
* The constant pool item that contains the descriptor of this method.
*/
private Item desc;
/**
* Access flags of this method.
*/
private int access;
/**
* Maximum stack size of this method.
*/
private int maxStack;
/**
* Maximum number of local variables for this method.
*/
private int maxLocals;
/**
* The bytecode of this method.
*/
private ByteVector code = new ByteVector();
/**
* Number of entries in the catch table of this method.
*/
private int catchCount;
/**
* The catch table of this method.
*/
private ByteVector catchTable;
/**
* Number of exceptions that can be thrown by this method.
*/
private int exceptionCount;
/**
* The exceptions that can be thrown by this method. More
* precisely, this array contains the indexes of the constant pool items
* that contain the internal names of these exception classes.
*/
private int[] exceptions;
/**
* Number of entries in the LocalVariableTable attribute.
*/
private int localVarCount;
/**
* The LocalVariableTable attribute.
*/
private ByteVector localVar;
/**
* Number of entries in the LineNumberTable attribute.
*/
private int lineNumberCount;
/**
* The LineNumberTable attribute.
*/
private ByteVector lineNumber;
/**
* Indicates if some jump instructions are too small and need to be resized.
*/
private boolean resize;
// --------------------------------------------------------------------------
// Fields for the control flow graph analysis algorithm (used to compute the
// maximum stack size). A control flow graph contains one node per "basic
// block", and one edge per "jump" from one basic block to another. Each node
// (i.e., each basic block) is represented by the Label object that
// corresponds to the first instruction of this basic block. Each node also
// stores the list of its successors in the graph, as a linked list of Edge
// objects.
// --------------------------------------------------------------------------
/**
* true if the maximum stack size and number of local variables must
* be automatically computed.
*/
private final boolean computeMaxs;
/**
* The (relative) stack size after the last visited instruction. This size is
* relative to the beginning of the current basic block, i.e., the true stack
* size after the last visited instruction is equal to the {@link
* Label#beginStackSize beginStackSize} of the current basic block plus
* stackSize.
*/
private int stackSize;
/**
* The (relative) maximum stack size after the last visited instruction. This
* size is relative to the beginning of the current basic block, i.e., the
* true maximum stack size after the last visited instruction is equal to the
* {@link Label#beginStackSize beginStackSize} of the current basic block plus
* stackSize.
*/
private int maxStackSize;
/**
* The current basic block. This block is the basic block to which the next
* instruction to be visited must be added.
*/
private Label currentBlock;
/**
* The basic block stack used by the control flow analysis algorithm. This
* stack is represented by a linked list of {@link Label Label} objects,
* linked to each other by their {@link Label#next} field. This stack must
* not be confused with the JVM stack used to execute the JVM instructions!
*/
private Label blockStack;
/**
* The stack size variation corresponding to each JVM instruction. This stack
* variation is equal to the size of the values produced by an instruction,
* minus the size of the values consumed by this instruction.
*/
private final static int[] SIZE;
// --------------------------------------------------------------------------
// Fields to optimize the creation of {@link Edge Edge} objects by using a
// pool of reusable objects. The (shared) pool is a linked list of Edge
// objects, linked to each other by their {@link Edge#poolNext} field. Each
// time a CodeWriter needs to allocate an Edge, it removes the first Edge
// of the pool and adds it to a private list of Edge objects. After the end
// of the control flow analysis algorithm, the Edge objects in the private
// list of the CodeWriter are added back to the pool (by appending this
// private list to the pool list; in order to do this in constant time, both
// head and tail of the private list are stored in this CodeWriter).
// --------------------------------------------------------------------------
/**
* The head of the list of {@link Edge Edge} objects used by this {@link
* CodeWriter CodeWriter}. These objects, linked to each other by their
* {@link Edge#poolNext} field, are added back to the shared pool at the
* end of the control flow analysis algorithm.
*/
private Edge head;
/**
* The tail of the list of {@link Edge Edge} objects used by this {@link
* CodeWriter CodeWriter}. These objects, linked to each other by their
* {@link Edge#poolNext} field, are added back to the shared pool at the
* end of the control flow analysis algorithm.
*/
private Edge tail;
/**
* The shared pool of {@link Edge Edge} objects. This pool is a linked list
* of Edge objects, linked to each other by their {@link Edge#poolNext} field.
*/
private static Edge pool;
// --------------------------------------------------------------------------
// Static initializer
// --------------------------------------------------------------------------
/**
* Computes the stack size variation corresponding to each JVM instruction.
*/
static {
int i;
int[] b = new int[202];
String s =
"EFFFFFFFFGGFFFGGFFFEEFGFGFEEEEEEEEEEEEEEEEEEEEDEDEDDDDDCDCDEEEEEEEEE" +
"EEEEEEEEEEEBABABBBBDCFFFGGGEDCDCDCDCDCDCDCDCDCDCEEEEDDDDDDDCDCDCEFEF" +
"DDEEFFDEDEEEBDDBBDDDDDDCCCCCCCCEFEDDDCDCDEEEEEEEEEEFEEEEEEDDEEDDEE";
for (i = 0; i < b.length; ++i) {
b[i] = s.charAt(i) - 'E';
}
SIZE = b;
/* code to generate the above string
int NA = 0; // not applicable (unused opcode or variable size opcode)
b = new int[] {
0, //NOP, // visitInsn
1, //ACONST_NULL, // -
1, //ICONST_M1, // -
1, //ICONST_0, // -
1, //ICONST_1, // -
1, //ICONST_2, // -
1, //ICONST_3, // -
1, //ICONST_4, // -
1, //ICONST_5, // -
2, //LCONST_0, // -
2, //LCONST_1, // -
1, //FCONST_0, // -
1, //FCONST_1, // -
1, //FCONST_2, // -
2, //DCONST_0, // -
2, //DCONST_1, // -
1, //BIPUSH, // visitIntInsn
1, //SIPUSH, // -
1, //LDC, // visitLdcInsn
NA, //LDC_W, // -
NA, //LDC2_W, // -
1, //ILOAD, // visitVarInsn
2, //LLOAD, // -
1, //FLOAD, // -
2, //DLOAD, // -
1, //ALOAD, // -
NA, //ILOAD_0, // -
NA, //ILOAD_1, // -
NA, //ILOAD_2, // -
NA, //ILOAD_3, // -
NA, //LLOAD_0, // -
NA, //LLOAD_1, // -
NA, //LLOAD_2, // -
NA, //LLOAD_3, // -
NA, //FLOAD_0, // -
NA, //FLOAD_1, // -
NA, //FLOAD_2, // -
NA, //FLOAD_3, // -
NA, //DLOAD_0, // -
NA, //DLOAD_1, // -
NA, //DLOAD_2, // -
NA, //DLOAD_3, // -
NA, //ALOAD_0, // -
NA, //ALOAD_1, // -
NA, //ALOAD_2, // -
NA, //ALOAD_3, // -
-1, //IALOAD, // visitInsn
0, //LALOAD, // -
-1, //FALOAD, // -
0, //DALOAD, // -
-1, //AALOAD, // -
-1, //BALOAD, // -
-1, //CALOAD, // -
-1, //SALOAD, // -
-1, //ISTORE, // visitVarInsn
-2, //LSTORE, // -
-1, //FSTORE, // -
-2, //DSTORE, // -
-1, //ASTORE, // -
NA, //ISTORE_0, // -
NA, //ISTORE_1, // -
NA, //ISTORE_2, // -
NA, //ISTORE_3, // -
NA, //LSTORE_0, // -
NA, //LSTORE_1, // -
NA, //LSTORE_2, // -
NA, //LSTORE_3, // -
NA, //FSTORE_0, // -
NA, //FSTORE_1, // -
NA, //FSTORE_2, // -
NA, //FSTORE_3, // -
NA, //DSTORE_0, // -
NA, //DSTORE_1, // -
NA, //DSTORE_2, // -
NA, //DSTORE_3, // -
NA, //ASTORE_0, // -
NA, //ASTORE_1, // -
NA, //ASTORE_2, // -
NA, //ASTORE_3, // -
-3, //IASTORE, // visitInsn
-4, //LASTORE, // -
-3, //FASTORE, // -
-4, //DASTORE, // -
-3, //AASTORE, // -
-3, //BASTORE, // -
-3, //CASTORE, // -
-3, //SASTORE, // -
-1, //POP, // -
-2, //POP2, // -
1, //DUP, // -
1, //DUP_X1, // -
1, //DUP_X2, // -
2, //DUP2, // -
2, //DUP2_X1, // -
2, //DUP2_X2, // -
0, //SWAP, // -
-1, //IADD, // -
-2, //LADD, // -
-1, //FADD, // -
-2, //DADD, // -
-1, //ISUB, // -
-2, //LSUB, // -
-1, //FSUB, // -
-2, //DSUB, // -
-1, //IMUL, // -
-2, //LMUL, // -
-1, //FMUL, // -
-2, //DMUL, // -
-1, //IDIV, // -
-2, //LDIV, // -
-1, //FDIV, // -
-2, //DDIV, // -
-1, //IREM, // -
-2, //LREM, // -
-1, //FREM, // -
-2, //DREM, // -
0, //INEG, // -
0, //LNEG, // -
0, //FNEG, // -
0, //DNEG, // -
-1, //ISHL, // -
-1, //LSHL, // -
-1, //ISHR, // -
-1, //LSHR, // -
-1, //IUSHR, // -
-1, //LUSHR, // -
-1, //IAND, // -
-2, //LAND, // -
-1, //IOR, // -
-2, //LOR, // -
-1, //IXOR, // -
-2, //LXOR, // -
0, //IINC, // visitIincInsn
1, //I2L, // visitInsn
0, //I2F, // -
1, //I2D, // -
-1, //L2I, // -
-1, //L2F, // -
0, //L2D, // -
0, //F2I, // -
1, //F2L, // -
1, //F2D, // -
-1, //D2I, // -
0, //D2L, // -
-1, //D2F, // -
0, //I2B, // -
0, //I2C, // -
0, //I2S, // -
-3, //LCMP, // -
-1, //FCMPL, // -
-1, //FCMPG, // -
-3, //DCMPL, // -
-3, //DCMPG, // -
-1, //IFEQ, // visitJumpInsn
-1, //IFNE, // -
-1, //IFLT, // -
-1, //IFGE, // -
-1, //IFGT, // -
-1, //IFLE, // -
-2, //IF_ICMPEQ, // -
-2, //IF_ICMPNE, // -
-2, //IF_ICMPLT, // -
-2, //IF_ICMPGE, // -
-2, //IF_ICMPGT, // -
-2, //IF_ICMPLE, // -
-2, //IF_ACMPEQ, // -
-2, //IF_ACMPNE, // -
0, //GOTO, // -
1, //JSR, // -
0, //RET, // visitVarInsn
-1, //TABLESWITCH, // visiTableSwitchInsn
-1, //LOOKUPSWITCH, // visitLookupSwitch
-1, //IRETURN, // visitInsn
-2, //LRETURN, // -
-1, //FRETURN, // -
-2, //DRETURN, // -
-1, //ARETURN, // -
0, //RETURN, // -
NA, //GETSTATIC, // visitFieldInsn
NA, //PUTSTATIC, // -
NA, //GETFIELD, // -
NA, //PUTFIELD, // -
NA, //INVOKEVIRTUAL, // visitMethodInsn
NA, //INVOKESPECIAL, // -
NA, //INVOKESTATIC, // -
NA, //INVOKEINTERFACE, // -
NA, //UNUSED, // NOT VISITED
1, //NEW, // visitTypeInsn
0, //NEWARRAY, // visitIntInsn
0, //ANEWARRAY, // visitTypeInsn
0, //ARRAYLENGTH, // visitInsn
NA, //ATHROW, // -
0, //CHECKCAST, // visitTypeInsn
0, //INSTANCEOF, // -
-1, //MONITORENTER, // visitInsn
-1, //MONITOREXIT, // -
NA, //WIDE, // NOT VISITED
NA, //MULTIANEWARRAY, // visitMultiANewArrayInsn
-1, //IFNULL, // visitJumpInsn
-1, //IFNONNULL, // -
NA, //GOTO_W, // -
NA, //JSR_W, // -
};
for (i = 0; i < b.length; ++i) {
System.err.print((char)('E' + b[i]));
}
System.err.println();
*/
}
// --------------------------------------------------------------------------
// Constructor
// --------------------------------------------------------------------------
/**
* Constructs a CodeWriter.
*
* @param cw the class writer in which the method must be added.
* @param computeMaxs true if the maximum stack size and number of
* local variables must be automatically computed.
*/
protected CodeWriter (final ClassWriter cw, final boolean computeMaxs) {
if (cw.firstMethod == null) {
cw.firstMethod = this;
cw.lastMethod = this;
} else {
cw.lastMethod.next = this;
cw.lastMethod = this;
}
this.cw = cw;
this.computeMaxs = computeMaxs;
if (computeMaxs) {
// pushes the first block onto the stack of blocks to be visited
currentBlock = new Label();
currentBlock.pushed = true;
blockStack = currentBlock;
}
}
/**
* Initializes this CodeWriter to define the bytecode of the specified method.
*
* @param access the method's access flags (see {@link Constants}).
* @param name the method's name.
* @param desc the method's descriptor (see {@link Type Type}).
* @param exceptions the internal names of the method's exceptions. May be
* null.
*/
protected void init (
final int access,
final String name,
final String desc,
final String[] exceptions)
{
this.access = access;
this.name = cw.newUTF8(name);
this.desc = cw.newUTF8(desc);
if (exceptions != null && exceptions.length > 0) {
exceptionCount = exceptions.length;
this.exceptions = new int[exceptionCount];
for (int i = 0; i < exceptionCount; ++i) {
this.exceptions[i] = cw.newClass(exceptions[i]).index;
}
}
if (computeMaxs) {
// updates maxLocals
int size = getArgumentsAndReturnSizes(desc) >> 2;
if ((access & Constants.ACC_STATIC) != 0) {
--size;
}
if (size > maxLocals) {
maxLocals = size;
}
}
}
// --------------------------------------------------------------------------
// Implementation of the CodeVisitor interface
// --------------------------------------------------------------------------
public void visitInsn (final int opcode) {
if (computeMaxs) {
// updates current and max stack sizes
int size = stackSize + SIZE[opcode];
if (size > maxStackSize) {
maxStackSize = size;
}
stackSize = size;
// if opcode == ATHROW or xRETURN, ends current block (no successor)
if ((opcode >= Constants.IRETURN && opcode <= Constants.RETURN) ||
opcode == Constants.ATHROW)
{
if (currentBlock != null) {
currentBlock.maxStackSize = maxStackSize;
currentBlock = null;
}
}
}
// adds the instruction to the bytecode of the method
code.put1(opcode);
}
public void visitIntInsn (final int opcode, final int operand) {
if (computeMaxs && opcode != Constants.NEWARRAY) {
// updates current and max stack sizes only if opcode == NEWARRAY
// (stack size variation = 0 for BIPUSH or SIPUSH)
int size = stackSize + 1;
if (size > maxStackSize) {
maxStackSize = size;
}
stackSize = size;
}
// adds the instruction to the bytecode of the method
if (opcode == Constants.SIPUSH) {
code.put12(opcode, operand);
} else { // BIPUSH or NEWARRAY
code.put11(opcode, operand);
}
}
public void visitVarInsn (final int opcode, final int var) {
if (computeMaxs) {
// updates current and max stack sizes
if (opcode == Constants.RET) {
// no stack change, but end of current block (no successor)
if (currentBlock != null) {
currentBlock.maxStackSize = maxStackSize;
currentBlock = null;
}
} else { // xLOAD or xSTORE
int size = stackSize + SIZE[opcode];
if (size > maxStackSize) {
maxStackSize = size;
}
stackSize = size;
}
// updates max locals
int n;
if (opcode == Constants.LLOAD || opcode == Constants.DLOAD ||
opcode == Constants.LSTORE || opcode == Constants.DSTORE)
{
n = var + 2;
} else {
n = var + 1;
}
if (n > maxLocals) {
maxLocals = n;
}
}
// adds the instruction to the bytecode of the method
if (var < 4 && opcode != Constants.RET) {
int opt;
if (opcode < Constants.ISTORE) {
opt = 26 /*ILOAD_0*/ + ((opcode - Constants.ILOAD) << 2) + var;
} else {
opt = 59 /*ISTORE_0*/ + ((opcode - Constants.ISTORE) << 2) + var;
}
code.put1(opt);
} else if (var >= 256) {
code.put1(196 /*WIDE*/).put12(opcode, var);
} else {
code.put11(opcode, var);
}
}
public void visitTypeInsn (final int opcode, final String desc) {
if (computeMaxs && opcode == Constants.NEW) {
// updates current and max stack sizes only if opcode == NEW
// (stack size variation = 0 for ANEWARRAY, CHECKCAST, INSTANCEOF)
int size = stackSize + 1;
if (size > maxStackSize) {
maxStackSize = size;
}
stackSize = size;
}
// adds the instruction to the bytecode of the method
code.put12(opcode, cw.newClass(desc).index);
}
public void visitFieldInsn (
final int opcode,
final String owner,
final String name,
final String desc)
{
if (computeMaxs) {
int size;
// computes the stack size variation
char c = desc.charAt(0);
switch (opcode) {
case Constants.GETSTATIC:
size = stackSize + (c == 'D' || c == 'J' ? 2 : 1);
break;
case Constants.PUTSTATIC:
size = stackSize + (c == 'D' || c == 'J' ? -2 : -1);
break;
case Constants.GETFIELD:
size = stackSize + (c == 'D' || c == 'J' ? 1 : 0);
break;
//case Constants.PUTFIELD:
default:
size = stackSize + (c == 'D' || c == 'J' ? -3 : -2);
break;
}
// updates current and max stack sizes
if (size > maxStackSize) {
maxStackSize = size;
}
stackSize = size;
}
// adds the instruction to the bytecode of the method
code.put12(opcode, cw.newField(owner, name, desc).index);
}
public void visitMethodInsn (
final int opcode,
final String owner,
final String name,
final String desc)
{
Item i;
if (opcode == Constants.INVOKEINTERFACE) {
i = cw.newItfMethod(owner, name, desc);
} else {
i = cw.newMethod(owner, name, desc);
}
int argSize = i.intVal;
if (computeMaxs) {
// computes the stack size variation. In order not to recompute several
// times this variation for the same Item, we use the intVal field of
// this item to store this variation, once it has been computed. More
// precisely this intVal field stores the sizes of the arguments and of
// the return value corresponding to desc.
if (argSize == 0) {
// the above sizes have not been computed yet, so we compute them...
argSize = getArgumentsAndReturnSizes(desc);
// ... and we save them in order not to recompute them in the future
i.intVal = argSize;
}
int size;
if (opcode == Constants.INVOKESTATIC) {
size = stackSize - (argSize >> 2) + (argSize & 0x03) + 1;
} else {
size = stackSize - (argSize >> 2) + (argSize & 0x03);
}
// updates current and max stack sizes
if (size > maxStackSize) {
maxStackSize = size;
}
stackSize = size;
}
// adds the instruction to the bytecode of the method
if (opcode == Constants.INVOKEINTERFACE) {
if (!computeMaxs) {
if (argSize == 0) {
argSize = getArgumentsAndReturnSizes(desc);
i.intVal = argSize;
}
}
code.put12(Constants.INVOKEINTERFACE, i.index).put11(argSize >> 2, 0);
} else {
code.put12(opcode, i.index);
}
}
public void visitJumpInsn (final int opcode, final Label label) {
if (CHECK) {
if (label.owner == null) {
label.owner = this;
} else if (label.owner != this) {
throw new IllegalArgumentException();
}
}
if (computeMaxs) {
if (opcode == Constants.GOTO) {
// no stack change, but end of current block (with one new successor)
if (currentBlock != null) {
currentBlock.maxStackSize = maxStackSize;
addSuccessor(stackSize, label);
currentBlock = null;
}
} else if (opcode == Constants.JSR) {
if (currentBlock != null) {
addSuccessor(stackSize + 1, label);
}
} else {
// updates current stack size (max stack size unchanged because stack
// size variation always negative in this case)
stackSize += SIZE[opcode];
if (currentBlock != null) {
addSuccessor(stackSize, label);
}
}
}
// adds the instruction to the bytecode of the method
if (label.resolved && label.position - code.length < Short.MIN_VALUE) {
// case of a backward jump with an offset < -32768. In this case we
// automatically replace GOTO with GOTO_W, JSR with JSR_W and IFxxx
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.