]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/java/constants.c
* class.c (build_static_field_ref): Check FIELD_FINAL.
[thirdparty/gcc.git] / gcc / java / constants.c
1 /* Handle the constant pool of the Java(TM) Virtual Machine.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC 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 GNU CC 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 You should have received a copy of the GNU General Public License
16 along with GNU CC; see the file COPYING. If not, write to
17 the Free Software Foundation, 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "jcf.h"
29 #include "tree.h"
30 #include "java-tree.h"
31 #include "toplev.h"
32 #include "ggc.h"
33
34 static void set_constant_entry PARAMS ((CPool *, int, int, jword));
35 static int find_class_or_string_constant PARAMS ((CPool *, int, tree));
36 static int find_name_and_type_constant PARAMS ((CPool *, tree, tree));
37 static tree get_tag_node PARAMS ((int));
38 static tree build_constant_data_ref PARAMS ((void));
39
40 /* Set the INDEX'th constant in CPOOL to have the given TAG and VALUE. */
41
42 static void
43 set_constant_entry (cpool, index, tag, value)
44 CPool *cpool;
45 int index;
46 int tag;
47 jword value;
48 {
49 if (cpool->data == NULL)
50 {
51 cpool->capacity = 100;
52 cpool->tags = xmalloc (sizeof(uint8) * cpool->capacity);
53 cpool->data = xmalloc (sizeof(jword) * cpool->capacity);
54 cpool->count = 1;
55 }
56 if (index >= cpool->capacity)
57 {
58 cpool->capacity *= 2;
59 if (index >= cpool->capacity)
60 cpool->capacity = index + 10;
61 cpool->tags = xrealloc (cpool->tags, sizeof(uint8) * cpool->capacity);
62 cpool->data = xrealloc (cpool->data, sizeof(jword) * cpool->capacity);
63 }
64 if (index >= cpool->count)
65 cpool->count = index + 1;
66 cpool->tags[index] = tag;
67 cpool->data[index] = value;
68 }
69
70 /* Find (or create) a constant pool entry matching TAG and VALUE. */
71
72 int
73 find_constant1 (cpool, tag, value)
74 CPool *cpool;
75 int tag;
76 jword value;
77 {
78 int i;
79 for (i = cpool->count; --i > 0; )
80 {
81 if (cpool->tags[i] == tag && cpool->data[i] == value)
82 return i;
83 }
84 i = cpool->count == 0 ? 1 : cpool->count;
85 set_constant_entry (cpool, i, tag, value);
86 return i;
87 }
88
89 /* Find a double-word constant pool entry matching TAG and WORD1/WORD2. */
90
91 int
92 find_constant2 (cpool, tag, word1, word2)
93 CPool *cpool;
94 int tag;
95 jword word1, word2;
96 {
97 int i;
98 for (i = cpool->count - 1; --i > 0; )
99 {
100 if (cpool->tags[i] == tag
101 && cpool->data[i] == word1
102 && cpool->data[i+1] == word2)
103 return i;
104 }
105 i = cpool->count == 0 ? 1 : cpool->count;
106 set_constant_entry (cpool, i, tag, word1);
107 set_constant_entry (cpool, i+1, 0, word2);
108 return i;
109 }
110
111 int
112 find_utf8_constant (cpool, name)
113 CPool *cpool;
114 tree name;
115 {
116 if (name == NULL_TREE)
117 return 0;
118 return find_constant1 (cpool, CONSTANT_Utf8, (jword) name);
119 }
120
121 static int
122 find_class_or_string_constant (cpool, tag, name)
123 CPool *cpool;
124 int tag;
125 tree name;
126 {
127 int j = find_utf8_constant (cpool, name);
128 int i;
129 for (i = cpool->count; --i > 0; )
130 {
131 if (cpool->tags[i] == tag && cpool->data[i] == (jword) j)
132 return i;
133 }
134 i = cpool->count;
135 set_constant_entry (cpool, i, tag, (jword) j);
136 return i;
137 }
138
139 int
140 find_class_constant (cpool, type)
141 CPool *cpool;
142 tree type;
143 {
144 return find_class_or_string_constant (cpool, CONSTANT_Class,
145 build_internal_class_name (type));
146 }
147
148 /* Allocate a CONSTANT_string entry given a STRING_CST. */
149
150 int
151 find_string_constant (cpool, string)
152 CPool *cpool;
153 tree string;
154 {
155 string = get_identifier (TREE_STRING_POINTER (string));
156 return find_class_or_string_constant (cpool, CONSTANT_String, string);
157
158 }
159
160 /* Find (or create) a CONSTANT_NameAndType matching NAME and TYPE.
161 Return its index in the constant pool CPOOL. */
162
163 static int
164 find_name_and_type_constant (cpool, name, type)
165 CPool *cpool;
166 tree name;
167 tree type;
168 {
169 int name_index = find_utf8_constant (cpool, name);
170 int type_index = find_utf8_constant (cpool, build_java_signature (type));
171 return find_constant1 (cpool, CONSTANT_NameAndType,
172 (name_index << 16) | type_index);
173 }
174
175 /* Find (or create) a CONSTANT_Fieldref for DECL (a FIELD_DECL or VAR_DECL).
176 Return its index in the constant pool CPOOL. */
177
178 int
179 find_fieldref_index (cpool, decl)
180 CPool *cpool;
181 tree decl;
182 {
183 int class_index = find_class_constant (cpool, DECL_CONTEXT (decl));
184 int name_type_index
185 = find_name_and_type_constant (cpool, DECL_NAME (decl), TREE_TYPE (decl));
186 return find_constant1 (cpool, CONSTANT_Fieldref,
187 (class_index << 16) | name_type_index);
188 }
189
190 /* Find (or create) a CONSTANT_Methodref for DECL (a FUNCTION_DECL).
191 Return its index in the constant pool CPOOL. */
192
193 int
194 find_methodref_index (cpool, decl)
195 CPool *cpool;
196 tree decl;
197 {
198 return find_methodref_with_class_index (cpool, decl, DECL_CONTEXT (decl));
199 }
200
201 int
202 find_methodref_with_class_index (cpool, decl, mclass)
203 CPool *cpool;
204 tree decl;
205 tree mclass;
206 {
207 int class_index = find_class_constant (cpool, mclass);
208 tree name = DECL_CONSTRUCTOR_P (decl) ? init_identifier_node
209 : DECL_NAME (decl);
210 int name_type_index;
211 name_type_index =
212 find_name_and_type_constant (cpool, name, TREE_TYPE (decl));
213 return find_constant1 (cpool,
214 CLASS_INTERFACE (TYPE_NAME (mclass))
215 ? CONSTANT_InterfaceMethodref
216 : CONSTANT_Methodref,
217 (class_index << 16) | name_type_index);
218 }
219
220 #define PUT1(X) (*ptr++ = (X))
221 #define PUT2(X) (PUT1((X) >> 8), PUT1(X))
222 #define PUT4(X) (PUT2((X) >> 16), PUT2(X))
223 #define PUTN(P, N) (memcpy(ptr, (P), (N)), ptr += (N))
224
225 /* Give the number of bytes needed in a .class file for the CPOOL
226 constant pool. Includes the 2-byte constant_pool_count. */
227
228 int
229 count_constant_pool_bytes (cpool)
230 CPool *cpool;
231 {
232 int size = 2;
233 int i = 1;
234 for ( ; i < cpool->count; i++)
235 {
236 size++;
237 switch (cpool->tags[i])
238 {
239 case CONSTANT_NameAndType:
240 case CONSTANT_Fieldref:
241 case CONSTANT_Methodref:
242 case CONSTANT_InterfaceMethodref:
243 case CONSTANT_Float:
244 case CONSTANT_Integer:
245 size += 4;
246 break;
247 case CONSTANT_Class:
248 case CONSTANT_String:
249 size += 2;
250 break;
251 case CONSTANT_Long:
252 case CONSTANT_Double:
253 size += 8;
254 i++;
255 break;
256 case CONSTANT_Utf8:
257 {
258 tree t = (tree) cpool->data[i];
259 int len = IDENTIFIER_LENGTH (t);
260 size += len + 2;
261 }
262 break;
263 default:
264 /* Second word of CONSTANT_Long and CONSTANT_Double. */
265 size--;
266 }
267 }
268 return size;
269 }
270
271 /* Write the constant pool CPOOL into BUFFER.
272 The length of BUFFER is LENGTH, which must match the needed length. */
273
274 void
275 write_constant_pool (cpool, buffer, length)
276 CPool *cpool;
277 unsigned char *buffer;
278 int length;
279 {
280 unsigned char *ptr = buffer;
281 int i = 1;
282 jword *datap = &cpool->data[1];
283 PUT2 (cpool->count);
284 for ( ; i < cpool->count; i++, datap++)
285 {
286 int tag = cpool->tags[i];
287 PUT1 (tag);
288 switch (tag)
289 {
290 case CONSTANT_NameAndType:
291 case CONSTANT_Fieldref:
292 case CONSTANT_Methodref:
293 case CONSTANT_InterfaceMethodref:
294 case CONSTANT_Float:
295 case CONSTANT_Integer:
296 PUT4 (*datap);
297 break;
298 case CONSTANT_Class:
299 case CONSTANT_String:
300 PUT2 (*datap);
301 break;
302 break;
303 case CONSTANT_Long:
304 case CONSTANT_Double:
305 PUT4(*datap);
306 i++;
307 datap++;
308 PUT4 (*datap);
309 break;
310 case CONSTANT_Utf8:
311 {
312 tree t = (tree) *datap;
313 int len = IDENTIFIER_LENGTH (t);
314 PUT2 (len);
315 PUTN (IDENTIFIER_POINTER (t), len);
316 }
317 break;
318 }
319 }
320
321 if (ptr != buffer + length)
322 abort ();
323 }
324
325 CPool *outgoing_cpool;
326
327 static GTY(()) tree tag_nodes[13];
328 static tree
329 get_tag_node (tag)
330 int tag;
331 {
332 /* A Cache for build_int_2 (CONSTANT_XXX, 0). */
333
334 if (tag_nodes[tag] == NULL_TREE)
335 tag_nodes[tag] = build_int_2 (tag, 0);
336 return tag_nodes[tag];
337 }
338
339 /* Look for a constant pool entry that matches TAG and NAME.
340 Creates a new entry if not found.
341 TAG is one of CONSTANT_Utf8, CONSTANT_String or CONSTANT_Class.
342 NAME is an IDENTIFIER_NODE naming the Utf8 constant, string, or class.
343 Returns the index of the entry. */
344
345 int
346 alloc_name_constant (tag, name)
347 int tag;
348 tree name;
349 {
350 return find_constant1 (outgoing_cpool, tag, (jword) name);
351 }
352
353 /* Build an identifier for the internal name of reference type TYPE. */
354
355 tree
356 build_internal_class_name (type)
357 tree type;
358 {
359 tree name;
360 if (TYPE_ARRAY_P (type))
361 name = build_java_signature (type);
362 else
363 {
364 name = TYPE_NAME (type);
365 if (TREE_CODE (name) != IDENTIFIER_NODE)
366 name = DECL_NAME (name);
367 name = identifier_subst (name, "", '.', '/', "");
368 }
369 return name;
370 }
371
372 /* Look for a CONSTANT_Class entry for CLAS, creating a new one if needed. */
373
374 int
375 alloc_class_constant (clas)
376 tree clas;
377 {
378 tree class_name = build_internal_class_name (clas);
379
380 return alloc_name_constant (CONSTANT_Class,
381 (unmangle_classname
382 (IDENTIFIER_POINTER(class_name),
383 IDENTIFIER_LENGTH(class_name))));
384 }
385
386 /* Return a reference to the data array of the current constant pool. */
387
388 static tree
389 build_constant_data_ref ()
390 {
391 tree cpool_data_ref = NULL_TREE;
392
393 if (TYPE_CPOOL_DATA_REF (current_class))
394 cpool_data_ref = TYPE_CPOOL_DATA_REF (current_class);
395
396 if (cpool_data_ref == NULL_TREE)
397 {
398 tree decl;
399 tree decl_name = mangled_classname ("_CD_", current_class);
400 decl = build_decl (VAR_DECL, decl_name,
401 build_array_type (ptr_type_node,
402 one_elt_array_domain_type));
403 TREE_STATIC (decl) = 1;
404 make_decl_rtl (decl, NULL);
405 TYPE_CPOOL_DATA_REF (current_class) = cpool_data_ref
406 = build1 (ADDR_EXPR, ptr_type_node, decl);
407 }
408 return cpool_data_ref;
409 }
410
411 /* Get the pointer value at the INDEX'th element of the constant pool. */
412
413 tree
414 build_ref_from_constant_pool (index)
415 int index;
416 {
417 tree t = build_constant_data_ref ();
418 index *= int_size_in_bytes (ptr_type_node);
419 t = fold (build (PLUS_EXPR, ptr_type_node,
420 t, build_int_2 (index, 0)));
421 return build1 (INDIRECT_REF, ptr_type_node, t);
422 }
423
424 /* Build an initializer for the constants field of the current constal pool.
425 Should only be called at top-level, since it may emit declarations. */
426
427 tree
428 build_constants_constructor ()
429 {
430 tree tags_value, data_value;
431 tree cons;
432 tree tags_list = NULL_TREE;
433 tree data_list = NULL_TREE;
434 int i;
435 for (i = outgoing_cpool->count; --i > 0; )
436 {
437 tags_list
438 = tree_cons (NULL_TREE, get_tag_node (outgoing_cpool->tags[i]),
439 tags_list);
440 data_list
441 = tree_cons (NULL_TREE, build_utf8_ref ((tree)outgoing_cpool->data[i]),
442 data_list);
443 }
444 if (outgoing_cpool->count > 0)
445 {
446 tree index_type;
447 tree data_decl, tags_decl, tags_type;
448 tree max_index = build_int_2 (outgoing_cpool->count - 1, 0);
449 TREE_TYPE (max_index) = sizetype;
450 index_type = build_index_type (max_index);
451
452 /* Add dummy 0'th element of constant pool. */
453 tags_list = tree_cons (NULL_TREE, get_tag_node (0), tags_list);
454 data_list = tree_cons (NULL_TREE, null_pointer_node, data_list);
455
456 data_decl = TREE_OPERAND (build_constant_data_ref (), 0);
457 TREE_TYPE (data_decl) = build_array_type (ptr_type_node, index_type),
458 DECL_INITIAL (data_decl) = build (CONSTRUCTOR, TREE_TYPE (data_decl),
459 NULL_TREE, data_list);
460 DECL_SIZE (data_decl) = TYPE_SIZE (TREE_TYPE (data_decl));
461 DECL_SIZE_UNIT (data_decl) = TYPE_SIZE_UNIT (TREE_TYPE (data_decl));
462 rest_of_decl_compilation (data_decl, (char *) 0, 1, 0);
463 data_value = build_address_of (data_decl);
464
465 tags_type = build_array_type (unsigned_byte_type_node, index_type);
466 tags_decl = build_decl (VAR_DECL, mangled_classname ("_CT_",
467 current_class),
468 tags_type);
469 TREE_STATIC (tags_decl) = 1;
470 DECL_INITIAL (tags_decl) = build (CONSTRUCTOR, tags_type,
471 NULL_TREE, tags_list);
472 rest_of_decl_compilation (tags_decl, (char*) 0, 1, 0);
473 tags_value = build_address_of (tags_decl);
474 }
475 else
476 {
477 data_value = null_pointer_node;
478 tags_value = null_pointer_node;
479 }
480 START_RECORD_CONSTRUCTOR (cons, constants_type_node);
481 PUSH_FIELD_VALUE (cons, "size", build_int_2 (outgoing_cpool->count, 0));
482 PUSH_FIELD_VALUE (cons, "tags", tags_value);
483 PUSH_FIELD_VALUE (cons, "data", data_value);
484 FINISH_RECORD_CONSTRUCTOR (cons);
485 return cons;
486 }
487
488 #include "gt-java-constants.h"