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.op.CoreOp;
35 import jdk.incubator.code.type.JavaType;
36 import jdk.incubator.code.CodeReflection;
37 import java.util.ArrayList;
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 TypeElement type;
52 
53         DNAOp(String opName, TypeElement type, List<Value> operands) {
54             super(opName, operands);
55             this.type = type;
56         }
57 
58         @Override
59         public Op transform(CopyContext copyContext, OpTransformer opTransformer) {
60             throw new IllegalStateException("in transform");
61             //  return null;
62         }
63 
64 
65         @Override
66         public TypeElement resultType() {
67             System.out.println("in result type");
68             return type;
69         }
70     }
71 
72 
73     static public void main(String[] args) throws Exception {
74         Method method = DNA.class.getDeclaredMethod("addMul", int.class, int.class);
75         var funcOp = Op.ofMethod(method).get();
76         var transformed = funcOp.transform((builder, op) -> {
77             CopyContext cc = builder.context();
78             if (op instanceof CoreOp.InvokeOp invokeOp) {
79                // List<Value> operands = new ArrayList<>();
80                 //builder.op(new DNAOp("dna", JavaType.INT, operands));
81                 List<Value> inputOperands = invokeOp.operands();
82                 List<Value> outputOperands = cc.getValues(inputOperands);
83                 Op.Result inputResult = invokeOp.result();
84                 Op.Result outputResult = builder.op(new DNAOp("dna", JavaType.INT, outputOperands));
85                 cc.mapValue(inputResult, outputResult);
86             } else {
87                 builder.op(op);
88             }
89             return builder;
90         });
91 
92 
93         System.out.println(transformed.toText());
94 
95     }
96 }
97