annotate graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/GuardsTest.java @ 9345:0a8bf24d660a

Implemented improvements to the generated source code.
author Christian Humer <christian.humer@gmail.com>
date Fri, 26 Apr 2013 22:43:23 +0200
parents 8e3a1635cc9e
children 86d981cd8e22
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8596
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1 /*
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
4 *
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
8 *
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
13 * accompanied this code).
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
14 *
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
18 *
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
21 * questions.
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
22 */
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
23 package com.oracle.truffle.api.codegen.test;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
24
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
25 import static com.oracle.truffle.api.codegen.test.TestHelper.*;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
26
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
27 import org.junit.*;
9226
e27f125147d6 Fixed deprecated static imports to junit.
Christian Humer <christian.humer@gmail.com>
parents: 9223
diff changeset
28 import static org.junit.Assert.*;
8596
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
29
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
30 import com.oracle.truffle.api.codegen.*;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
31 import com.oracle.truffle.api.codegen.test.GuardsTestFactory.GlobalFlagGuardFactory;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
32 import com.oracle.truffle.api.codegen.test.GuardsTestFactory.InvocationGuardFactory;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
33 import com.oracle.truffle.api.codegen.test.TypeSystemTest.TestRootNode;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
34 import com.oracle.truffle.api.codegen.test.TypeSystemTest.ValueNode;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
35
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
36 @SuppressWarnings("unused")
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
37 public class GuardsTest {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
38
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
39 private static final Object NULL = new Object();
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
40
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
41 @Test
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
42 public void testGuardInvocations() {
9287
8e3a1635cc9e Implemented @NodeChild(executeWith={...}).
Christian Humer <christian.humer@gmail.com>
parents: 9226
diff changeset
43 TestRootNode<InvocationGuard> root = createRoot(InvocationGuardFactory.getInstance());
8596
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
44
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
45 assertEquals(Integer.MAX_VALUE, executeWith(root, Integer.MAX_VALUE - 1, 1));
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
46 assertEquals(1, InvocationGuard.specializedInvocations);
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
47 assertEquals(0, InvocationGuard.genericInvocations);
9345
0a8bf24d660a Implemented improvements to the generated source code.
Christian Humer <christian.humer@gmail.com>
parents: 9287
diff changeset
48 assertEquals(1, InvocationGuard.guardInvocations);
8596
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
49
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
50 assertEquals(42, executeWith(root, Integer.MAX_VALUE, 1));
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
51 assertEquals(1, InvocationGuard.specializedInvocations);
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
52 assertEquals(1, InvocationGuard.genericInvocations);
9345
0a8bf24d660a Implemented improvements to the generated source code.
Christian Humer <christian.humer@gmail.com>
parents: 9287
diff changeset
53 assertEquals(2, InvocationGuard.guardInvocations);
8596
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
54 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
55
9223
5f7f0d3e3638 Updated codegen tests to new codegen API.
Christian Humer <christian.humer@gmail.com>
parents: 8596
diff changeset
56 @NodeChildren({@NodeChild("value0"), @NodeChild("value1")})
5f7f0d3e3638 Updated codegen tests to new codegen API.
Christian Humer <christian.humer@gmail.com>
parents: 8596
diff changeset
57 public abstract static class InvocationGuard extends ValueNode {
8596
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
58
9345
0a8bf24d660a Implemented improvements to the generated source code.
Christian Humer <christian.humer@gmail.com>
parents: 9287
diff changeset
59 static int guardInvocations = 0;
8596
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
60 static int specializedInvocations = 0;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
61 static int genericInvocations = 0;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
62
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
63 boolean guard(int value0, int value1) {
9345
0a8bf24d660a Implemented improvements to the generated source code.
Christian Humer <christian.humer@gmail.com>
parents: 9287
diff changeset
64 guardInvocations++;
8596
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
65 return value0 != Integer.MAX_VALUE;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
66 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
67
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
68 @Specialization(guards = "guard")
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
69 int doSpecialized(int value0, int value1) {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
70 specializedInvocations++;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
71 return value0 + value1;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
72 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
73
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
74 @Generic
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
75 int doGeneric(Object value0, Object value1) {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
76 genericInvocations++;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
77 return 42; // the generic answer to all questions
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
78 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
79 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
80
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
81 @Test
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
82 public void testGuardGlobal() {
9287
8e3a1635cc9e Implemented @NodeChild(executeWith={...}).
Christian Humer <christian.humer@gmail.com>
parents: 9226
diff changeset
83 TestRootNode<GlobalFlagGuard> root = createRoot(GlobalFlagGuardFactory.getInstance());
8596
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
84
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
85 assertEquals(42, executeWith(root, NULL));
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
86
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
87 GlobalFlagGuard.globalFlag = true;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
88 assertEquals(41, executeWith(root, NULL));
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
89
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
90 GlobalFlagGuard.globalFlag = false;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
91 assertEquals(42, executeWith(root, NULL));
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
92 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
93
9223
5f7f0d3e3638 Updated codegen tests to new codegen API.
Christian Humer <christian.humer@gmail.com>
parents: 8596
diff changeset
94 @NodeChild("expression")
5f7f0d3e3638 Updated codegen tests to new codegen API.
Christian Humer <christian.humer@gmail.com>
parents: 8596
diff changeset
95 public abstract static class GlobalFlagGuard extends ValueNode {
8596
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
96
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
97 static boolean globalFlag = false;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
98
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
99 static boolean globalFlagGuard() {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
100 return globalFlag;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
101 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
102
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
103 @Specialization(guards = "globalFlagGuard")
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
104 int doSpecialized(Object value0) {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
105 return 41;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
106 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
107
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
108 @Generic
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
109 int doGeneric(Object value0) {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
110 return 42; // the generic answer to all questions
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
111 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
112 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
113
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
114 }