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.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 import java.io.StringWriter;
 25 import java.lang.reflect.Field;
 26 import java.lang.reflect.Member;
 27 import java.lang.reflect.Method;
 28 import java.lang.reflect.code.*;
 29 import java.lang.reflect.code.op.ExtendedOp;
 30 import java.lang.reflect.code.parser.OpParser;
 31 import java.lang.reflect.code.writer.OpWriter;
 32 import java.lang.runtime.CodeReflection;
 33 
 34 import static java.lang.reflect.code.op.CoreOp._return;
 35 import static java.lang.reflect.code.op.CoreOp.func;
 36 import static java.lang.reflect.code.type.FunctionType.VOID;
 37 
 38 public class CodeReflectionTester {
 39 
 40     public static void main(String[] args) throws ReflectiveOperationException {
 41         if (args.length != 1) {
 42             System.err.println("Usage: CodeReflectionTester <classname>");
 43             System.exit(1);
 44         }
 45         Class<?> clazz = Class.forName(args[0]);
 46         for (Method m : clazz.getDeclaredMethods()) {
 47             check(m);
 48         }
 49         for (Field f : clazz.getDeclaredFields()) {
 50             check(f);
 51         }
 52     }
 53 
 54     static void check(Method method) throws ReflectiveOperationException {
 55         if (!method.isAnnotationPresent(CodeReflection.class)) return;
 56         Field field = method.getDeclaringClass().getDeclaredField(method.getName() + "$op");
 57         String found = canonicalizeModel(method, (String) field.get(null));
 58         IR ir = method.getAnnotation(IR.class);
 59         if (ir == null) {
 60             throw new AssertionError("No @IR annotation found on reflective method");
 61         }
 62         String expected = canonicalizeModel(method, ir.value());
 63         if (!found.equals(expected)) {
 64             throw new AssertionError(String.format("Bad IR\nFound:\n%s\n\nExpected:\n%s", found, expected));
 65         }
 66     }
 67 
 68     static void check(Field field) throws ReflectiveOperationException {
 69         IR ir = field.getAnnotation(IR.class);
 70         if (ir == null) return;
 71         if (field.getType().equals(Quoted.class)) {
 72             // transitional
 73             Quoted quoted = (Quoted) field.get(null);
 74             String found = canonicalizeModel(field, getModelOfQuotedOp(quoted));
 75             String expected = canonicalizeModel(field, ir.value());
 76             if (!found.equals(expected)) {
 77                 throw new AssertionError(String.format("Bad IR\nFound:\n%s\n\nExpected:\n%s", found, expected));
 78             }
 79         } else if (Quotable.class.isAssignableFrom(field.getType())) {
 80             Quotable quotable = (Quotable) field.get(null);
 81             String found = canonicalizeModel(field, getModelOfQuotedOp(quotable.quoted()));
 82             String expected = canonicalizeModel(field, ir.value());
 83             if (!found.equals(expected)) {
 84                 throw new AssertionError(String.format("Bad IR\nFound:\n%s\n\nExpected:\n%s", found, expected));
 85             }
 86         } else {
 87             throw new AssertionError("Field annotated with @IR should be of a quotable type (Quoted/Quotable)");
 88         }
 89     }
 90 
 91     // serializes dropping location information, parses, and then serializes, dropping location information
 92     static String canonicalizeModel(Member m, Op o) {
 93         return canonicalizeModel(m, serialize(o));
 94     }
 95 
 96     // parses, and then serializes, dropping location information
 97     static String canonicalizeModel(Member m, String d) {
 98         Op o;
 99         try {
100             o = OpParser.fromString(ExtendedOp.FACTORY, d).get(0);
101         } catch (Exception e) {
102             throw new IllegalStateException(m.toString(), e);
103         }
104         return serialize(o);
105     }
106 
107     // serializes, dropping location information
108     static String serialize(Op o) {
109         StringWriter w = new StringWriter();
110         OpWriter.writeTo(w, o, OpWriter.LocationOption.DROP_LOCATION);
111         return w.toString();
112     }
113 
114     static Op getModelOfQuotedOp(Quoted quoted) {
115         return func("f", VOID).body(fblock -> {
116             CopyContext cc = fblock.context();
117             for (Value cv : quoted.capturedValues().keySet()) {
118                 Block.Parameter p = fblock.parameter(cv.type());
119                 cc.mapValue(cv, p);
120             }
121 
122             Op qOp = quoted.op();
123             // Associate the quoted ops ancestor body's entry block
124             // with the function's entry block, thereby ensuring that
125             // captured values mapped to the function's parameters
126             // are reachable
127             cc.mapBlock(qOp.ancestorBody().entryBlock(), fblock);
128             fblock.op(qOp);
129 
130             fblock.op(_return());
131         });
132     }
133 }