* src/vmcore/loader.c (load_class_from_classloader) [!ENABLE_GC_CACAO]: Pass
[cacao.git] / src / vm / primitive.c
1 /* src/vm/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 "native/jni.h"
36 #include "native/llni.h"
37
38 #include "native/include/java_lang_Boolean.h"
39 #include "native/include/java_lang_Byte.h"
40 #include "native/include/java_lang_Short.h"
41 #include "native/include/java_lang_Character.h"
42 #include "native/include/java_lang_Integer.h"
43 #include "native/include/java_lang_Long.h"
44 #include "native/include/java_lang_Float.h"
45 #include "native/include/java_lang_Double.h"
46
47 #include "vm/builtin.h"
48 #include "vm/global.h"
49 #include "vm/primitive.h"
50 #include "vm/vm.h"
51
52 #include "vmcore/class.h"
53 #include "vmcore/utf8.h"
54
55
56 /* primitive_class_get_by_name *************************************************
57
58    Returns the primitive class of the given class name.
59
60 *******************************************************************************/
61
62 classinfo *primitive_class_get_by_name(utf *name)
63 {
64         int i;
65
66         /* search table of primitive classes */
67
68         for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
69                 if (primitivetype_table[i].name == name)
70                         return primitivetype_table[i].class_primitive;
71
72         /* keep compiler happy */
73
74         return NULL;
75 }
76
77
78 /* primitive_class_get_by_type *************************************************
79
80    Returns the primitive class of the given type.
81
82 *******************************************************************************/
83
84 classinfo *primitive_class_get_by_type(int type)
85 {
86         return primitivetype_table[type].class_primitive;
87 }
88
89
90 /* primitive_class_get_by_char *************************************************
91
92    Returns the primitive class of the given type.
93
94 *******************************************************************************/
95
96 classinfo *primitive_class_get_by_char(char ch)
97 {
98         int index;
99
100         switch (ch) {
101         case 'I':
102                 index = PRIMITIVETYPE_INT;
103                 break;
104         case 'J':
105                 index = PRIMITIVETYPE_LONG;
106                 break;
107         case 'F':
108                 index = PRIMITIVETYPE_FLOAT;
109                 break;
110         case 'D':
111                 index = PRIMITIVETYPE_DOUBLE;
112                 break;
113         case 'B':
114                 index = PRIMITIVETYPE_BYTE;
115                 break;
116         case 'C':
117                 index = PRIMITIVETYPE_CHAR;
118                 break;
119         case 'S':
120                 index = PRIMITIVETYPE_SHORT;
121                 break;
122         case 'Z':
123                 index = PRIMITIVETYPE_BOOLEAN;
124                 break;
125         case 'V':
126                 index = PRIMITIVETYPE_VOID;
127                 break;
128         default:
129                 return NULL;
130         }
131
132         return primitivetype_table[index].class_primitive;
133 }
134
135
136 /* primitive_arrayclass_get_by_name ********************************************
137
138    Returns the primitive array-class of the given primitive class
139    name.
140
141 *******************************************************************************/
142
143 classinfo *primitive_arrayclass_get_by_name(utf *name)
144 {
145         int i;
146
147         /* search table of primitive classes */
148
149         for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
150                 if (primitivetype_table[i].name == name)
151                         return primitivetype_table[i].arrayclass;
152
153         /* keep compiler happy */
154
155         return NULL;
156 }
157
158
159 /* primitive_arrayclass_get_by_type ********************************************
160
161    Returns the primitive array-class of the given type.
162
163 *******************************************************************************/
164
165 classinfo *primitive_arrayclass_get_by_type(int type)
166 {
167         return primitivetype_table[type].arrayclass;
168 }
169
170
171 /* primitive_box ***************************************************************
172
173    Box a primitive of the given type.
174
175 *******************************************************************************/
176
177 java_handle_t *primitive_box(int type, imm_union value)
178 {
179         java_handle_t *o;
180
181         switch (type) {
182         case PRIMITIVETYPE_BOOLEAN:
183                 o = primitive_box_boolean(value.i);
184                 break;
185         case PRIMITIVETYPE_BYTE:
186                 o = primitive_box_byte(value.i);
187                 break;
188         case PRIMITIVETYPE_CHAR:
189                 o = primitive_box_char(value.i);
190                 break;
191         case PRIMITIVETYPE_SHORT:
192                 o = primitive_box_short(value.i);
193                 break;
194         case PRIMITIVETYPE_INT:
195                 o = primitive_box_int(value.i);
196                 break;
197         case PRIMITIVETYPE_LONG:
198                 o = primitive_box_long(value.l);
199                 break;
200         case PRIMITIVETYPE_FLOAT:
201                 o = primitive_box_float(value.f);
202                 break;
203         case PRIMITIVETYPE_DOUBLE:
204                 o = primitive_box_double(value.d);
205                 break;
206         default:
207                 vm_abort("primitive_box: invalid primitive type %d", type);
208         }
209
210         return o;
211 }
212
213
214 /* primitive_unbox *************************************************************
215
216    Unbox a primitive of the given type.
217
218 *******************************************************************************/
219
220 imm_union primitive_unbox(int type, java_handle_t *o)
221 {
222         imm_union value;
223
224         switch (type) {
225         case PRIMITIVETYPE_BOOLEAN:
226                 value.i = primitive_unbox_boolean(o);
227                 break;
228         case PRIMITIVETYPE_BYTE:
229                 value.i = primitive_unbox_byte(o);
230                 break;
231         case PRIMITIVETYPE_CHAR:
232                 value.i = primitive_unbox_char(o);
233                 break;
234         case PRIMITIVETYPE_SHORT:
235                 value.i = primitive_unbox_short(o);
236                 break;
237         case PRIMITIVETYPE_INT:
238                 value.i = primitive_unbox_int(o);
239                 break;
240         case PRIMITIVETYPE_LONG:
241                 value.l = primitive_unbox_long(o);
242                 break;
243         case PRIMITIVETYPE_FLOAT:
244                 value.f = primitive_unbox_float(o);
245                 break;
246         case PRIMITIVETYPE_DOUBLE:
247                 value.d = primitive_unbox_double(o);
248                 break;
249         default:
250                 vm_abort("primitive_unbox: invalid primitive type %d", type);
251         }
252
253         return value;
254 }
255
256
257 /* primitive_box_xxx ***********************************************************
258
259    Box a primitive type.
260
261 *******************************************************************************/
262
263 #define PRIMITIVE_BOX_TYPE(name, object, type)      \
264 java_handle_t *primitive_box_##name(type value)     \
265 {                                                   \
266         java_handle_t      *o;                          \
267         java_lang_##object *jo;                         \
268                                                     \
269         o = builtin_new(class_java_lang_##object);      \
270                                                     \
271         if (o == NULL)                                  \
272                 return NULL;                                \
273                                                     \
274         jo = (java_lang_##object *) o;                  \
275                                                     \
276         LLNI_field_set_val(jo, value, value);           \
277                                                     \
278         return o;                                       \
279 }
280
281 PRIMITIVE_BOX_TYPE(boolean, Boolean,   int32_t)
282 PRIMITIVE_BOX_TYPE(byte,    Byte,      int32_t)
283 PRIMITIVE_BOX_TYPE(char,    Character, int32_t)
284 PRIMITIVE_BOX_TYPE(short,   Short,     int32_t)
285 PRIMITIVE_BOX_TYPE(int,     Integer,   int32_t)
286 PRIMITIVE_BOX_TYPE(long,    Long,      int64_t)
287 PRIMITIVE_BOX_TYPE(float,   Float,     float)
288 PRIMITIVE_BOX_TYPE(double,  Double,    double)
289
290
291 /* primitive_unbox_xxx *********************************************************
292
293    Unbox a primitive type.
294
295 *******************************************************************************/
296
297 #define PRIMITIVE_UNBOX_TYPE(name, object, type)  \
298 type primitive_unbox_##name(java_handle_t *o)     \
299 {                                                 \
300         java_lang_##object *jo;                       \
301         type                value;                    \
302                                                   \
303         jo = (java_lang_##object *) o;                \
304                                                   \
305         LLNI_field_get_val(jo, value, value);         \
306                                                   \
307         return value;                                 \
308 }
309
310 PRIMITIVE_UNBOX_TYPE(boolean, Boolean,   int32_t)
311 PRIMITIVE_UNBOX_TYPE(byte,    Byte,      int32_t)
312 PRIMITIVE_UNBOX_TYPE(char,    Character, int32_t)
313 PRIMITIVE_UNBOX_TYPE(short,   Short,     int32_t)
314 PRIMITIVE_UNBOX_TYPE(int,     Integer,   int32_t)
315 PRIMITIVE_UNBOX_TYPE(long,    Long,      int64_t)
316 PRIMITIVE_UNBOX_TYPE(float,   Float,     float)
317 PRIMITIVE_UNBOX_TYPE(double,  Double,    double)
318
319
320 /*
321  * These are local overrides for various environment variables in Emacs.
322  * Please do not remove this and leave it at the end of the file, where
323  * Emacs will automagically detect them.
324  * ---------------------------------------------------------------------
325  * Local variables:
326  * mode: c
327  * indent-tabs-mode: t
328  * c-basic-offset: 4
329  * tab-width: 4
330  * End:
331  * vim:noexpandtab:sw=4:ts=4:
332  */