1 /*
   2  * Copyright (c) 2010, 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 org.graalvm.compiler.lir;
  24 
  25 import jdk.vm.ci.code.RegisterValue;
  26 import jdk.vm.ci.code.StackSlot;
  27 import jdk.vm.ci.meta.AllocatableValue;
  28 import jdk.vm.ci.meta.ValueKind;
  29 
  30 /**
  31  * Represents a value that is yet to be bound to a machine location (such as a {@link RegisterValue}
  32  * or {@link StackSlot}) by a register allocator.
  33  */
  34 public final class Variable extends AllocatableValue {
  35 
  36     /**
  37      * The identifier of the variable. This is a non-zero index in a contiguous 0-based name space.
  38      */
  39     public final int index;
  40 
  41     private String name;
  42 
  43     /**
  44      * Creates a new variable.
  45      *
  46      * @param kind
  47      * @param index
  48      */
  49     public Variable(ValueKind<?> kind, int index) {
  50         super(kind);
  51         assert index >= 0;
  52         this.index = index;
  53     }
  54 
  55     public void setName(String name) {
  56         this.name = name;
  57     }
  58 
  59     public String getName() {
  60         return name;
  61     }
  62 
  63     @Override
  64     public String toString() {
  65         if (name != null) {
  66             return name;
  67         } else {
  68             return "v" + index + getKindSuffix();
  69         }
  70     }
  71 
  72     @Override
  73     public int hashCode() {
  74         return 71 * super.hashCode() + index;
  75     }
  76 
  77     @Override
  78     public boolean equals(Object obj) {
  79         if (obj instanceof Variable) {
  80             Variable other = (Variable) obj;
  81             return super.equals(other) && index == other.index;
  82         }
  83         return false;
  84     }
  85 }