1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 package com.sun.org.apache.bcel.internal.generic; 6 7 /* ==================================================================== 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 2001 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, 26 * if any, must include the following acknowledgment: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowledgment may appear in the software itself, 30 * if and wherever such third-party acknowledgments normally appear. 31 * 32 * 4. The names "Apache" and "Apache Software Foundation" and 33 * "Apache BCEL" must not be used to endorse or promote products 34 * derived from this software without prior written permission. For 35 * written permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache", 38 * "Apache BCEL", nor may "Apache" appear in their name, without 39 * prior written permission of the Apache Software Foundation. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 */ 60 import com.sun.org.apache.bcel.internal.Constants; 61 import java.io.*; 62 63 /** 64 * Wrapper class for push operations, which are implemented either as BIPUSH, 65 * LDC or xCONST_n instructions. 66 * 67 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 68 */ 69 public final class PUSH 70 implements CompoundInstruction, VariableLengthInstruction, InstructionConstants 71 { 72 private Instruction instruction; 73 74 /** 75 * This constructor also applies for values of type short, char, byte 76 * 77 * @param cp Constant pool 78 * @param value to be pushed 79 */ PUSH(ConstantPoolGen cp, int value)80 public PUSH(ConstantPoolGen cp, int value) { 81 if((value >= -1) && (value <= 5)) // Use ICONST_n 82 instruction = INSTRUCTIONS[Constants.ICONST_0 + value]; 83 else if((value >= -128) && (value <= 127)) // Use BIPUSH 84 instruction = new BIPUSH((byte)value); 85 else if((value >= -32768) && (value <= 32767)) // Use SIPUSH 86 instruction = new SIPUSH((short)value); 87 else // If everything fails create a Constant pool entry 88 instruction = new LDC(cp.addInteger(value)); 89 } 90 91 /** 92 * @param cp Constant pool 93 * @param value to be pushed 94 */ PUSH(ConstantPoolGen cp, boolean value)95 public PUSH(ConstantPoolGen cp, boolean value) { 96 instruction = INSTRUCTIONS[Constants.ICONST_0 + (value? 1 : 0)]; 97 } 98 99 /** 100 * @param cp Constant pool 101 * @param value to be pushed 102 */ PUSH(ConstantPoolGen cp, float value)103 public PUSH(ConstantPoolGen cp, float value) { 104 if(value == 0.0) 105 instruction = FCONST_0; 106 else if(value == 1.0) 107 instruction = FCONST_1; 108 else if(value == 2.0) 109 instruction = FCONST_2; 110 else // Create a Constant pool entry 111 instruction = new LDC(cp.addFloat(value)); 112 } 113 114 /** 115 * @param cp Constant pool 116 * @param value to be pushed 117 */ PUSH(ConstantPoolGen cp, long value)118 public PUSH(ConstantPoolGen cp, long value) { 119 if(value == 0) 120 instruction = LCONST_0; 121 else if(value == 1) 122 instruction = LCONST_1; 123 else // Create a Constant pool entry 124 instruction = new LDC2_W(cp.addLong(value)); 125 } 126 127 /** 128 * @param cp Constant pool 129 * @param value to be pushed 130 */ PUSH(ConstantPoolGen cp, double value)131 public PUSH(ConstantPoolGen cp, double value) { 132 if(value == 0.0) 133 instruction = DCONST_0; 134 else if(value == 1.0) 135 instruction = DCONST_1; 136 else // Create a Constant pool entry 137 instruction = new LDC2_W(cp.addDouble(value)); 138 } 139 140 /** 141 * @param cp Constant pool 142 * @param value to be pushed 143 */ PUSH(ConstantPoolGen cp, String value)144 public PUSH(ConstantPoolGen cp, String value) { 145 if(value == null) 146 instruction = ACONST_NULL; 147 else // Create a Constant pool entry 148 instruction = new LDC(cp.addString(value)); 149 } 150 151 /** 152 * @param cp Constant pool 153 * @param value to be pushed 154 */ PUSH(ConstantPoolGen cp, Number value)155 public PUSH(ConstantPoolGen cp, Number value) { 156 if((value instanceof Integer) || (value instanceof Short) || (value instanceof Byte)) 157 instruction = new PUSH(cp, value.intValue()).instruction; 158 else if(value instanceof Double) 159 instruction = new PUSH(cp, value.doubleValue()).instruction; 160 else if(value instanceof Float) 161 instruction = new PUSH(cp, value.floatValue()).instruction; 162 else if(value instanceof Long) 163 instruction = new PUSH(cp, value.longValue()).instruction; 164 else 165 throw new ClassGenException("What's this: " + value); 166 } 167 168 /** 169 * @param cp Constant pool 170 * @param value to be pushed 171 */ PUSH(ConstantPoolGen cp, Character value)172 public PUSH(ConstantPoolGen cp, Character value) { 173 this(cp, (int)value.charValue()); 174 } 175 176 /** 177 * @param cp Constant pool 178 * @param value to be pushed 179 */ PUSH(ConstantPoolGen cp, Boolean value)180 public PUSH(ConstantPoolGen cp, Boolean value) { 181 this(cp, value.booleanValue()); 182 } 183 getInstructionList()184 public final InstructionList getInstructionList() { 185 return new InstructionList(instruction); 186 } 187 getInstruction()188 public final Instruction getInstruction() { 189 return instruction; 190 } 191 192 /** 193 * @return mnemonic for instruction 194 */ toString()195 public String toString() { 196 return instruction.toString() + " (PUSH)"; 197 } 198 } 199