1 /* src/vm/jit/argument.c - argument passing from and to JIT methods
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
35 #include "mm/memory.h"
37 #include "native/llni.h"
39 #include "vm/exceptions.h"
40 #include "vm/global.h"
41 #include "vm/primitive.h"
42 #include "vm/resolve.h"
45 #include "vm/jit/abi-asm.h"
47 #include "vmcore/descriptor.h"
48 #include "vmcore/method.h"
51 /* argument_jitarray_load ******************************************************
53 Returns the argument specified by index from one of the passed arrays
56 *******************************************************************************/
58 imm_union argument_jitarray_load(methoddesc *md, int32_t index,
59 uint64_t *arg_regs, uint64_t *stack)
64 pd = &md->params[index];
66 switch (md->paramtypes[index].type) {
70 #if (SIZEOF_VOID_P == 8)
71 ret.l = (int64_t)stack[pd->index];
73 ret.l = *(int32_t *)(stack + pd->index);
76 #if (SIZEOF_VOID_P == 8)
77 ret.l = arg_regs[index];
79 ret.l = *(int32_t *)(arg_regs + index);
85 ret.l = (int64_t)stack[pd->index];
87 ret.l = (int64_t)arg_regs[index];
92 ret.l = (int64_t)stack[pd->index];
94 ret.l = (int64_t)arg_regs[index];
99 ret.l = (int64_t)stack[pd->index];
101 ret.l = (int64_t)arg_regs[index];
110 /* argument_jitarray_store *****************************************************
112 Stores the argument into one of the passed arrays at a slot specified
115 *******************************************************************************/
117 void argument_jitarray_store(methoddesc *md, int32_t index,
118 uint64_t *arg_regs, uint64_t *stack,
123 pd = &md->params[index];
125 switch (md->paramtypes[index].type) {
128 #if (SIZEOF_VOID_P == 8)
129 stack[pd->index] = param.l;
134 arg_regs[index] = param.l;
138 vm_abort("argument_jitarray_store: type not implemented");
144 /* argument_jitreturn_load *****************************************************
146 Loads the proper return value form the return register and returns it.
148 *******************************************************************************/
150 imm_union argument_jitreturn_load(methoddesc *md, uint64_t *return_regs)
154 switch (md->returntype.type) {
157 #if (SIZEOF_VOID_P == 8)
158 ret.l = return_regs[0];
160 ret.l = *(int32_t *)return_regs;
164 ret.l = *(int64_t *)return_regs;
167 ret.l = *(int64_t *)return_regs;
170 ret.l = *(int64_t *)return_regs;
178 /* argument_jitreturn_store ****************************************************
180 Stores the proper return value into the return registers.
182 *******************************************************************************/
184 void argument_jitreturn_store(methoddesc *md, uint64_t *return_regs, imm_union ret)
186 switch (md->returntype.type) {
188 #if (SIZEOF_VOID_P == 8)
189 return_regs[0] = ret.l;
195 vm_abort("argument_jitreturn_store: type not implemented");
201 /* argument_vmarray_store_int **************************************************
203 Helper function to store an integer into the argument array, taking
204 care of architecture specific issues.
206 *******************************************************************************/
208 static void argument_vmarray_store_int(uint64_t *array, paramdesc *pd, int32_t value)
214 array[index] = (int64_t) value;
217 index = ARG_CNT + pd->index;
218 #if SIZEOF_VOID_P == 8
219 array[index] = (int64_t) value;
221 # if WORDS_BIGENDIAN == 1
222 array[index] = ((int64_t) value) << 32;
224 array[index] = (int64_t) value;
231 /* argument_vmarray_store_lng **************************************************
233 Helper function to store a long into the argument array, taking
234 care of architecture specific issues.
236 *******************************************************************************/
238 static void argument_vmarray_store_lng(uint64_t *array, paramdesc *pd, int64_t value)
242 #if SIZEOF_VOID_P == 8
246 index = ARG_CNT + pd->index;
248 array[index] = value;
251 /* move low and high 32-bits into it's own argument slot */
253 index = GET_LOW_REG(pd->index);
254 array[index] = value & 0x00000000ffffffff;
256 index = GET_HIGH_REG(pd->index);
257 array[index] = value >> 32;
260 index = ARG_CNT + pd->index;
261 array[index] = value;
267 /* argument_vmarray_store_flt **************************************************
269 Helper function to store a float into the argument array, taking
270 care of architecture specific issues.
272 *******************************************************************************/
274 static void argument_vmarray_store_flt(uint64_t *array, paramdesc *pd, uint64_t value)
279 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
282 index = INT_ARG_CNT + pd->index;
284 #if WORDS_BIGENDIAN == 1 && !defined(__POWERPC__) && !defined(__POWERPC64__) && !defined(__S390__)
285 array[index] = value >> 32;
287 array[index] = value;
291 index = ARG_CNT + pd->index;
292 #if defined(__SPARC_64__)
293 array[index] = value >> 32;
295 array[index] = value;
301 /* argument_vmarray_store_dbl **************************************************
303 Helper function to store a double into the argument array, taking
304 care of architecture specific issues.
306 *******************************************************************************/
308 static void argument_vmarray_store_dbl(uint64_t *array, paramdesc *pd, uint64_t value)
313 #if SIZEOF_VOID_P != 8 && defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
314 index = GET_LOW_REG(pd->index);
315 array[index] = value & 0x00000000ffffffff;
317 index = GET_HIGH_REG(pd->index);
318 array[index] = value >> 32;
320 index = INT_ARG_CNT + pd->index;
321 array[index] = value;
325 index = ARG_CNT + pd->index;
326 array[index] = value;
331 /* argument_vmarray_store_adr **************************************************
333 Helper function to store an address into the argument array, taking
334 care of architecture specific issues.
336 *******************************************************************************/
338 static void argument_vmarray_store_adr(uint64_t *array, paramdesc *pd, void *value)
343 #if defined(HAS_ADDRESS_REGISTER_FILE)
344 /* When the architecture has address registers, place them
345 after integer and float registers. */
347 index = INT_ARG_CNT + FLT_ARG_CNT + pd->index;
351 array[index] = (uint64_t) (intptr_t) value;
354 index = ARG_CNT + pd->index;
355 #if SIZEOF_VOID_P == 8
356 array[index] = (uint64_t) (intptr_t) value;
358 # if WORDS_BIGENDIAN == 1
359 array[index] = ((uint64_t) (intptr_t) value) << 32;
361 array[index] = (uint64_t) (intptr_t) value;
368 /* argument_vmarray_from_valist ************************************************
372 *******************************************************************************/
374 uint64_t *argument_vmarray_from_valist(methodinfo *m, java_handle_t *o, va_list ap)
383 /* get the descriptors */
389 /* allocate argument array */
391 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
393 /* if method is non-static fill first block and skip `this' pointer */
398 /* the `this' pointer */
399 argument_vmarray_store_adr(array, pd, o);
406 for (; i < md->paramcount; i++, pd++, td++) {
409 value.i = va_arg(ap, int32_t);
410 argument_vmarray_store_int(array, pd, value.i);
414 value.l = va_arg(ap, int64_t);
415 argument_vmarray_store_lng(array, pd, value.l);
419 #if defined(__ALPHA__) || defined(__POWERPC__) || defined(__POWERPC64__)
420 /* This is required to load the correct float value in
423 value.d = (double) va_arg(ap, double);
425 value.f = (float) va_arg(ap, double);
427 argument_vmarray_store_flt(array, pd, value.l);
431 value.d = va_arg(ap, double);
432 argument_vmarray_store_dbl(array, pd, value.l);
436 value.a = va_arg(ap, void*);
437 argument_vmarray_store_adr(array, pd, value.a);
446 /* argument_vmarray_from_jvalue ************************************************
450 *******************************************************************************/
452 uint64_t *argument_vmarray_from_jvalue(methodinfo *m, java_handle_t *o,
462 /* get the descriptors */
468 /* allocate argument array */
470 #if defined(HAS_ADDRESS_REGISTER_FILE)
471 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + ADR_ARG_CNT + md->memuse);
473 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
476 /* if method is non-static fill first block and skip `this' pointer */
481 /* the `this' pointer */
482 argument_vmarray_store_adr(array, pd, o);
489 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
490 switch (td->decltype) {
492 argument_vmarray_store_int(array, pd, args[j].i);
496 argument_vmarray_store_lng(array, pd, args[j].j);
500 argument_vmarray_store_flt(array, pd, args[j].j);
504 argument_vmarray_store_dbl(array, pd, args[j].j);
508 argument_vmarray_store_adr(array, pd, args[j].l);
517 /* argument_vmarray_from_objectarray *******************************************
521 *******************************************************************************/
523 uint64_t *argument_vmarray_from_objectarray(methodinfo *m, java_handle_t *o,
524 java_handle_objectarray_t *params)
530 java_handle_t *param;
537 /* get the descriptors */
543 /* allocate argument array */
545 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
547 /* if method is non-static fill first block and skip `this' pointer */
553 argument_vmarray_store_adr(array, pd, o);
560 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
561 LLNI_objectarray_element_get(params, j, param);
566 exceptions_throw_illegalargumentexception();
570 /* convert the value according to its declared type */
572 LLNI_class_get(param, c);
573 type = primitive_type_get_by_wrapperclass(c);
575 switch (td->decltype) {
576 case PRIMITIVETYPE_BOOLEAN:
578 case PRIMITIVETYPE_BOOLEAN:
579 /* This type is OK. */
582 exceptions_throw_illegalargumentexception();
587 case PRIMITIVETYPE_BYTE:
589 case PRIMITIVETYPE_BYTE:
590 /* This type is OK. */
593 exceptions_throw_illegalargumentexception();
598 case PRIMITIVETYPE_CHAR:
600 case PRIMITIVETYPE_CHAR:
601 /* This type is OK. */
604 exceptions_throw_illegalargumentexception();
609 case PRIMITIVETYPE_SHORT:
611 case PRIMITIVETYPE_BYTE:
612 case PRIMITIVETYPE_SHORT:
613 /* These types are OK. */
616 exceptions_throw_illegalargumentexception();
621 case PRIMITIVETYPE_INT:
623 case PRIMITIVETYPE_BYTE:
624 case PRIMITIVETYPE_SHORT:
625 case PRIMITIVETYPE_INT:
626 /* These types are OK. */
629 exceptions_throw_illegalargumentexception();
635 vm_abort("argument_vmarray_from_objectarray: invalid type %d",
639 value = primitive_unbox(param);
640 argument_vmarray_store_int(array, pd, value.i);
645 exceptions_throw_illegalargumentexception();
649 LLNI_class_get(param, c);
650 type = primitive_type_get_by_wrapperclass(c);
652 assert(td->decltype == PRIMITIVETYPE_LONG);
655 case PRIMITIVETYPE_BYTE:
656 case PRIMITIVETYPE_SHORT:
657 case PRIMITIVETYPE_INT:
658 case PRIMITIVETYPE_LONG:
659 /* These types are OK. */
662 exceptions_throw_illegalargumentexception();
666 value = primitive_unbox(param);
667 argument_vmarray_store_lng(array, pd, value.l);
672 exceptions_throw_illegalargumentexception();
676 LLNI_class_get(param, c);
677 type = primitive_type_get_by_wrapperclass(c);
679 assert(td->decltype == PRIMITIVETYPE_FLOAT);
682 case PRIMITIVETYPE_FLOAT:
683 /* This type is OK. */
686 exceptions_throw_illegalargumentexception();
690 value = primitive_unbox(param);
691 argument_vmarray_store_flt(array, pd, value.l);
696 exceptions_throw_illegalargumentexception();
700 LLNI_class_get(param, c);
701 type = primitive_type_get_by_wrapperclass(c);
703 assert(td->decltype == PRIMITIVETYPE_DOUBLE);
706 case PRIMITIVETYPE_FLOAT:
707 case PRIMITIVETYPE_DOUBLE:
708 /* These types are OK. */
711 exceptions_throw_illegalargumentexception();
715 value = primitive_unbox(param);
716 argument_vmarray_store_dbl(array, pd, value.l);
720 if (!resolve_class_from_typedesc(td, true, true, &c))
724 if (td->arraydim > 0) {
725 if (!builtin_arrayinstanceof(param, c)) {
726 exceptions_throw_illegalargumentexception();
731 if (!builtin_instanceof(param, c)) {
732 exceptions_throw_illegalargumentexception();
738 argument_vmarray_store_adr(array, pd, param);
742 vm_abort("argument_vmarray_from_objectarray: invalid type %d", td->type);
751 * These are local overrides for various environment variables in Emacs.
752 * Please do not remove this and leave it at the end of the file, where
753 * Emacs will automagically detect them.
754 * ---------------------------------------------------------------------
757 * indent-tabs-mode: t
761 * vim:noexpandtab:sw=4:ts=4: