1 /*
  2  * Copyright (c) 2011, 2019, 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 package jdk.vm.ci.hotspot;
 24 
 25 import jdk.vm.ci.meta.JavaMethodProfile;
 26 import jdk.vm.ci.meta.JavaTypeProfile;
 27 import jdk.vm.ci.meta.ProfilingInfo;
 28 import jdk.vm.ci.meta.TriState;
 29 
 30 /**
 31  * Base class for accessing the different kinds of data in a HotSpot {@code MethodData}. This is
 32  * similar to {@link ProfilingInfo}, but most methods require a {@link HotSpotMethodData} and the
 33  * exact position within the method data.
 34  */
 35 abstract class HotSpotMethodDataAccessor {
 36 
 37     final int tag;
 38     final int staticSize;
 39     final HotSpotMethodData.VMState state;
 40     final HotSpotVMConfig config;
 41 
 42     protected HotSpotMethodDataAccessor(HotSpotMethodData.VMState state, int tag, int staticSize) {
 43         this.state = state;
 44         this.config = state.config;
 45         this.tag = tag;
 46         this.staticSize = staticSize;
 47     }
 48 
 49     /**
 50      * Returns the tag stored in the LayoutData header.
 51      *
 52      * @return tag stored in the LayoutData header
 53      */
 54     int getTag() {
 55         return tag;
 56     }
 57 
 58     static int readTag(HotSpotVMConfig config, HotSpotMethodData data, int position) {
 59         final int tag = data.readUnsignedByte(position, config.dataLayoutTagOffset);
 60         assert tag >= config.dataLayoutNoTag && tag <= config.dataLayoutSpeculativeTrapDataTag : "profile data tag out of bounds: " + tag;
 61         return tag;
 62     }
 63 
 64     /**
 65      * Returns the BCI stored in the LayoutData header.
 66      *
 67      * @return an integer between 0 and {@link Short#MAX_VALUE} inclusive, or -1 if not supported
 68      */
 69     int getBCI(HotSpotMethodData data, int position) {
 70         return data.readUnsignedShort(position, config.dataLayoutBCIOffset);
 71     }
 72 
 73     /**
 74      * Computes the size for the specific data at the given position.
 75      *
 76      * @return a value greater than 0
 77      */
 78     final int getSize(HotSpotMethodData data, int position) {
 79         int size = staticSize + getDynamicSize(data, position);
 80         // Sanity check against VM
 81         int vmSize = HotSpotJVMCIRuntime.runtime().compilerToVm.methodDataProfileDataSize(data.metaspaceMethodData, position);
 82         assert size == vmSize : size + " != " + vmSize;
 83         return size;
 84     }
 85 
 86     TriState getExceptionSeen(HotSpotMethodData data, int position) {
 87         final int exceptionsMask = 1 << config.bitDataExceptionSeenFlag;
 88         return TriState.get((getFlags(data, position) & exceptionsMask) != 0);
 89     }
 90 
 91     /**
 92      * @param data
 93      * @param position
 94      */
 95     JavaTypeProfile getTypeProfile(HotSpotMethodData data, int position) {
 96         return null;
 97     }
 98 
 99     /**
100      * @param data
101      * @param position
102      */
103     JavaMethodProfile getMethodProfile(HotSpotMethodData data, int position) {
104         return null;
105     }
106 
107     /**
108      * @param data
109      * @param position
110      */
111     double getBranchTakenProbability(HotSpotMethodData data, int position) {
112         return -1;
113     }
114 
115     /**
116      * @param data
117      * @param position
118      */
119     double[] getSwitchProbabilities(HotSpotMethodData data, int position) {
120         return null;
121     }
122 
123     /**
124      * @param data
125      * @param position
126      */
127     int getExecutionCount(HotSpotMethodData data, int position) {
128         return -1;
129     }
130 
131     /**
132      * @param data
133      * @param position
134      */
135     TriState getNullSeen(HotSpotMethodData data, int position) {
136         return TriState.UNKNOWN;
137     }
138 
139     protected int getFlags(HotSpotMethodData data, int position) {
140         return data.readUnsignedByte(position, config.dataLayoutFlagsOffset);
141     }
142 
143     /**
144      * @param data
145      * @param position
146      */
147     protected int getDynamicSize(HotSpotMethodData data, int position) {
148         return 0;
149     }
150 
151     abstract StringBuilder appendTo(StringBuilder sb, HotSpotMethodData data, int pos);
152 
153 }