* src/vm/jit/argument.c (argument_vmarray_store_int),
[cacao.git] / src / vm / jit / argument.c
1 /* src/vm/jit/argument.c - argument passing from and to JIT methods
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32
33 #include "arch.h"
34
35 #include "mm/memory.h"
36
37 #include "native/llni.h"
38
39 #include "vm/exceptions.h"
40 #include "vm/global.h"
41 #include "vm/primitive.h"
42 #include "vm/resolve.h"
43 #include "vm/vm.h"
44
45 #include "vm/jit/abi-asm.h"
46
47 #include "vmcore/descriptor.h"
48 #include "vmcore/method.h"
49
50
51 /* argument_jitarray_load ******************************************************
52  
53    Returns the argument specified by index from one of the passed arrays
54    and returns it.
55
56 *******************************************************************************/
57
58 imm_union argument_jitarray_load(methoddesc *md, int32_t index,
59                                                                  uint64_t *arg_regs, uint64_t *stack)
60 {
61         imm_union  ret;
62         paramdesc *pd;
63
64         pd = &md->params[index];
65
66         switch (md->paramtypes[index].type) {
67                 case TYPE_INT:
68                 case TYPE_ADR:
69                         if (pd->inmemory) {
70 #if (SIZEOF_VOID_P == 8)
71                                 ret.l = (int64_t)stack[pd->index];
72 #else
73                                 ret.l = *(int32_t *)(stack + pd->index);
74 #endif
75                         } else {
76 #if (SIZEOF_VOID_P == 8)
77                                 ret.l = arg_regs[index];
78 #else
79                                 ret.l = *(int32_t *)(arg_regs + index);
80 #endif
81                         }
82                         break;
83                 case TYPE_LNG:
84                         if (pd->inmemory) {
85                                 ret.l = (int64_t)stack[pd->index];
86                         } else {
87                                 ret.l = (int64_t)arg_regs[index];
88                         }
89                         break;
90                 case TYPE_FLT:
91                         if (pd->inmemory) {
92                                 ret.l = (int64_t)stack[pd->index];
93                         } else {
94                                 ret.l = (int64_t)arg_regs[index];
95                         }
96                         break;
97                 case TYPE_DBL:
98                         if (pd->inmemory) {
99                                 ret.l = (int64_t)stack[pd->index];
100                         } else {
101                                 ret.l = (int64_t)arg_regs[index];
102                         }
103                         break;
104         }
105
106         return ret;
107 }
108
109
110 /* argument_jitarray_store *****************************************************
111  
112    Stores the argument into one of the passed arrays at a slot specified
113    by index.
114
115 *******************************************************************************/
116
117 void argument_jitarray_store(methoddesc *md, int32_t index,
118                                                          uint64_t *arg_regs, uint64_t *stack,
119                                                          imm_union param)
120 {
121         paramdesc *pd;
122
123         pd = &md->params[index];
124
125         switch (md->paramtypes[index].type) {
126                 case TYPE_ADR:
127                         if (pd->inmemory) {
128 #if (SIZEOF_VOID_P == 8)
129                                 stack[pd->index] = param.l;
130 #else
131                                 assert(0);
132 #endif
133                         } else {
134                                 arg_regs[index] = param.l;
135                         }
136                         break;
137                 default:
138                         vm_abort("argument_jitarray_store: type not implemented");
139                         break;
140         }
141 }
142
143
144 /* argument_jitreturn_load *****************************************************
145
146    Loads the proper return value form the return register and returns it.
147
148 *******************************************************************************/
149
150 imm_union argument_jitreturn_load(methoddesc *md, uint64_t *return_regs)
151 {
152         imm_union ret;
153
154         switch (md->returntype.type) {
155                 case TYPE_INT:
156                 case TYPE_ADR:
157 #if (SIZEOF_VOID_P == 8)
158                         ret.l = return_regs[0];
159 #else
160                         ret.l = *(int32_t *)return_regs;
161 #endif
162                         break;
163                 case TYPE_LNG:
164                         ret.l = *(int64_t *)return_regs;
165                         break;
166                 case TYPE_FLT:
167                         ret.l = *(int64_t *)return_regs;
168                         break;
169                 case TYPE_DBL:
170                         ret.l = *(int64_t *)return_regs;
171                         break;
172         }
173
174         return ret;
175 }
176
177
178 /* argument_jitreturn_store ****************************************************
179
180    Stores the proper return value into the return registers.
181
182 *******************************************************************************/
183
184 void argument_jitreturn_store(methoddesc *md, uint64_t *return_regs, imm_union ret)
185 {
186         switch (md->returntype.type) {
187                 case TYPE_ADR:
188 #if (SIZEOF_VOID_P == 8)
189                         return_regs[0] = ret.l;
190 #else
191                         assert(0);
192 #endif
193                         break;
194                 default:
195                         vm_abort("argument_jitreturn_store: type not implemented");
196                         break;
197         }
198 }
199
200
201 /* argument_vmarray_store_int **************************************************
202
203    Helper function to store an integer into the argument array, taking
204    care of architecture specific issues.
205
206 *******************************************************************************/
207
208 static void argument_vmarray_store_int(uint64_t *array, paramdesc *pd, int32_t value)
209 {
210         int32_t index;
211
212         if (!pd->inmemory) {
213                 index        = pd->index;
214                 array[index] = (int64_t) value;
215         }
216         else {
217                 index        = ARG_CNT + pd->index;
218 #if SIZEOF_VOID_P == 8
219                 array[index] = (int64_t) value;
220 #else
221 # if WORDS_BIGENDIAN == 1
222                 array[index] = ((int64_t) value) << 32;
223 # else
224                 array[index] = (int64_t) value;
225 # endif
226 #endif
227         }
228 }
229
230
231 /* argument_vmarray_store_lng **************************************************
232
233    Helper function to store a long into the argument array, taking
234    care of architecture specific issues.
235
236 *******************************************************************************/
237
238 static void argument_vmarray_store_lng(uint64_t *array, paramdesc *pd, int64_t value)
239 {
240         int32_t index;
241
242 #if SIZEOF_VOID_P == 8
243         if (!pd->inmemory)
244                 index = pd->index;
245         else
246                 index = ARG_CNT + pd->index;
247
248         array[index] = value;
249 #else
250         if (!pd->inmemory) {
251                 /* move low and high 32-bits into it's own argument slot */
252
253                 index        = GET_LOW_REG(pd->index);
254                 array[index] = value & 0x00000000ffffffff;
255
256                 index        = GET_HIGH_REG(pd->index);
257                 array[index] = value >> 32;
258         }
259         else {
260                 index        = ARG_CNT + pd->index;
261                 array[index] = value;
262         }
263 #endif
264 }
265
266
267 /* argument_vmarray_store_flt **************************************************
268
269    Helper function to store a float into the argument array, taking
270    care of architecture specific issues.
271
272 *******************************************************************************/
273
274 static void argument_vmarray_store_flt(uint64_t *array, paramdesc *pd, uint64_t value)
275 {
276         int32_t index;
277
278         if (!pd->inmemory) {
279 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
280                 index        = pd->index;
281 #else
282                 index        = INT_ARG_CNT + pd->index;
283 #endif
284 #if WORDS_BIGENDIAN == 1 && !defined(__POWERPC__) && !defined(__POWERPC64__) && !defined(__S390__)
285                 array[index] = value >> 32;
286 #else
287                 array[index] = value;
288 #endif
289         }
290         else {
291                 index        = ARG_CNT + pd->index;
292 #if defined(__SPARC_64__)
293                 array[index] = value >> 32;
294 #else
295                 array[index] = value;
296 #endif
297         }
298 }
299
300
301 /* argument_vmarray_store_dbl **************************************************
302
303    Helper function to store a double into the argument array, taking
304    care of architecture specific issues.
305
306 *******************************************************************************/
307
308 static void argument_vmarray_store_dbl(uint64_t *array, paramdesc *pd, uint64_t value)
309 {
310         int32_t index;
311
312         if (!pd->inmemory) {
313 #if SIZEOF_VOID_P != 8 && defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
314                 index        = GET_LOW_REG(pd->index);
315                 array[index] = value & 0x00000000ffffffff;
316
317                 index        = GET_HIGH_REG(pd->index);
318                 array[index] = value >> 32;
319 #else
320                 index        = INT_ARG_CNT + pd->index;
321                 array[index] = value;
322 #endif
323         }
324         else {
325                 index        = ARG_CNT + pd->index;
326                 array[index] = value;
327         }
328 }
329
330
331 /* argument_vmarray_store_adr **************************************************
332
333    Helper function to store an address into the argument array, taking
334    care of architecture specific issues.
335
336 *******************************************************************************/
337
338 static void argument_vmarray_store_adr(uint64_t *array, paramdesc *pd, void *value)
339 {
340         int32_t index;
341
342         if (!pd->inmemory) {
343 #if defined(HAS_ADDRESS_REGISTER_FILE)
344                 /* When the architecture has address registers, place them
345                    after integer and float registers. */
346
347                 index        = INT_ARG_CNT + FLT_ARG_CNT + pd->index;
348 #else
349                 index        = pd->index;
350 #endif
351                 array[index] = (uint64_t) (intptr_t) value;
352         }
353         else {
354                 index        = ARG_CNT + pd->index;
355 #if SIZEOF_VOID_P == 8
356                 array[index] = (uint64_t) (intptr_t) value;
357 #else
358 # if WORDS_BIGENDIAN == 1
359                 array[index] = ((uint64_t) (intptr_t) value) << 32;
360 # else
361                 array[index] = (uint64_t) (intptr_t) value;
362 # endif
363 #endif
364         }
365 }
366
367
368 /* argument_vmarray_from_valist ************************************************
369
370    XXX
371
372 *******************************************************************************/
373
374 uint64_t *argument_vmarray_from_valist(methodinfo *m, java_handle_t *o, va_list ap)
375 {
376         methoddesc *md;
377         paramdesc  *pd;
378         typedesc   *td;
379         uint64_t   *array;
380         int32_t     i;
381         imm_union   value;
382
383         /* get the descriptors */
384
385         md = m->parseddesc;
386         pd = md->params;
387         td = md->paramtypes;
388
389         /* allocate argument array */
390
391         array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
392
393         /* if method is non-static fill first block and skip `this' pointer */
394
395         i = 0;
396
397         if (o != NULL) {
398                 /* the `this' pointer */
399                 argument_vmarray_store_adr(array, pd, o);
400
401                 pd++;
402                 td++;
403                 i++;
404         } 
405
406         for (; i < md->paramcount; i++, pd++, td++) {
407                 switch (td->type) {
408                 case TYPE_INT:
409                         value.i = va_arg(ap, int32_t);
410                         argument_vmarray_store_int(array, pd, value.i);
411                         break;
412
413                 case TYPE_LNG:
414                         value.l = va_arg(ap, int64_t);
415                         argument_vmarray_store_lng(array, pd, value.l);
416                         break;
417
418                 case TYPE_FLT:
419 #if defined(__ALPHA__) || defined(__POWERPC__) || defined(__POWERPC64__)
420                         /* This is required to load the correct float value in
421                            assembler code. */
422
423                         value.d = (double) va_arg(ap, double);
424 #else
425                         value.f = (float) va_arg(ap, double);
426 #endif
427                         argument_vmarray_store_flt(array, pd, value.l);
428                         break;
429
430                 case TYPE_DBL:
431                         value.d = va_arg(ap, double);
432                         argument_vmarray_store_dbl(array, pd, value.l);
433                         break;
434
435                 case TYPE_ADR: 
436                         value.a = va_arg(ap, void*);
437                         argument_vmarray_store_adr(array, pd, value.a);
438                         break;
439                 }
440         }
441
442         return array;
443 }
444
445
446 /* argument_vmarray_from_jvalue ************************************************
447
448    XXX
449
450 *******************************************************************************/
451
452 uint64_t *argument_vmarray_from_jvalue(methodinfo *m, java_handle_t *o,
453                                                                            const jvalue *args)
454 {
455         methoddesc *md;
456         paramdesc  *pd;
457         typedesc   *td;
458         uint64_t   *array;
459         int32_t     i;
460         int32_t     j;
461
462         /* get the descriptors */
463
464         md = m->parseddesc;
465         pd = md->params;
466         td = md->paramtypes;
467
468         /* allocate argument array */
469
470 #if defined(HAS_ADDRESS_REGISTER_FILE)
471         array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + ADR_ARG_CNT + md->memuse);
472 #else
473         array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
474 #endif
475
476         /* if method is non-static fill first block and skip `this' pointer */
477
478         i = 0;
479
480         if (o != NULL) {
481                 /* the `this' pointer */
482                 argument_vmarray_store_adr(array, pd, o);
483
484                 pd++;
485                 td++;
486                 i++;
487         } 
488
489         for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
490                 switch (td->decltype) {
491                 case TYPE_INT:
492                         argument_vmarray_store_int(array, pd, args[j].i);
493                         break;
494
495                 case TYPE_LNG:
496                         argument_vmarray_store_lng(array, pd, args[j].j);
497                         break;
498
499                 case TYPE_FLT:
500                         argument_vmarray_store_flt(array, pd, args[j].j);
501                         break;
502
503                 case TYPE_DBL:
504                         argument_vmarray_store_dbl(array, pd, args[j].j);
505                         break;
506
507                 case TYPE_ADR: 
508                         argument_vmarray_store_adr(array, pd, args[j].l);
509                         break;
510                 }
511         }
512
513         return array;
514 }
515
516
517 /* argument_vmarray_from_objectarray *******************************************
518
519    XXX
520
521 *******************************************************************************/
522
523 uint64_t *argument_vmarray_from_objectarray(methodinfo *m, java_handle_t *o,
524                                                                                         java_handle_objectarray_t *params)
525 {
526         methoddesc    *md;
527         paramdesc     *pd;
528         typedesc      *td;
529         uint64_t      *array;
530         java_handle_t *param;
531         classinfo     *c;
532         int            type;
533         int32_t        i;
534         int32_t        j;
535         imm_union      value;
536
537         /* get the descriptors */
538
539         md = m->parseddesc;
540         pd = md->params;
541         td = md->paramtypes;
542
543         /* allocate argument array */
544
545         array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
546
547         /* if method is non-static fill first block and skip `this' pointer */
548
549         i = 0;
550
551         if (o != NULL) {
552                 /* this pointer */
553                 argument_vmarray_store_adr(array, pd, o);
554
555                 pd++;
556                 td++;
557                 i++;
558         }
559
560         for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
561                 LLNI_objectarray_element_get(params, j, param);
562
563                 switch (td->type) {
564                 case TYPE_INT:
565                         if (param == NULL) {
566                                 exceptions_throw_illegalargumentexception();
567                                 return NULL;
568                         }
569
570                         /* convert the value according to its declared type */
571
572                         LLNI_class_get(param, c);
573                         type = primitive_type_get_by_wrapperclass(c);
574
575                         switch (td->decltype) {
576                         case PRIMITIVETYPE_BOOLEAN:
577                                 switch (type) {
578                                 case PRIMITIVETYPE_BOOLEAN:
579                                         /* This type is OK. */
580                                         break;
581                                 default:
582                                         exceptions_throw_illegalargumentexception();
583                                         return NULL;
584                                 }
585                                 break;
586
587                         case PRIMITIVETYPE_BYTE:
588                                 switch (type) {
589                                 case PRIMITIVETYPE_BYTE:
590                                         /* This type is OK. */
591                                         break;
592                                 default:
593                                         exceptions_throw_illegalargumentexception();
594                                         return NULL;
595                                 }
596                                 break;
597
598                         case PRIMITIVETYPE_CHAR:
599                                 switch (type) {
600                                 case PRIMITIVETYPE_CHAR:
601                                         /* This type is OK. */
602                                         break;
603                                 default:
604                                         exceptions_throw_illegalargumentexception();
605                                         return NULL;
606                                 }
607                                 break;
608
609                         case PRIMITIVETYPE_SHORT:
610                                 switch (type) {
611                                 case PRIMITIVETYPE_BYTE:
612                                 case PRIMITIVETYPE_SHORT:
613                                         /* These types are OK. */
614                                         break;
615                                 default:
616                                         exceptions_throw_illegalargumentexception();
617                                         return NULL;
618                                 }
619                                 break;
620
621                         case PRIMITIVETYPE_INT:
622                                 switch (type) {
623                                 case PRIMITIVETYPE_BYTE:
624                                 case PRIMITIVETYPE_SHORT:
625                                 case PRIMITIVETYPE_INT:
626                                         /* These types are OK. */
627                                         break;
628                                 default:
629                                         exceptions_throw_illegalargumentexception();
630                                         return NULL;
631                                 }
632                                 break;
633
634                         default:
635                                 vm_abort("argument_vmarray_from_objectarray: invalid type %d",
636                                                  td->decltype);
637                         }
638
639                         value = primitive_unbox(param);
640                         argument_vmarray_store_int(array, pd, value.i);
641                         break;
642
643                 case TYPE_LNG:
644                         if (param == NULL) {
645                                 exceptions_throw_illegalargumentexception();
646                                 return NULL;
647                         }
648
649                         LLNI_class_get(param, c);
650                         type = primitive_type_get_by_wrapperclass(c);
651
652                         assert(td->decltype == PRIMITIVETYPE_LONG);
653
654                         switch (type) {
655                         case PRIMITIVETYPE_BYTE:
656                         case PRIMITIVETYPE_SHORT:
657                         case PRIMITIVETYPE_INT:
658                         case PRIMITIVETYPE_LONG:
659                                 /* These types are OK. */
660                                 break;
661                         default:
662                                 exceptions_throw_illegalargumentexception();
663                                 return NULL;
664                         }
665
666                         value = primitive_unbox(param);
667                         argument_vmarray_store_lng(array, pd, value.l);
668                         break;
669
670                 case TYPE_FLT:
671                         if (param == NULL) {
672                                 exceptions_throw_illegalargumentexception();
673                                 return NULL;
674                         }
675
676                         LLNI_class_get(param, c);
677                         type = primitive_type_get_by_wrapperclass(c);
678
679                         assert(td->decltype == PRIMITIVETYPE_FLOAT);
680
681                         switch (type) {
682                         case PRIMITIVETYPE_FLOAT:
683                                 /* This type is OK. */
684                                 break;
685                         default:
686                                 exceptions_throw_illegalargumentexception();
687                                 return NULL;
688                         }
689
690                         value = primitive_unbox(param);
691                         argument_vmarray_store_flt(array, pd, value.l);
692                         break;
693
694                 case TYPE_DBL:
695                         if (param == NULL) {
696                                 exceptions_throw_illegalargumentexception();
697                                 return NULL;
698                         }
699
700                         LLNI_class_get(param, c);
701                         type = primitive_type_get_by_wrapperclass(c);
702
703                         assert(td->decltype == PRIMITIVETYPE_DOUBLE);
704
705                         switch (type) {
706                         case PRIMITIVETYPE_FLOAT:
707                         case PRIMITIVETYPE_DOUBLE:
708                                 /* These types are OK. */
709                                 break;
710                         default:
711                                 exceptions_throw_illegalargumentexception();
712                                 return NULL;
713                         }
714
715                         value = primitive_unbox(param);
716                         argument_vmarray_store_dbl(array, pd, value.l);
717                         break;
718                 
719                 case TYPE_ADR:
720                         if (!resolve_class_from_typedesc(td, true, true, &c))
721                                 return NULL;
722
723                         if (param != NULL) {
724                                 if (td->arraydim > 0) {
725                                         if (!builtin_arrayinstanceof(param, c)) {
726                                                 exceptions_throw_illegalargumentexception();
727                                                 return NULL;
728                                         }
729                                 }
730                                 else {
731                                         if (!builtin_instanceof(param, c)) {
732                                                 exceptions_throw_illegalargumentexception();
733                                                 return NULL;
734                                         }
735                                 }
736                         }
737
738                         argument_vmarray_store_adr(array, pd, param);
739                         break;
740
741                 default:
742                         vm_abort("argument_vmarray_from_objectarray: invalid type %d", td->type);
743                 }
744         }
745
746         return array;
747 }
748
749
750 /*
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  * ---------------------------------------------------------------------
755  * Local variables:
756  * mode: c
757  * indent-tabs-mode: t
758  * c-basic-offset: 4
759  * tab-width: 4
760  * End:
761  * vim:noexpandtab:sw=4:ts=4:
762  */