Use 'a' operand code for prefetch instruction.
[official-gcc.git] / gcc / java / jcf-reader.c
blob5df7a2e6353e3eebbf60887383492a5bacf8005e
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, 1997, 1998, 1999, 2000 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 PARAMS ((JCF *));
30 static int jcf_parse_preamble PARAMS ((JCF *));
31 static int jcf_parse_constant_pool PARAMS ((JCF *));
32 static void jcf_parse_class PARAMS ((JCF *));
33 static int jcf_parse_fields PARAMS ((JCF *));
34 static int jcf_parse_one_method PARAMS ((JCF *));
35 static int jcf_parse_methods PARAMS ((JCF *));
36 static int jcf_parse_final_attributes PARAMS ((JCF *));
37 #ifdef NEED_PEEK_ATTRIBUTE
38 static int peek_attribute PARAMS ((JCF *, int, const char *, int));
39 #endif
40 #ifdef NEED_SKIP_ATTRIBUTE
41 static void skip_attribute PARAMS ((JCF *, int));
42 #endif
44 /* Go through all available attribute (ATTRIBUTE_NUMER) and try to
45 identify PEEKED_NAME. Return 1 if PEEKED_NAME was found, 0
46 otherwise. JCF is restored to its initial position before
47 returning. */
49 #ifdef NEED_PEEK_ATTRIBUTE /* Not everyone uses this function */
50 static int
51 peek_attribute (jcf, attribute_number, peeked_name, peeked_name_length)
52 JCF *jcf;
53 int attribute_number;
54 const char *peeked_name;
55 int peeked_name_length;
57 int to_return = 0;
58 long absolute_offset = (long)JCF_TELL (jcf);
59 int i;
61 for (i = 0; !to_return && i < attribute_number; i++)
63 uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
64 uint32 attribute_length = JCF_readu4 (jcf);
65 int name_length;
66 const unsigned char *name_data;
68 JCF_FILL (jcf, (long) attribute_length);
69 if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf)
70 || JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
71 continue;
73 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
74 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
76 if (name_length == peeked_name_length
77 && ! memcmp (name_data, peeked_name, peeked_name_length))
79 to_return = 1;
80 break;
83 JCF_SKIP (jcf, attribute_length);
86 JCF_SEEK (jcf, absolute_offset);
87 return to_return;
89 #endif
91 #ifdef NEED_SKIP_ATTRIBUTE /* Not everyone uses this function */
92 static void
93 skip_attribute (jcf, number_of_attribute)
94 JCF *jcf;
95 int number_of_attribute;
97 while (number_of_attribute--)
99 JCF_FILL (jcf, 6);
100 (void) JCF_readu2 (jcf);
101 JCF_SKIP (jcf, JCF_readu4 (jcf));
104 #endif
106 static int
107 DEFUN(get_attribute, (jcf),
108 JCF *jcf)
110 uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
111 uint32 attribute_length = JCF_readu4 (jcf);
112 uint32 start_pos = JCF_TELL(jcf);
113 int name_length;
114 const unsigned char *name_data;
115 JCF_FILL (jcf, (long) attribute_length);
116 if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf))
117 return -2;
118 if (JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
119 return -2;
120 name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
121 name_data = JPOOL_UTF_DATA (jcf, attribute_name);
123 #define MATCH_ATTRIBUTE(S) \
124 (name_length == sizeof (S)-1 && memcmp (name_data, S, sizeof (S)-1) == 0)
126 #ifdef IGNORE_ATTRIBUTE
127 if (IGNORE_ATTRIBUTE (jcf, attribute_name, attribute_length))
129 JCF_SKIP (jcf, attribute_length);
131 else
132 #endif
133 #ifdef HANDLE_SOURCEFILE
134 if (MATCH_ATTRIBUTE ("SourceFile"))
136 uint16 sourcefile_index = JCF_readu2 (jcf);
137 HANDLE_SOURCEFILE(sourcefile_index);
139 else
140 #endif
141 #ifdef HANDLE_CONSTANTVALUE
142 if (MATCH_ATTRIBUTE ("ConstantValue"))
144 uint16 constantvalue_index = JCF_readu2 (jcf);
145 if (constantvalue_index <= 0 || constantvalue_index >= JPOOL_SIZE(jcf))
146 return -2;
147 HANDLE_CONSTANTVALUE(constantvalue_index);
149 else
150 #endif
151 #ifdef HANDLE_CODE_ATTRIBUTE
152 if (MATCH_ATTRIBUTE ("Code"))
154 uint16 j;
155 uint16 max_stack ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
156 uint16 max_locals ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
157 uint32 code_length = JCF_readu4 (jcf);
158 uint16 exception_table_length, attributes_count;
159 if (code_length + 12 > attribute_length)
160 return -1;
161 HANDLE_CODE_ATTRIBUTE(max_stack, max_locals, code_length);
162 JCF_SKIP (jcf, code_length);
163 exception_table_length = JCF_readu2 (jcf);
164 if (code_length + 8 * exception_table_length + 12 > attribute_length)
165 return -1;
166 #ifdef HANDLE_EXCEPTION_TABLE
167 HANDLE_EXCEPTION_TABLE (jcf->read_ptr, exception_table_length);
168 #endif
169 JCF_SKIP (jcf, 2 * 4 * exception_table_length);
170 attributes_count = JCF_readu2 (jcf);
171 for (j = 0; j < attributes_count; j++)
173 int code = get_attribute (jcf);
174 if (code != 0)
175 return code;
178 else
179 #endif /* HANDLE_CODE_ATTRIBUTE */
180 #ifdef HANDLE_EXCEPTIONS_ATTRIBUTE
181 if (MATCH_ATTRIBUTE ("Exceptions"))
183 uint16 count = JCF_readu2 (jcf);
184 HANDLE_EXCEPTIONS_ATTRIBUTE (count);
186 else
187 #endif
188 #ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
189 if (MATCH_ATTRIBUTE ("LineNumberTable"))
191 uint16 count = JCF_readu2 (jcf);
192 HANDLE_LINENUMBERTABLE_ATTRIBUTE (count);
194 else
195 #endif
196 #ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
197 if (MATCH_ATTRIBUTE ("LocalVariableTable"))
199 uint16 count = JCF_readu2 (jcf);
200 HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count);
202 else
203 #endif
204 #ifdef HANDLE_INNERCLASSES_ATTRIBUTE
205 if (MATCH_ATTRIBUTE ("InnerClasses"))
207 uint16 count = JCF_readu2 (jcf);
208 HANDLE_INNERCLASSES_ATTRIBUTE (count);
210 else
211 #endif
212 #ifdef HANDLE_SYNTHETIC_ATTRIBUTE
213 if (MATCH_ATTRIBUTE ("Synthetic"))
215 HANDLE_SYNTHETIC_ATTRIBUTE ();
217 else
218 #endif
219 #ifdef HANDLE_GCJCOMPILED_ATTRIBUTE
220 if (MATCH_ATTRIBUTE ("gnu.gcj.gcj-compiled"))
222 HANDLE_GCJCOMPILED_ATTRIBUTE ();
224 else
225 #endif
227 #ifdef PROCESS_OTHER_ATTRIBUTE
228 PROCESS_OTHER_ATTRIBUTE(jcf, attribute_name, attribute_length);
229 #else
230 JCF_SKIP (jcf, attribute_length);
231 #endif
233 if ((long) (start_pos + attribute_length) != JCF_TELL(jcf))
234 return -1;
235 return 0;
238 /* Read and handle the pre-amble. */
239 static int
240 DEFUN(jcf_parse_preamble, (jcf),
241 JCF* jcf)
243 uint32 magic = (JCF_FILL (jcf, 8), JCF_readu4 (jcf));
244 uint16 minor_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
245 uint16 major_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
246 #ifdef HANDLE_MAGIC
247 HANDLE_MAGIC (magic, minor_version, major_version);
248 #endif
249 if (magic != 0xcafebabe)
250 return -1;
251 else
252 return 0;
255 /* Read and handle the constant pool.
257 Return 0 if OK.
258 Return -2 if a bad cross-reference (index of other constant) was seen.
260 static int
261 DEFUN(jcf_parse_constant_pool, (jcf),
262 JCF* jcf)
264 int i, n;
265 JPOOL_SIZE (jcf) = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
266 jcf->cpool.tags = ALLOC (JPOOL_SIZE (jcf));
267 jcf->cpool.data = ALLOC (sizeof (jword) * JPOOL_SIZE (jcf));
268 jcf->cpool.tags[0] = 0;
269 #ifdef HANDLE_START_CONSTANT_POOL
270 HANDLE_START_CONSTANT_POOL (JPOOL_SIZE (jcf));
271 #endif
272 for (i = 1; i < (int) JPOOL_SIZE (jcf); i++)
274 int constant_kind;
276 /* Make sure at least 9 bytes are available. This is enough
277 for all fixed-sized constant pool entries (so we don't need many
278 more JCF_FILL calls below), but is is small enough that
279 we are guaranteed to not hit EOF (in a valid .class file). */
280 JCF_FILL (jcf, 9);
281 constant_kind = JCF_readu (jcf);
282 jcf->cpool.tags[i] = constant_kind;
283 switch (constant_kind)
285 case CONSTANT_String:
286 case CONSTANT_Class:
287 jcf->cpool.data[i] = JCF_readu2 (jcf);
288 break;
289 case CONSTANT_Fieldref:
290 case CONSTANT_Methodref:
291 case CONSTANT_InterfaceMethodref:
292 case CONSTANT_NameAndType:
293 jcf->cpool.data[i] = JCF_readu2 (jcf);
294 jcf->cpool.data[i] |= JCF_readu2 (jcf) << 16;
295 break;
296 case CONSTANT_Integer:
297 case CONSTANT_Float:
298 jcf->cpool.data[i] = JCF_readu4 (jcf);
299 break;
300 case CONSTANT_Long:
301 case CONSTANT_Double:
302 jcf->cpool.data[i] = JCF_readu4 (jcf);
303 i++; /* These take up two spots in the constant pool */
304 jcf->cpool.tags[i] = 0;
305 jcf->cpool.data[i] = JCF_readu4 (jcf);
306 break;
307 case CONSTANT_Utf8:
308 n = JCF_readu2 (jcf);
309 JCF_FILL (jcf, n);
310 #ifdef HANDLE_CONSTANT_Utf8
311 HANDLE_CONSTANT_Utf8(jcf, i, n);
312 #else
313 jcf->cpool.data[i] = JCF_TELL(jcf) - 2;
314 JCF_SKIP (jcf, n);
315 #endif
316 break;
317 default:
318 return i;
321 return 0;
324 /* Read various class flags and numbers. */
326 static void
327 DEFUN(jcf_parse_class, (jcf),
328 JCF* jcf)
330 int i;
331 uint16 interfaces_count;
332 JCF_FILL (jcf, 8);
333 jcf->access_flags = JCF_readu2 (jcf);
334 jcf->this_class = JCF_readu2 (jcf);
335 jcf->super_class = JCF_readu2 (jcf);
336 interfaces_count = JCF_readu2 (jcf);
338 #ifdef HANDLE_CLASS_INFO
339 HANDLE_CLASS_INFO(jcf->access_flags, jcf->this_class, jcf->super_class, interfaces_count);
340 #endif
342 JCF_FILL (jcf, 2 * interfaces_count);
344 /* Read interfaces. */
345 for (i = 0; i < interfaces_count; i++)
347 uint16 index ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
348 #ifdef HANDLE_CLASS_INTERFACE
349 HANDLE_CLASS_INTERFACE (index);
350 #endif
354 /* Read fields. */
355 static int
356 DEFUN(jcf_parse_fields, (jcf),
357 JCF* jcf)
359 int i, j;
360 uint16 fields_count;
361 JCF_FILL (jcf, 2);
362 fields_count = JCF_readu2 (jcf);
364 #ifdef HANDLE_START_FIELDS
365 HANDLE_START_FIELDS (fields_count);
366 #endif
367 for (i = 0; i < fields_count; i++)
369 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
370 uint16 name_index = JCF_readu2 (jcf);
371 uint16 signature_index = JCF_readu2 (jcf);
372 uint16 attribute_count = JCF_readu2 (jcf);
373 #ifdef HANDLE_START_FIELD
374 HANDLE_START_FIELD (access_flags, name_index, signature_index,
375 attribute_count);
376 #endif
377 for (j = 0; j < attribute_count; j++)
379 int code = get_attribute (jcf);
380 if (code != 0)
381 return code;
383 #ifdef HANDLE_END_FIELD
384 HANDLE_END_FIELD ();
385 #endif
387 #ifdef HANDLE_END_FIELDS
388 HANDLE_END_FIELDS ();
389 #endif
390 return 0;
393 /* Read methods. */
395 static int
396 DEFUN(jcf_parse_one_method, (jcf),
397 JCF* jcf)
399 int i;
400 uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
401 uint16 name_index = JCF_readu2 (jcf);
402 uint16 signature_index = JCF_readu2 (jcf);
403 uint16 attribute_count = JCF_readu2 (jcf);
404 #ifdef HANDLE_METHOD
405 HANDLE_METHOD(access_flags, name_index, signature_index, attribute_count);
406 #endif
407 for (i = 0; i < attribute_count; i++)
409 int code = get_attribute (jcf);
410 if (code != 0)
411 return code;
413 #ifdef HANDLE_END_METHOD
414 HANDLE_END_METHOD ();
415 #endif
416 return 0;
419 static int
420 DEFUN(jcf_parse_methods, (jcf),
421 JCF* jcf)
423 int i;
424 uint16 methods_count;
425 JCF_FILL (jcf, 2);
426 methods_count = JCF_readu2 (jcf);
427 #ifdef HANDLE_START_METHODS
428 HANDLE_START_METHODS (methods_count);
429 #endif
430 for (i = 0; i < methods_count; i++)
432 int code = jcf_parse_one_method (jcf);
433 if (code != 0)
434 return code;
436 #ifdef HANDLE_END_METHODS
437 HANDLE_END_METHODS ();
438 #endif
439 return 0;
442 /* Read attributes. */
443 static int
444 DEFUN(jcf_parse_final_attributes, (jcf),
445 JCF *jcf)
447 int i;
448 uint16 attributes_count = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
449 #ifdef START_FINAL_ATTRIBUTES
450 START_FINAL_ATTRIBUTES (attributes_count)
451 #endif
452 for (i = 0; i < attributes_count; i++)
454 int code = get_attribute (jcf);
455 if (code != 0)
456 return code;
458 return 0;