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