< prev index next >

src/java.base/share/classes/jdk/internal/foreign/abi/x64/SharedUtils.java

Print this page




   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 package jdk.internal.foreign.abi.x64;
  26 
  27 public class SharedConstants {









  28     public static final int INTEGER_REGISTER_SIZE = 8;
  29     public static final int VECTOR_REGISTER_SIZE = 64; // (maximum) vector size is 512 bits
  30     public static final int X87_REGISTER_SIZE = 16; // x87 register is 128 bits
  31 
  32     public static final int STACK_SLOT_SIZE = 8;
































































































































  33 }


   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 package jdk.internal.foreign.abi.x64;
  26 
  27 import jdk.internal.foreign.abi.Storage;
  28 
  29 import java.foreign.layout.Address;
  30 import java.foreign.layout.Group;
  31 import java.foreign.layout.Layout;
  32 import java.foreign.layout.Padding;
  33 import java.foreign.layout.Sequence;
  34 import java.foreign.layout.Value;
  35 
  36 public class SharedUtils {
  37     public static final int INTEGER_REGISTER_SIZE = 8;
  38     public static final int VECTOR_REGISTER_SIZE = 64; // (maximum) vector size is 512 bits
  39     public static final int X87_REGISTER_SIZE = 16; // x87 register is 128 bits
  40 
  41     public static final int STACK_SLOT_SIZE = 8;
  42 
  43     /**
  44      * Align the specified type from a given address
  45      * @return The address the data should be at based on alignment requirement
  46      */
  47     public static long align(Layout t, boolean isVar, long addr) {
  48         return alignUp(addr, alignment(t, isVar));
  49     }
  50 
  51     public static long alignUp(long addr, long alignment) {
  52         return ((addr - 1) | (alignment - 1)) + 1;
  53     }
  54 
  55     public static long alignDown(long addr, long alignment) {
  56         return addr & ~(alignment - 1);
  57     }
  58 
  59     /**
  60      * The alignment requirement for a given type
  61      * @param isVar indicate if the type is a standalone variable. This change how
  62      * array is aligned. for example.
  63      */
  64     public static long alignment(Layout t, boolean isVar) {
  65         if (t instanceof Value) {
  66             return alignmentOfScalar((Value) t);
  67         } else if (t instanceof Sequence) {
  68             // when array is used alone
  69             return alignmentOfArray((Sequence) t, isVar);
  70         } else if (t instanceof Group) {
  71             return alignmentOfContainer((Group) t);
  72         } else if (t instanceof Address) {
  73             return 8;
  74         } else if (t instanceof Padding) {
  75             return 1;
  76         } else {
  77             throw new IllegalArgumentException("Invalid type: " + t);
  78         }
  79     }
  80 
  81     private static long alignmentOfScalar(Value st) {
  82         return st.bitsSize() / 8;
  83     }
  84 
  85     private static long alignmentOfArray(Sequence ar, boolean isVar) {
  86         if (ar.elementsSize() == 0) {
  87             // VLA or incomplete
  88             return 16;
  89         } else if ((ar.bitsSize() / 8) >= 16 && isVar) {
  90             return 16;
  91         } else {
  92             // align as element type
  93             Layout elementType = ar.element();
  94             return alignment(elementType, false);
  95         }
  96     }
  97 
  98     private static long alignmentOfContainer(Group ct) {
  99         // Most strict member
 100         return ct.elements().stream().mapToLong(t -> alignment(t, false)).max().orElse(1);
 101     }
 102 
 103     public static class StorageDebugHelper {
 104 
 105         private final String[] integerArgumentRegisterNames;
 106         private final String[] integerReturnRegisterNames;
 107         private final String[] x87ReturnRegisterNames;
 108         private final int maxVectorArgRegisters;
 109         private final int maxVectorReturnRegisters;
 110 
 111         public StorageDebugHelper(String[] integerArgumentRegisterNames, String[] integerReturnRegisterNames,
 112                                          String[]  x87ReturnRegisterNames, int maxVectorArgRegisters, int maxVectorReturnRegisters) {
 113             this.integerArgumentRegisterNames = integerArgumentRegisterNames;
 114             this.integerReturnRegisterNames = integerReturnRegisterNames;
 115             this.x87ReturnRegisterNames = x87ReturnRegisterNames;
 116             this.maxVectorArgRegisters = maxVectorArgRegisters;
 117             this.maxVectorReturnRegisters = maxVectorReturnRegisters;
 118         }
 119 
 120 
 121         private static String getVectorRegisterName(long index, long size) {
 122             switch ((int)size) {
 123                 case 8: return "xmm" + index + "_8";
 124                 case 16: return "xmm" + index;
 125                 case 32: return "ymm" + index;
 126                 case 64: return "zmm" + index;
 127                 default: throw new IllegalArgumentException("Illegal vector size: " + size);
 128             }
 129         }
 130 
 131         public String getStorageName(Storage storage) {
 132             switch (storage.getStorageClass()) {
 133                 case INTEGER_ARGUMENT_REGISTER:
 134                     if (storage.getStorageIndex() > integerArgumentRegisterNames.length) {
 135                         throw new IllegalArgumentException("Illegal storage: " + storage);
 136                     }
 137                     return integerArgumentRegisterNames[(int) storage.getStorageIndex()];
 138 
 139                 case VECTOR_ARGUMENT_REGISTER:
 140                     if (storage.getStorageIndex() > maxVectorArgRegisters) {
 141                         throw new IllegalArgumentException("Illegal storage: " + storage);
 142                     }
 143                     return getVectorRegisterName(storage.getStorageIndex(), storage.getSize());
 144 
 145                 case INTEGER_RETURN_REGISTER:
 146                     if (storage.getStorageIndex() > integerReturnRegisterNames.length) {
 147                         throw new IllegalArgumentException("Illegal storage: " + storage);
 148                     }
 149 
 150                     return integerReturnRegisterNames[(int) storage.getStorageIndex()];
 151 
 152                 case VECTOR_RETURN_REGISTER:
 153                     if (storage.getStorageIndex() > maxVectorReturnRegisters) {
 154                         throw new IllegalArgumentException("Illegal storage: " + storage);
 155                     }
 156                     return getVectorRegisterName(storage.getStorageIndex(), storage.getSize());
 157 
 158                 case X87_RETURN_REGISTER:
 159                     if (storage.getStorageIndex() > x87ReturnRegisterNames.length) {
 160                         throw new IllegalArgumentException("Illegal storage: " + storage);
 161                     }
 162                     return x87ReturnRegisterNames[(int) storage.getStorageIndex()];
 163 
 164                 case STACK_ARGUMENT_SLOT: return "[sp + " + Long.toHexString(8 * storage.getStorageIndex()) + "]";
 165             }
 166 
 167             throw new IllegalArgumentException("Unhandled storage type: " + storage.getStorageClass());
 168         }
 169     }
 170 }
< prev index next >