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