1 /* src/vm/primitive.c - primitive types
3 Copyright (C) 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
31 #include "native/jni.h"
32 #include "native/llni.h"
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"
43 #include "vm/builtin.h"
44 #include "vm/global.h"
45 #include "vm/primitive.h"
48 #include "vmcore/class.h"
49 #include "vmcore/utf8.h"
52 /* primitive_class_get_by_name *************************************************
54 Returns the primitive class of the given class name.
56 *******************************************************************************/
58 classinfo *primitive_class_get_by_name(utf *name)
62 /* search table of primitive classes */
64 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
65 if (primitivetype_table[i].name == name)
66 return primitivetype_table[i].class_primitive;
68 /* keep compiler happy */
74 /* primitive_class_get_by_type *************************************************
76 Returns the primitive class of the given type.
78 *******************************************************************************/
80 classinfo *primitive_class_get_by_type(int type)
82 return primitivetype_table[type].class_primitive;
86 /* primitive_class_get_by_char *************************************************
88 Returns the primitive class of the given type.
90 *******************************************************************************/
92 classinfo *primitive_class_get_by_char(char ch)
98 index = PRIMITIVETYPE_INT;
101 index = PRIMITIVETYPE_LONG;
104 index = PRIMITIVETYPE_FLOAT;
107 index = PRIMITIVETYPE_DOUBLE;
110 index = PRIMITIVETYPE_BYTE;
113 index = PRIMITIVETYPE_CHAR;
116 index = PRIMITIVETYPE_SHORT;
119 index = PRIMITIVETYPE_BOOLEAN;
122 index = PRIMITIVETYPE_VOID;
128 return primitivetype_table[index].class_primitive;
132 /* primitive_arrayclass_get_by_name ********************************************
134 Returns the primitive array-class of the given primitive class
137 *******************************************************************************/
139 classinfo *primitive_arrayclass_get_by_name(utf *name)
143 /* search table of primitive classes */
145 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
146 if (primitivetype_table[i].name == name)
147 return primitivetype_table[i].arrayclass;
149 /* keep compiler happy */
155 /* primitive_arrayclass_get_by_type ********************************************
157 Returns the primitive array-class of the given type.
159 *******************************************************************************/
161 classinfo *primitive_arrayclass_get_by_type(int type)
163 return primitivetype_table[type].arrayclass;
167 /* primitive_type_get_by_wrapperclass ******************************************
169 Returns the primitive type of the given wrapper-class.
171 *******************************************************************************/
173 int primitive_type_get_by_wrapperclass(classinfo *c)
177 /* Search primitive table. */
179 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
180 if (primitivetype_table[i].class_wrap == c)
183 /* Invalid primitive wrapper-class. */
189 /* primitive_box ***************************************************************
191 Box a primitive of the given type. If the type is an object,
194 *******************************************************************************/
196 java_handle_t *primitive_box(int type, imm_union value)
201 case PRIMITIVETYPE_BOOLEAN:
202 o = primitive_box_boolean(value.i);
204 case PRIMITIVETYPE_BYTE:
205 o = primitive_box_byte(value.i);
207 case PRIMITIVETYPE_CHAR:
208 o = primitive_box_char(value.i);
210 case PRIMITIVETYPE_SHORT:
211 o = primitive_box_short(value.i);
213 case PRIMITIVETYPE_INT:
214 o = primitive_box_int(value.i);
216 case PRIMITIVETYPE_LONG:
217 o = primitive_box_long(value.l);
219 case PRIMITIVETYPE_FLOAT:
220 o = primitive_box_float(value.f);
222 case PRIMITIVETYPE_DOUBLE:
223 o = primitive_box_double(value.d);
225 case PRIMITIVETYPE_VOID:
230 vm_abort("primitive_box: invalid primitive type %d", type);
237 /* primitive_unbox *************************************************************
239 Unbox a primitive of the given type. If the type is an object,
242 *******************************************************************************/
244 imm_union primitive_unbox(java_handle_t *o)
255 LLNI_class_get(o, c);
257 type = primitive_type_get_by_wrapperclass(c);
260 case PRIMITIVETYPE_BOOLEAN:
261 value.i = primitive_unbox_boolean(o);
263 case PRIMITIVETYPE_BYTE:
264 value.i = primitive_unbox_byte(o);
266 case PRIMITIVETYPE_CHAR:
267 value.i = primitive_unbox_char(o);
269 case PRIMITIVETYPE_SHORT:
270 value.i = primitive_unbox_short(o);
272 case PRIMITIVETYPE_INT:
273 value.i = primitive_unbox_int(o);
275 case PRIMITIVETYPE_LONG:
276 value.l = primitive_unbox_long(o);
278 case PRIMITIVETYPE_FLOAT:
279 value.f = primitive_unbox_float(o);
281 case PRIMITIVETYPE_DOUBLE:
282 value.d = primitive_unbox_double(o);
285 /* If type is -1 the object is not a primitive box but a
290 vm_abort("primitive_unbox: invalid primitive type %d", type);
297 /* primitive_box_xxx ***********************************************************
299 Box a primitive type.
301 *******************************************************************************/
303 #define PRIMITIVE_BOX_TYPE(name, object, type) \
304 java_handle_t *primitive_box_##name(type value) \
307 java_lang_##object *jo; \
309 o = builtin_new(class_java_lang_##object); \
314 jo = (java_lang_##object *) o; \
316 LLNI_field_set_val(jo, value, value); \
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)
331 /* primitive_unbox_xxx *********************************************************
333 Unbox a primitive type.
335 *******************************************************************************/
337 #define PRIMITIVE_UNBOX_TYPE(name, object, type) \
338 type primitive_unbox_##name(java_handle_t *o) \
340 java_lang_##object *jo; \
343 jo = (java_lang_##object *) o; \
345 LLNI_field_get_val(jo, value, value); \
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)
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 * ---------------------------------------------------------------------
367 * indent-tabs-mode: t
371 * vim:noexpandtab:sw=4:ts=4: