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.
197 *******************************************************************************/
199 java_handle_t *primitive_box(int type, imm_union value)
204 case PRIMITIVETYPE_BOOLEAN:
205 o = primitive_box_boolean(value.i);
207 case PRIMITIVETYPE_BYTE:
208 o = primitive_box_byte(value.i);
210 case PRIMITIVETYPE_CHAR:
211 o = primitive_box_char(value.i);
213 case PRIMITIVETYPE_SHORT:
214 o = primitive_box_short(value.i);
216 case PRIMITIVETYPE_INT:
217 o = primitive_box_int(value.i);
219 case PRIMITIVETYPE_LONG:
220 o = primitive_box_long(value.l);
222 case PRIMITIVETYPE_FLOAT:
223 o = primitive_box_float(value.f);
225 case PRIMITIVETYPE_DOUBLE:
226 o = primitive_box_double(value.d);
229 vm_abort("primitive_box: invalid primitive type %d", type);
236 /* primitive_unbox *************************************************************
238 Unbox a primitive of the given type.
240 *******************************************************************************/
242 imm_union primitive_unbox(java_handle_t *o)
250 type = primitive_type_get_by_wrapperclass(c);
253 case PRIMITIVETYPE_BOOLEAN:
254 value.i = primitive_unbox_boolean(o);
256 case PRIMITIVETYPE_BYTE:
257 value.i = primitive_unbox_byte(o);
259 case PRIMITIVETYPE_CHAR:
260 value.i = primitive_unbox_char(o);
262 case PRIMITIVETYPE_SHORT:
263 value.i = primitive_unbox_short(o);
265 case PRIMITIVETYPE_INT:
266 value.i = primitive_unbox_int(o);
268 case PRIMITIVETYPE_LONG:
269 value.l = primitive_unbox_long(o);
271 case PRIMITIVETYPE_FLOAT:
272 value.f = primitive_unbox_float(o);
274 case PRIMITIVETYPE_DOUBLE:
275 value.d = primitive_unbox_double(o);
278 vm_abort("primitive_unbox: invalid primitive type %d", type);
285 /* primitive_box_xxx ***********************************************************
287 Box a primitive type.
289 *******************************************************************************/
291 #define PRIMITIVE_BOX_TYPE(name, object, type) \
292 java_handle_t *primitive_box_##name(type value) \
295 java_lang_##object *jo; \
297 o = builtin_new(class_java_lang_##object); \
302 jo = (java_lang_##object *) o; \
304 LLNI_field_set_val(jo, value, value); \
309 PRIMITIVE_BOX_TYPE(boolean, Boolean, int32_t)
310 PRIMITIVE_BOX_TYPE(byte, Byte, int32_t)
311 PRIMITIVE_BOX_TYPE(char, Character, int32_t)
312 PRIMITIVE_BOX_TYPE(short, Short, int32_t)
313 PRIMITIVE_BOX_TYPE(int, Integer, int32_t)
314 PRIMITIVE_BOX_TYPE(long, Long, int64_t)
315 PRIMITIVE_BOX_TYPE(float, Float, float)
316 PRIMITIVE_BOX_TYPE(double, Double, double)
319 /* primitive_unbox_xxx *********************************************************
321 Unbox a primitive type.
323 *******************************************************************************/
325 #define PRIMITIVE_UNBOX_TYPE(name, object, type) \
326 type primitive_unbox_##name(java_handle_t *o) \
328 java_lang_##object *jo; \
331 jo = (java_lang_##object *) o; \
333 LLNI_field_get_val(jo, value, value); \
338 PRIMITIVE_UNBOX_TYPE(boolean, Boolean, int32_t)
339 PRIMITIVE_UNBOX_TYPE(byte, Byte, int32_t)
340 PRIMITIVE_UNBOX_TYPE(char, Character, int32_t)
341 PRIMITIVE_UNBOX_TYPE(short, Short, int32_t)
342 PRIMITIVE_UNBOX_TYPE(int, Integer, int32_t)
343 PRIMITIVE_UNBOX_TYPE(long, Long, int64_t)
344 PRIMITIVE_UNBOX_TYPE(float, Float, float)
345 PRIMITIVE_UNBOX_TYPE(double, Double, double)
349 * These are local overrides for various environment variables in Emacs.
350 * Please do not remove this and leave it at the end of the file, where
351 * Emacs will automagically detect them.
352 * ---------------------------------------------------------------------
355 * indent-tabs-mode: t
359 * vim:noexpandtab:sw=4:ts=4: