Path: blob/master/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/DirectiveWriter.java
41161 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.compilercontrol.share.scenario;2425import compiler.compilercontrol.share.JSONFile;26import compiler.compilercontrol.share.method.MethodDescriptor;2728import java.util.List;2930/**31* Simple directive file writer.32*/33public class DirectiveWriter implements AutoCloseable {34private final JSONFile jsonFile;3536/**37* Builds directive file for the given name38*39* @param fileName name the file to be created40*/41public DirectiveWriter(String fileName) {42jsonFile = new JSONFile(fileName);43}4445/**46* Emits match block with a given methods47*48* @param methods methods used for the match49* @return this DirectiveWriter instance50*/51public DirectiveWriter match(String... methods) {52if (jsonFile.getElement() == null) {53write(JSONFile.Element.ARRAY);54}55write(JSONFile.Element.OBJECT);56write(JSONFile.Element.PAIR, "match");57writeMethods(methods);58return this;59}6061/**62* Emits match block with a given methods63*64* @param methodDescriptors method descriptors used for the match65* @return this DirectiveWriter instance66*/67public DirectiveWriter match(MethodDescriptor... methodDescriptors) {68String[] methods = new String[methodDescriptors.length];69for (int i = 0; i < methodDescriptors.length; i++) {70methods[i] = methodDescriptors[i].getString();71}72match(methods);73return this;74}7576/**77* Emits inline block with a given methods to be inlined or not.78* Each method should be prepended with + or - to show if it should be79* inlined or not.80*81* @param methods methods used for the inline82* @return this DirectiveWriter instance83*/84public DirectiveWriter inline(String... methods) {85write(JSONFile.Element.PAIR, "inline");86writeMethods(methods);87return this;88}8990/**91* Emits inline block with a given methods to be inlined or not.92* Each method should be prepended with + or - to show if it should be93* inlined or not.94*95* @param methods methods used for the inline96* @return this DirectiveWriter instance97*/98public DirectiveWriter inline(List<String> methods) {99write(JSONFile.Element.PAIR, "inline");100writeMethods(methods.toArray(new String[methods.size()]));101return this;102}103104private void writeMethods(String[] methods) {105if (methods.length == 0) {106throw new IllegalArgumentException("ERROR: empty methods array");107}108if (methods.length > 1) {109write(JSONFile.Element.ARRAY);110for (String method : methods) {111write(JSONFile.Element.VALUE, "\"" + method + "\"");112}113end(); // ends array114} else {115write(JSONFile.Element.VALUE, "\"" + methods[0] + "\"");116}117}118119/**120* Emits compiler blocks that makes current match to be excluded or not121* from compilation with specified compiler122*123* @param compiler compiler to be excluded or null, for all124* @param exclude shows if compiler should be disabled for this match125* @return this DirectiveWriter instance126*/127public DirectiveWriter excludeCompile(Scenario.Compiler compiler,128boolean exclude) {129for (Scenario.Compiler comp : Scenario.Compiler.values()) {130emitCompiler(comp);131if (comp == compiler || compiler == null) {132option(Option.EXCLUDE, exclude);133} else {134// just make this block be enabled135option(Option.ENABLE, true);136}137end(); // end compiler block138}139return this;140}141142/**143* Emits compiler directive block144*145* @return this DirectiveWriter instance146*/147public DirectiveWriter emitCompiler(Scenario.Compiler compiler) {148write(JSONFile.Element.PAIR, compiler.name);149write(JSONFile.Element.OBJECT);150return this;151}152153@Override154public void close() {155jsonFile.close();156}157158/**159* Ends current object element. It could be either a160* c1 or c2 block, or a whole match block161*162* @return this DirectiveWriter instance163*/164public DirectiveWriter end() {165jsonFile.end();166return this;167}168169public DirectiveWriter write(JSONFile.Element element, String... value) {170jsonFile.write(element, value);171return this;172}173174/**175* Emits directive option with a given value176*177* @param option directive to be set178* @param value value of the directive179* @return this DirectiveWriter instance180*/181public DirectiveWriter option(Option option, Object value) {182write(JSONFile.Element.PAIR, option.string);183write(JSONFile.Element.VALUE, String.valueOf(value));184return this;185}186187/**188* Directive option list189*/190public enum Option {191PRINT_ASSEMBLY("PrintAssembly"),192LOG("Log"),193EXCLUDE("Exclude"),194ENABLE("Enable"),195INTRINSIC("ControlIntrinsic");196197public final String string;198199private Option(String directive) {200this.string = directive;201}202}203}204205206