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 664 2003-11-21 18:24:01Z jowenn $
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 */
351 /* log_text("utf_new entered");*/
356 key = utf_hashkey (text, length);
357 slot = key & (utf_hash.size-1);
358 u = utf_hash.ptr[slot];
360 /* search external hash chain for utf-symbol */
362 if (u->blength == length) {
364 /* compare text of hashtable elements */
365 for (i=0; i<length; i++)
366 if (text[i] != u->text[i]) goto nomatch;
369 count_utf_new_found++;
371 /* log_text("symbol found in hash table");*/
372 /* symbol found in hashtable */
383 u = u->hashlink; /* next element in external chain */
387 count_utf_len += sizeof(utf) + length;
390 /* location in hashtable found, create new utf element */
392 u->blength = length; /* length in bytes of utfstring */
393 u->hashlink = utf_hash.ptr[slot]; /* link in external hashchain */
394 u->text = mem_alloc(length/*JOWENN*/+1); /* allocate memory for utf-text */
395 memcpy(u->text,text,length); /* copy utf-text */
396 u->text[length]='\0';/*JOWENN*/
397 utf_hash.ptr[slot] = u; /* insert symbol into table */
399 utf_hash.entries++; /* update number of entries */
401 if ( utf_hash.entries > (utf_hash.size*2)) {
403 /* reorganization of hashtable, average length of
404 the external chains is approx. 2 */
408 hashtable newhash; /* the new hashtable */
410 /* create new hashtable, double the size */
411 init_hashtable(&newhash, utf_hash.size*2);
412 newhash.entries=utf_hash.entries;
415 count_utf_len += sizeof(utf*) * utf_hash.size;
418 /* transfer elements to new hashtable */
419 for (i=0; i<utf_hash.size; i++) {
420 u = (utf*) utf_hash.ptr[i];
422 utf *nextu = u -> hashlink;
423 u4 slot = (utf_hashkey(u->text,u->blength)) & (newhash.size-1);
425 u->hashlink = (utf*) newhash.ptr[slot];
426 newhash.ptr[slot] = u;
428 /* follow link in external hash chain */
433 /* dispose old table */
434 MFREE (utf_hash.ptr, void*, utf_hash.size);
442 /********************* function: utf_new_char ********************************
444 creates a new utf symbol, the text for this symbol is passed
445 as a c-string ( = char* )
447 ******************************************************************************/
449 utf *utf_new_char (char *text)
451 return utf_new(text, strlen(text));
455 /********************* function: utf_new_char ********************************
457 creates a new utf symbol, the text for this symbol is passed
458 as a c-string ( = char* )
459 "." characters are going to be replaced by "/". since the above function is
460 used often, this is a separte function, instead of an if
462 ******************************************************************************/
464 utf *utf_new_char_classname (char *text)
466 if (strchr(text,'.')) {
467 char *txt=strdup(text);
468 char *end=txt+strlen(txt);
471 for (c=txt;c<end;c++)
473 tmpRes=utf_new(txt,strlen(txt));
478 return utf_new(text, strlen(text));
481 /************************** Funktion: utf_show ******************************
483 writes the utf symbols in the utfhash to stdout and
484 displays the number of external hash chains grouped
485 according to the chainlength
488 *****************************************************************************/
493 #define CHAIN_LIMIT 20 /* limit for seperated enumeration */
495 u4 chain_count[CHAIN_LIMIT]; /* numbers of chains */
496 u4 max_chainlength = 0; /* maximum length of the chains */
497 u4 sum_chainlength = 0; /* sum of the chainlengths */
498 u4 beyond_limit = 0; /* number of utf-symbols in chains with length>=CHAIN_LIMIT-1 */
501 printf ("UTF-HASH:\n");
503 /* show element of utf-hashtable */
504 for (i=0; i<utf_hash.size; i++) {
505 utf *u = utf_hash.ptr[i];
507 printf ("SLOT %d: ", (int) i);
519 printf ("UTF-HASH: %d slots for %d entries\n",
520 (int) utf_hash.size, (int) utf_hash.entries );
523 if (utf_hash.entries == 0)
526 printf("chains:\n chainlength number of chains %% of utfstrings\n");
528 for (i=0;i<CHAIN_LIMIT;i++)
531 /* count numbers of hashchains according to their length */
532 for (i=0; i<utf_hash.size; i++) {
534 utf *u = (utf*) utf_hash.ptr[i];
537 /* determine chainlength */
543 /* update sum of all chainlengths */
544 sum_chainlength+=chain_length;
546 /* determine the maximum length of the chains */
547 if (chain_length>max_chainlength)
548 max_chainlength = chain_length;
550 /* update number of utf-symbols in chains with length>=CHAIN_LIMIT-1 */
551 if (chain_length>=CHAIN_LIMIT) {
552 beyond_limit+=chain_length;
553 chain_length=CHAIN_LIMIT-1;
556 /* update number of hashchains of current length */
557 chain_count[chain_length]++;
560 /* display results */
561 for (i=1;i<CHAIN_LIMIT-1;i++)
562 printf(" %2d %17d %18.2f%%\n",i,chain_count[i],(((float) chain_count[i]*i*100)/utf_hash.entries));
564 printf(" >=%2d %17d %18.2f%%\n",CHAIN_LIMIT-1,chain_count[CHAIN_LIMIT-1],((float) beyond_limit*100)/utf_hash.entries);
567 printf("max. chainlength:%5d\n",max_chainlength);
569 /* avg. chainlength = sum of chainlengths / number of chains */
570 printf("avg. chainlength:%5.2f\n",(float) sum_chainlength / (utf_hash.size-chain_count[0]));
573 /******************************************************************************
574 *********************** Misc support functions ********************************
575 ******************************************************************************/
578 /******************** Function: desc_to_type **********************************
580 Determines the corresponding Java base data type for a given type
583 ******************************************************************************/
585 u2 desc_to_type(utf *descriptor)
587 char *utf_ptr = descriptor->text; /* current position in utf text */
589 if (descriptor->blength < 1) panic("Type-Descriptor is empty string");
591 switch (*utf_ptr++) {
596 case 'Z': return TYPE_INT;
597 case 'D': return TYPE_DOUBLE;
598 case 'F': return TYPE_FLOAT;
599 case 'J': return TYPE_LONG;
601 case '[': return TYPE_ADDRESS;
604 sprintf(logtext, "Invalid Type-Descriptor: ");
605 utf_sprint(logtext+strlen(logtext), descriptor);
612 /********************** Function: desc_typesize *******************************
614 Calculates the lenght in bytes needed for a data element of the type given
615 by its type descriptor.
617 ******************************************************************************/
619 u2 desc_typesize (utf *descriptor)
621 switch (desc_to_type(descriptor)) {
622 case TYPE_INT: return 4;
623 case TYPE_LONG: return 8;
624 case TYPE_FLOAT: return 4;
625 case TYPE_DOUBLE: return 8;
626 case TYPE_ADDRESS: return sizeof(voidptr);
632 /********************** function: utf_nextu2 *********************************
634 read the next unicode character from the utf string and
635 increment the utf-string pointer accordingly
637 ******************************************************************************/
639 u2 utf_nextu2(char **utf_ptr)
641 /* uncompressed unicode character */
643 /* current position in utf text */
644 unsigned char *utf = (unsigned char *) (*utf_ptr);
645 /* bytes representing the unicode character */
646 unsigned char ch1, ch2, ch3;
647 /* number of bytes used to represent the unicode character */
650 switch ((ch1 = utf[0]) >> 4) {
651 default: /* 1 byte */
655 case 0xD: /* 2 bytes */
656 if (((ch2 = utf[1]) & 0xC0) == 0x80) {
657 unsigned char high = ch1 & 0x1F;
658 unsigned char low = ch2 & 0x3F;
659 unicode_char = (high << 6) + low;
664 case 0xE: /* 2 or 3 bytes */
665 if (((ch2 = utf[1]) & 0xC0) == 0x80) {
666 if (((ch3 = utf[2]) & 0xC0) == 0x80) {
667 unsigned char low = ch3 & 0x3f;
668 unsigned char mid = ch2 & 0x3f;
669 unsigned char high = ch1 & 0x0f;
670 unicode_char = (((high << 6) + mid) << 6) + low;
678 /* update position in utf-text */
679 *utf_ptr = (char *) (utf + len);
684 /******************** Function: class_new **************************************
686 searches for the class with the specified name in the classes hashtable,
687 if there is no such class a new classinfo structure is created and inserted
688 into the list of classes to be loaded
690 *******************************************************************************/
692 classinfo *class_new(utf *u)
694 classinfo *c; /* hashtable element */
695 u4 key; /* hashkey computed from classname */
696 u4 slot; /* slot in hashtable */
699 key = utf_hashkey (u->text, u->blength);
700 slot = key & (class_hash.size-1);
701 c = class_hash.ptr[slot];
703 /* search external hash chain for the class */
705 if (c->name->blength == u->blength) {
706 for (i=0; i<u->blength; i++)
707 if (u->text[i] != c->name->text[i]) goto nomatch;
709 /* class found in hashtable */
714 c = c->hashlink; /* next element in external chain */
717 /* location in hashtable found, create new classinfo structure */
720 count_class_infos += sizeof(classinfo);
733 c -> interfacescount = 0;
734 c -> interfaces = NULL;
735 c -> fieldscount = 0;
737 c -> methodscount = 0;
741 c -> instancesize = 0;
742 c -> header.vftbl = NULL;
743 c -> innerclasscount = 0;
744 c -> innerclass = NULL;
746 c -> initialized = false;
747 c -> classvftbl = false;
751 /* prepare loading of the class */
752 list_addlast(&unloadedclasses, c);
754 /* insert class into the hashtable */
755 c->hashlink = class_hash.ptr[slot];
756 class_hash.ptr[slot] = c;
758 /* update number of hashtable-entries */
759 class_hash.entries++;
761 if (class_hash.entries > (class_hash.size*2)) {
763 /* reorganization of hashtable, average length of
764 the external chains is approx. 2 */
768 hashtable newhash; /* the new hashtable */
770 /* create new hashtable, double the size */
771 init_hashtable(&newhash, class_hash.size*2);
772 newhash.entries = class_hash.entries;
774 /* transfer elements to new hashtable */
775 for (i = 0; i < class_hash.size; i++) {
776 c = (classinfo*) class_hash.ptr[i];
778 classinfo *nextc = c->hashlink;
779 u4 slot = (utf_hashkey(c->name->text, c->name->blength)) & (newhash.size - 1);
781 c->hashlink = newhash.ptr[slot];
782 newhash.ptr[slot] = c;
788 /* dispose old table */
789 MFREE(class_hash.ptr, void*, class_hash.size);
790 class_hash = newhash;
793 /* Array classes need further initialization. */
794 if (u->text[0] == '[')
800 /******************** Function: class_get **************************************
802 searches for the class with the specified name in the classes hashtable
803 if there is no such class NULL is returned
805 *******************************************************************************/
807 classinfo *class_get(utf *u)
809 classinfo *c; /* hashtable element */
810 u4 key; /* hashkey computed from classname */
811 u4 slot; /* slot in hashtable */
814 key = utf_hashkey (u->text, u->blength);
815 slot = key & (class_hash.size-1);
816 c = class_hash.ptr[slot];
818 /* search external hash-chain */
820 if (c->name->blength == u->blength) {
822 /* compare classnames */
823 for (i=0; i<u->blength; i++)
824 if (u->text[i] != c->name->text[i]) goto nomatch;
826 /* class found in hashtable */
834 /* class not found */
838 /***************** Function: class_array_of ***********************************
840 Returns an array class with the given component class.
841 The array class is dynamically created if neccessary.
843 *******************************************************************************/
845 classinfo *class_array_of(classinfo *component)
850 /* Assemble the array class name */
851 namelen = component->name->blength;
853 if (component->name->text[0] == '[') {
854 /* the component is itself an array */
855 namebuf = DMNEW(char,namelen+1);
857 memcpy(namebuf+1,component->name->text,namelen);
861 /* the component is a non-array class */
862 namebuf = DMNEW(char,namelen+3);
865 memcpy(namebuf+2,component->name->text,namelen);
866 namebuf[2+namelen] = ';';
870 return class_new( utf_new(namebuf,namelen) );
873 /************************** function: utf_strlen ******************************
875 determine number of unicode characters in the utf string
877 *******************************************************************************/
879 u4 utf_strlen(utf *u)
881 char *endpos = utf_end(u); /* points behind utf string */
882 char *utf_ptr = u->text; /* current position in utf text */
883 u4 len = 0; /* number of unicode characters */
885 while (utf_ptr<endpos) {
887 /* next unicode character */
888 utf_nextu2(&utf_ptr);
892 /* string ended abruptly */
893 panic("illegal utf string");
900 * These are local overrides for various environment variables in Emacs.
901 * Please do not remove this and leave it at the end of the file, where
902 * Emacs will automagically detect them.
903 * ---------------------------------------------------------------------
906 * indent-tabs-mode: t