comparison graal/com.oracle.max.cri/src/com/oracle/max/cri/ci/CiCalleeSaveLayout.java @ 4199:aaac4894175c

Renamed cri packages from sun to oracle.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 03 Jan 2012 16:29:28 +0100
parents graal/com.oracle.max.cri/src/com/sun/cri/ci/CiCalleeSaveLayout.java@bc8527f3071c
children
comparison
equal deleted inserted replaced
4198:8c9c0e1eaab1 4199:aaac4894175c
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.oracle.max.cri.ci;
24
25 import java.util.*;
26
27
28 /**
29 * The callee save area (CSA) is a contiguous space in a stack frame
30 * used to save (and restore) the values of the caller's registers.
31 * This class describes the layout of a CSA in terms of its
32 * {@linkplain #size size}, {@linkplain #slotSize slot size} and
33 * the {@linkplain #registers callee save registers} covered by the CSA.
34 */
35 public class CiCalleeSaveLayout {
36
37 /**
38 * The size (in bytes) of the CSA.
39 */
40 public final int size;
41
42 /**
43 * The size (in bytes) of an {@linkplain #registerAtIndex(int) indexable} slot in the CSA.
44 */
45 public final int slotSize;
46
47 /**
48 * Map from {@linkplain CiRegister#number register numbers} to slot indexes in the CSA.
49 */
50 private final int[] regNumToIndex;
51
52 private final CiRegister[] indexToReg;
53
54 /**
55 * The list of registers {@linkplain #contains(CiRegister) contained} by this CSA.
56 */
57 public final CiRegister[] registers;
58
59 /**
60 * The offset from the frame pointer to the CSA. If this is not known, then this field
61 * will have the value {@link Integer#MAX_VALUE}.
62 */
63 public final int frameOffsetToCSA;
64
65 /**
66 * Creates a CSA layout.
67 *
68 * @param size size (in bytes) of the CSA. If this is {@code -1}, then the CSA size will be computed from {@code registers}.
69 * @param slotSize the size (in bytes) of an {@linkplain #registerAtIndex(int) indexable} slot in the CSA
70 * @param registers the registers that can be saved in the CSA
71 */
72 public CiCalleeSaveLayout(int frameOffsetToCSA, int size, int slotSize, CiRegister... registers) {
73 this.frameOffsetToCSA = frameOffsetToCSA;
74 assert slotSize == 0 || CiUtil.isPowerOf2(slotSize);
75 this.slotSize = slotSize;
76 int maxRegNum = -1;
77 int maxOffset = 0;
78 this.registers = registers;
79 int offset = 0;
80 for (CiRegister reg : registers) {
81 assert offset % slotSize == 0;
82 assert reg.number >= 0;
83 if (reg.number > maxRegNum) {
84 maxRegNum = reg.number;
85 }
86 if (offset > maxOffset) {
87 maxOffset = offset;
88 }
89 offset += reg.spillSlotSize;
90 }
91 if (size == -1) {
92 this.size = offset;
93 } else {
94 assert offset <= size;
95 this.size = size;
96 }
97
98 this.regNumToIndex = new int[maxRegNum + 1];
99 this.indexToReg = offset == 0 ? new CiRegister[0] : new CiRegister[offset / slotSize];
100 Arrays.fill(regNumToIndex, -1);
101 offset = 0;
102 for (CiRegister reg : registers) {
103 int index = offset / slotSize;
104 regNumToIndex[reg.number] = index;
105 indexToReg[index] = reg;
106 offset += reg.spillSlotSize;
107 }
108 }
109
110 /**
111 * Gets the offset of a given register in the CSA.
112 *
113 * @return the offset (in bytes) of {@code reg} in the CSA
114 * @throws IllegalArgumentException if {@code reg} does not have a slot in the CSA
115 */
116 public int offsetOf(int reg) {
117 return indexOf(reg) * slotSize;
118 }
119
120 /**
121 * Gets the index of a given register in the CSA.
122 *
123 * @return the index of {@code reg} in the CSA
124 * @throws IllegalArgumentException if {@code reg} does not have a slot in the CSA
125 */
126 public int indexOf(int reg) {
127 if (!contains(reg)) {
128 throw new IllegalArgumentException(String.valueOf(reg));
129 }
130 return regNumToIndex[reg];
131 }
132
133 /**
134 * Gets the offset of a given register in the CSA.
135 *
136 * @return the offset (in bytes) of {@code reg} in the CSA
137 * @throws IllegalArgumentException if {@code reg} does not have a slot in the CSA
138 */
139 public int offsetOf(CiRegister reg) {
140 return offsetOf(reg.number);
141 }
142
143 /**
144 * Determines if the CSA includes a slot for a given register.
145 *
146 * @param reg the register to test
147 * @return true if the CSA contains a slot for {@code reg}
148 */
149 public boolean contains(int reg) {
150 return reg >= 0 && reg < regNumToIndex.length && regNumToIndex[reg] != -1;
151 }
152
153 /**
154 * Gets the register whose slot in the CSA is at a given index.
155 *
156 * @param index an index of a slot in the CSA
157 * @return the register whose slot in the CSA is at {@code index} or {@code null} if {@code index} does not denote a
158 * slot in the CSA aligned with a register
159 */
160 public CiRegister registerAt(int index) {
161 if (index < 0 || index >= indexToReg.length) {
162 return null;
163 }
164 return indexToReg[index];
165 }
166
167 @Override
168 public String toString() {
169 StringBuilder sb = new StringBuilder("[");
170 for (CiRegister reg : registers) {
171 if (sb.length() != 1) {
172 sb.append(", ");
173 }
174 sb.append(reg).append("{+").append(offsetOf(reg)).append('}');
175 }
176 return sb.append("] size=").append(size).toString();
177 }
178 }