rt/vm/src/main/java/org/apidesign/vm4brwsr/VarType.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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 final class VarType {
    21     public static final int INTEGER = 0;
    22     public static final int LONG = 1;
    23     public static final int FLOAT = 2;
    24     public static final int DOUBLE = 3;
    25     public static final int REFERENCE = 4;
    26 
    27     public static final int LAST = REFERENCE;
    28 
    29     private VarType() {
    30     }
    31 
    32     public static boolean isCategory2(final int varType) {
    33         return (varType == LONG) || (varType == DOUBLE);
    34     }
    35 
    36     public static int fromStackMapType(final int smType) {
    37         switch (smType & 0xff) {
    38             case ByteCodeParser.ITEM_Integer:
    39                 return VarType.INTEGER;
    40             case ByteCodeParser.ITEM_Float:
    41                 return VarType.FLOAT;
    42             case ByteCodeParser.ITEM_Double:
    43                 return VarType.DOUBLE;
    44             case ByteCodeParser.ITEM_Long:
    45                 return VarType.LONG;
    46             case ByteCodeParser.ITEM_Null:
    47             case ByteCodeParser.ITEM_InitObject:
    48             case ByteCodeParser.ITEM_Object:
    49             case ByteCodeParser.ITEM_NewObject:
    50                 return VarType.REFERENCE;
    51 
    52             case ByteCodeParser.ITEM_Bogus:
    53                 /* unclear how to handle for now */
    54             default:
    55                 throw new IllegalStateException("Unhandled stack map type");
    56         }
    57     }
    58 
    59     public static int fromConstantType(final byte constantTag) {
    60         switch (constantTag) {
    61             case ByteCodeParser.CONSTANT_INTEGER:
    62                 return VarType.INTEGER;
    63             case ByteCodeParser.CONSTANT_FLOAT:
    64                 return VarType.FLOAT;
    65             case ByteCodeParser.CONSTANT_LONG:
    66                 return VarType.LONG;
    67             case ByteCodeParser.CONSTANT_DOUBLE:
    68                 return VarType.DOUBLE;
    69 
    70             case ByteCodeParser.CONSTANT_CLASS:
    71             case ByteCodeParser.CONSTANT_UTF8:
    72             case ByteCodeParser.CONSTANT_UNICODE:
    73             case ByteCodeParser.CONSTANT_STRING:
    74                 return VarType.REFERENCE;
    75 
    76             case ByteCodeParser.CONSTANT_FIELD:
    77             case ByteCodeParser.CONSTANT_METHOD:
    78             case ByteCodeParser.CONSTANT_INTERFACEMETHOD:
    79             case ByteCodeParser.CONSTANT_NAMEANDTYPE:
    80                 /* unclear how to handle for now */
    81             default:
    82                 throw new IllegalStateException("Unhandled constant tag");
    83         }
    84     }
    85 
    86     public static int fromFieldType(final char fieldType) {
    87         switch (fieldType) {
    88             case 'B':
    89             case 'C':
    90             case 'S':
    91             case 'Z':
    92             case 'I':
    93                 return VarType.INTEGER;
    94             case 'J':
    95                 return VarType.LONG;
    96             case 'F':
    97                 return VarType.FLOAT;
    98             case 'D':
    99                 return VarType.DOUBLE;
   100             case 'L':
   101             case '[':
   102                 return VarType.REFERENCE;
   103 
   104             default:
   105                 throw new IllegalStateException("Unhandled field type");
   106         }
   107     }
   108     
   109     public static String toString(final int varType) {
   110         switch (varType) {
   111             case VarType.INTEGER: return "int";
   112             case VarType.LONG: return "long";
   113             case VarType.FLOAT: return "float";
   114             case VarType.DOUBLE: return "double";
   115             case VarType.REFERENCE: return "ref";
   116             default:
   117                 throw new IllegalStateException("Unhandled field type");
   118         }
   119     }
   120 }