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 experiments; 27 28 import java.lang.reflect.Method; 29 import jdk.incubator.code.CopyContext; 30 import jdk.incubator.code.Op; 31 import jdk.incubator.code.OpTransformer; 32 import jdk.incubator.code.TypeElement; 33 import jdk.incubator.code.Value; 34 import jdk.incubator.code.CodeReflection; 35 import jdk.incubator.code.dialect.java.JavaOp; 36 import jdk.incubator.code.dialect.java.JavaType; 37 38 import java.util.ArrayList; 39 import java.util.List; 40 41 public class DNA { 42 static int myFunc(int i) { 43 return 0; 44 } 45 46 @CodeReflection 47 public static void addMul(int add, int mul) { 48 int len = myFunc(add); 49 } 50 51 public static class DNAOp extends Op { // externalized 52 private final TypeElement type; 53 54 DNAOp(String opName, TypeElement type, List<Value> operands) { 55 super(opName, operands); 56 this.type = type; 57 } 58 59 @Override 60 public Op transform(CopyContext copyContext, OpTransformer opTransformer) { 61 throw new IllegalStateException("in transform"); 62 // return null; 63 } 64 65 66 @Override 67 public TypeElement resultType() { 68 System.out.println("in result type"); 69 return type; 70 } 71 } 72 73 74 static public void main(String[] args) throws Exception { 75 Method method = DNA.class.getDeclaredMethod("addMul", int.class, int.class); 76 var funcOp = Op.ofMethod(method).get(); 77 var transformed = funcOp.transform((builder, op) -> { 78 CopyContext cc = builder.context(); 79 if (op instanceof JavaOp.InvokeOp invokeOp) { 80 // List<Value> operands = new ArrayList<>(); 81 //builder.op(new DNAOp("dna", JavaType.INT, operands)); 82 List<Value> inputOperands = invokeOp.operands(); 83 List<Value> outputOperands = cc.getValues(inputOperands); 84 Op.Result inputResult = invokeOp.result(); 85 Op.Result outputResult = builder.op(new DNAOp("dna", JavaType.INT, outputOperands)); 86 cc.mapValue(inputResult, outputResult); 87 } else { 88 builder.op(op); 89 } 90 return builder; 91 }); 92 93 94 System.out.println(transformed.toText()); 95 96 } 97 } 98