1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Variable.java Fri Dec 07 15:02:35 2012 +0100
1.3 @@ -0,0 +1,101 @@
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 +public final class Variable implements CharSequence {
1.24 + public static final int TYPE_INT = 0;
1.25 + public static final int TYPE_LONG = 1;
1.26 + public static final int TYPE_FLOAT = 2;
1.27 + public static final int TYPE_DOUBLE = 3;
1.28 + public static final int TYPE_REF = 4;
1.29 +
1.30 + public static final int LAST_TYPE = TYPE_REF;
1.31 +
1.32 + private static final String STACK_VAR_PREFIX = "st";
1.33 +
1.34 + private final String name;
1.35 + private final int type;
1.36 + private final int index;
1.37 +
1.38 + private static final char[] TYPE_IDS = { 'I', 'L', 'F', 'D', 'A' };
1.39 +
1.40 + private Variable(final String prefix, final int type, final int index) {
1.41 + this.name = prefix + TYPE_IDS[type] + index;
1.42 + this.type = type;
1.43 + this.index = index;
1.44 + }
1.45 +
1.46 + public static Variable getStackVariable(
1.47 + final int type, final int index) {
1.48 + // TODO: precreate frequently used variables
1.49 + return new Variable(STACK_VAR_PREFIX, type, index);
1.50 + }
1.51 +
1.52 + public String getName() {
1.53 + return name;
1.54 + }
1.55 +
1.56 + public int getType() {
1.57 + return type;
1.58 + }
1.59 +
1.60 + public int getIndex() {
1.61 + return index;
1.62 + }
1.63 +
1.64 + public boolean isCategory2() {
1.65 + return (type == TYPE_LONG) || (type == TYPE_DOUBLE);
1.66 + }
1.67 +
1.68 + @Override
1.69 + public int length() {
1.70 + return name.length();
1.71 + }
1.72 +
1.73 + @Override
1.74 + public char charAt(final int index) {
1.75 + return name.charAt(index);
1.76 + }
1.77 +
1.78 + @Override
1.79 + public CharSequence subSequence(final int start, final int end) {
1.80 + return name.subSequence(start, end);
1.81 + }
1.82 +
1.83 + @Override
1.84 + public int hashCode() {
1.85 + return name.hashCode();
1.86 + }
1.87 +
1.88 + @Override
1.89 + public boolean equals(final Object other) {
1.90 + if (this == other) {
1.91 + return true;
1.92 + }
1.93 + if (!(other instanceof Variable)) {
1.94 + return false;
1.95 + }
1.96 +
1.97 + return name.equals(((Variable) other).name);
1.98 + }
1.99 +
1.100 + @Override
1.101 + public String toString() {
1.102 + return name;
1.103 + }
1.104 +}