comparison graal/com.oracle.max.cri/src/com/sun/cri/ci/CiVariable.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children bc8527f3071c
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
1 /*
2 * Copyright (c) 2010, 2011, 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 com.sun.cri.ci;
24
25 /**
26 * Represents a value that is yet to be bound to a machine location (such as
27 * a {@linkplain CiRegister register} or stack {@linkplain CiAddress address})
28 * by a register allocator.
29 */
30 public final class CiVariable extends CiValue {
31
32 /**
33 * The identifier of the variable. This is a non-zero index in a contiguous 0-based name space.
34 */
35 public final int index;
36
37 /**
38 * Creates a new variable.
39 * @param kind
40 * @param index
41 */
42 private CiVariable(CiKind kind, int index) {
43 super(kind);
44 this.index = index;
45 }
46
47 private static CiVariable[] generate(CiKind kind, int count) {
48 CiVariable[] variables = new CiVariable[count];
49 for (int i = 0; i < count; i++) {
50 variables[i] = new CiVariable(kind, i);
51 }
52 return variables;
53 }
54
55 private static final int CACHE_PER_KIND_SIZE = 100;
56
57 /**
58 * Cache of common variables.
59 */
60 private static final CiVariable[][] cache = new CiVariable[CiKind.values().length][];
61 static {
62 for (CiKind kind : CiKind.values()) {
63 cache[kind.ordinal()] = generate(kind, CACHE_PER_KIND_SIZE);
64 }
65 }
66
67 /**
68 * Gets a variable for a given kind and index.
69 *
70 * @param kind
71 * @param index
72 * @return the corresponding {@code CiVariable}
73 */
74 public static CiVariable get(CiKind kind, int index) {
75 //assert kind == kind.stackKind() : "Variables can be only created for stack kinds";
76 assert index >= 0;
77 CiVariable[] cachedVars = cache[kind.ordinal()];
78 if (index < cachedVars.length) {
79 return cachedVars[index];
80 }
81 return new CiVariable(kind, index);
82 }
83
84 @Override
85 public boolean equals(Object obj) {
86 if (this == obj) {
87 return true;
88 }
89 if (obj instanceof CiVariable) {
90 CiVariable var = (CiVariable) obj;
91 return kind == var.kind && index == var.index;
92 }
93 return false;
94 }
95
96 @Override
97 public boolean equalsIgnoringKind(CiValue o) {
98 if (this == o) {
99 return true;
100 }
101 if (o instanceof CiVariable) {
102 CiVariable var = (CiVariable) o;
103 return index == var.index;
104 }
105 return false;
106 }
107
108 @Override
109 public int hashCode() {
110 return (index << 4) | kind.ordinal();
111 }
112
113 @Override
114 public String name() {
115 return "v" + index;
116 }
117 }