comparison graal/com.oracle.max.cri/src/com/sun/cri/ci/CiCalleeSaveLayout.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 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 size = this.size;
98
99 this.regNumToIndex = new int[maxRegNum + 1];
100 this.indexToReg = offset == 0 ? new CiRegister[0] : new CiRegister[offset / slotSize];
101 Arrays.fill(regNumToIndex, -1);
102 offset = 0;
103 for (CiRegister reg : registers) {
104 int index = offset / slotSize;
105 regNumToIndex[reg.number] = index;
106 indexToReg[index] = reg;
107 offset += reg.spillSlotSize;
108 }
109 }
110
111 /**
112 * Gets the offset of a given register in the CSA.
113 *
114 * @return the offset (in bytes) of {@code reg} in the CSA
115 * @throws IllegalArgumentException if {@code reg} does not have a slot in the CSA
116 */
117 public int offsetOf(int reg) {
118 return indexOf(reg) * slotSize;
119 }
120
121 /**
122 * Gets the index of a given register in the CSA.
123 *
124 * @return the index of {@code reg} in the CSA
125 * @throws IllegalArgumentException if {@code reg} does not have a slot in the CSA
126 */
127 public int indexOf(int reg) {
128 if (!contains(reg)) {
129 throw new IllegalArgumentException(String.valueOf(reg));
130 }
131 return regNumToIndex[reg];
132 }
133
134 /**
135 * Gets the offset of a given register in the CSA.
136 *
137 * @return the offset (in bytes) of {@code reg} in the CSA
138 * @throws IllegalArgumentException if {@code reg} does not have a slot in the CSA
139 */
140 public int offsetOf(CiRegister reg) {
141 return offsetOf(reg.number);
142 }
143
144 /**
145 * Determines if the CSA includes a slot for a given register.
146 *
147 * @param reg the register to test
148 * @return true if the CSA contains a slot for {@code reg}
149 */
150 public boolean contains(int reg) {
151 return reg >= 0 && reg < regNumToIndex.length && regNumToIndex[reg] != -1;
152 }
153
154 /**
155 * Gets the register whose slot in the CSA is at a given index.
156 *
157 * @param index an index of a slot in the CSA
158 * @return the register whose slot in the CSA is at {@code index} or {@code null} if {@code index} does not denote a
159 * slot in the CSA aligned with a register
160 */
161 public CiRegister registerAt(int index) {
162 if (index < 0 || index >= indexToReg.length) {
163 return null;
164 }
165 return indexToReg[index];
166 }
167
168 @Override
169 public String toString() {
170 StringBuilder sb = new StringBuilder("[");
171 for (CiRegister reg : registers) {
172 if (sb.length() != 1) {
173 sb.append(", ");
174 }
175 sb.append(reg).append("{+").append(offsetOf(reg)).append('}');
176 }
177 return sb.append("] size=").append(size).toString();
178 }
179 }