1 /*
   2  * Copyright (c) 2018, 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 com.sun.tools.javac.intrinsics;
  27 
  28 import java.lang.constant.ClassDesc;
  29 import java.lang.constant.ConstantDesc;
  30 import java.lang.constant.DynamicCallSiteDesc;
  31 import java.lang.constant.MethodTypeDesc;
  32 
  33 /**
  34  *  <p><b>This is NOT part of any supported API.
  35  *  If you write code that depends on this, you do so at your own risk.
  36  *  This code and its internal interfaces are subject to change or
  37  *  deletion without notice.</b>
  38  */
  39 public interface IntrinsicProcessor {
  40     public interface Result {
  41         public enum ResultKind {
  42             NONE, LDC, INDY
  43         }
  44 
  45         public abstract ResultKind getKind();
  46 
  47         public class None implements Result {
  48             @Override
  49             public ResultKind getKind() {
  50                 return ResultKind.NONE;
  51             }
  52         }
  53 
  54         public class Ldc implements Result {
  55             private final ConstantDesc constant;
  56 
  57             public Ldc(ConstantDesc constant) {
  58                 this.constant = constant;
  59             }
  60 
  61             public ConstantDesc constant() {
  62                 return constant;
  63             }
  64 
  65             @Override
  66             public ResultKind getKind() {
  67                 return ResultKind.LDC;
  68             }
  69         }
  70 
  71         public class Indy implements Result {
  72             private final DynamicCallSiteDesc indy;
  73             private final int[] args;
  74 
  75             /**
  76              * @param indy call site descriptor
  77              * @param args indices of call arguments
  78              */
  79             public Indy(DynamicCallSiteDesc indy, int[] args) {
  80                 this.indy = indy;
  81                 this.args = args;
  82             }
  83 
  84             /**
  85              * @param indy  call site descriptor
  86              * @param nArgs argument range, 0..nArgs
  87              */
  88             public Indy(DynamicCallSiteDesc indy, int nArgs) {
  89                 this.indy = indy;
  90                 this.args = new int[nArgs];
  91                 for (int i = 0; i < nArgs; i++)
  92                     args[i] = i;
  93             }
  94 
  95             /**
  96              * @return DynamicCallSiteDesc value
  97              */
  98             public DynamicCallSiteDesc indy() {
  99                 return indy;
 100             }
 101 
 102             /**
 103              * @return indices of call arguments
 104              */
 105             public int[] args() {
 106                 return args.clone();
 107             }
 108 
 109             @Override
 110             public ResultKind getKind() {
 111                 return ResultKind.INDY;
 112             }
 113         }
 114    }
 115 
 116     public void register(Intrinsics intrinsics);
 117 
 118     /**
 119      * @param ownerDesc       method owner
 120      * @param methodName      method name
 121      * @param methodType      method type descriptor
 122      * @param isStatic        true if static method call
 123      * @param argClassDescs   class descriptors for each argument (includes receiver)
 124      * @param constantArgs    constant value for each argument (includes receiver), null means unknown
 125      * @return IntrinsicProcessor.Result value
 126      */
 127     public Result tryIntrinsify(ClassDesc ownerDesc,
 128                                 String methodName,
 129                                 MethodTypeDesc methodType,
 130                                 boolean isStatic,
 131                                 ClassDesc[] argClassDescs,
 132                                 ConstantDesc[] constantArgs);
 133 }