comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultVirtualFrame.java @ 7267:a4b84ba6dc2e

Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 18 Dec 2012 15:33:55 +0100
parents
children dc3e86fd3be1
comparison
equal deleted inserted replaced
7259:494d99e07614 7267:a4b84ba6dc2e
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.
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.truffle.api.impl;
24
25 import java.util.*;
26
27 import com.oracle.truffle.api.*;
28 import com.oracle.truffle.api.frame.*;
29
30 final class DefaultVirtualFrame implements VirtualFrame {
31
32 private static final Object UNDEFINED_OBJECT = null;
33 private static final Boolean UNDEFINED_BOOLEAN = false;
34 private static final Integer UNDEFINED_INTEGER = 0;
35 private static final Float UNDEFINED_FLOAT = 0.0f;
36 private static final Long UNDEFINED_LONG = 0L;
37 private static final Double UNDEFINED_DOUBLE = 0.0d;
38
39 private final FrameDescriptor descriptor;
40 private final PackedFrame caller;
41 private final Arguments arguments;
42 private FrameVersion currentVersion;
43 protected Object[] locals;
44 protected Class[] tags;
45
46 DefaultVirtualFrame(FrameDescriptor descriptor, PackedFrame caller, Arguments arguments) {
47 this.descriptor = descriptor;
48 this.caller = caller;
49 this.arguments = arguments;
50 this.currentVersion = descriptor.getCurrentVersion();
51 this.locals = new Object[descriptor.getSize()];
52 // The tags are only needed for assertion checking, so initialize the field only when assertions are enabled
53 assert (this.tags = new Class[descriptor.getSize()]) != null;
54 }
55
56 @Override
57 public Arguments getArguments() {
58 return arguments;
59 }
60
61 @Override
62 public PackedFrame getCaller() {
63 return caller;
64 }
65
66 @Override
67 public PackedFrame pack() {
68 return new DefaultPackedFrame(this);
69 }
70
71 @Override
72 public MaterializedFrame materialize() {
73 return new DefaultMaterializedFrame(this);
74 }
75
76 @Override
77 public Object getObject(FrameSlot slot) {
78 return get(slot, Object.class, UNDEFINED_OBJECT);
79 }
80
81 @Override
82 public void setObject(FrameSlot slot, Object value) {
83 set(slot, Object.class, value);
84 }
85
86 @Override
87 public boolean getBoolean(FrameSlot slot) {
88 return (Boolean) get(slot, Float.class, UNDEFINED_BOOLEAN);
89 }
90
91 @Override
92 public void setBoolean(FrameSlot slot, boolean value) {
93 set(slot, Float.class, value);
94 }
95
96 @Override
97 public int getInt(FrameSlot slot) {
98 return (Integer) get(slot, Integer.class, UNDEFINED_INTEGER);
99 }
100
101 @Override
102 public void setInt(FrameSlot slot, int value) {
103 set(slot, Integer.class, value);
104 }
105
106 @Override
107 public long getLong(FrameSlot slot) {
108 return (Long) get(slot, Long.class, UNDEFINED_LONG);
109 }
110
111 @Override
112 public void setLong(FrameSlot slot, long value) {
113 set(slot, Long.class, value);
114 }
115
116 @Override
117 public float getFloat(FrameSlot slot) {
118 return (Float) get(slot, Float.class, UNDEFINED_FLOAT);
119 }
120
121 @Override
122 public void setFloat(FrameSlot slot, float value) {
123 set(slot, Float.class, value);
124 }
125
126 @Override
127 public double getDouble(FrameSlot slot) {
128 return (Double) get(slot, Double.class, UNDEFINED_DOUBLE);
129 }
130
131 @Override
132 public void setDouble(FrameSlot slot, double value) {
133 set(slot, Double.class, value);
134 }
135
136 private Object get(FrameSlot slot, Class< ? > accessType, Object defaultValue) {
137 Object value = locals[slot.getIndex()];
138 assert verifyGet(slot, accessType, value);
139 if (value == null) {
140 return defaultValue;
141 } else {
142 return value;
143 }
144 }
145
146 private boolean verifyGet(FrameSlot slot, Class< ? > accessType, Object value) {
147 assert descriptor.getSlots().get(slot.getIndex()) == slot;
148 Class< ? > tag = tags[slot.getIndex()];
149 if (value == null) {
150 assert tag == null || tag == Object.class;
151 } else {
152 assert tag == accessType : "Local variable " + slot + " was written with set" + tag.getSimpleName() + ", but is read with get" + accessType.getSimpleName();
153 }
154 return true;
155 }
156
157 private void set(FrameSlot slot, Class< ? > accessType, Object value) {
158 assert verifySet(slot, accessType, value);
159 locals[slot.getIndex()] = value;
160 }
161
162 private boolean verifySet(FrameSlot slot, Class< ? > accessType, Object value) {
163 assert descriptor.getSlots().get(slot.getIndex()) == slot;
164 tags[slot.getIndex()] = accessType;
165 assert accessType.isAssignableFrom(slot.getType()) : "Local variable " + slot + ": " + accessType + " is not assignable from " + slot.getType();
166 if (value == null) {
167 assert accessType == Object.class;
168 } else {
169 assert slot.getType().isAssignableFrom(value.getClass()) : "Local variable " + slot + ": " + slot.getType() + " is not assignable from " + value.getClass();
170 }
171 return true;
172 }
173
174 @Override
175 public void updateToLatestVersion() {
176 if (currentVersion.getNext() != null) {
177 doUpdateToLatestVersion();
178 }
179 }
180
181 private void doUpdateToLatestVersion() {
182 FrameVersion version = currentVersion;
183 while (version.getNext() != null) {
184 version = version.getNext();
185 if (version instanceof FrameVersion.TypeChange) {
186 ((FrameVersion.TypeChange) version).applyTransformation(this);
187 } else if (version instanceof FrameVersion.Resize) {
188 int newSize = ((FrameVersion.Resize) version).getNewSize();
189 locals = Arrays.copyOf(locals, newSize);
190 }
191 }
192 currentVersion = version;
193 }
194 }