001/*
002 * Copyright (c) 2013, 2014, 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 jdk.internal.jvmci.code;
024
025import java.util.*;
026
027/**
028 * A map from registers to frame slots. This can be used to describe where callee saved registers
029 * are saved in a callee's frame.
030 */
031public final class RegisterSaveLayout {
032
033    /**
034     * Keys.
035     */
036    private final Register[] registers;
037
038    /**
039     * Slot indexes relative to stack pointer.
040     */
041    private final int[] slots;
042
043    /**
044     * Creates a map from registers to frame slots.
045     *
046     * @param registers the keys in the map
047     * @param slots frame slot index for each register in {@code registers}
048     */
049    public RegisterSaveLayout(Register[] registers, int[] slots) {
050        assert registers.length == slots.length;
051        this.registers = registers;
052        this.slots = slots;
053        assert registersToSlots(false).size() == registers.length : "non-unique registers";
054        assert new HashSet<>(registersToSlots(false).values()).size() == slots.length : "non-unqiue slots";
055    }
056
057    /**
058     * Gets the frame slot index for a given register.
059     *
060     * @param register register to get the frame slot index for
061     * @return frame slot index
062     */
063    public int registerToSlot(Register register) {
064        for (int i = 0; i < registers.length; i++) {
065            if (register.equals(registers[i])) {
066                return slots[i];
067            }
068        }
069        throw new IllegalArgumentException(register + " not saved by this layout: " + this);
070    }
071
072    /**
073     * Gets this layout information as a {@link Map} from registers to slots.
074     */
075    public Map<Register, Integer> registersToSlots(boolean sorted) {
076        Map<Register, Integer> result;
077        if (sorted) {
078            result = new TreeMap<>();
079        } else {
080            result = new HashMap<>();
081        }
082        for (int i = 0; i < registers.length; i++) {
083            result.put(registers[i], slots[i]);
084        }
085        return result;
086    }
087
088    /**
089     * Gets this layout information as a {@link Map} from slots to registers.
090     */
091    public Map<Integer, Register> slotsToRegisters(boolean sorted) {
092        Map<Integer, Register> result;
093        if (sorted) {
094            result = new TreeMap<>();
095        } else {
096            result = new HashMap<>();
097        }
098        for (int i = 0; i < registers.length; i++) {
099            result.put(slots[i], registers[i]);
100        }
101        return result;
102    }
103
104    @Override
105    public int hashCode() {
106        throw new UnsupportedOperationException();
107    }
108
109    @Override
110    public boolean equals(Object obj) {
111        if (this == obj) {
112            return true;
113        }
114        if (obj instanceof RegisterSaveLayout) {
115            RegisterSaveLayout that = (RegisterSaveLayout) obj;
116            if (Arrays.equals(registers, that.registers) && Arrays.equals(slots, that.slots)) {
117                return true;
118            }
119        }
120        return false;
121    }
122
123    @Override
124    public String toString() {
125        return registersToSlots(true).toString();
126    }
127}