* tree.c (make_node): Set TREE_SIDE_EFFECTS for expressions that
[official-gcc.git] / gcc / java / jcf-reader.c
blob44fb37e859bcd85a3d423be6f3e58d363e856c40
1 /* This file read a Java(TM) .class file.
2 It is not stand-alone: It depends on tons of macros, and the
3 intent is you #include this file after you've defined the macros.
5 Copyright (C) 1996 Free Software Foundation, Inc.
7 This program 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.
12 This program 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.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
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. */
26 #include "jcf.h"
27 #include "zipfile.h"
29 static int get_attribute PROTO((JCF *));
30 static int jcf_parse_preamble PROTO((JCF *));
31 static int jcf_parse_constant_pool PROTO((JCF *));
32 static void jcf_parse_class PROTO((JCF *));
33 static int jcf_parse_fields PROTO((JCF *));
34 static int jcf_parse_one_method PROTO((JCF *));
35 static int jcf_parse_methods PROTO((JCF *));
36 static int jcf_parse_final_attributes PROTO((JCF *));
38 static int
39 DEFUN(get_attribute, (jcf),
40 JCF *jcf)
42 uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
43 uint32 attribute_length = JCF_readu4 (jcf);
44 uint32 start_pos = JCF_TELL(jcf);
45 int name_length;
46 const unsigned char *name_data;
47 JCF_FILL (jcf, (long) attribute_length);
48 if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf))
49 return -2;
50 if (JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
51 return -2;
52 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
53 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
55 #ifdef IGNORE_ATTRIBUTE
56 if (IGNORE_ATTRIBUTE (jcf, attribute_name, attribute_length))
58 JCF_SKIP (jcf, attribute_length);
60 else
61 #endif
62 #ifdef HANDLE_SOURCEFILE
63 if (name_length == 10 && memcmp (name_data, "SourceFile", 10) == 0)
65 uint16 sourcefile_index = JCF_readu2 (jcf);
66 HANDLE_SOURCEFILE(sourcefile_index);
68 else
69 #endif
70 #ifdef HANDLE_CONSTANTVALUE
71 if (name_length == 13 && memcmp (name_data, "ConstantValue", 13) == 0)
73 uint16 constantvalue_index = JCF_readu2 (jcf);
74 if (constantvalue_index <= 0 || constantvalue_index >= JPOOL_SIZE(jcf))
75 return -2;
76 HANDLE_CONSTANTVALUE(constantvalue_index);
78 else
79 #endif
80 #ifdef HANDLE_CODE_ATTRIBUTE
81 if (name_length == 4 && memcmp (name_data, "Code", 4) == 0)
83 uint16 j;
84 uint16 max_stack ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
85 uint16 max_locals ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
86 uint32 code_length = JCF_readu4 (jcf);
87 uint16 exception_table_length, attributes_count;
88 if (code_length + 12 > attribute_length)
89 return -1;
90 HANDLE_CODE_ATTRIBUTE(max_stack, max_locals, code_length);
91 JCF_SKIP (jcf, code_length);
92 exception_table_length = JCF_readu2 (jcf);
93 if (code_length + 8 * exception_table_length + 12 > attribute_length)
94 return -1;
95 #ifdef HANDLE_EXCEPTION_TABLE
96 HANDLE_EXCEPTION_TABLE (jcf->read_ptr, exception_table_length);
97 #endif
98 JCF_SKIP (jcf, 2 * 4 * exception_table_length);
99 attributes_count = JCF_readu2 (jcf);
100 for (j = 0; j < attributes_count; j++)
102 int code = get_attribute (jcf);
103 if (code != 0)
104 return code;
107 else
108 #endif /* HANDLE_CODE_ATTRIBUTE */
109 #ifdef HANDLE_EXCEPTIONS_ATTRIBUTE
110 if (name_length == 10 && memcmp (name_data, "Exceptions", 10) == 0)
112 uint16 count = JCF_readu2 (jcf);
113 HANDLE_EXCEPTIONS_ATTRIBUTE (count);
115 else
116 #endif
117 #ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
118 if (name_length == 15 && memcmp (name_data, "LineNumberTable", 15) == 0)
120 uint16 count = JCF_readu2 (jcf);
121 HANDLE_LINENUMBERTABLE_ATTRIBUTE (count);
123 else
124 #endif
125 #ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
126 if (name_length == 18 && memcmp (name_data, "LocalVariableTable", 18) == 0)
128 uint16 count = JCF_readu2 (jcf);
129 HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count);
131 else
132 #endif
134 #ifdef PROCESS_OTHER_ATTRIBUTE
135 PROCESS_OTHER_ATTRIBUTE(jcf, attribute_name, attribute_length);
136 #else
137 JCF_SKIP (jcf, attribute_length);
138 #endif
140 if ((long) (start_pos + attribute_length) != JCF_TELL(jcf))
141 return -1;
142 return 0;
145 /* Read and handle the pre-amble. */
146 static int
147 DEFUN(jcf_parse_preamble, (jcf),
148 JCF* jcf)
150 uint32 magic = (JCF_FILL (jcf, 8), JCF_readu4 (jcf));
151 uint16 minor_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
152 uint16 major_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
153 #ifdef HANDLE_MAGIC
154 HANDLE_MAGIC (magic, minor_version, major_version);
155 #endif
156 if (magic != 0xcafebabe)
157 return -1;
158 else
159 return 0;
162 /* Read and handle the constant pool.
164 Return 0 if OK.
165 Return -2 if a bad cross-reference (index of other constant) was seen.
167 static int
168 DEFUN(jcf_parse_constant_pool, (jcf),
169 JCF* jcf)
171 int i, n;
172 JPOOL_SIZE (jcf) = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
173 jcf->cpool.tags = ALLOC (JPOOL_SIZE (jcf));
174 jcf->cpool.data = ALLOC (sizeof (jword) * JPOOL_SIZE (jcf));
175 jcf->cpool.tags[0] = 0;
176 #ifdef HANDLE_START_CONSTANT_POOL
177 HANDLE_START_CONSTANT_POOL (JPOOL_SIZE (jcf));
178 #endif
179 for (i = 1; i < (int) JPOOL_SIZE (jcf); i++)
181 int constant_kind;
183 /* Make sure at least 9 bytes are available. This is enough
184 for all fixed-sized constant pool entries (so we don't need many
185 more JCF_FILL calls below), but is is small enough that
186 we are guaranteed to not hit EOF (in a valid .class file). */
187 JCF_FILL (jcf, 9);
188 constant_kind = JCF_readu (jcf);
189 jcf->cpool.tags[i] = constant_kind;
190 switch (constant_kind)
192 case CONSTANT_String:
193 case CONSTANT_Class:
194 jcf->cpool.data[i] = JCF_readu2 (jcf);
195 break;
196 case CONSTANT_Fieldref:
197 case CONSTANT_Methodref:
198 case CONSTANT_InterfaceMethodref:
199 case CONSTANT_NameAndType:
200 jcf->cpool.data[i] = JCF_readu2 (jcf);
201 jcf->cpool.data[i] |= JCF_readu2 (jcf) << 16;
202 break;
203 case CONSTANT_Integer:
204 case CONSTANT_Float:
205 jcf->cpool.data[i] = JCF_readu4 (jcf);
206 break;
207 case CONSTANT_Long:
208 case CONSTANT_Double:
209 jcf->cpool.data[i] = JCF_readu4 (jcf);
210 i++; /* These take up two spots in the constant pool */
211 jcf->cpool.tags[i] = 0;
212 jcf->cpool.data[i] = JCF_readu4 (jcf);
213 break;
214 case CONSTANT_Utf8:
215 n = JCF_readu2 (jcf);
216 JCF_FILL (jcf, n);
217 #ifdef HANDLE_CONSTANT_Utf8
218 HANDLE_CONSTANT_Utf8(jcf, i, n);
219 #else
220 jcf->cpool.data[i] = JCF_TELL(jcf) - 2;
221 JCF_SKIP (jcf, n);
222 #endif
223 break;
224 default:
225 return i;
228 return 0;
231 /* Read various class flags and numbers. */
233 static void
234 DEFUN(jcf_parse_class, (jcf),
235 JCF* jcf)
237 int i;
238 uint16 interfaces_count;
239 JCF_FILL (jcf, 8);
240 jcf->access_flags = JCF_readu2 (jcf);
241 jcf->this_class = JCF_readu2 (jcf);
242 jcf->super_class = JCF_readu2 (jcf);
243 interfaces_count = JCF_readu2 (jcf);
245 #ifdef HANDLE_CLASS_INFO
246 HANDLE_CLASS_INFO(jcf->access_flags, jcf->this_class, jcf->super_class, interfaces_count);
247 #endif
249 JCF_FILL (jcf, 2 * interfaces_count);
251 /* Read interfaces. */
252 for (i = 0; i < interfaces_count; i++)
254 uint16 index ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
255 #ifdef HANDLE_CLASS_INTERFACE
256 HANDLE_CLASS_INTERFACE (index);
257 #endif
261 /* Read fields. */
262 static int
263 DEFUN(jcf_parse_fields, (jcf),
264 JCF* jcf)
266 int i, j;
267 uint16 fields_count;
268 JCF_FILL (jcf, 2);
269 fields_count = JCF_readu2 (jcf);
271 #ifdef HANDLE_START_FIELDS
272 HANDLE_START_FIELDS (fields_count);
273 #endif
274 for (i = 0; i < fields_count; i++)
276 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
277 uint16 name_index = JCF_readu2 (jcf);
278 uint16 signature_index = JCF_readu2 (jcf);
279 uint16 attribute_count = JCF_readu2 (jcf);
280 #ifdef HANDLE_START_FIELD
281 HANDLE_START_FIELD (access_flags, name_index, signature_index,
282 attribute_count);
283 #endif
284 for (j = 0; j < attribute_count; j++)
286 int code = get_attribute (jcf);
287 if (code != 0)
288 return code;
290 #ifdef HANDLE_END_FIELD
291 HANDLE_END_FIELD ();
292 #endif
294 #ifdef HANDLE_END_FIELDS
295 HANDLE_END_FIELDS ();
296 #endif
297 return 0;
300 /* Read methods. */
302 static int
303 DEFUN(jcf_parse_one_method, (jcf),
304 JCF* jcf)
306 int i;
307 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
308 uint16 name_index = JCF_readu2 (jcf);
309 uint16 signature_index = JCF_readu2 (jcf);
310 uint16 attribute_count = JCF_readu2 (jcf);
311 #ifdef HANDLE_METHOD
312 HANDLE_METHOD(access_flags, name_index, signature_index, attribute_count);
313 #endif
314 for (i = 0; i < attribute_count; i++)
316 int code = get_attribute (jcf);
317 if (code != 0)
318 return code;
320 #ifdef HANDLE_END_METHOD
321 HANDLE_END_METHOD ();
322 #endif
323 return 0;
326 static int
327 DEFUN(jcf_parse_methods, (jcf),
328 JCF* jcf)
330 int i;
331 uint16 methods_count;
332 JCF_FILL (jcf, 2);
333 methods_count = JCF_readu2 (jcf);
334 #ifdef HANDLE_START_METHODS
335 HANDLE_START_METHODS (methods_count);
336 #endif
337 for (i = 0; i < methods_count; i++)
339 int code = jcf_parse_one_method (jcf);
340 if (code != 0)
341 return code;
343 #ifdef HANDLE_END_METHODS
344 HANDLE_END_METHODS ();
345 #endif
346 return 0;
349 /* Read attributes. */
350 static int
351 DEFUN(jcf_parse_final_attributes, (jcf),
352 JCF *jcf)
354 int i;
355 uint16 attributes_count = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
356 #ifdef START_FINAL_ATTRIBUTES
357 START_FINAL_ATTRIBUTES (attributes_count)
358 #endif
359 for (i = 0; i < attributes_count; i++)
361 int code = get_attribute (jcf);
362 if (code != 0)
363 return code;
365 return 0;