vm/src/main/java/org/apidesign/vm4brwsr/VarType.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Wed, 12 Dec 2012 11:04:02 +0100
branchregisters
changeset 307 eaf4e8387065
child 310 ec7d8bc17725
permissions -rwxr-xr-x
Type specific local variables
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 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 import org.apidesign.javap.RuntimeConstants;
    21 
    22 public final class VarType {
    23     public static final int INTEGER = 0;
    24     public static final int LONG = 1;
    25     public static final int FLOAT = 2;
    26     public static final int DOUBLE = 3;
    27     public static final int REFERENCE = 4;
    28 
    29     public static final int LAST = REFERENCE;
    30 
    31     private VarType() {
    32     }
    33 
    34     public static boolean isCategory2(final int varType) {
    35         return (varType == LONG) || (varType == DOUBLE);
    36     }
    37 
    38     public static int fromStackMapType(final int smType) {
    39         switch (smType & 0xff) {
    40             case RuntimeConstants.ITEM_Integer:
    41                 return VarType.INTEGER;
    42             case RuntimeConstants.ITEM_Float:
    43                 return VarType.FLOAT;
    44             case RuntimeConstants.ITEM_Double:
    45                 return VarType.DOUBLE;
    46             case RuntimeConstants.ITEM_Long:
    47                 return VarType.LONG;
    48             case RuntimeConstants.ITEM_Object:
    49                 return VarType.REFERENCE;
    50 
    51             case RuntimeConstants.ITEM_Bogus:
    52             case RuntimeConstants.ITEM_Null:
    53             case RuntimeConstants.ITEM_InitObject:
    54             case RuntimeConstants.ITEM_NewObject:
    55                 /* unclear how to handle for now */
    56             default:
    57                 throw new IllegalStateException("Unhandled stack map type");
    58         }
    59     }
    60 
    61     public static int fromConstantType(final byte constantTag) {
    62         switch (constantTag) {
    63             case RuntimeConstants.CONSTANT_INTEGER:
    64                 return VarType.INTEGER;
    65             case RuntimeConstants.CONSTANT_FLOAT:
    66                 return VarType.FLOAT;
    67             case RuntimeConstants.CONSTANT_LONG:
    68                 return VarType.LONG;
    69             case RuntimeConstants.CONSTANT_DOUBLE:
    70                 return VarType.DOUBLE;
    71 
    72             case RuntimeConstants.CONSTANT_CLASS:
    73             case RuntimeConstants.CONSTANT_UTF8:
    74             case RuntimeConstants.CONSTANT_UNICODE:
    75             case RuntimeConstants.CONSTANT_STRING:
    76                 return VarType.REFERENCE;
    77 
    78             case RuntimeConstants.CONSTANT_FIELD:
    79             case RuntimeConstants.CONSTANT_METHOD:
    80             case RuntimeConstants.CONSTANT_INTERFACEMETHOD:
    81             case RuntimeConstants.CONSTANT_NAMEANDTYPE:
    82                 /* unclear how to handle for now */
    83             default:
    84                 throw new IllegalStateException("Unhandled constant tag");
    85         }
    86     }
    87 
    88     public static int fromFieldType(final char fieldType) {
    89         switch (fieldType) {
    90             case 'B':
    91             case 'C':
    92             case 'S':
    93             case 'Z':
    94             case 'I':
    95                 return VarType.INTEGER;
    96             case 'J':
    97                 return VarType.LONG;
    98             case 'F':
    99                 return VarType.FLOAT;
   100             case 'D':
   101                 return VarType.DOUBLE;
   102             case 'L':
   103             case '[':
   104                 return VarType.REFERENCE;
   105 
   106             default:
   107                 throw new IllegalStateException("Unhandled field type");
   108         }
   109     }
   110 }