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 }