--- old/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java 2015-11-25 01:24:53.310906290 +0300 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java 2015-11-25 01:24:53.254906556 +0300 @@ -26,9 +26,7 @@ package com.sun.tools.javac.code; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.Set; import javax.lang.model.element.ElementVisitor; import javax.tools.JavaFileObject; @@ -39,7 +37,6 @@ import com.sun.tools.javac.code.Symbol.Completer; import com.sun.tools.javac.code.Symbol.CompletionFailure; import com.sun.tools.javac.code.Symbol.MethodSymbol; -import com.sun.tools.javac.code.Symbol.OperatorSymbol; import com.sun.tools.javac.code.Symbol.PackageSymbol; import com.sun.tools.javac.code.Symbol.TypeSymbol; import com.sun.tools.javac.code.Symbol.VarSymbol; @@ -50,7 +47,6 @@ import com.sun.tools.javac.code.Type.JCVoidType; import com.sun.tools.javac.code.Type.MethodType; import com.sun.tools.javac.code.Type.UnknownType; -import com.sun.tools.javac.jvm.ByteCodes; import com.sun.tools.javac.jvm.Target; import com.sun.tools.javac.util.Assert; import com.sun.tools.javac.util.Context; @@ -65,7 +61,6 @@ import static com.sun.tools.javac.code.Flags.*; import static com.sun.tools.javac.code.Kinds.Kind.*; -import static com.sun.tools.javac.jvm.ByteCodes.*; import static com.sun.tools.javac.code.TypeTag.*; /** A class that defines all predefined constants and operators @@ -148,6 +143,7 @@ */ public final Type objectType; public final Type objectsType; + public final Type voidClassType; public final Type classType; public final Type classLoaderType; public final Type stringType; @@ -193,6 +189,7 @@ public final Type autoCloseableType; public final Type trustMeType; public final Type lambdaMetafactory; + public final Type stringConcatFactory; public final Type repeatableType; public final Type documentedType; public final Type elementTypeType; @@ -416,6 +413,7 @@ objectType = enterClass("java.lang.Object"); objectsType = enterClass("java.util.Objects"); classType = enterClass("java.lang.Class"); + voidClassType = enterClass("java.lang.Void"); stringType = enterClass("java.lang.String"); stringBufferType = enterClass("java.lang.StringBuffer"); stringBuilderType = enterClass("java.lang.StringBuilder"); @@ -472,6 +470,7 @@ trustMeType = enterClass("java.lang.SafeVarargs"); nativeHeaderType = enterClass("java.lang.annotation.Native"); lambdaMetafactory = enterClass("java.lang.invoke.LambdaMetafactory"); + stringConcatFactory = enterClass("java.lang.invoke.StringConcatFactory"); functionalInterfaceType = enterClass("java.lang.FunctionalInterface"); synthesizeEmptyInterfaceIfMissing(autoCloseableType); @@ -479,6 +478,7 @@ synthesizeEmptyInterfaceIfMissing(serializableType); synthesizeEmptyInterfaceIfMissing(lambdaMetafactory); synthesizeEmptyInterfaceIfMissing(serializedLambdaType); + synthesizeEmptyInterfaceIfMissing(stringConcatFactory); synthesizeBoxTypeIfMissing(doubleType); synthesizeBoxTypeIfMissing(floatType); synthesizeBoxTypeIfMissing(voidType); --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java 2015-11-25 01:24:53.602904903 +0300 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java 2015-11-25 01:24:53.546905169 +0300 @@ -45,7 +45,6 @@ import static com.sun.tools.javac.code.Flags.*; import static com.sun.tools.javac.code.Kinds.Kind.*; -import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE; import static com.sun.tools.javac.code.TypeTag.*; import static com.sun.tools.javac.jvm.ByteCodes.*; import static com.sun.tools.javac.jvm.CRTFlags.*; @@ -124,6 +123,40 @@ debugCode = options.isSet("debugcode"); allowInvokedynamic = target.hasInvokedynamic() || options.isSet("invokedynamic"); allowBetterNullChecks = target.hasObjects(); + + { + String concat = options.get("stringConcat"); + if (target.hasStringConcatFactory()) { + if (concat == null) { + concat = "indyWithConstants"; + } + + switch (concat) { + case "inline": + allowIndyStringConcat = false; + indyStringConcatConstants = false; + break; + case "indy": + allowIndyStringConcat = true; + indyStringConcatConstants = false; + break; + case "indyWithConstants": + allowIndyStringConcat = true; + indyStringConcatConstants = true; + break; + default: + Assert.error("Unknown stringConcat: " + concat); + throw new IllegalStateException("Unknown stringConcat: " + concat); + } + } else { + if (concat != null && !"inline".equals(concat)) { + Assert.error("StringConcatFactory-based string concat is requested on a platform that does not support it."); + } + allowIndyStringConcat = false; + indyStringConcatConstants = false; + } + } + pool = new Pool(types); // ignore cldc because we cannot have both stackmap formats @@ -152,6 +185,8 @@ private final boolean debugCode; private final boolean allowInvokedynamic; private final boolean allowBetterNullChecks; + private final boolean allowIndyStringConcat; + private final boolean indyStringConcatConstants; /** Default limit of (approximate) size of finalizer to inline. * Zero means always use jsr. 100 or greater means never use @@ -1895,25 +1930,30 @@ OperatorSymbol operator = (OperatorSymbol) tree.operator; Item l; if (operator.opcode == string_add) { - // Generate code to make a string buffer - makeStringBuffer(tree.pos()); + if (allowIndyStringConcat) { + l = genExpr(tree.lhs, tree.lhs.type); + emitIndyStringConcat(tree); + } else { + // Generate code to make a string buffer + makeStringBuffer(tree.pos()); - // Generate code for first string, possibly save one - // copy under buffer - l = genExpr(tree.lhs, tree.lhs.type); - if (l.width() > 0) { - code.emitop0(dup_x1 + 3 * (l.width() - 1)); - } + // Generate code for first string, possibly save one + // copy under buffer + l = genExpr(tree.lhs, tree.lhs.type); + if (l.width() > 0) { + code.emitop0(dup_x1 + 3 * (l.width() - 1)); + } - // Load first string and append to buffer. - l.load(); - appendString(tree.lhs); + // Load first string and append to buffer. + l.load(); + appendString(tree.lhs); - // Append all other strings to buffer. - appendStrings(tree.rhs); + // Append all other strings to buffer. + appendStrings(tree.rhs); - // Convert buffer to string. - bufferToString(tree.pos()); + // Convert buffer to string. + bufferToString(tree.pos()); + } } else { // Generate code for first expression l = genExpr(tree.lhs, tree.lhs.type); @@ -2026,12 +2066,17 @@ public void visitBinary(JCBinary tree) { OperatorSymbol operator = (OperatorSymbol)tree.operator; if (operator.opcode == string_add) { - // Create a string buffer. - makeStringBuffer(tree.pos()); - // Append all strings to buffer. - appendStrings(tree); - // Convert buffer to string. - bufferToString(tree.pos()); + if (allowIndyStringConcat) { + // Emit indified version of String concat + emitIndyStringConcat(tree); + } else { + // Create a string buffer. + makeStringBuffer(tree.pos()); + // Append all strings to buffer. + appendStrings(tree); + // Convert buffer to string. + bufferToString(tree.pos()); + } result = items.makeStackItem(syms.stringType); } else if (tree.hasTag(AND)) { CondItem lcond = genCond(tree.lhs, CRT_FLOW_CONTROLLER); @@ -2066,6 +2111,211 @@ result = completeBinop(tree.lhs, tree.rhs, operator); } } + + /** + * Maximum number of slots for String Concat call. + * JDK's StringConcatFactory does not support more than that. + */ + private static final int MAX_INDY_CONCAT_ARG_SLOTS = 200; + private static final String TAG_ARG = "\u0001"; + private static final String TAG_CONST = "\u0002"; + + List collectStringsRecursive(JCTree tree, List res) { + tree = TreeInfo.skipParens(tree); + if (tree.hasTag(PLUS) && tree.type.constValue() == null) { + JCBinary op = (JCBinary) tree; + if (op.operator.kind == MTH && + ((OperatorSymbol) op.operator).opcode == string_add) { + return res + .appendList(collectStringsRecursive(op.lhs, res)) + .appendList(collectStringsRecursive(op.rhs, res)); + } + } + return res.append(tree); + } + + /** Handle the inline assignments, collect all subtrees */ + private void emitIndyStringConcat(JCAssignOp tree) { + List args = + List.nil() + .appendList(collectStringsRecursive(tree.lhs, List.nil())) + .appendList(collectStringsRecursive(tree.rhs, List.nil())); + emitIndyStringConcat(args, tree.type, tree.pos()); + } + + /** Handle the string_add operation, collect all subtrees */ + private void emitIndyStringConcat(JCBinary tree) { + List args = + List.nil() + .appendList(collectStringsRecursive(tree.lhs, List.nil())) + .appendList(collectStringsRecursive(tree.rhs, List.nil())); + emitIndyStringConcat(args, tree.type, tree.pos()); + } + + /** Emit the indy concat for all these arguments, possibly peeling along the way */ + private void emitIndyStringConcat(List args, Type type, DiagnosticPosition pos) { + int slots = 0; + int count = 0; + + // Need to peel, so that neither call has more than acceptable number + // of slots for the arguments. + ListBuffer cArgs = new ListBuffer<>(); + for (JCTree t : args) { + int needSlots = (t.type.getTag() == LONG || t.type.getTag() == DOUBLE) ? 2 : 1; + if (slots + needSlots >= MAX_INDY_CONCAT_ARG_SLOTS) { + emitIndyStringConcatOne(cArgs.toList(), type, pos); + cArgs.clear(); + slots = 0; + count++; + } + cArgs.add(t); + slots += needSlots; + } + + // Flush the tail slice + if (!cArgs.isEmpty()) { + emitIndyStringConcatOne(cArgs.toList(), type, pos); + count++; + } + + // More that one peel slice produced: concatenate the results + if (count > 1) { + emitIndyStringConcatMerge(count, type, pos); + } + } + + /** + * This code builds the recipe, static and dynamic arguments for calling JDK's + * StringConcatFactory. See the interface description there. + * + * We also bypass empty strings, because they have no meaning at this level. This + * captures the Java language trick to force String concat with e.g. ("" + int)-like + * expression. Down here, we already know we are in String concat business, and do + * not require these markers. + */ + private void emitIndyStringConcatOne(List args, Type type, DiagnosticPosition pos) { + Assert.check(!args.isEmpty(), "Arguments list is empty"); + + StringBuilder recipe = new StringBuilder(args.size()); + ListBuffer dynamicArgs = new ListBuffer<>(); + ListBuffer staticArgs = new ListBuffer<>(); + + if (indyStringConcatConstants) { + for (JCTree arg : args) { + Object constVal = arg.type.constValue(); + if ("".equals(constVal)) continue; + if (arg.type == syms.botType) { + // Concat the null into the recipe right away + recipe.append("null"); + } else if (constVal != null) { + // Concat the String representation of the constant, except + // for the case it contains special tags, which requires us + // to expose it as detached constant. + String a = arg.type.stringValue(); + if (a.contains(TAG_CONST) || a.contains(TAG_ARG)) { + recipe.append(TAG_CONST); + staticArgs.add(a); + } else { + recipe.append(a); + } + } else { + // Ordinary arguments come through the dynamic arguments. + recipe.append(TAG_ARG); + dynamicArgs.add(arg.type); + genExpr(arg, arg.type).load(); + } + } + } else { + for (JCTree arg : args) { + Object constVal = arg.type.constValue(); + if ("".equals(constVal)) continue; + if (arg.type == syms.botType) { + dynamicArgs.add(syms.voidClassType); + } else { + dynamicArgs.add(arg.type); + } + genExpr(arg, arg.type).load(); + } + } + + indyStringConcatDoCall(type, pos, recipe.toString(), staticArgs, dynamicArgs); + } + + /** Special version for concatenating the known number of known Strings */ + private void emitIndyStringConcatMerge(int count, Type type, DiagnosticPosition pos) { + Assert.check(count != 0, "Arguments list is empty"); + Assert.check(count <= MAX_INDY_CONCAT_ARG_SLOTS, "Too many arguments for concatenation"); + + // All arguments are assumed to be non-constant Strings + StringBuilder recipe = new StringBuilder(count); + ListBuffer argTypes = new ListBuffer<>(); + for (int c = 0; c < count; c++) { + argTypes.append(syms.stringType); + recipe.append(TAG_ARG); + } + + indyStringConcatDoCall(type, pos, recipe.toString(), new ListBuffer<>(), argTypes); + } + + /** Produce the actual invokedynamic call to StringConcatFactory */ + private void indyStringConcatDoCall(Type type, DiagnosticPosition pos, String recipe, ListBuffer staticArgs, ListBuffer dynamicArgTypes) { + MethodType indyType = new MethodType(dynamicArgTypes.toList(), + type, + List.nil(), + syms.methodClass); + + int prevPos = make.pos; + try { + make.at(pos); + + DynamicMethodSymbol dynSym; + + if (indyStringConcatConstants) { + ListBuffer constTypes = new ListBuffer<>(); + ListBuffer constants = new ListBuffer<>(); + for (Object t : staticArgs) { + constants.add(t); + constTypes.add(syms.stringType); + } + + List bsm_staticArgs = List.of(syms.methodHandleLookupType, + syms.stringType, + syms.methodTypeType) + .append(syms.stringType) + .appendList(constTypes); + + Symbol bsm = rs.resolveInternalMethod(pos, attrEnv, syms.stringConcatFactory, + names.makeConcatWithConstants, bsm_staticArgs, List.nil()); + + dynSym = new DynamicMethodSymbol(names.makeConcatWithConstants, + syms.noSymbol, + ClassFile.REF_invokeStatic, + (MethodSymbol)bsm, + indyType, + List.of(recipe).appendList(constants).toArray()); + } else { + List bsm_staticArgs = List.of(syms.methodHandleLookupType, + syms.stringType, + syms.methodTypeType); + + Symbol bsm = rs.resolveInternalMethod(pos, attrEnv, syms.stringConcatFactory, + names.makeConcat, bsm_staticArgs, List.nil()); + + dynSym = new DynamicMethodSymbol(names.makeConcat, + syms.noSymbol, + ClassFile.REF_invokeStatic, + (MethodSymbol)bsm, + indyType, + List.nil().toArray()); + } + + Item item = items.makeDynamicItem(dynSym); + item.invoke(); + } finally { + make.at(prevPos); + } + } + //where /** Make a new string buffer. */ --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Target.java 2015-11-25 01:24:53.906903456 +0300 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Target.java 2015-11-25 01:24:53.850903722 +0300 @@ -135,4 +135,10 @@ return hasInvokedynamic(); } + /** Does the target JDK contain StringConcatFactory class? + */ + public boolean hasStringConcatFactory() { + return compareTo(JDK1_9) >= 0; + } + } --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java 2015-11-25 01:24:54.770899350 +0300 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java 2015-11-25 01:24:54.714899616 +0300 @@ -179,6 +179,10 @@ public final Name altMetafactory; public final Name dollarThis; + // string concat + public final Name makeConcat; + public final Name makeConcatWithConstants; + public final Name.Table table; public Names(Context context) { @@ -316,6 +320,10 @@ lambda = fromString("lambda$"); metafactory = fromString("metafactory"); altMetafactory = fromString("altMetafactory"); + + // string concat + makeConcat = fromString("makeConcat"); + makeConcatWithConstants = fromString("makeConcatWithConstants"); } protected Name.Table createTable(Options options) { --- old/test/tools/javac/T5024091/T5024091.java 2015-11-25 01:24:55.062897961 +0300 +++ new/test/tools/javac/T5024091/T5024091.java 2015-11-25 01:24:55.006898227 +0300 @@ -3,7 +3,7 @@ * @bug 5024091 * @summary AssertionError shouldn't be thrown * @author Wei Tao - * @compile/fail/ref=T5024091.out -XDfailcomplete=java.lang.StringBuilder -XDdev -XDrawDiagnostics T5024091.java + * @compile/fail/ref=T5024091.out -XDfailcomplete=java.lang.StringBuilder -XDdev -XDrawDiagnostics -XDstringConcat=inline T5024091.java */ public class T5024091 { --- old/test/tools/javap/T6868539.java 2015-11-25 01:24:55.346896611 +0300 +++ new/test/tools/javap/T6868539.java 2015-11-25 01:24:55.290896877 +0300 @@ -58,10 +58,12 @@ throw new Error(errors + " found."); } + String notFound = " not found"; + void verify(String output, String... expects) { for (String expect: expects) { if (!output.matches("(?s).*" + expect + ".*")) - error(expect + " not found"); + error(expect + notFound); } } --- /dev/null 2015-11-23 01:27:16.425045507 +0300 +++ new/test/tools/javac/TestIndyStringConcat.java 2015-11-25 01:24:55.574895528 +0300 @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary Test that StringConcat is working for JDK >= 9 + * @compile -source 6 -target 6 TestIndyStringConcat.java + * @run main TestIndyStringConcat false + * @clean TestIndyStringConcat* + * @compile -source 7 -target 7 TestIndyStringConcat.java + * @run main TestIndyStringConcat false + * @clean TestIndyStringConcat* + * @compile -source 8 -target 8 TestIndyStringConcat.java + * @run main TestIndyStringConcat false + * @clean TestIndyStringConcat* + * @compile -XDstringConcat=inline -source 9 -target 9 TestIndyStringConcat.java + * @run main TestIndyStringConcat false + * @clean TestIndyStringConcat* + * @compile -XDstringConcat=indy -source 9 -target 9 TestIndyStringConcat.java + * @run main TestIndyStringConcat true + * @clean TestIndyStringConcat* + * @compile -XDstringConcat=indyWithConstants -source 9 -target 9 TestIndyStringConcat.java + * @run main TestIndyStringConcat true + */ +public class TestIndyStringConcat { + + private static class MyObject { + public String toString() { + throw new RuntimeException("Boyyaa"); + } + } + + class Inner { } + + public static void main(String[] args) { + boolean useIndyConcat = Boolean.valueOf(args[0]); + try { + String s = "Foo" + new MyObject(); + } catch (RuntimeException ex) { + boolean indifiedStringConcat = false; + ex.printStackTrace(); + for (StackTraceElement e : ex.getStackTrace()) { + if (e.getClassName().startsWith("java.lang.String$Concat") && + e.getMethodName().equals("concat")) { + indifiedStringConcat = true; + break; + } + } + if (indifiedStringConcat != useIndyConcat) { + throw new AssertionError(); + } + } + } +}