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