* src/native/localref.c [ENABLE_HANDLES] (localref_native_exit): Implemented.
[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 "vm/global.h"
34 #include "vm/vm.h"
35
36 #include "vmcore/descriptor.h"
37
38
39 /* argument_jitarray_load ******************************************************
40  
41    Returns the argument specified by index from one of the passed arrays
42    and returns it.
43
44 *******************************************************************************/
45
46 imm_union argument_jitarray_load(methoddesc *md, int32_t index,
47                                                                  uint64_t *arg_regs, uint64_t *stack)
48 {
49         imm_union  ret;
50         paramdesc *pd;
51
52         pd = &md->params[index];
53
54         switch (md->paramtypes[index].type) {
55                 case TYPE_INT:
56                 case TYPE_ADR:
57                         if (pd->inmemory) {
58 #if (SIZEOF_VOID_P == 8)
59                                 ret.l = (int64_t)stack[pd->index];
60 #else
61                                 ret.l = *(int32_t *)(stack + pd->index);
62 #endif
63                         } else {
64 #if (SIZEOF_VOID_P == 8)
65                                 ret.l = arg_regs[index];
66 #else
67                                 ret.l = *(int32_t *)(arg_regs + index);
68 #endif
69                         }
70                         break;
71                 case TYPE_LNG:
72                         if (pd->inmemory) {
73                                 ret.l = (int64_t)stack[pd->index];
74                         } else {
75                                 ret.l = (int64_t)arg_regs[index];
76                         }
77                         break;
78                 case TYPE_FLT:
79                         if (pd->inmemory) {
80                                 ret.l = (int64_t)stack[pd->index];
81                         } else {
82                                 ret.l = (int64_t)arg_regs[index];
83                         }
84                         break;
85                 case TYPE_DBL:
86                         if (pd->inmemory) {
87                                 ret.l = (int64_t)stack[pd->index];
88                         } else {
89                                 ret.l = (int64_t)arg_regs[index];
90                         }
91                         break;
92         }
93
94         return ret;
95 }
96
97
98 /* argument_jitarray_store *****************************************************
99  
100    Stores the argument into one of the passed arrays at a slot specified
101    by index.
102
103 *******************************************************************************/
104
105 void argument_jitarray_store(methoddesc *md, int32_t index,
106                                                          uint64_t *arg_regs, uint64_t *stack,
107                                                          imm_union param)
108 {
109         paramdesc *pd;
110
111         pd = &md->params[index];
112
113         switch (md->paramtypes[index].type) {
114                 case TYPE_ADR:
115                         if (pd->inmemory) {
116 #if (SIZEOF_VOID_P == 8)
117                                 stack[pd->index] = param.l;
118 #else
119                                 assert(0);
120 #endif
121                         } else {
122                                 arg_regs[index] = param.l;
123                         }
124                         break;
125                 default:
126                         vm_abort("argument_jitarray_store: type not implemented");
127                         break;
128         }
129 }
130
131
132 /* argument_jitreturn_load *****************************************************
133
134    Loads the proper return value form the return register and returns it.
135
136 *******************************************************************************/
137
138 imm_union argument_jitreturn_load(methoddesc *md, uint64_t *return_regs)
139 {
140         imm_union ret;
141
142         switch (md->returntype.type) {
143                 case TYPE_INT:
144                 case TYPE_ADR:
145 #if (SIZEOF_VOID_P == 8)
146                         ret.l = return_regs[0];
147 #else
148                         ret.l = *(int32_t *)return_regs;
149 #endif
150                         break;
151                 case TYPE_LNG:
152                         ret.l = *(int64_t *)return_regs;
153                         break;
154                 case TYPE_FLT:
155                         ret.l = *(int64_t *)return_regs;
156                         break;
157                 case TYPE_DBL:
158                         ret.l = *(int64_t *)return_regs;
159                         break;
160         }
161
162         return ret;
163 }
164
165
166 /* argument_jitreturn_store ****************************************************
167
168    Stores the proper return value into the return registers.
169
170 *******************************************************************************/
171
172 void argument_jitreturn_store(methoddesc *md, uint64_t *return_regs, imm_union ret)
173 {
174         switch (md->returntype.type) {
175                 case TYPE_ADR:
176 #if (SIZEOF_VOID_P == 8)
177                         return_regs[0] = ret.l;
178 #else
179                         assert(0);
180 #endif
181                         break;
182                 default:
183                         vm_abort("argument_jitreturn_store: type not implemented");
184                         break;
185         }
186 }
187
188
189 /*
190  * These are local overrides for various environment variables in Emacs.
191  * Please do not remove this and leave it at the end of the file, where
192  * Emacs will automagically detect them.
193  * ---------------------------------------------------------------------
194  * Local variables:
195  * mode: c
196  * indent-tabs-mode: t
197  * c-basic-offset: 4
198  * tab-width: 4
199  * End:
200  * vim:noexpandtab:sw=4:ts=4:
201  */