286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/Apackage com.sun.org.apache.bcel.internal.util;
286N/A
286N/Aimport com.sun.org.apache.bcel.internal.generic.*;
286N/Aimport com.sun.org.apache.bcel.internal.classfile.Utility;
286N/Aimport com.sun.org.apache.bcel.internal.Constants;
286N/Aimport java.io.PrintWriter;
286N/Aimport java.util.*;
286N/A
286N/A/* ====================================================================
286N/A * The Apache Software License, Version 1.1
286N/A *
286N/A * Copyright (c) 2002 The Apache Software Foundation. All rights
286N/A * reserved.
286N/A *
286N/A * Redistribution and use in source and binary forms, with or without
286N/A * modification, are permitted provided that the following conditions
286N/A * are met:
286N/A *
286N/A * 1. Redistributions of source code must retain the above copyright
286N/A * notice, this list of conditions and the following disclaimer.
286N/A *
286N/A * 2. Redistributions in binary form must reproduce the above copyright
286N/A * notice, this list of conditions and the following disclaimer in
286N/A * the documentation and/or other materials provided with the
286N/A * distribution.
286N/A *
286N/A * 3. The end-user documentation included with the redistribution,
286N/A * if any, must include the following acknowledgment:
286N/A * "This product includes software developed by the
286N/A * Apache Software Foundation (http://www.apache.org/)."
286N/A * Alternately, this acknowledgment may appear in the software itself,
286N/A * if and wherever such third-party acknowledgments normally appear.
286N/A *
286N/A * 4. The names "Apache" and "Apache Software Foundation" and
286N/A * "Apache BCEL" must not be used to endorse or promote products
286N/A * derived from this software without prior written permission. For
286N/A * written permission, please contact apache@apache.org.
286N/A *
286N/A * 5. Products derived from this software may not be called "Apache",
286N/A * "Apache BCEL", nor may "Apache" appear in their name, without
286N/A * prior written permission of the Apache Software Foundation.
286N/A *
286N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
286N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
286N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
286N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
286N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
286N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
286N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
286N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
286N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
286N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
286N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286N/A * SUCH DAMAGE.
286N/A * ====================================================================
286N/A *
286N/A * This software consists of voluntary contributions made by many
286N/A * individuals on behalf of the Apache Software Foundation. For more
286N/A * information on the Apache Software Foundation, please see
286N/A * <http://www.apache.org/>.
286N/A */
286N/A
286N/A/**
286N/A * Factory creates il.append() statements, and sets instruction targets.
286N/A * A helper class for BCELifier.
286N/A *
286N/A * @see BCELifier
286N/A * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
286N/A */
286N/Aclass BCELFactory extends EmptyVisitor {
286N/A private MethodGen _mg;
286N/A private PrintWriter _out;
286N/A private ConstantPoolGen _cp;
286N/A
286N/A BCELFactory(MethodGen mg, PrintWriter out) {
286N/A _mg = mg;
286N/A _cp = mg.getConstantPool();
286N/A _out = out;
286N/A }
286N/A
286N/A private HashMap branch_map = new HashMap(); // Map<Instruction, InstructionHandle>
286N/A
286N/A public void start() {
286N/A if(!_mg.isAbstract() && !_mg.isNative()) {
286N/A for(InstructionHandle ih = _mg.getInstructionList().getStart();
286N/A ih != null; ih = ih.getNext()) {
286N/A Instruction i = ih.getInstruction();
286N/A
286N/A if(i instanceof BranchInstruction) {
286N/A branch_map.put(i, ih); // memorize container
286N/A }
286N/A
286N/A if(ih.hasTargeters()) {
286N/A if(i instanceof BranchInstruction) {
286N/A _out.println(" InstructionHandle ih_" + ih.getPosition() + ";");
286N/A } else {
286N/A _out.print(" InstructionHandle ih_" + ih.getPosition() + " = ");
286N/A }
286N/A } else {
286N/A _out.print(" ");
286N/A }
286N/A
286N/A if(!visitInstruction(i))
286N/A i.accept(this);
286N/A }
286N/A
286N/A updateBranchTargets();
286N/A updateExceptionHandlers();
286N/A }
286N/A }
286N/A
286N/A private boolean visitInstruction(Instruction i) {
286N/A short opcode = i.getOpcode();
286N/A
286N/A if((InstructionConstants.INSTRUCTIONS[opcode] != null) &&
286N/A !(i instanceof ConstantPushInstruction) &&
286N/A !(i instanceof ReturnInstruction)) { // Handled below
286N/A _out.println("il.append(InstructionConstants." +
286N/A i.getName().toUpperCase() + ");");
286N/A return true;
286N/A }
286N/A
286N/A return false;
286N/A }
286N/A
286N/A public void visitLocalVariableInstruction(LocalVariableInstruction i) {
286N/A short opcode = i.getOpcode();
286N/A Type type = i.getType(_cp);
286N/A
286N/A if(opcode == Constants.IINC) {
286N/A _out.println("il.append(new IINC(" + i.getIndex() + ", " +
286N/A ((IINC)i).getIncrement() + "));");
286N/A } else {
286N/A String kind = (opcode < Constants.ISTORE)? "Load" : "Store";
286N/A _out.println("il.append(_factory.create" + kind + "(" +
286N/A BCELifier.printType(type) + ", " +
286N/A i.getIndex() + "));");
286N/A }
286N/A }
286N/A
286N/A public void visitArrayInstruction(ArrayInstruction i) {
286N/A short opcode = i.getOpcode();
286N/A Type type = i.getType(_cp);
286N/A String kind = (opcode < Constants.IASTORE)? "Load" : "Store";
286N/A
286N/A _out.println("il.append(_factory.createArray" + kind + "(" +
286N/A BCELifier.printType(type) + "));");
286N/A }
286N/A
286N/A public void visitFieldInstruction(FieldInstruction i) {
286N/A short opcode = i.getOpcode();
286N/A
286N/A String class_name = i.getClassName(_cp);
286N/A String field_name = i.getFieldName(_cp);
286N/A Type type = i.getFieldType(_cp);
286N/A
286N/A _out.println("il.append(_factory.createFieldAccess(\"" +
286N/A class_name + "\", \"" + field_name + "\", " +
286N/A BCELifier.printType(type) + ", " +
286N/A "Constants." + Constants.OPCODE_NAMES[opcode].toUpperCase() +
286N/A "));");
286N/A }
286N/A
286N/A public void visitInvokeInstruction(InvokeInstruction i) {
286N/A short opcode = i.getOpcode();
286N/A String class_name = i.getClassName(_cp);
286N/A String method_name = i.getMethodName(_cp);
286N/A Type type = i.getReturnType(_cp);
286N/A Type[] arg_types = i.getArgumentTypes(_cp);
286N/A
286N/A _out.println("il.append(_factory.createInvoke(\"" +
286N/A class_name + "\", \"" + method_name + "\", " +
286N/A BCELifier.printType(type) + ", " +
286N/A BCELifier.printArgumentTypes(arg_types) + ", " +
286N/A "Constants." + Constants.OPCODE_NAMES[opcode].toUpperCase() +
286N/A "));");
286N/A }
286N/A
286N/A public void visitAllocationInstruction(AllocationInstruction i) {
286N/A Type type;
286N/A
286N/A if(i instanceof CPInstruction) {
286N/A type = ((CPInstruction)i).getType(_cp);
286N/A } else {
286N/A type = ((NEWARRAY)i).getType();
286N/A }
286N/A
286N/A short opcode = ((Instruction)i).getOpcode();
286N/A int dim = 1;
286N/A
286N/A switch(opcode) {
286N/A case Constants.NEW:
286N/A _out.println("il.append(_factory.createNew(\"" +
286N/A ((ObjectType)type).getClassName() + "\"));");
286N/A break;
286N/A
286N/A case Constants.MULTIANEWARRAY:
286N/A dim = ((MULTIANEWARRAY)i).getDimensions();
286N/A
286N/A case Constants.ANEWARRAY:
286N/A case Constants.NEWARRAY:
286N/A _out.println("il.append(_factory.createNewArray(" +
286N/A BCELifier.printType(type) + ", (short) " + dim + "));");
286N/A break;
286N/A
286N/A default:
286N/A throw new RuntimeException("Oops: " + opcode);
286N/A }
286N/A }
286N/A
286N/A private void createConstant(Object value) {
286N/A String embed = value.toString();
286N/A
286N/A if(value instanceof String)
286N/A embed = '"' + Utility.convertString(value.toString()) + '"';
286N/A else if(value instanceof Character)
286N/A embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());
286N/A
286N/A _out.println("il.append(new PUSH(_cp, " + embed + "));");
286N/A }
286N/A
286N/A public void visitLDC(LDC i) {
286N/A createConstant(i.getValue(_cp));
286N/A }
286N/A
286N/A public void visitLDC2_W(LDC2_W i) {
286N/A createConstant(i.getValue(_cp));
286N/A }
286N/A
286N/A public void visitConstantPushInstruction(ConstantPushInstruction i) {
286N/A createConstant(i.getValue());
286N/A }
286N/A
286N/A public void visitINSTANCEOF(INSTANCEOF i) {
286N/A Type type = i.getType(_cp);
286N/A
286N/A _out.println("il.append(new INSTANCEOF(_cp.addClass(" +
286N/A BCELifier.printType(type) + ")));");
286N/A }
286N/A
286N/A public void visitCHECKCAST(CHECKCAST i) {
286N/A Type type = i.getType(_cp);
286N/A
286N/A _out.println("il.append(_factory.createCheckCast(" +
286N/A BCELifier.printType(type) + "));");
286N/A }
286N/A
286N/A public void visitReturnInstruction(ReturnInstruction i) {
286N/A Type type = i.getType(_cp);
286N/A
286N/A _out.println("il.append(_factory.createReturn(" +
286N/A BCELifier.printType(type) + "));");
286N/A }
286N/A
286N/A // Memorize BranchInstructions that need an update
286N/A private ArrayList branches = new ArrayList();
286N/A
286N/A public void visitBranchInstruction(BranchInstruction bi) {
286N/A BranchHandle bh = (BranchHandle)branch_map.get(bi);
286N/A int pos = bh.getPosition();
286N/A String name = bi.getName() + "_" + pos;
286N/A
286N/A if(bi instanceof Select) {
286N/A Select s = (Select)bi;
286N/A branches.add(bi);
286N/A
286N/A StringBuffer args = new StringBuffer("new int[] { ");
286N/A int[] matchs = s.getMatchs();
286N/A
286N/A for(int i=0; i < matchs.length; i++) {
286N/A args.append(matchs[i]);
286N/A
286N/A if(i < matchs.length - 1)
286N/A args.append(", ");
286N/A }
286N/A
286N/A args.append(" }");
286N/A
286N/A _out.print(" Select " + name + " = new " +
286N/A bi.getName().toUpperCase() + "(" + args +
286N/A ", new InstructionHandle[] { ");
286N/A
286N/A for(int i=0; i < matchs.length; i++) {
286N/A _out.print("null");
286N/A
286N/A if(i < matchs.length - 1)
286N/A _out.print(", ");
286N/A }
286N/A
286N/A _out.println(");");
286N/A } else {
286N/A int t_pos = bh.getTarget().getPosition();
286N/A String target;
286N/A
286N/A if(pos > t_pos) {
286N/A target = "ih_" + t_pos;
286N/A } else {
286N/A branches.add(bi);
286N/A target = "null";
286N/A }
286N/A
286N/A _out.println(" BranchInstruction " + name +
286N/A " = _factory.createBranchInstruction(" +
286N/A "Constants." + bi.getName().toUpperCase() + ", " +
286N/A target + ");");
286N/A }
286N/A
286N/A if(bh.hasTargeters())
286N/A _out.println(" ih_" + pos + " = il.append(" + name + ");");
286N/A else
286N/A _out.println(" il.append(" + name + ");");
286N/A }
286N/A
286N/A public void visitRET(RET i) {
286N/A _out.println("il.append(new RET(" + i.getIndex() + ")));");
286N/A }
286N/A
286N/A private void updateBranchTargets() {
286N/A for(Iterator i = branches.iterator(); i.hasNext(); ) {
286N/A BranchInstruction bi = (BranchInstruction)i.next();
286N/A BranchHandle bh = (BranchHandle)branch_map.get(bi);
286N/A int pos = bh.getPosition();
286N/A String name = bi.getName() + "_" + pos;
286N/A int t_pos = bh.getTarget().getPosition();
286N/A
286N/A _out.println(" " + name + ".setTarget(ih_" + t_pos + ");");
286N/A
286N/A if(bi instanceof Select) {
286N/A InstructionHandle[] ihs = ((Select)bi).getTargets();
286N/A
286N/A for(int j = 0; j < ihs.length; j++) {
286N/A t_pos = ihs[j].getPosition();
286N/A
286N/A _out.println(" " + name + ".setTarget(" + j +
286N/A ", ih_" + t_pos + ");");
286N/A }
286N/A }
286N/A }
286N/A }
286N/A
286N/A private void updateExceptionHandlers() {
286N/A CodeExceptionGen[] handlers = _mg.getExceptionHandlers();
286N/A
286N/A for(int i=0; i < handlers.length; i++) {
286N/A CodeExceptionGen h = handlers[i];
286N/A String type = (h.getCatchType() == null)?
286N/A "null" : BCELifier.printType(h.getCatchType());
286N/A
286N/A _out.println(" method.addExceptionHandler(" +
286N/A "ih_" + h.getStartPC().getPosition() + ", " +
286N/A "ih_" + h.getEndPC().getPosition() + ", " +
286N/A "ih_" + h.getHandlerPC().getPosition() + ", " +
286N/A type + ");");
286N/A }
286N/A }
286N/A}