1 /* 2 * Copyright (C) 2011 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.google.dexmaker; 18 19 import com.android.dx.rop.code.RegisterSpec; 20 21 /** 22 * A temporary variable that holds a single value of a known type. 23 */ 24 public final class Local<T> { 25 private final Code code; 26 final TypeId<T> type; 27 private int reg = -1; 28 private RegisterSpec spec; 29 30 private Local(Code code, TypeId<T> type) { 31 this.code = code; 32 this.type = type; 33 } 34 35 static <T> Local<T> get(Code code, TypeId<T> type) { 36 return new Local<T>(code, type); 37 } 38 39 /** 40 * Assigns registers to this local. 41 * 42 * @return the number of registers required. 43 */ 44 int initialize(int nextAvailableRegister) { 45 this.reg = nextAvailableRegister; 46 this.spec = RegisterSpec.make(nextAvailableRegister, type.ropType); 47 return size(); 48 } 49 50 /** 51 * Returns the number of registered required to hold this local. 52 */ 53 int size() { 54 return type.ropType.getCategory(); 55 } 56 57 RegisterSpec spec() { 58 if (spec == null) { 59 code.initializeLocals(); 60 if (spec == null) { 61 throw new AssertionError(); 62 } 63 } 64 return spec; 65 } 66 67 public TypeId getType() { 68 return type; 69 } 70 71 @Override public String toString() { 72 return "v" + reg + "(" + type + ")"; 73 } 74 } 75