changeset 54707:1cd0381faa0d intrinsics-project

refactoring: some simplifications
author vromero
date Thu, 31 Jan 2019 16:42:13 -0500
parents 1920d753c07f
children fa6233b78298
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/IntrinsicsVisitor.java src/jdk.compiler/share/classes/com/sun/tools/javac/intrinsics/IntrinsicProcessor.java src/jdk.compiler/share/classes/com/sun/tools/javac/intrinsics/Intrinsics.java
diffstat 3 files changed, 93 insertions(+), 132 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/IntrinsicsVisitor.java	Thu Jan 31 22:08:54 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/IntrinsicsVisitor.java	Thu Jan 31 16:42:13 2019 -0500
@@ -66,6 +66,7 @@
 
 import static com.sun.tools.javac.code.Flags.STATIC;
 import static com.sun.tools.javac.code.TypeTag.BOT;
+import static com.sun.tools.javac.tree.JCTree.Tag.SELECT;
 
 /**
  *  <p><b>This is NOT part of any supported API.
@@ -82,7 +83,6 @@
     private final Symtab syms;
     private final Resolve rs;
     private final Types types;
-    private final Log log;
     private final TreeMaker make;
     private final MethodHandles.Lookup lookup;
 
@@ -105,7 +105,6 @@
         syms = Symtab.instance(context);
         rs = Resolve.instance(context);
         types = Types.instance(context);
-        log = Log.instance(context);
         make = TreeMaker.instance(context);
         lookup = MethodHandles.lookup();
 
@@ -117,9 +116,7 @@
                 tree.type.tsym.packge().modle == syms.java_base) {
             return tree;
         }
-
         this.attrEnv = attrEnv;
-
         return translator.translate(tree);
     }
 
@@ -146,7 +143,7 @@
             try {
                 return constantDesc.resolveConstantDesc(lookup);
             } catch (ReflectiveOperationException ex) {
-                // Fall thru
+                // do nothing
             }
             return null;
         }
@@ -159,13 +156,8 @@
             return names.fromString(cd.packageName() + "." + cd.displayName());
         }
 
-        public JCTree translate(JCMethodInvocation tree) {
-            if (!(tree.meth instanceof JCFieldAccess)) {
-                return tree;
-            }
-
-            JCFieldAccess meth = (JCFieldAccess)tree.meth;
-            ClassDesc owner = ClassDesc.of(meth.sym.owner.toString());
+        public JCTree translate(JCMethodInvocation tree, JCTree.JCClassDecl currentClass) {
+            ClassDesc owner = ClassDesc.of(msym.owner.toString());
             String methodName = msym.name.toString();
             MethodTypeDesc methodTypeDesc = MethodTypeDesc.ofDescriptor(signature(msym.type));
             boolean isStatic = (msym.flags() & STATIC) != 0;
@@ -179,10 +171,12 @@
             ConstantDesc[] constantArgs = new ConstantDesc[allArgsSize];
 
             if (!isStatic) {
-                JCExpression selected = meth.selected;
-                allArgs[0] = selected;
-                argClassDescs[0] = ClassDesc.ofDescriptor(signature(selected.type));
-                constantArgs[0] = makeConstantDesc(selected.type);
+                JCExpression qualifierExpr = tree.meth.hasTag(SELECT) ?
+                        ((JCFieldAccess)tree.meth).selected :
+                        make.at(tree).This(currentClass.sym.erasure(types));
+                allArgs[0] = qualifierExpr;
+                argClassDescs[0] = ClassDesc.ofDescriptor(signature(qualifierExpr.type));
+                constantArgs[0] = makeConstantDesc(qualifierExpr.type);
             }
 
             for (int i = 0; i < argSize; i++) {
@@ -208,66 +202,66 @@
                     constantArgs
             );
 
-            if (result instanceof Result.None) {
-                return tree;
-            } else if (result instanceof Result.Ldc) {
-                Result.Ldc ldc = (Result.Ldc)result;
-                Object constant = resolveConstantDesc(ldc.constant());
+            switch (result.getKind()) {
+                case NONE: return tree;
+                case LDC:
+                    Result.Ldc ldc = (Result.Ldc)result;
+                    Object constant = resolveConstantDesc(ldc.constant());
 
-                return constant == null ? make.Literal(BOT, null).setType(syms.botType) :
-                        make.Literal(constant);
-            } else if (result instanceof Result.Indy) {
-                Result.Indy indy = (Result.Indy)result;
-                DynamicCallSiteDesc callSite = indy.indy();
-                String invocationName = callSite.invocationName();
-                DirectMethodHandleDesc bootstrapMethod = (DirectMethodHandleDesc)callSite.bootstrapMethod();
-                ClassDesc ownerClass = bootstrapMethod.owner();
-                String bootstrapName = bootstrapMethod.methodName();
-                List<Object> staticArgs = List.nil();
+                    return constant == null ? make.Literal(BOT, null).setType(syms.botType) :
+                            make.Literal(constant);
+                case INDY:
+                    Result.Indy indy = (Result.Indy)result;
+                    DynamicCallSiteDesc callSite = indy.indy();
+                    String invocationName = callSite.invocationName();
+                    DirectMethodHandleDesc bootstrapMethod = (DirectMethodHandleDesc)callSite.bootstrapMethod();
+                    ClassDesc ownerClass = bootstrapMethod.owner();
+                    String bootstrapName = bootstrapMethod.methodName();
+                    List<Object> staticArgs = List.nil();
 
-                for (ConstantDesc constantDesc : callSite.bootstrapArgs()) {
-                    staticArgs = staticArgs.append(resolveConstantDesc(constantDesc));
-                }
+                    for (ConstantDesc constantDesc : callSite.bootstrapArgs()) {
+                        staticArgs = staticArgs.append(resolveConstantDesc(constantDesc));
+                    }
 
-                List<JCExpression> indyArgs = List.nil();
+                    List<JCExpression> indyArgs = List.nil();
 
-                for (int i : indy.args()) {
-                    indyArgs = indyArgs.append(allArgs[i]);
-                }
+                    for (int i : indy.args()) {
+                        indyArgs = indyArgs.append(allArgs[i]);
+                    }
 
-                List<Type> argTypes = List.nil();
+                    List<Type> argTypes = List.nil();
 
-                for (JCExpression arg : indyArgs) {
-                    if (arg.type == syms.botType) {
-                        argTypes = argTypes.append(syms.objectType);
-                    } else {
-                        argTypes = argTypes.append(arg.type);
+                    for (JCExpression arg : indyArgs) {
+                        if (arg.type == syms.botType) {
+                            argTypes = argTypes.append(syms.objectType);
+                        } else {
+                            argTypes = argTypes.append(arg.type);
+                        }
                     }
-                }
 
-                Type returnType = msym.type.getReturnType();
-                MethodType indyType = new MethodType(argTypes, returnType,  List.nil(), syms.methodClass);
-                ClassSymbol classSymbol = syms.enterClass(msym.packge().modle, fullName(ownerClass));
+                    Type returnType = msym.type.getReturnType();
+                    MethodType indyType = new MethodType(argTypes, returnType,  List.nil(), syms.methodClass);
+                    ClassSymbol classSymbol = syms.enterClass(msym.packge().modle, fullName(ownerClass));
 
-                return makeDynamicCall(
-                        new SimpleDiagnosticPosition(tree.pos),
-                        classSymbol.type,
-                        names.fromString(bootstrapName),
-                        staticArgs,
-                        indyType,
-                        indyArgs,
-                        names.fromString(invocationName)
-                );
-            } else {
-                assert false : "tryIntrinsifyMethod result unknown";
+                    return makeDynamicCall(
+                            new SimpleDiagnosticPosition(tree.pos),
+                            classSymbol.type,
+                            names.fromString(bootstrapName),
+                            staticArgs,
+                            indyType,
+                            indyArgs,
+                            names.fromString(invocationName)
+                    );
+                    default:
+                        throw new AssertionError("tryIntrinsifyMethod result unknown");
             }
-
-            return tree;
         }
     }
 
     Translator translator = new Translator();
     class Translator extends TreeTranslator {
+        JCTree.JCClassDecl currentClass;
+
         @Override
         public void visitApply(JCMethodInvocation tree) {
             super.visitApply(tree);
@@ -282,7 +276,18 @@
 
             if (attr != null) {
                 TransformIntrinsic transform = new TransformIntrinsic(msym);
-                result = transform.translate(tree);
+                result = transform.translate(tree, currentClass);
+            }
+        }
+
+        @Override
+        public void visitClassDef(JCTree.JCClassDecl tree) {
+            JCTree.JCClassDecl previousClass = currentClass;
+            try {
+                currentClass = tree;
+                super.visitClassDef(tree);
+            } finally {
+                currentClass = previousClass;
             }
         }
     }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/intrinsics/IntrinsicProcessor.java	Thu Jan 31 22:08:54 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/intrinsics/IntrinsicProcessor.java	Thu Jan 31 16:42:13 2019 -0500
@@ -37,60 +37,42 @@
  *  deletion without notice.</b>
  */
 public interface IntrinsicProcessor {
-    /**
-     *  <p><b>This is NOT part of any supported API.
-     *  If you write code that depends on this, you do so at your own risk.
-     *  This code and its internal interfaces are subject to change or
-     *  deletion without notice.</b>
-     */
     public interface Result {
-        /**
-         *  <p><b>This is NOT part of any supported API.
-         *  If you write code that depends on this, you do so at your own risk.
-         *  This code and its internal interfaces are subject to change or
-         *  deletion without notice.</b>
-         */
-        public class None implements Result {
+        public enum ResultKind {
+            NONE, LDC, INDY
         }
 
-        /**
-         *  <p><b>This is NOT part of any supported API.
-         *  If you write code that depends on this, you do so at your own risk.
-         *  This code and its internal interfaces are subject to change or
-         *  deletion without notice.</b>
-         */
+        public abstract ResultKind getKind();
+
+        public class None implements Result {
+            @Override
+            public ResultKind getKind() {
+                return ResultKind.NONE;
+            }
+        }
+
         public class Ldc implements Result {
             private final ConstantDesc constant;
 
-            /**
-             * <p><b>This is NOT part of any supported API.</b>
-             * @param constant  constant value result
-             */
             public Ldc(ConstantDesc constant) {
                 this.constant = constant;
             }
 
-            /**
-             * <p><b>This is NOT part of any supported API.</b>
-             * @return constant result descriptor
-             */
             public ConstantDesc constant() {
                 return constant;
             }
+
+            @Override
+            public ResultKind getKind() {
+                return ResultKind.LDC;
+            }
         }
 
-        /**
-         *  <p><b>This is NOT part of any supported API.
-         *  If you write code that depends on this, you do so at your own risk.
-         *  This code and its internal interfaces are subject to change or
-         *  deletion without notice.</b>
-         */
         public class Indy implements Result {
             private final DynamicCallSiteDesc indy;
             private final int[] args;
 
             /**
-             * <p><b>This is NOT part of any supported API.</b>
              * @param indy call site descriptor
              * @param args indices of call arguments
              */
@@ -100,7 +82,6 @@
             }
 
             /**
-             * <p><b>This is NOT part of any supported API.</b>
              * @param indy  call site descriptor
              * @param nArgs argument range, 0..nArgs
              */
@@ -112,7 +93,6 @@
             }
 
             /**
-             * <p><b>This is NOT part of any supported API.</b>
              * @return DynamicCallSiteDesc value
              */
             public DynamicCallSiteDesc indy() {
@@ -120,19 +100,22 @@
             }
 
             /**
-             * <p><b>This is NOT part of any supported API.</b>
              * @return indices of call arguments
              */
             public int[] args() {
                 return args.clone();
             }
+
+            @Override
+            public ResultKind getKind() {
+                return ResultKind.INDY;
+            }
         }
    }
 
     public void register();
 
     /**
-     * <p><b>This is NOT part of any supported API.</b>
      * @param ownerDesc       method owner
      * @param methodName      method name
      * @param methodType      method type descriptor
@@ -148,4 +131,3 @@
                                 ClassDesc[] argClassDescs,
                                 ConstantDesc[] constantArgs);
 }
-
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/intrinsics/Intrinsics.java	Thu Jan 31 22:08:54 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/intrinsics/Intrinsics.java	Thu Jan 31 16:42:13 2019 -0500
@@ -111,24 +111,6 @@
 
     }
 
-    static Class<?> getClass(ClassDesc classDesc) {
-        try {
-            return (Class<?>)classDesc.resolveConstantDesc(LOOKUP);
-        } catch (ReflectiveOperationException ex) {
-            // Fall thru
-        }
-        return null;
-    }
-
-    static Object getConstant(ConstantDesc constantDesc) {
-        try {
-            return constantDesc.resolveConstantDesc(LOOKUP);
-        } catch (ReflectiveOperationException ex) {
-            // Fall thru
-        }
-        return null;
-    }
-
     static Object getConstant(ClassDesc classDesc, ConstantDesc constantDesc) {
         try {
             Object constant = constantDesc.resolveConstantDesc(LOOKUP);
@@ -160,19 +142,12 @@
                                  ConstantDesc[] constantDescs,
                                  boolean skipReceiver) {
         int length = constantDescs.length;
-        if (skipReceiver) {
-            Object[] constants = new Object[length - 1];
-            for (int i = 1; i < length; i++) {
-                constants[i - 1] = getConstant(classDescs[i], constantDescs[i]);
-            }
-            return constants;
-        } else {
-            Object[] constants = new Object[length];
-            for (int i = 0; i < length; i++) {
-                constants[i] = getConstant(classDescs[i], constantDescs[i]);
-            }
-            return constants;
+        Object[] constants = skipReceiver ? new Object[length - 1] : new Object[length];
+        int offset = skipReceiver ? 1 : 0;
+        for (int i = offset; i < length; i++) {
+            constants[i - offset] = getConstant(classDescs[i], constantDescs[i]);
         }
+        return constants;
     }
 
     static boolean isAllConstants(ConstantDesc[] constantDescs, boolean skipReceiver) {
@@ -196,7 +171,6 @@
     }
 
     /**
-     * <p><b>This is NOT part of any supported API.</b>
      * @param ownerDesc       method owner
      * @param methodName      method name
      * @param methodType      method type descriptor