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