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