vm/src/main/java/org/apidesign/vm4brwsr/Variable.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Fri, 07 Dec 2012 15:02:35 +0100
branchregisters
changeset 281 f2352e0b713e
child 307 eaf4e8387065
permissions -rw-r--r--
Type specific stack variables
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 public final class Variable implements CharSequence {
    21     public static final int TYPE_INT = 0;
    22     public static final int TYPE_LONG = 1;
    23     public static final int TYPE_FLOAT = 2;
    24     public static final int TYPE_DOUBLE = 3;
    25     public static final int TYPE_REF = 4;
    26 
    27     public static final int LAST_TYPE = TYPE_REF;
    28 
    29     private static final String STACK_VAR_PREFIX = "st";
    30 
    31     private final String name;
    32     private final int type;
    33     private final int index;
    34 
    35     private static final char[] TYPE_IDS = { 'I', 'L', 'F', 'D', 'A' };
    36 
    37     private Variable(final String prefix, final int type, final int index) {
    38         this.name = prefix + TYPE_IDS[type] + index;
    39         this.type = type;
    40         this.index = index;
    41     }
    42 
    43     public static Variable getStackVariable(
    44             final int type, final int index) {
    45         // TODO: precreate frequently used variables
    46         return new Variable(STACK_VAR_PREFIX, type, index);
    47     }
    48 
    49     public String getName() {
    50         return name;
    51     }
    52 
    53     public int getType() {
    54         return type;
    55     }
    56 
    57     public int getIndex() {
    58         return index;
    59     }
    60 
    61     public boolean isCategory2() {
    62         return (type == TYPE_LONG) || (type == TYPE_DOUBLE);
    63     }
    64 
    65     @Override
    66     public int length() {
    67         return name.length();
    68     }
    69 
    70     @Override
    71     public char charAt(final int index) {
    72         return name.charAt(index);
    73     }
    74 
    75     @Override
    76     public CharSequence subSequence(final int start, final int end) {
    77         return name.subSequence(start, end);
    78     }
    79 
    80     @Override
    81     public int hashCode() {
    82         return name.hashCode();
    83     }
    84 
    85     @Override
    86     public boolean equals(final Object other) {
    87         if (this == other) {
    88             return true;
    89         }
    90         if (!(other instanceof Variable)) {
    91             return false;
    92         }
    93 
    94         return name.equals(((Variable) other).name);
    95     }
    96 
    97     @Override
    98     public String toString() {
    99         return name;
   100     }
   101 }