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 17package com.android.dx.ssa; 18 19import com.android.dx.rop.code.RegisterSpec; 20import com.android.dx.rop.code.RegisterSpecList; 21 22/** 23 * Represents a mapping between two register numbering schemes. 24 * Subclasses of this may be mutable, and as such the mapping provided 25 * is only valid for the lifetime of the method call in which 26 * instances of this class are passed. 27 */ 28public abstract class RegisterMapper { 29 /** 30 * Gets the count of registers (really, the total register width, since 31 * category width is counted) in the new namespace. 32 * @return >= 0 width of new namespace. 33 */ 34 public abstract int getNewRegisterCount(); 35 36 /** 37 * @param registerSpec old register 38 * @return register in new space 39 */ 40 public abstract RegisterSpec map(RegisterSpec registerSpec); 41 42 /** 43 * 44 * @param sources old register list 45 * @return new mapped register list, or old if nothing has changed. 46 */ 47 public final RegisterSpecList map(RegisterSpecList sources) { 48 int sz = sources.size(); 49 RegisterSpecList newSources = new RegisterSpecList(sz); 50 51 for (int i = 0; i < sz; i++) { 52 newSources.set(i, map(sources.get(i))); 53 } 54 55 newSources.setImmutable(); 56 57 // Return the old sources if nothing has changed. 58 return newSources.equals(sources) ? sources : newSources; 59 } 60} 61