< prev index next >

src/java.xml/share/classes/com/sun/org/apache/bcel/internal/util/BCELFactory.java

Print this page




   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.util;
  23 
  24 import com.sun.org.apache.bcel.internal.generic.*;
  25 import com.sun.org.apache.bcel.internal.classfile.Utility;
  26 import com.sun.org.apache.bcel.internal.Constants;
  27 import java.io.PrintWriter;
  28 import java.util.*;




  29 































  30 
  31 /**
  32  * Factory creates il.append() statements, and sets instruction targets.
  33  * A helper class for BCELifier.
  34  *
  35  * @see BCELifier
  36  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  37  */
  38 class BCELFactory extends EmptyVisitor {
  39   private MethodGen       _mg;
  40   private PrintWriter     _out;
  41   private ConstantPoolGen _cp;
  42 
  43   BCELFactory(MethodGen mg, PrintWriter out) {






  44     _mg  = mg;
  45     _cp  = mg.getConstantPool();
  46     _out = out;
  47   }
  48 
  49   private HashMap branch_map = new HashMap(); // Map<Instruction, InstructionHandle>
  50 
  51   public void start() {
  52     if(!_mg.isAbstract() && !_mg.isNative()) {
  53       for(InstructionHandle ih = _mg.getInstructionList().getStart();
  54           ih != null; ih = ih.getNext()) {
  55         Instruction i = ih.getInstruction();
  56 
  57         if(i instanceof BranchInstruction) {





  58           branch_map.put(i, ih); // memorize container
  59         }
  60 
  61         if(ih.hasTargeters()) {
  62           if(i instanceof BranchInstruction) {
  63             _out.println("    InstructionHandle ih_" + ih.getPosition() + ";");
  64           } else {
  65             _out.print("    InstructionHandle ih_" + ih.getPosition() + " = ");
  66           }
  67         } else {
  68           _out.print("    ");
  69         }
  70 
  71         if(!visitInstruction(i))
  72           i.accept(this);
  73       }
  74 
  75       updateBranchTargets();
  76       updateExceptionHandlers();
  77     }
  78   }
  79 
  80   private boolean visitInstruction(Instruction i) {
  81     short opcode = i.getOpcode();
  82 
  83     if((InstructionConstants.INSTRUCTIONS[opcode] != null) &&
  84        !(i instanceof ConstantPushInstruction) &&
  85        !(i instanceof ReturnInstruction)) { // Handled below
  86       _out.println("il.append(InstructionConstants." +
  87                    i.getName().toUpperCase() + ");");

  88       return true;
  89     }
  90 
  91     return false;
  92   }
  93 
  94   public void visitLocalVariableInstruction(LocalVariableInstruction i) {
  95     short  opcode = i.getOpcode();
  96     Type   type   = i.getType(_cp);
  97 
  98     if(opcode == Constants.IINC) {
  99       _out.println("il.append(new IINC(" + i.getIndex() + ", " +
 100                    ((IINC)i).getIncrement() + "));");

 101     } else {
 102       String kind   = (opcode < Constants.ISTORE)? "Load" : "Store";
 103       _out.println("il.append(_factory.create" + kind + "(" +
 104                    BCELifier.printType(type) + ", " +
 105                    i.getIndex() + "));");
 106     }
 107   }
 108 
 109   public void visitArrayInstruction(ArrayInstruction i) {
 110     short  opcode = i.getOpcode();
 111     Type   type   = i.getType(_cp);
 112     String kind   = (opcode < Constants.IASTORE)? "Load" : "Store";
 113 
 114     _out.println("il.append(_factory.createArray" + kind + "(" +
 115                  BCELifier.printType(type) + "));");





 116   }
 117 
 118   public void visitFieldInstruction(FieldInstruction i) {
 119     short  opcode = i.getOpcode();
 120 
 121     String class_name = i.getClassName(_cp);
 122     String field_name = i.getFieldName(_cp);
 123     Type   type       = i.getFieldType(_cp);
 124 
 125     _out.println("il.append(_factory.createFieldAccess(\"" +
 126                  class_name + "\", \"" + field_name + "\", " +
 127                  BCELifier.printType(type) + ", " +
 128                  "Constants." + Constants.OPCODE_NAMES[opcode].toUpperCase() +
 129                  "));");




 130   }
 131 
 132   public void visitInvokeInstruction(InvokeInstruction i) {
 133     short  opcode      = i.getOpcode();
 134     String class_name  = i.getClassName(_cp);
 135     String method_name = i.getMethodName(_cp);
 136     Type   type        = i.getReturnType(_cp);
 137     Type[] arg_types   = i.getArgumentTypes(_cp);
 138 
 139     _out.println("il.append(_factory.createInvoke(\"" +
 140                  class_name + "\", \"" + method_name + "\", " +
 141                  BCELifier.printType(type) + ", " +
 142                  BCELifier.printArgumentTypes(arg_types) + ", " +
 143                  "Constants." + Constants.OPCODE_NAMES[opcode].toUpperCase() +
 144                  "));");





 145   }
 146 
 147   public void visitAllocationInstruction(AllocationInstruction i) {
 148     Type type;
 149 
 150     if(i instanceof CPInstruction) {
 151       type = ((CPInstruction)i).getType(_cp);



 152     } else {
 153       type = ((NEWARRAY)i).getType();
 154     }
 155 
 156     short opcode = ((Instruction)i).getOpcode();
 157     int   dim    = 1;
 158 
 159     switch(opcode) {
 160     case Constants.NEW:
 161       _out.println("il.append(_factory.createNew(\"" +
 162                    ((ObjectType)type).getClassName() + "\"));");
 163       break;
 164 
 165     case Constants.MULTIANEWARRAY:
 166       dim = ((MULTIANEWARRAY)i).getDimensions();
 167 
 168     case Constants.ANEWARRAY:
 169     case Constants.NEWARRAY:
 170       _out.println("il.append(_factory.createNewArray(" +
 171                    BCELifier.printType(type) + ", (short) " + dim + "));");


 172       break;
 173 
 174     default:
 175       throw new RuntimeException("Oops: " + opcode);
 176     }
 177   }
 178 
 179   private void createConstant(Object value) {
 180     String embed = value.toString();
 181 
 182     if(value instanceof String)
 183       embed = '"' + Utility.convertString(value.toString()) + '"';
 184     else if(value instanceof Character)
 185       embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());










 186 
 187     _out.println("il.append(new PUSH(_cp, " + embed + "));");
 188   }
 189 
 190   public void visitLDC(LDC i) {


 191     createConstant(i.getValue(_cp));
 192   }
 193 
 194   public void visitLDC2_W(LDC2_W i) {


 195     createConstant(i.getValue(_cp));
 196   }
 197 
 198   public void visitConstantPushInstruction(ConstantPushInstruction i) {


 199     createConstant(i.getValue());
 200   }
 201 
 202   public void visitINSTANCEOF(INSTANCEOF i) {
 203     Type type = i.getType(_cp);
 204 
 205     _out.println("il.append(new INSTANCEOF(_cp.addClass(" +
 206                  BCELifier.printType(type) + ")));");


 207   }
 208 
 209   public void visitCHECKCAST(CHECKCAST i) {
 210     Type type = i.getType(_cp);
 211 
 212     _out.println("il.append(_factory.createCheckCast(" +
 213                  BCELifier.printType(type) + "));");


 214   }
 215 
 216   public void visitReturnInstruction(ReturnInstruction i) {
 217     Type type = i.getType(_cp);
 218 
 219     _out.println("il.append(_factory.createReturn(" +
 220                  BCELifier.printType(type) + "));");


 221   }
 222 
 223   // Memorize BranchInstructions that need an update
 224   private ArrayList branches = new ArrayList();
 225 
 226   public void visitBranchInstruction(BranchInstruction bi) {
 227     BranchHandle bh   = (BranchHandle)branch_map.get(bi);
 228     int          pos  = bh.getPosition();
 229     String       name = bi.getName() + "_" + pos;
 230 
 231     if(bi instanceof Select) {
 232       Select s = (Select)bi;





 233       branches.add(bi);
 234 
 235       StringBuffer args   = new StringBuffer("new int[] { ");
 236       int[]        matchs = s.getMatchs();
 237 
 238       for(int i=0; i < matchs.length; i++) {
 239         args.append(matchs[i]);
 240 
 241         if(i < matchs.length - 1)
 242           args.append(", ");
 243       }
 244 
 245       args.append(" }");
 246 
 247       _out.print("    Select " + name + " = new " +
 248                  bi.getName().toUpperCase() + "(" + args +
 249                  ", new InstructionHandle[] { ");
 250 
 251       for(int i=0; i < matchs.length; i++) {
 252         _out.print("null");
 253 
 254         if(i < matchs.length - 1)
 255           _out.print(", ");
 256       }
 257 
 258       _out.println(");");
 259     } else {
 260       int    t_pos  = bh.getTarget().getPosition();
 261       String target;
 262 
 263       if(pos > t_pos) {
 264         target = "ih_" + t_pos;
 265       } else {
 266         branches.add(bi);
 267         target = "null";
 268       }
 269 
 270       _out.println("    BranchInstruction " + name +
 271                    " = _factory.createBranchInstruction(" +
 272                    "Constants." + bi.getName().toUpperCase() + ", " +
 273                    target + ");");
 274     }
 275 
 276     if(bh.hasTargeters())
 277       _out.println("    ih_" + pos + " = il.append(" + name + ");");
 278     else
 279       _out.println("    il.append(" + name + ");");
 280   }


 281 
 282   public void visitRET(RET i) {

 283     _out.println("il.append(new RET(" + i.getIndex() + ")));");
 284   }
 285 

 286   private void updateBranchTargets() {
 287     for(Iterator i = branches.iterator(); i.hasNext(); ) {
 288       BranchInstruction bi    = (BranchInstruction)i.next();
 289       BranchHandle      bh    = (BranchHandle)branch_map.get(bi);
 290       int               pos   = bh.getPosition();
 291       String            name  = bi.getName() + "_" + pos;
 292       int               t_pos = bh.getTarget().getPosition();
 293 
 294       _out.println("    " + name + ".setTarget(ih_" + t_pos + ");");
 295 
 296       if(bi instanceof Select) {
 297         InstructionHandle[] ihs = ((Select)bi).getTargets();
 298 
 299         for(int j = 0; j < ihs.length; j++) {
 300           t_pos = ihs[j].getPosition();
 301 
 302           _out.println("    " + name + ".setTarget(" + j +
 303                        ", ih_" + t_pos + ");");
 304         }
 305       }
 306     }
 307   }
 308 
 309   private void updateExceptionHandlers() {
 310     CodeExceptionGen[] handlers = _mg.getExceptionHandlers();
 311 
 312     for(int i=0; i < handlers.length; i++) {
 313       CodeExceptionGen h    = handlers[i];
 314       String           type = (h.getCatchType() == null)?
 315         "null" : BCELifier.printType(h.getCatchType());
 316 
 317       _out.println("    method.addExceptionHandler(" +
 318                    "ih_" + h.getStartPC().getPosition() + ", " +
 319                    "ih_" + h.getEndPC().getPosition() + ", " +
 320                    "ih_" + h.getHandlerPC().getPosition() + ", " +
 321                    type + ");");
 322     }
 323   }
 324 }


   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.util;
  23 



  24 import java.io.PrintWriter;
  25 import java.util.ArrayList;
  26 import java.util.HashMap;
  27 import java.util.List;
  28 import java.util.Locale;
  29 import java.util.Map;
  30 
  31 import com.sun.org.apache.bcel.internal.Const;
  32 import com.sun.org.apache.bcel.internal.classfile.Utility;
  33 import com.sun.org.apache.bcel.internal.generic.AllocationInstruction;
  34 import com.sun.org.apache.bcel.internal.generic.ArrayInstruction;
  35 import com.sun.org.apache.bcel.internal.generic.ArrayType;
  36 import com.sun.org.apache.bcel.internal.generic.BranchHandle;
  37 import com.sun.org.apache.bcel.internal.generic.BranchInstruction;
  38 import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
  39 import com.sun.org.apache.bcel.internal.generic.CPInstruction;
  40 import com.sun.org.apache.bcel.internal.generic.CodeExceptionGen;
  41 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  42 import com.sun.org.apache.bcel.internal.generic.ConstantPushInstruction;
  43 import com.sun.org.apache.bcel.internal.generic.EmptyVisitor;
  44 import com.sun.org.apache.bcel.internal.generic.FieldInstruction;
  45 import com.sun.org.apache.bcel.internal.generic.IINC;
  46 import com.sun.org.apache.bcel.internal.generic.INSTANCEOF;
  47 import com.sun.org.apache.bcel.internal.generic.Instruction;
  48 import com.sun.org.apache.bcel.internal.generic.InstructionConst;
  49 import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
  50 import com.sun.org.apache.bcel.internal.generic.InvokeInstruction;
  51 import com.sun.org.apache.bcel.internal.generic.LDC;
  52 import com.sun.org.apache.bcel.internal.generic.LDC2_W;
  53 import com.sun.org.apache.bcel.internal.generic.LocalVariableInstruction;
  54 import com.sun.org.apache.bcel.internal.generic.MULTIANEWARRAY;
  55 import com.sun.org.apache.bcel.internal.generic.MethodGen;
  56 import com.sun.org.apache.bcel.internal.generic.NEWARRAY;
  57 import com.sun.org.apache.bcel.internal.generic.ObjectType;
  58 import com.sun.org.apache.bcel.internal.generic.RET;
  59 import com.sun.org.apache.bcel.internal.generic.ReturnInstruction;
  60 import com.sun.org.apache.bcel.internal.generic.Select;
  61 import com.sun.org.apache.bcel.internal.generic.Type;
  62 
  63 /**
  64  * Factory creates il.append() statements, and sets instruction targets.
  65  * A helper class for BCELifier.
  66  *
  67  * @see BCELifier
  68  * @version $Id: BCELFactory.java 1749603 2016-06-21 20:50:19Z ggregory $
  69  */
  70 class BCELFactory extends EmptyVisitor {



  71 
  72     private static final String CONSTANT_PREFIX = Const.class.getSimpleName()+".";
  73     private final MethodGen _mg;
  74     private final PrintWriter _out;
  75     private final ConstantPoolGen _cp;
  76 
  77 
  78     BCELFactory(final MethodGen mg, final PrintWriter out) {
  79         _mg = mg;
  80         _cp = mg.getConstantPool();
  81         _out = out;
  82     }
  83 
  84     private final Map<Instruction, InstructionHandle> branch_map = new HashMap<>();
  85 





  86 
  87     public void start() {
  88         if (!_mg.isAbstract() && !_mg.isNative()) {
  89             for (InstructionHandle ih = _mg.getInstructionList().getStart(); ih != null; ih = ih
  90                     .getNext()) {
  91                 final Instruction i = ih.getInstruction();
  92                 if (i instanceof BranchInstruction) {
  93                     branch_map.put(i, ih); // memorize container
  94                 }
  95                 if (ih.hasTargeters()) {
  96                     if (i instanceof BranchInstruction) {

  97                         _out.println("    InstructionHandle ih_" + ih.getPosition() + ";");
  98                     } else {
  99                         _out.print("    InstructionHandle ih_" + ih.getPosition() + " = ");
 100                     }
 101                 } else {
 102                     _out.print("    ");
 103                 }
 104                 if (!visitInstruction(i)) {

 105                     i.accept(this);
 106                 }
 107             }
 108             updateBranchTargets();
 109             updateExceptionHandlers();
 110         }
 111     }
 112 


 113 
 114     private boolean visitInstruction( final Instruction i ) {
 115         final short opcode = i.getOpcode();
 116         if ((InstructionConst.getInstruction(opcode) != null)
 117                 && !(i instanceof ConstantPushInstruction) && !(i instanceof ReturnInstruction)) { // Handled below
 118             _out.println("il.append(InstructionConst."
 119                     + i.getName().toUpperCase(Locale.ENGLISH) + ");");
 120             return true;
 121         }

 122         return false;
 123     }
 124 
 125 
 126     @Override
 127     public void visitLocalVariableInstruction( final LocalVariableInstruction i ) {
 128         final short opcode = i.getOpcode();
 129         final Type type = i.getType(_cp);
 130         if (opcode == Const.IINC) {
 131             _out.println("il.append(new IINC(" + i.getIndex() + ", " + ((IINC) i).getIncrement()
 132                     + "));");
 133         } else {
 134             final String kind = (opcode < Const.ISTORE) ? "Load" : "Store";
 135             _out.println("il.append(_factory.create" + kind + "(" + BCELifier.printType(type)
 136                     + ", " + i.getIndex() + "));");

 137         }
 138     }
 139 




 140 
 141     @Override
 142     public void visitArrayInstruction( final ArrayInstruction i ) {
 143         final short opcode = i.getOpcode();
 144         final Type type = i.getType(_cp);
 145         final String kind = (opcode < Const.IASTORE) ? "Load" : "Store";
 146         _out.println("il.append(_factory.createArray" + kind + "(" + BCELifier.printType(type)
 147                 + "));");
 148     }
 149 






 150 
 151     @Override
 152     public void visitFieldInstruction( final FieldInstruction i ) {
 153         final short opcode = i.getOpcode();
 154         final String class_name = i.getReferenceType(_cp).getSignature();
 155         final String field_name = i.getFieldName(_cp);
 156         final Type type = i.getFieldType(_cp);
 157         _out.println("il.append(_factory.createFieldAccess(\"" + class_name + "\", \"" + field_name
 158                 + "\", " + BCELifier.printType(type) + ", " + CONSTANT_PREFIX
 159                 + Const.getOpcodeName(opcode).toUpperCase(Locale.ENGLISH) + "));");
 160     }
 161 






 162 
 163     @Override
 164     public void visitInvokeInstruction( final InvokeInstruction i ) {
 165         final short opcode = i.getOpcode();
 166         final String class_name = i.getReferenceType(_cp).getSignature();
 167         final String method_name = i.getMethodName(_cp);
 168         final Type type = i.getReturnType(_cp);
 169         final Type[] arg_types = i.getArgumentTypes(_cp);
 170         _out.println("il.append(_factory.createInvoke(\"" + class_name + "\", \"" + method_name
 171                 + "\", " + BCELifier.printType(type) + ", "
 172                 + BCELifier.printArgumentTypes(arg_types) + ", " + CONSTANT_PREFIX
 173                 + Const.getOpcodeName(opcode).toUpperCase(Locale.ENGLISH) + "));");
 174     }
 175 


 176 
 177     @Override
 178     public void visitAllocationInstruction( final AllocationInstruction i ) {
 179         Type type;
 180         if (i instanceof CPInstruction) {
 181             type = ((CPInstruction) i).getType(_cp);
 182         } else {
 183             type = ((NEWARRAY) i).getType();
 184         }
 185         final short opcode = ((Instruction) i).getOpcode();

 186         int dim = 1;
 187         switch (opcode) {
 188             case Const.NEW:
 189                 _out.println("il.append(_factory.createNew(\"" + ((ObjectType) type).getClassName()
 190                         + "\"));");

 191                 break;
 192             case Const.MULTIANEWARRAY:
 193                 dim = ((MULTIANEWARRAY) i).getDimensions();
 194                 //$FALL-THROUGH$
 195             case Const.ANEWARRAY:
 196             case Const.NEWARRAY:
 197                 if (type instanceof ArrayType) {
 198                     type = ((ArrayType) type).getBasicType();
 199                 }
 200                 _out.println("il.append(_factory.createNewArray(" + BCELifier.printType(type)
 201                         + ", (short) " + dim + "));");
 202                 break;

 203             default:
 204                 throw new RuntimeException("Oops: " + opcode);
 205         }
 206     }
 207 


 208 
 209     private void createConstant( final Object value ) {
 210         String embed = value.toString();
 211         if (value instanceof String) {
 212             embed = '"' + Utility.convertString(embed) + '"';
 213         } else if (value instanceof Character) {
 214             embed = "(char)0x" + Integer.toHexString(((Character) value).charValue());
 215         } else if (value instanceof Float) {
 216             embed += "f";
 217         } else if (value instanceof Long) {
 218             embed += "L";
 219         } else if (value instanceof ObjectType) {
 220             final ObjectType ot = (ObjectType) value;
 221             embed = "new ObjectType(\""+ot.getClassName()+"\")";
 222         }
 223 
 224         _out.println("il.append(new PUSH(_cp, " + embed + "));");
 225     }
 226 
 227 
 228     @Override
 229     public void visitLDC( final LDC i ) {
 230         createConstant(i.getValue(_cp));
 231     }
 232 
 233 
 234     @Override
 235     public void visitLDC2_W( final LDC2_W i ) {
 236         createConstant(i.getValue(_cp));
 237     }
 238 
 239 
 240     @Override
 241     public void visitConstantPushInstruction( final ConstantPushInstruction i ) {
 242         createConstant(i.getValue());
 243     }
 244 


 245 
 246     @Override
 247     public void visitINSTANCEOF( final INSTANCEOF i ) {
 248         final Type type = i.getType(_cp);
 249         _out.println("il.append(new INSTANCEOF(_cp.addClass(" + BCELifier.printType(type) + ")));");
 250     }
 251 


 252 
 253     @Override
 254     public void visitCHECKCAST( final CHECKCAST i ) {
 255         final Type type = i.getType(_cp);
 256         _out.println("il.append(_factory.createCheckCast(" + BCELifier.printType(type) + "));");
 257     }
 258 


 259 
 260     @Override
 261     public void visitReturnInstruction( final ReturnInstruction i ) {
 262         final Type type = i.getType(_cp);
 263         _out.println("il.append(_factory.createReturn(" + BCELifier.printType(type) + "));");
 264     }
 265 
 266     // Memorize BranchInstructions that need an update
 267     private final List<BranchInstruction> branches = new ArrayList<>();
 268 




 269 
 270     @Override
 271     public void visitBranchInstruction( final BranchInstruction bi ) {
 272         final BranchHandle bh = (BranchHandle) branch_map.get(bi);
 273         final int pos = bh.getPosition();
 274         final String name = bi.getName() + "_" + pos;
 275         if (bi instanceof Select) {
 276             final Select s = (Select) bi;
 277             branches.add(bi);
 278             final StringBuilder args = new StringBuilder("new int[] { ");
 279             final int[] matchs = s.getMatchs();
 280             for (int i = 0; i < matchs.length; i++) {


 281                 args.append(matchs[i]);
 282                 if (i < matchs.length - 1) {

 283                     args.append(", ");
 284                 }
 285             }
 286             args.append(" }");
 287             _out.print("Select " + name + " = new " + bi.getName().toUpperCase(Locale.ENGLISH)
 288                     + "(" + args + ", new InstructionHandle[] { ");
 289             for (int i = 0; i < matchs.length; i++) {



 290                 _out.print("null");
 291                 if (i < matchs.length - 1) {

 292                     _out.print(", ");
 293                 }
 294             }
 295             _out.println(" }, null);");
 296         } else {
 297             final int t_pos = bh.getTarget().getPosition();
 298             String target;
 299             if (pos > t_pos) {

 300                 target = "ih_" + t_pos;
 301             } else {
 302                 branches.add(bi);
 303                 target = "null";
 304             }
 305             _out.println("    BranchInstruction " + name + " = _factory.createBranchInstruction("
 306                     + CONSTANT_PREFIX + bi.getName().toUpperCase(Locale.ENGLISH) + ", " + target
 307                     + ");");


 308         }
 309         if (bh.hasTargeters()) {

 310             _out.println("    ih_" + pos + " = il.append(" + name + ");");
 311         } else {
 312             _out.println("    il.append(" + name + ");");
 313         }
 314     }
 315 
 316 
 317     @Override
 318     public void visitRET( final RET i ) {
 319         _out.println("il.append(new RET(" + i.getIndex() + ")));");
 320     }
 321 
 322 
 323     private void updateBranchTargets() {
 324         for (final BranchInstruction bi : branches) {
 325             final BranchHandle bh = (BranchHandle) branch_map.get(bi);
 326             final int pos = bh.getPosition();
 327             final String name = bi.getName() + "_" + pos;

 328             int t_pos = bh.getTarget().getPosition();

 329             _out.println("    " + name + ".setTarget(ih_" + t_pos + ");");
 330             if (bi instanceof Select) {
 331                 final InstructionHandle[] ihs = ((Select) bi).getTargets();
 332                 for (int j = 0; j < ihs.length; j++) {


 333                     t_pos = ihs[j].getPosition();
 334                     _out.println("    " + name + ".setTarget(" + j + ", ih_" + t_pos + ");");


 335                 }
 336             }
 337         }
 338     }
 339 


 340 
 341     private void updateExceptionHandlers() {
 342         final CodeExceptionGen[] handlers = _mg.getExceptionHandlers();
 343         for (final CodeExceptionGen h : handlers) {
 344             final String type = (h.getCatchType() == null) ? "null" : BCELifier.printType(h
 345                     .getCatchType());
 346             _out.println("    method.addExceptionHandler(" + "ih_" + h.getStartPC().getPosition()
 347                     + ", " + "ih_" + h.getEndPC().getPosition() + ", " + "ih_"
 348                     + h.getHandlerPC().getPosition() + ", " + type + ");");


 349         }
 350     }
 351 }
< prev index next >