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.rop.code.RegisterSpec;
20 import com.android.dx.rop.code.RegisterSpecList;
21 import com.android.dx.rop.code.RegisterSpecSet;
22 import com.android.dx.rop.code.SourcePosition;
23 import com.android.dx.ssa.RegisterMapper;
24 
25 /**
26  * Pseudo-instruction which is used to hold a snapshot of the
27  * state of local variable name mappings that exists immediately after
28  * the instance in an instruction array.
29  */
30 public final class LocalSnapshot extends ZeroSizeInsn {
31     /** {@code non-null;} local state associated with this instance */
32     private final RegisterSpecSet locals;
33 
34     /**
35      * Constructs an instance. The output address of this instance is initially
36      * unknown ({@code -1}).
37      *
38      * @param position {@code non-null;} source position
39      * @param locals {@code non-null;} associated local variable state
40      */
LocalSnapshot(SourcePosition position, RegisterSpecSet locals)41     public LocalSnapshot(SourcePosition position, RegisterSpecSet locals) {
42         super(position);
43 
44         if (locals == null) {
45             throw new NullPointerException("locals == null");
46         }
47 
48         this.locals = locals;
49     }
50 
51     /** {@inheritDoc} */
52     @Override
withRegisterOffset(int delta)53     public DalvInsn withRegisterOffset(int delta) {
54         return new LocalSnapshot(getPosition(), locals.withOffset(delta));
55     }
56 
57     /** {@inheritDoc} */
58     @Override
withRegisters(RegisterSpecList registers)59     public DalvInsn withRegisters(RegisterSpecList registers) {
60         return new LocalSnapshot(getPosition(), locals);
61     }
62 
63     /**
64      * Gets the local state associated with this instance.
65      *
66      * @return {@code non-null;} the state
67      */
getLocals()68     public RegisterSpecSet getLocals() {
69         return locals;
70     }
71 
72     /** {@inheritDoc} */
73     @Override
argString()74     protected String argString() {
75         return locals.toString();
76     }
77 
78     /** {@inheritDoc} */
79     @Override
listingString0(boolean noteIndices)80     protected String listingString0(boolean noteIndices) {
81         int sz = locals.size();
82         int max = locals.getMaxSize();
83         StringBuffer sb = new StringBuffer(100 + sz * 40);
84 
85         sb.append("local-snapshot");
86 
87         for (int i = 0; i < max; i++) {
88             RegisterSpec spec = locals.get(i);
89             if (spec != null) {
90                 sb.append("\n  ");
91                 sb.append(LocalStart.localString(spec));
92             }
93         }
94 
95         return sb.toString();
96     }
97 
98     /** {@inheritDoc} */
99     @Override
withMapper(RegisterMapper mapper)100     public DalvInsn withMapper(RegisterMapper mapper) {
101       return new LocalSnapshot(getPosition(), mapper.map(locals));
102     }
103 }
104