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 
17 package com.android.dx;
18 
19 import com.android.dx.rop.code.Rop;
20 import com.android.dx.rop.code.Rops;
21 import 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  */
39 public enum BinaryOp {
40     /** {@code a + b} */
ADD()41     ADD() {
42         @Override
43         Rop rop(TypeList types) {
44             return Rops.opAdd(types);
45         }
46     },
47 
48     /** {@code a - b} */
SUBTRACT()49     SUBTRACT() {
50         @Override
51         Rop rop(TypeList types) {
52             return Rops.opSub(types);
53         }
54     },
55 
56     /** {@code a * b} */
MULTIPLY()57     MULTIPLY() {
58         @Override
59         Rop rop(TypeList types) {
60             return Rops.opMul(types);
61         }
62     },
63 
64     /** {@code a / b} */
DIVIDE()65     DIVIDE() {
66         @Override
67         Rop rop(TypeList types) {
68             return Rops.opDiv(types);
69         }
70     },
71 
72     /** {@code a % b} */
REMAINDER()73     REMAINDER() {
74         @Override
75         Rop rop(TypeList types) {
76             return Rops.opRem(types);
77         }
78     },
79 
80     /** {@code a & b} */
AND()81     AND() {
82         @Override
83         Rop rop(TypeList types) {
84             return Rops.opAnd(types);
85         }
86     },
87 
88     /** {@code a | b} */
OR()89     OR() {
90         @Override
91         Rop rop(TypeList types) {
92             return Rops.opOr(types);
93         }
94     },
95 
96     /** {@code a ^ b} */
XOR()97     XOR() {
98         @Override
99         Rop rop(TypeList types) {
100             return Rops.opXor(types);
101         }
102     },
103 
104     /** {@code a << b} */
SHIFT_LEFT()105     SHIFT_LEFT() {
106         @Override
107         Rop rop(TypeList types) {
108             return Rops.opShl(types);
109         }
110     },
111 
112     /** {@code a >> b} */
SHIFT_RIGHT()113     SHIFT_RIGHT() {
114         @Override
115         Rop rop(TypeList types) {
116             return Rops.opShr(types);
117         }
118     },
119 
120     /** {@code a >>> b} */
UNSIGNED_SHIFT_RIGHT()121     UNSIGNED_SHIFT_RIGHT() {
122         @Override
123         Rop rop(TypeList types) {
124             return Rops.opUshr(types);
125         }
126     };
127 
rop(com.android.dx.rop.type.TypeList types)128     abstract Rop rop(com.android.dx.rop.type.TypeList types);
129 }
130