rt/vm/src/main/java/org/apidesign/vm4brwsr/VarType.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 04 Mar 2013 19:20:40 +0100
changeset 810 9eb750594b15
parent 772 d382dacfd73f
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Moving the bytecode parser into a single class inside the vm module - Nexus does not like modules without javadoc and I certainly don't want to publish javadoc for the former javap
lubomir@319
     1
/**
lubomir@319
     2
 * Back 2 Browser Bytecode Translator
lubomir@319
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
lubomir@319
     4
 *
lubomir@319
     5
 * This program is free software: you can redistribute it and/or modify
lubomir@319
     6
 * it under the terms of the GNU General Public License as published by
lubomir@319
     7
 * the Free Software Foundation, version 2 of the License.
lubomir@319
     8
 *
lubomir@319
     9
 * This program is distributed in the hope that it will be useful,
lubomir@319
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
lubomir@319
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
lubomir@319
    12
 * GNU General Public License for more details.
lubomir@319
    13
 *
lubomir@319
    14
 * You should have received a copy of the GNU General Public License
lubomir@319
    15
 * along with this program. Look for COPYING file in the top folder.
lubomir@319
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
lubomir@319
    17
 */
lubomir@319
    18
package org.apidesign.vm4brwsr;
lubomir@319
    19
lubomir@319
    20
final class VarType {
lubomir@319
    21
    public static final int INTEGER = 0;
lubomir@319
    22
    public static final int LONG = 1;
lubomir@319
    23
    public static final int FLOAT = 2;
lubomir@319
    24
    public static final int DOUBLE = 3;
lubomir@319
    25
    public static final int REFERENCE = 4;
lubomir@319
    26
lubomir@319
    27
    public static final int LAST = REFERENCE;
lubomir@319
    28
lubomir@319
    29
    private VarType() {
lubomir@319
    30
    }
lubomir@319
    31
lubomir@319
    32
    public static boolean isCategory2(final int varType) {
lubomir@319
    33
        return (varType == LONG) || (varType == DOUBLE);
lubomir@319
    34
    }
lubomir@319
    35
lubomir@319
    36
    public static int fromStackMapType(final int smType) {
lubomir@319
    37
        switch (smType & 0xff) {
jaroslav@810
    38
            case ByteCodeParser.ITEM_Integer:
lubomir@319
    39
                return VarType.INTEGER;
jaroslav@810
    40
            case ByteCodeParser.ITEM_Float:
lubomir@319
    41
                return VarType.FLOAT;
jaroslav@810
    42
            case ByteCodeParser.ITEM_Double:
lubomir@319
    43
                return VarType.DOUBLE;
jaroslav@810
    44
            case ByteCodeParser.ITEM_Long:
lubomir@319
    45
                return VarType.LONG;
jaroslav@810
    46
            case ByteCodeParser.ITEM_Null:
jaroslav@810
    47
            case ByteCodeParser.ITEM_InitObject:
jaroslav@810
    48
            case ByteCodeParser.ITEM_Object:
jaroslav@810
    49
            case ByteCodeParser.ITEM_NewObject:
lubomir@319
    50
                return VarType.REFERENCE;
lubomir@319
    51
jaroslav@810
    52
            case ByteCodeParser.ITEM_Bogus:
lubomir@319
    53
                /* unclear how to handle for now */
lubomir@319
    54
            default:
lubomir@319
    55
                throw new IllegalStateException("Unhandled stack map type");
lubomir@319
    56
        }
lubomir@319
    57
    }
lubomir@319
    58
lubomir@319
    59
    public static int fromConstantType(final byte constantTag) {
lubomir@319
    60
        switch (constantTag) {
jaroslav@810
    61
            case ByteCodeParser.CONSTANT_INTEGER:
lubomir@319
    62
                return VarType.INTEGER;
jaroslav@810
    63
            case ByteCodeParser.CONSTANT_FLOAT:
lubomir@319
    64
                return VarType.FLOAT;
jaroslav@810
    65
            case ByteCodeParser.CONSTANT_LONG:
lubomir@319
    66
                return VarType.LONG;
jaroslav@810
    67
            case ByteCodeParser.CONSTANT_DOUBLE:
lubomir@319
    68
                return VarType.DOUBLE;
lubomir@319
    69
jaroslav@810
    70
            case ByteCodeParser.CONSTANT_CLASS:
jaroslav@810
    71
            case ByteCodeParser.CONSTANT_UTF8:
jaroslav@810
    72
            case ByteCodeParser.CONSTANT_UNICODE:
jaroslav@810
    73
            case ByteCodeParser.CONSTANT_STRING:
lubomir@319
    74
                return VarType.REFERENCE;
lubomir@319
    75
jaroslav@810
    76
            case ByteCodeParser.CONSTANT_FIELD:
jaroslav@810
    77
            case ByteCodeParser.CONSTANT_METHOD:
jaroslav@810
    78
            case ByteCodeParser.CONSTANT_INTERFACEMETHOD:
jaroslav@810
    79
            case ByteCodeParser.CONSTANT_NAMEANDTYPE:
lubomir@319
    80
                /* unclear how to handle for now */
lubomir@319
    81
            default:
lubomir@319
    82
                throw new IllegalStateException("Unhandled constant tag");
lubomir@319
    83
        }
lubomir@319
    84
    }
lubomir@319
    85
lubomir@319
    86
    public static int fromFieldType(final char fieldType) {
lubomir@319
    87
        switch (fieldType) {
lubomir@319
    88
            case 'B':
lubomir@319
    89
            case 'C':
lubomir@319
    90
            case 'S':
lubomir@319
    91
            case 'Z':
lubomir@319
    92
            case 'I':
lubomir@319
    93
                return VarType.INTEGER;
lubomir@319
    94
            case 'J':
lubomir@319
    95
                return VarType.LONG;
lubomir@319
    96
            case 'F':
lubomir@319
    97
                return VarType.FLOAT;
lubomir@319
    98
            case 'D':
lubomir@319
    99
                return VarType.DOUBLE;
lubomir@319
   100
            case 'L':
lubomir@319
   101
            case '[':
lubomir@319
   102
                return VarType.REFERENCE;
lubomir@319
   103
lubomir@319
   104
            default:
lubomir@319
   105
                throw new IllegalStateException("Unhandled field type");
lubomir@319
   106
        }
lubomir@319
   107
    }
lubomir@319
   108
}