1/*
2 * Copyright (c) 2007, 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// Checkstyle: stop
24
25package org.graalvm.compiler.jtt.hotpath;
26
27import org.junit.Test;
28
29import org.graalvm.compiler.jtt.JTTTest;
30
31/*
32 */
33public class HP_invoke01 extends JTTTest {
34
35    private static int sum;
36
37    public static int test(int count) {
38        sum = 0;
39        final Instruction[] instructions = new Instruction[]{new Instruction.Add(), new Instruction.Sub(), new Instruction.Mul(), new Instruction.Div()};
40        final Visitor v = new Visitor();
41        for (int i = 0; i < count; i++) {
42            instructions[i % 4].accept(v);
43        }
44        return sum;
45    }
46
47    public static abstract class Instruction {
48
49        public abstract void accept(Visitor v);
50
51        public static abstract class Binary extends Instruction {
52
53        }
54
55        public static class Add extends Binary {
56
57            @Override
58            public void accept(Visitor v) {
59                v.visit(this);
60            }
61        }
62
63        public static class Sub extends Binary {
64
65            @Override
66            public void accept(Visitor v) {
67                v.visit(this);
68            }
69        }
70
71        public static class Mul extends Binary {
72
73            @Override
74            public void accept(Visitor v) {
75                v.visit(this);
76            }
77        }
78
79        public static class Div extends Binary {
80
81            @Override
82            public void accept(Visitor v) {
83                v.visit(this);
84            }
85        }
86    }
87
88    @SuppressWarnings("unused")
89    public static class Visitor {
90
91        public void visit(Instruction.Add i) {
92            sum += 7;
93        }
94
95        public void visit(Instruction.Sub i) {
96            sum += 194127;
97        }
98
99        public void visit(Instruction.Mul i) {
100            sum += 18991;
101        }
102
103        public void visit(Instruction.Div i) {
104            sum += 91823;
105        }
106    }
107
108    @Test
109    public void run0() throws Throwable {
110        runTest("test", 40);
111    }
112
113    @Test
114    public void run1() throws Throwable {
115        runTest("test", 80);
116    }
117
118}
119