1 /* src/vm/primitive.c - primitive types
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
8 This file is part of CACAO.
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.
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.
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
33 #include "native/jni.h"
34 #include "native/llni.h"
36 #include "native/include/java_lang_Boolean.h"
37 #include "native/include/java_lang_Byte.h"
38 #include "native/include/java_lang_Short.h"
39 #include "native/include/java_lang_Character.h"
40 #include "native/include/java_lang_Integer.h"
41 #include "native/include/java_lang_Long.h"
42 #include "native/include/java_lang_Float.h"
43 #include "native/include/java_lang_Double.h"
45 #include "vm/builtin.h"
46 #include "vm/global.h"
47 #include "vm/primitive.h"
50 #include "vmcore/class.h"
51 #include "vmcore/utf8.h"
54 /* primitive_class_get_by_name *************************************************
56 Returns the primitive class of the given class name.
58 *******************************************************************************/
60 classinfo *primitive_class_get_by_name(utf *name)
64 /* search table of primitive classes */
66 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
67 if (primitivetype_table[i].name == name)
68 return primitivetype_table[i].class_primitive;
70 /* keep compiler happy */
76 /* primitive_class_get_by_type *************************************************
78 Returns the primitive class of the given type.
80 *******************************************************************************/
82 classinfo *primitive_class_get_by_type(int type)
84 return primitivetype_table[type].class_primitive;
88 /* primitive_class_get_by_char *************************************************
90 Returns the primitive class of the given type.
92 *******************************************************************************/
94 classinfo *primitive_class_get_by_char(char ch)
100 index = PRIMITIVETYPE_INT;
103 index = PRIMITIVETYPE_LONG;
106 index = PRIMITIVETYPE_FLOAT;
109 index = PRIMITIVETYPE_DOUBLE;
112 index = PRIMITIVETYPE_BYTE;
115 index = PRIMITIVETYPE_CHAR;
118 index = PRIMITIVETYPE_SHORT;
121 index = PRIMITIVETYPE_BOOLEAN;
124 index = PRIMITIVETYPE_VOID;
130 return primitivetype_table[index].class_primitive;
134 /* primitive_arrayclass_get_by_name ********************************************
136 Returns the primitive array-class of the given primitive class
139 *******************************************************************************/
141 classinfo *primitive_arrayclass_get_by_name(utf *name)
145 /* search table of primitive classes */
147 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
148 if (primitivetype_table[i].name == name)
149 return primitivetype_table[i].arrayclass;
151 /* keep compiler happy */
157 /* primitive_arrayclass_get_by_type ********************************************
159 Returns the primitive array-class of the given type.
161 *******************************************************************************/
163 classinfo *primitive_arrayclass_get_by_type(int type)
165 return primitivetype_table[type].arrayclass;
169 /* primitive_type_get_by_wrapperclass ******************************************
171 Returns the primitive type of the given wrapper-class.
173 *******************************************************************************/
175 int primitive_type_get_by_wrapperclass(classinfo *c)
179 /* Search primitive table. */
181 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
182 if (primitivetype_table[i].class_wrap == c)
185 /* Invalid primitive wrapper-class. */
191 /* primitive_box ***************************************************************
193 Box a primitive of the given type. If the type is an object,
196 *******************************************************************************/
198 java_handle_t *primitive_box(int type, imm_union value)
203 case PRIMITIVETYPE_BOOLEAN:
204 o = primitive_box_boolean(value.i);
206 case PRIMITIVETYPE_BYTE:
207 o = primitive_box_byte(value.i);
209 case PRIMITIVETYPE_CHAR:
210 o = primitive_box_char(value.i);
212 case PRIMITIVETYPE_SHORT:
213 o = primitive_box_short(value.i);
215 case PRIMITIVETYPE_INT:
216 o = primitive_box_int(value.i);
218 case PRIMITIVETYPE_LONG:
219 o = primitive_box_long(value.l);
221 case PRIMITIVETYPE_FLOAT:
222 o = primitive_box_float(value.f);
224 case PRIMITIVETYPE_DOUBLE:
225 o = primitive_box_double(value.d);
227 case PRIMITIVETYPE_VOID:
231 vm_abort("primitive_box: invalid primitive type %d", type);
238 /* primitive_unbox *************************************************************
240 Unbox a primitive of the given type. If the type is an object,
243 *******************************************************************************/
245 imm_union primitive_unbox(java_handle_t *o)
253 type = primitive_type_get_by_wrapperclass(c);
256 case PRIMITIVETYPE_BOOLEAN:
257 value.i = primitive_unbox_boolean(o);
259 case PRIMITIVETYPE_BYTE:
260 value.i = primitive_unbox_byte(o);
262 case PRIMITIVETYPE_CHAR:
263 value.i = primitive_unbox_char(o);
265 case PRIMITIVETYPE_SHORT:
266 value.i = primitive_unbox_short(o);
268 case PRIMITIVETYPE_INT:
269 value.i = primitive_unbox_int(o);
271 case PRIMITIVETYPE_LONG:
272 value.l = primitive_unbox_long(o);
274 case PRIMITIVETYPE_FLOAT:
275 value.f = primitive_unbox_float(o);
277 case PRIMITIVETYPE_DOUBLE:
278 value.d = primitive_unbox_double(o);
281 /* If type is -1 the object is not a primitive box but a
286 vm_abort("primitive_unbox: invalid primitive type %d", type);
293 /* primitive_box_xxx ***********************************************************
295 Box a primitive type.
297 *******************************************************************************/
299 #define PRIMITIVE_BOX_TYPE(name, object, type) \
300 java_handle_t *primitive_box_##name(type value) \
303 java_lang_##object *jo; \
305 o = builtin_new(class_java_lang_##object); \
310 jo = (java_lang_##object *) o; \
312 LLNI_field_set_val(jo, value, value); \
317 PRIMITIVE_BOX_TYPE(boolean, Boolean, int32_t)
318 PRIMITIVE_BOX_TYPE(byte, Byte, int32_t)
319 PRIMITIVE_BOX_TYPE(char, Character, int32_t)
320 PRIMITIVE_BOX_TYPE(short, Short, int32_t)
321 PRIMITIVE_BOX_TYPE(int, Integer, int32_t)
322 PRIMITIVE_BOX_TYPE(long, Long, int64_t)
323 PRIMITIVE_BOX_TYPE(float, Float, float)
324 PRIMITIVE_BOX_TYPE(double, Double, double)
327 /* primitive_unbox_xxx *********************************************************
329 Unbox a primitive type.
331 *******************************************************************************/
333 #define PRIMITIVE_UNBOX_TYPE(name, object, type) \
334 type primitive_unbox_##name(java_handle_t *o) \
336 java_lang_##object *jo; \
339 jo = (java_lang_##object *) o; \
341 LLNI_field_get_val(jo, value, value); \
346 PRIMITIVE_UNBOX_TYPE(boolean, Boolean, int32_t)
347 PRIMITIVE_UNBOX_TYPE(byte, Byte, int32_t)
348 PRIMITIVE_UNBOX_TYPE(char, Character, int32_t)
349 PRIMITIVE_UNBOX_TYPE(short, Short, int32_t)
350 PRIMITIVE_UNBOX_TYPE(int, Integer, int32_t)
351 PRIMITIVE_UNBOX_TYPE(long, Long, int64_t)
352 PRIMITIVE_UNBOX_TYPE(float, Float, float)
353 PRIMITIVE_UNBOX_TYPE(double, Double, double)
357 * These are local overrides for various environment variables in Emacs.
358 * Please do not remove this and leave it at the end of the file, where
359 * Emacs will automagically detect them.
360 * ---------------------------------------------------------------------
363 * indent-tabs-mode: t
367 * vim:noexpandtab:sw=4:ts=4: