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