* src/vmcore/class.h (classinfo): Changed type of super and interfaces
[cacao.git] / src / vmcore / primitivecore.c
1 /* src/vmcore/primitivecore.c - core functions for 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 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32
33 #include "vm/global.h"
34 #include "vm/primitive.h"
35 #include "vm/vm.h"
36
37 #include "vmcore/class.h"
38 #include "vmcore/utf8.h"
39
40
41 /* primitivetype_table *********************************************************
42
43    Structure for primitive classes: contains the class for wrapping
44    the primitive type, the primitive class, the name of the class for
45    wrapping, the one character type signature and the name of the
46    primitive class.
47  
48    CAUTION: Don't change the order of the types. This table is indexed
49    by the ARRAYTYPE_ constants (except ARRAYTYPE_OBJECT).
50
51 *******************************************************************************/
52
53 primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = {
54         { "int"     , NULL, NULL, NULL, "java/lang/Integer",   'I', "[I", NULL },
55         { "long"    , NULL, NULL, NULL, "java/lang/Long",      'J', "[J", NULL },
56         { "float"   , NULL, NULL, NULL, "java/lang/Float",     'F', "[F", NULL },
57         { "double"  , NULL, NULL, NULL, "java/lang/Double",    'D', "[D", NULL },
58         { NULL      , NULL, NULL, NULL, NULL,                   0 , NULL, NULL },
59         { "byte"    , NULL, NULL, NULL, "java/lang/Byte",      'B', "[B", NULL },
60         { "char"    , NULL, NULL, NULL, "java/lang/Character", 'C', "[C", NULL },
61         { "short"   , NULL, NULL, NULL, "java/lang/Short",     'S', "[S", NULL },
62         { "boolean" , NULL, NULL, NULL, "java/lang/Boolean",   'Z', "[Z", NULL },
63         { NULL      , NULL, NULL, NULL, NULL,                   0 , NULL, NULL },
64 #if defined(ENABLE_JAVASE)
65         { "void"    , NULL, NULL, NULL, "java/lang/Void",      'V', NULL, NULL }
66 #else
67         { NULL      , NULL, NULL, NULL, NULL,                   0 , NULL, NULL },
68 #endif
69 };
70
71
72 /* primitive_init **************************************************************
73
74    Fill the primitive type table with the primitive-type classes,
75    array-classes and wrapper classes.  This is important in the VM
76    startup.
77
78    We split this primitive-type table initialization because of
79    annotations in the bootstrap classes.
80
81    But we may get a problem if we have annotations in:
82
83    java/lang/Object
84    java/lang/Cloneable
85    java/io/Serializable
86
87    Also see: loader_preinit and linker_preinit.
88
89 *******************************************************************************/
90
91 void primitive_init(void)
92 {  
93         utf       *name;
94         classinfo *c;
95         utf       *u;
96         classinfo *ac;
97         int        i;
98
99         /* Load and link primitive-type classes and array-classes. */
100
101         for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
102                 /* Skip dummy entries. */
103
104                 if (primitivetype_table[i].cname == NULL)
105                         continue;
106
107                 /* create UTF-8 name */
108
109                 name = utf_new_char(primitivetype_table[i].cname);
110
111                 primitivetype_table[i].name = name;
112
113                 /* create primitive class */
114
115                 c = class_create_classinfo(name);
116
117                 /* Primitive type classes don't have a super class. */
118
119                 c->super = NULL;
120
121                 /* set flags and mark it as primitive class */
122
123                 c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT | ACC_CLASS_PRIMITIVE;
124                 
125                 /* prevent loader from loading primitive class */
126
127                 c->state |= CLASS_LOADED;
128
129                 /* INFO: don't put primitive classes into the classcache */
130
131                 if (!link_class(c))
132                         vm_abort("linker_init: linking failed");
133
134                 /* Just to be sure. */
135
136                 assert(c->state & CLASS_LOADED);
137                 assert(c->state & CLASS_LINKED);
138
139                 primitivetype_table[i].class_primitive = c;
140
141                 /* Create primitive array class. */
142
143                 if (primitivetype_table[i].arrayname != NULL) {
144                         u  = utf_new_char(primitivetype_table[i].arrayname);
145                         ac = class_create_classinfo(u);
146                         ac = load_newly_created_array(ac, NULL);
147
148                         if (ac == NULL)
149                                 vm_abort("primitive_init: loading failed");
150
151                         assert(ac->state & CLASS_LOADED);
152
153                         if (!link_class(ac))
154                                 vm_abort("primitive_init: linking failed");
155
156                         /* Just to be sure. */
157
158                         assert(ac->state & CLASS_LOADED);
159                         assert(ac->state & CLASS_LINKED);
160
161                         primitivetype_table[i].arrayclass = ac;
162                 }
163         }
164
165         /* We use two for-loops to have the array-classes already in the
166            primitive-type table (hint: annotations in wrapper-classes). */
167
168         for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
169                 /* Skip dummy entries. */
170
171                 if (primitivetype_table[i].cname == NULL)
172                         continue;
173
174                 /* Create class for wrapping the primitive type. */
175
176                 u = utf_new_char(primitivetype_table[i].wrapname);
177                 c = load_class_bootstrap(u);
178
179                 if (c == NULL)
180                         vm_abort("primitive_init: loading failed");
181
182                 if (!link_class(c))
183                         vm_abort("primitive_init: linking failed");
184
185                 /* Just to be sure. */
186
187                 assert(c->state & CLASS_LOADED);
188                 assert(c->state & CLASS_LINKED);
189
190                 primitivetype_table[i].class_wrap = c;
191         }
192 }
193
194
195 /* primitive_postinit **********************************************************
196
197    Finish the primitive-type table initialization.  In this step we
198    set the vftbl of the primitive-type classes.
199
200    This is necessary because java/lang/Class is loaded and linked
201    after the primitive types have been linked.
202
203    We have to do that in an extra function, as the primitive types are
204    not stored in the classcache.
205
206 *******************************************************************************/
207
208 void primitive_postinit(void)
209 {
210         classinfo *c;
211         int        i;
212
213         assert(class_java_lang_Class);
214         assert(class_java_lang_Class->vftbl);
215
216         for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
217                 /* Skip dummy entries. */
218
219                 if (primitivetype_table[i].cname == NULL)
220                         continue;
221
222                 c = primitivetype_table[i].class_primitive;
223
224                 c->object.header.vftbl = class_java_lang_Class->vftbl;
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  * vim:noexpandtab:sw=4:ts=4:
241  */