1 /* 2 * Copyright (c) 2013, 2014, 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.vm.ci.code; 24 25 import java.util.Arrays; 26 import java.util.HashMap; 27 import java.util.HashSet; 28 import java.util.Map; 29 import java.util.TreeMap; 30 31 /** 32 * A map from registers to frame slots. This can be used to describe where callee saved registers 33 * are saved in a callee's frame. 34 */ 35 public final class RegisterSaveLayout { 36 37 /** 38 * Keys. 39 */ 40 private final Register[] registers; 41 42 /** 43 * Slot indexes relative to stack pointer. 44 */ 45 private final int[] slots; 46 47 /** 48 * Creates a map from registers to frame slots. 49 * 50 * @param registers the keys in the map 51 * @param slots frame slot index for each register in {@code registers} 52 */ 53 public RegisterSaveLayout(Register[] registers, int[] slots) { 54 assert registers.length == slots.length; 55 this.registers = registers; 56 this.slots = slots; 57 assert registersToSlots(false).size() == registers.length : "non-unique registers"; 58 assert new HashSet<>(registersToSlots(false).values()).size() == slots.length : "non-unqiue slots"; 59 } 60 61 /** 62 * Gets the frame slot index for a given register. 63 * 64 * @param register register to get the frame slot index for 65 * @return frame slot index 66 */ 67 public int registerToSlot(Register register) { 68 for (int i = 0; i < registers.length; i++) { 69 if (register.equals(registers[i])) { 70 return slots[i]; 71 } 72 } 73 throw new IllegalArgumentException(register + " not saved by this layout: " + this); 74 } 75 76 /** 77 * Gets this layout information as a {@link Map} from registers to slots. 78 */ 79 public Map<Register, Integer> registersToSlots(boolean sorted) { 80 Map<Register, Integer> result; 81 if (sorted) { 82 result = new TreeMap<>(); 83 } else { 84 result = new HashMap<>(); 85 } 86 for (int i = 0; i < registers.length; i++) { 87 result.put(registers[i], slots[i]); 88 } 89 return result; 90 } 91 92 /** 93 * Gets this layout information as a {@link Map} from slots to registers. 94 */ 95 public Map<Integer, Register> slotsToRegisters(boolean sorted) { 96 Map<Integer, Register> result; 97 if (sorted) { 98 result = new TreeMap<>(); 99 } else { 100 result = new HashMap<>(); 101 } 102 for (int i = 0; i < registers.length; i++) { 103 result.put(slots[i], registers[i]); 104 } 105 return result; 106 } 107 108 @Override 109 public int hashCode() { 110 throw new UnsupportedOperationException(); 111 } 112 113 @Override 114 public boolean equals(Object obj) { 115 if (this == obj) { 116 return true; 117 } 118 if (obj instanceof RegisterSaveLayout) { 119 RegisterSaveLayout that = (RegisterSaveLayout) obj; 120 if (Arrays.equals(registers, that.registers) && Arrays.equals(slots, that.slots)) { 121 return true; 122 } 123 } 124 return false; 125 } 126 127 @Override 128 public String toString() { 129 return registersToSlots(true).toString(); 130 } 131 }