vm/src/main/java/org/apidesign/vm4brwsr/VarType.java
branchregisters
changeset 307 eaf4e8387065
child 310 ec7d8bc17725
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VarType.java	Wed Dec 12 11:04:02 2012 +0100
     1.3 @@ -0,0 +1,110 @@
     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 +import org.apidesign.javap.RuntimeConstants;
    1.24 +
    1.25 +public final class VarType {
    1.26 +    public static final int INTEGER = 0;
    1.27 +    public static final int LONG = 1;
    1.28 +    public static final int FLOAT = 2;
    1.29 +    public static final int DOUBLE = 3;
    1.30 +    public static final int REFERENCE = 4;
    1.31 +
    1.32 +    public static final int LAST = REFERENCE;
    1.33 +
    1.34 +    private VarType() {
    1.35 +    }
    1.36 +
    1.37 +    public static boolean isCategory2(final int varType) {
    1.38 +        return (varType == LONG) || (varType == DOUBLE);
    1.39 +    }
    1.40 +
    1.41 +    public static int fromStackMapType(final int smType) {
    1.42 +        switch (smType & 0xff) {
    1.43 +            case RuntimeConstants.ITEM_Integer:
    1.44 +                return VarType.INTEGER;
    1.45 +            case RuntimeConstants.ITEM_Float:
    1.46 +                return VarType.FLOAT;
    1.47 +            case RuntimeConstants.ITEM_Double:
    1.48 +                return VarType.DOUBLE;
    1.49 +            case RuntimeConstants.ITEM_Long:
    1.50 +                return VarType.LONG;
    1.51 +            case RuntimeConstants.ITEM_Object:
    1.52 +                return VarType.REFERENCE;
    1.53 +
    1.54 +            case RuntimeConstants.ITEM_Bogus:
    1.55 +            case RuntimeConstants.ITEM_Null:
    1.56 +            case RuntimeConstants.ITEM_InitObject:
    1.57 +            case RuntimeConstants.ITEM_NewObject:
    1.58 +                /* unclear how to handle for now */
    1.59 +            default:
    1.60 +                throw new IllegalStateException("Unhandled stack map type");
    1.61 +        }
    1.62 +    }
    1.63 +
    1.64 +    public static int fromConstantType(final byte constantTag) {
    1.65 +        switch (constantTag) {
    1.66 +            case RuntimeConstants.CONSTANT_INTEGER:
    1.67 +                return VarType.INTEGER;
    1.68 +            case RuntimeConstants.CONSTANT_FLOAT:
    1.69 +                return VarType.FLOAT;
    1.70 +            case RuntimeConstants.CONSTANT_LONG:
    1.71 +                return VarType.LONG;
    1.72 +            case RuntimeConstants.CONSTANT_DOUBLE:
    1.73 +                return VarType.DOUBLE;
    1.74 +
    1.75 +            case RuntimeConstants.CONSTANT_CLASS:
    1.76 +            case RuntimeConstants.CONSTANT_UTF8:
    1.77 +            case RuntimeConstants.CONSTANT_UNICODE:
    1.78 +            case RuntimeConstants.CONSTANT_STRING:
    1.79 +                return VarType.REFERENCE;
    1.80 +
    1.81 +            case RuntimeConstants.CONSTANT_FIELD:
    1.82 +            case RuntimeConstants.CONSTANT_METHOD:
    1.83 +            case RuntimeConstants.CONSTANT_INTERFACEMETHOD:
    1.84 +            case RuntimeConstants.CONSTANT_NAMEANDTYPE:
    1.85 +                /* unclear how to handle for now */
    1.86 +            default:
    1.87 +                throw new IllegalStateException("Unhandled constant tag");
    1.88 +        }
    1.89 +    }
    1.90 +
    1.91 +    public static int fromFieldType(final char fieldType) {
    1.92 +        switch (fieldType) {
    1.93 +            case 'B':
    1.94 +            case 'C':
    1.95 +            case 'S':
    1.96 +            case 'Z':
    1.97 +            case 'I':
    1.98 +                return VarType.INTEGER;
    1.99 +            case 'J':
   1.100 +                return VarType.LONG;
   1.101 +            case 'F':
   1.102 +                return VarType.FLOAT;
   1.103 +            case 'D':
   1.104 +                return VarType.DOUBLE;
   1.105 +            case 'L':
   1.106 +            case '[':
   1.107 +                return VarType.REFERENCE;
   1.108 +
   1.109 +            default:
   1.110 +                throw new IllegalStateException("Unhandled field type");
   1.111 +        }
   1.112 +    }
   1.113 +}