1 /* src/vm/primitive.cpp - 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 "vm/builtin.h"
36 #include "vm/global.h"
37 #include "vm/globals.hpp"
38 #include "vm/javaobjects.hpp"
39 #include "vm/primitive.hpp"
45 * Returns the primitive class of the given class name.
47 * @param name Name of the class.
49 * @return Class structure.
51 classinfo* Primitive::get_class_by_name(utf *name)
55 /* search table of primitive classes */
57 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
58 if (primitivetype_table[i].name == name)
59 return primitivetype_table[i].class_primitive;
61 /* keep compiler happy */
68 * Returns the primitive class of the given type.
70 * @param type Integer type of the class.
72 * @return Class structure.
74 classinfo* Primitive::get_class_by_type(int type)
76 return primitivetype_table[type].class_primitive;
81 * Returns the primitive class of the given type.
85 * @return Class structure.
87 classinfo* Primitive::get_class_by_char(char ch)
93 index = PRIMITIVETYPE_INT;
96 index = PRIMITIVETYPE_LONG;
99 index = PRIMITIVETYPE_FLOAT;
102 index = PRIMITIVETYPE_DOUBLE;
105 index = PRIMITIVETYPE_BYTE;
108 index = PRIMITIVETYPE_CHAR;
111 index = PRIMITIVETYPE_SHORT;
114 index = PRIMITIVETYPE_BOOLEAN;
117 index = PRIMITIVETYPE_VOID;
123 return primitivetype_table[index].class_primitive;
128 * Returns the primitive array-class of the given primitive class
131 * @param name Name of the class.
133 * @return Class structure.
135 classinfo* Primitive::get_arrayclass_by_name(utf *name)
139 /* search table of primitive classes */
141 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
142 if (primitivetype_table[i].name == name)
143 return primitivetype_table[i].arrayclass;
145 /* keep compiler happy */
152 * Returns the primitive array-class of the given type.
154 * @param type Integer type of the class.
156 * @return Class structure.
158 classinfo* Primitive::get_arrayclass_by_type(int type)
160 return primitivetype_table[type].arrayclass;
165 * Returns the primitive type of the given wrapper-class.
167 * @param c Class structure.
169 * @return Integer type of the class.
171 int Primitive::get_type_by_wrapperclass(classinfo *c)
175 /* Search primitive table. */
177 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
178 if (primitivetype_table[i].class_wrap == c)
181 /* Invalid primitive wrapper-class. */
188 * Box a primitive of the given type. If the type is an object,
191 * @param type Type of the passed value.
192 * @param value Value to box.
194 * @return Handle of the boxing Java object.
196 java_handle_t* Primitive::box(int type, imm_union value)
201 case PRIMITIVETYPE_BOOLEAN:
202 o = box((uint8_t) value.i);
204 case PRIMITIVETYPE_BYTE:
205 o = box((int8_t) value.i);
207 case PRIMITIVETYPE_CHAR:
208 o = box((uint16_t) value.i);
210 case PRIMITIVETYPE_SHORT:
211 o = box((int16_t) value.i);
213 case PRIMITIVETYPE_INT:
216 case PRIMITIVETYPE_LONG:
219 case PRIMITIVETYPE_FLOAT:
222 case PRIMITIVETYPE_DOUBLE:
225 case PRIMITIVETYPE_VOID:
226 o = (java_handle_t*) value.a;
230 vm_abort("primitive_box: invalid primitive type %d", type);
238 * Unbox a primitive of the given type. If the type is an object,
241 * @param h Handle of the Java object.
243 * @return Unboxed value as union.
245 imm_union Primitive::unbox(java_handle_t *h)
256 LLNI_class_get(h, c);
258 type = get_type_by_wrapperclass(c);
261 case PRIMITIVETYPE_BOOLEAN:
262 value.i = unbox_boolean(h);
264 case PRIMITIVETYPE_BYTE:
265 value.i = unbox_byte(h);
267 case PRIMITIVETYPE_CHAR:
268 value.i = unbox_char(h);
270 case PRIMITIVETYPE_SHORT:
271 value.i = unbox_short(h);
273 case PRIMITIVETYPE_INT:
274 value.i = unbox_int(h);
276 case PRIMITIVETYPE_LONG:
277 value.l = unbox_long(h);
279 case PRIMITIVETYPE_FLOAT:
280 value.f = unbox_float(h);
282 case PRIMITIVETYPE_DOUBLE:
283 value.d = unbox_double(h);
286 /* If type is -1 the object is not a primitive box but a
291 vm_abort("Primitive::unbox: invalid primitive type %d", type);
299 * Box a primitive type.
301 java_handle_t* Primitive::box(uint8_t value)
303 java_handle_t *h = builtin_new(class_java_lang_Boolean);
308 java_lang_Boolean b(h);
314 java_handle_t* Primitive::box(int8_t value)
316 java_handle_t *h = builtin_new(class_java_lang_Byte);
327 java_handle_t* Primitive::box(uint16_t value)
329 java_handle_t *h = builtin_new(class_java_lang_Character);
334 java_lang_Character c(h);
340 java_handle_t* Primitive::box(int16_t value)
342 java_handle_t *h = builtin_new(class_java_lang_Short);
347 java_lang_Short s(h);
353 java_handle_t* Primitive::box(int32_t value)
355 java_handle_t *h = builtin_new(class_java_lang_Integer);
360 java_lang_Integer i(h);
366 java_handle_t* Primitive::box(int64_t value)
368 java_handle_t *h = builtin_new(class_java_lang_Long);
379 java_handle_t* Primitive::box(float value)
381 java_handle_t *h = builtin_new(class_java_lang_Float);
386 java_lang_Float f(h);
392 java_handle_t* Primitive::box(double value)
394 java_handle_t *h = builtin_new(class_java_lang_Double);
399 java_lang_Double d(h);
408 * Unbox a primitive type.
411 // template<class T> T Primitive::unbox(java_handle_t *h)
413 // return java_lang_Boolean::get_value(h);
416 inline uint8_t Primitive::unbox_boolean(java_handle_t *h)
418 java_lang_Boolean b(h);
419 return b.get_value();
422 inline int8_t Primitive::unbox_byte(java_handle_t *h)
425 return b.get_value();
428 inline uint16_t Primitive::unbox_char(java_handle_t *h)
430 java_lang_Character c(h);
431 return c.get_value();
434 inline int16_t Primitive::unbox_short(java_handle_t *h)
436 java_lang_Short s(h);
437 return s.get_value();
440 inline int32_t Primitive::unbox_int(java_handle_t *h)
442 java_lang_Integer i(h);
443 return i.get_value();
446 inline int64_t Primitive::unbox_long(java_handle_t *h)
449 return l.get_value();
452 inline float Primitive::unbox_float(java_handle_t *h)
454 java_lang_Float f(h);
455 return f.get_value();
458 inline double Primitive::unbox_double(java_handle_t *h)
460 java_lang_Double d(h);
461 return d.get_value();
466 // Legacy C interface.
470 classinfo* Primitive_get_class_by_name(utf *name) { return Primitive::get_class_by_name(name); }
471 classinfo* Primitive_get_class_by_type(int type) { return Primitive::get_class_by_type(type); }
472 classinfo* Primitive_get_class_by_char(char ch) { return Primitive::get_class_by_char(ch); }
473 classinfo* Primitive_get_arrayclass_by_name(utf *name) { return Primitive::get_arrayclass_by_name(name); }
474 classinfo* Primitive_get_arrayclass_by_type(int type) { return Primitive::get_arrayclass_by_type(type); }
475 int Primitive_get_type_by_wrapperclass(classinfo *c) { return Primitive::get_type_by_wrapperclass(c); }
476 java_handle_t* Primitive_box(int type, imm_union value) { return Primitive::box(type, value); }
477 imm_union Primitive_unbox(java_handle_t *h) { return Primitive::unbox(h); }
482 * These are local overrides for various environment variables in Emacs.
483 * Please do not remove this and leave it at the end of the file, where
484 * Emacs will automagically detect them.
485 * ---------------------------------------------------------------------
488 * indent-tabs-mode: t
492 * vim:noexpandtab:sw=4:ts=4: