Home | History | Annotate | Download | only in code
      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      */
     52     public OutputCollector(DexOptions dexOptions, int initialCapacity, int suffixInitialCapacity,
     53             int regCount) {
     54         this.finisher = new OutputFinisher(dexOptions, initialCapacity, regCount);
     55         this.suffix = new ArrayList<DalvInsn>(suffixInitialCapacity);
     56     }
     57 
     58     /**
     59      * Adds an instruction to the output.
     60      *
     61      * @param insn {@code non-null;} the instruction to add
     62      */
     63     public void add(DalvInsn insn) {
     64         finisher.add(insn);
     65     }
     66 
     67     /**
     68      * Reverses a branch which is buried a given number of instructions
     69      * backward in the output. It is illegal to call this unless the
     70      * indicated instruction really is a reversible branch.
     71      *
     72      * @param which how many instructions back to find the branch;
     73      * {@code 0} is the most recently added instruction,
     74      * {@code 1} is the instruction before that, etc.
     75      * @param newTarget {@code non-null;} the new target for the reversed branch
     76      */
     77     public void reverseBranch(int which, CodeAddress newTarget) {
     78         finisher.reverseBranch(which, newTarget);
     79     }
     80 
     81     /**
     82      * Adds an instruction to the output suffix.
     83      *
     84      * @param insn {@code non-null;} the instruction to add
     85      */
     86     public void addSuffix(DalvInsn insn) {
     87         suffix.add(insn);
     88     }
     89 
     90     /**
     91      * Gets the results of all the calls on this instance, in the form of
     92      * an {@link OutputFinisher}.
     93      *
     94      * @return {@code non-null;} the output finisher
     95      * @throws UnsupportedOperationException if this method has
     96      * already been called
     97      */
     98     public OutputFinisher getFinisher() {
     99         if (suffix == null) {
    100             throw new UnsupportedOperationException("already processed");
    101         }
    102 
    103         appendSuffixToOutput();
    104         return finisher;
    105     }
    106 
    107     /**
    108      * Helper for {@link #getFinisher}, which appends the suffix to
    109      * the primary output.
    110      */
    111     private void appendSuffixToOutput() {
    112         int size = suffix.size();
    113 
    114         for (int i = 0; i < size; i++) {
    115             finisher.add(suffix.get(i));
    116         }
    117 
    118         suffix = null;
    119     }
    120 }
    121