vm/src/main/java/org/apidesign/vm4brwsr/Variable.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Wed, 12 Dec 2012 11:04:02 +0100
branchregisters
changeset 307 eaf4e8387065
parent 281 f2352e0b713e
child 310 ec7d8bc17725
permissions -rw-r--r--
Type specific local 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     private static final String STACK_VAR_PREFIX = "st";
    22     private static final String LOCAL_VAR_PREFIX = "lc";
    23 
    24     private final String name;
    25     private final int type;
    26     private final int index;
    27 
    28     private static final char[] TYPE_IDS = { 'I', 'L', 'F', 'D', 'A' };
    29 
    30     private Variable(final String prefix, final int type, final int index) {
    31         this.name = prefix + TYPE_IDS[type] + index;
    32         this.type = type;
    33         this.index = index;
    34     }
    35 
    36     public static Variable getStackVariable(
    37             final int type, final int index) {
    38         // TODO: precreate frequently used variables
    39         return new Variable(STACK_VAR_PREFIX, type, index);
    40     }
    41 
    42     public static Variable getLocalVariable(
    43             final int type, final int index) {
    44         // TODO: precreate frequently used variables
    45         return new Variable(LOCAL_VAR_PREFIX, type, index);
    46     }
    47 
    48     public String getName() {
    49         return name;
    50     }
    51 
    52     public int getType() {
    53         return type;
    54     }
    55 
    56     public int getIndex() {
    57         return index;
    58     }
    59 
    60     public boolean isCategory2() {
    61         return VarType.isCategory2(type);
    62     }
    63 
    64     @Override
    65     public int length() {
    66         return name.length();
    67     }
    68 
    69     @Override
    70     public char charAt(final int index) {
    71         return name.charAt(index);
    72     }
    73 
    74     @Override
    75     public CharSequence subSequence(final int start, final int end) {
    76         return name.subSequence(start, end);
    77     }
    78 
    79     @Override
    80     public int hashCode() {
    81         return name.hashCode();
    82     }
    83 
    84     @Override
    85     public boolean equals(final Object other) {
    86         if (this == other) {
    87             return true;
    88         }
    89         if (!(other instanceof Variable)) {
    90             return false;
    91         }
    92 
    93         return name.equals(((Variable) other).name);
    94     }
    95 
    96     @Override
    97     public String toString() {
    98         return name;
    99     }
   100 }