1917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/* 2917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Copyright (C) 2008 The Android Open Source Project 3917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 4917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Licensed under the Apache License, Version 2.0 (the "License"); 5917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * you may not use this file except in compliance with the License. 6917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * You may obtain a copy of the License at 7917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 8917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * http://www.apache.org/licenses/LICENSE-2.0 9917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 10917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Unless required by applicable law or agreed to in writing, software 11917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * distributed under the License is distributed on an "AS IS" BASIS, 12917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * See the License for the specific language governing permissions and 14917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * limitations under the License. 15917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 16917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 17917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpackage com.android.dexgen.dex.code; 18917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 19917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.code.RegisterSpecList; 20917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.code.SourcePosition; 21917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.cst.*; 22917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.type.Type; 23917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.util.AnnotatedOutput; 24917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.util.Hex; 25917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 26917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.util.ArrayList; 27917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 28917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/** 29917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Pseudo-instruction which holds fill array data. 30917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 31917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpublic final class ArrayData extends VariableSizeInsn { 32917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 33917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@code non-null;} address representing the instruction that uses this 34917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * instance 35917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 36917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private final CodeAddress user; 37917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 38917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@code non-null;} initial values to be filled into an array */ 39917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private final ArrayList<Constant> values; 40917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 41917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** non-null: type of constant that initializes the array */ 42917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private final Constant arrayType; 43917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 44917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** Width of the init value element */ 45917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private final int elemWidth; 46917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 47917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** Length of the init list */ 48917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private final int initLength; 49917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 50917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 51917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Constructs an instance. The output address of this instance is initially 52917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * unknown ({@code -1}). 53917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 54917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param position {@code non-null;} source position 55917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param user {@code non-null;} address representing the instruction that 56917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * uses this instance 57917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param values {@code non-null;} initial values to be filled into an array 58917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 59917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public ArrayData(SourcePosition position, CodeAddress user, 60917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul ArrayList<Constant> values, 61917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul Constant arrayType) { 62917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul super(position, RegisterSpecList.EMPTY); 63917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 64917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (user == null) { 65917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new NullPointerException("user == null"); 66917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 67917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 68917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (values == null) { 69917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new NullPointerException("values == null"); 70917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 71917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 72917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int sz = values.size(); 73917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 74917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (sz <= 0) { 75917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new IllegalArgumentException("Illegal number of init values"); 76917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 77917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 78917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul this.arrayType = arrayType; 79917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 80917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (arrayType == CstType.BYTE_ARRAY || 81917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul arrayType == CstType.BOOLEAN_ARRAY) { 82917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul elemWidth = 1; 83917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } else if (arrayType == CstType.SHORT_ARRAY || 84917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul arrayType == CstType.CHAR_ARRAY) { 85917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul elemWidth = 2; 86917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } else if (arrayType == CstType.INT_ARRAY || 87917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul arrayType == CstType.FLOAT_ARRAY) { 88917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul elemWidth = 4; 89917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } else if (arrayType == CstType.LONG_ARRAY || 90917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul arrayType == CstType.DOUBLE_ARRAY) { 91917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul elemWidth = 8; 92917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } else { 93917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new IllegalArgumentException("Unexpected constant type"); 94917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 95917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul this.user = user; 96917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul this.values = values; 97917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul initLength = values.size(); 98917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 99917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 100917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 101917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 102917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public int codeSize() { 103917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int sz = initLength; 104917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul // Note: the unit here is 16-bit 105917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return 4 + ((sz * elemWidth) + 1) / 2; 106917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 107917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 108917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 109917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 110917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public void writeTo(AnnotatedOutput out) { 111917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int sz = values.size(); 112917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 113917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeShort(0x300 | DalvOps.NOP); 114917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeShort(elemWidth); 115917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeInt(initLength); 116917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 117917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 118917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul // For speed reasons, replicate the for loop in each case 119917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul switch (elemWidth) { 120917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul case 1: { 121917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (int i = 0; i < sz; i++) { 122917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul Constant cst = values.get(i); 123917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeByte((byte) ((CstLiteral32) cst).getIntBits()); 124917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 125917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul break; 126917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 127917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul case 2: { 128917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (int i = 0; i < sz; i++) { 129917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul Constant cst = values.get(i); 130917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeShort((short) ((CstLiteral32) cst).getIntBits()); 131917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 132917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul break; 133917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 134917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul case 4: { 135917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (int i = 0; i < sz; i++) { 136917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul Constant cst = values.get(i); 137917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeInt(((CstLiteral32) cst).getIntBits()); 138917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 139917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul break; 140917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 141917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul case 8: { 142917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (int i = 0; i < sz; i++) { 143917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul Constant cst = values.get(i); 144917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeLong(((CstLiteral64) cst).getLongBits()); 145917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 146917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul break; 147917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 148917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul default: 149917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul break; 150917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 151917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 152917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul // Pad one byte to make the size of data table multiples of 16-bits 153917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (elemWidth == 1 && (sz % 2 != 0)) { 154917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeByte(0x00); 155917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 156917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 157917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 158917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 159917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 160917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public DalvInsn withRegisters(RegisterSpecList registers) { 161917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return new ArrayData(getPosition(), user, values, arrayType); 162917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 163917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 164917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 165917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 166917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul protected String argString() { 167917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul StringBuffer sb = new StringBuffer(100); 168917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 169917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int sz = values.size(); 170917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (int i = 0; i < sz; i++) { 171917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append("\n "); 172917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append(i); 173917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append(": "); 174917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append(values.get(i).toHuman()); 175917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 176917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 177917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return sb.toString(); 178917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 179917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 180917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 181917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 182917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul protected String listingString0(boolean noteIndices) { 183917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int baseAddress = user.getAddress(); 184917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul StringBuffer sb = new StringBuffer(100); 185917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int sz = values.size(); 186917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 187917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append("array-data // for fill-array-data @ "); 188917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append(Hex.u2(baseAddress)); 189917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 190917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul for (int i = 0; i < sz; i++) { 191917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append("\n "); 192917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append(i); 193917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append(": "); 194917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul sb.append(values.get(i).toHuman()); 195917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 196917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 197917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return sb.toString(); 198917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 199917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul} 200