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 2523 2005-05-23 18:13:52Z 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;
121 /* some classes which may be used more often */
123 classinfo *class_java_util_Vector = NULL;
124 classinfo *class_java_lang_reflect_Constructor = NULL;
125 classinfo *class_java_lang_reflect_Method = NULL;
126 classinfo *arrayclass_java_lang_Object=NULL;
128 /* pseudo classes for the typechecker */
130 classinfo *pseudo_class_Arraystub = NULL;
131 classinfo *pseudo_class_Null = NULL;
132 classinfo *pseudo_class_New = NULL;
134 /* class_set_packagename *******************************************************
136 Derive the package name from the class name and store it in the struct.
138 *******************************************************************************/
140 void class_set_packagename(classinfo *c)
142 if (c->name->text[0] == '[') {
143 /* Array classes are not loaded from classfiles. */
144 c->packagename = array_packagename;
147 /* Find the package name */
148 /* Classes in the unnamed package keep packagename == NULL. */
149 char *p = utf_end(c->name) - 1;
150 char *start = c->name->text;
151 for (;p > start; --p) {
153 c->packagename = utf_new(start, p - start);
160 /* class_create_classinfo ******************************************************
162 Create a new classinfo struct. The class name is set to the given utf *,
163 most other fields are initialized to zero.
165 Note: classname may be NULL. In this case a not-yet-named classinfo is
166 created. The name must be filled in later and class_set_packagename
167 must be called after that.
169 *******************************************************************************/
171 classinfo *class_create_classinfo(utf *classname)
175 #if defined(STATISTICS)
177 count_class_infos += sizeof(classinfo);
180 /* we use a safe name for temporarily unnamed classes */
182 classname = utf_not_named_yet;
185 log_message_utf("Creating class: ", classname);
187 c = GCNEW(classinfo, 1); /*JOWENN: NEW*/
188 /*c=NEW(classinfo);*/
192 c->packagename = NULL;
197 c->extclassrefs = NULL;
198 c->classrefcount = 0;
199 c->parseddescs = NULL;
200 c->parseddescsize = 0;
204 c->interfacescount = 0;
205 c->interfaces = NULL;
214 c->header.vftbl = NULL;
215 c->innerclasscount = 0;
216 c->innerclass = NULL;
218 c->initialized = false;
219 c->initializing = false;
220 c->classvftbl = false;
223 c->classloader = NULL;
224 c->sourcefile = NULL;
226 if (classname != utf_not_named_yet) {
227 class_set_packagename(c);
230 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
231 initObjectLock(&c->header);
237 /* class_freepool **************************************************************
239 Frees all resources used by this classes Constant Pool.
241 *******************************************************************************/
243 static void class_freecpool(classinfo *c)
249 if (c->cptags && c->cpinfos) {
250 for (idx = 0; idx < c->cpcount; idx++) {
251 tag = c->cptags[idx];
252 info = c->cpinfos[idx];
256 case CONSTANT_Fieldref:
257 case CONSTANT_Methodref:
258 case CONSTANT_InterfaceMethodref:
259 FREE(info, constant_FMIref);
261 case CONSTANT_Integer:
262 FREE(info, constant_integer);
265 FREE(info, constant_float);
268 FREE(info, constant_long);
270 case CONSTANT_Double:
271 FREE(info, constant_double);
273 case CONSTANT_NameAndType:
274 FREE(info, constant_nameandtype);
282 MFREE(c->cptags, u1, c->cpcount);
285 MFREE(c->cpinfos, voidptr, c->cpcount);
289 /* class_getconstant ***********************************************************
291 Retrieves the value at position 'pos' of the constantpool of a
292 class. If the type of the value is other than 'ctype', an error is
295 *******************************************************************************/
297 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
299 /* check index and type of constantpool entry */
300 /* (pos == 0 is caught by type comparison) */
302 if (pos >= c->cpcount || c->cptags[pos] != ctype) {
303 *exceptionptr = new_classformaterror(c, "Illegal constant pool index");
307 return c->cpinfos[pos];
311 /* innerclass_getconstant ******************************************************
313 Like class_getconstant, but if cptags is ZERO, null is returned.
315 *******************************************************************************/
317 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
319 /* invalid position in constantpool */
320 if (pos >= c->cpcount) {
321 *exceptionptr = new_classformaterror(c, "Illegal constant pool index");
325 /* constantpool entry of type 0 */
329 /* check type of constantpool entry */
330 if (c->cptags[pos] != ctype) {
331 *exceptionptr = new_classformaterror(c, "Illegal constant pool index");
335 return c->cpinfos[pos];
339 /* class_free ******************************************************************
341 Frees all resources used by the class.
343 *******************************************************************************/
345 void class_free(classinfo *c)
353 MFREE(c->interfaces, classinfo*, c->interfacescount);
356 for (i = 0; i < c->fieldscount; i++)
357 field_free(&(c->fields[i]));
358 /* MFREE(c->fields, fieldinfo, c->fieldscount); */
362 for (i = 0; i < c->methodscount; i++)
363 method_free(&(c->methods[i]));
364 MFREE(c->methods, methodinfo, c->methodscount);
367 if ((v = c->vftbl) != NULL) {
369 mem_free(v->arraydesc,sizeof(arraydescriptor));
371 for (i = 0; i < v->interfacetablelength; i++) {
372 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
374 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
376 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
377 sizeof(methodptr*) * (v->interfacetablelength -
378 (v->interfacetablelength > 0));
379 v = (vftbl_t*) (((methodptr*) v) -
380 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
385 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
387 /* if (c->classvftbl)
388 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
394 /* get_array_class *************************************************************
396 Returns the array class with the given name for the given
397 classloader, or NULL if an exception occurred.
399 Note: This function does eager loading.
401 *******************************************************************************/
403 static classinfo *get_array_class(utf *name,java_objectheader *initloader,
404 java_objectheader *defloader,bool link)
408 /* lookup this class in the classcache */
409 c = classcache_lookup(initloader,name);
411 c = classcache_lookup_defined(defloader,name);
414 /* we have to create it */
415 c = class_create_classinfo(name);
416 if (!load_newly_created_array(c,initloader))
421 CLASS_ASSERT(c->loaded);
422 CLASS_ASSERT(c->classloader == defloader);
424 if (link && !c->linked)
428 CLASS_ASSERT(!link || c->linked);
434 /* class_array_of **************************************************************
436 Returns an array class with the given component class. The array
437 class is dynamically created if neccessary.
439 *******************************************************************************/
441 classinfo *class_array_of(classinfo *component, bool link)
446 /* Assemble the array class name */
447 namelen = component->name->blength;
449 if (component->name->text[0] == '[') {
450 /* the component is itself an array */
451 namebuf = DMNEW(char, namelen + 1);
453 MCOPY(namebuf + 1, component->name->text, char, namelen);
457 /* the component is a non-array class */
458 namebuf = DMNEW(char, namelen + 3);
461 MCOPY(namebuf + 2, component->name->text, char, namelen);
462 namebuf[2 + namelen] = ';';
466 return get_array_class(utf_new(namebuf, namelen),
467 component->classloader,
468 component->classloader,
473 /* class_multiarray_of *********************************************************
475 Returns an array class with the given dimension and element class.
476 The array class is dynamically created if neccessary.
478 *******************************************************************************/
480 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
486 log_text("Invalid array dimension requested");
490 /* Assemble the array class name */
491 namelen = element->name->blength;
493 if (element->name->text[0] == '[') {
494 /* the element is itself an array */
495 namebuf = DMNEW(char, namelen + dim);
496 memcpy(namebuf + dim, element->name->text, namelen);
500 /* the element is a non-array class */
501 namebuf = DMNEW(char, namelen + 2 + dim);
503 memcpy(namebuf + dim + 1, element->name->text, namelen);
504 namelen += (2 + dim);
505 namebuf[namelen - 1] = ';';
507 memset(namebuf, '[', dim);
509 return get_array_class(utf_new(namebuf, namelen),
510 element->classloader,
511 element->classloader,
516 /* class_lookup_classref *******************************************************
518 Looks up the constant_classref for a given classname in the classref
522 cls..............the class containing the reference
523 name.............the name of the class refered to
526 a pointer to a constant_classref, or
527 NULL if the reference was not found
529 *******************************************************************************/
531 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
533 constant_classref *ref;
534 extra_classref *xref;
539 CLASS_ASSERT(!cls->classrefcount || cls->classrefs);
541 /* first search the main classref table */
542 count = cls->classrefcount;
543 ref = cls->classrefs;
544 for (; count; --count, ++ref)
545 if (ref->name == name)
548 /* next try the list of extra classrefs */
549 for (xref = cls->extclassrefs; xref; xref = xref->next) {
550 if (xref->classref.name == name)
551 return &(xref->classref);
559 /* class_get_classref **********************************************************
561 Returns the constant_classref for a given classname.
564 cls..............the class containing the reference
565 name.............the name of the class refered to
568 a pointer to a constant_classref (never NULL)
571 The given name is not checked for validity!
573 *******************************************************************************/
575 constant_classref *class_get_classref(classinfo *cls, utf *name)
577 constant_classref *ref;
578 extra_classref *xref;
583 ref = class_lookup_classref(cls,name);
587 xref = NEW(extra_classref);
588 CLASSREF_INIT(xref->classref,cls,name);
590 xref->next = cls->extclassrefs;
591 cls->extclassrefs = xref;
593 return &(xref->classref);
597 /* class_get_classref_multiarray_of ********************************************
599 Returns an array type reference with the given dimension and element class
603 dim..............the requested dimension
604 dim must be in [1;255]. This is NOT checked!
605 ref..............the component class reference
608 a pointer to the class reference for the array type
611 The referer of `ref` is used as the referer for the new classref.
613 *******************************************************************************/
615 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
621 CLASS_ASSERT(dim >= 1 && dim <= 255);
623 /* Assemble the array class name */
624 namelen = ref->name->blength;
626 if (ref->name->text[0] == '[') {
627 /* the element is itself an array */
628 namebuf = DMNEW(char, namelen + dim);
629 memcpy(namebuf + dim, ref->name->text, namelen);
633 /* the element is a non-array class */
634 namebuf = DMNEW(char, namelen + 2 + dim);
636 memcpy(namebuf + dim + 1, ref->name->text, namelen);
637 namelen += (2 + dim);
638 namebuf[namelen - 1] = ';';
640 memset(namebuf, '[', dim);
642 return class_get_classref(ref->referer,utf_new(namebuf, namelen));
645 /* class_get_classref_component_of *********************************************
647 Returns the component classref of a given array type reference
650 ref..............the array type reference
653 a reference to the component class, or
654 NULL if `ref` is not an object array type reference
657 The referer of `ref` is used as the referer for the new classref.
659 *******************************************************************************/
661 constant_classref *class_get_classref_component_of(constant_classref *ref)
668 name = ref->name->text;
672 namelen = ref->name->blength - 1;
677 else if (*name != '[') {
681 return class_get_classref(ref->referer, utf_new(name, namelen));
686 * These are local overrides for various environment variables in Emacs.
687 * Please do not remove this and leave it at the end of the file, where
688 * Emacs will automagically detect them.
689 * ---------------------------------------------------------------------
692 * indent-tabs-mode: t