1 /*
   2  * Copyright (c) 2015, 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 
  28 class ShuffleRecipeBuilder {
  29     private final ShuffleRecipeOperationCollector arguments = new ShuffleRecipeOperationCollector();
  30     private final ShuffleRecipeOperationCollector returns = new ShuffleRecipeOperationCollector();
  31 
  32     ShuffleRecipeBuilder() {
  33     }
  34 
  35     ShuffleRecipeOperationCollector getArgumentsCollector() {
  36         return arguments;
  37     }
  38 
  39     ShuffleRecipeOperationCollector getReturnsCollector() {
  40         return returns;
  41     }
  42 
  43     private int getTotalNumberOfOps() {
  44         return arguments.getTotalNumberOfOps() + returns.getTotalNumberOfOps();
  45     }
  46 
  47     private long[] allocArray() {
  48         long nOpBits = getTotalNumberOfOps() * ShuffleRecipeOperation.BITS_PER_OP;
  49         long nOpWords = nOpBits / 64;
  50 
  51         long nBits = nOpBits + nOpWords; // MSB in each word is reserved
  52 
  53         nBits = Util.alignUp(nBits, 64);
  54 
  55         long nWords = nBits / 64;
  56 
  57         return new long[(int)nWords];
  58     }
  59 
  60     private void encodeOp(long[] arr, int index, ShuffleRecipeOperation op) {
  61         int n = index * ShuffleRecipeOperation.BITS_PER_OP;
  62 
  63         int word = n / 64;
  64         n += word; // MSB bits are reserved
  65 
  66         int start_bit = n % 64;
  67         if (start_bit == 63) {
  68             word++;
  69             start_bit++;
  70         }
  71 
  72         arr[word] |= (long)op.ordinal() << start_bit;
  73     }
  74 
  75     private long[] recipeToLongArray() {
  76         long[] arr = allocArray();
  77 
  78         for (int i = 0; i < arr.length; i++) {
  79             arr[i] = (1L << 63);
  80         }
  81 
  82         int i = 0;
  83         for (ArrayList<ShuffleRecipeOperation> oparr : arguments.getOps()) {
  84             for (ShuffleRecipeOperation op : oparr) {
  85                 encodeOp(arr, i++, op);
  86             }
  87             encodeOp(arr, i++, ShuffleRecipeOperation.STOP);
  88         }
  89         for (ArrayList<ShuffleRecipeOperation> oparr : returns.getOps()) {
  90             for (ShuffleRecipeOperation op : oparr) {
  91                 encodeOp(arr, i++, op);
  92             }
  93             encodeOp(arr, i++, ShuffleRecipeOperation.STOP);
  94         }
  95 
  96         return arr;
  97     }
  98 
  99     ShuffleRecipe build() {
 100         return new ShuffleRecipe(recipeToLongArray(), getArgumentsCollector().getNoofPulls(), getReturnsCollector().getNoofPulls());
 101     }
 102 
 103     public String asString() {
 104         StringBuilder sb = new StringBuilder();
 105 
 106         sb.append("ShuffleRecipe: {\n");
 107         sb.append("  Arguments: {\n");
 108         sb.append(arguments.asString().indent(4));
 109         sb.append("  }\n");
 110         sb.append("  Returns: {\n");
 111         sb.append(returns.asString().indent(4));
 112         sb.append("  }\n");
 113         sb.append("}\n");
 114 
 115         return sb.toString();
 116     }
 117 }