1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Variable.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,100 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.vm4brwsr;
1.22 +
1.23 +final class Variable implements CharSequence {
1.24 + private static final String STACK_VAR_PREFIX = "st";
1.25 + private static final String LOCAL_VAR_PREFIX = "lc";
1.26 +
1.27 + private final String name;
1.28 + private final int type;
1.29 + private final int index;
1.30 +
1.31 + private static final char[] TYPE_IDS = { 'I', 'L', 'F', 'D', 'A' };
1.32 +
1.33 + private Variable(final String prefix, final int type, final int index) {
1.34 + this.name = prefix + TYPE_IDS[type] + index;
1.35 + this.type = type;
1.36 + this.index = index;
1.37 + }
1.38 +
1.39 + public static Variable getStackVariable(
1.40 + final int type, final int index) {
1.41 + // TODO: precreate frequently used variables
1.42 + return new Variable(STACK_VAR_PREFIX, type, index);
1.43 + }
1.44 +
1.45 + public static Variable getLocalVariable(
1.46 + final int type, final int index) {
1.47 + // TODO: precreate frequently used variables
1.48 + return new Variable(LOCAL_VAR_PREFIX, type, index);
1.49 + }
1.50 +
1.51 + public String getName() {
1.52 + return name;
1.53 + }
1.54 +
1.55 + public int getType() {
1.56 + return type;
1.57 + }
1.58 +
1.59 + public int getIndex() {
1.60 + return index;
1.61 + }
1.62 +
1.63 + public boolean isCategory2() {
1.64 + return VarType.isCategory2(type);
1.65 + }
1.66 +
1.67 + @Override
1.68 + public int length() {
1.69 + return name.length();
1.70 + }
1.71 +
1.72 + @Override
1.73 + public char charAt(final int index) {
1.74 + return name.charAt(index);
1.75 + }
1.76 +
1.77 + @Override
1.78 + public CharSequence subSequence(final int start, final int end) {
1.79 + return name.subSequence(start, end);
1.80 + }
1.81 +
1.82 + @Override
1.83 + public int hashCode() {
1.84 + return name.hashCode();
1.85 + }
1.86 +
1.87 + @Override
1.88 + public boolean equals(final Object other) {
1.89 + if (this == other) {
1.90 + return true;
1.91 + }
1.92 + if (!(other instanceof Variable)) {
1.93 + return false;
1.94 + }
1.95 +
1.96 + return name.equals(((Variable) other).name);
1.97 + }
1.98 +
1.99 + @Override
1.100 + public String toString() {
1.101 + return name;
1.102 + }
1.103 +}