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