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.foreign.memory.Pointer;
  26 import java.util.Map;
  27 
  28 import static sun.security.action.GetBooleanAction.privilegedGetProperty;
  29 
  30 public class ShuffleRecipe {
  31     private static final boolean DEBUG = privilegedGetProperty("jdk.internal.foreign.abi.ShuffleRecipe.DEBUG");
  32 
  33     private final long[] recipe;
  34 
  35     private final int nArgumentPulls;
  36     private final int nReturnPulls;
  37 
  38     private final Map<ArgumentBinding, Long> offsets;
  39 
  40     ShuffleRecipe(long[] recipe, int nArgumentPulls, int nReturnPulls, Map<ArgumentBinding, Long> offsets) {
  41         this.recipe = recipe;
  42         this.nArgumentPulls = nArgumentPulls;
  43         this.nReturnPulls = nReturnPulls;
  44         this.offsets = offsets;
  45     }
  46 
  47     public static ShuffleRecipe make(CallingSequence callingSequence) {
  48         ShuffleRecipeBuilder builder = new ShuffleRecipeBuilder();
  49 
  50         for (int i = 0 ; i < 2; i++) {
  51             boolean args = i == 0;
  52             long offset = 0L;
  53             for (ShuffleRecipeClass recipeClass : ShuffleRecipeClass.values()) {
  54                 StorageClass storageClass = recipeClass.storageClass(args);
  55                 if (storageClass != null) {
  56                     int indexInClass = 0;
  57                     for (ArgumentBinding binding : callingSequence.bindings(storageClass)) {
  58                         while (indexInClass < binding.storage().getStorageIndex()) {
  59                             builder.addSkip();
  60                             indexInClass++;
  61                         }
  62                         long size = binding.storage().getSize() / 8;
  63                         builder.addPulls(!args, size);
  64                         if (binding.storage().getSize() < binding.storage().getMaxSize()) {
  65                             builder.addSkip();
  66                         }
  67                         builder.addOffset(binding, offset);
  68                         offset += size;
  69                         indexInClass++;
  70                     }
  71                 }
  72                 builder.addStop();
  73             }
  74         }
  75 
  76         if(DEBUG) {
  77             System.out.println("Translating CallingSequence:");
  78             System.out.println(callingSequence.asString().indent(2));
  79             System.out.println("into:");
  80             System.out.println(builder.asString().indent(2));
  81         }
  82 
  83         return builder.build();
  84     }
  85 
  86     public long[] getRecipe() {
  87         return recipe;
  88     }
  89 
  90     public int getNoofArgumentPulls() {
  91         return nArgumentPulls;
  92     }
  93 
  94     public int getNoofReturnPulls() {
  95         return nReturnPulls;
  96     }
  97 
  98     public Pointer<?> offset(Pointer<?> ptr, ArgumentBinding binding) {
  99         return ptr.offset(offsets.get(binding));
 100     }
 101 
 102     @Override
 103     public String toString() {
 104         StringBuilder sb = new StringBuilder();
 105 
 106         sb.append("ShuffleRecipe { nArgumentPulls: ")
 107                 .append(nArgumentPulls)
 108                 .append(" nReturnPulls: ")
 109                 .append(nReturnPulls)
 110                 .append("}\n");
 111 
 112         return sb.toString();
 113     }
 114 }