comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FrameTest.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 5e3d1a68664e
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.test;
24
25 import org.junit.*;
26
27 import com.oracle.truffle.api.*;
28 import com.oracle.truffle.api.frame.*;
29 import com.oracle.truffle.api.nodes.*;
30
31 /**
32 * <h3>Storing Values in Frame Slots</h3>
33 *
34 * <p>
35 * The frame is the preferred data structure for passing values between nodes. It can in particular be used for storing
36 * the values of local variables of the guest language. The {@link FrameDescriptor} represents the current structure of
37 * the frame. The method {@link FrameDescriptor#addFrameSlot(String, Class)} can be used to create predefined frame
38 * slots. The setter and getter methods in the {@link Frame} class can be used to access the current value of a
39 * particular frame slot.
40 * </p>
41 *
42 * <p>
43 * There are five primitive types for slots available: {@link java.lang.Boolean}, @{link java.lang.Integer},
44 * {@link java.lang.Long}, {@link java.lang.Float}, and {@link java.lang.Double}. It is encouraged to use those types
45 * whenever possible. Dynamically typed languages can speculate on the type of a value fitting into a primitive (see
46 * {@link FrameSlotTypeSpecializationTest}). When a frame slot is of one of those particular primitive types, its value
47 * may only be accessed with the repectively typed getter method ({@link Frame#getBoolean}, {@link Frame#getInt},
48 * {@link Frame#getLong}, {@link Frame#getFloat}, or {@link Frame#getDouble}) or setter method ({@link Frame#setBoolean},
49 * {@link Frame#setInt}, {@link Frame#setLong}, {@link Frame#setFloat}, or {@link Frame#setDouble}) in the
50 * {@link Frame} class.
51 * </p>
52 *
53 * <p>
54 * The next part of the Truffle API introduction is at
55 * {@link com.oracle.truffle.api.test.FrameSlotTypeSpecializationTest}.
56 * </p>
57 */
58 public class FrameTest {
59
60 @Test
61 public void test() {
62 TruffleRuntime runtime = Truffle.getRuntime();
63 FrameDescriptor frameDescriptor = new FrameDescriptor();
64 FrameSlot slot = frameDescriptor.addFrameSlot("localVar", Integer.class);
65 TestRootNode rootNode = new TestRootNode(new AssignLocal(slot), new ReadLocal(slot));
66 CallTarget target = runtime.createCallTarget(rootNode, frameDescriptor);
67 Object result = target.call();
68 Assert.assertEquals(42, result);
69 }
70
71 class TestRootNode extends RootNode {
72
73 @Child TestChildNode left;
74 @Child TestChildNode right;
75
76 public TestRootNode(TestChildNode left, TestChildNode right) {
77 this.left = adoptChild(left);
78 this.right = adoptChild(right);
79 }
80
81 @Override
82 public Object execute(VirtualFrame frame) {
83 return left.execute(frame) + right.execute(frame);
84 }
85 }
86
87 abstract class TestChildNode extends Node {
88 abstract int execute(VirtualFrame frame);
89 }
90
91 abstract class FrameSlotNode extends TestChildNode {
92 protected final FrameSlot slot;
93
94 public FrameSlotNode(FrameSlot slot) {
95 this.slot = slot;
96 }
97 }
98
99 class AssignLocal extends FrameSlotNode {
100 AssignLocal(FrameSlot slot) {
101 super(slot);
102 }
103
104 @Override
105 int execute(VirtualFrame frame) {
106 frame.setInt(slot, 42);
107 return 0;
108 }
109 }
110
111 class ReadLocal extends FrameSlotNode {
112 ReadLocal(FrameSlot slot) {
113 super(slot);
114 }
115
116 @Override
117 int execute(VirtualFrame frame) {
118 return frame.getInt(slot);
119 }
120 }
121 }
122