Form20t.java revision 579d7739c53a2707ad711a2d2cae46d7d782f061
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.form;
18
19import com.android.dx.dex.code.DalvInsn;
20import com.android.dx.dex.code.InsnFormat;
21import com.android.dx.dex.code.TargetInsn;
22import com.android.dx.util.AnnotatedOutput;
23
24/**
25 * Instruction format {@code 20t}. See the instruction format spec
26 * for details.
27 */
28public final class Form20t extends InsnFormat {
29    /** {@code non-null;} unique instance of this class */
30    public static final InsnFormat THE_ONE = new Form20t();
31
32    /**
33     * Constructs an instance. This class is not publicly
34     * instantiable. Use {@link #THE_ONE}.
35     */
36    private Form20t() {
37        // This space intentionally left blank.
38    }
39
40    /** {@inheritDoc} */
41    @Override
42    public String insnArgString(DalvInsn insn) {
43        return branchString(insn);
44    }
45
46    /** {@inheritDoc} */
47    @Override
48    public String insnCommentString(DalvInsn insn, boolean noteIndices) {
49        return branchComment(insn);
50    }
51
52    /** {@inheritDoc} */
53    @Override
54    public int codeSize() {
55        return 2;
56    }
57
58    /** {@inheritDoc} */
59    @Override
60    public boolean isCompatible(DalvInsn insn) {
61        if (!((insn instanceof TargetInsn) &&
62              (insn.getRegisters().size() == 0))) {
63            return false;
64        }
65
66        TargetInsn ti = (TargetInsn) insn;
67        return ti.hasTargetOffset() ? branchFits(ti) : true;
68    }
69
70    /** {@inheritDoc} */
71    @Override
72    public boolean branchFits(TargetInsn insn) {
73        int offset = insn.getTargetOffset();
74
75        // Note: A zero offset would fit, but it is prohibited by the spec.
76        return (offset != 0) && signedFitsInShort(offset);
77    }
78
79    /** {@inheritDoc} */
80    @Override
81    public void writeTo(AnnotatedOutput out, DalvInsn insn) {
82        int offset = ((TargetInsn) insn).getTargetOffset();
83
84        write(out, opcodeUnit(insn, 0), (short) offset);
85    }
86}
87