1 /*
   2  * Copyright (c) 2009, 2015, 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.code;
  24 
  25 import java.nio.*;
  26 import java.util.*;
  27 
  28 import jdk.vm.ci.code.Register.RegisterCategory;
  29 import jdk.vm.ci.meta.*;
  30 
  31 /**
  32  * Represents a CPU architecture, including information such as its endianness, CPU registers, word
  33  * width, etc.
  34  */
  35 public abstract class Architecture {
  36 
  37     /**
  38      * The number of entries required in a {@link ReferenceMap} covering all the registers that may
  39      * store references. The index of a register in the reference map is given by
  40      * {@link Register#getReferenceMapIndex()}.
  41      */
  42     private final int registerReferenceMapSize;
  43 
  44     /**
  45      * The architecture specific type of a native word.
  46      */
  47     private final PlatformKind wordKind;
  48 
  49     /**
  50      * The name of this architecture (e.g. "AMD64", "SPARCv9").
  51      */
  52     private final String name;
  53 
  54     /**
  55      * Array of all available registers on this architecture. The index of each register in this
  56      * array is equal to its {@linkplain Register#number number}.
  57      */
  58     private final Register[] registers;
  59 
  60     /**
  61      * The byte ordering can be either little or big endian.
  62      */
  63     private final ByteOrder byteOrder;
  64 
  65     /**
  66      * Whether the architecture supports unaligned memory accesses.
  67      */
  68     private final boolean unalignedMemoryAccess;
  69 
  70     /**
  71      * Mask of the barrier constants denoting the barriers that are not required to be explicitly
  72      * inserted under this architecture.
  73      */
  74     private final int implicitMemoryBarriers;
  75 
  76     /**
  77      * Offset in bytes from the beginning of a call instruction to the displacement.
  78      */
  79     private final int machineCodeCallDisplacementOffset;
  80 
  81     /**
  82      * The size of the return address pushed to the stack by a call instruction. A value of 0
  83      * denotes that call linkage uses registers instead (e.g. SPARC).
  84      */
  85     private final int returnAddressSize;
  86 
  87     protected Architecture(String name, PlatformKind wordKind, ByteOrder byteOrder, boolean unalignedMemoryAccess, Register[] registers, int implicitMemoryBarriers, int nativeCallDisplacementOffset,
  88                     int registerReferenceMapSize, int returnAddressSize) {
  89         this.name = name;
  90         this.registers = registers;
  91         this.wordKind = wordKind;
  92         this.byteOrder = byteOrder;
  93         this.unalignedMemoryAccess = unalignedMemoryAccess;
  94         this.implicitMemoryBarriers = implicitMemoryBarriers;
  95         this.machineCodeCallDisplacementOffset = nativeCallDisplacementOffset;
  96         this.registerReferenceMapSize = registerReferenceMapSize;
  97         this.returnAddressSize = returnAddressSize;
  98     }
  99 
 100     /**
 101      * Converts this architecture to a string.
 102      *
 103      * @return the string representation of this architecture
 104      */
 105     @Override
 106     public final String toString() {
 107         return getName().toLowerCase();
 108     }
 109 
 110     public int getRegisterReferenceMapSize() {
 111         return registerReferenceMapSize;
 112     }
 113 
 114     /**
 115      * Gets the natural size of words (typically registers and pointers) of this architecture, in
 116      * bytes.
 117      */
 118     public int getWordSize() {
 119         return wordKind.getSizeInBytes();
 120     }
 121 
 122     public PlatformKind getWordKind() {
 123         return wordKind;
 124     }
 125 
 126     /**
 127      * Gets the name of this architecture.
 128      */
 129     public String getName() {
 130         return name;
 131     }
 132 
 133     /**
 134      * Gets an array of all available registers on this architecture. The index of each register in
 135      * this array is equal to its {@linkplain Register#number number}.
 136      */
 137     public Register[] getRegisters() {
 138         return registers.clone();
 139     }
 140 
 141     public ByteOrder getByteOrder() {
 142         return byteOrder;
 143     }
 144 
 145     /**
 146      * @return true if the architecture supports unaligned memory accesses.
 147      */
 148     public boolean supportsUnalignedMemoryAccess() {
 149         return unalignedMemoryAccess;
 150     }
 151 
 152     /**
 153      * Gets the size of the return address pushed to the stack by a call instruction. A value of 0
 154      * denotes that call linkage uses registers instead.
 155      */
 156     public int getReturnAddressSize() {
 157         return returnAddressSize;
 158     }
 159 
 160     /**
 161      * Gets the offset in bytes from the beginning of a call instruction to the displacement.
 162      */
 163     public int getMachineCodeCallDisplacementOffset() {
 164         return machineCodeCallDisplacementOffset;
 165     }
 166 
 167     /**
 168      * Determines the barriers in a given barrier mask that are explicitly required on this
 169      * architecture.
 170      *
 171      * @param barriers a mask of the barrier constants
 172      * @return the value of {@code barriers} minus the barriers unnecessary on this architecture
 173      */
 174     public final int requiredBarriers(int barriers) {
 175         return barriers & ~implicitMemoryBarriers;
 176     }
 177 
 178     /**
 179      * Determine whether a kind can be stored in a register of a given category.
 180      *
 181      * @param category the category of the register
 182      * @param kind the kind that should be stored in the register
 183      */
 184     public abstract boolean canStoreValue(RegisterCategory category, PlatformKind kind);
 185 
 186     /**
 187      * Return the largest kind that can be stored in a register of a given category.
 188      *
 189      * @param category the category of the register
 190      * @return the largest kind that can be stored in a register {@code category}
 191      */
 192     public abstract PlatformKind getLargestStorableKind(RegisterCategory category);
 193 
 194     /**
 195      * Return the {@link PlatformKind} that is used to store values of a given {@link JavaKind}.
 196      */
 197     public abstract PlatformKind getPlatformKind(JavaKind javaKind);
 198 
 199     @Override
 200     public final boolean equals(Object obj) {
 201         if (obj == this) {
 202             return true;
 203         }
 204         if (obj instanceof Architecture) {
 205             Architecture that = (Architecture) obj;
 206             if (this.name.equals(that.name)) {
 207                 assert this.byteOrder.equals(that.byteOrder);
 208                 assert this.implicitMemoryBarriers == that.implicitMemoryBarriers;
 209                 assert this.machineCodeCallDisplacementOffset == that.machineCodeCallDisplacementOffset;
 210                 assert this.registerReferenceMapSize == that.registerReferenceMapSize;
 211                 assert Arrays.equals(this.registers, that.registers);
 212                 assert this.returnAddressSize == that.returnAddressSize;
 213                 assert this.unalignedMemoryAccess == that.unalignedMemoryAccess;
 214                 assert this.wordKind == that.wordKind;
 215                 return true;
 216             }
 217         }
 218         return false;
 219     }
 220 
 221     @Override
 222     public final int hashCode() {
 223         return name.hashCode();
 224     }
 225 }