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 jdk.vm.ci.code;
  24 
  25 /**
  26  * Represents a location where a value can be stored. This can be either a {@link Register} or a
  27  * stack slot.
  28  */
  29 public final class Location {
  30 
  31     public final Register reg;
  32     public final int offset;
  33 
  34     private Location(Register reg, int offset) {
  35         this.reg = reg;
  36         this.offset = offset;
  37     }
  38 
  39     /**
  40      * Create a {@link Location} for a register.
  41      */
  42     public static Location register(Register reg) {
  43         return new Location(reg, 0);
  44     }
  45 
  46     /**
  47      * Create a {@link Location} for a vector subregister.
  48      *
  49      * @param reg the {@link Register vector register}
  50      * @param offset the offset in bytes into the vector register
  51      */
  52     public static Location subregister(Register reg, int offset) {
  53         return new Location(reg, offset);
  54     }
  55 
  56     /**
  57      * Create a {@link Location} for a stack slot.
  58      */
  59     public static Location stack(int offset) {
  60         return new Location(null, offset);
  61     }
  62 
  63     public boolean isRegister() {
  64         return reg != null;
  65     }
  66 
  67     public boolean isStack() {
  68         return reg == null;
  69     }
  70 
  71     @Override
  72     public String toString() {
  73         String regName;
  74         if (isRegister()) {
  75             regName = reg.name + ":";
  76         } else {
  77             regName = "stack:";
  78         }
  79         return regName + offset;
  80     }
  81 }