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.SimpleInsn;
22import com.android.dx.rop.code.RegisterSpec;
23import com.android.dx.rop.code.RegisterSpecList;
24import com.android.dx.util.AnnotatedOutput;
25
26import java.util.BitSet;
27
28/**
29 * Instruction format {@code 12x}. See the instruction format spec
30 * for details.
31 */
32public final class Form12x extends InsnFormat {
33    /** {@code non-null;} unique instance of this class */
34    public static final InsnFormat THE_ONE = new Form12x();
35
36    /**
37     * Constructs an instance. This class is not publicly
38     * instantiable. Use {@link #THE_ONE}.
39     */
40    private Form12x() {
41        // This space intentionally left blank.
42    }
43
44    /** {@inheritDoc} */
45    @Override
46    public String insnArgString(DalvInsn insn) {
47        RegisterSpecList regs = insn.getRegisters();
48        int sz = regs.size();
49
50        /*
51         * The (sz - 2) and (sz - 1) below makes this code work for
52         * both the two- and three-register ops. (See "case 3" in
53         * isCompatible(), below.)
54         */
55
56        return regs.get(sz - 2).regString() + ", " +
57            regs.get(sz - 1).regString();
58    }
59
60    /** {@inheritDoc} */
61    @Override
62    public String insnCommentString(DalvInsn insn, boolean noteIndices) {
63        // This format has no comment.
64        return "";
65    }
66
67    /** {@inheritDoc} */
68    @Override
69    public int codeSize() {
70        return 1;
71    }
72
73    /** {@inheritDoc} */
74    @Override
75    public boolean isCompatible(DalvInsn insn) {
76        if (!(insn instanceof SimpleInsn)) {
77            return false;
78        }
79
80        RegisterSpecList regs = insn.getRegisters();
81        RegisterSpec rs1;
82        RegisterSpec rs2;
83
84        switch (regs.size()) {
85            case 2: {
86                rs1 = regs.get(0);
87                rs2 = regs.get(1);
88                break;
89            }
90            case 3: {
91                /*
92                 * This format is allowed for ops that are effectively
93                 * 3-arg but where the first two args are identical.
94                 */
95                rs1 = regs.get(1);
96                rs2 = regs.get(2);
97                if (rs1.getReg() != regs.get(0).getReg()) {
98                    return false;
99                }
100                break;
101            }
102            default: {
103                return false;
104            }
105        }
106
107        return unsignedFitsInNibble(rs1.getReg()) &&
108            unsignedFitsInNibble(rs2.getReg());
109    }
110
111    /** {@inheritDoc} */
112    @Override
113    public BitSet compatibleRegs(DalvInsn insn) {
114        RegisterSpecList regs = insn.getRegisters();
115        BitSet bits = new BitSet(2);
116
117        bits.set(0, unsignedFitsInNibble(regs.get(0).getReg()));
118        bits.set(1, unsignedFitsInNibble(regs.get(1).getReg()));
119        return bits;
120    }
121
122    /** {@inheritDoc} */
123    @Override
124    public void writeTo(AnnotatedOutput out, DalvInsn insn) {
125        RegisterSpecList regs = insn.getRegisters();
126        int sz = regs.size();
127
128        /*
129         * The (sz - 2) and (sz - 1) below makes this code work for
130         * both the two- and three-register ops. (See "case 3" in
131         * isCompatible(), above.)
132         */
133
134        write(out, opcodeUnit(insn,
135                              makeByte(regs.get(sz - 2).getReg(),
136                                       regs.get(sz - 1).getReg())));
137    }
138}
139