001/*
002 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.lir.util;
024
025import static com.oracle.graal.lir.LIRValueUtil.*;
026import static jdk.internal.jvmci.code.ValueUtil.*;
027import jdk.internal.jvmci.common.*;
028import jdk.internal.jvmci.meta.*;
029
030public class VariableVirtualStackValueMap<K extends Value, T> extends ValueMap<K, T> {
031
032    private final Object[] variables;
033    private final Object[] slots;
034
035    public VariableVirtualStackValueMap(int initialVariableCapacity, int initialStackSlotCapacity) {
036        variables = new Object[initialVariableCapacity];
037        slots = new Object[initialStackSlotCapacity];
038    }
039
040    @Override
041    public T get(K value) {
042        if (isVariable(value)) {
043            return get(variables, asVariable(value).index);
044        }
045        if (isVirtualStackSlot(value)) {
046            return get(slots, asVirtualStackSlot(value).getId());
047        }
048        throw JVMCIError.shouldNotReachHere("Unsupported Value: " + value);
049    }
050
051    @Override
052    public void remove(K value) {
053        if (isVariable(value)) {
054            remove(variables, asVariable(value).index);
055        } else if (isVirtualStackSlot(value)) {
056            remove(slots, asVirtualStackSlot(value).getId());
057        } else {
058            throw JVMCIError.shouldNotReachHere("Unsupported Value: " + value);
059        }
060    }
061
062    @Override
063    public void put(K value, T object) {
064        if (isVariable(value)) {
065            put(variables, asVariable(value).index, object);
066        } else if (isVirtualStackSlot(value)) {
067            put(slots, asVirtualStackSlot(value).getId(), object);
068        } else {
069            throw JVMCIError.shouldNotReachHere("Unsupported Value: " + value);
070        }
071    }
072
073    @SuppressWarnings("unchecked")
074    private static <T> T get(Object[] array, int index) {
075        if (index >= array.length) {
076            return null;
077        }
078        return (T) array[index];
079    }
080
081    private static void remove(Object[] array, int index) {
082        if (index >= array.length) {
083            return;
084        }
085        array[index] = null;
086    }
087
088    private static <T> Object[] put(Object[] array, int index, T object) {
089        if (index >= array.length) {
090            Object[] newArray = new Object[index + 1];
091            System.arraycopy(array, 0, newArray, 0, array.length);
092            newArray[index] = object;
093            return newArray;
094        }
095        array[index] = object;
096        return null;
097    }
098}