1 package hat;
  2 
  3 /*
  4  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 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 import org.testng.annotations.Test;
 27 
 28 import jdk.incubator.code.CopyContext;
 29 import jdk.incubator.code.OpTransformer;
 30 import jdk.incubator.code.TypeElement;
 31 import jdk.incubator.code.Value;
 32 import jdk.incubator.code.dialect.core.CoreOp;
 33 import jdk.incubator.code.dialect.java.JavaOp;
 34 import jdk.incubator.code.Op;
 35 
 36 import java.lang.reflect.Method;
 37 import jdk.incubator.code.dialect.java.JavaType;
 38 import jdk.incubator.code.CodeReflection;
 39 import java.util.List;
 40 
 41 
 42 /*
 43  * @test
 44  * @run testng hat.CustomOpTest
 45  */
 46 
 47 public class CustomOpTest {
 48 
 49 
 50     static int myFunc(int i) {
 51         return 0;
 52     }
 53 
 54     @CodeReflection
 55     public static void addMul(int add, int mul) {
 56         int len = myFunc(add);
 57     }
 58 
 59     public static class DNAOp extends Op { // externalized
 60         private final TypeElement type;
 61 
 62         DNAOp(String opName, TypeElement type, List<Value> operands) {
 63             super(opName, operands);
 64             this.type = type;
 65         }
 66 
 67         @Override
 68         public Op transform(CopyContext copyContext, OpTransformer opTransformer) {
 69             throw new IllegalStateException("in transform");
 70 
 71         }
 72 
 73 
 74         @Override
 75         public TypeElement resultType() {
 76             System.out.println("in result type");
 77             return type;
 78         }
 79     }
 80 
 81     @Test
 82     public void testDNAOp() throws NoSuchMethodException {
 83         Method method = CustomOpTest.class.getDeclaredMethod("addMul", int.class, int.class);
 84         CoreOp.FuncOp funcOp = Op.ofMethod(method).get();
 85         var transformed = funcOp.transform((builder, op) -> {
 86             CopyContext cc = builder.context();
 87             if (op instanceof JavaOp.InvokeOp invokeOp) {
 88                 List<Value> inputOperands = invokeOp.operands();
 89                 List<Value> outputOperands = cc.getValues(inputOperands);
 90                 Op.Result inputResult = invokeOp.result();
 91                 Op.Result outputResult = builder.op(new DNAOp("dna", JavaType.INT, outputOperands));
 92                 cc.mapValue(inputResult, outputResult);
 93             } else {
 94                 builder.op(op);
 95             }
 96             return builder;
 97         });
 98 
 99         System.out.println(transformed.toText());
100     }
101 
102 
103 }