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