From ecc5fd3f0bd1be9a74278912d5a15985410e32e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Sun, 19 Aug 2018 18:19:12 +0200 Subject: [PATCH] [lang] Add optimization level parameter to the SARL batch compiler. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Galland --- .../compiler/batch/OptimizationLevel.java | 83 +++++++++++++++++++ .../compiler/batch/SarlBatchCompiler.java | 23 +++++ 2 files changed, 106 insertions(+) create mode 100644 main/coreplugins/io.sarl.lang/src/io/sarl/lang/compiler/batch/OptimizationLevel.java diff --git a/main/coreplugins/io.sarl.lang/src/io/sarl/lang/compiler/batch/OptimizationLevel.java b/main/coreplugins/io.sarl.lang/src/io/sarl/lang/compiler/batch/OptimizationLevel.java new file mode 100644 index 0000000000..6b1b9decdb --- /dev/null +++ b/main/coreplugins/io.sarl.lang/src/io/sarl/lang/compiler/batch/OptimizationLevel.java @@ -0,0 +1,83 @@ +/* + * $Id$ + * + * SARL is an general-purpose agent programming language. + * More details on http://www.sarl.io + * + * Copyright (C) 2014-2018 the original authors or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.sarl.lang.compiler.batch; + +import com.google.common.base.Strings; + +/** Level of optimization of the Java code. + * + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + * @since 0.8 + */ +public enum OptimizationLevel { + + /** + * No optimization, debugging information included. + */ + G0, + + /** + * No optimization, no debugging information included. + */ + G1, + + /** + * Optimization, no debugging information included. + */ + G2; + + /** Replies the default optimization level. + * + * @return the default optimization level. + */ + public static OptimizationLevel getDefault() { + return G0; + } + + /** Parse the given case insensitive string for obtaining the optimization level. + * + * @param name the string to parse. + * @return the optimization level, or {@code null} if the string cannot be parsed. + */ + public static OptimizationLevel valueOfCaseInsensitive(String name) { + if (Strings.isNullOrEmpty(name)) { + return null; + } + try { + return valueOf(name.toUpperCase()); + } catch (Exception exception) { + return null; + } + } + + /** Replies the string representation of this optimization level. + * + * @return the string representation. + */ + public String getCaseInsensitiveName() { + return name().toLowerCase(); + } + +} diff --git a/main/coreplugins/io.sarl.lang/src/io/sarl/lang/compiler/batch/SarlBatchCompiler.java b/main/coreplugins/io.sarl.lang/src/io/sarl/lang/compiler/batch/SarlBatchCompiler.java index 903b7857c6..74a8c4d5f4 100644 --- a/main/coreplugins/io.sarl.lang/src/io/sarl/lang/compiler/batch/SarlBatchCompiler.java +++ b/main/coreplugins/io.sarl.lang/src/io/sarl/lang/compiler/batch/SarlBatchCompiler.java @@ -254,6 +254,8 @@ public boolean accept(File pathname) { private boolean reportInternalProblemsAsIssues; + private OptimizationLevel optimizationLevel; + /** Constructor the batch compiler. */ public SarlBatchCompiler() { @@ -305,6 +307,27 @@ public IJavaBatchCompiler getJavaCompiler() { return this.javaCompiler; } + /** Change the optimization level that should be applied to the generated Java byte code. + * + * @param level the optimization level. + * @since 0.8 + */ + public void setOptimizationLevel(OptimizationLevel level) { + this.optimizationLevel = level; + } + + /** Replies the optimization level that should be applied to the generated Java byte code. + * + * @return the optimization level. + * @since 0.8 + */ + public OptimizationLevel getOptimizationLevel() { + if (this.optimizationLevel == null) { + this.optimizationLevel = OptimizationLevel.getDefault(); + } + return this.optimizationLevel; + } + /** Change the flag that permits to report the compiler's internal problems as issues. * * @param reportAsIssues {@code true} if the internal errors are reported as issues.