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