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