1 /* src/vm/class.c - class related functions
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
33 $Id: class.c 2743 2005-06-20 11:54:06Z edwin $
40 #include "vm/global.h"
41 #include "mm/memory.h"
43 #if defined(USE_THREADS)
44 # if defined(NATIVE_THREADS)
45 # include "threads/native/threads.h"
47 # include "threads/green/threads.h"
48 # include "threads/green/locks.h"
52 #include "toolbox/logging.h"
54 #include "vm/classcache.h"
55 #include "vm/exceptions.h"
56 #include "vm/loader.h"
57 #include "vm/options.h"
58 #include "vm/resolve.h"
59 #include "vm/statistics.h"
60 #include "vm/tables.h"
64 /******************************************************************************/
66 /******************************************************************************/
73 #define CLASS_ASSERT(cond) assert(cond)
75 #define CLASS_ASSERT(cond)
79 /* global variables ***********************************************************/
81 list unlinkedclasses; /* this is only used for eager class */
85 /* frequently used classes ****************************************************/
87 /* important system classes */
89 classinfo *class_java_lang_Object = NULL;
90 classinfo *class_java_lang_Class = NULL;
91 classinfo *class_java_lang_ClassLoader = NULL;
92 classinfo *class_java_lang_Cloneable = NULL;
93 classinfo *class_java_lang_SecurityManager = NULL;
94 classinfo *class_java_lang_String = NULL;
95 classinfo *class_java_lang_System = NULL;
96 classinfo *class_java_lang_ThreadGroup = NULL;
97 classinfo *class_java_io_Serializable = NULL;
100 /* system exception classes required in cacao */
102 classinfo *class_java_lang_Throwable = NULL;
103 classinfo *class_java_lang_VMThrowable = NULL;
104 classinfo *class_java_lang_Error = NULL;
105 classinfo *class_java_lang_NoClassDefFoundError = NULL;
106 classinfo *class_java_lang_OutOfMemoryError = NULL;
108 classinfo *class_java_lang_Exception = NULL;
109 classinfo *class_java_lang_ClassNotFoundException = NULL;
111 classinfo *class_java_lang_Void = NULL;
112 classinfo *class_java_lang_Boolean = NULL;
113 classinfo *class_java_lang_Byte = NULL;
114 classinfo *class_java_lang_Character = NULL;
115 classinfo *class_java_lang_Short = NULL;
116 classinfo *class_java_lang_Integer = NULL;
117 classinfo *class_java_lang_Long = NULL;
118 classinfo *class_java_lang_Float = NULL;
119 classinfo *class_java_lang_Double = NULL;
122 /* some classes which may be used more often */
124 classinfo *class_java_lang_StackTraceElement = NULL;
125 classinfo *class_java_lang_reflect_Constructor = NULL;
126 classinfo *class_java_lang_reflect_Field = NULL;
127 classinfo *class_java_lang_reflect_Method = NULL;
128 classinfo *class_java_security_PrivilegedAction = NULL;
129 classinfo *class_java_util_Vector = NULL;
131 classinfo *arrayclass_java_lang_Object = NULL;
134 /* pseudo classes for the typechecker */
136 classinfo *pseudo_class_Arraystub = NULL;
137 classinfo *pseudo_class_Null = NULL;
138 classinfo *pseudo_class_New = NULL;
140 /* class_set_packagename *******************************************************
142 Derive the package name from the class name and store it in the struct.
144 *******************************************************************************/
146 void class_set_packagename(classinfo *c)
148 if (c->name->text[0] == '[') {
149 /* Array classes are not loaded from classfiles. */
150 c->packagename = array_packagename;
153 /* Find the package name */
154 /* Classes in the unnamed package keep packagename == NULL. */
155 char *p = UTF_END(c->name) - 1;
156 char *start = c->name->text;
157 for (;p > start; --p) {
159 c->packagename = utf_new(start, p - start);
166 /* class_create_classinfo ******************************************************
168 Create a new classinfo struct. The class name is set to the given utf *,
169 most other fields are initialized to zero.
171 Note: classname may be NULL. In this case a not-yet-named classinfo is
172 created. The name must be filled in later and class_set_packagename
173 must be called after that.
175 *******************************************************************************/
177 classinfo *class_create_classinfo(utf *classname)
181 #if defined(STATISTICS)
183 count_class_infos += sizeof(classinfo);
186 /* we use a safe name for temporarily unnamed classes */
188 classname = utf_not_named_yet;
191 log_message_utf("Creating class: ", classname);
193 c = GCNEW(classinfo, 1); /*JOWENN: NEW*/
194 /*c=NEW(classinfo);*/
198 c->packagename = NULL;
203 c->extclassrefs = NULL;
204 c->classrefcount = 0;
205 c->parseddescs = NULL;
206 c->parseddescsize = 0;
210 c->interfacescount = 0;
211 c->interfaces = NULL;
220 c->header.vftbl = NULL;
221 c->innerclasscount = 0;
222 c->innerclass = NULL;
224 c->initialized = false;
225 c->initializing = false;
226 c->classvftbl = false;
229 c->classloader = NULL;
230 c->sourcefile = NULL;
232 if (classname != utf_not_named_yet) {
233 class_set_packagename(c);
236 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
237 initObjectLock(&c->header);
243 /* class_freepool **************************************************************
245 Frees all resources used by this classes Constant Pool.
247 *******************************************************************************/
249 static void class_freecpool(classinfo *c)
255 if (c->cptags && c->cpinfos) {
256 for (idx = 0; idx < c->cpcount; idx++) {
257 tag = c->cptags[idx];
258 info = c->cpinfos[idx];
262 case CONSTANT_Fieldref:
263 case CONSTANT_Methodref:
264 case CONSTANT_InterfaceMethodref:
265 FREE(info, constant_FMIref);
267 case CONSTANT_Integer:
268 FREE(info, constant_integer);
271 FREE(info, constant_float);
274 FREE(info, constant_long);
276 case CONSTANT_Double:
277 FREE(info, constant_double);
279 case CONSTANT_NameAndType:
280 FREE(info, constant_nameandtype);
288 MFREE(c->cptags, u1, c->cpcount);
291 MFREE(c->cpinfos, voidptr, c->cpcount);
295 /* class_getconstant ***********************************************************
297 Retrieves the value at position 'pos' of the constantpool of a
298 class. If the type of the value is other than 'ctype', an error is
301 *******************************************************************************/
303 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
305 /* check index and type of constantpool entry */
306 /* (pos == 0 is caught by type comparison) */
308 if (pos >= c->cpcount || c->cptags[pos] != ctype) {
309 *exceptionptr = new_classformaterror(c, "Illegal constant pool index");
313 return c->cpinfos[pos];
317 /* innerclass_getconstant ******************************************************
319 Like class_getconstant, but if cptags is ZERO, null is returned.
321 *******************************************************************************/
323 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
325 /* invalid position in constantpool */
326 if (pos >= c->cpcount) {
327 *exceptionptr = new_classformaterror(c, "Illegal constant pool index");
331 /* constantpool entry of type 0 */
335 /* check type of constantpool entry */
336 if (c->cptags[pos] != ctype) {
337 *exceptionptr = new_classformaterror(c, "Illegal constant pool index");
341 return c->cpinfos[pos];
345 /* class_free ******************************************************************
347 Frees all resources used by the class.
349 *******************************************************************************/
351 void class_free(classinfo *c)
359 MFREE(c->interfaces, classinfo*, c->interfacescount);
362 for (i = 0; i < c->fieldscount; i++)
363 field_free(&(c->fields[i]));
364 /* MFREE(c->fields, fieldinfo, c->fieldscount); */
368 for (i = 0; i < c->methodscount; i++)
369 method_free(&(c->methods[i]));
370 MFREE(c->methods, methodinfo, c->methodscount);
373 if ((v = c->vftbl) != NULL) {
375 mem_free(v->arraydesc,sizeof(arraydescriptor));
377 for (i = 0; i < v->interfacetablelength; i++) {
378 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
380 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
382 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
383 sizeof(methodptr*) * (v->interfacetablelength -
384 (v->interfacetablelength > 0));
385 v = (vftbl_t*) (((methodptr*) v) -
386 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
391 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
393 /* if (c->classvftbl)
394 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
400 /* get_array_class *************************************************************
402 Returns the array class with the given name for the given
403 classloader, or NULL if an exception occurred.
405 Note: This function does eager loading.
407 *******************************************************************************/
409 static classinfo *get_array_class(utf *name,java_objectheader *initloader,
410 java_objectheader *defloader,bool link)
414 /* lookup this class in the classcache */
415 c = classcache_lookup(initloader,name);
417 c = classcache_lookup_defined(defloader,name);
420 /* we have to create it */
421 c = class_create_classinfo(name);
422 c = load_newly_created_array(c,initloader);
428 CLASS_ASSERT(c->loaded);
429 CLASS_ASSERT(c->classloader == defloader);
431 if (link && !c->linked)
435 CLASS_ASSERT(!link || c->linked);
441 /* class_array_of **************************************************************
443 Returns an array class with the given component class. The array
444 class is dynamically created if neccessary.
446 *******************************************************************************/
448 classinfo *class_array_of(classinfo *component, bool link)
453 /* Assemble the array class name */
454 namelen = component->name->blength;
456 if (component->name->text[0] == '[') {
457 /* the component is itself an array */
458 namebuf = DMNEW(char, namelen + 1);
460 MCOPY(namebuf + 1, component->name->text, char, namelen);
464 /* the component is a non-array class */
465 namebuf = DMNEW(char, namelen + 3);
468 MCOPY(namebuf + 2, component->name->text, char, namelen);
469 namebuf[2 + namelen] = ';';
473 return get_array_class(utf_new(namebuf, namelen),
474 component->classloader,
475 component->classloader,
480 /* class_multiarray_of *********************************************************
482 Returns an array class with the given dimension and element class.
483 The array class is dynamically created if neccessary.
485 *******************************************************************************/
487 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
493 log_text("Invalid array dimension requested");
497 /* Assemble the array class name */
498 namelen = element->name->blength;
500 if (element->name->text[0] == '[') {
501 /* the element is itself an array */
502 namebuf = DMNEW(char, namelen + dim);
503 memcpy(namebuf + dim, element->name->text, namelen);
507 /* the element is a non-array class */
508 namebuf = DMNEW(char, namelen + 2 + dim);
510 memcpy(namebuf + dim + 1, element->name->text, namelen);
511 namelen += (2 + dim);
512 namebuf[namelen - 1] = ';';
514 memset(namebuf, '[', dim);
516 return get_array_class(utf_new(namebuf, namelen),
517 element->classloader,
518 element->classloader,
523 /* class_lookup_classref *******************************************************
525 Looks up the constant_classref for a given classname in the classref
529 cls..............the class containing the reference
530 name.............the name of the class refered to
533 a pointer to a constant_classref, or
534 NULL if the reference was not found
536 *******************************************************************************/
538 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
540 constant_classref *ref;
541 extra_classref *xref;
546 CLASS_ASSERT(!cls->classrefcount || cls->classrefs);
548 /* first search the main classref table */
549 count = cls->classrefcount;
550 ref = cls->classrefs;
551 for (; count; --count, ++ref)
552 if (ref->name == name)
555 /* next try the list of extra classrefs */
556 for (xref = cls->extclassrefs; xref; xref = xref->next) {
557 if (xref->classref.name == name)
558 return &(xref->classref);
566 /* class_get_classref **********************************************************
568 Returns the constant_classref for a given classname.
571 cls..............the class containing the reference
572 name.............the name of the class refered to
575 a pointer to a constant_classref (never NULL)
578 The given name is not checked for validity!
580 *******************************************************************************/
582 constant_classref *class_get_classref(classinfo *cls, utf *name)
584 constant_classref *ref;
585 extra_classref *xref;
590 ref = class_lookup_classref(cls,name);
594 xref = NEW(extra_classref);
595 CLASSREF_INIT(xref->classref,cls,name);
597 xref->next = cls->extclassrefs;
598 cls->extclassrefs = xref;
600 return &(xref->classref);
604 /* class_get_self_classref *****************************************************
606 Returns the constant_classref to the class itself.
609 cls..............the class containing the reference
612 a pointer to a constant_classref (never NULL)
614 *******************************************************************************/
616 constant_classref *class_get_self_classref(classinfo *cls)
618 /* XXX this should be done in a faster way. Maybe always make */
619 /* the classref of index 0 a self reference. */
620 return class_get_classref(cls,cls->name);
623 /* class_get_classref_multiarray_of ********************************************
625 Returns an array type reference with the given dimension and element class
629 dim..............the requested dimension
630 dim must be in [1;255]. This is NOT checked!
631 ref..............the component class reference
634 a pointer to the class reference for the array type
637 The referer of `ref` is used as the referer for the new classref.
639 *******************************************************************************/
641 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
647 CLASS_ASSERT(dim >= 1 && dim <= 255);
649 /* Assemble the array class name */
650 namelen = ref->name->blength;
652 if (ref->name->text[0] == '[') {
653 /* the element is itself an array */
654 namebuf = DMNEW(char, namelen + dim);
655 memcpy(namebuf + dim, ref->name->text, namelen);
659 /* the element is a non-array class */
660 namebuf = DMNEW(char, namelen + 2 + dim);
662 memcpy(namebuf + dim + 1, ref->name->text, namelen);
663 namelen += (2 + dim);
664 namebuf[namelen - 1] = ';';
666 memset(namebuf, '[', dim);
668 return class_get_classref(ref->referer,utf_new(namebuf, namelen));
671 /* class_get_classref_component_of *********************************************
673 Returns the component classref of a given array type reference
676 ref..............the array type reference
679 a reference to the component class, or
680 NULL if `ref` is not an object array type reference
683 The referer of `ref` is used as the referer for the new classref.
685 *******************************************************************************/
687 constant_classref *class_get_classref_component_of(constant_classref *ref)
694 name = ref->name->text;
698 namelen = ref->name->blength - 1;
703 else if (*name != '[') {
707 return class_get_classref(ref->referer, utf_new(name, namelen));
712 * These are local overrides for various environment variables in Emacs.
713 * Please do not remove this and leave it at the end of the file, where
714 * Emacs will automagically detect them.
715 * ---------------------------------------------------------------------
718 * indent-tabs-mode: t