1 /* -*- mode: c; tab-width: 4; c-basic-offset: 4 -*- */
2 /****************************** tables.c ***************************************
4 Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
6 See file COPYRIGHT for information on usage and disclaimer of warranties
8 Contains support functions for:
9 - Reading of Java class files
12 - additional support functions
14 Authors: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
15 Changes: Mark Probst EMAIL: cacao@complang.tuwien.ac.at
16 Andreas Krall EMAIL: cacao@complang.tuwien.ac.at
18 Last Change: 1998/03/24
20 *******************************************************************************/
23 #include <sys/types.h>
31 #include "threads/thread.h" /* schani */
32 #include "threads/locks.h"
34 bool runverbose = false;
37 int count_utf_len = 0; /* size of utf hash */
38 int count_utf_new = 0; /* calls of utf_new */
39 int count_utf_new_found = 0; /* calls of utf_new with fast return */
41 hashtable utf_hash; /* hashtable for utf8-symbols */
42 hashtable string_hash; /* hashtable for javastrings */
43 hashtable class_hash; /* hashtable for classes */
45 /******************************************************************************
46 *********************** hashtable functions **********************************
47 ******************************************************************************/
49 /* hashsize must be power of 2 */
51 #define UTF_HASHSTART 16384 /* initial size of utf-hash */
52 #define HASHSTART 2048 /* initial size of javastring and class-hash */
55 /******************** function: init_hashtable ******************************
57 Initializes a hashtable structure and allocates memory.
58 The parameter size specifies the initial size of the hashtable.
60 *****************************************************************************/
62 void init_hashtable(hashtable *hash, u4 size)
68 hash->ptr = MNEW (void*, size);
71 for (i=0; i<size; i++) hash->ptr[i] = NULL;
74 /*********************** function: tables_init *****************************
76 creates hashtables for symboltables
77 (called once at startup)
79 *****************************************************************************/
83 init_hashtable(&utf_hash, UTF_HASHSTART); /* hashtable for utf8-symbols */
84 init_hashtable(&string_hash, HASHSTART); /* hashtable for javastrings */
85 init_hashtable(&class_hash, HASHSTART); /* hashtable for classes */
88 count_utf_len += sizeof(utf*) * utf_hash.size;
93 /********************** function: tables_close ******************************
95 free memory for hashtables
97 *****************************************************************************/
99 void tables_close (stringdeleter del)
105 /* dispose utf symbols */
106 for (i=0; i<utf_hash.size; i++) {
109 /* process elements in external hash chain */
110 utf *nextu = u->hashlink;
111 MFREE (u->text, u1, u->blength);
117 /* dispose javastrings */
118 for (i=0; i<string_hash.size; i++) {
119 s = string_hash.ptr[i];
121 /* process elements in external hash chain */
122 literalstring *nexts = s->hashlink;
124 FREE(s, literalstring);
129 /* dispose hashtable structures */
130 MFREE (utf_hash.ptr, void*, utf_hash.size);
131 MFREE (string_hash.ptr, void*, string_hash.size);
132 MFREE (class_hash.ptr, void*, class_hash.size);
135 /********************* function: utf_display *********************************
137 write utf symbol to stdout (debugging purposes)
139 ******************************************************************************/
141 void utf_display (utf *u)
143 char *endpos = utf_end(u); /* points behind utf string */
144 char *utf_ptr = u->text; /* current position in utf text */
146 while (utf_ptr<endpos) {
148 /* read next unicode character */
149 u2 c = utf_nextu2(&utf_ptr);
150 if (c>=32 && c<=127) printf ("%c",c);
157 /************************ function: utf_sprint *******************************
159 write utf symbol into c-string (debugging purposes)
161 ******************************************************************************/
163 void utf_sprint (char *buffer, utf *u)
165 char *endpos = utf_end(u); /* points behind utf string */
166 char *utf_ptr = u->text; /* current position in utf text */
167 u2 pos = 0; /* position in c-string */
169 while (utf_ptr<endpos)
170 /* copy next unicode character */
171 buffer[pos++] = utf_nextu2(&utf_ptr);
173 /* terminate string */
178 /********************* Funktion: utf_fprint **********************************
180 write utf symbol into file
182 ******************************************************************************/
184 void utf_fprint (FILE *file, utf *u)
186 char *endpos = utf_end(u); /* points behind utf string */
187 char *utf_ptr = u->text; /* current position in utf text */
189 while (utf_ptr<endpos)
190 /* write next unicode character */
191 putc ( utf_nextu2(&utf_ptr), file );
195 /****************** internal function: utf_hashkey ***************************
197 The hashkey is computed from the utf-text by using up to 8 characters.
198 For utf-symbols longer than 15 characters 3 characters are taken from
199 the beginning and the end, 2 characters are taken from the middle.
201 ******************************************************************************/
203 #define nbs(val) ((u4) *(++text) << val) /* get next byte, left shift by val */
204 #define fbs(val) ((u4) *( text) << val) /* get first byte, left shift by val */
206 static u4 utf_hashkey (char *text, u4 length)
208 char *start_pos = text; /* pointer to utf text */
213 case 0: /* empty string */
216 case 1: return fbs(0);
217 case 2: return fbs(0) ^ nbs(3);
218 case 3: return fbs(0) ^ nbs(3) ^ nbs(5);
219 case 4: return fbs(0) ^ nbs(2) ^ nbs(4) ^ nbs(6);
220 case 5: return fbs(0) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(6);
221 case 6: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(5) ^ nbs(6);
222 case 7: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(5) ^ nbs(6);
223 case 8: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(5) ^ nbs(6) ^ nbs(7);
225 case 9: a = fbs(0) ^ nbs(1) ^ nbs(2);
227 return a ^ nbs(4) ^ nbs(5) ^ nbs(6) ^ nbs(7) ^ nbs(8);
231 a^= nbs(2) ^ nbs(3) ^ nbs(4);
233 return a ^ nbs(6) ^ nbs(7) ^ nbs(8) ^ nbs(9);
237 a^= nbs(2) ^ nbs(3) ^ nbs(4);
239 return a ^ nbs(6) ^ nbs(7) ^ nbs(8) ^ nbs(9) ^ nbs(10);
245 a^= nbs(5) ^ nbs(6) ^ nbs(7);
247 return a ^ nbs(9) ^ nbs(10);
249 case 13: a = fbs(0) ^ nbs(1);
255 return a ^ nbs(9) ^ nbs(10);
263 return a ^ nbs(9) ^ nbs(10) ^ nbs(11);
271 return a ^ nbs(9) ^ nbs(10) ^ nbs(11);
273 default: /* 3 characters from beginning */
278 /* 2 characters from middle */
279 text = start_pos + (length / 2);
284 /* 3 characters from end */
285 text = start_pos + length - 4;
290 return a ^ nbs(10) ^ nbs(11);
295 /*************************** function: utf_hashkey ***************************
297 compute the hashkey of a unicode string
299 ******************************************************************************/
301 u4 unicode_hashkey (u2 *text, u2 len)
303 return utf_hashkey((char*) text, len);
306 /************************ function: utf_new **********************************
308 Creates a new utf-symbol, the text of the symbol is passed as a
309 u1-array. The function searches the utf-hashtable for a utf-symbol
310 with this text. On success the element returned, otherwise a new
311 hashtable element is created.
313 If the number of entries in the hashtable exceeds twice the size of the
314 hashtable slots a reorganization of the hashtable is done and the utf
315 symbols are copied to a new hashtable with doubled size.
317 ******************************************************************************/
319 utf *utf_new (char *text, u2 length)
321 u4 key; /* hashkey computed from utf-text */
322 u4 slot; /* slot in hashtable */
323 utf *u; /* hashtable element */
330 key = utf_hashkey (text, length);
331 slot = key & (utf_hash.size-1);
332 u = utf_hash.ptr[slot];
334 /* search external hash chain for utf-symbol */
336 if (u->blength == length) {
338 /* compare text of hashtable elements */
339 for (i=0; i<length; i++)
340 if (text[i] != u->text[i]) goto nomatch;
343 count_utf_new_found++;
345 /* symbol found in hashtable */
349 u = u->hashlink; /* next element in external chain */
353 count_utf_len += sizeof(utf) + length;
356 /* location in hashtable found, create new utf element */
358 u->blength = length; /* length in bytes of utfstring */
359 u->hashlink = utf_hash.ptr[slot]; /* link in external hashchain */
360 u->text = mem_alloc(length); /* allocate memory for utf-text */
361 memcpy(u->text,text,length); /* copy utf-text */
362 utf_hash.ptr[slot] = u; /* insert symbol into table */
364 utf_hash.entries++; /* update number of entries */
366 if ( utf_hash.entries > (utf_hash.size*2)) {
368 /* reorganization of hashtable, average length of
369 the external chains is approx. 2 */
373 hashtable newhash; /* the new hashtable */
375 /* create new hashtable, double the size */
376 init_hashtable(&newhash, utf_hash.size*2);
377 newhash.entries=utf_hash.entries;
380 count_utf_len += sizeof(utf*) * utf_hash.size;
383 /* transfer elements to new hashtable */
384 for (i=0; i<utf_hash.size; i++) {
385 u = (utf*) utf_hash.ptr[i];
387 utf *nextu = u -> hashlink;
388 u4 slot = (utf_hashkey(u->text,u->blength)) & (newhash.size-1);
390 u->hashlink = (utf*) newhash.ptr[slot];
391 newhash.ptr[slot] = u;
393 /* follow link in external hash chain */
398 /* dispose old table */
399 MFREE (utf_hash.ptr, void*, utf_hash.size);
407 /********************* function: utf_new_char ********************************
409 creates a new utf symbol, the text for this symbol is passed
410 as a c-string ( = char* )
412 ******************************************************************************/
414 utf *utf_new_char (char *text)
416 return utf_new(text, strlen(text));
419 /************************** Funktion: utf_show ******************************
421 writes the utf symbols in the utfhash to stdout and
422 displays the number of external hash chains grouped
423 according to the chainlength
426 *****************************************************************************/
431 #define CHAIN_LIMIT 20 /* limit for seperated enumeration */
433 u4 chain_count[CHAIN_LIMIT]; /* numbers of chains */
434 u4 max_chainlength = 0; /* maximum length of the chains */
435 u4 sum_chainlength = 0; /* sum of the chainlengths */
436 u4 beyond_limit = 0; /* number of utf-symbols in chains with length>=CHAIN_LIMIT-1 */
439 printf ("UTF-HASH:\n");
441 /* show element of utf-hashtable */
442 for (i=0; i<utf_hash.size; i++) {
443 utf *u = utf_hash.ptr[i];
445 printf ("SLOT %d: ", (int) i);
457 printf ("UTF-HASH: %d slots for %d entries\n",
458 (int) utf_hash.size, (int) utf_hash.entries );
461 if (utf_hash.entries == 0)
464 printf("chains:\n chainlength number of chains %% of utfstrings\n");
466 for (i=0;i<CHAIN_LIMIT;i++)
469 /* count numbers of hashchains according to their length */
470 for (i=0; i<utf_hash.size; i++) {
472 utf *u = (utf*) utf_hash.ptr[i];
475 /* determine chainlength */
481 /* update sum of all chainlengths */
482 sum_chainlength+=chain_length;
484 /* determine the maximum length of the chains */
485 if (chain_length>max_chainlength)
486 max_chainlength = chain_length;
488 /* update number of utf-symbols in chains with length>=CHAIN_LIMIT-1 */
489 if (chain_length>=CHAIN_LIMIT) {
490 beyond_limit+=chain_length;
491 chain_length=CHAIN_LIMIT-1;
494 /* update number of hashchains of current length */
495 chain_count[chain_length]++;
498 /* display results */
499 for (i=1;i<CHAIN_LIMIT-1;i++)
500 printf(" %2d %17d %18.2f%%\n",i,chain_count[i],(((float) chain_count[i]*i*100)/utf_hash.entries));
502 printf(" >=%2d %17d %18.2f%%\n",CHAIN_LIMIT-1,chain_count[CHAIN_LIMIT-1],((float) beyond_limit*100)/utf_hash.entries);
505 printf("max. chainlength:%5d\n",max_chainlength);
507 /* avg. chainlength = sum of chainlengths / number of chains */
508 printf("avg. chainlength:%5.2f\n",(float) sum_chainlength / (utf_hash.size-chain_count[0]));
511 /******************************************************************************
512 *********************** Misc support functions ********************************
513 ******************************************************************************/
516 /******************** Function: desc_to_type **********************************
518 Determines the corresponding Java base data type for a given type
521 ******************************************************************************/
523 u2 desc_to_type (utf *descriptor)
525 char *utf_ptr = descriptor->text; /* current position in utf text */
527 if (descriptor->blength < 1) panic ("Type-Descriptor is empty string");
529 switch (*utf_ptr++) {
534 case 'Z': return TYPE_INT;
535 case 'D': return TYPE_DOUBLE;
536 case 'F': return TYPE_FLOAT;
537 case 'J': return TYPE_LONG;
539 case '[': return TYPE_ADDRESS;
542 sprintf (logtext, "Invalid Type-Descriptor: ");
543 utf_sprint (logtext+strlen(logtext), descriptor);
549 /********************** Function: desc_typesize *******************************
551 Calculates the lenght in bytes needed for a data element of the type given
552 by its type descriptor.
554 ******************************************************************************/
556 u2 desc_typesize (utf *descriptor)
558 switch (desc_to_type(descriptor)) {
559 case TYPE_INT: return 4;
560 case TYPE_LONG: return 8;
561 case TYPE_FLOAT: return 4;
562 case TYPE_DOUBLE: return 8;
563 case TYPE_ADDRESS: return sizeof(voidptr);
569 /********************** function: utf_nextu2 *********************************
571 read the next unicode character from the utf string and
572 increment the utf-string pointer accordingly
574 ******************************************************************************/
576 u2 utf_nextu2(char **utf_ptr)
578 /* uncompressed unicode character */
580 /* current position in utf text */
581 unsigned char *utf = (unsigned char *) (*utf_ptr);
582 /* bytes representing the unicode character */
583 unsigned char ch1, ch2, ch3;
584 /* number of bytes used to represent the unicode character */
587 switch ((ch1 = utf[0]) >> 4) {
588 default: /* 1 byte */
592 case 0xD: /* 2 bytes */
593 if (((ch2 = utf[1]) & 0xC0) == 0x80) {
594 unsigned char high = ch1 & 0x1F;
595 unsigned char low = ch2 & 0x3F;
596 unicode_char = (high << 6) + low;
601 case 0xE: /* 2 or 3 bytes */
602 if (((ch2 = utf[1]) & 0xC0) == 0x80) {
603 if (((ch3 = utf[2]) & 0xC0) == 0x80) {
604 unsigned char low = ch3 & 0x3f;
605 unsigned char mid = ch2 & 0x3f;
606 unsigned char high = ch1 & 0x0f;
607 unicode_char = (((high << 6) + mid) << 6) + low;
615 /* update position in utf-text */
616 *utf_ptr = (char *) (utf + len);
620 /******************** Function: class_new **************************************
622 searches for the class with the specified name in the classes hashtable,
623 if there is no such class a new classinfo structure is created and inserted
624 into the list of classes to be loaded
626 *******************************************************************************/
628 classinfo *class_new (utf *u)
630 classinfo *c; /* hashtable element */
631 u4 key; /* hashkey computed from classname */
632 u4 slot; /* slot in hashtable */
635 key = utf_hashkey (u->text, u->blength);
636 slot = key & (class_hash.size-1);
637 c = class_hash.ptr[slot];
639 /* search external hash chain for the class */
641 if (c->name->blength == u->blength) {
642 for (i=0; i<u->blength; i++)
643 if (u->text[i] != c->name->text[i]) goto nomatch;
645 /* class found in hashtable */
650 c = c->hashlink; /* next element in external chain */
653 /* location in hashtable found, create new classinfo structure */
656 count_class_infos += sizeof(classinfo);
668 c -> interfacescount = 0;
669 c -> interfaces = NULL;
670 c -> fieldscount = 0;
672 c -> methodscount = 0;
676 c -> instancesize = 0;
677 c -> header.vftbl = NULL;
678 c -> innerclasscount = 0;
679 c -> innerclass = NULL;
681 c -> initialized = false;
682 c -> classvftbl = false;
684 /* prepare loading of the class */
685 list_addlast (&unloadedclasses, c);
687 /* insert class into the hashtable */
688 c->hashlink = class_hash.ptr[slot];
689 class_hash.ptr[slot] = c;
691 /* update number of hashtable-entries */
692 class_hash.entries++;
694 if ( class_hash.entries > (class_hash.size*2)) {
696 /* reorganization of hashtable, average length of
697 the external chains is approx. 2 */
701 hashtable newhash; /* the new hashtable */
703 /* create new hashtable, double the size */
704 init_hashtable(&newhash, class_hash.size*2);
705 newhash.entries = class_hash.entries;
707 /* transfer elements to new hashtable */
708 for (i=0; i<class_hash.size; i++) {
709 c = (classinfo*) class_hash.ptr[i];
711 classinfo *nextc = c -> hashlink;
712 u4 slot = (utf_hashkey(c->name->text,c->name->blength)) & (newhash.size-1);
714 c->hashlink = newhash.ptr[slot];
715 newhash.ptr[slot] = c;
721 /* dispose old table */
722 MFREE (class_hash.ptr, void*, class_hash.size);
723 class_hash = newhash;
729 /******************** Function: class_get **************************************
731 searches for the class with the specified name in the classes hashtable
732 if there is no such class NULL is returned
734 *******************************************************************************/
736 classinfo *class_get (utf *u)
738 classinfo *c; /* hashtable element */
739 u4 key; /* hashkey computed from classname */
740 u4 slot; /* slot in hashtable */
743 key = utf_hashkey (u->text, u->blength);
744 slot = key & (class_hash.size-1);
745 c = class_hash.ptr[slot];
747 /* search external hash-chain */
749 if (c->name->blength == u->blength) {
751 /* compare classnames */
752 for (i=0; i<u->blength; i++)
753 if (u->text[i] != c->name->text[i]) goto nomatch;
755 /* class found in hashtable */
763 /* class not found */
768 /************************** function: utf_strlen ******************************
770 determine number of unicode characters in the utf string
772 *******************************************************************************/
774 u4 utf_strlen(utf *u)
776 char *endpos = utf_end(u); /* points behind utf string */
777 char *utf_ptr = u->text; /* current position in utf text */
778 u4 len = 0; /* number of unicode characters */
780 while (utf_ptr<endpos) {
782 /* next unicode character */
783 utf_nextu2(&utf_ptr);
787 /* string ended abruptly */
788 panic("illegal utf string");