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