1 /*
2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package jdk.vm.ci.code;
24 
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Iterator;
29 import java.util.List;
30 
31 /**
32  * An immutable ordered list of registers. Only required because Java lacks immutable arrays.
33  */
34 public final class RegisterArray implements Iterable<Register> {
35 
36     private final Register[] registers;
37     private int hash;
38 
RegisterArray(Register... registers)39     public RegisterArray(Register... registers) {
40         this.registers = registers;
41     }
42 
RegisterArray(Collection<Register> registers)43     public RegisterArray(Collection<Register> registers) {
44         this.registers = registers.toArray(new Register[registers.size()]);
45     }
46 
47     /**
48      * Gets the number of registers.
49      */
size()50     public int size() {
51         return registers.length;
52     }
53 
54     /**
55      * Gets the register at a given index.
56      *
57      * @param index the index of the register to retrieve
58      */
get(int index)59     public Register get(int index) {
60         return registers[index];
61     }
62 
addTo(Collection<Register> collection)63     public void addTo(Collection<Register> collection) {
64         collection.addAll(Arrays.asList(registers));
65     }
66 
67     /**
68      * Gets an immutable view of the registers as a list.
69      */
asList()70     public List<Register> asList() {
71         return Collections.unmodifiableList(Arrays.asList(registers));
72     }
73 
74     /**
75      * Gets a copy of the registers as an array.
76      */
toArray()77     public Register[] toArray() {
78         return registers.clone();
79     }
80 
81     @Override
iterator()82     public Iterator<Register> iterator() {
83         return Arrays.asList(registers).iterator();
84     }
85 
86     @Override
hashCode()87     public int hashCode() {
88         if (hash == 0 && registers.length > 0) {
89             hash = Arrays.hashCode(registers);
90         }
91         return hash;
92     }
93 
94     @Override
equals(Object obj)95     public boolean equals(Object obj) {
96         if (obj instanceof RegisterArray) {
97             return Arrays.equals(registers, ((RegisterArray) obj).registers);
98         }
99         return false;
100     }
101 
102     @Override
toString()103     public String toString() {
104         return Arrays.toString(registers);
105     }
106 }
107