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.io.Opcodes;
20import com.android.dx.rop.code.RegisterSpecList;
21import com.android.dx.rop.code.SourcePosition;
22import com.android.dx.util.AnnotatedOutput;
23
24/**
25 * Pseudo-instruction which either turns into a {@code nop} or
26 * nothingness, in order to make the subsequent instruction have an
27 * even address. This is used to align (subsequent) instructions that
28 * require it.
29 */
30public final class OddSpacer extends VariableSizeInsn {
31    /**
32     * Constructs an instance. The output address of this instance is initially
33     * unknown ({@code -1}).
34     *
35     * @param position {@code non-null;} source position
36     */
37    public OddSpacer(SourcePosition position) {
38        super(position, RegisterSpecList.EMPTY);
39    }
40
41    /** {@inheritDoc} */
42    @Override
43    public int codeSize() {
44        return (getAddress() & 1);
45    }
46
47    /** {@inheritDoc} */
48    @Override
49    public void writeTo(AnnotatedOutput out) {
50        if (codeSize() != 0) {
51            out.writeShort(InsnFormat.codeUnit(Opcodes.NOP, 0));
52        }
53    }
54
55    /** {@inheritDoc} */
56    @Override
57    public DalvInsn withRegisters(RegisterSpecList registers) {
58        return new OddSpacer(getPosition());
59    }
60
61    /** {@inheritDoc} */
62    @Override
63    protected String argString() {
64        return null;
65    }
66
67    /** {@inheritDoc} */
68    @Override
69    protected String listingString0(boolean noteIndices) {
70        if (codeSize() == 0) {
71            return null;
72        }
73
74        return "nop // spacer";
75    }
76}
77