ArrayData.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
1/*
2 * Copyright (C) 2008 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;
18
19import com.android.dx.rop.code.RegisterSpecList;
20import com.android.dx.rop.code.SourcePosition;
21import com.android.dx.rop.cst.*;
22import com.android.dx.util.AnnotatedOutput;
23import com.android.dx.util.Hex;
24import com.android.dx.rop.type.Type;
25import java.util.ArrayList;
26
27/**
28 * Pseudo-instruction which holds fill array data.
29 */
30public final class ArrayData extends VariableSizeInsn {
31    /**
32     * non-null; address representing the instruction that uses this
33     * instance
34     */
35    private final CodeAddress user;
36
37    /** non-null; initial values to be filled into an array */
38    private final ArrayList<Constant> values;
39
40    /** non-null: type of constant that initializes the array */
41    private final Constant arrayType;
42
43    /** Width of the init value element */
44    private final int elemWidth;
45
46    /** Length of the init list */
47    private final int initLength;
48
49    /**
50     * Constructs an instance. The output address of this instance is initially
51     * unknown (<code>-1</code>).
52     *
53     * @param position non-null; source position
54     * @param user non-null; address representing the instruction that
55     * uses this instance
56     * @param values non-null; initial values to be filled into an array
57     */
58    public ArrayData(SourcePosition position, CodeAddress user,
59                     ArrayList<Constant> values,
60                     Constant arrayType) {
61        super(position, RegisterSpecList.EMPTY);
62
63        if (user == null) {
64            throw new NullPointerException("user == null");
65        }
66
67        if (values == null) {
68            throw new NullPointerException("values == null");
69        }
70
71        int sz = values.size();
72
73        if (sz <= 0) {
74            throw new IllegalArgumentException("Illegal number of init values");
75        }
76
77        this.arrayType = arrayType;
78
79        if (arrayType == CstType.BYTE_ARRAY ||
80                arrayType == CstType.BOOLEAN_ARRAY) {
81            elemWidth = 1;
82        } else if (arrayType == CstType.SHORT_ARRAY ||
83                arrayType == CstType.CHAR_ARRAY) {
84            elemWidth = 2;
85        } else if (arrayType == CstType.INT_ARRAY ||
86                arrayType == CstType.FLOAT_ARRAY) {
87            elemWidth = 4;
88        } else if (arrayType == CstType.LONG_ARRAY ||
89                arrayType == CstType.DOUBLE_ARRAY) {
90            elemWidth = 8;
91        } else {
92            throw new IllegalArgumentException("Unexpected constant type");
93        }
94        this.user = user;
95        this.values = values;
96        initLength = values.size();
97    }
98
99    /** {@inheritDoc} */
100    @Override
101    public int codeSize() {
102        int sz = initLength;
103        // Note: the unit here is 16-bit
104        return 4 + ((sz * elemWidth) + 1) / 2;
105    }
106
107    /** {@inheritDoc} */
108    @Override
109    public void writeTo(AnnotatedOutput out) {
110        int baseAddress = user.getAddress();
111        int sz = values.size();
112
113        out.writeShort(0x300 | DalvOps.NOP);
114        out.writeShort(elemWidth);
115        out.writeInt(initLength);
116
117
118        // For speed reasons, replicate the for loop in each case
119        switch (elemWidth) {
120            case 1: {
121                for (int i = 0; i < sz; i++) {
122                    Constant cst = values.get(i);
123                    out.writeByte((byte) ((CstLiteral32) cst).getIntBits());
124                }
125                break;
126            }
127            case 2: {
128                for (int i = 0; i < sz; i++) {
129                    Constant cst = values.get(i);
130                    out.writeShort((short) ((CstLiteral32) cst).getIntBits());
131                }
132                break;
133            }
134            case 4: {
135                for (int i = 0; i < sz; i++) {
136                    Constant cst = values.get(i);
137                    out.writeInt(((CstLiteral32) cst).getIntBits());
138                }
139                break;
140            }
141            case 8: {
142                for (int i = 0; i < sz; i++) {
143                    Constant cst = values.get(i);
144                    out.writeLong(((CstLiteral64) cst).getLongBits());
145                }
146                break;
147            }
148            default:
149                break;
150        }
151
152        // Pad one byte to make the size of data table multiples of 16-bits
153        if (elemWidth == 1 && (sz % 2 != 0)) {
154            out.writeByte(0x00);
155        }
156    }
157
158    /** {@inheritDoc} */
159    @Override
160    public DalvInsn withRegisters(RegisterSpecList registers) {
161        return new ArrayData(getPosition(), user, values, arrayType);
162    }
163
164    /** {@inheritDoc} */
165    @Override
166    protected String argString() {
167        StringBuffer sb = new StringBuffer(100);
168
169        int sz = values.size();
170        for (int i = 0; i < sz; i++) {
171            sb.append("\n    ");
172            sb.append(i);
173            sb.append(": ");
174            sb.append(values.get(i).toHuman());
175        }
176
177        return sb.toString();
178    }
179
180    /** {@inheritDoc} */
181    @Override
182    protected String listingString0(boolean noteIndices) {
183        int baseAddress = user.getAddress();
184        StringBuffer sb = new StringBuffer(100);
185        int sz = values.size();
186
187        sb.append("array-data // for fill-array-data @ ");
188        sb.append(Hex.u2(baseAddress));
189
190        for (int i = 0; i < sz; i++) {
191            sb.append("\n  ");
192            sb.append(i);
193            sb.append(": ");
194            sb.append(values.get(i).toHuman());
195        }
196
197        return sb.toString();
198    }
199}
200