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.ssa;
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 
23 /**
24  * Represents a mapping between two register numbering schemes.
25  * Subclasses of this may be mutable, and as such the mapping provided
26  * is only valid for the lifetime of the method call in which
27  * instances of this class are passed.
28  */
29 public abstract class RegisterMapper {
30     /**
31      * Gets the count of registers (really, the total register width, since
32      * category width is counted) in the new namespace.
33      * @return >= 0 width of new namespace.
34      */
getNewRegisterCount()35     public abstract int getNewRegisterCount();
36 
37     /**
38      * @param registerSpec old register
39      * @return register in new space
40      */
map(RegisterSpec registerSpec)41     public abstract RegisterSpec map(RegisterSpec registerSpec);
42 
43     /**
44      *
45      * @param sources old register list
46      * @return new mapped register list, or old if nothing has changed.
47      */
map(RegisterSpecList sources)48     public final RegisterSpecList map(RegisterSpecList sources) {
49         int sz = sources.size();
50         RegisterSpecList newSources = new RegisterSpecList(sz);
51 
52         for (int i = 0; i < sz; i++) {
53             newSources.set(i, map(sources.get(i)));
54         }
55 
56         newSources.setImmutable();
57 
58         // Return the old sources if nothing has changed.
59         return newSources.equals(sources) ? sources : newSources;
60     }
61 
62     /**
63      *
64      * @param sources old register set
65      * @return new mapped register set, or old if nothing has changed.
66      */
map(RegisterSpecSet sources)67     public final RegisterSpecSet map(RegisterSpecSet sources) {
68         int sz = sources.getMaxSize();
69         RegisterSpecSet newSources = new RegisterSpecSet(getNewRegisterCount());
70 
71         for (int i = 0; i < sz; i++) {
72             RegisterSpec registerSpec = sources.get(i);
73             if (registerSpec != null) {
74                 newSources.put(map(registerSpec));
75             }
76         }
77 
78         newSources.setImmutable();
79 
80         // Return the old sources if nothing has changed.
81         return newSources.equals(sources) ? sources : newSources;
82     }
83 }
84