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 ATTENTION: This function has to be used outside the nativeworld.
338 *******************************************************************************/
340 static void argument_vmarray_store_adr(uint64_t *array, paramdesc *pd, java_handle_t *h)
345 /* Take the reference value out of the handle. */
347 value = LLNI_UNWRAP(h);
350 #if defined(HAS_ADDRESS_REGISTER_FILE)
351 /* When the architecture has address registers, place them
352 after integer and float registers. */
354 index = INT_ARG_CNT + FLT_ARG_CNT + pd->index;
358 array[index] = (uint64_t) (intptr_t) value;
361 index = ARG_CNT + pd->index;
362 #if SIZEOF_VOID_P == 8
363 array[index] = (uint64_t) (intptr_t) value;
365 # if WORDS_BIGENDIAN == 1
366 array[index] = ((uint64_t) (intptr_t) value) << 32;
368 array[index] = (uint64_t) (intptr_t) value;
375 /* argument_vmarray_from_valist ************************************************
379 ATTENTION: This function has to be used outside the native world.
381 *******************************************************************************/
383 uint64_t *argument_vmarray_from_valist(methodinfo *m, java_handle_t *o, va_list ap)
392 /* get the descriptors */
398 /* allocate argument array */
400 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
402 /* if method is non-static fill first block and skip `this' pointer */
407 /* the `this' pointer */
408 argument_vmarray_store_adr(array, pd, o);
415 for (; i < md->paramcount; i++, pd++, td++) {
418 value.i = va_arg(ap, int32_t);
419 argument_vmarray_store_int(array, pd, value.i);
423 value.l = va_arg(ap, int64_t);
424 argument_vmarray_store_lng(array, pd, value.l);
428 #if defined(__ALPHA__) || defined(__POWERPC__) || defined(__POWERPC64__)
429 /* This is required to load the correct float value in
432 value.d = (double) va_arg(ap, double);
434 value.f = (float) va_arg(ap, double);
436 argument_vmarray_store_flt(array, pd, value.l);
440 value.d = va_arg(ap, double);
441 argument_vmarray_store_dbl(array, pd, value.l);
445 value.a = va_arg(ap, void*);
446 argument_vmarray_store_adr(array, pd, value.a);
455 /* argument_vmarray_from_jvalue ************************************************
459 ATTENTION: This function has to be used outside the native world.
461 *******************************************************************************/
463 uint64_t *argument_vmarray_from_jvalue(methodinfo *m, java_handle_t *o,
473 /* get the descriptors */
479 /* allocate argument array */
481 #if defined(HAS_ADDRESS_REGISTER_FILE)
482 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + ADR_ARG_CNT + md->memuse);
484 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
487 /* if method is non-static fill first block and skip `this' pointer */
492 /* the `this' pointer */
493 argument_vmarray_store_adr(array, pd, o);
500 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
501 switch (td->decltype) {
503 argument_vmarray_store_int(array, pd, args[j].i);
507 argument_vmarray_store_lng(array, pd, args[j].j);
511 argument_vmarray_store_flt(array, pd, args[j].j);
515 argument_vmarray_store_dbl(array, pd, args[j].j);
519 argument_vmarray_store_adr(array, pd, args[j].l);
528 /* argument_vmarray_from_objectarray *******************************************
532 ATTENTION: This function has to be used outside the native world.
534 *******************************************************************************/
536 uint64_t *argument_vmarray_from_objectarray(methodinfo *m, java_handle_t *o,
537 java_handle_objectarray_t *params)
543 java_handle_t *param;
550 /* get the descriptors */
556 /* allocate argument array */
558 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
560 /* if method is non-static fill first block and skip `this' pointer */
566 argument_vmarray_store_adr(array, pd, o);
573 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
574 LLNI_objectarray_element_get(params, j, param);
579 exceptions_throw_illegalargumentexception();
583 /* convert the value according to its declared type */
585 LLNI_class_get(param, c);
586 type = primitive_type_get_by_wrapperclass(c);
588 switch (td->decltype) {
589 case PRIMITIVETYPE_BOOLEAN:
591 case PRIMITIVETYPE_BOOLEAN:
592 /* This type is OK. */
595 exceptions_throw_illegalargumentexception();
600 case PRIMITIVETYPE_BYTE:
602 case PRIMITIVETYPE_BYTE:
603 /* This type is OK. */
606 exceptions_throw_illegalargumentexception();
611 case PRIMITIVETYPE_CHAR:
613 case PRIMITIVETYPE_CHAR:
614 /* This type is OK. */
617 exceptions_throw_illegalargumentexception();
622 case PRIMITIVETYPE_SHORT:
624 case PRIMITIVETYPE_BYTE:
625 case PRIMITIVETYPE_SHORT:
626 /* These types are OK. */
629 exceptions_throw_illegalargumentexception();
634 case PRIMITIVETYPE_INT:
636 case PRIMITIVETYPE_BYTE:
637 case PRIMITIVETYPE_SHORT:
638 case PRIMITIVETYPE_INT:
639 /* These types are OK. */
642 exceptions_throw_illegalargumentexception();
648 vm_abort("argument_vmarray_from_objectarray: invalid type %d",
652 value = primitive_unbox(param);
653 argument_vmarray_store_int(array, pd, value.i);
658 exceptions_throw_illegalargumentexception();
662 LLNI_class_get(param, c);
663 type = primitive_type_get_by_wrapperclass(c);
665 assert(td->decltype == PRIMITIVETYPE_LONG);
668 case PRIMITIVETYPE_BYTE:
669 case PRIMITIVETYPE_SHORT:
670 case PRIMITIVETYPE_INT:
671 case PRIMITIVETYPE_LONG:
672 /* These types are OK. */
675 exceptions_throw_illegalargumentexception();
679 value = primitive_unbox(param);
680 argument_vmarray_store_lng(array, pd, value.l);
685 exceptions_throw_illegalargumentexception();
689 LLNI_class_get(param, c);
690 type = primitive_type_get_by_wrapperclass(c);
692 assert(td->decltype == PRIMITIVETYPE_FLOAT);
695 case PRIMITIVETYPE_FLOAT:
696 /* This type is OK. */
699 exceptions_throw_illegalargumentexception();
703 value = primitive_unbox(param);
704 argument_vmarray_store_flt(array, pd, value.l);
709 exceptions_throw_illegalargumentexception();
713 LLNI_class_get(param, c);
714 type = primitive_type_get_by_wrapperclass(c);
716 assert(td->decltype == PRIMITIVETYPE_DOUBLE);
719 case PRIMITIVETYPE_FLOAT:
720 case PRIMITIVETYPE_DOUBLE:
721 /* These types are OK. */
724 exceptions_throw_illegalargumentexception();
728 value = primitive_unbox(param);
729 argument_vmarray_store_dbl(array, pd, value.l);
733 if (!resolve_class_from_typedesc(td, true, true, &c))
737 if (td->arraydim > 0) {
738 if (!builtin_arrayinstanceof(param, c)) {
739 exceptions_throw_illegalargumentexception();
744 if (!builtin_instanceof(param, c)) {
745 exceptions_throw_illegalargumentexception();
751 argument_vmarray_store_adr(array, pd, param);
755 vm_abort("argument_vmarray_from_objectarray: invalid type %d", td->type);
764 * These are local overrides for various environment variables in Emacs.
765 * Please do not remove this and leave it at the end of the file, where
766 * Emacs will automagically detect them.
767 * ---------------------------------------------------------------------
770 * indent-tabs-mode: t
774 * vim:noexpandtab:sw=4:ts=4: