3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
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
32 Contains support functions for:
33 - Reading of Java class files
36 - additional support functions
38 $Id: tables.c 584 2003-11-09 19:15:25Z twisti $
44 #include <sys/types.h>
51 #include "threads/thread.h"
52 #include "threads/locks.h"
53 #include "toolbox/loging.h"
54 #include "toolbox/memory.h"
58 int count_utf_len = 0; /* size of utf hash */
59 int count_utf_new = 0; /* calls of utf_new */
60 int count_utf_new_found = 0; /* calls of utf_new with fast return */
62 hashtable utf_hash; /* hashtable for utf8-symbols */
63 hashtable string_hash; /* hashtable for javastrings */
64 hashtable class_hash; /* hashtable for classes */
66 /******************************************************************************
67 *********************** hashtable functions **********************************
68 ******************************************************************************/
70 /* hashsize must be power of 2 */
72 #define UTF_HASHSTART 16384 /* initial size of utf-hash */
73 #define HASHSTART 2048 /* initial size of javastring and class-hash */
76 /******************** function: init_hashtable ******************************
78 Initializes a hashtable structure and allocates memory.
79 The parameter size specifies the initial size of the hashtable.
81 *****************************************************************************/
83 void init_hashtable(hashtable *hash, u4 size)
89 hash->ptr = MNEW(void*, size);
92 for (i = 0; i < size; i++) hash->ptr[i] = NULL;
95 /*********************** function: tables_init *****************************
97 creates hashtables for symboltables
98 (called once at startup)
100 *****************************************************************************/
104 init_hashtable(&utf_hash, UTF_HASHSTART); /* hashtable for utf8-symbols */
105 init_hashtable(&string_hash, HASHSTART); /* hashtable for javastrings */
106 init_hashtable(&class_hash, HASHSTART); /* hashtable for classes */
109 count_utf_len += sizeof(utf*) * utf_hash.size;
114 /********************** function: tables_close ******************************
116 free memory for hashtables
118 *****************************************************************************/
120 void tables_close (stringdeleter del)
126 /* dispose utf symbols */
127 for (i=0; i<utf_hash.size; i++) {
130 /* process elements in external hash chain */
131 utf *nextu = u->hashlink;
132 MFREE (u->text, u1, u->blength);
138 /* dispose javastrings */
139 for (i=0; i<string_hash.size; i++) {
140 s = string_hash.ptr[i];
142 /* process elements in external hash chain */
143 literalstring *nexts = s->hashlink;
145 FREE(s, literalstring);
150 /* dispose hashtable structures */
151 MFREE (utf_hash.ptr, void*, utf_hash.size);
152 MFREE (string_hash.ptr, void*, string_hash.size);
153 MFREE (class_hash.ptr, void*, class_hash.size);
156 /********************* function: utf_display *********************************
158 write utf symbol to stdout (debugging purposes)
160 ******************************************************************************/
162 void utf_display (utf *u)
164 char *endpos = utf_end(u); /* points behind utf string */
165 char *utf_ptr = u->text; /* current position in utf text */
167 while (utf_ptr<endpos) {
169 /* read next unicode character */
170 u2 c = utf_nextu2(&utf_ptr);
171 if (c>=32 && c<=127) printf ("%c",c);
178 /************************ function: utf_sprint *******************************
180 write utf symbol into c-string (debugging purposes)
182 ******************************************************************************/
184 void utf_sprint (char *buffer, utf *u)
186 char *endpos = utf_end(u); /* points behind utf string */
187 char *utf_ptr = u->text; /* current position in utf text */
188 u2 pos = 0; /* position in c-string */
190 while (utf_ptr<endpos)
191 /* copy next unicode character */
192 buffer[pos++] = utf_nextu2(&utf_ptr);
194 /* terminate string */
199 /********************* Funktion: utf_fprint **********************************
201 write utf symbol into file
203 ******************************************************************************/
205 void utf_fprint (FILE *file, utf *u)
207 char *endpos = utf_end(u); /* points behind utf string */
208 char *utf_ptr = u->text; /* current position in utf text */
210 while (utf_ptr<endpos) {
211 /* read next unicode character */
212 u2 c = utf_nextu2(&utf_ptr);
214 if (c>=32 && c<=127) fprintf (file,"%c",c);
215 else fprintf (file,"?");
220 /****************** internal function: utf_hashkey ***************************
222 The hashkey is computed from the utf-text by using up to 8 characters.
223 For utf-symbols longer than 15 characters 3 characters are taken from
224 the beginning and the end, 2 characters are taken from the middle.
226 ******************************************************************************/
228 #define nbs(val) ((u4) *(++text) << val) /* get next byte, left shift by val */
229 #define fbs(val) ((u4) *( text) << val) /* get first byte, left shift by val */
231 static u4 utf_hashkey (char *text, u4 length)
233 char *start_pos = text; /* pointer to utf text */
238 case 0: /* empty string */
241 case 1: return fbs(0);
242 case 2: return fbs(0) ^ nbs(3);
243 case 3: return fbs(0) ^ nbs(3) ^ nbs(5);
244 case 4: return fbs(0) ^ nbs(2) ^ nbs(4) ^ nbs(6);
245 case 5: return fbs(0) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(6);
246 case 6: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(5) ^ nbs(6);
247 case 7: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(5) ^ nbs(6);
248 case 8: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(5) ^ nbs(6) ^ nbs(7);
250 case 9: a = fbs(0) ^ nbs(1) ^ nbs(2);
252 return a ^ nbs(4) ^ nbs(5) ^ nbs(6) ^ nbs(7) ^ nbs(8);
256 a^= nbs(2) ^ nbs(3) ^ nbs(4);
258 return a ^ nbs(6) ^ nbs(7) ^ nbs(8) ^ nbs(9);
262 a^= nbs(2) ^ nbs(3) ^ nbs(4);
264 return a ^ nbs(6) ^ nbs(7) ^ nbs(8) ^ nbs(9) ^ nbs(10);
270 a^= nbs(5) ^ nbs(6) ^ nbs(7);
272 return a ^ nbs(9) ^ nbs(10);
274 case 13: a = fbs(0) ^ nbs(1);
280 return a ^ nbs(9) ^ nbs(10);
288 return a ^ nbs(9) ^ nbs(10) ^ nbs(11);
296 return a ^ nbs(9) ^ nbs(10) ^ nbs(11);
298 default: /* 3 characters from beginning */
303 /* 2 characters from middle */
304 text = start_pos + (length / 2);
309 /* 3 characters from end */
310 text = start_pos + length - 4;
315 return a ^ nbs(10) ^ nbs(11);
320 /*************************** function: utf_hashkey ***************************
322 compute the hashkey of a unicode string
324 ******************************************************************************/
326 u4 unicode_hashkey (u2 *text, u2 len)
328 return utf_hashkey((char*) text, len);
331 /************************ function: utf_new **********************************
333 Creates a new utf-symbol, the text of the symbol is passed as a
334 u1-array. The function searches the utf-hashtable for a utf-symbol
335 with this text. On success the element returned, otherwise a new
336 hashtable element is created.
338 If the number of entries in the hashtable exceeds twice the size of the
339 hashtable slots a reorganization of the hashtable is done and the utf
340 symbols are copied to a new hashtable with doubled size.
342 ******************************************************************************/
344 utf *utf_new (char *text, u2 length)
346 u4 key; /* hashkey computed from utf-text */
347 u4 slot; /* slot in hashtable */
348 utf *u; /* hashtable element */
355 key = utf_hashkey (text, length);
356 slot = key & (utf_hash.size-1);
357 u = utf_hash.ptr[slot];
359 /* search external hash chain for utf-symbol */
361 if (u->blength == length) {
363 /* compare text of hashtable elements */
364 for (i=0; i<length; i++)
365 if (text[i] != u->text[i]) goto nomatch;
368 count_utf_new_found++;
370 /* symbol found in hashtable */
374 u = u->hashlink; /* next element in external chain */
378 count_utf_len += sizeof(utf) + length;
381 /* location in hashtable found, create new utf element */
383 u->blength = length; /* length in bytes of utfstring */
384 u->hashlink = utf_hash.ptr[slot]; /* link in external hashchain */
385 u->text = mem_alloc(length); /* allocate memory for utf-text */
386 memcpy(u->text,text,length); /* copy utf-text */
387 utf_hash.ptr[slot] = u; /* insert symbol into table */
389 utf_hash.entries++; /* update number of entries */
391 if ( utf_hash.entries > (utf_hash.size*2)) {
393 /* reorganization of hashtable, average length of
394 the external chains is approx. 2 */
398 hashtable newhash; /* the new hashtable */
400 /* create new hashtable, double the size */
401 init_hashtable(&newhash, utf_hash.size*2);
402 newhash.entries=utf_hash.entries;
405 count_utf_len += sizeof(utf*) * utf_hash.size;
408 /* transfer elements to new hashtable */
409 for (i=0; i<utf_hash.size; i++) {
410 u = (utf*) utf_hash.ptr[i];
412 utf *nextu = u -> hashlink;
413 u4 slot = (utf_hashkey(u->text,u->blength)) & (newhash.size-1);
415 u->hashlink = (utf*) newhash.ptr[slot];
416 newhash.ptr[slot] = u;
418 /* follow link in external hash chain */
423 /* dispose old table */
424 MFREE (utf_hash.ptr, void*, utf_hash.size);
432 /********************* function: utf_new_char ********************************
434 creates a new utf symbol, the text for this symbol is passed
435 as a c-string ( = char* )
437 ******************************************************************************/
439 utf *utf_new_char (char *text)
441 return utf_new(text, strlen(text));
444 /************************** Funktion: utf_show ******************************
446 writes the utf symbols in the utfhash to stdout and
447 displays the number of external hash chains grouped
448 according to the chainlength
451 *****************************************************************************/
456 #define CHAIN_LIMIT 20 /* limit for seperated enumeration */
458 u4 chain_count[CHAIN_LIMIT]; /* numbers of chains */
459 u4 max_chainlength = 0; /* maximum length of the chains */
460 u4 sum_chainlength = 0; /* sum of the chainlengths */
461 u4 beyond_limit = 0; /* number of utf-symbols in chains with length>=CHAIN_LIMIT-1 */
464 printf ("UTF-HASH:\n");
466 /* show element of utf-hashtable */
467 for (i=0; i<utf_hash.size; i++) {
468 utf *u = utf_hash.ptr[i];
470 printf ("SLOT %d: ", (int) i);
482 printf ("UTF-HASH: %d slots for %d entries\n",
483 (int) utf_hash.size, (int) utf_hash.entries );
486 if (utf_hash.entries == 0)
489 printf("chains:\n chainlength number of chains %% of utfstrings\n");
491 for (i=0;i<CHAIN_LIMIT;i++)
494 /* count numbers of hashchains according to their length */
495 for (i=0; i<utf_hash.size; i++) {
497 utf *u = (utf*) utf_hash.ptr[i];
500 /* determine chainlength */
506 /* update sum of all chainlengths */
507 sum_chainlength+=chain_length;
509 /* determine the maximum length of the chains */
510 if (chain_length>max_chainlength)
511 max_chainlength = chain_length;
513 /* update number of utf-symbols in chains with length>=CHAIN_LIMIT-1 */
514 if (chain_length>=CHAIN_LIMIT) {
515 beyond_limit+=chain_length;
516 chain_length=CHAIN_LIMIT-1;
519 /* update number of hashchains of current length */
520 chain_count[chain_length]++;
523 /* display results */
524 for (i=1;i<CHAIN_LIMIT-1;i++)
525 printf(" %2d %17d %18.2f%%\n",i,chain_count[i],(((float) chain_count[i]*i*100)/utf_hash.entries));
527 printf(" >=%2d %17d %18.2f%%\n",CHAIN_LIMIT-1,chain_count[CHAIN_LIMIT-1],((float) beyond_limit*100)/utf_hash.entries);
530 printf("max. chainlength:%5d\n",max_chainlength);
532 /* avg. chainlength = sum of chainlengths / number of chains */
533 printf("avg. chainlength:%5.2f\n",(float) sum_chainlength / (utf_hash.size-chain_count[0]));
536 /******************************************************************************
537 *********************** Misc support functions ********************************
538 ******************************************************************************/
541 /******************** Function: desc_to_type **********************************
543 Determines the corresponding Java base data type for a given type
546 ******************************************************************************/
548 u2 desc_to_type(utf *descriptor)
550 char *utf_ptr = descriptor->text; /* current position in utf text */
552 if (descriptor->blength < 1) panic("Type-Descriptor is empty string");
554 switch (*utf_ptr++) {
559 case 'Z': return TYPE_INT;
560 case 'D': return TYPE_DOUBLE;
561 case 'F': return TYPE_FLOAT;
562 case 'J': return TYPE_LONG;
564 case '[': return TYPE_ADDRESS;
567 sprintf(logtext, "Invalid Type-Descriptor: ");
568 utf_sprint(logtext+strlen(logtext), descriptor);
575 /********************** Function: desc_typesize *******************************
577 Calculates the lenght in bytes needed for a data element of the type given
578 by its type descriptor.
580 ******************************************************************************/
582 u2 desc_typesize (utf *descriptor)
584 switch (desc_to_type(descriptor)) {
585 case TYPE_INT: return 4;
586 case TYPE_LONG: return 8;
587 case TYPE_FLOAT: return 4;
588 case TYPE_DOUBLE: return 8;
589 case TYPE_ADDRESS: return sizeof(voidptr);
595 /********************** function: utf_nextu2 *********************************
597 read the next unicode character from the utf string and
598 increment the utf-string pointer accordingly
600 ******************************************************************************/
602 u2 utf_nextu2(char **utf_ptr)
604 /* uncompressed unicode character */
606 /* current position in utf text */
607 unsigned char *utf = (unsigned char *) (*utf_ptr);
608 /* bytes representing the unicode character */
609 unsigned char ch1, ch2, ch3;
610 /* number of bytes used to represent the unicode character */
613 switch ((ch1 = utf[0]) >> 4) {
614 default: /* 1 byte */
618 case 0xD: /* 2 bytes */
619 if (((ch2 = utf[1]) & 0xC0) == 0x80) {
620 unsigned char high = ch1 & 0x1F;
621 unsigned char low = ch2 & 0x3F;
622 unicode_char = (high << 6) + low;
627 case 0xE: /* 2 or 3 bytes */
628 if (((ch2 = utf[1]) & 0xC0) == 0x80) {
629 if (((ch3 = utf[2]) & 0xC0) == 0x80) {
630 unsigned char low = ch3 & 0x3f;
631 unsigned char mid = ch2 & 0x3f;
632 unsigned char high = ch1 & 0x0f;
633 unicode_char = (((high << 6) + mid) << 6) + low;
641 /* update position in utf-text */
642 *utf_ptr = (char *) (utf + len);
647 /******************** Function: class_new **************************************
649 searches for the class with the specified name in the classes hashtable,
650 if there is no such class a new classinfo structure is created and inserted
651 into the list of classes to be loaded
653 *******************************************************************************/
655 classinfo *class_new(utf *u)
657 classinfo *c; /* hashtable element */
658 u4 key; /* hashkey computed from classname */
659 u4 slot; /* slot in hashtable */
662 key = utf_hashkey (u->text, u->blength);
663 slot = key & (class_hash.size-1);
664 c = class_hash.ptr[slot];
666 /* search external hash chain for the class */
668 if (c->name->blength == u->blength) {
669 for (i=0; i<u->blength; i++)
670 if (u->text[i] != c->name->text[i]) goto nomatch;
672 /* class found in hashtable */
677 c = c->hashlink; /* next element in external chain */
680 /* location in hashtable found, create new classinfo structure */
683 count_class_infos += sizeof(classinfo);
695 c->interfacescount = 0;
696 c->interfaces = NULL;
704 c->header.vftbl = NULL;
705 c->innerclasscount = 0;
706 c->innerclass = NULL;
708 c->initialized = false;
709 c->classvftbl = false;
711 /* prepare loading of the class */
712 list_addlast(&unloadedclasses, c);
714 /* insert class into the hashtable */
715 c->hashlink = class_hash.ptr[slot];
716 class_hash.ptr[slot] = c;
718 /* update number of hashtable-entries */
719 class_hash.entries++;
721 if (class_hash.entries > (class_hash.size*2)) {
723 /* reorganization of hashtable, average length of
724 the external chains is approx. 2 */
728 hashtable newhash; /* the new hashtable */
730 /* create new hashtable, double the size */
731 init_hashtable(&newhash, class_hash.size*2);
732 newhash.entries = class_hash.entries;
734 /* transfer elements to new hashtable */
735 for (i = 0; i < class_hash.size; i++) {
736 c = (classinfo*) class_hash.ptr[i];
738 classinfo *nextc = c->hashlink;
739 u4 slot = (utf_hashkey(c->name->text, c->name->blength)) & (newhash.size - 1);
741 c->hashlink = newhash.ptr[slot];
742 newhash.ptr[slot] = c;
748 /* dispose old table */
749 MFREE(class_hash.ptr, void*, class_hash.size);
750 class_hash = newhash;
756 /******************** Function: class_get **************************************
758 searches for the class with the specified name in the classes hashtable
759 if there is no such class NULL is returned
761 *******************************************************************************/
763 classinfo *class_get(utf *u)
765 classinfo *c; /* hashtable element */
766 u4 key; /* hashkey computed from classname */
767 u4 slot; /* slot in hashtable */
770 key = utf_hashkey (u->text, u->blength);
771 slot = key & (class_hash.size-1);
772 c = class_hash.ptr[slot];
774 /* search external hash-chain */
776 if (c->name->blength == u->blength) {
778 /* compare classnames */
779 for (i=0; i<u->blength; i++)
780 if (u->text[i] != c->name->text[i]) goto nomatch;
782 /* class found in hashtable */
790 /* class not found */
795 /************************** function: utf_strlen ******************************
797 determine number of unicode characters in the utf string
799 *******************************************************************************/
801 u4 utf_strlen(utf *u)
803 char *endpos = utf_end(u); /* points behind utf string */
804 char *utf_ptr = u->text; /* current position in utf text */
805 u4 len = 0; /* number of unicode characters */
807 while (utf_ptr<endpos) {
809 /* next unicode character */
810 utf_nextu2(&utf_ptr);
814 /* string ended abruptly */
815 panic("illegal utf string");
822 * These are local overrides for various environment variables in Emacs.
823 * Please do not remove this and leave it at the end of the file, where
824 * Emacs will automagically detect them.
825 * ---------------------------------------------------------------------
828 * indent-tabs-mode: t