BinaryOp.java revision 579d7739c53a2707ad711a2d2cae46d7d782f061
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.dexmaker;
18
19import com.android.dx.rop.code.Rop;
20import com.android.dx.rop.code.Rops;
21import com.android.dx.rop.type.TypeList;
22
23/**
24 * An operation on two values of the same type.
25 *
26 * <p>Math operations ({@link #ADD}, {@link #SUBTRACT}, {@link #MULTIPLY},
27 * {@link #DIVIDE}, and {@link #REMAINDER}) support ints, longs, floats and
28 * doubles.
29 *
30 * <p>Bit operations ({@link #AND}, {@link #OR}, {@link #XOR}, {@link
31 * #SHIFT_LEFT}, {@link #SHIFT_RIGHT}, {@link #UNSIGNED_SHIFT_RIGHT}) support
32 * ints and longs.
33 *
34 * <p>Division by zero behaves differently depending on the operand type.
35 * For int and long operands, {@link #DIVIDE} and {@link #REMAINDER} throw
36 * {@link ArithmeticException} if {@code b == 0}. For float and double operands,
37 * the operations return {@code NaN}.
38 */
39public enum BinaryOp {
40    /** {@code a + b} */
41    ADD() {
42        @Override Rop rop(TypeList types) {
43            return Rops.opAdd(types);
44        }
45    },
46
47    /** {@code a - b} */
48    SUBTRACT() {
49        @Override Rop rop(TypeList types) {
50            return Rops.opSub(types);
51        }
52    },
53
54    /** {@code a * b} */
55    MULTIPLY() {
56        @Override Rop rop(TypeList types) {
57            return Rops.opMul(types);
58        }
59    },
60
61    /** {@code a / b} */
62    DIVIDE() {
63        @Override Rop rop(TypeList types) {
64            return Rops.opDiv(types);
65        }
66    },
67
68    /** {@code a % b} */
69    REMAINDER() {
70        @Override Rop rop(TypeList types) {
71            return Rops.opRem(types);
72        }
73    },
74
75    /** {@code a & b} */
76    AND() {
77        @Override Rop rop(TypeList types) {
78            return Rops.opAnd(types);
79        }
80    },
81
82    /** {@code a | b} */
83    OR() {
84        @Override Rop rop(TypeList types) {
85            return Rops.opOr(types);
86        }
87    },
88
89    /** {@code a ^ b} */
90    XOR() {
91        @Override Rop rop(TypeList types) {
92            return Rops.opXor(types);
93        }
94    },
95
96    /** {@code a << b} */
97    SHIFT_LEFT() {
98        @Override Rop rop(TypeList types) {
99            return Rops.opShl(types);
100        }
101    },
102
103    /** {@code a >> b} */
104    SHIFT_RIGHT() {
105        @Override Rop rop(TypeList types) {
106            return Rops.opShr(types);
107        }
108    },
109
110    /** {@code a >>> b} */
111    UNSIGNED_SHIFT_RIGHT() {
112        @Override Rop rop(TypeList types) {
113            return Rops.opUshr(types);
114        }
115    };
116
117    abstract Rop rop(com.android.dx.rop.type.TypeList types);
118}
119