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 jdk.internal.foreign.Util;
  26 import java.util.ArrayList;
  27 import java.util.HashMap;
  28 import java.util.List;
  29 import java.util.Map;
  30 
  31 class ShuffleRecipeBuilder {
  32 
  33     List<ShuffleRecipeOperation> ops = new ArrayList<>();
  34     Map<ArgumentBinding, Long> offsets = new HashMap<>();
  35     int nArgPulls;
  36     int nRetPulls;
  37 
  38     void addSkip() {
  39         ops.add(ShuffleRecipeOperation.SKIP);
  40     }
  41 
  42     void addStop() {
  43         ops.add(ShuffleRecipeOperation.STOP);
  44     }
  45 
  46     void addPulls(boolean isReturn, long n) {
  47         for (long i = 0; i < n; i++) {
  48             ops.add(ShuffleRecipeOperation.PULL);
  49             if (isReturn) {
  50                 nRetPulls++;
  51             } else {
  52                 nArgPulls++;
  53             }
  54         }
  55     }
  56 
  57     void addOffset(ArgumentBinding binding, long offset) {
  58         offsets.put(binding, offset);
  59     }
  60 
  61     private long[] allocArray() {
  62         long nOpBits = ops.size() * ShuffleRecipeOperation.BITS_PER_OP;
  63         long nOpWords = nOpBits / 64;
  64 
  65         long nBits = nOpBits + nOpWords; // MSB in each word is reserved
  66 
  67         nBits = Util.alignUp(nBits, 64);
  68 
  69         long nWords = nBits / 64;
  70 
  71         return new long[(int)nWords];
  72     }
  73 
  74     private void encodeOp(long[] arr, int index, ShuffleRecipeOperation op) {
  75         int n = index * ShuffleRecipeOperation.BITS_PER_OP;
  76 
  77         int word = n / 64;
  78         n += word; // MSB bits are reserved
  79 
  80         int start_bit = n % 64;
  81         if (start_bit == 63) {
  82             word++;
  83             start_bit++;
  84         }
  85 
  86         arr[word] |= (long)op.ordinal() << start_bit;
  87     }
  88 
  89     private long[] recipeToLongArray() {
  90         long[] arr = allocArray();
  91 
  92         for (int i = 0; i < arr.length; i++) {
  93             arr[i] = (1L << 63);
  94         }
  95 
  96         int i = 0;
  97 
  98         for (ShuffleRecipeOperation op : ops) {
  99             encodeOp(arr, i++, op);
 100         }
 101 
 102         return arr;
 103     }
 104 
 105     ShuffleRecipe build() {
 106         return new ShuffleRecipe(recipeToLongArray(), nArgPulls, nRetPulls, offsets);
 107     }
 108 
 109     public String asString() {
 110         StringBuilder sb = new StringBuilder();
 111 
 112         sb.append("ShuffleRecipe: {\n");
 113         sb.append("  Operations: {\n");
 114         sb.append(ops);
 115         sb.append("  }\n");
 116         sb.append("}\n");
 117 
 118         return sb.toString();
 119     }
 120 }