1 /*
  2  * Copyright (c) 2024, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package jdk.incubator.code.analysis;
 27 
 28 import jdk.incubator.code.*;
 29 import jdk.incubator.code.op.CoreOp;
 30 import jdk.incubator.code.type.*;
 31 
 32 import java.util.*;
 33 
 34 /**
 35  * StringConcatTransformer is an {@link java.lang.reflect.code.OpTransformer} that removes concatenation operations
 36  * from blocks and replaces them with equivalent {@link java.lang.StringBuilder} operations. This provides a pathway
 37  * to remove {@link java.lang.reflect.code.op.CoreOp.ConcatOp} for easier lowering to Bytecode.
 38  */
 39 public final class StringConcatTransformer implements OpTransformer {
 40 
 41     private static final JavaType J_L_STRING_BUILDER = JavaType.type(StringBuilder.class);
 42     private static final MethodRef SB_TO_STRING_REF = MethodRef.method(
 43             J_L_STRING_BUILDER, "toString", JavaType.J_L_STRING);
 44 
 45     public StringConcatTransformer() {}
 46 
 47     @Override
 48     public Block.Builder apply(Block.Builder block, Op op) {
 49         switch (op) {
 50             case CoreOp.ConcatOp cz when isRootConcat(cz) -> {
 51                 // Create a string builder and build by traversing tree of operands
 52                 Op.Result builder = block.apply(CoreOp._new(ConstructorRef.constructor(J_L_STRING_BUILDER)));
 53                 buildFromTree(block, builder, cz);
 54                 // Convert to string
 55                 Value s = block.op(CoreOp.invoke(SB_TO_STRING_REF, builder));
 56                 block.context().mapValue(cz.result(), s);
 57             }
 58             case CoreOp.ConcatOp _ -> {
 59                 // Process later when building from root concat
 60             }
 61             default -> block.op(op);
 62         }
 63         return block;
 64     }
 65 
 66     static boolean isRootConcat(CoreOp.ConcatOp cz) {
 67         // Root of concat tree, zero uses, two or more uses,
 68         // or one use that is not a subsequent concat op
 69         Set<Op.Result> uses = cz.result().uses();
 70         return uses.size() != 1 || !(uses.iterator().next().op() instanceof CoreOp.ConcatOp);
 71     }
 72 
 73     static void buildFromTree(Block.Builder block, Op.Result builder, CoreOp.ConcatOp cz) {
 74         // Process concat op's operands from left to right
 75         buildFromTree(block, builder, cz.operands().get(0));
 76         buildFromTree(block, builder, cz.operands().get(1));
 77     }
 78 
 79     static void buildFromTree(Block.Builder block, Op.Result builder, Value v) {
 80         if (v instanceof Op.Result r &&
 81                 r.op() instanceof CoreOp.ConcatOp cz &&
 82                 r.uses().size() == 1) {
 83             // Node of tree, recursively traverse the operands
 84             buildFromTree(block, builder, cz);
 85         } else {
 86             // Leaf of tree, append value to builder
 87             // Note leaf can be the result of a ConcatOp with multiple uses
 88             block.op(append(block, builder, block.context().getValue(v)));
 89         }
 90     }
 91 
 92     private static Op append(Block.Builder block, Value builder, Value arg) {
 93         // Check if we need to widen unsupported integer types in the StringBuilder API
 94         // Strings are fed in as-is, everything else given as an Object.
 95         TypeElement type = arg.type();
 96         if (type instanceof PrimitiveType) {
 97             //Widen Short and Byte to Int.
 98             if (type.equals(JavaType.BYTE) || type.equals(JavaType.SHORT)) {
 99                 arg = block.op(CoreOp.conv(JavaType.INT, arg));
100                 type = JavaType.INT;
101             }
102         } else if (!type.equals(JavaType.J_L_STRING)){
103             type = JavaType.J_L_OBJECT;
104         }
105 
106         MethodRef methodDesc = MethodRef.method(J_L_STRING_BUILDER, "append", J_L_STRING_BUILDER, type);
107         return CoreOp.invoke(methodDesc, builder, arg);
108     }
109 
110 
111 }