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
17package com.android.dx.rop.cst;
18
19/**
20 * Constants which are literal bitwise values of some sort.
21 */
22public abstract class CstLiteralBits
23        extends TypedConstant {
24    /**
25     * Returns whether or not this instance's value may be accurately
26     * represented as an {@code int}. The rule is that if there
27     * is an {@code int} which may be sign-extended to yield this
28     * instance's value, then this method returns {@code true}.
29     * Otherwise, it returns {@code false}.
30     *
31     * @return {@code true} iff this instance fits in an {@code int}
32     */
33    public abstract boolean fitsInInt();
34
35    /**
36     * Gets the value as {@code int} bits. If this instance contains
37     * more bits than fit in an {@code int}, then this returns only
38     * the low-order bits.
39     *
40     * @return the bits
41     */
42    public abstract int getIntBits();
43
44    /**
45     * Gets the value as {@code long} bits. If this instance contains
46     * fewer bits than fit in a {@code long}, then the result of this
47     * method is the sign extension of the value.
48     *
49     * @return the bits
50     */
51    public abstract long getLongBits();
52
53    /**
54     * Returns true if this value can fit in 16 bits with sign-extension.
55     *
56     * @return true if the sign-extended lower 16 bits are the same as
57     * the value.
58     */
59    public boolean fitsIn16Bits() {
60        if (! fitsInInt()) {
61            return false;
62        }
63
64        int bits = getIntBits();
65        return (short) bits == bits;
66    }
67
68    /**
69     * Returns true if this value can fit in 8 bits with sign-extension.
70     *
71     * @return true if the sign-extended lower 8 bits are the same as
72     * the value.
73     */
74    public boolean fitsIn8Bits() {
75        if (! fitsInInt()) {
76            return false;
77        }
78
79        int bits = getIntBits();
80        return (byte) bits == bits;
81    }
82}
83