Thu Sep 20 16:32:42 CEST 2001 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / arch / x86 / tramp.c
1 /*
2  * Create trampolines to invoke arbitrary functions.
3  * 
4  * Copyright (C) Ximian Inc.
5  * 
6  * Author: Paolo Molaro (lupus@ximian.com)
7  * 
8  */
9
10 #include "config.h"
11 #include "x86-codegen.h"
12 #include "mono/metadata/class.h"
13 #include "mono/interpreter/interp.h"
14
15 /*
16  * The resulting function takes the form:
17  * void func (void (*callme)(), void *retval, void *this_obj, stackval *arguments);
18  */
19 #define FUNC_ADDR_POS   8
20 #define RETVAL_POS      12
21 #define THIS_POS        16
22 #define ARGP_POS        20
23 #define LOC_POS -4
24
25 #define ARG_SIZE        sizeof (stackval)
26
27 static char *
28 mono_get_ansi_string (MonoObject *o)
29 {
30         MonoStringObject *s = (MonoStringObject *)o;
31         char *as, *vector;
32         int i;
33
34         g_assert (o != NULL);
35
36         if (!s->length)
37                 return g_strdup ("");
38
39         vector = s->c_str->vector;
40
41         g_assert (vector != NULL);
42
43         as = g_malloc (s->length + 1);
44
45         /* fixme: replace with a real unicode/ansi conversion */
46         for (i = 0; i < s->length; i++) {
47                 as [i] = vector [i*2];
48         }
49
50         as [i] = '\0';
51
52         return as;
53 }
54
55 MonoPIFunc
56 mono_create_trampoline (MonoMethod *method)
57 {
58         MonoMethodSignature *sig;
59         unsigned char *p, *code_buffer;
60         guint32 local_size = 0, stack_size = 0, code_size = 30;
61         guint32 arg_pos;
62         int i, stringp;
63
64         sig = method->signature;
65         
66         if (sig->hasthis) {
67                 stack_size += sizeof (gpointer);
68                 code_size += 5;
69         }
70         
71         for (i = 0; i < sig->param_count; ++i) {
72                 if (sig->params [i]->byref) {
73                         stack_size += sizeof (gpointer);
74                         code_size += i < 10 ? 5 : 8;
75                         continue;
76                 }
77                 switch (sig->params [i]->type) {
78                 case MONO_TYPE_BOOLEAN:
79                 case MONO_TYPE_CHAR:
80                 case MONO_TYPE_I1:
81                 case MONO_TYPE_U1:
82                 case MONO_TYPE_I2:
83                 case MONO_TYPE_U2:
84                 case MONO_TYPE_I4:
85                 case MONO_TYPE_U4:
86                 case MONO_TYPE_I:
87                 case MONO_TYPE_U:
88                 case MONO_TYPE_PTR:
89                 case MONO_TYPE_R4:
90                 case MONO_TYPE_SZARRAY:
91                 case MONO_TYPE_CLASS:
92                 case MONO_TYPE_OBJECT:
93                         stack_size += 4;
94                         code_size += i < 10 ? 5 : 8;
95                         break;
96                 case MONO_TYPE_STRING:
97                         stack_size += 4;
98                         code_size += 20;
99                         local_size++;
100                         break;
101                 case MONO_TYPE_I8:
102                         stack_size += 8;
103                         code_size += i < 10 ? 5 : 8;
104                         break;
105                 case MONO_TYPE_R8:
106                         stack_size += 8;
107                         code_size += i < 10 ? 7 : 10;
108                         break;
109                 default:
110                         g_error ("Can't trampoline 0x%x", sig->params [i]->type);
111                 }
112         }
113         /*
114          * FIXME: take into account large return values.
115          */
116
117         code_buffer = p = alloca (code_size);
118
119         /*
120          * Standard function prolog.
121          */
122         x86_push_reg (p, X86_EBP);
123         x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
124         /*
125          * We store some local vars here to handle string pointers.
126          * and align to 16 byte boundary...
127          */
128         if (local_size) {
129                 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size * 4);
130                 stack_size = (stack_size * local_size * 4) % 16;
131         } else {
132                 stack_size = stack_size % 16;
133         }
134         x86_alu_reg_imm (p, X86_SUB, X86_ESP, stack_size);
135
136         /*
137          * EDX has the pointer to the args.
138          */
139         x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
140
141         /*
142          * Push arguments in reverse order.
143          */
144         stringp = 0;
145         for (i = sig->param_count; i; --i) {
146                 arg_pos = ARG_SIZE * (i - 1);
147                 if (sig->params [i - 1]->byref) {
148                         x86_push_membase (p, X86_EDX, arg_pos);
149                         continue;
150                 }
151                 switch (sig->params [i - 1]->type) {
152                 case MONO_TYPE_I1:
153                 case MONO_TYPE_U1:
154                 case MONO_TYPE_I2:
155                 case MONO_TYPE_U2:
156                 case MONO_TYPE_I4:
157                 case MONO_TYPE_U4:
158                 case MONO_TYPE_I:
159                 case MONO_TYPE_U:
160                 case MONO_TYPE_PTR:
161                 case MONO_TYPE_SZARRAY:
162                 case MONO_TYPE_CLASS:
163                 case MONO_TYPE_OBJECT:
164                 case MONO_TYPE_R4:
165                         x86_push_membase (p, X86_EDX, arg_pos);
166                         break;
167                 case MONO_TYPE_R8:
168                         x86_alu_reg_imm (p, X86_SUB, X86_ESP, 8);
169                         x86_fld_membase (p, X86_EDX, arg_pos, TRUE);
170                         x86_fst_membase (p, X86_ESP, 0, TRUE, TRUE);
171                         break;
172                 case MONO_TYPE_STRING:
173                         /*if (frame->method->flags & PINVOKE_ATTRIBUTE_CHAR_SET_ANSI*/
174                         x86_push_membase (p, X86_EDX, arg_pos);
175                         x86_mov_reg_imm (p, X86_EDX, mono_get_ansi_string);
176                         x86_call_reg (p, X86_EDX);
177                         x86_alu_reg_imm (p, X86_SUB, X86_ESP, 4);
178                         x86_push_reg (p, X86_EAX);
179                         /*
180                          * Store the pointer in a local we'll free later.
181                          */
182                         stringp++;
183                         x86_mov_membase_reg (p, X86_EBP, LOC_POS * stringp, X86_EAX, 4);
184                         /*
185                          * we didn't save the reg: restore it here.
186                          */
187                         x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
188                         break;
189                 case MONO_TYPE_I8:
190                         x86_push_membase (p, X86_EDX, arg_pos + 4);
191                         x86_push_membase (p, X86_EDX, arg_pos);
192                         break;
193                 case MONO_TYPE_BOOLEAN:
194                 case MONO_TYPE_CHAR:
195                 default:
196                         g_error ("Can't trampoline 0x%x", sig->params [i - 1]->type);
197                 }
198         }
199
200         if (sig->hasthis) {
201                 if (sig->call_convention != MONO_CALL_THISCALL) {
202                         x86_mov_reg_membase (p, X86_EDX, X86_EBP, THIS_POS, 4);
203                         x86_push_reg (p, X86_EDX);
204                 } else {
205                         x86_mov_reg_membase (p, X86_ECX, X86_EBP, THIS_POS, 4);
206                 }
207         }
208
209         /* 
210          * Insert call to function 
211          */
212         x86_mov_reg_membase (p, X86_EDX, X86_EBP, FUNC_ADDR_POS, 4);
213         x86_call_reg (p, X86_EDX);
214
215         /*
216          * Handle retval.
217          * Small integer and pointer values are in EAX.
218          * Long integers are in EAX:EDX.
219          * FP values are on the FP stack.
220          */
221         if (sig->ret->byref) {
222                 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
223                 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
224         } else {
225                 switch (sig->ret->type) {
226                 case MONO_TYPE_I4:
227                 case MONO_TYPE_U4:
228                 case MONO_TYPE_I:
229                 case MONO_TYPE_U:
230                 case MONO_TYPE_OBJECT:
231                 case MONO_TYPE_STRING: /* this is going to cause large pains... */
232                         x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
233                         x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
234                         break;
235                 case MONO_TYPE_R4:
236                         x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
237                         x86_fst_membase (p, X86_ECX, 0, FALSE, TRUE);
238                         break;
239                 case MONO_TYPE_R8:
240                         x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
241                         x86_fst_membase (p, X86_ECX, 0, TRUE, TRUE);
242                         break;
243                 case MONO_TYPE_I8:
244                         x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
245                         x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
246                         x86_mov_membase_reg (p, X86_ECX, 4, X86_EDX, 4);
247                         break;
248                 case MONO_TYPE_VOID:
249                         break;
250                 default:
251                         g_error ("Can't handle as return value 0x%x", sig->ret->type);
252                 }
253         }
254
255         /*
256          * free the allocated strings.
257          */
258         if (local_size)
259                 x86_mov_reg_imm (p, X86_EDX, g_free);
260         for (i = 1; i <= local_size; ++i) {
261                 x86_push_membase (p, X86_EBP, LOC_POS * i);
262                 x86_call_reg (p, X86_EDX);
263         }
264         /*
265          * Standard epilog.
266          */
267         x86_leave (p);
268         x86_ret (p);
269
270         return g_memdup (code_buffer, p - code_buffer);
271 }
272
273 #define MINV_POS  (- sizeof (MonoInvocation))
274 #define STACK_POS (MINV_POS - sizeof (stackval) * sig->param_count)
275 #define OBJ_POS   8
276 #define TYPE_OFFSET (G_STRUCT_OFFSET (stackval, type))
277
278 /*
279  * Returns a pointer to a native function that can be used to
280  * call the specified method.
281  * The function created will receive the arguments according
282  * to the call convention specified in the method.
283  * This function works by creating a MonoInvocation structure,
284  * filling the fields in and calling ves_exec_method on it.
285  * Still need to figure out how to handle the exception stuff
286  * across the managed/unmanaged boundary.
287  */
288 void *
289 mono_create_method_pointer (MonoMethod *method)
290 {
291         MonoMethodSignature *sig;
292         unsigned char *p, *code_buffer;
293         gint32 local_size;
294         gint32 stackval_pos, arg_pos = 8;
295         int i;
296
297         /*
298          * If it is a static P/Invoke method, we can just return the pointer
299          * to the method implementation.
300          */
301         sig = method->signature;
302
303         code_buffer = p = alloca (512); /* FIXME: check for overflows... */
304
305         local_size = sizeof (MonoInvocation) + sizeof (stackval) * (sig->param_count + 1);
306         stackval_pos = -local_size;
307
308         /*
309          * Standard function prolog with magic trick.
310          */
311         x86_jump_code (p, code_buffer + 8);
312         *p++ = 'M';
313         *p++ = 'o';
314         *(void**)p = method;
315         p += 4;
316         x86_push_reg (p, X86_EBP);
317         x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
318         x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size);
319
320         /*
321          * Initialize MonoInvocation fields, first the ones known now.
322          */
323         x86_mov_reg_imm (p, X86_EAX, 0);
324         x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex)), X86_EAX, 4);
325         x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex_handler)), X86_EAX, 4);
326         x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, child)), X86_EAX, 4);
327         x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, parent)), X86_EAX, 4);
328         /*
329          * Set the method pointer.
330          */
331         x86_mov_membase_imm (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, method)), (int)method, 4);
332
333         /*
334          * Handle this.
335          */
336         if (sig->hasthis) {
337                 if (sig->call_convention != MONO_CALL_THISCALL) {
338                         /*
339                          * Grab it from the stack, otherwise it's already in ECX.
340                          */
341                         x86_mov_reg_membase (p, X86_ECX, X86_EBP, OBJ_POS, 4);
342                         arg_pos += 4;
343                 }
344                 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, obj)), X86_ECX, 4);
345         }
346         /*
347          * Handle the arguments. stackval_pos is the posset of the stackval array from EBP.
348          * arg_pos is the offset from EBP to the incoming arg on the stack.
349          * We just call stackval_from_data to handle all the (nasty) issues....
350          */
351         for (i = 0; i < sig->param_count; ++i) {
352                 x86_mov_reg_imm (p, X86_ECX, stackval_from_data);
353                 x86_lea_membase (p, X86_EDX, X86_EBP, arg_pos);
354                 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
355                 x86_push_reg (p, X86_EDX);
356                 x86_push_reg (p, X86_EAX);
357                 x86_push_imm (p, sig->params [i]);
358                 x86_call_reg (p, X86_ECX);
359                 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 12);
360                 stackval_pos += sizeof (stackval);
361                 arg_pos += 4;
362                 if (!sig->params [i]->byref) {
363                         switch (sig->params [i]->type) {
364                         case MONO_TYPE_I8:
365                         case MONO_TYPE_R8:
366                                 arg_pos += 4;
367                                 break;
368                         case MONO_TYPE_VALUETYPE:
369                                 g_assert_not_reached (); /* Not implemented yet. */
370                         default:
371                                 break;
372                         }
373                 }
374         }
375
376         /*
377          * Handle the return value storage area.
378          */
379         x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
380         x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, retval)), X86_EAX, 4);
381
382         /*
383          * Call the method.
384          */
385         x86_lea_membase (p, X86_EAX, X86_EBP, MINV_POS);
386         x86_push_reg (p, X86_EAX);
387         x86_mov_reg_imm (p, X86_EDX, ves_exec_method);
388         x86_call_reg (p, X86_EDX);
389
390         /*
391          * Move the return value to the proper place.
392          */
393         x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
394         if (sig->ret->byref) {
395                 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
396         } else {
397                 switch (sig->ret->type) {
398                 case MONO_TYPE_VOID:
399                         break;
400                 case MONO_TYPE_I4:
401                 case MONO_TYPE_U4:
402                 case MONO_TYPE_I:
403                 case MONO_TYPE_U:
404                 case MONO_TYPE_OBJECT:
405                 case MONO_TYPE_STRING:
406                         x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
407                         break;
408                 case MONO_TYPE_I8:
409                         x86_mov_reg_membase (p, X86_EDX, X86_EAX, 4, 4);
410                         x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
411                         break;
412                 case MONO_TYPE_R8:
413                         x86_fld_membase (p, X86_EAX, 0, TRUE);
414                         break;
415                 default:
416                         g_error ("Type 0x%x not handled yet in thunk creation", sig->ret->type);
417                         break;
418                 }
419         }
420         
421         /*
422          * Standard epilog.
423          */
424         x86_leave (p);
425         x86_ret (p);
426
427         return g_memdup (code_buffer, p - code_buffer);
428 }
429
430