• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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      */
reverseBranch(int which, CodeAddress newTarget)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      */
addSuffix(DalvInsn insn)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      */
getFinisher()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      */
appendSuffixToOutput()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