1   // Copyright 2013 Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
2   
3   package jminusminus;
4   
5   import java.util.ArrayList;
6   import java.util.Collections;
7   
8   /**
9    * An abstraction for a (virtual or physical) register.
10   */
11  
12  abstract class NRegister {
13  
14      /** Register number. */
15      protected int number;
16  
17      /** Register name. */
18      protected String name;
19  
20      /**
21       * Construct an NRegister.
22       * 
23       * @param number
24       *            register number.
25       * @param name
26       *            register name.
27       */
28  
29      protected NRegister(int number, String name) {
30          this.number = number;
31          this.name = name;
32      }
33  
34      /**
35       * Return the number of this register.
36       * 
37       * @return register number.
38       */
39  
40      public int number() {
41          return number;
42      }
43  
44      /**
45       * Return the name of this register.
46       * 
47       * @return register name.
48       */
49  
50      public String name() {
51          return name;
52      }
53  
54  }
55  
56  /**
57   * An abstraction for a virtual register.
58   */
59  
60  class NVirtualRegister extends NRegister {
61  
62      /** Type (short name) of value in register. */
63      private String sType;
64  
65      /** Type (long name) of value in register. */
66      private String lType;
67  
68      /**
69       * Construct an NVirutalRegister.
70       * 
71       * @param number
72       *            register number.
73       * @param sType
74       *            type (short name) of value in register.
75       * @param lType
76       *            type (long name) of value in register.
77       */
78  
79      public NVirtualRegister(int number, String sType, String lType) {
80          super(number, "V" + number);
81          this.sType = sType;
82          this.lType = lType;
83      }
84  
85      /**
86       * Return a string representation of this virtual register.
87       * 
88       * @return string representation.
89       */
90  
91      public String toString() {
92          return "[" + name + "|" + sType + "]";
93      }
94  
95  }
96  
97  /**
98   * An abstraction for a physical (SPIM) register.
99   */
100 
101 class NPhysicalRegister extends NRegister {
102 
103     /**
104      * Maximum number of physical registers used for allocation, starting at T0.
105      */
106     public static int MAX_COUNT = 8;
107 
108     // Constants identifying the physical registers. These
109     // can be used as indices into the static regInfo array
110     // to access the representations of the corresponding
111     // registers.
112 
113     /** Constant 0. */
114     public static final int ZERO = 0;
115 
116     /** Reserved for assembler. */
117     public static final int AT = 1;
118 
119     /** Expression evaluation and results of a function. */
120     public static final int V0 = 2;
121 
122     /** Expression evaluation and results of a function. */
123     public static final int V1 = 3;
124 
125     /** Argument 1. */
126     public static final int A0 = 4;
127 
128     /** Argument 2. */
129     public static final int A1 = 5;
130 
131     /** Argument 3. */
132     public static final int A2 = 6;
133 
134     /** Argument 4. */
135     public static final int A3 = 7;
136 
137     /** Temporary (not preserved across call). */
138     public static final int T0 = 8;
139 
140     /** Temporary (not preserved across call). */
141     public static final int T1 = 9;
142 
143     /** Temporary (not preserved across call). */
144     public static final int T2 = 10;
145 
146     /** Temporary (not preserved across call). */
147     public static final int T3 = 11;
148 
149     /** Temporary (not preserved across call). */
150     public static final int T4 = 12;
151 
152     /** Temporary (not preserved across call). */
153     public static final int T5 = 13;
154 
155     /** Temporary (not preserved across call). */
156     public static final int T6 = 14;
157 
158     /** Temporary (not preserved across call). */
159     public static final int T7 = 15;
160 
161     /** Temporary (preserved across call). */
162     public static final int S0 = 16;
163 
164     /** Temporary (preserved across call). */
165     public static final int S1 = 17;
166 
167     /** Temporary (preserved across call). */
168     public static final int S2 = 18;
169 
170     /** Temporary (preserved across call). */
171     public static final int S3 = 19;
172 
173     /** Temporary (preserved across call). */
174     public static final int S4 = 20;
175 
176     /** Temporary (preserved across call). */
177     public static final int S5 = 21;
178 
179     /** Temporary (preserved across call). */
180     public static final int S6 = 22;
181 
182     /** Temporary (preserved across call). */
183     public static final int S7 = 23;
184 
185     /** Temporary (not preserved across call). */
186     public static final int T8 = 24;
187 
188     /** Temporary (not preserved across call). */
189     public static final int T9 = 25;
190 
191     /** Reserved for OS kernel. */
192     public static final int K0 = 26;
193 
194     /** Reserved for OS kernel. */
195     public static final int K1 = 27;
196 
197     /** Pointer to global area. */
198     public static final int GP = 28;
199 
200     /** Stack pointer. */
201     public static final int SP = 29;
202 
203     /** Frame pointer. */
204     public static final int FP = 30;
205 
206     /** Return address (used by function call). */
207     public static final int RA = 31;
208 
209     /**
210      * Maps register number to the register's representation.
211      */
212     public static final NPhysicalRegister[] regInfo = {
213             new NPhysicalRegister(0, "zero"), new NPhysicalRegister(1, "at"),
214             new NPhysicalRegister(2, "v0"), new NPhysicalRegister(3, "v1"),
215             new NPhysicalRegister(4, "a0"), new NPhysicalRegister(5, "a1"),
216             new NPhysicalRegister(6, "a2"), new NPhysicalRegister(7, "a3"),
217             new NPhysicalRegister(8, "t0"), new NPhysicalRegister(9, "t1"),
218             new NPhysicalRegister(10, "t2"), new NPhysicalRegister(11, "t3"),
219             new NPhysicalRegister(12, "t4"), new NPhysicalRegister(13, "t5"),
220             new NPhysicalRegister(14, "t6"), new NPhysicalRegister(15, "t7"),
221             new NPhysicalRegister(16, "s0"), new NPhysicalRegister(17, "s1"),
222             new NPhysicalRegister(18, "s2"), new NPhysicalRegister(19, "s3"),
223             new NPhysicalRegister(20, "s4"), new NPhysicalRegister(21, "s5"),
224             new NPhysicalRegister(22, "s6"), new NPhysicalRegister(23, "s7"),
225             new NPhysicalRegister(24, "t8"), new NPhysicalRegister(25, "t9"),
226             new NPhysicalRegister(26, "k0"), new NPhysicalRegister(27, "k1"),
227             new NPhysicalRegister(28, "gp"), new NPhysicalRegister(29, "sp"),
228             new NPhysicalRegister(30, "fp"), new NPhysicalRegister(31, "ra"), };
229 
230     /**
231      * Construct an NPhysicalRegister.
232      * 
233      * @param number
234      *            number of the register.
235      * @param name
236      *            name of the register.
237      */
238 
239     public NPhysicalRegister(int number, String name) {
240         super(number, name);
241     }
242 
243     /**
244      * Return a string representation of this physical register.
245      * 
246      * @return string representation.
247      */
248 
249     public String toString() {
250         return "$" + name();
251     }
252 
253 }
254