Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FinalFieldTest.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>Using Final Fields in Node Classes</h3> | |
33 * | |
34 * <p> | |
35 * The usage of final fields in node classes is highly encouraged. It is beneficial for performance to declare every | |
36 * field that is not pointing to a child node as final. This gives the Truffle runtime an increased opportunity to | |
37 * optimize this node. | |
38 * </p> | |
39 * | |
40 * <p> | |
41 * If a node has a value which may change at run time, but will rarely do so, it is recommended to speculate on the | |
42 * field being final. This involves starting executing with a node where this field is final and only if this | |
43 * turns out to be no longer the case, the node is replaced with an alternative implementation of the operation (see | |
44 * {@link ReplaceTest}). | |
45 * </p> | |
46 * | |
47 * <p> | |
48 * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.ReplaceTest}. | |
49 * </p> | |
50 */ | |
51 public class FinalFieldTest { | |
52 | |
53 @Test | |
54 public void test() { | |
55 TruffleRuntime runtime = Truffle.getRuntime(); | |
56 TestRootNode rootNode = new TestRootNode(new TestChildNode[]{new TestChildNode(20), new TestChildNode(22)}); | |
57 CallTarget target = runtime.createCallTarget(rootNode); | |
58 Object result = target.call(); | |
59 Assert.assertEquals(42, result); | |
60 } | |
61 | |
62 class TestRootNode extends RootNode { | |
63 | |
64 @Children TestChildNode[] children; | |
65 | |
66 public TestRootNode(TestChildNode[] children) { | |
67 this.children = adoptChildren(children); | |
68 } | |
69 | |
70 @Override | |
71 public Object execute(VirtualFrame frame) { | |
72 int sum = 0; | |
73 for (int i = 0; i < children.length; ++i) { | |
74 sum += children[i].execute(); | |
75 } | |
76 return sum; | |
77 } | |
78 } | |
79 | |
80 class TestChildNode extends Node { | |
81 private final int value; | |
82 | |
83 public TestChildNode(int value) { | |
84 this.value = value; | |
85 } | |
86 | |
87 public int execute() { | |
88 return value; | |
89 } | |
90 } | |
91 } | |
92 |