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.internal.jvmci.code;
  24 
  25 import java.util.*;
  26 
  27 /**
  28  * A map from registers to frame slots. This can be used to describe where callee saved registers
  29  * are saved in a callee's frame.
  30  */
  31 public final class RegisterSaveLayout {
  32 
  33     /**
  34      * Keys.
  35      */
  36     private final Register[] registers;
  37 
  38     /**
  39      * Slot indexes relative to stack pointer.
  40      */
  41     private final int[] slots;
  42 
  43     /**
  44      * Creates a map from registers to frame slots.
  45      *
  46      * @param registers the keys in the map
  47      * @param slots frame slot index for each register in {@code registers}
  48      */
  49     public RegisterSaveLayout(Register[] registers, int[] slots) {
  50         assert registers.length == slots.length;
  51         this.registers = registers;
  52         this.slots = slots;
  53         assert registersToSlots(false).size() == registers.length : "non-unique registers";
  54         assert new HashSet<>(registersToSlots(false).values()).size() == slots.length : "non-unqiue slots";
  55     }
  56 
  57     /**
  58      * Gets the frame slot index for a given register.
  59      *
  60      * @param register register to get the frame slot index for
  61      * @return frame slot index
  62      */
  63     public int registerToSlot(Register register) {
  64         for (int i = 0; i < registers.length; i++) {
  65             if (register.equals(registers[i])) {
  66                 return slots[i];
  67             }
  68         }
  69         throw new IllegalArgumentException(register + " not saved by this layout: " + this);
  70     }
  71 
  72     /**
  73      * Gets this layout information as a {@link Map} from registers to slots.
  74      */
  75     public Map<Register, Integer> registersToSlots(boolean sorted) {
  76         Map<Register, Integer> result;
  77         if (sorted) {
  78             result = new TreeMap<>();
  79         } else {
  80             result = new HashMap<>();
  81         }
  82         for (int i = 0; i < registers.length; i++) {
  83             result.put(registers[i], slots[i]);
  84         }
  85         return result;
  86     }
  87 
  88     /**
  89      * Gets this layout information as a {@link Map} from slots to registers.
  90      */
  91     public Map<Integer, Register> slotsToRegisters(boolean sorted) {
  92         Map<Integer, Register> result;
  93         if (sorted) {
  94             result = new TreeMap<>();
  95         } else {
  96             result = new HashMap<>();
  97         }
  98         for (int i = 0; i < registers.length; i++) {
  99             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 }