< prev index next >

src/java.base/share/classes/jdk/internal/foreign/abi/CallingSequenceBuilder.java

Print this page


   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 }
   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.


   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 
  24 package jdk.internal.foreign.abi;
  25 
  26 import java.foreign.layout.Address;
  27 import java.foreign.layout.Layout;
  28 import java.util.ArrayList;
  29 import java.util.EnumMap;
  30 import java.util.List;
  31 import java.util.function.BiConsumer;
  32 
  33 public abstract class CallingSequenceBuilder {





  34 
  35     private final EnumMap<StorageClass, List<ArgumentBinding>> bindings = new EnumMap<>(StorageClass.class);
  36     private final boolean returnsInMemory;














  37 
  38     private int argIndex = 0;















  39 
  40     private final BindingsComputer returnBindgingsComputer;
  41     private final BindingsComputer argumentBindgingsComputer;
  42     private final BindingsComputer varargsBindgingsComputer;
  43 
  44     protected CallingSequenceBuilder(Layout ret,
  45                                      BindingsComputer returnBindgingsComputer,
  46                                      BindingsComputer argumentBindgingsComputer,
  47                                      BindingsComputer varargsBindgingsComputer) {
  48         this.returnBindgingsComputer = returnBindgingsComputer;
  49         this.argumentBindgingsComputer = argumentBindgingsComputer;
  50         this.varargsBindgingsComputer = varargsBindgingsComputer;
  51         if (ret != null) {
  52             Argument retInfo = makeArgument(ret, -1, "__retval");
  53             this.returnsInMemory = retInfo.inMemory();
  54             if (returnsInMemory) {
  55                 retInfo = makeArgument(Address.ofLayout(64, ret), -1, "__retval");
  56                 addArgumentBindings(retInfo, false);
  57             }
  58             addReturnBindings(retInfo);

















  59         } else {
  60             this.returnsInMemory = false;
  61         }
  62     }
  63 
  64     public final CallingSequenceBuilder addArgument(Layout l) {
  65         return addArgument(l, false);




  66     }
  67 
  68     public final CallingSequenceBuilder addArgument(Layout l, boolean isVarargs) {
  69         addArgumentBindings(makeArgument(l, argIndex, "arg" + argIndex), isVarargs);
  70         argIndex++;
  71         return this;




  72     }
  73 
  74     public final CallingSequence build() {
  75         return new CallingSequence(returnsInMemory, bindings);



  76     }

  77 
  78     protected abstract Argument makeArgument(Layout layout, int pos, String name);




  79 
  80     private void addReturnBindings(Argument a) {
  81         returnBindgingsComputer.computeBindings(a, this::addBinding);

  82     }
  83 
  84     private void addArgumentBindings(Argument a, boolean isVarargs) {
  85         if (isVarargs) {
  86             varargsBindgingsComputer.computeBindings(a, this::addBinding);
  87         } else {
  88             argumentBindgingsComputer.computeBindings(a, this::addBinding);
  89         }





  90     }

  91 
  92     private void addBinding(StorageClass storageClass, ArgumentBinding binding) {
  93         bindings.computeIfAbsent(storageClass, _unused -> new ArrayList<>()).add(binding);
  94     }
  95 
  96     public interface BindingsComputer {
  97         void computeBindings(Argument argument, BiConsumer<StorageClass, ArgumentBinding> bindingConsumer);
  98     }
  99 }
< prev index next >