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