1 /*
2  * Copyright (C) 2007 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.rop.code;
18 
19 import com.android.dx.rop.cst.Constant;
20 
21 /**
22  * Instruction which contains an explicit reference to a constant.
23  */
24 public abstract class CstInsn
25         extends Insn {
26     /** {@code non-null;} the constant */
27     private final Constant cst;
28 
29     /**
30      * Constructs an instance.
31      *
32      * @param opcode {@code non-null;} the opcode
33      * @param position {@code non-null;} source position
34      * @param result {@code null-ok;} spec for the result, if any
35      * @param sources {@code non-null;} specs for all the sources
36      * @param cst {@code non-null;} constant
37      */
CstInsn(Rop opcode, SourcePosition position, RegisterSpec result, RegisterSpecList sources, Constant cst)38     public CstInsn(Rop opcode, SourcePosition position, RegisterSpec result,
39                    RegisterSpecList sources, Constant cst) {
40         super(opcode, position, result, sources);
41 
42         if (cst == null) {
43             throw new NullPointerException("cst == null");
44         }
45 
46         this.cst = cst;
47     }
48 
49     /** {@inheritDoc} */
50     @Override
getInlineString()51     public String getInlineString() {
52         return cst.toHuman();
53     }
54 
55     /**
56      * Gets the constant.
57      *
58      * @return {@code non-null;} the constant
59      */
getConstant()60     public Constant getConstant() {
61         return cst;
62     }
63 
64     /** {@inheritDoc} */
65     @Override
contentEquals(Insn b)66     public boolean contentEquals(Insn b) {
67         /*
68          * The cast (CstInsn)b below should always succeed since
69          * Insn.contentEquals compares classes of this and b.
70          */
71         return super.contentEquals(b)
72                 && cst.equals(((CstInsn)b).getConstant());
73     }
74 }
75