1*c87b03e5Sespie /* Utility macros to read Java(TM) .class files and byte codes. 2*c87b03e5Sespie 3*c87b03e5Sespie Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 4*c87b03e5Sespie Free Software Foundation, Inc. 5*c87b03e5Sespie 6*c87b03e5Sespie This program is free software; you can redistribute it and/or modify 7*c87b03e5Sespie it under the terms of the GNU General Public License as published by 8*c87b03e5Sespie the Free Software Foundation; either version 2, or (at your option) 9*c87b03e5Sespie any later version. 10*c87b03e5Sespie 11*c87b03e5Sespie This program is distributed in the hope that it will be useful, 12*c87b03e5Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of 13*c87b03e5Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*c87b03e5Sespie GNU General Public License for more details. 15*c87b03e5Sespie 16*c87b03e5Sespie You should have received a copy of the GNU General Public License 17*c87b03e5Sespie along with GNU CC; see the file COPYING. If not, write to 18*c87b03e5Sespie the Free Software Foundation, 59 Temple Place - Suite 330, 19*c87b03e5Sespie Boston, MA 02111-1307, USA. 20*c87b03e5Sespie 21*c87b03e5Sespie Java and all Java-based marks are trademarks or registered trademarks 22*c87b03e5Sespie of Sun Microsystems, Inc. in the United States and other countries. 23*c87b03e5Sespie The Free Software Foundation is independent of Sun Microsystems, Inc. */ 24*c87b03e5Sespie 25*c87b03e5Sespie /* Written by Per Bothner <bothner@cygnus.com>, February 1996. */ 26*c87b03e5Sespie 27*c87b03e5Sespie #ifndef GCC_JCF_H 28*c87b03e5Sespie #define GCC_JCF_H 29*c87b03e5Sespie #include "javaop.h" 30*c87b03e5Sespie #ifndef DEFUN 31*c87b03e5Sespie #if defined (__STDC__) 32*c87b03e5Sespie #define AND , 33*c87b03e5Sespie #define PTR void * 34*c87b03e5Sespie #define DEFUN(name, arglist, args) name(args) 35*c87b03e5Sespie #else 36*c87b03e5Sespie #define PTR char * 37*c87b03e5Sespie #define AND ; 38*c87b03e5Sespie #define DEFUN(name, arglist, args) name arglist args; 39*c87b03e5Sespie #endif 40*c87b03e5Sespie #endif /* !DEFUN */ 41*c87b03e5Sespie 42*c87b03e5Sespie #ifndef PARAMS 43*c87b03e5Sespie #if defined (__STDC__) 44*c87b03e5Sespie #define PARAMS (paramlist) paramlist 45*c87b03e5Sespie #else 46*c87b03e5Sespie #define PARAMS (paramlist) () 47*c87b03e5Sespie #endif 48*c87b03e5Sespie #endif 49*c87b03e5Sespie 50*c87b03e5Sespie #ifndef JCF_u4 51*c87b03e5Sespie #define JCF_u4 unsigned long 52*c87b03e5Sespie #endif 53*c87b03e5Sespie #ifndef JCF_u2 54*c87b03e5Sespie #define JCF_u2 unsigned short 55*c87b03e5Sespie #endif 56*c87b03e5Sespie 57*c87b03e5Sespie #define ALLOC xmalloc 58*c87b03e5Sespie #define REALLOC xrealloc 59*c87b03e5Sespie #ifndef FREE 60*c87b03e5Sespie #define FREE(PTR) free(PTR) 61*c87b03e5Sespie #endif 62*c87b03e5Sespie 63*c87b03e5Sespie #ifdef JCF_word 64*c87b03e5Sespie #define JCF_word JCF_u4 65*c87b03e5Sespie #endif 66*c87b03e5Sespie 67*c87b03e5Sespie /* If we have both "scandir" and "alphasort", we can cache directory 68*c87b03e5Sespie listings to reduce the time taken to search the classpath. */ 69*c87b03e5Sespie #if defined(HAVE_SCANDIR) && defined(HAVE_ALPHASORT) 70*c87b03e5Sespie #define JCF_USE_SCANDIR 1 71*c87b03e5Sespie #else 72*c87b03e5Sespie #define JCF_USE_SCANDIR 0 73*c87b03e5Sespie #endif 74*c87b03e5Sespie 75*c87b03e5Sespie /* On case-insensitive file systems, file name components must be 76*c87b03e5Sespie compared using "strcasecmp", if available, instead of "strcmp". 77*c87b03e5Sespie Assumes "config.h" has already been included. */ 78*c87b03e5Sespie #if defined (HAVE_DOS_BASED_FILE_SYSTEM) && defined (HAVE_STRCASECMP) 79*c87b03e5Sespie #define COMPARE_FILENAMES(X, Y) strcasecmp ((X), (Y)) 80*c87b03e5Sespie #else 81*c87b03e5Sespie #define COMPARE_FILENAMES(X, Y) strcmp ((X), (Y)) 82*c87b03e5Sespie #endif 83*c87b03e5Sespie 84*c87b03e5Sespie /* On case-insensitive file systems, we need to ensure that a request 85*c87b03e5Sespie to open a .java or .class file is honored only if the file to be 86*c87b03e5Sespie opened is of the exact case we are asking for. In other words, we 87*c87b03e5Sespie want to override the inherent case insensitivity of the underlying 88*c87b03e5Sespie file system. On other platforms, this macro becomes the vanilla 89*c87b03e5Sespie open() call. 90*c87b03e5Sespie 91*c87b03e5Sespie If you want to add another host, add your define to the list below 92*c87b03e5Sespie (i.e. defined(WIN32) || defined(YOUR_HOST)) and add an host-specific 93*c87b03e5Sespie .c file to Make-lang.in similar to win32-host.c */ 94*c87b03e5Sespie #if defined(WIN32) 95*c87b03e5Sespie extern int 96*c87b03e5Sespie jcf_open_exact_case (const char* filename, int oflag); 97*c87b03e5Sespie #define JCF_OPEN_EXACT_CASE(X, Y) jcf_open_exact_case (X, Y) 98*c87b03e5Sespie #else 99*c87b03e5Sespie #define JCF_OPEN_EXACT_CASE open 100*c87b03e5Sespie #endif /* WIN32 */ 101*c87b03e5Sespie 102*c87b03e5Sespie struct JCF; 103*c87b03e5Sespie typedef int (*jcf_filbuf_t) PARAMS ((struct JCF*, int needed)); 104*c87b03e5Sespie 105*c87b03e5Sespie typedef struct CPool { 106*c87b03e5Sespie /* Available number of elements in the constants array, before it 107*c87b03e5Sespie must be re-allocated. */ 108*c87b03e5Sespie int capacity; 109*c87b03e5Sespie 110*c87b03e5Sespie /* The constant_pool_count. */ 111*c87b03e5Sespie int count; 112*c87b03e5Sespie 113*c87b03e5Sespie uint8* tags; 114*c87b03e5Sespie 115*c87b03e5Sespie jword* data; 116*c87b03e5Sespie } CPool; 117*c87b03e5Sespie 118*c87b03e5Sespie struct ZipDirectory; 119*c87b03e5Sespie 120*c87b03e5Sespie /* JCF encapsulates the state of reading a Java Class File. */ 121*c87b03e5Sespie 122*c87b03e5Sespie typedef struct JCF { 123*c87b03e5Sespie unsigned char *buffer; 124*c87b03e5Sespie unsigned char *buffer_end; 125*c87b03e5Sespie unsigned char *read_ptr; 126*c87b03e5Sespie unsigned char *read_end; 127*c87b03e5Sespie int java_source : 1; 128*c87b03e5Sespie int right_zip : 1; 129*c87b03e5Sespie int finished : 1; 130*c87b03e5Sespie jcf_filbuf_t filbuf; 131*c87b03e5Sespie void *read_state; 132*c87b03e5Sespie const char *filename; 133*c87b03e5Sespie const char *classname; 134*c87b03e5Sespie struct ZipDirectory *zipd; /* Directory entry where it was found */ 135*c87b03e5Sespie JCF_u2 access_flags, this_class, super_class; 136*c87b03e5Sespie CPool cpool; 137*c87b03e5Sespie } JCF; 138*c87b03e5Sespie /*typedef JCF* JCF_FILE;*/ 139*c87b03e5Sespie 140*c87b03e5Sespie #define JCF_SEEN_IN_ZIP(JCF) ((JCF)->zipd != NULL) 141*c87b03e5Sespie 142*c87b03e5Sespie /* The CPOOL macros take a (pointer to a) CPool. 143*c87b03e5Sespie The JPOOL macros take a (pointer to a) JCF. 144*c87b03e5Sespie Some of the latter should perhaps be deprecated or removed. */ 145*c87b03e5Sespie 146*c87b03e5Sespie #define CPOOL_COUNT(CPOOL) ((CPOOL)->count) 147*c87b03e5Sespie #define JPOOL_SIZE(JCF) CPOOL_COUNT(&(JCF)->cpool) 148*c87b03e5Sespie #define JPOOL_TAG(JCF, INDEX) ((JCF)->cpool.tags[INDEX]) 149*c87b03e5Sespie /* The INDEX'th constant pool entry as a JCF_u4. */ 150*c87b03e5Sespie #define CPOOL_UINT(CPOOL, INDEX) ((CPOOL)->data[INDEX]) 151*c87b03e5Sespie #define JPOOL_UINT(JCF, INDEX) CPOOL_UINT(&(JCF)->cpool, INDEX) /*deprecated*/ 152*c87b03e5Sespie /* The first uint16 of the INDEX'th constant pool entry. */ 153*c87b03e5Sespie #define CPOOL_USHORT1(CPOOL, INDEX) ((CPOOL)->data[INDEX] & 0xFFFF) 154*c87b03e5Sespie #define JPOOL_USHORT1(JCF, INDEX) CPOOL_USHORT1(&(JCF)->cpool, INDEX) 155*c87b03e5Sespie /* The second uint16 of the INDEX'th constant pool entry. */ 156*c87b03e5Sespie #define CPOOL_USHORT2(CPOOL, INDEX) ((CPOOL)->data[INDEX] >> 16) 157*c87b03e5Sespie #define JPOOL_USHORT2(JCF, INDEX) CPOOL_USHORT2(&(JCF)->cpool, INDEX) 158*c87b03e5Sespie #define JPOOL_LONG(JCF, INDEX) \ 159*c87b03e5Sespie WORDS_TO_LONG (JPOOL_UINT(JCF, INDEX), JPOOL_UINT(JCF, (INDEX)+1)) 160*c87b03e5Sespie #define JPOOL_DOUBLE(JCF, INDEX) \ 161*c87b03e5Sespie WORDS_TO_DOUBLE (JPOOL_UINT(JCF, INDEX), JPOOL_UINT(JCF, (INDEX)+1)) 162*c87b03e5Sespie #ifndef JPOOL_UTF_LENGTH 163*c87b03e5Sespie #define JPOOL_UTF_LENGTH(JCF, INDEX) \ 164*c87b03e5Sespie GET_u2 ((JCF)->buffer+JPOOL_UINT(JCF, INDEX)) 165*c87b03e5Sespie #endif 166*c87b03e5Sespie #ifndef JPOOL_UTF_DATA 167*c87b03e5Sespie #define JPOOL_UTF_DATA(JCF, INDEX) \ 168*c87b03e5Sespie ((JCF)->buffer+JPOOL_UINT(JCF, INDEX)+2) 169*c87b03e5Sespie #endif 170*c87b03e5Sespie #define JPOOL_INT(JCF, INDEX) (WORD_TO_INT(JPOOL_UINT (JCF, INDEX))) 171*c87b03e5Sespie #define JPOOL_FLOAT(JCF, INDEX) WORD_TO_FLOAT (JPOOL_UINT (JCF, INDEX)) 172*c87b03e5Sespie 173*c87b03e5Sespie #define CPOOL_INDEX_IN_RANGE(CPOOL, INDEX) \ 174*c87b03e5Sespie ((INDEX) > 0 && (INDEX) < CPOOL_COUNT(CPOOL)) 175*c87b03e5Sespie 176*c87b03e5Sespie #define CPOOL_FINISH(CPOOL) { \ 177*c87b03e5Sespie if ((CPOOL)->tags) FREE ((CPOOL)->tags); \ 178*c87b03e5Sespie if ((CPOOL)->data) FREE ((CPOOL)->data); } 179*c87b03e5Sespie 180*c87b03e5Sespie #define JCF_FINISH(JCF) { \ 181*c87b03e5Sespie CPOOL_FINISH(&(JCF)->cpool); \ 182*c87b03e5Sespie if ((JCF)->buffer) FREE ((JCF)->buffer); \ 183*c87b03e5Sespie if ((JCF)->filename) FREE ((char *) (JCF)->filename); \ 184*c87b03e5Sespie if ((JCF)->classname) FREE ((char *) (JCF)->classname); \ 185*c87b03e5Sespie (JCF)->finished = 1; } 186*c87b03e5Sespie 187*c87b03e5Sespie #define CPOOL_INIT(CPOOL) \ 188*c87b03e5Sespie ((CPOOL)->capacity = 0, (CPOOL)->count = 0, (CPOOL)->tags = 0, (CPOOL)->data = 0) 189*c87b03e5Sespie 190*c87b03e5Sespie #define CPOOL_REINIT(CPOOL) ((CPOOL)->count = 0) 191*c87b03e5Sespie 192*c87b03e5Sespie #define JCF_ZERO(JCF) \ 193*c87b03e5Sespie ((JCF)->buffer = (JCF)->buffer_end = (JCF)->read_ptr = (JCF)->read_end = 0,\ 194*c87b03e5Sespie (JCF)->read_state = 0, (JCF)->filename = (JCF)->classname = 0, \ 195*c87b03e5Sespie CPOOL_INIT(&(JCF)->cpool), (JCF)->java_source = 0, (JCF)->zipd = 0, \ 196*c87b03e5Sespie (JCF)->finished = 0) 197*c87b03e5Sespie 198*c87b03e5Sespie /* Given that PTR points to a 2-byte unsigned integer in network 199*c87b03e5Sespie (big-endian) byte-order, return that integer. */ 200*c87b03e5Sespie #define GET_u2(PTR) (((PTR)[0] << 8) | ((PTR)[1])) 201*c87b03e5Sespie /* Like GET_u2, but for little-endian format. */ 202*c87b03e5Sespie #define GET_u2_le(PTR) (((PTR)[1] << 8) | ((PTR)[0])) 203*c87b03e5Sespie 204*c87b03e5Sespie /* Given that PTR points to a 4-byte unsigned integer in network 205*c87b03e5Sespie (big-endian) byte-order, return that integer. */ 206*c87b03e5Sespie #define GET_u4(PTR) (((JCF_u4)(PTR)[0] << 24) | ((JCF_u4)(PTR)[1] << 16) \ 207*c87b03e5Sespie | ((JCF_u4)(PTR)[2] << 8) | ((JCF_u4)(PTR)[3])) 208*c87b03e5Sespie /* Like GET_u4, but for little-endian order. */ 209*c87b03e5Sespie #define GET_u4_le(PTR) (((JCF_u4)(PTR)[3] << 24) | ((JCF_u4)(PTR)[2] << 16) \ 210*c87b03e5Sespie | ((JCF_u4)(PTR)[1] << 8) | ((JCF_u4)(PTR)[0])) 211*c87b03e5Sespie 212*c87b03e5Sespie /* Make sure there are COUNT bytes readable. */ 213*c87b03e5Sespie #define JCF_FILL(JCF, COUNT) \ 214*c87b03e5Sespie ((JCF)->read_end-(JCF)->read_ptr >= (COUNT) ? 0 : (*(JCF)->filbuf)(JCF, COUNT)) 215*c87b03e5Sespie #define JCF_GETC(JCF) (JCF_FILL(JCF, 1) ? -1 : *(JCF)->read_ptr++) 216*c87b03e5Sespie #define JCF_READ(JCF, BUFFER, N) \ 217*c87b03e5Sespie (memcpy (BUFFER, (JCF)->read_ptr, N), (JCF)->read_ptr += (N)) 218*c87b03e5Sespie #define JCF_SKIP(JCF,N) ((JCF)->read_ptr += (N)) 219*c87b03e5Sespie #define JCF_readu(JCF) (*(JCF)->read_ptr++) 220*c87b03e5Sespie 221*c87b03e5Sespie /* Reads an unsigned 2-byte integer in network (big-endian) byte-order 222*c87b03e5Sespie from JCF. Returns that integer. 223*c87b03e5Sespie Does not check for EOF (make sure to call JCF_FILL before-hand). */ 224*c87b03e5Sespie #define JCF_readu2(JCF) ((JCF)->read_ptr += 2, GET_u2 ((JCF)->read_ptr-2)) 225*c87b03e5Sespie #define JCF_readu2_le(JCF) ((JCF)->read_ptr += 2, GET_u2_le((JCF)->read_ptr-2)) 226*c87b03e5Sespie 227*c87b03e5Sespie /* Like JCF_readu2, but read a 4-byte unsigned integer. */ 228*c87b03e5Sespie #define JCF_readu4(JCF) ((JCF)->read_ptr += 4, GET_u4 ((JCF)->read_ptr-4)) 229*c87b03e5Sespie #define JCF_readu4_le(JCF) ((JCF)->read_ptr += 4, GET_u4_le((JCF)->read_ptr-4)) 230*c87b03e5Sespie 231*c87b03e5Sespie #define JCF_TELL(JCF) ((JCF)->read_ptr - (JCF)->buffer) 232*c87b03e5Sespie #define JCF_SEEK(JCF, POS) ((JCF)->read_ptr = (JCF)->buffer + (POS)) 233*c87b03e5Sespie 234*c87b03e5Sespie #define ACC_PUBLIC 0x0001 235*c87b03e5Sespie #define ACC_PRIVATE 0x0002 236*c87b03e5Sespie #define ACC_PROTECTED 0x0004 237*c87b03e5Sespie #define ACC_STATIC 0x0008 238*c87b03e5Sespie #define ACC_FINAL 0x0010 239*c87b03e5Sespie #define ACC_SYNCHRONIZED 0x0020 240*c87b03e5Sespie #define ACC_SUPER 0x0020 241*c87b03e5Sespie #define ACC_VOLATILE 0x0040 242*c87b03e5Sespie #define ACC_TRANSIENT 0x0080 243*c87b03e5Sespie #define ACC_NATIVE 0x0100 244*c87b03e5Sespie #define ACC_INTERFACE 0x0200 245*c87b03e5Sespie #define ACC_ABSTRACT 0x0400 246*c87b03e5Sespie #define ACC_STRICT 0x0800 247*c87b03e5Sespie 248*c87b03e5Sespie #define ACC_VISIBILITY (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED) 249*c87b03e5Sespie 250*c87b03e5Sespie #define CONSTANT_Class 7 251*c87b03e5Sespie #define CONSTANT_Fieldref 9 252*c87b03e5Sespie #define CONSTANT_Methodref 10 253*c87b03e5Sespie #define CONSTANT_InterfaceMethodref 11 254*c87b03e5Sespie #define CONSTANT_String 8 255*c87b03e5Sespie #define CONSTANT_Integer 3 256*c87b03e5Sespie #define CONSTANT_Float 4 257*c87b03e5Sespie #define CONSTANT_Long 5 258*c87b03e5Sespie #define CONSTANT_Double 6 259*c87b03e5Sespie #define CONSTANT_NameAndType 12 260*c87b03e5Sespie #define CONSTANT_Utf8 1 261*c87b03e5Sespie #define CONSTANT_Unicode 2 262*c87b03e5Sespie 263*c87b03e5Sespie #define DEFAULT_CLASS_PATH "." 264*c87b03e5Sespie 265*c87b03e5Sespie extern const char *find_class PARAMS ((const char *, int, JCF*, int)); 266*c87b03e5Sespie extern const char *find_classfile PARAMS ((char *, JCF*, const char *)); 267*c87b03e5Sespie extern int jcf_filbuf_from_stdio PARAMS ((JCF *jcf, int count)); 268*c87b03e5Sespie extern int jcf_unexpected_eof PARAMS ((JCF*, int)) ATTRIBUTE_NORETURN; 269*c87b03e5Sespie 270*c87b03e5Sespie /* Extract a character from a Java-style Utf8 string. 271*c87b03e5Sespie * PTR points to the current character. 272*c87b03e5Sespie * LIMIT points to the end of the Utf8 string. 273*c87b03e5Sespie * PTR is incremented to point after the character that gets returned. 274*c87b03e5Sespie * On an error, -1 is returned. */ 275*c87b03e5Sespie #define UTF8_GET(PTR, LIMIT) \ 276*c87b03e5Sespie ((PTR) >= (LIMIT) ? -1 \ 277*c87b03e5Sespie : *(PTR) < 128 ? *(PTR)++ \ 278*c87b03e5Sespie : (*(PTR)&0xE0) == 0xC0 && ((PTR)+=2)<=(LIMIT) && ((PTR)[-1]&0xC0) == 0x80 \ 279*c87b03e5Sespie ? (((PTR)[-2] & 0x1F) << 6) + ((PTR)[-1] & 0x3F) \ 280*c87b03e5Sespie : (*(PTR) & 0xF0) == 0xE0 && ((PTR) += 3) <= (LIMIT) \ 281*c87b03e5Sespie && ((PTR)[-2] & 0xC0) == 0x80 && ((PTR)[-1] & 0xC0) == 0x80 \ 282*c87b03e5Sespie ? (((PTR)[-3]&0x0F) << 12) + (((PTR)[-2]&0x3F) << 6) + ((PTR)[-1]&0x3F) \ 283*c87b03e5Sespie : ((PTR)++, -1)) 284*c87b03e5Sespie 285*c87b03e5Sespie extern char *jcf_write_base_directory; 286*c87b03e5Sespie 287*c87b03e5Sespie /* Debug macros, for the front end */ 288*c87b03e5Sespie 289*c87b03e5Sespie extern int quiet_flag; 290*c87b03e5Sespie #ifdef VERBOSE_SKELETON 291*c87b03e5Sespie #undef SOURCE_FRONTEND_DEBUG 292*c87b03e5Sespie #define SOURCE_FRONTEND_DEBUG(X) \ 293*c87b03e5Sespie {if (!quiet_flag) {printf ("* "); printf X; putchar ('\n');} } 294*c87b03e5Sespie #else 295*c87b03e5Sespie #define SOURCE_FRONTEND_DEBUG(X) 296*c87b03e5Sespie #endif 297*c87b03e5Sespie 298*c87b03e5Sespie /* Declarations for dependency code. */ 299*c87b03e5Sespie extern void jcf_dependency_reset PARAMS ((void)); 300*c87b03e5Sespie extern void jcf_dependency_set_target PARAMS ((const char *)); 301*c87b03e5Sespie extern void jcf_dependency_add_target PARAMS ((const char *)); 302*c87b03e5Sespie extern void jcf_dependency_set_dep_file PARAMS ((const char *)); 303*c87b03e5Sespie extern void jcf_dependency_add_file PARAMS ((const char *, int)); 304*c87b03e5Sespie extern void jcf_dependency_write PARAMS ((void)); 305*c87b03e5Sespie extern void jcf_dependency_init PARAMS ((int)); 306*c87b03e5Sespie extern void jcf_dependency_print_dummies PARAMS ((void)); 307*c87b03e5Sespie 308*c87b03e5Sespie /* Declarations for path handling code. */ 309*c87b03e5Sespie extern void jcf_path_init PARAMS ((void)); 310*c87b03e5Sespie extern void jcf_path_classpath_arg PARAMS ((const char *)); 311*c87b03e5Sespie extern void jcf_path_bootclasspath_arg PARAMS ((const char *)); 312*c87b03e5Sespie extern void jcf_path_extdirs_arg PARAMS ((const char *)); 313*c87b03e5Sespie extern void jcf_path_include_arg PARAMS ((const char *)); 314*c87b03e5Sespie extern void jcf_path_seal PARAMS ((int)); 315*c87b03e5Sespie extern void *jcf_path_start PARAMS ((void)); 316*c87b03e5Sespie extern void *jcf_path_next PARAMS ((void *)); 317*c87b03e5Sespie extern char *jcf_path_name PARAMS ((void *)); 318*c87b03e5Sespie extern int jcf_path_is_zipfile PARAMS ((void *)); 319*c87b03e5Sespie extern int jcf_path_is_system PARAMS ((void *)); 320*c87b03e5Sespie extern int jcf_path_max_len PARAMS ((void)); 321*c87b03e5Sespie 322*c87b03e5Sespie #endif /* ! GCC_JCF_H */ 323