Mercurial > hg > graal-compiler
comparison graal/Runtime/src/com/sun/hotspot/c1x/HotSpotConstantPool.java @ 2297:099e697d8934
Renaming c1x4hotspotsrc => graal and HotSpotVM => Runtime
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Fri, 22 Apr 2011 15:08:53 +0200 |
parents | |
children | 4e5515d09314 |
comparison
equal
deleted
inserted
replaced
2296:34354e2e40a3 | 2297:099e697d8934 |
---|---|
1 /* | |
2 * Copyright (c) 2010 Sun Microsystems, Inc. All rights reserved. | |
3 * | |
4 * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product | |
5 * that is described in this document. In particular, and without limitation, these intellectual property | |
6 * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or | |
7 * more additional patents or pending patent applications in the U.S. and in other countries. | |
8 * | |
9 * U.S. Government Rights - Commercial software. Government users are subject to the Sun | |
10 * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its | |
11 * supplements. | |
12 * | |
13 * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or | |
14 * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks | |
15 * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the | |
16 * U.S. and other countries. | |
17 * | |
18 * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open | |
19 * Company, Ltd. | |
20 */ | |
21 package com.sun.hotspot.c1x; | |
22 | |
23 import java.io.*; | |
24 import java.util.*; | |
25 | |
26 import com.sun.cri.ri.*; | |
27 | |
28 /** | |
29 * Implementation of RiConstantPool for HotSpot. | |
30 * | |
31 * @author Thomas Wuerthinger, Lukas Stadler | |
32 */ | |
33 public class HotSpotConstantPool extends CompilerObject implements RiConstantPool { | |
34 | |
35 private final long vmId; | |
36 | |
37 private final FastLRUIntCache<RiMethod> methodCache = new FastLRUIntCache<RiMethod>(); | |
38 private final FastLRUIntCache<RiField> fieldCache = new FastLRUIntCache<RiField>(); | |
39 private final FastLRUIntCache<RiType> typeCache = new FastLRUIntCache<RiType>(); | |
40 | |
41 public static class FastLRUIntCache<T> implements Serializable { | |
42 | |
43 private static final int InitialCapacity = 4; | |
44 private int lastKey; | |
45 private T lastObject; | |
46 | |
47 private int[] keys; | |
48 private Object[] objects; | |
49 private int count; | |
50 | |
51 @SuppressWarnings("unchecked") | |
52 private T access(int index) { | |
53 return (T) objects[index]; | |
54 } | |
55 | |
56 public T get(int key) { | |
57 if (key == lastKey) { | |
58 return lastObject; | |
59 } else if (count > 1) { | |
60 for (int i = 0; i < count; ++i) { | |
61 if (keys[i] == key) { | |
62 lastObject = access(i); | |
63 lastKey = key; | |
64 return lastObject; | |
65 } | |
66 } | |
67 } | |
68 return null; | |
69 } | |
70 | |
71 public void add(int key, T object) { | |
72 count++; | |
73 if (count == 1) { | |
74 lastKey = key; | |
75 lastObject = object; | |
76 } else { | |
77 ensureSize(); | |
78 keys[count - 1] = key; | |
79 objects[count - 1] = object; | |
80 if (count == 2) { | |
81 keys[0] = lastKey; | |
82 objects[0] = lastObject; | |
83 } | |
84 lastKey = key; | |
85 lastObject = object; | |
86 } | |
87 } | |
88 | |
89 private void ensureSize() { | |
90 if (keys == null) { | |
91 keys = new int[InitialCapacity]; | |
92 objects = new Object[InitialCapacity]; | |
93 } else if (count > keys.length) { | |
94 keys = Arrays.copyOf(keys, keys.length * 2); | |
95 objects = Arrays.copyOf(objects, objects.length * 2); | |
96 } | |
97 } | |
98 } | |
99 | |
100 public HotSpotConstantPool(Compiler compiler, long vmId) { | |
101 super(compiler); | |
102 this.vmId = vmId; | |
103 } | |
104 | |
105 @Override | |
106 public Object lookupConstant(int cpi) { | |
107 Object constant = compiler.getVMEntries().RiConstantPool_lookupConstant(vmId, cpi); | |
108 return constant; | |
109 } | |
110 | |
111 @Override | |
112 public RiSignature lookupSignature(int cpi) { | |
113 return compiler.getVMEntries().RiConstantPool_lookupSignature(vmId, cpi); | |
114 } | |
115 | |
116 @Override | |
117 public RiMethod lookupMethod(int cpi, int byteCode) { | |
118 RiMethod result = methodCache.get(cpi); | |
119 if (result == null) { | |
120 result = compiler.getVMEntries().RiConstantPool_lookupMethod(vmId, cpi, (byte) byteCode); | |
121 methodCache.add(cpi, result); | |
122 } | |
123 return result; | |
124 } | |
125 | |
126 @Override | |
127 public RiType lookupType(int cpi, int opcode) { | |
128 RiType result = typeCache.get(cpi); | |
129 if (result == null) { | |
130 result = compiler.getVMEntries().RiConstantPool_lookupType(vmId, cpi); | |
131 typeCache.add(cpi, result); | |
132 } | |
133 return result; | |
134 } | |
135 | |
136 @Override | |
137 public RiField lookupField(int cpi, int opcode) { | |
138 RiField result = fieldCache.get(cpi); | |
139 if (result == null) { | |
140 result = compiler.getVMEntries().RiConstantPool_lookupField(vmId, cpi, (byte) opcode); | |
141 fieldCache.add(cpi, result); | |
142 } | |
143 return result; | |
144 } | |
145 } |