* exceptions_new_nosuchmethoderror: Added.
[cacao.git] / src / vm / tables.c
1 /* src/vm/tables.c - 
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28
29    Changes: Mark Probst
30             Andreas Krall
31             Christian Thalinger
32
33    Contains support functions for:
34        - Reading of Java class files
35        - Unicode symbols
36        - the heap
37        - additional support functions
38
39    $Id: tables.c 3239 2005-09-21 14:09:22Z twisti $
40
41 */
42
43
44 #include <string.h>
45 #include <stdlib.h>
46 #include <assert.h>
47 #include <sys/types.h>
48 #include <sys/mman.h>
49 #include <unistd.h>
50
51 #include "config.h"
52 #include "vm/types.h"
53
54 #include "mm/memory.h"
55 #include "native/native.h"
56 #include "toolbox/logging.h"
57 #include "vm/builtin.h"
58 #include "vm/exceptions.h"
59 #include "vm/global.h"
60 #include "vm/loader.h"
61 #include "vm/options.h"
62 #include "vm/statistics.h"
63 #include "vm/stringlocal.h"
64 #include "vm/tables.h"
65 #include "vm/classcache.h"
66
67
68 hashtable string_hash;  /* hashtable for javastrings  */
69
70
71 /******************************************************************************
72  *********************** hashtable functions **********************************
73  ******************************************************************************/
74
75 /* hashsize must be power of 2 */
76
77 #define UTF_HASHSTART   16384   /* initial size of utf-hash */    
78 #define HASHSTART        2048   /* initial size of javastring and class-hash */
79
80
81 /******************** function: init_hashtable ******************************
82
83     Initializes a hashtable structure and allocates memory.
84     The parameter size specifies the initial size of the hashtable.
85         
86 *****************************************************************************/
87
88 void init_hashtable(hashtable *hash, u4 size)
89 {
90         u4 i;
91
92         hash->entries = 0;
93         hash->size    = size;
94         hash->ptr     = MNEW(void*, size);
95
96         /* clear table */
97         for (i = 0; i < size; i++) hash->ptr[i] = NULL;
98 }
99
100
101 /*********************** function: tables_init  *****************************
102
103     creates hashtables for symboltables 
104         (called once at startup)                         
105         
106 *****************************************************************************/
107
108 void tables_init()
109 {
110         init_hashtable(&utf_hash,    UTF_HASHSTART);  /* hashtable for utf8-symbols */
111         init_hashtable(&string_hash, HASHSTART);      /* hashtable for javastrings */
112
113         classcache_init();
114
115 /*      if (opt_eager) */
116 /*              list_init(&unlinkedclasses, OFFSET(classinfo, listnode)); */
117
118 #if defined(STATISTICS)
119         if (opt_stat)
120                 count_utf_len += sizeof(utf*) * utf_hash.size;
121 #endif
122 }
123
124
125 /********************** function: tables_close ******************************
126
127         free memory for hashtables                    
128         
129 *****************************************************************************/
130
131 void tables_close()
132 {
133         utf *u = NULL;
134         literalstring *s;
135         u4 i;
136
137         classcache_free();
138         
139         /* dispose utf symbols */
140         for (i = 0; i < utf_hash.size; i++) {
141                 u = utf_hash.ptr[i];
142                 while (u) {
143                         /* process elements in external hash chain */
144                         utf *nextu = u->hashlink;
145                         MFREE(u->text, u1, u->blength);
146                         FREE(u, utf);
147                         u = nextu;
148                 }       
149         }
150
151         /* dispose javastrings */
152         for (i = 0; i < string_hash.size; i++) {
153                 s = string_hash.ptr[i];
154                 while (u) {
155                         /* process elements in external hash chain */
156                         literalstring *nexts = s->hashlink;
157                         literalstring_free(s->string);
158                         FREE(s, literalstring);
159                         s = nexts;
160                 }       
161         }
162
163         /* dispose hashtable structures */
164         MFREE(utf_hash.ptr,    void*, utf_hash.size);
165         MFREE(string_hash.ptr, void*, string_hash.size);
166 }
167
168
169 /******************************************************************************
170 *********************** Misc support functions ********************************
171 ******************************************************************************/
172
173
174 /******************** Function: desc_to_type **********************************
175    
176         Determines the corresponding Java base data type for a given type
177         descriptor.
178         
179 ******************************************************************************/
180
181 u2 desc_to_type(utf *descriptor)
182 {
183         char *utf_ptr = descriptor->text;  /* current position in utf text */
184
185         if (descriptor->blength < 1) {
186                 log_text("Type-Descriptor is empty string");
187                 assert(0);
188         }
189         
190         switch (*utf_ptr++) {
191         case 'B': 
192         case 'C':
193         case 'I':
194         case 'S':  
195         case 'Z':  return TYPE_INT;
196         case 'D':  return TYPE_DOUBLE;
197         case 'F':  return TYPE_FLOAT;
198         case 'J':  return TYPE_LONG;
199         case 'L':
200         case '[':  return TYPE_ADDRESS;
201         }
202                         
203         assert(0);
204
205         return 0;
206 }
207
208
209 /********************** Function: desc_typesize *******************************
210
211         Calculates the lenght in bytes needed for a data element of the type given
212         by its type descriptor.
213         
214 ******************************************************************************/
215
216 u2 desc_typesize(utf *descriptor)
217 {
218         switch (desc_to_type(descriptor)) {
219         case TYPE_INT:     return 4;
220         case TYPE_LONG:    return 8;
221         case TYPE_FLOAT:   return 4;
222         case TYPE_DOUBLE:  return 8;
223         case TYPE_ADDRESS: return sizeof(voidptr);
224         default:           return 0;
225         }
226 }
227
228
229 /*
230  * These are local overrides for various environment variables in Emacs.
231  * Please do not remove this and leave it at the end of the file, where
232  * Emacs will automagically detect them.
233  * ---------------------------------------------------------------------
234  * Local variables:
235  * mode: c
236  * indent-tabs-mode: t
237  * c-basic-offset: 4
238  * tab-width: 4
239  * End:
240  */