001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1997-1999 Raja Vallee-Rai
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: /*
021: * Modified by the Sable Research Group and others 1997-1999.
022: * See the 'credits' file distributed with Soot for the complete list of
023: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
024: */
025:
026: package soot.jimple;
027:
028: import soot.*;
029: import soot.util.*;
030:
031: public class IntConstant extends ArithmeticConstant {
032: public final int value;
033:
034: protected IntConstant(int value) {
035: this .value = value;
036: }
037:
038: public static IntConstant v(int value) {
039: return new IntConstant(value);
040: }
041:
042: public boolean equals(Object c) {
043: return c instanceof IntConstant
044: && ((IntConstant) c).value == value;
045: }
046:
047: public int hashCode() {
048: return value;
049: }
050:
051: // PTC 1999/06/28
052: public NumericConstant add(NumericConstant c) {
053: if (!(c instanceof IntConstant))
054: throw new IllegalArgumentException("IntConstant expected");
055: return IntConstant.v(this .value + ((IntConstant) c).value);
056: }
057:
058: public NumericConstant subtract(NumericConstant c) {
059: if (!(c instanceof IntConstant))
060: throw new IllegalArgumentException("IntConstant expected");
061: return IntConstant.v(this .value - ((IntConstant) c).value);
062: }
063:
064: public NumericConstant multiply(NumericConstant c) {
065: if (!(c instanceof IntConstant))
066: throw new IllegalArgumentException("IntConstant expected");
067: return IntConstant.v(this .value * ((IntConstant) c).value);
068: }
069:
070: public NumericConstant divide(NumericConstant c) {
071: if (!(c instanceof IntConstant))
072: throw new IllegalArgumentException("IntConstant expected");
073: return IntConstant.v(this .value / ((IntConstant) c).value);
074: }
075:
076: public NumericConstant remainder(NumericConstant c) {
077: if (!(c instanceof IntConstant))
078: throw new IllegalArgumentException("IntConstant expected");
079: return IntConstant.v(this .value % ((IntConstant) c).value);
080: }
081:
082: public NumericConstant equalEqual(NumericConstant c) {
083: if (!(c instanceof IntConstant))
084: throw new IllegalArgumentException("IntConstant expected");
085: return IntConstant
086: .v((this .value == ((IntConstant) c).value) ? 1 : 0);
087: }
088:
089: public NumericConstant notEqual(NumericConstant c) {
090: if (!(c instanceof IntConstant))
091: throw new IllegalArgumentException("IntConstant expected");
092: return IntConstant
093: .v((this .value != ((IntConstant) c).value) ? 1 : 0);
094: }
095:
096: public NumericConstant lessThan(NumericConstant c) {
097: if (!(c instanceof IntConstant))
098: throw new IllegalArgumentException("IntConstant expected");
099: return IntConstant.v((this .value < ((IntConstant) c).value) ? 1
100: : 0);
101: }
102:
103: public NumericConstant lessThanOrEqual(NumericConstant c) {
104: if (!(c instanceof IntConstant))
105: throw new IllegalArgumentException("IntConstant expected");
106: return IntConstant
107: .v((this .value <= ((IntConstant) c).value) ? 1 : 0);
108: }
109:
110: public NumericConstant greaterThan(NumericConstant c) {
111: if (!(c instanceof IntConstant))
112: throw new IllegalArgumentException("IntConstant expected");
113: return IntConstant.v((this .value > ((IntConstant) c).value) ? 1
114: : 0);
115: }
116:
117: public NumericConstant greaterThanOrEqual(NumericConstant c) {
118: if (!(c instanceof IntConstant))
119: throw new IllegalArgumentException("IntConstant expected");
120: return IntConstant
121: .v((this .value >= ((IntConstant) c).value) ? 1 : 0);
122: }
123:
124: public NumericConstant negate() {
125: return IntConstant.v(-(this .value));
126: }
127:
128: public ArithmeticConstant and(ArithmeticConstant c) {
129: if (!(c instanceof IntConstant))
130: throw new IllegalArgumentException("IntConstant expected");
131: return IntConstant.v(this .value & ((IntConstant) c).value);
132: }
133:
134: public ArithmeticConstant or(ArithmeticConstant c) {
135: if (!(c instanceof IntConstant))
136: throw new IllegalArgumentException("IntConstant expected");
137: return IntConstant.v(this .value | ((IntConstant) c).value);
138: }
139:
140: public ArithmeticConstant xor(ArithmeticConstant c) {
141: if (!(c instanceof IntConstant))
142: throw new IllegalArgumentException("IntConstant expected");
143: return IntConstant.v(this .value ^ ((IntConstant) c).value);
144: }
145:
146: public ArithmeticConstant shiftLeft(ArithmeticConstant c) {
147: if (!(c instanceof IntConstant))
148: throw new IllegalArgumentException("IntConstant expected");
149: return IntConstant.v(this .value << ((IntConstant) c).value);
150: }
151:
152: public ArithmeticConstant shiftRight(ArithmeticConstant c) {
153: if (!(c instanceof IntConstant))
154: throw new IllegalArgumentException("IntConstant expected");
155: return IntConstant.v(this .value >> ((IntConstant) c).value);
156: }
157:
158: public ArithmeticConstant unsignedShiftRight(ArithmeticConstant c) {
159: if (!(c instanceof IntConstant))
160: throw new IllegalArgumentException("IntConstant expected");
161: return IntConstant.v(this .value >>> ((IntConstant) c).value);
162: }
163:
164: public String toString() {
165: return new Integer(value).toString();
166: }
167:
168: public Type getType() {
169: return IntType.v();
170: }
171:
172: public void apply(Switch sw) {
173: ((ConstantSwitch) sw).caseIntConstant(this);
174: }
175:
176: }
|