1 /*
   2  * Copyright (c) 2012, 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.debug.internal;
  24 
  25 /**
  26  * A name and index for a value managed in a thread local value map. All access to the value is made
  27  * via a {@link DebugValue} instance.
  28  */
  29 public abstract class DebugValue implements Comparable<DebugValue> {
  30 
  31     private final String name;
  32     private int index;
  33     private boolean conditional;
  34 
  35     protected DebugValue(String name, boolean conditional) {
  36         this.name = name;
  37         this.index = -1;
  38         this.conditional = conditional;
  39     }
  40 
  41     public long getCurrentValue() {
  42         ensureInitialized();
  43         return DebugScope.getInstance().getCurrentValue(index);
  44     }
  45 
  46     protected void setCurrentValue(long l) {
  47         ensureInitialized();
  48         DebugScope.getInstance().setCurrentValue(index, l);
  49     }
  50 
  51     public void setConditional(boolean flag) {
  52         conditional = flag;
  53     }
  54 
  55     public boolean isConditional() {
  56         return conditional;
  57     }
  58 
  59     private void ensureInitialized() {
  60         if (index == -1) {
  61             index = KeyRegistry.register(this);
  62         }
  63     }
  64 
  65     protected void addToCurrentValue(long value) {
  66         setCurrentValue(getCurrentValue() + value);
  67     }
  68 
  69     /**
  70      * Gets the globally unique index for the value represented by this object.
  71      */
  72     public int getIndex() {
  73         ensureInitialized();
  74         return index;
  75     }
  76 
  77     /**
  78      * Gets the globally unique name for the value represented by this object.
  79      */
  80     public String getName() {
  81         return name;
  82     }
  83 
  84     @Override
  85     public int compareTo(DebugValue o) {
  86         return name.compareTo(o.name);
  87     }
  88 
  89     @Override
  90     public String toString() {
  91         return name + "@" + index;
  92     }
  93 
  94     public abstract String toString(long value);
  95 
  96     /**
  97      * The raw unit of the value (e.g. 'us', 'ms'). Use {@code ""} if there is no unit.
  98      */
  99     public abstract String rawUnit();
 100 
 101     /**
 102      * The raw value.
 103      */
 104     public abstract String toRawString(long value);
 105 }