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
25 $Id: linker.c 8042 2007-06-07 17:43:29Z twisti $
35 #include "native/jni.h"
36 #include "native/llni.h"
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"
47 #include "vm/builtin.h"
48 #include "vm/global.h"
49 #include "vm/primitive.h"
52 #include "vmcore/class.h"
53 #include "vmcore/utf8.h"
56 /* primitive_class_get_by_name *************************************************
58 Returns the primitive class of the given class name.
60 *******************************************************************************/
62 classinfo *primitive_class_get_by_name(utf *name)
66 /* search table of primitive classes */
68 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
69 if (primitivetype_table[i].name == name)
70 return primitivetype_table[i].class_primitive;
72 /* keep compiler happy */
78 /* primitive_class_get_by_type *************************************************
80 Returns the primitive class of the given type.
82 *******************************************************************************/
84 classinfo *primitive_class_get_by_type(int type)
86 return primitivetype_table[type].class_primitive;
90 /* primitive_class_get_by_char *************************************************
92 Returns the primitive class of the given type.
94 *******************************************************************************/
96 classinfo *primitive_class_get_by_char(char ch)
102 index = PRIMITIVETYPE_INT;
105 index = PRIMITIVETYPE_LONG;
108 index = PRIMITIVETYPE_FLOAT;
111 index = PRIMITIVETYPE_DOUBLE;
114 index = PRIMITIVETYPE_BYTE;
117 index = PRIMITIVETYPE_CHAR;
120 index = PRIMITIVETYPE_SHORT;
123 index = PRIMITIVETYPE_BOOLEAN;
126 index = PRIMITIVETYPE_VOID;
132 return primitivetype_table[index].class_primitive;
136 /* primitive_arrayclass_get_by_name ********************************************
138 Returns the primitive array-class of the given primitive class
141 *******************************************************************************/
143 classinfo *primitive_arrayclass_get_by_name(utf *name)
147 /* search table of primitive classes */
149 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
150 if (primitivetype_table[i].name == name)
151 return primitivetype_table[i].arrayclass;
153 /* keep compiler happy */
159 /* primitive_arrayclass_get_by_type ********************************************
161 Returns the primitive array-class of the given type.
163 *******************************************************************************/
165 classinfo *primitive_arrayclass_get_by_type(int type)
167 return primitivetype_table[type].arrayclass;
171 /* primitive_type_get_by_wrapperclass ******************************************
173 Returns the primitive type of the given wrapper-class.
175 *******************************************************************************/
177 int primitive_type_get_by_wrapperclass(classinfo *c)
181 /* Search primitive table. */
183 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
184 if (primitivetype_table[i].class_wrap == c)
187 /* Invalid primitive wrapper-class. */
193 /* primitive_box ***************************************************************
195 Box a primitive of the given type. If the type is an object,
198 *******************************************************************************/
200 java_handle_t *primitive_box(int type, imm_union value)
205 case PRIMITIVETYPE_BOOLEAN:
206 o = primitive_box_boolean(value.i);
208 case PRIMITIVETYPE_BYTE:
209 o = primitive_box_byte(value.i);
211 case PRIMITIVETYPE_CHAR:
212 o = primitive_box_char(value.i);
214 case PRIMITIVETYPE_SHORT:
215 o = primitive_box_short(value.i);
217 case PRIMITIVETYPE_INT:
218 o = primitive_box_int(value.i);
220 case PRIMITIVETYPE_LONG:
221 o = primitive_box_long(value.l);
223 case PRIMITIVETYPE_FLOAT:
224 o = primitive_box_float(value.f);
226 case PRIMITIVETYPE_DOUBLE:
227 o = primitive_box_double(value.d);
229 case PRIMITIVETYPE_VOID:
233 vm_abort("primitive_box: invalid primitive type %d", type);
240 /* primitive_unbox *************************************************************
242 Unbox a primitive of the given type. If the type is an object,
245 *******************************************************************************/
247 imm_union primitive_unbox(java_handle_t *o)
255 type = primitive_type_get_by_wrapperclass(c);
258 case PRIMITIVETYPE_BOOLEAN:
259 value.i = primitive_unbox_boolean(o);
261 case PRIMITIVETYPE_BYTE:
262 value.i = primitive_unbox_byte(o);
264 case PRIMITIVETYPE_CHAR:
265 value.i = primitive_unbox_char(o);
267 case PRIMITIVETYPE_SHORT:
268 value.i = primitive_unbox_short(o);
270 case PRIMITIVETYPE_INT:
271 value.i = primitive_unbox_int(o);
273 case PRIMITIVETYPE_LONG:
274 value.l = primitive_unbox_long(o);
276 case PRIMITIVETYPE_FLOAT:
277 value.f = primitive_unbox_float(o);
279 case PRIMITIVETYPE_DOUBLE:
280 value.d = primitive_unbox_double(o);
283 /* If type is -1 the object is not a primitive box but a
288 vm_abort("primitive_unbox: invalid primitive type %d", type);
295 /* primitive_box_xxx ***********************************************************
297 Box a primitive type.
299 *******************************************************************************/
301 #define PRIMITIVE_BOX_TYPE(name, object, type) \
302 java_handle_t *primitive_box_##name(type value) \
305 java_lang_##object *jo; \
307 o = builtin_new(class_java_lang_##object); \
312 jo = (java_lang_##object *) o; \
314 LLNI_field_set_val(jo, value, value); \
319 PRIMITIVE_BOX_TYPE(boolean, Boolean, int32_t)
320 PRIMITIVE_BOX_TYPE(byte, Byte, int32_t)
321 PRIMITIVE_BOX_TYPE(char, Character, int32_t)
322 PRIMITIVE_BOX_TYPE(short, Short, int32_t)
323 PRIMITIVE_BOX_TYPE(int, Integer, int32_t)
324 PRIMITIVE_BOX_TYPE(long, Long, int64_t)
325 PRIMITIVE_BOX_TYPE(float, Float, float)
326 PRIMITIVE_BOX_TYPE(double, Double, double)
329 /* primitive_unbox_xxx *********************************************************
331 Unbox a primitive type.
333 *******************************************************************************/
335 #define PRIMITIVE_UNBOX_TYPE(name, object, type) \
336 type primitive_unbox_##name(java_handle_t *o) \
338 java_lang_##object *jo; \
341 jo = (java_lang_##object *) o; \
343 LLNI_field_get_val(jo, value, value); \
348 PRIMITIVE_UNBOX_TYPE(boolean, Boolean, int32_t)
349 PRIMITIVE_UNBOX_TYPE(byte, Byte, int32_t)
350 PRIMITIVE_UNBOX_TYPE(char, Character, int32_t)
351 PRIMITIVE_UNBOX_TYPE(short, Short, int32_t)
352 PRIMITIVE_UNBOX_TYPE(int, Integer, int32_t)
353 PRIMITIVE_UNBOX_TYPE(long, Long, int64_t)
354 PRIMITIVE_UNBOX_TYPE(float, Float, float)
355 PRIMITIVE_UNBOX_TYPE(double, Double, double)
359 * These are local overrides for various environment variables in Emacs.
360 * Please do not remove this and leave it at the end of the file, where
361 * Emacs will automagically detect them.
362 * ---------------------------------------------------------------------
365 * indent-tabs-mode: t
369 * vim:noexpandtab:sw=4:ts=4: