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 }