jdk/src/share/native/common/check_code.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1090 c5805b1672a6
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *      Verify that the code within a method block doesn't exploit any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *      security holes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
   Exported function:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
   jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
   VerifyClass(JNIEnv *env, jclass cb, char *message_buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
               jint buffer_length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
   jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
   VerifyClassForMajorVersion(JNIEnv *env, jclass cb, char *message_buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
                              jint buffer_length, jint major_version)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
   This file now only uses the standard JNI and the following VM functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
   exported in jvm.h:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
   JVM_FindClassFromClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
   JVM_IsInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
   JVM_GetClassNameUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
   JVM_GetClassCPEntriesCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
   JVM_GetClassCPTypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
   JVM_GetClassFieldsCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
   JVM_GetClassMethodsCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
   JVM_GetFieldIxModifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
   JVM_GetMethodIxModifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
   JVM_GetMethodIxExceptionTableLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
   JVM_GetMethodIxLocalsCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
   JVM_GetMethodIxArgsSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
   JVM_GetMethodIxMaxStack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
   JVM_GetMethodIxNameUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
   JVM_GetMethodIxSignatureUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
   JVM_GetMethodIxExceptionsCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
   JVM_GetMethodIxExceptionIndexes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
   JVM_GetMethodIxByteCodeLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
   JVM_GetMethodIxByteCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
   JVM_GetMethodIxExceptionTableEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
   JVM_IsConstructorIx
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
   JVM_GetCPClassNameUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
   JVM_GetCPFieldNameUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
   JVM_GetCPMethodNameUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
   JVM_GetCPFieldSignatureUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
   JVM_GetCPMethodSignatureUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
   JVM_GetCPFieldClassNameUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
   JVM_GetCPMethodClassNameUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
   JVM_GetCPFieldModifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
   JVM_GetCPMethodModifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
   JVM_ReleaseUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
   JVM_IsSameClassPackage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
#include <setjmp.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
#include "typedefs.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
#include "opcodes.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
#include "opcodes.length"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
#include "opcodes.in_out"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
#define MAX_ARRAY_DIMENSIONS 255
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
/* align byte code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
#ifndef ALIGN_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
#define ALIGN_UP(n,align_grain) (((n) + ((align_grain) - 1)) & ~((align_grain)-1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
#endif /* ALIGN_UP */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
#define UCALIGN(n) ((unsigned char *)ALIGN_UP((uintptr_t)(n),sizeof(int)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
int verify_verbose = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
static struct context_type *GlobalContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
enum {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    ITEM_Bogus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    ITEM_Void,                  /* only as a function return value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    ITEM_Integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    ITEM_Float,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    ITEM_Double,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    ITEM_Double_2,              /* 2nd word of double in register */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    ITEM_Long,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    ITEM_Long_2,                /* 2nd word of long in register */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    ITEM_Array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    ITEM_Object,                /* Extra info field gives name. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    ITEM_NewObject,             /* Like object, but uninitialized. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    ITEM_InitObject,            /* "this" is init method, before call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                    to super() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    ITEM_ReturnAddress,         /* Extra info gives instr # of start pc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /* The following three are only used within array types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Normally, we use ITEM_Integer, instead. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    ITEM_Byte,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    ITEM_Short,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    ITEM_Char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
#define UNKNOWN_STACK_SIZE -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
#define UNKNOWN_REGISTER_COUNT -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
#define UNKNOWN_RET_INSTRUCTION -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
#undef MAX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
#undef MIN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
#define MAX(a, b) ((a) > (b) ? (a) : (b))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
#define MIN(a, b) ((a) < (b) ? (a) : (b))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
#define BITS_PER_INT   (CHAR_BIT * sizeof(int)/sizeof(char))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
#define SET_BIT(flags, i)  (flags[(i)/BITS_PER_INT] |= \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                       ((unsigned)1 << ((i) % BITS_PER_INT)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
#define IS_BIT_SET(flags, i) (flags[(i)/BITS_PER_INT] & \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                       ((unsigned)1 << ((i) % BITS_PER_INT)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
typedef unsigned int fullinfo_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
typedef unsigned int *bitvector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
#define GET_ITEM_TYPE(thing) ((thing) & 0x1F)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
#define GET_INDIRECTION(thing) (((thing) & 0xFFFF) >> 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
#define GET_EXTRA_INFO(thing) ((thing) >> 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
#define WITH_ZERO_INDIRECTION(thing) ((thing) & ~(0xFFE0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
#define WITH_ZERO_EXTRA_INFO(thing) ((thing) & 0xFFFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
#define MAKE_FULLINFO(type, indirect, extra) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     ((type) + ((indirect) << 5) + ((extra) << 16))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
#define MAKE_Object_ARRAY(indirect) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
       (context->object_info + ((indirect) << 5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
#define NULL_FULLINFO MAKE_FULLINFO(ITEM_Object, 0, 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
/* opc_invokespecial calls to <init> need to be treated special */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
#define opc_invokeinit 0x100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
/* A hash mechanism used by the verifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * Maps class names to unique 16 bit integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
#define HASH_TABLE_SIZE 503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
/* The buckets are managed as a 256 by 256 matrix. We allocate an entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * row (256 buckets) at a time to minimize fragmentation. Rows are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * allocated on demand so that we don't waste too much space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
#define MAX_HASH_ENTRIES 65536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
#define HASH_ROW_SIZE 256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
typedef struct hash_bucket_type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    char *name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    unsigned int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    jclass class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    unsigned short ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    unsigned short next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    unsigned loadable:1;  /* from context->class loader */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
} hash_bucket_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    hash_bucket_type **buckets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    unsigned short *table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    int entries_used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
} hash_table_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
#define GET_BUCKET(class_hash, ID)\
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    (class_hash->buckets[ID / HASH_ROW_SIZE] + ID % HASH_ROW_SIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * There are currently two types of resources that we need to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * track of (in addition to the CCalloc pool).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
enum {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    VM_STRING_UTF, /* VM-allocated UTF strings */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    VM_MALLOC_BLK  /* malloc'ed blocks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
#define LDC_CLASS_MAJOR_VERSION 49
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
#define ALLOC_STACK_SIZE 16 /* big enough */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
typedef struct alloc_stack_type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    void *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    int kind;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    struct alloc_stack_type *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
} alloc_stack_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
/* The context type encapsulates the current invocation of the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * code verifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
struct context_type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    JNIEnv *env;                /* current JNIEnv */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /* buffers etc. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    char *message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    jint message_buf_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    jboolean err_code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    alloc_stack_type *allocated_memory; /* all memory blocks that we have not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                           had a chance to free */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /* Store up to ALLOC_STACK_SIZE number of handles to allocated memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
       blocks here, to save mallocs. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    alloc_stack_type alloc_stack[ALLOC_STACK_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    int alloc_stack_top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /* these fields are per class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    jclass class;               /* current class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    jint major_version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    jint nconstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    unsigned char *constant_types;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    hash_table_type class_hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    fullinfo_type object_info;  /* fullinfo for java/lang/Object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    fullinfo_type string_info;  /* fullinfo for java/lang/String */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    fullinfo_type throwable_info; /* fullinfo for java/lang/Throwable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    fullinfo_type cloneable_info; /* fullinfo for java/lang/Cloneable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    fullinfo_type serializable_info; /* fullinfo for java/io/Serializable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    fullinfo_type currentclass_info; /* fullinfo for context->class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    fullinfo_type superclass_info;   /* fullinfo for superclass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /* these fields are per method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    int method_index;   /* current method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    unsigned short *exceptions; /* exceptions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    unsigned char *code;        /* current code object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    jint code_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    int *code_data;             /* offset to instruction number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    struct instruction_data_type *instruction_data; /* info about each */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    struct handler_info_type *handler_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    fullinfo_type *superclasses; /* null terminated superclasses */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    int instruction_count;      /* number of instructions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    fullinfo_type return_type;  /* function return type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    fullinfo_type swap_table[4]; /* used for passing information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    int bitmask_size;           /* words needed to hold bitmap of arguments */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /* these fields are per field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    int field_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /* Used by the space allocator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    struct CCpool *CCroot, *CCcurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    char *CCfree_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    int CCfree_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /* Jump here on any error. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    jmp_buf jump_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /* keep track of how many global refs are allocated. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    int n_globalrefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
struct stack_info_type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    struct stack_item_type *stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    int stack_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
struct register_info_type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    int register_count;         /* number of registers used */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    fullinfo_type *registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    int mask_count;             /* number of masks in the following */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    struct mask_type *masks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
struct mask_type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    int entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    int *modifies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
typedef unsigned short flag_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
struct instruction_data_type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    opcode_type opcode;         /* may turn into "canonical" opcode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    unsigned changed:1;         /* has it changed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    unsigned protected:1;       /* must accessor be a subclass of "this" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        int i;                  /* operand to the opcode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        int *ip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        fullinfo_type fi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    } operand, operand2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    fullinfo_type p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    struct stack_info_type stack_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    struct register_info_type register_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
#define FLAG_REACHED            0x01 /* instruction reached */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
#define FLAG_NEED_CONSTRUCTOR   0x02 /* must call this.<init> or super.<init> */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
#define FLAG_NO_RETURN          0x04 /* must throw out of method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    flag_type or_flags;         /* true for at least one path to this inst */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
#define FLAG_CONSTRUCTED        0x01 /* this.<init> or super.<init> called */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    flag_type and_flags;        /* true for all paths to this instruction */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
struct handler_info_type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    int start, end, handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    struct stack_info_type stack_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
struct stack_item_type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    fullinfo_type item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    struct stack_item_type *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
typedef struct context_type context_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
typedef struct instruction_data_type instruction_data_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
typedef struct stack_item_type stack_item_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
typedef struct register_info_type register_info_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
typedef struct stack_info_type stack_info_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
typedef struct mask_type mask_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
static void read_all_code(context_type *context, jclass cb, int num_methods,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                          int** code_lengths, unsigned char*** code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
static void verify_method(context_type *context, jclass cb, int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                          int code_length, unsigned char* code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
static void free_all_code(int num_methods, int* lengths, unsigned char** code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
static void verify_field(context_type *context, jclass cb, int index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
static void verify_opcode_operands (context_type *, unsigned int inumber, int offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
static void set_protected(context_type *, unsigned int inumber, int key, opcode_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
static jboolean is_superclass(context_type *, fullinfo_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
static void initialize_exception_table(context_type *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
static int instruction_length(unsigned char *iptr, unsigned char *end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
static jboolean isLegalTarget(context_type *, int offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
static void verify_constant_pool_type(context_type *, int, unsigned);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
static void initialize_dataflow(context_type *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
static void run_dataflow(context_type *context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
static void check_register_values(context_type *context, unsigned int inumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
static void check_flags(context_type *context, unsigned int inumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
static void pop_stack(context_type *, unsigned int inumber, stack_info_type *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
static void update_registers(context_type *, unsigned int inumber, register_info_type *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
static void update_flags(context_type *, unsigned int inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                         flag_type *new_and_flags, flag_type *new_or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
static void push_stack(context_type *, unsigned int inumber, stack_info_type *stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
static void merge_into_successors(context_type *, unsigned int inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                  register_info_type *register_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                  stack_info_type *stack_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                  flag_type and_flags, flag_type or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
static void merge_into_one_successor(context_type *context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                                     unsigned int from_inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                     unsigned int inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                     register_info_type *register_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                     stack_info_type *stack_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                     flag_type and_flags, flag_type or_flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                     jboolean isException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
static void merge_stack(context_type *, unsigned int inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        unsigned int to_inumber, stack_info_type *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
static void merge_registers(context_type *, unsigned int inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                            unsigned int to_inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                            register_info_type *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
static void merge_flags(context_type *context, unsigned int from_inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        unsigned int to_inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        flag_type new_and_flags, flag_type new_or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
static stack_item_type *copy_stack(context_type *, stack_item_type *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
static mask_type *copy_masks(context_type *, mask_type *masks, int mask_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
static mask_type *add_to_masks(context_type *, mask_type *, int , int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
static fullinfo_type decrement_indirection(fullinfo_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
static fullinfo_type merge_fullinfo_types(context_type *context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                          fullinfo_type a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                          fullinfo_type b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                          jboolean assignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
static jboolean isAssignableTo(context_type *,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                               fullinfo_type a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                               fullinfo_type b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
static jclass object_fullinfo_to_classclass(context_type *, fullinfo_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
#define NEW(type, count) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        ((type *)CCalloc(context, (count)*(sizeof(type)), JNI_FALSE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
#define ZNEW(type, count) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        ((type *)CCalloc(context, (count)*(sizeof(type)), JNI_TRUE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
static void CCinit(context_type *context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
static void CCreinit(context_type *context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
static void CCdestroy(context_type *context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
static void *CCalloc(context_type *context, int size, jboolean zero);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
static fullinfo_type cp_index_to_class_fullinfo(context_type *, int, int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
static char signature_to_fieldtype(context_type *context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                   const char **signature_p, fullinfo_type *info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
static void CCerror (context_type *, char *format, ...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
static void CFerror (context_type *, char *format, ...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
static void CCout_of_memory (context_type *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
/* Because we can longjmp any time, we need to be very careful about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
 * remembering what needs to be freed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
static void check_and_push(context_type *context, const void *ptr, int kind);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
static void pop_and_free(context_type *context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
static int signature_to_args_size(const char *method_signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
static void print_stack (context_type *, stack_info_type *stack_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
static void print_registers(context_type *, register_info_type *register_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
static void print_flags(context_type *, flag_type, flag_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
static void print_formatted_fieldname(context_type *context, int index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
static void print_formatted_methodname(context_type *context, int index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
void initialize_class_hash(context_type *context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    hash_table_type *class_hash = &(context->class_hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    class_hash->buckets = (hash_bucket_type **)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        calloc(MAX_HASH_ENTRIES / HASH_ROW_SIZE, sizeof(hash_bucket_type *));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    class_hash->table = (unsigned short *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        calloc(HASH_TABLE_SIZE, sizeof(unsigned short));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    if (class_hash->buckets == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        class_hash->table == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    class_hash->entries_used = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
static void finalize_class_hash(context_type *context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    hash_table_type *class_hash = &(context->class_hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /* 4296677: bucket index starts from 1. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    for (i=1;i<=class_hash->entries_used;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        hash_bucket_type *bucket = GET_BUCKET(class_hash, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        assert(bucket != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        free(bucket->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (bucket->class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            (*env)->DeleteGlobalRef(env, bucket->class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            context->n_globalrefs--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    if (class_hash->buckets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        for (i=0;i<MAX_HASH_ENTRIES / HASH_ROW_SIZE; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (class_hash->buckets[i] == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            free(class_hash->buckets[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    free(class_hash->buckets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    free(class_hash->table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
static hash_bucket_type *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
new_bucket(context_type *context, unsigned short *pID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    hash_table_type *class_hash = &(context->class_hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    int i = *pID = class_hash->entries_used + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    int row = i / HASH_ROW_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    if (i >= MAX_HASH_ENTRIES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        CCerror(context, "Exceeded verifier's limit of 65535 referred classes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    if (class_hash->buckets[row] == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        class_hash->buckets[row] = (hash_bucket_type*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            calloc(HASH_ROW_SIZE, sizeof(hash_bucket_type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (class_hash->buckets[row] == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    class_hash->entries_used++; /* only increment when we are sure there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                   is no overflow. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    return GET_BUCKET(class_hash, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
static unsigned int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
class_hash_fun(const char *s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    unsigned raw_hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    for (raw_hash = 0; (i = *s) != '\0'; ++s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        raw_hash = raw_hash * 37 + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    return raw_hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
 * Find a class using the defining loader of the current class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
 * and return a local reference to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
static jclass load_class_local(context_type *context,const char *classname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    jclass cb = JVM_FindClassFromClass(context->env, classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                 JNI_FALSE, context->class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    if (cb == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         CCerror(context, "Cannot find class %s", classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    return cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
 * Find a class using the defining loader of the current class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
 * and return a global reference to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
static jclass load_class_global(context_type *context, const char *classname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    jclass local, global;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    local = load_class_local(context, classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    global = (*env)->NewGlobalRef(env, local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    if (global == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    context->n_globalrefs++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    (*env)->DeleteLocalRef(env, local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    return global;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
 * Return a unique ID given a local class reference. The loadable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
 * flag is true if the defining class loader of context->class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
 * is known to be capable of loading the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
static unsigned short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
class_to_ID(context_type *context, jclass cb, jboolean loadable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    hash_table_type *class_hash = &(context->class_hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    unsigned int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    hash_bucket_type *bucket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    unsigned short *pID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    const char *name = JVM_GetClassNameUTF(env, cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    check_and_push(context, name, VM_STRING_UTF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    hash = class_hash_fun(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    pID = &(class_hash->table[hash % HASH_TABLE_SIZE]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    while (*pID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        bucket = GET_BUCKET(class_hash, *pID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (bucket->hash == hash && strcmp(name, bucket->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
             * There is an unresolved entry with our name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
             * so we're forced to load it in case it matches us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            if (bucket->class == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                assert(bucket->loadable == JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                bucket->class = load_class_global(context, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
             * It's already in the table. Update the loadable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
             * state if it's known and then we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            if ((*env)->IsSameObject(env, cb, bucket->class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                if (loadable && !bucket->loadable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    bucket->loadable = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        pID = &bucket->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    bucket = new_bucket(context, pID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    bucket->next = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    bucket->hash = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    bucket->name = malloc(strlen(name) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    if (bucket->name == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    strcpy(bucket->name, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    bucket->loadable = loadable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    bucket->class = (*env)->NewGlobalRef(env, cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    if (bucket->class == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    context->n_globalrefs++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
done:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    return *pID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
 * Return a unique ID given a class name from the constant pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
 * All classes are lazily loaded from the defining loader of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
 * context->class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
static unsigned short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
class_name_to_ID(context_type *context, const char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    hash_table_type *class_hash = &(context->class_hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    unsigned int hash = class_hash_fun(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    hash_bucket_type *bucket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    unsigned short *pID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    jboolean force_load = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    pID = &(class_hash->table[hash % HASH_TABLE_SIZE]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    while (*pID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        bucket = GET_BUCKET(class_hash, *pID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (bucket->hash == hash && strcmp(name, bucket->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            if (bucket->loadable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            force_load = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        pID = &bucket->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    if (force_load) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * We found at least one matching named entry for a class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * was not known to be loadable through the defining class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * of context->class. We must load our named class and update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         * the hash table in case one these entries matches our class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        jclass cb = load_class_local(context, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        unsigned short id = class_to_ID(context, cb, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        (*env)->DeleteLocalRef(env, cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    bucket = new_bucket(context, pID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    bucket->next = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    bucket->class = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    bucket->loadable = JNI_TRUE; /* name-only IDs are implicitly loadable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    bucket->hash = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    bucket->name = malloc(strlen(name) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    if (bucket->name == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    strcpy(bucket->name, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
done:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    return *pID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
static const char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
ID_to_class_name(context_type *context, unsigned short ID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    hash_table_type *class_hash = &(context->class_hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    hash_bucket_type *bucket = GET_BUCKET(class_hash, ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    return bucket->name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
static jclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
ID_to_class(context_type *context, unsigned short ID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    hash_table_type *class_hash = &(context->class_hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    hash_bucket_type *bucket = GET_BUCKET(class_hash, ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    if (bucket->class == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        assert(bucket->loadable == JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        bucket->class = load_class_global(context, bucket->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    return bucket->class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
static fullinfo_type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
make_loadable_class_info(context_type *context, jclass cb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    return MAKE_FULLINFO(ITEM_Object, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                           class_to_ID(context, cb, JNI_TRUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
static fullinfo_type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
make_class_info(context_type *context, jclass cb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    return MAKE_FULLINFO(ITEM_Object, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                         class_to_ID(context, cb, JNI_FALSE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
static fullinfo_type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
make_class_info_from_name(context_type *context, const char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    return MAKE_FULLINFO(ITEM_Object, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                         class_name_to_ID(context, name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
/* RETURNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
 * 1: on success       chosen to be consistent with previous VerifyClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
 * 0: verify error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
 * 2: out of memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
 * 3: class format error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
 * Called by verify_class.  Verify the code of each of the methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
 * in a class.  Note that this function apparently can't be JNICALL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
 * because if it is the dynamic linker doesn't appear to be able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
 * find it on Win32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
#define CC_OK 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
#define CC_VerifyError 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
#define CC_OutOfMemory 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
#define CC_ClassFormatError 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
JNIEXPORT jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
VerifyClassForMajorVersion(JNIEnv *env, jclass cb, char *buffer, jint len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                           jint major_version)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    context_type context_structure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    context_type *context = &context_structure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    jboolean result = CC_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    int num_methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    int* code_lengths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    unsigned char** code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    GlobalContext = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    memset(context, 0, sizeof(context_type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    context->message = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    context->message_buf_len = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    context->env = env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    context->class = cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /* Set invalid method/field index of the context, in case anyone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
       calls CCerror */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    context->method_index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    context->field_index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /* Don't call CCerror or anything that can call it above the setjmp! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    if (!setjmp(context->jump_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        jclass super;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        CCinit(context);                /* initialize heap; may throw */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        initialize_class_hash(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        context->major_version = major_version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        context->nconstants = JVM_GetClassCPEntriesCount(env, cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        context->constant_types = (unsigned char *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            malloc(sizeof(unsigned char) * context->nconstants + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if (context->constant_types == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        JVM_GetClassCPTypes(env, cb, context->constant_types);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if (context->constant_types == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        context->object_info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            make_class_info_from_name(context, "java/lang/Object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        context->string_info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            make_class_info_from_name(context, "java/lang/String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        context->throwable_info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            make_class_info_from_name(context, "java/lang/Throwable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        context->cloneable_info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            make_class_info_from_name(context, "java/lang/Cloneable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        context->serializable_info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            make_class_info_from_name(context, "java/io/Serializable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        context->currentclass_info = make_loadable_class_info(context, cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        super = (*env)->GetSuperclass(env, cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (super != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            fullinfo_type *gptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            context->superclass_info = make_loadable_class_info(context, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            while(super != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                jclass tmp_cb = (*env)->GetSuperclass(env, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                (*env)->DeleteLocalRef(env, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                super = tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            (*env)->DeleteLocalRef(env, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            super = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            /* Can't go on context heap since it survives more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
               one method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            context->superclasses = gptr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                malloc(sizeof(fullinfo_type)*(i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            if (gptr == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            super = (*env)->GetSuperclass(env, context->class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            while(super != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                jclass tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                *gptr++ = make_class_info(context, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                tmp_cb = (*env)->GetSuperclass(env, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                (*env)->DeleteLocalRef(env, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                super = tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            *gptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            context->superclass_info = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        (*env)->DeleteLocalRef(env, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        /* Look at each method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        for (i = JVM_GetClassFieldsCount(env, cb); --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            verify_field(context, cb, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
  num_methods = JVM_GetClassMethodsCount(env, cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
  read_all_code(context, cb, num_methods, &code_lengths, &code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
  for (i = num_methods - 1; i >= 0; --i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            verify_method(context, cb, i, code_lengths[i], code[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
  free_all_code(num_methods, code_lengths, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        result = CC_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        result = context->err_code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    /* Cleanup */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    finalize_class_hash(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    while(context->allocated_memory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    GlobalContext = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    if (context->exceptions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        free(context->exceptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    if (context->code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        free(context->code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    if (context->constant_types)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        free(context->constant_types);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    if (context->superclasses)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        free(context->superclasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /* Make sure all global refs created in the verifier are freed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    assert(context->n_globalrefs == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    CCdestroy(context);         /* destroy heap */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
#define OLD_FORMAT_MAX_MAJOR_VERSION 48
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
JNIEXPORT jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
VerifyClass(JNIEnv *env, jclass cb, char *buffer, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    static int warned = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    if (!warned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
      jio_fprintf(stdout, "Warning! An old version of jvm is used. This is not supported.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
      warned = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    return VerifyClassForMajorVersion(env, cb, buffer, len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                                      OLD_FORMAT_MAX_MAJOR_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
verify_field(context_type *context, jclass cb, int field_index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    int access_bits = JVM_GetFieldIxModifiers(env, cb, field_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    context->field_index = field_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    if (  ((access_bits & JVM_ACC_PUBLIC) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
          ((access_bits & (JVM_ACC_PRIVATE | JVM_ACC_PROTECTED)) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        CCerror(context, "Inconsistent access bits.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    context->field_index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
 * We read all of the class's methods' code because it is possible that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
 * the verification of one method could resulting in linking further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
 * down the stack (due to class loading), which could end up rewriting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
 * some of the bytecode of methods we haven't verified yet.  Since we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
 * don't want to see the rewritten bytecode, cache all the code and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
 * operate only on that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
read_all_code(context_type* context, jclass cb, int num_methods,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
              int** lengths_addr, unsigned char*** code_addr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
  int* lengths = malloc(sizeof(int) * num_methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
  unsigned char** code = malloc(sizeof(unsigned char*) * num_methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
  *(lengths_addr) = lengths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
  *(code_addr) = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
  if (lengths == 0 || code == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    for (i = 0; i < num_methods; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
      lengths[i] = JVM_GetMethodIxByteCodeLength(context->env, cb, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
      if (lengths[i] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        code[i] = malloc(sizeof(unsigned char) * (lengths[i] + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        if (code[i] == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
          CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
          JVM_GetMethodIxByteCode(context->env, cb, i, code[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        code[i] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
free_all_code(int num_methods, int* lengths, unsigned char** code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
  for (i = 0; i < num_methods; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    free(code[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
  free(lengths);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
  free(code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
/* Verify the code of one method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
verify_method(context_type *context, jclass cb, int method_index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
              int code_length, unsigned char* code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    int access_bits = JVM_GetMethodIxModifiers(env, cb, method_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    int *code_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    instruction_data_type *idata = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    int instruction_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    int i, offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    unsigned int inumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    jint nexceptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    if ((access_bits & (JVM_ACC_NATIVE | JVM_ACC_ABSTRACT)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        /* not much to do for abstract and native methods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    context->code_length = code_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    context->code = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /* CCerror can give method-specific info once this is set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    context->method_index = method_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    CCreinit(context);          /* initial heap */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    code_data = NEW(int, code_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    if (verify_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        const char *classname = JVM_GetClassNameUTF(env, cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        const char *methodname =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            JVM_GetMethodIxNameUTF(env, cb, method_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        const char *signature =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            JVM_GetMethodIxSignatureUTF(env, cb, method_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        jio_fprintf(stdout, "Looking at %s.%s%s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                    (classname ? classname : ""),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    (methodname ? methodname : ""),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    (signature ? signature : ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        JVM_ReleaseUTF(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        JVM_ReleaseUTF(methodname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        JVM_ReleaseUTF(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    if (((access_bits & JVM_ACC_PUBLIC) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        ((access_bits & (JVM_ACC_PRIVATE | JVM_ACC_PROTECTED)) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        CCerror(context, "Inconsistent access bits.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /* Run through the code.  Mark the start of each instruction, and give
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * the instruction a number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    for (i = 0, offset = 0; offset < code_length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        int length = instruction_length(&code[offset], code + code_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        int next_offset = offset + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        if (length <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            CCerror(context, "Illegal instruction found at offset %d", offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        if (next_offset > code_length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            CCerror(context, "Code stops in the middle of instruction "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                    " starting at offset %d", offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        code_data[offset] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        while (++offset < next_offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            code_data[offset] = -1; /* illegal location */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    instruction_count = i;      /* number of instructions in code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    /* Allocate a structure to hold info about each instruction. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    idata = NEW(instruction_data_type, instruction_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    /* Initialize the heap, and other info in the context structure. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    context->code = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    context->instruction_data = idata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    context->code_data = code_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    context->instruction_count = instruction_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    context->handler_info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        NEW(struct handler_info_type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            JVM_GetMethodIxExceptionTableLength(env, cb, method_index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    context->bitmask_size =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        (JVM_GetMethodIxLocalsCount(env, cb, method_index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
         + (BITS_PER_INT - 1))/BITS_PER_INT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    if (instruction_count == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        CCerror(context, "Empty code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    for (inumber = 0, offset = 0; offset < code_length; inumber++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        int length = instruction_length(&code[offset], code + code_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        instruction_data_type *this_idata = &idata[inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        this_idata->opcode = code[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        this_idata->stack_info.stack = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        this_idata->stack_info.stack_size  = UNKNOWN_STACK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        this_idata->register_info.register_count = UNKNOWN_REGISTER_COUNT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        this_idata->changed = JNI_FALSE;  /* no need to look at it yet. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        this_idata->protected = JNI_FALSE;  /* no need to look at it yet. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        this_idata->and_flags = (flag_type) -1; /* "bottom" and value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        this_idata->or_flags = 0; /* "bottom" or value*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        /* This also sets up this_data->operand.  It also makes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
         * xload_x and xstore_x instructions look like the generic form. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        verify_opcode_operands(context, inumber, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        offset += length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    /* make sure exception table is reasonable. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    initialize_exception_table(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    /* Set up first instruction, and start of exception handlers. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    initialize_dataflow(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    /* Run data flow analysis on the instructions. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    run_dataflow(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    /* verify checked exceptions, if any */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    nexceptions = JVM_GetMethodIxExceptionsCount(env, cb, method_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    context->exceptions = (unsigned short *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        malloc(sizeof(unsigned short) * nexceptions + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    if (context->exceptions == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    JVM_GetMethodIxExceptionIndexes(env, cb, method_index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                                    context->exceptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    for (i = 0; i < nexceptions; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        /* Make sure the constant pool item is JVM_CONSTANT_Class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        verify_constant_pool_type(context, (int)context->exceptions[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                                  1 << JVM_CONSTANT_Class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    free(context->exceptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    context->exceptions = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    context->code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    context->method_index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
/* Look at a single instruction, and verify its operands.  Also, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
 * simplicity, move the operand into the ->operand field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
 * Make sure that branches don't go into the middle of nowhere.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
static jint ntohl(jint n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    unsigned char *p = (unsigned char *)&n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
verify_opcode_operands(context_type *context, unsigned int inumber, int offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    instruction_data_type *this_idata = &idata[inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    int *code_data = context->code_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    int mi = context->method_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    unsigned char *code = context->code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    opcode_type opcode = this_idata->opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    int var;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * Set the ip fields to 0 not the i fields because the ip fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * are 64 bits on 64 bit architectures, the i field is only 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    this_idata->operand.ip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    this_idata->operand2.ip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    switch (opcode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    case opc_jsr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        /* instruction of ret statement */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        this_idata->operand2.i = UNKNOWN_RET_INSTRUCTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        /* FALLTHROUGH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    case opc_ifeq: case opc_ifne: case opc_iflt:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    case opc_ifge: case opc_ifgt: case opc_ifle:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    case opc_ifnull: case opc_ifnonnull:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    case opc_if_icmpeq: case opc_if_icmpne: case opc_if_icmplt:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    case opc_if_icmpge: case opc_if_icmpgt: case opc_if_icmple:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    case opc_if_acmpeq: case opc_if_acmpne:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    case opc_goto: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        /* Set the ->operand to be the instruction number of the target. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        int jump = (((signed char)(code[offset+1])) << 8) + code[offset+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        int target = offset + jump;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        if (!isLegalTarget(context, target))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            CCerror(context, "Illegal target of jump or branch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        this_idata->operand.i = code_data[target];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    case opc_jsr_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        /* instruction of ret statement */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        this_idata->operand2.i = UNKNOWN_RET_INSTRUCTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        /* FALLTHROUGH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    case opc_goto_w: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        /* Set the ->operand to be the instruction number of the target. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        int jump = (((signed char)(code[offset+1])) << 24) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                     (code[offset+2] << 16) + (code[offset+3] << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                     (code[offset + 4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        int target = offset + jump;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        if (!isLegalTarget(context, target))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            CCerror(context, "Illegal target of jump or branch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        this_idata->operand.i = code_data[target];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    case opc_tableswitch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    case opc_lookupswitch: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        /* Set the ->operand to be a table of possible instruction targets. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        int *lpc = (int *) UCALIGN(code + offset + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        int *lptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        int *saved_operand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        int keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        int k, delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        /* 4639449, 4647081: Padding bytes must be zero. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        unsigned char* bptr = (unsigned char*) (code + offset + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        for (; bptr < (unsigned char*)lpc; bptr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            if (*bptr != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                CCerror(context, "Non zero padding bytes in switch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        if (opcode == opc_tableswitch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            keys = ntohl(lpc[2]) -  ntohl(lpc[1]) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            delta = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            keys = ntohl(lpc[1]); /* number of pairs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            delta = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            /* Make sure that the tableswitch items are sorted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            for (k = keys - 1, lptr = &lpc[2]; --k >= 0; lptr += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                int this_key = ntohl(lptr[0]);  /* NB: ntohl may be unsigned */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                int next_key = ntohl(lptr[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                if (this_key >= next_key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    CCerror(context, "Unsorted lookup switch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        saved_operand = NEW(int, keys + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        if (!isLegalTarget(context, offset + ntohl(lpc[0])))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            CCerror(context, "Illegal default target in switch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        saved_operand[keys + 1] = code_data[offset + ntohl(lpc[0])];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        for (k = keys, lptr = &lpc[3]; --k >= 0; lptr += delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            int target = offset + ntohl(lptr[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            if (!isLegalTarget(context, target))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                CCerror(context, "Illegal branch in opc_tableswitch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            saved_operand[k + 1] = code_data[target];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        saved_operand[0] = keys + 1; /* number of successors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        this_idata->operand.ip = saved_operand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    case opc_ldc: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        /* Make sure the constant pool item is the right type. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        int key = code[offset + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        int types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                    (1 << JVM_CONSTANT_String);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        if (context->major_version >= LDC_CLASS_MAJOR_VERSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            types |= 1 << JVM_CONSTANT_Class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        this_idata->operand.i = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        verify_constant_pool_type(context, key, types);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    case opc_ldc_w: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        /* Make sure the constant pool item is the right type. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        int key = (code[offset + 1] << 8) + code[offset + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        int types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    (1 << JVM_CONSTANT_String);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        if (context->major_version >= LDC_CLASS_MAJOR_VERSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            types |= 1 << JVM_CONSTANT_Class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        this_idata->operand.i = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        verify_constant_pool_type(context, key, types);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    case opc_ldc2_w: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        /* Make sure the constant pool item is the right type. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        int key = (code[offset + 1] << 8) + code[offset + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        int types = (1 << JVM_CONSTANT_Double) | (1 << JVM_CONSTANT_Long);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        this_idata->operand.i = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        verify_constant_pool_type(context, key, types);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    case opc_getfield: case opc_putfield:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    case opc_getstatic: case opc_putstatic: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        /* Make sure the constant pool item is the right type. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        int key = (code[offset + 1] << 8) + code[offset + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        this_idata->operand.i = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        verify_constant_pool_type(context, key, 1 << JVM_CONSTANT_Fieldref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        if (opcode == opc_getfield || opcode == opc_putfield)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            set_protected(context, inumber, key, opcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    case opc_invokevirtual:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    case opc_invokespecial:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    case opc_invokestatic:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    case opc_invokeinterface: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        /* Make sure the constant pool item is the right type. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        int key = (code[offset + 1] << 8) + code[offset + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        const char *methodname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        jclass cb = context->class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        fullinfo_type clazz_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        int is_constructor, is_internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        int kind = (opcode == opc_invokeinterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                            ? 1 << JVM_CONSTANT_InterfaceMethodref
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                            : 1 << JVM_CONSTANT_Methodref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        /* Make sure the constant pool item is the right type. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        verify_constant_pool_type(context, key, kind);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        methodname = JVM_GetCPMethodNameUTF(env, cb, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        check_and_push(context, methodname, VM_STRING_UTF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        is_constructor = !strcmp(methodname, "<init>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        is_internal = methodname[0] == '<';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        clazz_info = cp_index_to_class_fullinfo(context, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                                                JVM_CONSTANT_Methodref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        this_idata->operand.i = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        this_idata->operand2.fi = clazz_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        if (is_constructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            if (opcode != opc_invokespecial) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                        "Must call initializers using invokespecial");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            this_idata->opcode = opc_invokeinit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            if (is_internal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                CCerror(context, "Illegal call to internal method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            if (opcode == opc_invokespecial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                   && clazz_info != context->currentclass_info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                   && clazz_info != context->superclass_info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                int not_found = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                jclass super = (*env)->GetSuperclass(env, context->class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                while(super != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                    jclass tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                    fullinfo_type new_info = make_class_info(context, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                    if (clazz_info == new_info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                        not_found = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                    tmp_cb = (*env)->GetSuperclass(env, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                    (*env)->DeleteLocalRef(env, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                    super = tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                (*env)->DeleteLocalRef(env, super);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                /* The optimizer make cause this to happen on local code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                if (not_found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
#ifdef BROKEN_JAVAC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    jobject loader = JVM_GetClassLoader(env, context->class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    int has_loader = (loader != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    (*env)->DeleteLocalRef(env, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    if (has_loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
#endif /* BROKEN_JAVAC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                        CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                                "Illegal use of nonvirtual function call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        if (opcode == opc_invokeinterface) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            unsigned int args1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            unsigned int args2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            const char *signature =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                JVM_GetCPMethodSignatureUTF(env, context->class, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            check_and_push(context, signature, VM_STRING_UTF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            args1 = signature_to_args_size(signature) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            args2 = code[offset + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            if (args1 != args2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                        "Inconsistent args_size for opc_invokeinterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            if (code[offset + 4] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                        "Fourth operand byte of invokeinterface must be zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        } else if (opcode == opc_invokevirtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                      || opcode == opc_invokespecial)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            set_protected(context, inumber, key, opcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    case opc_instanceof:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    case opc_checkcast:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    case opc_new:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    case opc_anewarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    case opc_multianewarray: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        /* Make sure the constant pool item is a class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        int key = (code[offset + 1] << 8) + code[offset + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        fullinfo_type target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        verify_constant_pool_type(context, key, 1 << JVM_CONSTANT_Class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        target = cp_index_to_class_fullinfo(context, key, JVM_CONSTANT_Class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        if (GET_ITEM_TYPE(target) == ITEM_Bogus)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            CCerror(context, "Illegal type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        switch(opcode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        case opc_anewarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            if ((GET_INDIRECTION(target)) >= MAX_ARRAY_DIMENSIONS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                CCerror(context, "Array with too many dimensions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            this_idata->operand.fi = MAKE_FULLINFO(GET_ITEM_TYPE(target),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                                                   GET_INDIRECTION(target) + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                                                   GET_EXTRA_INFO(target));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        case opc_new:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            if (WITH_ZERO_EXTRA_INFO(target) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                             MAKE_FULLINFO(ITEM_Object, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                CCerror(context, "Illegal creation of multi-dimensional array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            /* operand gets set to the "unitialized object".  operand2 gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
             * set to what the value will be after it's initialized. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            this_idata->operand.fi = MAKE_FULLINFO(ITEM_NewObject, 0, inumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            this_idata->operand2.fi = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        case opc_multianewarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            this_idata->operand.fi = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            this_idata->operand2.i = code[offset + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            if (    (this_idata->operand2.i > GET_INDIRECTION(target))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                 || (this_idata->operand2.i == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                CCerror(context, "Illegal dimension argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            this_idata->operand.fi = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    case opc_newarray: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        /* Cache the result of the opc_newarray into the operand slot */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        fullinfo_type full_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        switch (code[offset + 1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            case JVM_T_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                full_info = MAKE_FULLINFO(ITEM_Integer, 1, 0); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            case JVM_T_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                full_info = MAKE_FULLINFO(ITEM_Long, 1, 0); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            case JVM_T_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                full_info = MAKE_FULLINFO(ITEM_Float, 1, 0); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            case JVM_T_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                full_info = MAKE_FULLINFO(ITEM_Double, 1, 0); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            case JVM_T_BYTE: case JVM_T_BOOLEAN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                full_info = MAKE_FULLINFO(ITEM_Byte, 1, 0); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            case JVM_T_CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                full_info = MAKE_FULLINFO(ITEM_Char, 1, 0); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            case JVM_T_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                full_info = MAKE_FULLINFO(ITEM_Short, 1, 0); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                full_info = 0;          /* Keep lint happy */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                CCerror(context, "Bad type passed to opc_newarray");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        this_idata->operand.fi = full_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    /* Fudge iload_x, aload_x, etc to look like their generic cousin. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    case opc_iload_0: case opc_iload_1: case opc_iload_2: case opc_iload_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        this_idata->opcode = opc_iload;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        var = opcode - opc_iload_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        goto check_local_variable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    case opc_fload_0: case opc_fload_1: case opc_fload_2: case opc_fload_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        this_idata->opcode = opc_fload;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        var = opcode - opc_fload_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        goto check_local_variable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    case opc_aload_0: case opc_aload_1: case opc_aload_2: case opc_aload_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        this_idata->opcode = opc_aload;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        var = opcode - opc_aload_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        goto check_local_variable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    case opc_lload_0: case opc_lload_1: case opc_lload_2: case opc_lload_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        this_idata->opcode = opc_lload;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        var = opcode - opc_lload_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        goto check_local_variable2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    case opc_dload_0: case opc_dload_1: case opc_dload_2: case opc_dload_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        this_idata->opcode = opc_dload;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        var = opcode - opc_dload_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        goto check_local_variable2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    case opc_istore_0: case opc_istore_1: case opc_istore_2: case opc_istore_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        this_idata->opcode = opc_istore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        var = opcode - opc_istore_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        goto check_local_variable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    case opc_fstore_0: case opc_fstore_1: case opc_fstore_2: case opc_fstore_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        this_idata->opcode = opc_fstore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        var = opcode - opc_fstore_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        goto check_local_variable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    case opc_astore_0: case opc_astore_1: case opc_astore_2: case opc_astore_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        this_idata->opcode = opc_astore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        var = opcode - opc_astore_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        goto check_local_variable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    case opc_lstore_0: case opc_lstore_1: case opc_lstore_2: case opc_lstore_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        this_idata->opcode = opc_lstore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        var = opcode - opc_lstore_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        goto check_local_variable2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    case opc_dstore_0: case opc_dstore_1: case opc_dstore_2: case opc_dstore_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        this_idata->opcode = opc_dstore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        var = opcode - opc_dstore_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        goto check_local_variable2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    case opc_wide:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        this_idata->opcode = code[offset + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        var = (code[offset + 2] << 8) + code[offset + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        switch(this_idata->opcode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            case opc_lload:  case opc_dload:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            case opc_lstore: case opc_dstore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                goto check_local_variable2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                goto check_local_variable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    case opc_iinc:              /* the increment amount doesn't matter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    case opc_ret:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    case opc_aload: case opc_iload: case opc_fload:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    case opc_astore: case opc_istore: case opc_fstore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        var = code[offset + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    check_local_variable:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        /* Make sure that the variable number isn't illegal. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        this_idata->operand.i = var;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        if (var >= JVM_GetMethodIxLocalsCount(env, context->class, mi))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            CCerror(context, "Illegal local variable number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    case opc_lload: case opc_dload: case opc_lstore: case opc_dstore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        var = code[offset + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    check_local_variable2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        /* Make sure that the variable number isn't illegal. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        this_idata->operand.i = var;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        if ((var + 1) >= JVM_GetMethodIxLocalsCount(env, context->class, mi))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            CCerror(context, "Illegal local variable number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        if (opcode >= opc_breakpoint)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            CCerror(context, "Quick instructions shouldn't appear yet.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    } /* of switch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
set_protected(context_type *context, unsigned int inumber, int key, opcode_type opcode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    fullinfo_type clazz_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    if (opcode != opc_invokevirtual && opcode != opc_invokespecial) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        clazz_info = cp_index_to_class_fullinfo(context, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                                                JVM_CONSTANT_Fieldref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        clazz_info = cp_index_to_class_fullinfo(context, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                                                JVM_CONSTANT_Methodref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    if (is_superclass(context, clazz_info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        jclass calledClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            object_fullinfo_to_classclass(context, clazz_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        int access;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        /* 4734966: JVM_GetCPFieldModifiers() or JVM_GetCPMethodModifiers() only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
           searches the referenced field or method in calledClass. The following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
           while loop is added to search up the superclass chain to make this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
           symbolic resolution consistent with the field/method resolution
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
           specified in VM spec 5.4.3. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        calledClass = (*env)->NewLocalRef(env, calledClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            jclass tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            if (opcode != opc_invokevirtual && opcode != opc_invokespecial) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                access = JVM_GetCPFieldModifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                    (env, context->class, key, calledClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                access = JVM_GetCPMethodModifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                    (env, context->class, key, calledClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            if (access != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            tmp_cb = (*env)->GetSuperclass(env, calledClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            (*env)->DeleteLocalRef(env, calledClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            calledClass = tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        } while (calledClass != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        if (access == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            /* field/method not found, detected at runtime. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        } else if (access & JVM_ACC_PROTECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            if (!JVM_IsSameClassPackage(env, calledClass, context->class))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                context->instruction_data[inumber].protected = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        (*env)->DeleteLocalRef(env, calledClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
is_superclass(context_type *context, fullinfo_type clazz_info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    fullinfo_type *fptr = context->superclasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
    if (fptr == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    for (; *fptr != 0; fptr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        if (*fptr == clazz_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
/* Look through each item on the exception table.  Each of the fields must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
 * refer to a legal instruction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
initialize_exception_table(context_type *context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    int mi = context->method_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    struct handler_info_type *handler_info = context->handler_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    int *code_data = context->code_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    int code_length = context->code_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    int max_stack_size = JVM_GetMethodIxMaxStack(env, context->class, mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    int i = JVM_GetMethodIxExceptionTableLength(env, context->class, mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    if (max_stack_size < 1 && i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        // If the method contains exception handlers, it must have room
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        // on the expression stack for the exception that the VM could push
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        CCerror(context, "Stack size too large");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    for (; --i >= 0; handler_info++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        JVM_ExceptionTableEntryType einfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        stack_item_type *stack_item = NEW(stack_item_type, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        JVM_GetMethodIxExceptionTableEntry(env, context->class, mi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                                           i, &einfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        if (!(einfo.start_pc < einfo.end_pc &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
              einfo.start_pc >= 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
              isLegalTarget(context, einfo.start_pc) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
              (einfo.end_pc ==  code_length ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
               isLegalTarget(context, einfo.end_pc)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            CFerror(context, "Illegal exception table range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        if (!((einfo.handler_pc > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
              isLegalTarget(context, einfo.handler_pc))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            CFerror(context, "Illegal exception table handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        handler_info->start = code_data[einfo.start_pc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        /* einfo.end_pc may point to one byte beyond the end of bytecodes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        handler_info->end = (einfo.end_pc == context->code_length) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            context->instruction_count : code_data[einfo.end_pc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        handler_info->handler = code_data[einfo.handler_pc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        handler_info->stack_info.stack = stack_item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        handler_info->stack_info.stack_size = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        stack_item->next = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        if (einfo.catchType != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            const char *classname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            /* Constant pool entry type has been checked in format checker */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            classname = JVM_GetCPClassNameUTF(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                                              context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                                              einfo.catchType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            check_and_push(context, classname, VM_STRING_UTF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            stack_item->item = make_class_info_from_name(context, classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            if (!isAssignableTo(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                                stack_item->item,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                                context->throwable_info))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                CCerror(context, "catch_type not a subclass of Throwable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            stack_item->item = context->throwable_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
/* Given a pointer to an instruction, return its length.  Use the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
 * opcode_length[] which is automatically built.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
static int instruction_length(unsigned char *iptr, unsigned char *end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    int instruction = *iptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    switch (instruction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        case opc_tableswitch: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            int *lpc = (int *)UCALIGN(iptr + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            if (lpc + 2 >= (int *)end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                return -1; /* do not read pass the end */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            index = ntohl(lpc[2]) - ntohl(lpc[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            if ((index < 0) || (index > 65535)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                return -1;      /* illegal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                return (unsigned char *)(&lpc[index + 4]) - iptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        case opc_lookupswitch: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            int *lpc = (int *) UCALIGN(iptr + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            int npairs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            if (lpc + 1 >= (int *)end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                return -1; /* do not read pass the end */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            npairs = ntohl(lpc[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            /* There can't be more than 64K labels because of the limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
             * on per-method byte code length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            if (npairs < 0 || npairs >= 65536)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                return  -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                return (unsigned char *)(&lpc[2 * (npairs + 1)]) - iptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        case opc_wide:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            if (iptr + 1 >= end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                return -1; /* do not read pass the end */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            switch(iptr[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                case opc_ret:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                case opc_iload: case opc_istore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                case opc_fload: case opc_fstore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                case opc_aload: case opc_astore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                case opc_lload: case opc_lstore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                case opc_dload: case opc_dstore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                    return 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                case opc_iinc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                    return 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        default: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            /* A length of 0 indicates an error. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            int length = opcode_length[instruction];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            return (length <= 0) ? -1 : length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
/* Given the target of a branch, make sure that it's a legal target. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
isLegalTarget(context_type *context, int offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    int code_length = context->code_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    int *code_data = context->code_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    return (offset >= 0 && offset < code_length && code_data[offset] >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
/* Make sure that an element of the constant pool really is of the indicated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
 * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
verify_constant_pool_type(context_type *context, int index, unsigned mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    int nconstants = context->nconstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    unsigned char *type_table = context->constant_types;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
    unsigned type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
    if ((index <= 0) || (index >= nconstants))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        CCerror(context, "Illegal constant pool index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    type = type_table[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
    if ((mask & (1 << type)) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        CCerror(context, "Illegal type in constant pool");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
initialize_dataflow(context_type *context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    int mi = context->method_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    jclass cb = context->class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    int args_size = JVM_GetMethodIxArgsSize(env, cb, mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
    fullinfo_type *reg_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    fullinfo_type full_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    const char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    const char *signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    /* Initialize the function entry, since we know everything about it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    idata[0].stack_info.stack_size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    idata[0].stack_info.stack = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    idata[0].register_info.register_count = args_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    idata[0].register_info.registers = NEW(fullinfo_type, args_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    idata[0].register_info.mask_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    idata[0].register_info.masks = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
    idata[0].and_flags = 0;     /* nothing needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    idata[0].or_flags = FLAG_REACHED; /* instruction reached */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
    reg_ptr = idata[0].register_info.registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
    if ((JVM_GetMethodIxModifiers(env, cb, mi) & JVM_ACC_STATIC) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        /* A non static method.  If this is an <init> method, the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
         * argument is an uninitialized object.  Otherwise it is an object of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
         * the given class type.  java.lang.Object.<init> is special since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
         * we don't call its superclass <init> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        if (JVM_IsConstructorIx(env, cb, mi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                && context->currentclass_info != context->object_info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            *reg_ptr++ = MAKE_FULLINFO(ITEM_InitObject, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            idata[0].or_flags |= FLAG_NEED_CONSTRUCTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            *reg_ptr++ = context->currentclass_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
    signature = JVM_GetMethodIxSignatureUTF(env, cb, mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    check_and_push(context, signature, VM_STRING_UTF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
    /* Fill in each of the arguments into the registers. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    for (p = signature + 1; *p != JVM_SIGNATURE_ENDFUNC; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        char fieldchar = signature_to_fieldtype(context, &p, &full_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        switch (fieldchar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            case 'D': case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                *reg_ptr++ = full_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                *reg_ptr++ = full_info + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                *reg_ptr++ = full_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    p++;                        /* skip over right parenthesis */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
    if (*p == 'V') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        context->return_type = MAKE_FULLINFO(ITEM_Void, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        signature_to_fieldtype(context, &p, &full_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        context->return_type = full_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
    pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
    /* Indicate that we need to look at the first instruction. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    idata[0].changed = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
/* Run the data flow analysis, as long as there are things to change. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
run_dataflow(context_type *context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    int mi = context->method_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    jclass cb = context->class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    int max_stack_size = JVM_GetMethodIxMaxStack(env, cb, mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
    int icount = context->instruction_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    jboolean work_to_do = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
    unsigned int inumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    /* Run through the loop, until there is nothing left to do. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    while (work_to_do) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        work_to_do = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        for (inumber = 0; inumber < icount; inumber++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            instruction_data_type *this_idata = &idata[inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            if (this_idata->changed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                register_info_type new_register_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                stack_info_type new_stack_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                flag_type new_and_flags, new_or_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                this_idata->changed = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                work_to_do = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                if (verify_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                    int opcode = this_idata->opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                    jio_fprintf(stdout, "Instruction %d: ", inumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                    print_stack(context, &this_idata->stack_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                    print_registers(context, &this_idata->register_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                    print_flags(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                                this_idata->and_flags, this_idata->or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                    fflush(stdout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                /* Make sure the registers and flags are appropriate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                check_register_values(context, inumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                check_flags(context, inumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                /* Make sure the stack can deal with this instruction */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                pop_stack(context, inumber, &new_stack_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                /* Update the registers  and flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                update_registers(context, inumber, &new_register_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                update_flags(context, inumber, &new_and_flags, &new_or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                /* Update the stack. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                push_stack(context, inumber, &new_stack_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                if (new_stack_info.stack_size > max_stack_size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                    CCerror(context, "Stack size too large");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                if (verify_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                    jio_fprintf(stdout, "  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                    print_stack(context, &new_stack_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                    print_registers(context, &new_register_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                    print_flags(context, new_and_flags, new_or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                    fflush(stdout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                /* Add the new stack and register information to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                 * instructions that can follow this instruction.     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                merge_into_successors(context, inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                                      &new_register_info, &new_stack_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                                      new_and_flags, new_or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
/* Make sure that the registers contain a legitimate value for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
 * instruction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
check_register_values(context_type *context, unsigned int inumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
    instruction_data_type *this_idata = &idata[inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    opcode_type opcode = this_idata->opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    int operand = this_idata->operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
    int register_count = this_idata->register_info.register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    fullinfo_type *registers = this_idata->register_info.registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
    jboolean double_word = JNI_FALSE;   /* default value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
    int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
    switch (opcode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        case opc_iload: case opc_iinc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            type = ITEM_Integer; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        case opc_fload:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            type = ITEM_Float; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        case opc_aload:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            type = ITEM_Object; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        case opc_ret:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            type = ITEM_ReturnAddress; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        case opc_lload:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            type = ITEM_Long; double_word = JNI_TRUE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        case opc_dload:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            type = ITEM_Double; double_word = JNI_TRUE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    if (!double_word) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        fullinfo_type reg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        /* Make sure we don't have an illegal register or one with wrong type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        if (operand >= register_count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                    "Accessing value from uninitialized register %d", operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        reg = registers[operand];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        if (WITH_ZERO_EXTRA_INFO(reg) == MAKE_FULLINFO(type, 0, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            /* the register is obviously of the given type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        } else if (GET_INDIRECTION(reg) > 0 && type == ITEM_Object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            /* address type stuff be used on all arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        } else if (GET_ITEM_TYPE(reg) == ITEM_ReturnAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            CCerror(context, "Cannot load return address from register %d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                              operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            /* alternatively
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                      (GET_ITEM_TYPE(reg) == ITEM_ReturnAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                   && (opcode == opc_iload)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                   && (type == ITEM_Object || type == ITEM_Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
               but this never occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        } else if (reg == ITEM_InitObject && type == ITEM_Object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        } else if (WITH_ZERO_EXTRA_INFO(reg) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                        MAKE_FULLINFO(ITEM_NewObject, 0, 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                   type == ITEM_Object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            CCerror(context, "Register %d contains wrong type", operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        /* Make sure we don't have an illegal register or one with wrong type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        if ((operand + 1) >= register_count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                    "Accessing value from uninitialized register pair %d/%d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                    operand, operand+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            if ((registers[operand] == MAKE_FULLINFO(type, 0, 0)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                (registers[operand + 1] == MAKE_FULLINFO(type + 1, 0, 0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                CCerror(context, "Register pair %d/%d contains wrong type",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                        operand, operand+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
/* Make sure the flags contain legitimate values for this instruction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
check_flags(context_type *context, unsigned int inumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    instruction_data_type *this_idata = &idata[inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
    opcode_type opcode = this_idata->opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    switch (opcode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        case opc_return:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            /* We need a constructor, but we aren't guaranteed it's called */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            if ((this_idata->or_flags & FLAG_NEED_CONSTRUCTOR) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                   !(this_idata->and_flags & FLAG_CONSTRUCTED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                CCerror(context, "Constructor must call super() or this()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            /* fall through */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        case opc_ireturn: case opc_lreturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        case opc_freturn: case opc_dreturn: case opc_areturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
            if (this_idata->or_flags & FLAG_NO_RETURN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                /* This method cannot exit normally */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                CCerror(context, "Cannot return normally");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            break; /* nothing to do. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
/* Make sure that the top of the stack contains reasonable values for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
 * given instruction.  The post-pop values of the stack and its size are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
 * returned in *new_stack_info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
pop_stack(context_type *context, unsigned int inumber, stack_info_type *new_stack_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
    instruction_data_type *this_idata = &idata[inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    opcode_type opcode = this_idata->opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
    stack_item_type *stack = this_idata->stack_info.stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    int stack_size = this_idata->stack_info.stack_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    char *stack_operands, *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    char buffer[257];           /* for holding manufactured argument lists */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    fullinfo_type stack_extra_info_buffer[256]; /* save info popped off stack */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
    fullinfo_type *stack_extra_info = &stack_extra_info_buffer[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    fullinfo_type full_info;    /* only used in case of invoke instructions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    fullinfo_type put_full_info; /* only used in case opc_putstatic and opc_putfield */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    switch(opcode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            /* For most instructions, we just use a built-in table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            stack_operands = opcode_in_out[opcode][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        case opc_putstatic: case opc_putfield: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            /* The top thing on the stack depends on the signature of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
             * the object.                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            int operand = this_idata->operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            const char *signature =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                JVM_GetCPFieldSignatureUTF(context->env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                                           context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                                           operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            char *ip = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            check_and_push(context, signature, VM_STRING_UTF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            if (verify_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                print_formatted_fieldname(context, operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            if (opcode == opc_putfield)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                *ip++ = 'A';    /* object for putfield */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            *ip++ = signature_to_fieldtype(context, &signature, &put_full_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            *ip = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            stack_operands = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
            pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        case opc_invokevirtual: case opc_invokespecial:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        case opc_invokeinit:    /* invokespecial call to <init> */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        case opc_invokestatic: case opc_invokeinterface: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            /* The top stuff on the stack depends on the method signature */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            int operand = this_idata->operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            const char *signature =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                JVM_GetCPMethodSignatureUTF(context->env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                                            context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                                            operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            char *ip = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            const char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
            check_and_push(context, signature, VM_STRING_UTF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            if (verify_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                print_formatted_methodname(context, operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            if (opcode != opc_invokestatic)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                /* First, push the object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                *ip++ = (opcode == opc_invokeinit ? '@' : 'A');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            for (p = signature + 1; *p != JVM_SIGNATURE_ENDFUNC; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                *ip++ = signature_to_fieldtype(context, &p, &full_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                if (ip >= buffer + sizeof(buffer) - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                    CCerror(context, "Signature %s has too many arguments",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                            signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            *ip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
            stack_operands = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
            pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        case opc_multianewarray: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            /* Count can't be larger than 255. So can't overflow buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            int count = this_idata->operand2.i; /* number of ints on stack */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            memset(buffer, 'I', count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            buffer[count] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            stack_operands = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
    } /* of switch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    /* Run through the list of operands >>backwards<< */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
    for (   p = stack_operands + strlen(stack_operands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
            p > stack_operands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            stack = stack->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        int type = *--p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        fullinfo_type top_type = stack ? stack->item : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        int size = (type == 'D' || type == 'L') ? 2 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        *--stack_extra_info = top_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        if (stack == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            CCerror(context, "Unable to pop operand off an empty stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                if (top_type != MAKE_FULLINFO(ITEM_Integer, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                    CCerror(context, "Expecting to find integer on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                if (top_type != MAKE_FULLINFO(ITEM_Float, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                    CCerror(context, "Expecting to find float on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
            case 'A':           /* object or array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                if (   (GET_ITEM_TYPE(top_type) != ITEM_Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                    && (GET_INDIRECTION(top_type) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                    /* The thing isn't an object or an array.  Let's see if it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                     * one of the special cases  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                    if (  (WITH_ZERO_EXTRA_INFO(top_type) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                                MAKE_FULLINFO(ITEM_ReturnAddress, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                        && (opcode == opc_astore))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                    if (   (GET_ITEM_TYPE(top_type) == ITEM_NewObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                            || (GET_ITEM_TYPE(top_type) == ITEM_InitObject))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                        && ((opcode == opc_astore) || (opcode == opc_aload)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                            || (opcode == opc_ifnull) || (opcode == opc_ifnonnull)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                    /* The 2nd edition VM of the specification allows field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                     * initializations before the superclass initializer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                     * if the field is defined within the current class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                     if (   (GET_ITEM_TYPE(top_type) == ITEM_InitObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                         && (opcode == opc_putfield)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                        int operand = this_idata->operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                        int access_bits = JVM_GetCPFieldModifiers(context->env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                                                                  context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                                                                  operand,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                                                                  context->class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                        /* Note: This relies on the fact that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                         * JVM_GetCPFieldModifiers retrieves only local fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                         * and does not respect inheritance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                        if (access_bits != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                            if ( cp_index_to_class_fullinfo(context, operand, JVM_CONSTANT_Fieldref) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                                 context->currentclass_info ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                                top_type = context->currentclass_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                                *stack_extra_info = top_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                    CCerror(context, "Expecting to find object/array on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
            case '@': {         /* unitialized object, for call to <init> */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                int item_type = GET_ITEM_TYPE(top_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                if (item_type != ITEM_NewObject && item_type != ITEM_InitObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                    CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                            "Expecting to find unitialized object on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            case 'O':           /* object, not array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                if (WITH_ZERO_EXTRA_INFO(top_type) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                       MAKE_FULLINFO(ITEM_Object, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                    CCerror(context, "Expecting to find object on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            case 'a':           /* integer, object, or array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                if (      (top_type != MAKE_FULLINFO(ITEM_Integer, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                       && (GET_ITEM_TYPE(top_type) != ITEM_Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                       && (GET_INDIRECTION(top_type) == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                    CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                            "Expecting to find object, array, or int on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
            case 'D':           /* double */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                if (top_type != MAKE_FULLINFO(ITEM_Double, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                    CCerror(context, "Expecting to find double on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
            case 'L':           /* long */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                if (top_type != MAKE_FULLINFO(ITEM_Long, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                    CCerror(context, "Expecting to find long on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            case ']':           /* array of some type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                if (top_type == NULL_FULLINFO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                    /* do nothing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                } else switch(p[-1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                    case 'I':   /* array of integers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                        if (top_type != MAKE_FULLINFO(ITEM_Integer, 1, 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                            top_type != NULL_FULLINFO)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                            CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                                    "Expecting to find array of ints on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                    case 'L':   /* array of longs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                        if (top_type != MAKE_FULLINFO(ITEM_Long, 1, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                            CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                                   "Expecting to find array of longs on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                    case 'F':   /* array of floats */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                        if (top_type != MAKE_FULLINFO(ITEM_Float, 1, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                            CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                                 "Expecting to find array of floats on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                    case 'D':   /* array of doubles */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                        if (top_type != MAKE_FULLINFO(ITEM_Double, 1, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                            CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                                "Expecting to find array of doubles on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                    case 'A': { /* array of addresses (arrays or objects) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                        int indirection = GET_INDIRECTION(top_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                        if ((indirection == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                            ((indirection == 1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                                (GET_ITEM_TYPE(top_type) != ITEM_Object)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                            CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                                "Expecting to find array of objects or arrays "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                                    "on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                    case 'B':   /* array of bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                        if (top_type != MAKE_FULLINFO(ITEM_Byte, 1, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                            CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                                  "Expecting to find array of bytes on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                    case 'C':   /* array of characters */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                        if (top_type != MAKE_FULLINFO(ITEM_Char, 1, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                            CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                                  "Expecting to find array of chars on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                    case 'S':   /* array of shorts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                        if (top_type != MAKE_FULLINFO(ITEM_Short, 1, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                            CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                                 "Expecting to find array of shorts on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                    case '?':   /* any type of array is okay */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                        if (GET_INDIRECTION(top_type) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                            CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                                    "Expecting to find array on stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                        CCerror(context, "Internal error #1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                p -= 2;         /* skip over [ <char> */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            case '1': case '2': case '3': case '4': /* stack swapping */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                if (top_type == MAKE_FULLINFO(ITEM_Double, 0, 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                    || top_type == MAKE_FULLINFO(ITEM_Long, 0, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                    if ((p > stack_operands) && (p[-1] == '+')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                        context->swap_table[type - '1'] = top_type + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                        context->swap_table[p[-2] - '1'] = top_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                        size = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                        p -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                        CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                                "Attempt to split long or double on the stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                    context->swap_table[type - '1'] = stack->item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                    if ((p > stack_operands) && (p[-1] == '+'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                        p--;    /* ignore */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
            case '+':           /* these should have been caught. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                CCerror(context, "Internal error #2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        stack_size -= size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
    /* For many of the opcodes that had an "A" in their field, we really
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
     * need to go back and do a little bit more accurate testing.  We can, of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     * course, assume that the minimal type checking has already been done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
    switch (opcode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        default: break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
        case opc_aastore: {     /* array index object  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
            fullinfo_type array_type = stack_extra_info[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            fullinfo_type object_type = stack_extra_info[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
            fullinfo_type target_type = decrement_indirection(array_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            if ((GET_ITEM_TYPE(object_type) != ITEM_Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                    && (GET_INDIRECTION(object_type) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                CCerror(context, "Expecting reference type on operand stack in aastore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            if ((GET_ITEM_TYPE(target_type) != ITEM_Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                    && (GET_INDIRECTION(target_type) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                CCerror(context, "Component type of the array must be reference type in aastore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        case opc_putfield:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        case opc_getfield:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        case opc_putstatic: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            int operand = this_idata->operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            fullinfo_type stack_object = stack_extra_info[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            if (opcode == opc_putfield || opcode == opc_getfield) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                if (!isAssignableTo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                        (context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                         stack_object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                         cp_index_to_class_fullinfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                             (context, operand, JVM_CONSTANT_Fieldref))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                    CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                            "Incompatible type for getting or setting field");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                if (this_idata->protected &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                    !isAssignableTo(context, stack_object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                                    context->currentclass_info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                    CCerror(context, "Bad access to protected data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            if (opcode == opc_putfield || opcode == opc_putstatic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                int item = (opcode == opc_putfield ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                if (!isAssignableTo(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                                    stack_extra_info[item], put_full_info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                    CCerror(context, "Bad type in putfield/putstatic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
        case opc_athrow:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
            if (!isAssignableTo(context, stack_extra_info[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                                context->throwable_info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                CCerror(context, "Can only throw Throwable objects");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
        case opc_aaload: {      /* array index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            /* We need to pass the information to the stack updater */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            fullinfo_type array_type = stack_extra_info[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            context->swap_table[0] = decrement_indirection(array_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        case opc_invokevirtual: case opc_invokespecial:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
        case opc_invokeinit:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
        case opc_invokeinterface: case opc_invokestatic: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
            int operand = this_idata->operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            const char *signature =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                JVM_GetCPMethodSignatureUTF(context->env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                                            context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                                            operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            int item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            const char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            check_and_push(context, signature, VM_STRING_UTF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
            if (opcode == opc_invokestatic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                item = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
            } else if (opcode == opc_invokeinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                fullinfo_type init_type = this_idata->operand2.fi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                fullinfo_type object_type = stack_extra_info[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                context->swap_table[0] = object_type; /* save value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                if (GET_ITEM_TYPE(stack_extra_info[0]) == ITEM_NewObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                    /* We better be calling the appropriate init.  Find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                     * inumber of the "opc_new" instruction", and figure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                     * out what the type really is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                    unsigned int new_inumber = GET_EXTRA_INFO(stack_extra_info[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                    fullinfo_type target_type = idata[new_inumber].operand2.fi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                    context->swap_table[1] = target_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                    if (target_type != init_type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                        CCerror(context, "Call to wrong initialization method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                    if (this_idata->protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                        && context->major_version > LDC_CLASS_MAJOR_VERSION
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                        && !isAssignableTo(context, object_type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                                           context->currentclass_info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                      CCerror(context, "Bad access to protected data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                    /* We better be calling super() or this(). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                    if (init_type != context->superclass_info &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                        init_type != context->currentclass_info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                        CCerror(context, "Call to wrong initialization method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                    context->swap_table[1] = context->currentclass_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                item = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                fullinfo_type target_type = this_idata->operand2.fi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                fullinfo_type object_type = stack_extra_info[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                if (!isAssignableTo(context, object_type, target_type)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                    CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                            "Incompatible object argument for function call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                if (opcode == opc_invokespecial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                    && !isAssignableTo(context, object_type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                                       context->currentclass_info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                    /* Make sure object argument is assignment compatible to current class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                    CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                            "Incompatible object argument for invokespecial");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                if (this_idata->protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                    && !isAssignableTo(context, object_type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                                       context->currentclass_info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                    /* This is ugly. Special dispensation.  Arrays pretend to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                       implement public Object clone() even though they don't */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                    const char *utfName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                        JVM_GetCPMethodNameUTF(context->env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                                               context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                                               this_idata->operand.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                    int is_clone = utfName && (strcmp(utfName, "clone") == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                    JVM_ReleaseUTF(utfName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                    if ((target_type == context->object_info) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                        (GET_INDIRECTION(object_type) > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                        is_clone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                        CCerror(context, "Bad access to protected data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                item = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            for (p = signature + 1; *p != JVM_SIGNATURE_ENDFUNC; item++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                if (signature_to_fieldtype(context, &p, &full_info) == 'A') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                    if (!isAssignableTo(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                                        stack_extra_info[item], full_info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
                        CCerror(context, "Incompatible argument to function");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        case opc_return:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
            if (context->return_type != MAKE_FULLINFO(ITEM_Void, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                CCerror(context, "Wrong return type in function");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
        case opc_ireturn: case opc_lreturn: case opc_freturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
        case opc_dreturn: case opc_areturn: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
            fullinfo_type target_type = context->return_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
            fullinfo_type object_type = stack_extra_info[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
            if (!isAssignableTo(context, object_type, target_type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                CCerror(context, "Wrong return type in function");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        case opc_new: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            /* Make sure that nothing on the stack already looks like what
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
             * we want to create.  I can't image how this could possibly happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
             * but we should test for it anyway, since if it could happen, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
             * result would be an unitialized object being able to masquerade
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
             * as an initialized one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            stack_item_type *item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            for (item = stack; item != NULL; item = item->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
                if (item->item == this_idata->operand.fi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                    CCerror(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                            "Uninitialized object on stack at creating point");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            /* Info for update_registers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            context->swap_table[0] = this_idata->operand.fi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            context->swap_table[1] = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
    new_stack_info->stack = stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
    new_stack_info->stack_size = stack_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
/* We've already determined that the instruction is legal.  Perform the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
 * operation on the registers, and return the updated results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
 * new_register_count_p and new_registers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
update_registers(context_type *context, unsigned int inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                 register_info_type *new_register_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
    instruction_data_type *this_idata = &idata[inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
    opcode_type opcode = this_idata->opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
    int operand = this_idata->operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
    int register_count = this_idata->register_info.register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
    fullinfo_type *registers = this_idata->register_info.registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
    stack_item_type *stack = this_idata->stack_info.stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
    int mask_count = this_idata->register_info.mask_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
    mask_type *masks = this_idata->register_info.masks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
    /* Use these as default new values. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    int            new_register_count = register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
    int            new_mask_count = mask_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
    fullinfo_type *new_registers = registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    mask_type     *new_masks = masks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
    enum { ACCESS_NONE, ACCESS_SINGLE, ACCESS_DOUBLE } access = ACCESS_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    /* Remember, we've already verified the type at the top of the stack. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
    switch (opcode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
        default: break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
        case opc_istore: case opc_fstore: case opc_astore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
            access = ACCESS_SINGLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
            goto continue_store;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
        case opc_lstore: case opc_dstore:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
            access = ACCESS_DOUBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
            goto continue_store;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
        continue_store: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
            /* We have a modification to the registers.  Copy them if needed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
            fullinfo_type stack_top_type = stack->item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
            int max_operand = operand + ((access == ACCESS_DOUBLE) ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
            if (     max_operand < register_count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
                  && registers[operand] == stack_top_type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                  && ((access == ACCESS_SINGLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                         (registers[operand + 1]== stack_top_type + 1)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                /* No changes have been made to the registers. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
            new_register_count = MAX(max_operand + 1, register_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
            new_registers = NEW(fullinfo_type, new_register_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
            for (i = 0; i < register_count; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                new_registers[i] = registers[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
            for (i = register_count; i < new_register_count; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                new_registers[i] = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
            new_registers[operand] = stack_top_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
            if (access == ACCESS_DOUBLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                new_registers[operand + 1] = stack_top_type + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
        case opc_iload: case opc_fload: case opc_aload:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
        case opc_iinc: case opc_ret:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
            access = ACCESS_SINGLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        case opc_lload: case opc_dload:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
            access = ACCESS_DOUBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
        case opc_jsr: case opc_jsr_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
            for (i = 0; i < new_mask_count; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                if (new_masks[i].entry == operand)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                    CCerror(context, "Recursive call to jsr entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
            new_masks = add_to_masks(context, masks, mask_count, operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
            new_mask_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
        case opc_invokeinit:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
        case opc_new: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
            /* For invokeinit, an uninitialized object has been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
             * For new, all previous occurrences of an uninitialized object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
             * from the same instruction must be made bogus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
             * We find all occurrences of swap_table[0] in the registers, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
             * replace them with swap_table[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
            fullinfo_type from = context->swap_table[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
            fullinfo_type to = context->swap_table[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
            for (i = 0; i < register_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                if (new_registers[i] == from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
                    /* Found a match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
            if (i < register_count) { /* We broke out loop for match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
                /* We have to change registers, and possibly a mask */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                jboolean copied_mask = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                int k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                new_registers = NEW(fullinfo_type, register_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
                memcpy(new_registers, registers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
                       register_count * sizeof(registers[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
                for ( ; i < register_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                    if (new_registers[i] == from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
                        new_registers[i] = to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
                        for (k = 0; k < new_mask_count; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
                            if (!IS_BIT_SET(new_masks[k].modifies, i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
                                if (!copied_mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
                                    new_masks = copy_masks(context, new_masks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
                                                           mask_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                                    copied_mask = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
                                SET_BIT(new_masks[k].modifies, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
    } /* of switch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
    if ((access != ACCESS_NONE) && (new_mask_count > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
        for (i = 0; i < new_mask_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            int *mask = new_masks[i].modifies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
            if ((!IS_BIT_SET(mask, operand)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
                  ((access == ACCESS_DOUBLE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
                   !IS_BIT_SET(mask, operand + 1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
                new_masks = copy_masks(context, new_masks, mask_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
                for (j = i; j < new_mask_count; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                    SET_BIT(new_masks[j].modifies, operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
                    if (access == ACCESS_DOUBLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                        SET_BIT(new_masks[j].modifies, operand + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
    new_register_info->register_count = new_register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
    new_register_info->registers = new_registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
    new_register_info->masks = new_masks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
    new_register_info->mask_count = new_mask_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
/* We've already determined that the instruction is legal, and have updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
 * the registers.  Update the flags, too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
update_flags(context_type *context, unsigned int inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
             flag_type *new_and_flags, flag_type *new_or_flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
    instruction_data_type *this_idata = &idata[inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
    flag_type and_flags = this_idata->and_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
    flag_type or_flags = this_idata->or_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
    /* Set the "we've done a constructor" flag */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
    if (this_idata->opcode == opc_invokeinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
        fullinfo_type from = context->swap_table[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        if (from == MAKE_FULLINFO(ITEM_InitObject, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
            and_flags |= FLAG_CONSTRUCTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
    *new_and_flags = and_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
    *new_or_flags = or_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
/* We've already determined that the instruction is legal.  Perform the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
 * operation on the stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
 * new_stack_size_p and new_stack_p point to the results after the pops have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
 * already been done.  Do the pushes, and then put the results back there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
push_stack(context_type *context, unsigned int inumber, stack_info_type *new_stack_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
    instruction_data_type *this_idata = &idata[inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    opcode_type opcode = this_idata->opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
    int operand = this_idata->operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
    int stack_size = new_stack_info->stack_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
    stack_item_type *stack = new_stack_info->stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
    char *stack_results;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
    fullinfo_type full_info = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
    char buffer[5], *p;         /* actually [2] is big enough */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
    /* We need to look at all those opcodes in which either we can't tell the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
     * value pushed onto the stack from the opcode, or in which the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
     * pushed onto the stack is an object or array.  For the latter, we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
     * to make sure that full_info is set to the right value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
    switch(opcode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            stack_results = opcode_in_out[opcode][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
        case opc_ldc: case opc_ldc_w: case opc_ldc2_w: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
            /* Look to constant pool to determine correct result. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
            unsigned char *type_table = context->constant_types;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
            switch (type_table[operand]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                case JVM_CONSTANT_Integer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                    stack_results = "I"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                case JVM_CONSTANT_Float:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
                    stack_results = "F"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                case JVM_CONSTANT_Double:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                    stack_results = "D"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                case JVM_CONSTANT_Long:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                    stack_results = "L"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                case JVM_CONSTANT_String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                    stack_results = "A";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
                    full_info = context->string_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
                case JVM_CONSTANT_Class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                    if (context->major_version < LDC_CLASS_MAJOR_VERSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
                        CCerror(context, "Internal error #3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                    stack_results = "A";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                    full_info = make_class_info_from_name(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
                                                          "java/lang/Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                    CCerror(context, "Internal error #3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                    stack_results = ""; /* Never reached: keep lint happy */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
        case opc_getstatic: case opc_getfield: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
            /* Look to signature to determine correct result. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
            int operand = this_idata->operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
            const char *signature = JVM_GetCPFieldSignatureUTF(context->env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
                                                               context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                                                               operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
            check_and_push(context, signature, VM_STRING_UTF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
            if (verify_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
                print_formatted_fieldname(context, operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
            buffer[0] = signature_to_fieldtype(context, &signature, &full_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
            buffer[1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
            stack_results = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
            pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
        case opc_invokevirtual: case opc_invokespecial:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
        case opc_invokeinit:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        case opc_invokestatic: case opc_invokeinterface: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
            /* Look to signature to determine correct result. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
            int operand = this_idata->operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
            const char *signature = JVM_GetCPMethodSignatureUTF(context->env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                                                                context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
                                                                operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
            const char *result_signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
            check_and_push(context, signature, VM_STRING_UTF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
            result_signature = strchr(signature, JVM_SIGNATURE_ENDFUNC) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
            if (result_signature[0] == JVM_SIGNATURE_VOID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
                stack_results = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                buffer[0] = signature_to_fieldtype(context, &result_signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                                                   &full_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
                buffer[1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                stack_results = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
            pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
        case opc_aconst_null:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
            stack_results = opcode_in_out[opcode][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
            full_info = NULL_FULLINFO; /* special NULL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
        case opc_new:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
        case opc_checkcast:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
        case opc_newarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
        case opc_anewarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
        case opc_multianewarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
            stack_results = opcode_in_out[opcode][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
            /* Conveniently, this result type is stored here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
            full_info = this_idata->operand.fi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
        case opc_aaload:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
            stack_results = opcode_in_out[opcode][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
            /* pop_stack() saved value for us. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
            full_info = context->swap_table[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
        case opc_aload:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
            stack_results = opcode_in_out[opcode][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
            /* The register hasn't been modified, so we can use its value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
            full_info = this_idata->register_info.registers[operand];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
    } /* of switch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
    for (p = stack_results; *p != 0; p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
        int type = *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
        stack_item_type *new_item = NEW(stack_item_type, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
        new_item->next = stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
        stack = new_item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
            case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
                stack->item = MAKE_FULLINFO(ITEM_Integer, 0, 0); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
            case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
                stack->item = MAKE_FULLINFO(ITEM_Float, 0, 0); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
            case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
                stack->item = MAKE_FULLINFO(ITEM_Double, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
                stack_size++; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
            case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
                stack->item = MAKE_FULLINFO(ITEM_Long, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
                stack_size++; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
            case 'R':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
                stack->item = MAKE_FULLINFO(ITEM_ReturnAddress, 0, operand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
            case '1': case '2': case '3': case '4': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
                /* Get the info saved in the swap_table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
                fullinfo_type stype = context->swap_table[type - '1'];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
                stack->item = stype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                if (stype == MAKE_FULLINFO(ITEM_Long, 0, 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                    stype == MAKE_FULLINFO(ITEM_Double, 0, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                    stack_size++; p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
            case 'A':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                /* full_info should have the appropriate value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                assert(full_info != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
                stack->item = full_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                CCerror(context, "Internal error #4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
            } /* switch type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
        stack_size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
    } /* outer for loop */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
    if (opcode == opc_invokeinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
        /* If there are any instances of "from" on the stack, we need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
         * replace it with "to", since calling <init> initializes all versions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
         * of the object, obviously.     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
        fullinfo_type from = context->swap_table[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
        stack_item_type *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
        for (ptr = stack; ptr != NULL; ptr = ptr->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
            if (ptr->item == from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                fullinfo_type to = context->swap_table[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
                stack = copy_stack(context, stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
                for (ptr = stack; ptr != NULL; ptr = ptr->next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
                    if (ptr->item == from) ptr->item = to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
    new_stack_info->stack_size = stack_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
    new_stack_info->stack = stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
/* We've performed an instruction, and determined the new registers and stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
 * value.  Look at all of the possibly subsequent instructions, and merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
 * this stack value into theirs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
merge_into_successors(context_type *context, unsigned int inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
                      register_info_type *register_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                      stack_info_type *stack_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                      flag_type and_flags, flag_type or_flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
    instruction_data_type *this_idata = &idata[inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
    opcode_type opcode = this_idata->opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
    int operand = this_idata->operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
    struct handler_info_type *handler_info = context->handler_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
    int handler_info_length =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
        JVM_GetMethodIxExceptionTableLength(context->env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                                            context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                                            context->method_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
    int buffer[2];              /* default value for successors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
    int *successors = buffer;   /* table of successors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
    int successors_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
    switch (opcode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
        successors_count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
        buffer[0] = inumber + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
    case opc_ifeq: case opc_ifne: case opc_ifgt:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
    case opc_ifge: case opc_iflt: case opc_ifle:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
    case opc_ifnull: case opc_ifnonnull:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
    case opc_if_icmpeq: case opc_if_icmpne: case opc_if_icmpgt:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
    case opc_if_icmpge: case opc_if_icmplt: case opc_if_icmple:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
    case opc_if_acmpeq: case opc_if_acmpne:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
        successors_count = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
        buffer[0] = inumber + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
        buffer[1] = operand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
    case opc_jsr: case opc_jsr_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        if (this_idata->operand2.i != UNKNOWN_RET_INSTRUCTION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
            idata[this_idata->operand2.i].changed = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
        /* FALLTHROUGH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
    case opc_goto: case opc_goto_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
        successors_count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
        buffer[0] = operand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
    case opc_ireturn: case opc_lreturn: case opc_return:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
    case opc_freturn: case opc_dreturn: case opc_areturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
    case opc_athrow:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
        /* The testing for the returns is handled in pop_stack() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        successors_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
    case opc_ret: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
        /* This is slightly slow, but good enough for a seldom used instruction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
         * The EXTRA_ITEM_INFO of the ITEM_ReturnAddress indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
         * address of the first instruction of the subroutine.  We can return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
         * to 1 after any instruction that jsr's to that instruction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
        if (this_idata->operand2.ip == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
            fullinfo_type *registers = this_idata->register_info.registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
            int called_instruction = GET_EXTRA_INFO(registers[operand]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
            int i, count, *ptr;;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
            for (i = context->instruction_count, count = 0; --i >= 0; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                if (((idata[i].opcode == opc_jsr) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
                     (idata[i].opcode == opc_jsr_w)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
                    (idata[i].operand.i == called_instruction))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
            this_idata->operand2.ip = ptr = NEW(int, count + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            *ptr++ = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
            for (i = context->instruction_count, count = 0; --i >= 0; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                if (((idata[i].opcode == opc_jsr) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
                     (idata[i].opcode == opc_jsr_w)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
                    (idata[i].operand.i == called_instruction))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
                    *ptr++ = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
        successors = this_idata->operand2.ip; /* use this instead */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
        successors_count = *successors++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
    case opc_tableswitch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
    case opc_lookupswitch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
        successors = this_idata->operand.ip; /* use this instead */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
        successors_count = *successors++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
    if (verify_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
        jio_fprintf(stdout, " [");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
        for (i = handler_info_length; --i >= 0; handler_info++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
            if (handler_info->start <= inumber && handler_info->end > inumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                jio_fprintf(stdout, "%d* ", handler_info->handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        for (i = 0; i < successors_count; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
            jio_fprintf(stdout, "%d ", successors[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
        jio_fprintf(stdout,   "]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
    handler_info = context->handler_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
    for (i = handler_info_length; --i >= 0; handler_info++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
        if (handler_info->start <= inumber && handler_info->end > inumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
            int handler = handler_info->handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
            if (opcode != opc_invokeinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                merge_into_one_successor(context, inumber, handler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                                         &this_idata->register_info, /* old */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                                         &handler_info->stack_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                                         (flag_type) (and_flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                                                      & this_idata->and_flags),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                                         (flag_type) (or_flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
                                                      | this_idata->or_flags),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                                         JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
                /* We need to be a little bit more careful with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                 * instruction.  Things could either be in the state before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
                 * the instruction or in the state afterwards */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                fullinfo_type from = context->swap_table[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                flag_type temp_or_flags = or_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                if (from == MAKE_FULLINFO(ITEM_InitObject, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
                    temp_or_flags |= FLAG_NO_RETURN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                merge_into_one_successor(context, inumber, handler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                                         &this_idata->register_info, /* old */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                                         &handler_info->stack_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                                         this_idata->and_flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                                         this_idata->or_flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                                         JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
                merge_into_one_successor(context, inumber, handler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
                                         register_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                                         &handler_info->stack_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                                         and_flags, temp_or_flags, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
    for (i = 0; i < successors_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
        int target = successors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
        if (target >= context->instruction_count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
            CCerror(context, "Falling off the end of the code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
        merge_into_one_successor(context, inumber, target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
                                 register_info, stack_info, and_flags, or_flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
                                 JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
/* We have a new set of registers and stack values for a given instruction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
 * Merge this new set into the values that are already there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
merge_into_one_successor(context_type *context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
                         unsigned int from_inumber, unsigned int to_inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
                         register_info_type *new_register_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
                         stack_info_type *new_stack_info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
                         flag_type new_and_flags, flag_type new_or_flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
                         jboolean isException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
    register_info_type register_info_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
    stack_info_type stack_info_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
    instruction_data_type *this_idata = &idata[to_inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
    register_info_type old_reg_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
    stack_info_type old_stack_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
    flag_type old_and_flags, old_or_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
    if (verify_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
        old_reg_info = this_idata->register_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
        old_stack_info = this_idata->stack_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
        old_and_flags = this_idata->and_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
        old_or_flags = this_idata->or_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
    /* All uninitialized objects are set to "bogus" when jsr and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
     * ret are executed. Thus uninitialized objects can't propagate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
     * into or out of a subroutine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
    if (idata[from_inumber].opcode == opc_ret ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        idata[from_inumber].opcode == opc_jsr ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        idata[from_inumber].opcode == opc_jsr_w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
        int new_register_count = new_register_info->register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
        fullinfo_type *new_registers = new_register_info->registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
        stack_item_type *item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
        for (item = new_stack_info->stack; item != NULL; item = item->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
            if (GET_ITEM_TYPE(item->item) == ITEM_NewObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
                /* This check only succeeds for hand-contrived code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
                 * Efficiency is not an issue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
                stack_info_buf.stack = copy_stack(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
                                                  new_stack_info->stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
                stack_info_buf.stack_size = new_stack_info->stack_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
                new_stack_info = &stack_info_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
                for (item = new_stack_info->stack; item != NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
                     item = item->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
                    if (GET_ITEM_TYPE(item->item) == ITEM_NewObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
                        item->item = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
        for (i = 0; i < new_register_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
            if (GET_ITEM_TYPE(new_registers[i]) == ITEM_NewObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                /* This check only succeeds for hand-contrived code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                 * Efficiency is not an issue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
                fullinfo_type *new_set = NEW(fullinfo_type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
                                             new_register_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
                for (i = 0; i < new_register_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
                    fullinfo_type t = new_registers[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
                    new_set[i] = GET_ITEM_TYPE(t) != ITEM_NewObject ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
                        t : MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                register_info_buf.register_count = new_register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
                register_info_buf.registers = new_set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
                register_info_buf.mask_count = new_register_info->mask_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
                register_info_buf.masks = new_register_info->masks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
                new_register_info = &register_info_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
    /* Returning from a subroutine is somewhat ugly.  The actual thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
     * that needs to get merged into the new instruction is a joining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
     * of info from the ret instruction with stuff in the jsr instruction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
    if (idata[from_inumber].opcode == opc_ret && !isException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
        int new_register_count = new_register_info->register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
        fullinfo_type *new_registers = new_register_info->registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
        int new_mask_count = new_register_info->mask_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
        mask_type *new_masks = new_register_info->masks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
        int operand = idata[from_inumber].operand.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
        int called_instruction = GET_EXTRA_INFO(new_registers[operand]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
        instruction_data_type *jsr_idata = &idata[to_inumber - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
        register_info_type *jsr_reginfo = &jsr_idata->register_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
        if (jsr_idata->operand2.i != from_inumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
            if (jsr_idata->operand2.i != UNKNOWN_RET_INSTRUCTION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
                CCerror(context, "Multiple returns to single jsr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
            jsr_idata->operand2.i = from_inumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
        if (jsr_reginfo->register_count == UNKNOWN_REGISTER_COUNT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
            /* We don't want to handle the returned-to instruction until
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
             * we've dealt with the jsr instruction.   When we get to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
             * jsr instruction (if ever), we'll re-mark the ret instruction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
            int register_count = jsr_reginfo->register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
            fullinfo_type *registers = jsr_reginfo->registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
            int max_registers = MAX(register_count, new_register_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
            fullinfo_type *new_set = NEW(fullinfo_type, max_registers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
            int *return_mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
            struct register_info_type new_new_register_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
            /* Make sure the place we're returning from is legal! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
            for (i = new_mask_count; --i >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
                if (new_masks[i].entry == called_instruction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
            if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
                CCerror(context, "Illegal return from subroutine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
            /* pop the masks down to the indicated one.  Remember the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
             * we're popping off. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
            return_mask = new_masks[i].modifies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
            new_mask_count = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
            for (i = 0; i < max_registers; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
                if (IS_BIT_SET(return_mask, i))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
                    new_set[i] = i < new_register_count ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
                          new_registers[i] : MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
                    new_set[i] = i < register_count ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
                        registers[i] : MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
            new_new_register_info.register_count = max_registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
            new_new_register_info.registers      = new_set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
            new_new_register_info.mask_count     = new_mask_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
            new_new_register_info.masks          = new_masks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
            merge_stack(context, from_inumber, to_inumber, new_stack_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
            merge_registers(context, to_inumber - 1, to_inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
                            &new_new_register_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
            merge_flags(context, from_inumber, to_inumber, new_and_flags, new_or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
        merge_stack(context, from_inumber, to_inumber, new_stack_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        merge_registers(context, from_inumber, to_inumber, new_register_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
        merge_flags(context, from_inumber, to_inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
                    new_and_flags, new_or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
    if (verify_verbose && idata[to_inumber].changed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
        register_info_type *register_info = &this_idata->register_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
        stack_info_type *stack_info = &this_idata->stack_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
        if (memcmp(&old_reg_info, register_info, sizeof(old_reg_info)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
            memcmp(&old_stack_info, stack_info, sizeof(old_stack_info)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
            (old_and_flags != this_idata->and_flags) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
            (old_or_flags != this_idata->or_flags)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
            jio_fprintf(stdout, "   %2d:", to_inumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
            print_stack(context, &old_stack_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
            print_registers(context, &old_reg_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
            print_flags(context, old_and_flags, old_or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
            jio_fprintf(stdout, " => ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
            print_stack(context, &this_idata->stack_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
            print_registers(context, &this_idata->register_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
            print_flags(context, this_idata->and_flags, this_idata->or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
            jio_fprintf(stdout, "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
merge_stack(context_type *context, unsigned int from_inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
            unsigned int to_inumber, stack_info_type *new_stack_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
    instruction_data_type *this_idata = &idata[to_inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
    int new_stack_size =  new_stack_info->stack_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
    stack_item_type *new_stack = new_stack_info->stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
    int stack_size = this_idata->stack_info.stack_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
    if (stack_size == UNKNOWN_STACK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
        /* First time at this instruction.  Just copy. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
        this_idata->stack_info.stack_size = new_stack_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
        this_idata->stack_info.stack = new_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
        this_idata->changed = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
    } else if (new_stack_size != stack_size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
        CCerror(context, "Inconsistent stack height %d != %d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
                new_stack_size, stack_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
        stack_item_type *stack = this_idata->stack_info.stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
        stack_item_type *old, *new;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
        jboolean change = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
        for (old = stack, new = new_stack; old != NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
                   old = old->next, new = new->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
            if (!isAssignableTo(context, new->item, old->item)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
                change = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        if (change) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
            stack = copy_stack(context, stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
            for (old = stack, new = new_stack; old != NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
                          old = old->next, new = new->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
                if (new == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
                old->item = merge_fullinfo_types(context, old->item, new->item,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
                                                 JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
                if (GET_ITEM_TYPE(old->item) == ITEM_Bogus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
                        CCerror(context, "Mismatched stack types");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
            if (old != NULL || new != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
                CCerror(context, "Mismatched stack types");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
            this_idata->stack_info.stack = stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
            this_idata->changed = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
merge_registers(context_type *context, unsigned int from_inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
                unsigned int to_inumber, register_info_type *new_register_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
    instruction_data_type *this_idata = &idata[to_inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
    register_info_type    *this_reginfo = &this_idata->register_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
    int            new_register_count = new_register_info->register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
    fullinfo_type *new_registers = new_register_info->registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
    int            new_mask_count = new_register_info->mask_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
    mask_type     *new_masks = new_register_info->masks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
    if (this_reginfo->register_count == UNKNOWN_REGISTER_COUNT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
        this_reginfo->register_count = new_register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
        this_reginfo->registers = new_registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
        this_reginfo->mask_count = new_mask_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
        this_reginfo->masks = new_masks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
        this_idata->changed = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
        /* See if we've got new information on the register set. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
        int register_count = this_reginfo->register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
        fullinfo_type *registers = this_reginfo->registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
        int mask_count = this_reginfo->mask_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
        mask_type *masks = this_reginfo->masks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
        jboolean copy = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
        if (register_count > new_register_count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
            /* Any register larger than new_register_count is now bogus */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
            this_reginfo->register_count = new_register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
            register_count = new_register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
            this_idata->changed = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
        for (i = 0; i < register_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
            fullinfo_type prev_value = registers[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
            if ((i < new_register_count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
                  ? (!isAssignableTo(context, new_registers[i], prev_value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
                  : (prev_value != MAKE_FULLINFO(ITEM_Bogus, 0, 0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
                copy = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
        if (copy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
            /* We need a copy.  So do it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
            fullinfo_type *new_set = NEW(fullinfo_type, register_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
            for (j = 0; j < i; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
                new_set[j] =  registers[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
            for (j = i; j < register_count; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
                if (i >= new_register_count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
                    new_set[j] = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
                    new_set[j] = merge_fullinfo_types(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
                                                      new_registers[j],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
                                                      registers[j], JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
            /* Some of the end items might now be bogus. This step isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
             * necessary, but it may save work later. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
            while (   register_count > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
                   && GET_ITEM_TYPE(new_set[register_count-1]) == ITEM_Bogus)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
                register_count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
            this_reginfo->register_count = register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
            this_reginfo->registers = new_set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
            this_idata->changed = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
        if (mask_count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
            /* If the target instruction already has a sequence of masks, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
             * we need to merge new_masks into it.  We want the entries on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
             * the mask to be the longest common substring of the two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
             *   (e.g.   a->b->d merged with a->c->d should give a->d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
             * The bits set in the mask should be the or of the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
             * entries in each of the original masks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
            int i, j, k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
            int matches = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
            int last_match = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
            jboolean copy_needed = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
            for (i = 0; i < mask_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
                int entry = masks[i].entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
                for (j = last_match + 1; j < new_mask_count; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
                    if (new_masks[j].entry == entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
                        /* We have a match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
                        int *prev = masks[i].modifies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
                        int *new = new_masks[j].modifies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
                        matches++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
                        /* See if new_mask has bits set for "entry" that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
                         * weren't set for mask.  If so, need to copy. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
                        for (k = context->bitmask_size - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
                               !copy_needed && k >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
                               k--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
                            if (~prev[k] & new[k])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
                                copy_needed = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
                        last_match = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
            if ((matches < mask_count) || copy_needed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
                /* We need to make a copy for the new item, since either the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
                 * size has decreased, or new bits are set. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
                mask_type *copy = NEW(mask_type, matches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
                for (i = 0; i < matches; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
                    copy[i].modifies = NEW(int, context->bitmask_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
                this_reginfo->masks = copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
                this_reginfo->mask_count = matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
                this_idata->changed = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
                matches = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
                last_match = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
                for (i = 0; i < mask_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
                    int entry = masks[i].entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
                    for (j = last_match + 1; j < new_mask_count; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
                        if (new_masks[j].entry == entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
                            int *prev1 = masks[i].modifies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
                            int *prev2 = new_masks[j].modifies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
                            int *new = copy[matches].modifies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
                            copy[matches].entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
                            for (k = context->bitmask_size - 1; k >= 0; k--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
                                new[k] = prev1[k] | prev2[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
                            matches++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
                            last_match = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
merge_flags(context_type *context, unsigned int from_inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
            unsigned int to_inumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
            flag_type new_and_flags, flag_type new_or_flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
    /* Set this_idata->and_flags &= new_and_flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
           this_idata->or_flags |= new_or_flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
    instruction_data_type *idata = context->instruction_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
    instruction_data_type *this_idata = &idata[to_inumber];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
    flag_type this_and_flags = this_idata->and_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
    flag_type this_or_flags = this_idata->or_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
    flag_type merged_and = this_and_flags & new_and_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
    flag_type merged_or = this_or_flags | new_or_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
    if ((merged_and != this_and_flags) || (merged_or != this_or_flags)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
        this_idata->and_flags = merged_and;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
        this_idata->or_flags = merged_or;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
        this_idata->changed = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
/* Make a copy of a stack */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
static stack_item_type *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
copy_stack(context_type *context, stack_item_type *stack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
    int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
    stack_item_type *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
    /* Find the length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
    for (ptr = stack, length = 0; ptr != NULL; ptr = ptr->next, length++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
    if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
        stack_item_type *new_stack = NEW(stack_item_type, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
        stack_item_type *new_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
        for (    ptr = stack, new_ptr = new_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
                 ptr != NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
                 ptr = ptr->next, new_ptr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
            new_ptr->item = ptr->item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
            new_ptr->next = new_ptr + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
        new_stack[length - 1].next = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
        return new_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
static mask_type *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
copy_masks(context_type *context, mask_type *masks, int mask_count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
    mask_type *result = NEW(mask_type, mask_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
    int bitmask_size = context->bitmask_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
    int *bitmaps = NEW(int, mask_count * bitmask_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
    for (i = 0; i < mask_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
        result[i].entry = masks[i].entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
        result[i].modifies = &bitmaps[i * bitmask_size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
        memcpy(result[i].modifies, masks[i].modifies, bitmask_size * sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
static mask_type *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
add_to_masks(context_type *context, mask_type *masks, int mask_count, int d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
    mask_type *result = NEW(mask_type, mask_count + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
    int bitmask_size = context->bitmask_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
    int *bitmaps = NEW(int, (mask_count + 1) * bitmask_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
    for (i = 0; i < mask_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
        result[i].entry = masks[i].entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
        result[i].modifies = &bitmaps[i * bitmask_size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
        memcpy(result[i].modifies, masks[i].modifies, bitmask_size * sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
    result[mask_count].entry = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
    result[mask_count].modifies = &bitmaps[mask_count * bitmask_size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
    memset(result[mask_count].modifies, 0, bitmask_size * sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
/* We create our own storage manager, since we malloc lots of little items,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
 * and I don't want to keep trace of when they become free.  I sure wish that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
 * we had heaps, and I could just free the heap when done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
#define CCSegSize 2000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
struct CCpool {                 /* a segment of allocated memory in the pool */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
    struct CCpool *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
    int segSize;                /* almost always CCSegSize */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
    int poolPad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
    char space[CCSegSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
/* Initialize the context's heap. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
static void CCinit(context_type *context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
    struct CCpool *new = (struct CCpool *) malloc(sizeof(struct CCpool));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
    /* Set context->CCroot to 0 if new == 0 to tell CCdestroy to lay off */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
    context->CCroot = context->CCcurrent = new;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
    if (new == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
        CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
    new->next = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
    new->segSize = CCSegSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
    context->CCfree_size = CCSegSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
    context->CCfree_ptr = &new->space[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
/* Reuse all the space that we have in the context's heap. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
static void CCreinit(context_type *context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
    struct CCpool *first = context->CCroot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
    context->CCcurrent = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
    context->CCfree_size = CCSegSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
    context->CCfree_ptr = &first->space[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
/* Destroy the context's heap. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
static void CCdestroy(context_type *context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
    struct CCpool *this = context->CCroot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
    while (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
        struct CCpool *next = this->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
        free(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
        this = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
    /* These two aren't necessary.  But can't hurt either */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
    context->CCroot = context->CCcurrent = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
    context->CCfree_ptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
/* Allocate an object of the given size from the context's heap. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
static void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
CCalloc(context_type *context, int size, jboolean zero)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
    register char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
    /* Round CC to the size of a pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
    size = (size + (sizeof(void *) - 1)) & ~(sizeof(void *) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
    if (context->CCfree_size <  size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
        struct CCpool *current = context->CCcurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
        struct CCpool *new;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
        if (size > CCSegSize) { /* we need to allocate a special block */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
            new = (struct CCpool *)malloc(sizeof(struct CCpool) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
                                          (size - CCSegSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
            if (new == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
                CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
            new->next = current->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
            new->segSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
            current->next = new;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
            new = current->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
            if (new == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
                new = (struct CCpool *) malloc(sizeof(struct CCpool));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
                if (new == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
                    CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
                current->next = new;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
                new->next = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
                new->segSize = CCSegSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
        context->CCcurrent = new;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
        context->CCfree_ptr = &new->space[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
        context->CCfree_size = new->segSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
    p = context->CCfree_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
    context->CCfree_ptr += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
    context->CCfree_size -= size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
    if (zero)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
        memset(p, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
/* Get the class associated with a particular field or method or class in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
 * constant pool.  If is_field is true, we've got a field or method.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
 * false, we've got a class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
static fullinfo_type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
cp_index_to_class_fullinfo(context_type *context, int cp_index, int kind)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
    fullinfo_type result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
    const char *classname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
    switch (kind) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
    case JVM_CONSTANT_Class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
        classname = JVM_GetCPClassNameUTF(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
                                          context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
                                          cp_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
    case JVM_CONSTANT_Methodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
        classname = JVM_GetCPMethodClassNameUTF(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
                                                context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
                                                cp_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
    case JVM_CONSTANT_Fieldref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
        classname = JVM_GetCPFieldClassNameUTF(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
                                               context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
                                               cp_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
        classname = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
        CCerror(context, "Internal error #5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
    check_and_push(context, classname, VM_STRING_UTF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
    if (classname[0] == JVM_SIGNATURE_ARRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
        /* This make recursively call us, in case of a class array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
        signature_to_fieldtype(context, &classname, &result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
        result = make_class_info_from_name(context, classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
    pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
print_CCerror_info(context_type *context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
    jclass cb = context->class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
    const char *classname = JVM_GetClassNameUTF(env, cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
    const char *name = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
    const char *signature = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
    int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
    if (context->method_index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
        name = JVM_GetMethodIxNameUTF(env, cb, context->method_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
        signature =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
            JVM_GetMethodIxSignatureUTF(env, cb, context->method_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
        n += jio_snprintf(context->message, context->message_buf_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
                          "(class: %s, method: %s signature: %s) ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
                          (classname ? classname : ""),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
                          (name ? name : ""),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
                          (signature ? signature : ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
    } else if (context->field_index != -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
        name = JVM_GetMethodIxNameUTF(env, cb, context->field_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
        n += jio_snprintf(context->message, context->message_buf_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
                          "(class: %s, field: %s) ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
                          (classname ? classname : 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
                          (name ? name : 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
        n += jio_snprintf(context->message, context->message_buf_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
                          "(class: %s) ", classname ? classname : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
    JVM_ReleaseUTF(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
    JVM_ReleaseUTF(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
    JVM_ReleaseUTF(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
CCerror (context_type *context, char *format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
    int n = print_CCerror_info(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
    va_list args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
    if (n >= 0 && n < context->message_buf_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
        va_start(args, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
        jio_vsnprintf(context->message + n, context->message_buf_len - n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
                      format, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
        va_end(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
    context->err_code = CC_VerifyError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
    longjmp(context->jump_buffer, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
CCout_of_memory(context_type *context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
    int n = print_CCerror_info(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
    context->err_code = CC_OutOfMemory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
    longjmp(context->jump_buffer, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
CFerror(context_type *context, char *format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
    int n = print_CCerror_info(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
    va_list args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
    if (n >= 0 && n < context->message_buf_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
        va_start(args, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
        jio_vsnprintf(context->message + n, context->message_buf_len - n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
                      format, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
        va_end(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
    context->err_code = CC_ClassFormatError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
    longjmp(context->jump_buffer, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
static char
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
signature_to_fieldtype(context_type *context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
                       const char **signature_p, fullinfo_type *full_info_p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
    const char *p = *signature_p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
    fullinfo_type full_info = MAKE_FULLINFO(0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
    char result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
    int array_depth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
        switch(*p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
                full_info = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
                result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
            case JVM_SIGNATURE_BOOLEAN: case JVM_SIGNATURE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
                full_info = (array_depth > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
                              ? MAKE_FULLINFO(ITEM_Byte, 0, 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
                              : MAKE_FULLINFO(ITEM_Integer, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
                result = 'I';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
            case JVM_SIGNATURE_CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
                full_info = (array_depth > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
                              ? MAKE_FULLINFO(ITEM_Char, 0, 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
                              : MAKE_FULLINFO(ITEM_Integer, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
                result = 'I';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
            case JVM_SIGNATURE_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
                full_info = (array_depth > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
                              ? MAKE_FULLINFO(ITEM_Short, 0, 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
                              : MAKE_FULLINFO(ITEM_Integer, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
                result = 'I';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
            case JVM_SIGNATURE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
                full_info = MAKE_FULLINFO(ITEM_Integer, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
                result = 'I';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
            case JVM_SIGNATURE_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
                full_info = MAKE_FULLINFO(ITEM_Float, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
                result = 'F';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
            case JVM_SIGNATURE_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
                full_info = MAKE_FULLINFO(ITEM_Double, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
                result = 'D';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
            case JVM_SIGNATURE_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
                full_info = MAKE_FULLINFO(ITEM_Long, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
                result = 'L';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
            case JVM_SIGNATURE_ARRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
                array_depth++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
                continue;       /* only time we ever do the loop > 1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
            case JVM_SIGNATURE_CLASS: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
                char buffer_space[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
                char *buffer = buffer_space;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
                char *finish = strchr(p, JVM_SIGNATURE_ENDCLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
                int length = finish - p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
                if (length + 1 > sizeof(buffer_space)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
                    buffer = malloc(length + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
                    check_and_push(context, buffer, VM_MALLOC_BLK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
                memcpy(buffer, p, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
                buffer[length] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
                full_info = make_class_info_from_name(context, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
                result = 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
                p = finish + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
                if (buffer != buffer_space)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
                    pop_and_free(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
        } /* end of switch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
    *signature_p = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
    if (array_depth == 0 || result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
        /* either not an array, or result is bogus */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
        *full_info_p = full_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
        if (array_depth > MAX_ARRAY_DIMENSIONS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
            CCerror(context, "Array with too many dimensions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
        *full_info_p = MAKE_FULLINFO(GET_ITEM_TYPE(full_info),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
                                     array_depth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
                                     GET_EXTRA_INFO(full_info));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
        return 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
/* Given an array type, create the type that has one less level of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
 * indirection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
static fullinfo_type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
decrement_indirection(fullinfo_type array_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
    if (array_info == NULL_FULLINFO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
        return NULL_FULLINFO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
        int type = GET_ITEM_TYPE(array_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
        int indirection = GET_INDIRECTION(array_info) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
        int extra_info = GET_EXTRA_INFO(array_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
        if (   (indirection == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
               && ((type == ITEM_Short || type == ITEM_Byte || type == ITEM_Char)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
            type = ITEM_Integer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
        return MAKE_FULLINFO(type, indirection, extra_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
/* See if we can assign an object of the "from" type to an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
 * of the "to" type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
static jboolean isAssignableTo(context_type *context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
                             fullinfo_type from, fullinfo_type to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
    return (merge_fullinfo_types(context, from, to, JNI_TRUE) == to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
/* Given two fullinfo_type's, find their lowest common denominator.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
 * the assignable_p argument is non-null, we're really just calling to find
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
 * out if "<target> := <value>" is a legitimate assignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
 * We treat all interfaces as if they were of type java/lang/Object, since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
 * runtime will do the full checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
static fullinfo_type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
merge_fullinfo_types(context_type *context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
                     fullinfo_type value, fullinfo_type target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
                     jboolean for_assignment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
    if (value == target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
        /* If they're identical, clearly just return what we've got */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
    /* Both must be either arrays or objects to go further */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
    if (GET_INDIRECTION(value) == 0 && GET_ITEM_TYPE(value) != ITEM_Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
        return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
    if (GET_INDIRECTION(target) == 0 && GET_ITEM_TYPE(target) != ITEM_Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
        return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
    /* If either is NULL, return the other. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
    if (value == NULL_FULLINFO)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
        return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
    else if (target == NULL_FULLINFO)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
    /* If either is java/lang/Object, that's the result. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
    if (target == context->object_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
        return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
    else if (value == context->object_info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
        /* Minor hack.  For assignments, Interface := Object, return Interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
         * rather than Object, so that isAssignableTo() will get the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
         * result.      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
        if (for_assignment && (WITH_ZERO_EXTRA_INFO(target) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
                                  MAKE_FULLINFO(ITEM_Object, 0, 0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
            jclass cb = object_fullinfo_to_classclass(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
                                                      target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
            int is_interface = cb && JVM_IsInterface(env, cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
            if (is_interface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
                return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
    if (GET_INDIRECTION(value) > 0 || GET_INDIRECTION(target) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
        /* At least one is an array.  Neither is java/lang/Object or NULL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
         * Moreover, the types are not identical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
         * The result must either be Object, or an array of some object type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
        fullinfo_type value_base, target_base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
        int dimen_value = GET_INDIRECTION(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
        int dimen_target = GET_INDIRECTION(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
        if (target == context->cloneable_info ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
            target == context->serializable_info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
            return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
        if (value == context->cloneable_info ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
            value == context->serializable_info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
        /* First, if either item's base type isn't ITEM_Object, promote it up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
         * to an object or array of object.  If either is elemental, we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
         * punt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
        if (GET_ITEM_TYPE(value) != ITEM_Object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
            if (dimen_value == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
                return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
            dimen_value--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
            value = MAKE_Object_ARRAY(dimen_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
        if (GET_ITEM_TYPE(target) != ITEM_Object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
            if (dimen_target == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
                return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
            dimen_target--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
            target = MAKE_Object_ARRAY(dimen_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
        /* Both are now objects or arrays of some sort of object type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
        value_base = WITH_ZERO_INDIRECTION(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
        target_base = WITH_ZERO_INDIRECTION(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
        if (dimen_value == dimen_target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
            /* Arrays of the same dimension.  Merge their base types. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
            fullinfo_type  result_base =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
                merge_fullinfo_types(context, value_base, target_base,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
                                            for_assignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
            if (result_base == MAKE_FULLINFO(ITEM_Bogus, 0, 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
                /* bogus in, bogus out */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
                return result_base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
            return MAKE_FULLINFO(ITEM_Object, dimen_value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
                                 GET_EXTRA_INFO(result_base));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
            /* Arrays of different sizes. If the smaller dimension array's base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
             * type is java/lang/Cloneable or java/io/Serializable, return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
             * Otherwise return java/lang/Object with a dimension of the smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
             * of the two */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
            if (dimen_value < dimen_target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
                if (value_base == context->cloneable_info ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
                    value_base == context ->serializable_info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
                    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
                return MAKE_Object_ARRAY(dimen_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
                if (target_base == context->cloneable_info ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
                    target_base == context->serializable_info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
                    return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
                return MAKE_Object_ARRAY(dimen_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
        /* Both are non-array objects. Neither is java/lang/Object or NULL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
        jclass cb_value, cb_target, cb_super_value, cb_super_target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
        fullinfo_type result_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
        /* Let's get the classes corresponding to each of these.  Treat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
         * interfaces as if they were java/lang/Object.  See hack note above. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
        cb_target = object_fullinfo_to_classclass(context, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
        if (cb_target == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
            return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
        if (JVM_IsInterface(env, cb_target))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
            return for_assignment ? target : context->object_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
        cb_value = object_fullinfo_to_classclass(context, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
        if (cb_value == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
            return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
        if (JVM_IsInterface(env, cb_value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
            return context->object_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
        /* If this is for assignment of target := value, we just need to see if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
         * cb_target is a superclass of cb_value.  Save ourselves a lot of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
         * work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
        if (for_assignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
            cb_super_value = (*env)->GetSuperclass(env, cb_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
            while (cb_super_value != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
                jclass tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
                if ((*env)->IsSameObject(env, cb_super_value, cb_target)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
                    (*env)->DeleteLocalRef(env, cb_super_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
                    return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
                tmp_cb =  (*env)->GetSuperclass(env, cb_super_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
                (*env)->DeleteLocalRef(env, cb_super_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
                cb_super_value = tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
            (*env)->DeleteLocalRef(env, cb_super_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
            return context->object_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
        /* Find out whether cb_value or cb_target is deeper in the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
         * tree by moving both toward the root, and seeing who gets there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
         * first.                                                          */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
        cb_super_value = (*env)->GetSuperclass(env, cb_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
        cb_super_target = (*env)->GetSuperclass(env, cb_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
        while((cb_super_value != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
              (cb_super_target != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
            jclass tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
            /* Optimization.  If either hits the other when going up looking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
             * for a parent, then might as well return the parent immediately */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
            if ((*env)->IsSameObject(env, cb_super_value, cb_target)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
                (*env)->DeleteLocalRef(env, cb_super_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
                (*env)->DeleteLocalRef(env, cb_super_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
                return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
            if ((*env)->IsSameObject(env, cb_super_target, cb_value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
                (*env)->DeleteLocalRef(env, cb_super_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
                (*env)->DeleteLocalRef(env, cb_super_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
                return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
            tmp_cb = (*env)->GetSuperclass(env, cb_super_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
            (*env)->DeleteLocalRef(env, cb_super_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
            cb_super_value = tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
            tmp_cb = (*env)->GetSuperclass(env, cb_super_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
            (*env)->DeleteLocalRef(env, cb_super_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
            cb_super_target = tmp_cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
        cb_value = (*env)->NewLocalRef(env, cb_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
        cb_target = (*env)->NewLocalRef(env, cb_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
        /* At most one of the following two while clauses will be executed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
         * Bring the deeper of cb_target and cb_value to the depth of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
         * shallower one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
        while (cb_super_value != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
          /* cb_value is deeper */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
            jclass cb_tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
            cb_tmp = (*env)->GetSuperclass(env, cb_super_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
            (*env)->DeleteLocalRef(env, cb_super_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
            cb_super_value = cb_tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
            cb_tmp = (*env)->GetSuperclass(env, cb_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
            (*env)->DeleteLocalRef(env, cb_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
            cb_value = cb_tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
        while (cb_super_target != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
          /* cb_target is deeper */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
            jclass cb_tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
            cb_tmp = (*env)->GetSuperclass(env, cb_super_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
            (*env)->DeleteLocalRef(env, cb_super_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
            cb_super_target = cb_tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
            cb_tmp = (*env)->GetSuperclass(env, cb_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
            (*env)->DeleteLocalRef(env, cb_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
            cb_target = cb_tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
        /* Walk both up, maintaining equal depth, until a join is found.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
         * know that we will find one.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
        while (!(*env)->IsSameObject(env, cb_value, cb_target)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
            jclass cb_tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
            cb_tmp = (*env)->GetSuperclass(env, cb_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
            (*env)->DeleteLocalRef(env, cb_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
            cb_value = cb_tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
            cb_tmp = (*env)->GetSuperclass(env, cb_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
            (*env)->DeleteLocalRef(env, cb_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
            cb_target = cb_tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
        result_info = make_class_info(context, cb_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
        (*env)->DeleteLocalRef(env, cb_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
        (*env)->DeleteLocalRef(env, cb_super_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
        (*env)->DeleteLocalRef(env, cb_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
        (*env)->DeleteLocalRef(env, cb_super_target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
        return result_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
    } /* both items are classes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
/* Given a fullinfo_type corresponding to an Object, return the jclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
 * of that type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
 * This function always returns a global reference!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
static jclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
object_fullinfo_to_classclass(context_type *context, fullinfo_type classinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
    unsigned short info = GET_EXTRA_INFO(classinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
    return ID_to_class(context, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
static void free_block(void *ptr, int kind)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
    switch (kind) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
    case VM_STRING_UTF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
        JVM_ReleaseUTF(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
    case VM_MALLOC_BLK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
static void check_and_push(context_type *context, const void *ptr, int kind)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
    alloc_stack_type *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
    if (ptr == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4003
        CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4004
    if (context->alloc_stack_top < ALLOC_STACK_SIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
        p = &(context->alloc_stack[context->alloc_stack_top++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
        /* Otherwise we have to malloc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
        p = malloc(sizeof(alloc_stack_type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4009
        if (p == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4010
            /* Make sure we clean up. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4011
            free_block((void *)ptr, kind);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4012
            CCout_of_memory(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
    p->kind = kind;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
    p->ptr = (void *)ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
    p->next = context->allocated_memory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
    context->allocated_memory = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
static void pop_and_free(context_type *context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
    alloc_stack_type *p = context->allocated_memory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
    context->allocated_memory = p->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
    free_block(p->ptr, p->kind);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
    if (p < context->alloc_stack + ALLOC_STACK_SIZE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
        p >= context->alloc_stack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
        context->alloc_stack_top--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
        free(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
static int signature_to_args_size(const char *method_signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
    const char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
    int args_size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
    for (p = method_signature; *p != JVM_SIGNATURE_ENDFUNC; p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
        switch (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4039
          case JVM_SIGNATURE_BOOLEAN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4040
          case JVM_SIGNATURE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
          case JVM_SIGNATURE_CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
          case JVM_SIGNATURE_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
          case JVM_SIGNATURE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
          case JVM_SIGNATURE_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4045
            args_size += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4046
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4047
          case JVM_SIGNATURE_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4048
            args_size += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
            while (*p != JVM_SIGNATURE_ENDCLASS) p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
          case JVM_SIGNATURE_ARRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
            args_size += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
            while ((*p == JVM_SIGNATURE_ARRAY)) p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
            /* If an array of classes, skip over class name, too. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
            if (*p == JVM_SIGNATURE_CLASS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
                while (*p != JVM_SIGNATURE_ENDCLASS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
                  p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
          case JVM_SIGNATURE_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
          case JVM_SIGNATURE_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
            args_size += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
          case JVM_SIGNATURE_FUNC:  /* ignore initial (, if given */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
            /* Indicate an error. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
    return args_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
/* Below are for debugging. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
static void print_fullinfo_type(context_type *, fullinfo_type, jboolean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
print_stack(context_type *context, stack_info_type *stack_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4082
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4083
    stack_item_type *stack = stack_info->stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4084
    if (stack_info->stack_size == UNKNOWN_STACK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
        jio_fprintf(stdout, "x");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
        jio_fprintf(stdout, "(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
        for ( ; stack != 0; stack = stack->next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
            print_fullinfo_type(context, stack->item,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
                (jboolean)(verify_verbose > 1 ? JNI_TRUE : JNI_FALSE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
        jio_fprintf(stdout, ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
print_registers(context_type *context, register_info_type *register_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
    int register_count = register_info->register_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
    if (register_count == UNKNOWN_REGISTER_COUNT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
        jio_fprintf(stdout, "x");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
        fullinfo_type *registers = register_info->registers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
        int mask_count = register_info->mask_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
        mask_type *masks = register_info->masks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
        jio_fprintf(stdout, "{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
        for (i = 0; i < register_count; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
            print_fullinfo_type(context, registers[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
                (jboolean)(verify_verbose > 1 ? JNI_TRUE : JNI_FALSE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4111
        jio_fprintf(stdout, "}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
        for (i = 0; i < mask_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
            char *separator = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
            int *modifies = masks[i].modifies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
            jio_fprintf(stdout, "<%d: ", masks[i].entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
            for (j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
                 j < JVM_GetMethodIxLocalsCount(context->env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4118
                                                context->class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4119
                                                context->method_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
                 j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
                if (IS_BIT_SET(modifies, j)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
                    jio_fprintf(stdout, "%s%d", separator, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
                    separator = ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
            jio_fprintf(stdout, ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
print_flags(context_type *context, flag_type and_flags, flag_type or_flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
    if (and_flags != ((flag_type)-1) || or_flags != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
        jio_fprintf(stdout, "<%x %x>", and_flags, or_flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
print_fullinfo_type(context_type *context, fullinfo_type type, jboolean verbose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
    int indirection = GET_INDIRECTION(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
    for (i = indirection; i-- > 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
        jio_fprintf(stdout, "[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4146
    switch (GET_ITEM_TYPE(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4147
        case ITEM_Integer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
            jio_fprintf(stdout, "I"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
        case ITEM_Float:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4150
            jio_fprintf(stdout, "F"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
        case ITEM_Double:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
            jio_fprintf(stdout, "D"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4153
        case ITEM_Double_2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4154
            jio_fprintf(stdout, "d"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4155
        case ITEM_Long:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
            jio_fprintf(stdout, "L"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
        case ITEM_Long_2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
            jio_fprintf(stdout, "l"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
        case ITEM_ReturnAddress:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
            jio_fprintf(stdout, "a"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
        case ITEM_Object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
            if (!verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
                jio_fprintf(stdout, "A");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
                unsigned short extra = GET_EXTRA_INFO(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
                if (extra == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
                    jio_fprintf(stdout, "/Null/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
                    const char *name = ID_to_class_name(context, extra);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
                    const char *name2 = strrchr(name, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
                    jio_fprintf(stdout, "/%s/", name2 ? name2 + 1 : name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
        case ITEM_Char:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
            jio_fprintf(stdout, "C"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
        case ITEM_Short:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
            jio_fprintf(stdout, "S"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
        case ITEM_Byte:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
            jio_fprintf(stdout, "B"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
        case ITEM_NewObject:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4182
            if (!verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4183
                jio_fprintf(stdout, "@");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4184
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
                int inum = GET_EXTRA_INFO(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
                fullinfo_type real_type =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
                    context->instruction_data[inum].operand2.fi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4188
                jio_fprintf(stdout, ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4189
                print_fullinfo_type(context, real_type, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4190
                jio_fprintf(stdout, "<");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4192
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4193
        case ITEM_InitObject:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4194
            jio_fprintf(stdout, verbose ? ">/this/<" : "@");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
            jio_fprintf(stdout, "?"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4200
    for (i = indirection; i-- > 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4201
        jio_fprintf(stdout, "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4202
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
print_formatted_fieldname(context_type *context, int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4208
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
    jclass cb = context->class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
    const char *classname = JVM_GetCPFieldClassNameUTF(env, cb, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4211
    const char *fieldname = JVM_GetCPFieldNameUTF(env, cb, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
    jio_fprintf(stdout, "  <%s.%s>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4213
                classname ? classname : "", fieldname ? fieldname : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
    JVM_ReleaseUTF(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4215
    JVM_ReleaseUTF(fieldname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4218
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4219
print_formatted_methodname(context_type *context, int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4220
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4221
    JNIEnv *env = context->env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4222
    jclass cb = context->class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4223
    const char *classname = JVM_GetCPMethodClassNameUTF(env, cb, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4224
    const char *methodname = JVM_GetCPMethodNameUTF(env, cb, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4225
    jio_fprintf(stdout, "  <%s.%s>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4226
                classname ? classname : "", methodname ? methodname : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4227
    JVM_ReleaseUTF(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4228
    JVM_ReleaseUTF(methodname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4229
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
#endif /*DEBUG*/