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