< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2015, 2018, 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.internal.foreign.abi;
  24 
  25 import java.util.ArrayList;
  26 import java.util.List;



  27 
  28 public class CallingSequence {
  29     private final List<ArgumentBinding>[] bindings;
  30     private final boolean returnsInMemory;
  31     private final List<ArgumentBinding>[] argBindings;
  32     private final List<ArgumentBinding> retBindings = new ArrayList<>();
  33     private final int[] argsOffsets = new int[StorageClass.ARGUMENT_STORAGE_CLASSES.length];
  34     private final int[] retOffsets = new int[StorageClass.values().length];
  35 
  36     @SuppressWarnings({"unchecked", "rawtypes"})
  37     public CallingSequence(int argSize, ArrayList<ArgumentBinding>[] bindings, boolean returnsInMemory) {
  38         this.bindings = bindings;
  39         this.returnsInMemory = returnsInMemory;
  40         argBindings = new List[argSize];
  41         classifyBindings();

  42     }
  43 
  44     public List<ArgumentBinding> getBindings(StorageClass storageClass) {
  45         return bindings[storageClass.ordinal()];
  46     }
  47 
  48     public boolean returnsInMemory() {
  49         return returnsInMemory;
  50     }
  51 
  52     private void classifyBindings() {
  53         for (StorageClass storageClass : StorageClass.values()) {
  54             for (ArgumentBinding binding : getBindings(storageClass)) {
  55                 if (storageClass.isArgumentClass()) {
  56                     //update offsets
  57                     for (int i = storageClass.ordinal() + 1 ; i < argsOffsets.length ; i++) {
  58                         argsOffsets[i]++;
  59                     }
  60                     //classify arguments
  61                     if (storageClass == StorageClass.INTEGER_ARGUMENT_REGISTER &&
  62                             returnsInMemory() && binding.getStorage().getStorageIndex() == 0) {
  63                         retBindings.add(binding);
  64                     } else {
  65                         int index = binding.getMember().getArgumentIndex();
  66                         List<ArgumentBinding> args = argBindings[index];
  67                         if (args == null) {
  68                             argBindings[index] = args = new ArrayList<>();
  69                         }
  70                         args.add(binding);
  71                     }
  72                 } else {
  73                     if (!returnsInMemory()) {
  74                         //update offsets
  75                         for (int i = storageClass.ordinal() + 1 ; i < retOffsets.length ; i++) {
  76                             retOffsets[i]++;
  77                         }
  78                         //classify returns
  79                         retBindings.add(binding);
  80                     }
  81                 }
  82             }
  83         }
  84     }
  85 
  86     public List<ArgumentBinding> getArgumentBindings(int i) {
  87         return argBindings[i] == null ? List.of() : argBindings[i];

  88     }
  89 
  90     public List<ArgumentBinding> getReturnBindings() {
  91         return retBindings == null ? List.of() : retBindings;
  92     }
  93 
  94     public long argumentStorageOffset(ArgumentBinding b) {
  95         return argsOffsets[b.getStorage().getStorageClass().ordinal()] +
  96             bindings[b.getStorage().getStorageClass().ordinal()].indexOf(b)
  97             * b.getStorage().getSize() / 8;
  98     }
  99 
 100     public long returnStorageOffset(ArgumentBinding b) {
 101         return retOffsets[b.getStorage().getStorageClass().ordinal()] +
 102             bindings[b.getStorage().getStorageClass().ordinal()].indexOf(b)
 103             * b.getStorage().getSize() / 8;

 104     }
 105 
 106     public String asString() {
 107         StringBuilder sb = new StringBuilder();
 108 
 109         sb.append("CallingSequence: {\n");
 110         sb.append("  returnsInMemory: " + returnsInMemory + "\n");
 111         sb.append("  Classes:\n");
 112         for (StorageClass c : StorageClass.values()) {
 113             sb.append("    ").append(c).append("\n");
 114             for (ArgumentBinding binding : getBindings(c)) {
 115                 if (binding != null) {
 116                     sb.append("      ").append(binding.toString()).append("\n");
 117                 }
 118             }
 119         }
 120         sb.append("}\n");
 121 
 122         return sb.toString();
 123     }
 124 }
   1 /*
   2  * Copyright (c) 2015, 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 package jdk.internal.foreign.abi;
  24 
  25 import java.util.EnumMap;
  26 import java.util.List;
  27 import java.util.Map;
  28 import java.util.stream.Collectors;
  29 import java.util.stream.Stream;
  30 
  31 public class CallingSequence {
  32     private final EnumMap<StorageClass, List<ArgumentBinding>> bindings;
  33     private final boolean returnsInMemory;
  34     private final Map<Integer, List<ArgumentBinding>> bindingsByIndex;
  35 
  36     public CallingSequence(boolean returnsInMemory, EnumMap<StorageClass, List<ArgumentBinding>> bindings) {
  37         this.bindings = new EnumMap<>(bindings);




  38         this.returnsInMemory = returnsInMemory;
  39         this.bindingsByIndex = Stream.of(StorageClass.values())
  40                 .flatMap(sc -> bindings(sc).stream())
  41                 .collect(Collectors.groupingBy(b -> b.argument().argumentIndex()));
  42     }
  43 
  44     public List<ArgumentBinding> bindings(StorageClass storageClass) {
  45         return bindings.getOrDefault(storageClass, List.of());
  46     }
  47 
  48     public boolean returnsInMemory() {
  49         return returnsInMemory;
  50     }
  51 
  52     public List<ArgumentBinding> argumentBindings(int i) {
  53         return bindingsByIndex.getOrDefault(i, List.of());






























  54     }
  55 
  56     public List<ArgumentBinding> returnBindings() {
  57         if (returnsInMemory) {
  58             throw new IllegalStateException("Attempting to obtain return bindings for in-memory return!");
  59         }
  60         return bindingsByIndex.getOrDefault(-1, List.of());


  61     }
  62 
  63     public ArgumentBinding returnInMemoryBinding() {
  64         if (!returnsInMemory) {
  65             throw new IllegalStateException("Attempting to obtain in-memory binding for regular return");

  66         }
  67         //if returns in memory, we have two bindings with position -1, the argument and the return.
  68         //The code below filters out the return binding.
  69         return bindingsByIndex.getOrDefault(-1, List.of()).stream()
  70                 .filter(b -> b.storage().getStorageClass() == StorageClass.INTEGER_ARGUMENT_REGISTER)
  71                 .findFirst()
  72                 .orElseThrow(IllegalStateException::new);
  73     }
  74 
  75     public String asString() {
  76         StringBuilder sb = new StringBuilder();
  77 
  78         sb.append("CallingSequence: {\n");
  79         sb.append("  returnsInMemory: " + returnsInMemory + "\n");
  80         sb.append("  Classes:\n");
  81         for (StorageClass c : StorageClass.values()) {
  82             sb.append("    ").append(c).append("\n");
  83             for (ArgumentBinding binding : bindings(c)) {
  84                 if (binding != null) {
  85                     sb.append("      ").append(binding.toString()).append("\n");
  86                 }
  87             }
  88         }
  89         sb.append("}\n");
  90 
  91         return sb.toString();
  92     }
  93 }
< prev index next >