* Merged executionstate branch.
[cacao.git] / src / vm / primitive.c
1 /* src/vm/primitive.c - primitive types
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30
31 #include "native/jni.h"
32 #include "native/llni.h"
33
34 #include "native/include/java_lang_Boolean.h"
35 #include "native/include/java_lang_Byte.h"
36 #include "native/include/java_lang_Short.h"
37 #include "native/include/java_lang_Character.h"
38 #include "native/include/java_lang_Integer.h"
39 #include "native/include/java_lang_Long.h"
40 #include "native/include/java_lang_Float.h"
41 #include "native/include/java_lang_Double.h"
42
43 #include "vm/builtin.h"
44 #include "vm/global.h"
45 #include "vm/primitive.h"
46 #include "vm/vm.h"
47
48 #include "vmcore/class.h"
49 #include "vmcore/utf8.h"
50
51
52 /* primitive_class_get_by_name *************************************************
53
54    Returns the primitive class of the given class name.
55
56 *******************************************************************************/
57
58 classinfo *primitive_class_get_by_name(utf *name)
59 {
60         int i;
61
62         /* search table of primitive classes */
63
64         for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
65                 if (primitivetype_table[i].name == name)
66                         return primitivetype_table[i].class_primitive;
67
68         /* keep compiler happy */
69
70         return NULL;
71 }
72
73
74 /* primitive_class_get_by_type *************************************************
75
76    Returns the primitive class of the given type.
77
78 *******************************************************************************/
79
80 classinfo *primitive_class_get_by_type(int type)
81 {
82         return primitivetype_table[type].class_primitive;
83 }
84
85
86 /* primitive_class_get_by_char *************************************************
87
88    Returns the primitive class of the given type.
89
90 *******************************************************************************/
91
92 classinfo *primitive_class_get_by_char(char ch)
93 {
94         int index;
95
96         switch (ch) {
97         case 'I':
98                 index = PRIMITIVETYPE_INT;
99                 break;
100         case 'J':
101                 index = PRIMITIVETYPE_LONG;
102                 break;
103         case 'F':
104                 index = PRIMITIVETYPE_FLOAT;
105                 break;
106         case 'D':
107                 index = PRIMITIVETYPE_DOUBLE;
108                 break;
109         case 'B':
110                 index = PRIMITIVETYPE_BYTE;
111                 break;
112         case 'C':
113                 index = PRIMITIVETYPE_CHAR;
114                 break;
115         case 'S':
116                 index = PRIMITIVETYPE_SHORT;
117                 break;
118         case 'Z':
119                 index = PRIMITIVETYPE_BOOLEAN;
120                 break;
121         case 'V':
122                 index = PRIMITIVETYPE_VOID;
123                 break;
124         default:
125                 return NULL;
126         }
127
128         return primitivetype_table[index].class_primitive;
129 }
130
131
132 /* primitive_arrayclass_get_by_name ********************************************
133
134    Returns the primitive array-class of the given primitive class
135    name.
136
137 *******************************************************************************/
138
139 classinfo *primitive_arrayclass_get_by_name(utf *name)
140 {
141         int i;
142
143         /* search table of primitive classes */
144
145         for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
146                 if (primitivetype_table[i].name == name)
147                         return primitivetype_table[i].arrayclass;
148
149         /* keep compiler happy */
150
151         return NULL;
152 }
153
154
155 /* primitive_arrayclass_get_by_type ********************************************
156
157    Returns the primitive array-class of the given type.
158
159 *******************************************************************************/
160
161 classinfo *primitive_arrayclass_get_by_type(int type)
162 {
163         return primitivetype_table[type].arrayclass;
164 }
165
166
167 /* primitive_type_get_by_wrapperclass ******************************************
168
169    Returns the primitive type of the given wrapper-class.
170
171 *******************************************************************************/
172
173 int primitive_type_get_by_wrapperclass(classinfo *c)
174 {
175         int i;
176
177         /* Search primitive table. */
178
179         for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
180                 if (primitivetype_table[i].class_wrap == c)
181                         return i;
182
183         /* Invalid primitive wrapper-class. */
184
185         return -1;
186 }
187
188
189 /* primitive_box ***************************************************************
190
191    Box a primitive of the given type.  If the type is an object,
192    simply return it.
193
194 *******************************************************************************/
195
196 java_handle_t *primitive_box(int type, imm_union value)
197 {
198         java_handle_t *o;
199
200         switch (type) {
201         case PRIMITIVETYPE_BOOLEAN:
202                 o = primitive_box_boolean(value.i);
203                 break;
204         case PRIMITIVETYPE_BYTE:
205                 o = primitive_box_byte(value.i);
206                 break;
207         case PRIMITIVETYPE_CHAR:
208                 o = primitive_box_char(value.i);
209                 break;
210         case PRIMITIVETYPE_SHORT:
211                 o = primitive_box_short(value.i);
212                 break;
213         case PRIMITIVETYPE_INT:
214                 o = primitive_box_int(value.i);
215                 break;
216         case PRIMITIVETYPE_LONG:
217                 o = primitive_box_long(value.l);
218                 break;
219         case PRIMITIVETYPE_FLOAT:
220                 o = primitive_box_float(value.f);
221                 break;
222         case PRIMITIVETYPE_DOUBLE:
223                 o = primitive_box_double(value.d);
224                 break;
225         case PRIMITIVETYPE_VOID:
226                 o = value.a;
227                 break;
228         default:
229                 o = NULL;
230                 vm_abort("primitive_box: invalid primitive type %d", type);
231         }
232
233         return o;
234 }
235
236
237 /* primitive_unbox *************************************************************
238
239    Unbox a primitive of the given type.  If the type is an object,
240    simply return it.
241
242 *******************************************************************************/
243
244 imm_union primitive_unbox(java_handle_t *o)
245 {
246         classinfo *c;
247         int        type;
248         imm_union  value;
249
250         if (o == NULL) {
251                 value.a = NULL;
252                 return value;
253         }
254
255         LLNI_class_get(o, c);
256
257         type = primitive_type_get_by_wrapperclass(c);
258
259         switch (type) {
260         case PRIMITIVETYPE_BOOLEAN:
261                 value.i = primitive_unbox_boolean(o);
262                 break;
263         case PRIMITIVETYPE_BYTE:
264                 value.i = primitive_unbox_byte(o);
265                 break;
266         case PRIMITIVETYPE_CHAR:
267                 value.i = primitive_unbox_char(o);
268                 break;
269         case PRIMITIVETYPE_SHORT:
270                 value.i = primitive_unbox_short(o);
271                 break;
272         case PRIMITIVETYPE_INT:
273                 value.i = primitive_unbox_int(o);
274                 break;
275         case PRIMITIVETYPE_LONG:
276                 value.l = primitive_unbox_long(o);
277                 break;
278         case PRIMITIVETYPE_FLOAT:
279                 value.f = primitive_unbox_float(o);
280                 break;
281         case PRIMITIVETYPE_DOUBLE:
282                 value.d = primitive_unbox_double(o);
283                 break;
284         case -1:
285                 /* If type is -1 the object is not a primitive box but a
286                    normal object. */
287                 value.a = o;
288                 break;
289         default:
290                 vm_abort("primitive_unbox: invalid primitive type %d", type);
291         }
292
293         return value;
294 }
295
296
297 /* primitive_box_xxx ***********************************************************
298
299    Box a primitive type.
300
301 *******************************************************************************/
302
303 #define PRIMITIVE_BOX_TYPE(name, object, type)      \
304 java_handle_t *primitive_box_##name(type value)     \
305 {                                                   \
306         java_handle_t      *o;                          \
307         java_lang_##object *jo;                         \
308                                                     \
309         o = builtin_new(class_java_lang_##object);      \
310                                                     \
311         if (o == NULL)                                  \
312                 return NULL;                                \
313                                                     \
314         jo = (java_lang_##object *) o;                  \
315                                                     \
316         LLNI_field_set_val(jo, value, value);           \
317                                                     \
318         return o;                                       \
319 }
320
321 PRIMITIVE_BOX_TYPE(boolean, Boolean,   int32_t)
322 PRIMITIVE_BOX_TYPE(byte,    Byte,      int32_t)
323 PRIMITIVE_BOX_TYPE(char,    Character, int32_t)
324 PRIMITIVE_BOX_TYPE(short,   Short,     int32_t)
325 PRIMITIVE_BOX_TYPE(int,     Integer,   int32_t)
326 PRIMITIVE_BOX_TYPE(long,    Long,      int64_t)
327 PRIMITIVE_BOX_TYPE(float,   Float,     float)
328 PRIMITIVE_BOX_TYPE(double,  Double,    double)
329
330
331 /* primitive_unbox_xxx *********************************************************
332
333    Unbox a primitive type.
334
335 *******************************************************************************/
336
337 #define PRIMITIVE_UNBOX_TYPE(name, object, type)  \
338 type primitive_unbox_##name(java_handle_t *o)     \
339 {                                                 \
340         java_lang_##object *jo;                       \
341         type                value;                    \
342                                                   \
343         jo = (java_lang_##object *) o;                \
344                                                   \
345         LLNI_field_get_val(jo, value, value);         \
346                                                   \
347         return value;                                 \
348 }
349
350 PRIMITIVE_UNBOX_TYPE(boolean, Boolean,   int32_t)
351 PRIMITIVE_UNBOX_TYPE(byte,    Byte,      int32_t)
352 PRIMITIVE_UNBOX_TYPE(char,    Character, int32_t)
353 PRIMITIVE_UNBOX_TYPE(short,   Short,     int32_t)
354 PRIMITIVE_UNBOX_TYPE(int,     Integer,   int32_t)
355 PRIMITIVE_UNBOX_TYPE(long,    Long,      int64_t)
356 PRIMITIVE_UNBOX_TYPE(float,   Float,     float)
357 PRIMITIVE_UNBOX_TYPE(double,  Double,    double)
358
359
360 /*
361  * These are local overrides for various environment variables in Emacs.
362  * Please do not remove this and leave it at the end of the file, where
363  * Emacs will automagically detect them.
364  * ---------------------------------------------------------------------
365  * Local variables:
366  * mode: c
367  * indent-tabs-mode: t
368  * c-basic-offset: 4
369  * tab-width: 4
370  * End:
371  * vim:noexpandtab:sw=4:ts=4:
372  */