CodeAddress.java revision 40c69d949e67fe2cc2cccf4dd16b2f9fdabea396
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.dex.code;
18
19import com.android.dx.rop.code.RegisterSpecList;
20import com.android.dx.rop.code.SourcePosition;
21
22/**
23 * Pseudo-instruction which is used to track an address within a code
24 * array. Instances are used for such things as branch targets and
25 * exception handler ranges. Its code size is zero, and so instances
26 * do not in general directly wind up in any output (either
27 * human-oriented or binary file).
28 */
29public final class CodeAddress extends ZeroSizeInsn {
30    /** If this address should bind closely to the following real instruction */
31    private final boolean bindsClosely;
32
33    /**
34     * Constructs an instance. The output address of this instance is initially
35     * unknown ({@code -1}).
36     *
37     * @param position {@code non-null;} source position
38     */
39    public CodeAddress(SourcePosition position) {
40        this(position, false);
41    }
42
43    /**
44     * Constructs an instance. The output address of this instance is initially
45     * unknown ({@code -1}).
46     *
47     * @param position {@code non-null;} source position
48     * @param bindsClosely if the address should bind closely to the following
49     *                     real instruction.
50     */
51    public CodeAddress(SourcePosition position, boolean bindsClosely) {
52        super(position);
53        this.bindsClosely = bindsClosely;
54    }
55
56    /** {@inheritDoc} */
57    @Override
58    public final DalvInsn withRegisters(RegisterSpecList registers) {
59        return new CodeAddress(getPosition());
60    }
61
62    /** {@inheritDoc} */
63    @Override
64    protected String argString() {
65        return null;
66    }
67
68    /** {@inheritDoc} */
69    @Override
70    protected String listingString0(boolean noteIndices) {
71        return "code-address";
72    }
73
74    /**
75     * Gets whether this address binds closely to the following "real"
76     * (non-zero-length) instruction.
77     *
78     * When a prefix is added to an instruction (for example, to move a value
79     * from a high register to a low register), this determines whether this
80     * {@code CodeAddress} will point to the prefix, or to the instruction
81     * itself.
82     *
83     * If bindsClosely is true, the address will point to the instruction
84     * itself, otherwise it will point to the prefix (if any)
85     *
86     * @return true if this address binds closely to the next real instruction
87     */
88    public boolean getBindsClosely() {
89        return bindsClosely;
90    }
91}
92