summaryrefslogtreecommitdiffstats
path: root/dx/src/com/android/jack/dx/ssa/BasicRegisterMapper.java
blob: 7093355c15dade4ddfdab5369f3becacda990840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.jack.dx.ssa;

import com.android.jack.dx.rop.code.RegisterSpec;
import com.android.jack.dx.util.IntList;

/**
 * This class maps one register space into another, with
 * each mapping built up individually and added via addMapping()
 */
public class BasicRegisterMapper extends RegisterMapper {
  /** indexed by old register, containing new name */
  private IntList oldToNew;

  /** running count of used registers in new namespace */
  private int runningCountNewRegisters;

  /**
   * Creates a new OneToOneRegisterMapper.
   *
   * @param countOldRegisters the number of registers in the old name space
   */
  public BasicRegisterMapper(int countOldRegisters) {
    oldToNew = new IntList(countOldRegisters);
  }

  /** {@inheritDoc} */
  @Override
  public int getNewRegisterCount() {
    return runningCountNewRegisters;
  }

  /** {@inheritDoc} */
  @Override
  public RegisterSpec map(RegisterSpec registerSpec) {
    if (registerSpec == null) {
      return null;
    }

    int newReg;
    try {
      newReg = oldToNew.get(registerSpec.getReg());
    } catch (IndexOutOfBoundsException ex) {
      newReg = -1;
    }

    if (newReg < 0) {
      throw new RuntimeException("no mapping specified for register");
    }

    return registerSpec.withReg(newReg);
  }

  /**
   * Returns the new-namespace mapping for the specified
   * old-namespace register, or -1 if one exists.
   *
   * @param oldReg {@code >= 0;} old-namespace register
   * @return new-namespace register or -1 if none
   */
  public int oldToNew(int oldReg) {
    if (oldReg >= oldToNew.size()) {
      return -1;
    }

    return oldToNew.get(oldReg);
  }

  /** {@inheritDoc} */
  public String toHuman() {
    StringBuilder sb = new StringBuilder();

    sb.append("Old\tNew\n");
    int sz = oldToNew.size();

    for (int i = 0; i < sz; i++) {
      sb.append(i);
      sb.append('\t');
      sb.append(oldToNew.get(i));
      sb.append('\n');
    }

    sb.append("new reg count:");

    sb.append(runningCountNewRegisters);
    sb.append('\n');

    return sb.toString();
  }

  /**
   * Adds a mapping to the mapper. If oldReg has already been mapped,
   * overwrites previous mapping with new mapping.
   *
   * @param oldReg {@code >= 0;} old register
   * @param newReg {@code >= 0;} new register
   * @param category {@code 1..2;} width of reg
   */
  public void addMapping(int oldReg, int newReg, int category) {
    if (oldReg >= oldToNew.size()) {
      // expand the array as necessary
      for (int i = oldReg - oldToNew.size(); i >= 0; i--) {
        oldToNew.add(-1);
      }
    }

    oldToNew.set(oldReg, newReg);

    if (runningCountNewRegisters < (newReg + category)) {
      runningCountNewRegisters = newReg + category;
    }
  }
}