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