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 }