1 /*
   2  * Copyright (c) 2016, 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 static jdk.vm.ci.common.InitTimer.timer;
  26 
  27 import java.util.Arrays;
  28 import java.util.Collections;
  29 import java.util.HashMap;
  30 import java.util.List;
  31 import java.util.Map;
  32 
  33 import jdk.vm.ci.common.InitTimer;
  34 
  35 /**
  36  * Access to VM configuration data.
  37  */
  38 public final class HotSpotVMConfigStore {
  39 
  40     /**
  41      * Gets the C++ symbols whose addresses are exposed by this object.
  42      *
  43      * @return an unmodifiable map from the symbol names to their addresses
  44      */
  45     public Map<String, Long> getAddresses() {
  46         return Collections.unmodifiableMap(vmAddresses);
  47     }
  48 
  49     /**
  50      * Gets the C++ constants exposed by this object.
  51      *
  52      * @return an unmodifiable map from the names of C++ constants to their values
  53      */
  54     public Map<String, Long> getConstants() {
  55         return Collections.unmodifiableMap(vmConstants);
  56     }
  57 
  58     /**
  59      * Gets the VM flags exposed by this object.
  60      *
  61      * @return an unmodifiable map from VM flag names to {@link VMFlag} objects
  62      */
  63     public Map<String, VMFlag> getFlags() {
  64         return Collections.unmodifiableMap(vmFlags);
  65     }
  66 
  67     /**
  68      * Gets the C++ fields exposed by this object.
  69      *
  70      * @return an unmodifiable map from VM field names to {@link VMField} objects
  71      */
  72     public Map<String, VMField> getFields() {
  73         return Collections.unmodifiableMap(vmFields);
  74     }
  75 
  76     /**
  77      * Gets the VM intrinsic descriptions exposed by this object.
  78      */
  79     public List<VMIntrinsicMethod> getIntrinsics() {
  80         return Collections.unmodifiableList(vmIntrinsics);
  81     }
  82 
  83     final HashMap<String, VMField> vmFields;
  84     final HashMap<String, Long> vmConstants;
  85     final HashMap<String, Long> vmAddresses;
  86     final HashMap<String, VMFlag> vmFlags;
  87     final List<VMIntrinsicMethod> vmIntrinsics;
  88     final CompilerToVM compilerToVm;
  89 
  90     /**
  91      * Reads the database of VM info. The return value encodes the info in a nested object array
  92      * that is described by the pseudo Java object {@code info} below:
  93      *
  94      * <pre>
  95      *     info = [
  96      *         VMField[] vmFields,
  97      *         [String name, Long value, ...] vmConstants,
  98      *         [String name, Long value, ...] vmAddresses,
  99      *         VMFlag[] vmFlags
 100      *         VMIntrinsicMethod[] vmIntrinsics
 101      *     ]
 102      * </pre>
 103      */
 104     @SuppressWarnings("try")
 105     HotSpotVMConfigStore(CompilerToVM compilerToVm) {
 106         this.compilerToVm = compilerToVm;
 107         Object[] data;
 108         try (InitTimer t = timer("CompilerToVm readConfiguration")) {
 109             data = compilerToVm.readConfiguration();
 110         }
 111         assert data.length == 5 : data.length;
 112 
 113         // @formatter:off
 114         VMField[] vmFieldsInfo    = (VMField[]) data[0];
 115         Object[] vmConstantsInfo  = (Object[])  data[1];
 116         Object[] vmAddressesInfo  = (Object[])  data[2];
 117         VMFlag[] vmFlagsInfo      = (VMFlag[])  data[3];
 118 
 119         vmFields     = new HashMap<>(vmFieldsInfo.length);
 120         vmConstants  = new HashMap<>(vmConstantsInfo.length);
 121         vmAddresses  = new HashMap<>(vmAddressesInfo.length);
 122         vmFlags      = new HashMap<>(vmFlagsInfo.length);
 123         vmIntrinsics = Arrays.asList((VMIntrinsicMethod[]) data[4]);
 124         // @formatter:on
 125 
 126         try (InitTimer t = timer("HotSpotVMConfigStore<init> fill maps")) {
 127             for (VMField vmField : vmFieldsInfo) {
 128                 vmFields.put(vmField.name, vmField);
 129             }
 130 
 131             for (int i = 0; i < vmConstantsInfo.length / 2; i++) {
 132                 String name = (String) vmConstantsInfo[i * 2];
 133                 Long value = (Long) vmConstantsInfo[i * 2 + 1];
 134                 vmConstants.put(name, value);
 135             }
 136 
 137             for (int i = 0; i < vmAddressesInfo.length / 2; i++) {
 138                 String name = (String) vmAddressesInfo[i * 2];
 139                 Long value = (Long) vmAddressesInfo[i * 2 + 1];
 140                 vmAddresses.put(name, value);
 141             }
 142 
 143             for (VMFlag vmFlag : vmFlagsInfo) {
 144                 vmFlags.put(vmFlag.name, vmFlag);
 145             }
 146         }
 147     }
 148 }