Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultVirtualFrame.java @ 21951:9c8c0937da41
Moving all sources into truffle subdirectory
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 17 Jun 2015 10:58:08 +0200 |
parents | graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultVirtualFrame.java@cef214c6d74a |
children | dc83cc1f94f2 |
comparison
equal
deleted
inserted
replaced
21950:2a5011c7e641 | 21951:9c8c0937da41 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 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. Oracle designates this | |
8 * particular file as subject to the "Classpath" exception as provided | |
9 * by Oracle in the LICENSE file that accompanied this code. | |
10 * | |
11 * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 * version 2 for more details (a copy is included in the LICENSE file that | |
15 * accompanied this code). | |
16 * | |
17 * You should have received a copy of the GNU General Public License version | |
18 * 2 along with this work; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 * | |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
22 * or visit www.oracle.com if you need additional information or have any | |
23 * questions. | |
24 */ | |
25 package com.oracle.truffle.api.impl; | |
26 | |
27 import java.util.*; | |
28 | |
29 import com.oracle.truffle.api.*; | |
30 import com.oracle.truffle.api.frame.*; | |
31 | |
32 /** | |
33 * This is an implementation-specific class. Do not use or instantiate it. Instead, use | |
34 * {@link TruffleRuntime#createVirtualFrame(Object[], FrameDescriptor)} to create a | |
35 * {@link VirtualFrame}. | |
36 */ | |
37 final class DefaultVirtualFrame implements VirtualFrame { | |
38 | |
39 private final FrameDescriptor descriptor; | |
40 private final Object[] arguments; | |
41 private Object[] locals; | |
42 private byte[] tags; | |
43 | |
44 DefaultVirtualFrame(FrameDescriptor descriptor, Object[] arguments) { | |
45 this.descriptor = descriptor; | |
46 this.arguments = arguments; | |
47 this.locals = new Object[descriptor.getSize()]; | |
48 Arrays.fill(locals, descriptor.getDefaultValue()); | |
49 this.tags = new byte[descriptor.getSize()]; | |
50 } | |
51 | |
52 @Override | |
53 public Object[] getArguments() { | |
54 return arguments; | |
55 } | |
56 | |
57 @Override | |
58 public MaterializedFrame materialize() { | |
59 return new DefaultMaterializedFrame(this); | |
60 } | |
61 | |
62 @Override | |
63 public Object getObject(FrameSlot slot) throws FrameSlotTypeException { | |
64 verifyGet(slot, FrameSlotKind.Object); | |
65 return locals[slot.getIndex()]; | |
66 } | |
67 | |
68 @Override | |
69 public void setObject(FrameSlot slot, Object value) { | |
70 verifySet(slot, FrameSlotKind.Object); | |
71 locals[slot.getIndex()] = value; | |
72 } | |
73 | |
74 @Override | |
75 public byte getByte(FrameSlot slot) throws FrameSlotTypeException { | |
76 verifyGet(slot, FrameSlotKind.Byte); | |
77 return (byte) locals[slot.getIndex()]; | |
78 } | |
79 | |
80 @Override | |
81 public void setByte(FrameSlot slot, byte value) { | |
82 verifySet(slot, FrameSlotKind.Byte); | |
83 locals[slot.getIndex()] = value; | |
84 } | |
85 | |
86 @Override | |
87 public boolean getBoolean(FrameSlot slot) throws FrameSlotTypeException { | |
88 verifyGet(slot, FrameSlotKind.Boolean); | |
89 return (boolean) locals[slot.getIndex()]; | |
90 } | |
91 | |
92 @Override | |
93 public void setBoolean(FrameSlot slot, boolean value) { | |
94 verifySet(slot, FrameSlotKind.Boolean); | |
95 locals[slot.getIndex()] = value; | |
96 } | |
97 | |
98 @Override | |
99 public int getInt(FrameSlot slot) throws FrameSlotTypeException { | |
100 verifyGet(slot, FrameSlotKind.Int); | |
101 return (int) locals[slot.getIndex()]; | |
102 } | |
103 | |
104 @Override | |
105 public void setInt(FrameSlot slot, int value) { | |
106 verifySet(slot, FrameSlotKind.Int); | |
107 locals[slot.getIndex()] = value; | |
108 } | |
109 | |
110 @Override | |
111 public long getLong(FrameSlot slot) throws FrameSlotTypeException { | |
112 verifyGet(slot, FrameSlotKind.Long); | |
113 return (long) locals[slot.getIndex()]; | |
114 } | |
115 | |
116 @Override | |
117 public void setLong(FrameSlot slot, long value) { | |
118 verifySet(slot, FrameSlotKind.Long); | |
119 locals[slot.getIndex()] = value; | |
120 } | |
121 | |
122 @Override | |
123 public float getFloat(FrameSlot slot) throws FrameSlotTypeException { | |
124 verifyGet(slot, FrameSlotKind.Float); | |
125 return (float) locals[slot.getIndex()]; | |
126 } | |
127 | |
128 @Override | |
129 public void setFloat(FrameSlot slot, float value) { | |
130 verifySet(slot, FrameSlotKind.Float); | |
131 locals[slot.getIndex()] = value; | |
132 } | |
133 | |
134 @Override | |
135 public double getDouble(FrameSlot slot) throws FrameSlotTypeException { | |
136 verifyGet(slot, FrameSlotKind.Double); | |
137 return (double) locals[slot.getIndex()]; | |
138 } | |
139 | |
140 @Override | |
141 public void setDouble(FrameSlot slot, double value) { | |
142 verifySet(slot, FrameSlotKind.Double); | |
143 locals[slot.getIndex()] = value; | |
144 } | |
145 | |
146 @Override | |
147 public FrameDescriptor getFrameDescriptor() { | |
148 return this.descriptor; | |
149 } | |
150 | |
151 @Override | |
152 public Object getValue(FrameSlot slot) { | |
153 int slotIndex = getSlotIndexChecked(slot); | |
154 return locals[slotIndex]; | |
155 } | |
156 | |
157 private int getSlotIndexChecked(FrameSlot slot) { | |
158 int slotIndex = slot.getIndex(); | |
159 if (slotIndex >= tags.length) { | |
160 if (!resize()) { | |
161 throw new IllegalArgumentException(String.format("The frame slot '%s' is not known by the frame descriptor.", slot)); | |
162 } | |
163 } | |
164 return slotIndex; | |
165 } | |
166 | |
167 private void verifySet(FrameSlot slot, FrameSlotKind accessKind) { | |
168 int slotIndex = getSlotIndexChecked(slot); | |
169 tags[slotIndex] = (byte) accessKind.ordinal(); | |
170 } | |
171 | |
172 private void verifyGet(FrameSlot slot, FrameSlotKind accessKind) throws FrameSlotTypeException { | |
173 int slotIndex = getSlotIndexChecked(slot); | |
174 byte tag = tags[slotIndex]; | |
175 if (accessKind == FrameSlotKind.Object ? tag != 0 : tag != accessKind.ordinal()) { | |
176 throw new FrameSlotTypeException(); | |
177 } | |
178 } | |
179 | |
180 private boolean resize() { | |
181 int oldSize = tags.length; | |
182 int newSize = descriptor.getSize(); | |
183 if (newSize > oldSize) { | |
184 locals = Arrays.copyOf(locals, newSize); | |
185 Arrays.fill(locals, oldSize, newSize, descriptor.getDefaultValue()); | |
186 tags = Arrays.copyOf(tags, newSize); | |
187 return true; | |
188 } | |
189 return false; | |
190 } | |
191 | |
192 private byte getTag(FrameSlot slot) { | |
193 int slotIndex = getSlotIndexChecked(slot); | |
194 return tags[slotIndex]; | |
195 } | |
196 | |
197 @Override | |
198 public boolean isObject(FrameSlot slot) { | |
199 return getTag(slot) == FrameSlotKind.Object.ordinal(); | |
200 } | |
201 | |
202 @Override | |
203 public boolean isByte(FrameSlot slot) { | |
204 return getTag(slot) == FrameSlotKind.Byte.ordinal(); | |
205 } | |
206 | |
207 @Override | |
208 public boolean isBoolean(FrameSlot slot) { | |
209 return getTag(slot) == FrameSlotKind.Boolean.ordinal(); | |
210 } | |
211 | |
212 @Override | |
213 public boolean isInt(FrameSlot slot) { | |
214 return getTag(slot) == FrameSlotKind.Int.ordinal(); | |
215 } | |
216 | |
217 @Override | |
218 public boolean isLong(FrameSlot slot) { | |
219 return getTag(slot) == FrameSlotKind.Long.ordinal(); | |
220 } | |
221 | |
222 @Override | |
223 public boolean isFloat(FrameSlot slot) { | |
224 return getTag(slot) == FrameSlotKind.Float.ordinal(); | |
225 } | |
226 | |
227 @Override | |
228 public boolean isDouble(FrameSlot slot) { | |
229 return getTag(slot) == FrameSlotKind.Double.ordinal(); | |
230 } | |
231 } |