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