8d39343b315e2880e8b4485195cf07cdea130f8c
[cacao.git] / src / vmcore / primitive.c
1 /* src/vmcore/primitive.c - primitive types
2
3    Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: linker.c 8042 2007-06-07 17:43:29Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34
35 #include "vm/global.h"
36
37 #include "vmcore/class.h"
38 #include "vmcore/primitive.h"
39 #include "vmcore/utf8.h"
40
41
42 /* primitivetype_table *********************************************************
43
44    Structure for primitive classes: contains the class for wrapping
45    the primitive type, the primitive class, the name of the class for
46    wrapping, the one character type signature and the name of the
47    primitive class.
48  
49    CAUTION: Don't change the order of the types. This table is indexed
50    by the ARRAYTYPE_ constants (except ARRAYTYPE_OBJECT).
51
52 *******************************************************************************/
53
54 primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = {
55         { "int"     , NULL, NULL, NULL, "java/lang/Integer",   'I', "[I", NULL },
56         { "long"    , NULL, NULL, NULL, "java/lang/Long",      'J', "[J", NULL },
57         { "float"   , NULL, NULL, NULL, "java/lang/Float",     'F', "[F", NULL },
58         { "double"  , NULL, NULL, NULL, "java/lang/Double",    'D', "[D", NULL },
59         { NULL      , NULL, NULL, NULL, NULL,                   0 , NULL, NULL },
60         { "byte"    , NULL, NULL, NULL, "java/lang/Byte",      'B', "[B", NULL },
61         { "char"    , NULL, NULL, NULL, "java/lang/Character", 'C', "[C", NULL },
62         { "short"   , NULL, NULL, NULL, "java/lang/Short",     'S', "[S", NULL },
63         { "boolean" , NULL, NULL, NULL, "java/lang/Boolean",   'Z', "[Z", NULL },
64         { NULL      , NULL, NULL, NULL, NULL,                   0 , NULL, NULL },
65 #if defined(ENABLE_JAVASE)
66         { "void"    , NULL, NULL, NULL, "java/lang/Void",      'V', NULL, NULL }
67 #else
68         { NULL      , NULL, NULL, NULL, NULL,                   0 , NULL, NULL },
69 #endif
70 };
71
72
73 /* primitive_init **************************************************************
74
75    Create classes representing primitive types.
76
77 *******************************************************************************/
78
79 bool primitive_init(void)
80 {  
81         utf       *name;
82         classinfo *c;
83         utf       *u;
84         int        i;
85
86         for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
87                 /* skip dummies */
88
89                 if (primitivetype_table[i].cname == NULL)
90                         continue;
91
92                 /* create UTF-8 name */
93
94                 name = utf_new_char(primitivetype_table[i].cname);
95
96                 primitivetype_table[i].name = name;
97
98                 /* create primitive class */
99
100                 c = class_create_classinfo(name);
101
102                 /* primitive classes don't have a super class */
103
104                 c->super.any = NULL;
105
106                 /* set flags and mark it as primitive class */
107
108                 c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT | ACC_CLASS_PRIMITIVE;
109                 
110                 /* prevent loader from loading primitive class */
111
112                 c->state |= CLASS_LOADED;
113
114                 /* INFO: don't put primitive classes into the classcache */
115
116                 if (!link_class(c))
117                         return false;
118
119                 primitivetype_table[i].class_primitive = c;
120
121                 /* create class for wrapping the primitive type */
122
123                 u = utf_new_char(primitivetype_table[i].wrapname);
124                 c = load_class_bootstrap(u);
125
126                 if (c == NULL)
127                         return false;
128
129                 primitivetype_table[i].class_wrap = c;
130
131                 /* create the primitive array class */
132
133                 if (primitivetype_table[i].arrayname) {
134                         u = utf_new_char(primitivetype_table[i].arrayname);
135                         c = class_create_classinfo(u);
136                         c = load_newly_created_array(c, NULL);
137
138                         if (c == NULL)
139                                 return false;
140
141                         primitivetype_table[i].arrayclass = c;
142
143                         assert(c->state & CLASS_LOADED);
144
145                         if (!(c->state & CLASS_LINKED))
146                                 if (!link_class(c))
147                                         return false;
148                 }
149         }
150
151         return true;
152 }
153
154
155 /* primitive_class_get_by_name *************************************************
156
157    Returns the primitive class of the given class name.
158
159 *******************************************************************************/
160
161 classinfo *primitive_class_get_by_name(utf *name)
162 {
163         int i;
164
165         /* search table of primitive classes */
166
167         for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
168                 if (primitivetype_table[i].name == name)
169                         return primitivetype_table[i].class_primitive;
170
171         /* keep compiler happy */
172
173         return NULL;
174 }
175
176
177 /* primitive_class_get_by_type *************************************************
178
179    Returns the primitive class of the given type.
180
181 *******************************************************************************/
182
183 classinfo *primitive_class_get_by_type(int32_t type)
184 {
185         return primitivetype_table[type].class_primitive;
186 }
187
188
189 /* primitive_class_get_by_char *************************************************
190
191    Returns the primitive class of the given type.
192
193 *******************************************************************************/
194
195 classinfo *primitive_class_get_by_char(char ch)
196 {
197         int32_t index;
198
199         switch (ch) {
200         case 'I':
201                 index = PRIMITIVETYPE_INT;
202                 break;
203         case 'J':
204                 index = PRIMITIVETYPE_LONG;
205                 break;
206         case 'F':
207                 index = PRIMITIVETYPE_FLOAT;
208                 break;
209         case 'D':
210                 index = PRIMITIVETYPE_DOUBLE;
211                 break;
212         case 'B':
213                 index = PRIMITIVETYPE_BYTE;
214                 break;
215         case 'C':
216                 index = PRIMITIVETYPE_CHAR;
217                 break;
218         case 'S':
219                 index = PRIMITIVETYPE_SHORT;
220                 break;
221         case 'Z':
222                 index = PRIMITIVETYPE_BOOLEAN;
223                 break;
224         case 'V':
225                 index = PRIMITIVETYPE_VOID;
226                 break;
227         default:
228                 return NULL;
229         }
230
231         return primitivetype_table[index].class_primitive;
232 }
233
234
235 /* primitive_arrayclass_get_by_name ********************************************
236
237    Returns the primitive array-class of the given primitive class
238    name.
239
240 *******************************************************************************/
241
242 classinfo *primitive_arrayclass_get_by_name(utf *name)
243 {
244         int i;
245
246         /* search table of primitive classes */
247
248         for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
249                 if (primitivetype_table[i].name == name)
250                         return primitivetype_table[i].arrayclass;
251
252         /* keep compiler happy */
253
254         return NULL;
255 }
256
257
258 /* primitive_arrayclass_get_by_type ********************************************
259
260    Returns the primitive array-class of the given type.
261
262 *******************************************************************************/
263
264 classinfo *primitive_arrayclass_get_by_type(int32_t type)
265 {
266         return primitivetype_table[type].arrayclass;
267 }
268
269
270 /*
271  * These are local overrides for various environment variables in Emacs.
272  * Please do not remove this and leave it at the end of the file, where
273  * Emacs will automagically detect them.
274  * ---------------------------------------------------------------------
275  * Local variables:
276  * mode: c
277  * indent-tabs-mode: t
278  * c-basic-offset: 4
279  * tab-width: 4
280  * End:
281  * vim:noexpandtab:sw=4:ts=4:
282  */