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