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 17 package com.android.dx.dex.code; 18 19 import com.android.dx.dex.DexOptions; 20 import java.util.ArrayList; 21 22 /** 23 * Destination for {@link DalvInsn} instances being output. This class 24 * receives and collects instructions in two pieces — a primary 25 * list and a suffix (generally consisting of adjunct data referred to 26 * by the primary list, such as switch case tables) — which it 27 * merges and emits back out in the form of a {@link DalvInsnList} 28 * instance. 29 */ 30 public final class OutputCollector { 31 /** 32 * {@code non-null;} the associated finisher (which holds the instruction 33 * list in-progress) 34 */ 35 private final OutputFinisher finisher; 36 37 /** 38 * {@code null-ok;} suffix for the output, or {@code null} if the suffix 39 * has been appended to the main output (by {@link #appendSuffixToOutput}) 40 */ 41 private ArrayList<DalvInsn> suffix; 42 43 /** 44 * Constructs an instance. 45 * 46 * @param dexOptions {@code non-null;} options for dex output 47 * @param initialCapacity {@code >= 0;} initial capacity of the output list 48 * @param suffixInitialCapacity {@code >= 0;} initial capacity of the output 49 * suffix 50 * @param regCount {@code >= 0;} register count for the method 51 * @param paramSize size, in register units, of all the parameters for this method 52 */ OutputCollector(DexOptions dexOptions, int initialCapacity, int suffixInitialCapacity, int regCount, int paramSize)53 public OutputCollector(DexOptions dexOptions, int initialCapacity, int suffixInitialCapacity, 54 int regCount, int paramSize) { 55 this.finisher = new OutputFinisher(dexOptions, initialCapacity, regCount, paramSize); 56 this.suffix = new ArrayList<DalvInsn>(suffixInitialCapacity); 57 } 58 59 /** 60 * Adds an instruction to the output. 61 * 62 * @param insn {@code non-null;} the instruction to add 63 */ add(DalvInsn insn)64 public void add(DalvInsn insn) { 65 finisher.add(insn); 66 } 67 get(int at)68 public DalvInsn get(int at) { 69 if (at >= finisher.size() || at < 0) { 70 return null; 71 } else { 72 return finisher.get(at); 73 } 74 } 75 size()76 public int size() { 77 return finisher.size(); 78 } 79 80 /** 81 * Reverses a branch which is buried a given number of instructions 82 * backward in the output. It is illegal to call this unless the 83 * indicated instruction really is a reversible branch. 84 * 85 * @param which how many instructions back to find the branch; 86 * {@code 0} is the most recently added instruction, 87 * {@code 1} is the instruction before that, etc. 88 * @param newTarget {@code non-null;} the new target for the reversed branch 89 */ reverseBranch(int which, CodeAddress newTarget)90 public void reverseBranch(int which, CodeAddress newTarget) { 91 finisher.reverseBranch(which, newTarget); 92 } 93 94 /** 95 * Adds an instruction to the output suffix. 96 * 97 * @param insn {@code non-null;} the instruction to add 98 */ addSuffix(DalvInsn insn)99 public void addSuffix(DalvInsn insn) { 100 suffix.add(insn); 101 } 102 103 /** 104 * Gets the results of all the calls on this instance, in the form of 105 * an {@link OutputFinisher}. 106 * 107 * @return {@code non-null;} the output finisher 108 * @throws UnsupportedOperationException if this method has 109 * already been called 110 */ getFinisher()111 public OutputFinisher getFinisher() { 112 if (suffix == null) { 113 throw new UnsupportedOperationException("already processed"); 114 } 115 116 appendSuffixToOutput(); 117 return finisher; 118 } 119 120 /** 121 * Helper for {@link #getFinisher}, which appends the suffix to 122 * the primary output. 123 */ appendSuffixToOutput()124 private void appendSuffixToOutput() { 125 int size = suffix.size(); 126 127 for (int i = 0; i < size; i++) { 128 finisher.add(suffix.get(i)); 129 } 130 131 suffix = null; 132 } 133 } 134