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