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"
35 #include "vm/global.h"
36 #include "vm/primitive.hpp"
39 #include "vmcore/class.h"
40 #include "vmcore/globals.hpp"
41 #include "vmcore/javaobjects.hpp"
42 #include "vmcore/utf8.h"
46 * Returns the primitive class of the given class name.
48 * @param name Name of the class.
50 * @return Class structure.
52 classinfo* Primitive::get_class_by_name(utf *name)
56 /* search table of primitive classes */
58 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
59 if (primitivetype_table[i].name == name)
60 return primitivetype_table[i].class_primitive;
62 /* keep compiler happy */
69 * Returns the primitive class of the given type.
71 * @param type Integer type of the class.
73 * @return Class structure.
75 classinfo* Primitive::get_class_by_type(int type)
77 return primitivetype_table[type].class_primitive;
82 * Returns the primitive class of the given type.
86 * @return Class structure.
88 classinfo* Primitive::get_class_by_char(char ch)
94 index = PRIMITIVETYPE_INT;
97 index = PRIMITIVETYPE_LONG;
100 index = PRIMITIVETYPE_FLOAT;
103 index = PRIMITIVETYPE_DOUBLE;
106 index = PRIMITIVETYPE_BYTE;
109 index = PRIMITIVETYPE_CHAR;
112 index = PRIMITIVETYPE_SHORT;
115 index = PRIMITIVETYPE_BOOLEAN;
118 index = PRIMITIVETYPE_VOID;
124 return primitivetype_table[index].class_primitive;
129 * Returns the primitive array-class of the given primitive class
132 * @param name Name of the class.
134 * @return Class structure.
136 classinfo* Primitive::get_arrayclass_by_name(utf *name)
140 /* search table of primitive classes */
142 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
143 if (primitivetype_table[i].name == name)
144 return primitivetype_table[i].arrayclass;
146 /* keep compiler happy */
153 * Returns the primitive array-class of the given type.
155 * @param type Integer type of the class.
157 * @return Class structure.
159 classinfo* Primitive::get_arrayclass_by_type(int type)
161 return primitivetype_table[type].arrayclass;
166 * Returns the primitive type of the given wrapper-class.
168 * @param c Class structure.
170 * @return Integer type of the class.
172 int Primitive::get_type_by_wrapperclass(classinfo *c)
176 /* Search primitive table. */
178 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
179 if (primitivetype_table[i].class_wrap == c)
182 /* Invalid primitive wrapper-class. */
189 * Box a primitive of the given type. If the type is an object,
192 * @param type Type of the passed value.
193 * @param value Value to box.
195 * @return Handle of the boxing Java object.
197 java_handle_t* Primitive::box(int type, imm_union value)
202 case PRIMITIVETYPE_BOOLEAN:
203 o = box((uint8_t) value.i);
205 case PRIMITIVETYPE_BYTE:
206 o = box((int8_t) value.i);
208 case PRIMITIVETYPE_CHAR:
209 o = box((uint16_t) value.i);
211 case PRIMITIVETYPE_SHORT:
212 o = box((int16_t) value.i);
214 case PRIMITIVETYPE_INT:
217 case PRIMITIVETYPE_LONG:
220 case PRIMITIVETYPE_FLOAT:
223 case PRIMITIVETYPE_DOUBLE:
226 case PRIMITIVETYPE_VOID:
227 o = (java_handle_t*) value.a;
231 vm_abort("primitive_box: invalid primitive type %d", type);
239 * Unbox a primitive of the given type. If the type is an object,
242 * @param h Handle of the Java object.
244 * @return Unboxed value as union.
246 imm_union Primitive::unbox(java_handle_t *h)
257 LLNI_class_get(h, c);
259 type = get_type_by_wrapperclass(c);
262 case PRIMITIVETYPE_BOOLEAN:
263 value.i = unbox_boolean(h);
265 case PRIMITIVETYPE_BYTE:
266 value.i = unbox_byte(h);
268 case PRIMITIVETYPE_CHAR:
269 value.i = unbox_char(h);
271 case PRIMITIVETYPE_SHORT:
272 value.i = unbox_short(h);
274 case PRIMITIVETYPE_INT:
275 value.i = unbox_int(h);
277 case PRIMITIVETYPE_LONG:
278 value.l = unbox_long(h);
280 case PRIMITIVETYPE_FLOAT:
281 value.f = unbox_float(h);
283 case PRIMITIVETYPE_DOUBLE:
284 value.d = unbox_double(h);
287 /* If type is -1 the object is not a primitive box but a
292 vm_abort("Primitive::unbox: invalid primitive type %d", type);
300 * Box a primitive type.
302 java_handle_t* Primitive::box(uint8_t value)
304 java_handle_t *h = builtin_new(class_java_lang_Boolean);
309 java_lang_Boolean b(h);
315 java_handle_t* Primitive::box(int8_t value)
317 java_handle_t *h = builtin_new(class_java_lang_Byte);
328 java_handle_t* Primitive::box(uint16_t value)
330 java_handle_t *h = builtin_new(class_java_lang_Character);
335 java_lang_Character c(h);
341 java_handle_t* Primitive::box(int16_t value)
343 java_handle_t *h = builtin_new(class_java_lang_Short);
348 java_lang_Short s(h);
354 java_handle_t* Primitive::box(int32_t value)
356 java_handle_t *h = builtin_new(class_java_lang_Integer);
361 java_lang_Integer i(h);
367 java_handle_t* Primitive::box(int64_t value)
369 java_handle_t *h = builtin_new(class_java_lang_Long);
380 java_handle_t* Primitive::box(float value)
382 java_handle_t *h = builtin_new(class_java_lang_Float);
387 java_lang_Float f(h);
393 java_handle_t* Primitive::box(double value)
395 java_handle_t *h = builtin_new(class_java_lang_Double);
400 java_lang_Double d(h);
409 * Unbox a primitive type.
412 // template<class T> T Primitive::unbox(java_handle_t *h)
414 // return java_lang_Boolean::get_value(h);
417 inline uint8_t Primitive::unbox_boolean(java_handle_t *h)
419 java_lang_Boolean b(h);
420 return b.get_value();
423 inline int8_t Primitive::unbox_byte(java_handle_t *h)
426 return b.get_value();
429 inline uint16_t Primitive::unbox_char(java_handle_t *h)
431 java_lang_Character c(h);
432 return c.get_value();
435 inline int16_t Primitive::unbox_short(java_handle_t *h)
437 java_lang_Short s(h);
438 return s.get_value();
441 inline int32_t Primitive::unbox_int(java_handle_t *h)
443 java_lang_Integer i(h);
444 return i.get_value();
447 inline int64_t Primitive::unbox_long(java_handle_t *h)
450 return l.get_value();
453 inline float Primitive::unbox_float(java_handle_t *h)
455 java_lang_Float f(h);
456 return f.get_value();
459 inline double Primitive::unbox_double(java_handle_t *h)
461 java_lang_Double d(h);
462 return d.get_value();
467 // Legacy C interface.
471 classinfo* Primitive_get_class_by_name(utf *name) { return Primitive::get_class_by_name(name); }
472 classinfo* Primitive_get_class_by_type(int type) { return Primitive::get_class_by_type(type); }
473 classinfo* Primitive_get_class_by_char(char ch) { return Primitive::get_class_by_char(ch); }
474 classinfo* Primitive_get_arrayclass_by_name(utf *name) { return Primitive::get_arrayclass_by_name(name); }
475 classinfo* Primitive_get_arrayclass_by_type(int type) { return Primitive::get_arrayclass_by_type(type); }
476 int Primitive_get_type_by_wrapperclass(classinfo *c) { return Primitive::get_type_by_wrapperclass(c); }
477 java_handle_t* Primitive_box(int type, imm_union value) { return Primitive::box(type, value); }
478 imm_union Primitive_unbox(java_handle_t *h) { return Primitive::unbox(h); }
483 * These are local overrides for various environment variables in Emacs.
484 * Please do not remove this and leave it at the end of the file, where
485 * Emacs will automagically detect them.
486 * ---------------------------------------------------------------------
489 * indent-tabs-mode: t
493 * vim:noexpandtab:sw=4:ts=4: