1 /*
   2  * Copyright (c) 2015, 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 org.graalvm.compiler.lir.util;
  24 
  25 import static org.graalvm.compiler.lir.LIRValueUtil.asVariable;
  26 import static org.graalvm.compiler.lir.LIRValueUtil.asVirtualStackSlot;
  27 import static org.graalvm.compiler.lir.LIRValueUtil.isVariable;
  28 import static org.graalvm.compiler.lir.LIRValueUtil.isVirtualStackSlot;
  29 
  30 import org.graalvm.compiler.debug.GraalError;
  31 
  32 import jdk.vm.ci.meta.Value;
  33 
  34 public class VariableVirtualStackValueMap<K extends Value, T> extends ValueMap<K, T> {
  35 
  36     private final Object[] variables;
  37     private final Object[] slots;
  38 
  39     public VariableVirtualStackValueMap(int initialVariableCapacity, int initialStackSlotCapacity) {
  40         variables = new Object[initialVariableCapacity];
  41         slots = new Object[initialStackSlotCapacity];
  42     }
  43 
  44     @Override
  45     public T get(K value) {
  46         if (isVariable(value)) {
  47             return get(variables, asVariable(value).index);
  48         }
  49         if (isVirtualStackSlot(value)) {
  50             return get(slots, asVirtualStackSlot(value).getId());
  51         }
  52         throw GraalError.shouldNotReachHere("Unsupported Value: " + value);
  53     }
  54 
  55     @Override
  56     public void remove(K value) {
  57         if (isVariable(value)) {
  58             remove(variables, asVariable(value).index);
  59         } else if (isVirtualStackSlot(value)) {
  60             remove(slots, asVirtualStackSlot(value).getId());
  61         } else {
  62             throw GraalError.shouldNotReachHere("Unsupported Value: " + value);
  63         }
  64     }
  65 
  66     @Override
  67     public void put(K value, T object) {
  68         if (isVariable(value)) {
  69             put(variables, asVariable(value).index, object);
  70         } else if (isVirtualStackSlot(value)) {
  71             put(slots, asVirtualStackSlot(value).getId(), object);
  72         } else {
  73             throw GraalError.shouldNotReachHere("Unsupported Value: " + value);
  74         }
  75     }
  76 
  77     @SuppressWarnings("unchecked")
  78     private static <T> T get(Object[] array, int index) {
  79         if (index >= array.length) {
  80             return null;
  81         }
  82         return (T) array[index];
  83     }
  84 
  85     private static void remove(Object[] array, int index) {
  86         if (index >= array.length) {
  87             return;
  88         }
  89         array[index] = null;
  90     }
  91 
  92     private static <T> Object[] put(Object[] array, int index, T object) {
  93         if (index >= array.length) {
  94             Object[] newArray = new Object[index + 1];
  95             System.arraycopy(array, 0, newArray, 0, array.length);
  96             newArray[index] = object;
  97             return newArray;
  98         }
  99         array[index] = object;
 100         return null;
 101     }
 102 }