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