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