OSDN Git Service

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