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

fixed bug in InstanceOfSnippets - deoptimization action should be None for instanceofWithProfile snippet
author Doug Simon <doug.simon@oracle.com>
date Fri, 14 Jun 2013 15:01:20 +0200
parents 86d981cd8e22
children
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);
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
48
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
49 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
50 assertEquals(1, InvocationGuard.specializedInvocations);
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
51 assertEquals(1, InvocationGuard.genericInvocations);
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
52 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
53
9223
5f7f0d3e3638 Updated codegen tests to new codegen API.
Christian Humer <christian.humer@gmail.com>
parents: 8596
diff changeset
54 @NodeChildren({@NodeChild("value0"), @NodeChild("value1")})
5f7f0d3e3638 Updated codegen tests to new codegen API.
Christian Humer <christian.humer@gmail.com>
parents: 8596
diff changeset
55 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
56
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
57 static int specializedInvocations = 0;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
58 static int genericInvocations = 0;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
59
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
60 boolean guard(int value0, int value1) {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
61 return value0 != Integer.MAX_VALUE;
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
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
64 @Specialization(guards = "guard")
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
65 int doSpecialized(int value0, int value1) {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
66 specializedInvocations++;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
67 return value0 + value1;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
68 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
69
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
70 @Generic
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
71 int doGeneric(Object value0, Object value1) {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
72 genericInvocations++;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
73 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
74 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
75 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
76
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
77 @Test
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
78 public void testGuardGlobal() {
9287
8e3a1635cc9e Implemented @NodeChild(executeWith={...}).
Christian Humer <christian.humer@gmail.com>
parents: 9226
diff changeset
79 TestRootNode<GlobalFlagGuard> root = createRoot(GlobalFlagGuardFactory.getInstance());
8596
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 assertEquals(42, executeWith(root, NULL));
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
82
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
83 GlobalFlagGuard.globalFlag = true;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
84 assertEquals(41, executeWith(root, NULL));
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
85
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
86 GlobalFlagGuard.globalFlag = false;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
87 assertEquals(42, executeWith(root, NULL));
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
88 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
89
9223
5f7f0d3e3638 Updated codegen tests to new codegen API.
Christian Humer <christian.humer@gmail.com>
parents: 8596
diff changeset
90 @NodeChild("expression")
5f7f0d3e3638 Updated codegen tests to new codegen API.
Christian Humer <christian.humer@gmail.com>
parents: 8596
diff changeset
91 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
92
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
93 static boolean globalFlag = false;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
94
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
95 static boolean globalFlagGuard() {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
96 return globalFlag;
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
97 }
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 @Specialization(guards = "globalFlagGuard")
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
100 int doSpecialized(Object value0) {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
101 return 41;
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
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
104 @Generic
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
105 int doGeneric(Object value0) {
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
106 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
107 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
108 }
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
109
6ef9fc7375c7 Updated codegen tests for guards and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
110 }