1 /*
2  * Copyright (c) 2012, 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 package vm.compiler.coverage.parentheses;
24 
25 import vm.compiler.coverage.parentheses.share.HotspotInstructionsExecutor;
26 import vm.share.options.Option;
27 import vm.share.options.Options;
28 import vm.share.options.OptionSupport;
29 
30 import nsk.share.Failure;
31 import nsk.share.Log;
32 import nsk.share.test.StressOptions;
33 
34 import vm.compiler.coverage.parentheses.share.InstructionSequence;
35 import vm.compiler.coverage.parentheses.share.TinyInstructionsExecutor;
36 import vm.compiler.coverage.parentheses.share.generation.RandomInstructionsGenerator;
37 
38 import java.io.IOException;
39 
40 public class Parentheses {
41 
42     private Log log;
43 
44     @Option(name = "iterations", default_value = "100", description = "number of iterations")
45     int iterations = 2000;
46 
47     @Option(name = "maxStackDepth", default_value = "100",
48         description = "maximal stack depth that can be required by generated instruction sequence")
49     int maxStackDepth = 100;
50 
51     @Options
52     StressOptions stressOptions = new StressOptions();
53 
54     @Option(name = "verbose", default_value = "false", description = "verbose mode")
55     boolean verbose;
56 
57     @Option(name = "loadFrom", default_value = "", description = "path to file that contains instruction sequence")
58     String loadFrom = "";
59 
60     @Option(name = "saveTo", default_value = "parentheses",
61         description = "path to file in which will be stored instruction sequence if errors will be occur")
62     String saveTo = "saveTo";
63 
main(String[] args)64     public static void main(String[] args) throws Exception {
65         Parentheses test = new Parentheses();
66         OptionSupport.setup(test, args);
67         test.run();
68     }
69 
run()70     public void run() throws IOException, ReflectiveOperationException {
71 
72         log = new Log(System.out, verbose);
73 
74         InstructionSequence instructionSequence = null;
75         for (int i = 0; i < iterations * stressOptions.getIterationsFactor(); i++) {
76             log.display("Iteration " + i);
77             if (loadFrom.isEmpty()) {
78                 log.display("generating instructions list");
79                 instructionSequence = new RandomInstructionsGenerator(maxStackDepth).generate();
80             } else {
81                 if (instructionSequence == null) {
82                     log.display("loading instructions list from file: " + loadFrom);
83                     instructionSequence = InstructionSequence.fromFile(loadFrom);
84                 }
85             }
86 
87             log.display("executing instructions");
88 
89             TinyInstructionsExecutor tinyVM = new TinyInstructionsExecutor(instructionSequence.getMaxStackDepth());
90             int tinyRes = tinyVM.execute(instructionSequence.getInstructions());
91 
92             HotspotInstructionsExecutor hotspot = new HotspotInstructionsExecutor(instructionSequence.getMaxStackDepth());
93             int hotspotRes = hotspot.execute(instructionSequence.getInstructions());
94 
95             if (tinyRes != hotspotRes) {
96                 log.complain("Incorrect results of InstructionsExecutor instructions computations");
97                 log.complain("instructions:");
98                 log.complain(instructionSequence.toString());
99                 log.complain("TinyInstructionsExecutor result: " + tinyRes);
100                 log.complain("HotspotInstructionsExecutor result: " + hotspotRes);
101                 log.complain("Instruction sequence was written to file: " + saveTo);
102                 instructionSequence.saveToFile(saveTo);
103                 throw new Failure("Incorrect results of InstructionsExecutor instructions computations");
104             } else {
105                 log.display("TinyInstructionsExecutor result: " + tinyRes);
106                 log.display("HotspotInstructionsExecutor result: " + hotspotRes);
107                 log.display("");
108             }
109         }
110 
111     }
112 }
113