2 * Create trampolines to invoke arbitrary functions.
4 * Copyright (C) Ximian Inc.
6 * Author: Paolo Molaro (lupus@ximian.com)
12 #include "x86-codegen.h"
13 #include "mono/metadata/class.h"
14 #include "mono/metadata/tabledefs.h"
15 #include "mono/interpreter/interp.h"
16 #include "mono/metadata/appdomain.h"
17 #include "mono/metadata/marshal.h"
20 * The resulting function takes the form:
21 * void func (void (*callme)(), void *retval, void *this_obj, stackval *arguments);
23 #define FUNC_ADDR_POS 8
29 #define ARG_SIZE sizeof (stackval)
32 mono_create_trampoline (MonoMethod *method, int runtime)
34 MonoMethodSignature *sig;
35 unsigned char *p, *code_buffer;
36 guint32 local_size = 0, stack_size = 0, code_size = 50;
37 guint32 arg_pos, simpletype;
40 GList *free_locs = NULL;
42 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || runtime)
47 sig = method->signature;
50 stack_size += sizeof (gpointer);
54 for (i = 0; i < sig->param_count; ++i) {
55 if (sig->params [i]->byref) {
56 stack_size += sizeof (gpointer);
61 simpletype = sig->params [i]->type;
64 case MONO_TYPE_BOOLEAN:
76 case MONO_TYPE_SZARRAY:
78 case MONO_TYPE_OBJECT:
80 code_size += i < 10 ? 5 : 8;
82 case MONO_TYPE_VALUETYPE:
83 if (sig->params [i]->data.klass->enumtype) {
84 simpletype = sig->params [i]->data.klass->enum_basetype->type;
87 if (mono_class_value_size (sig->params [i]->data.klass, NULL) != 4)
88 g_error ("can only marshal enums, not generic structures (size: %d)", mono_class_value_size (sig->params [i]->data.klass, NULL));
90 code_size += i < 10 ? 5 : 8;
92 case MONO_TYPE_STRING:
99 code_size += i < 10 ? 5 : 8;
103 code_size += i < 10 ? 7 : 10;
106 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
110 * FIXME: take into account large return values.
113 code_buffer = p = alloca (code_size);
116 * Standard function prolog.
118 x86_push_reg (p, X86_EBP);
119 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
121 * We store some local vars here to handle string pointers.
122 * and align to 16 byte boundary...
125 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size * 4);
126 stack_size = (stack_size * local_size * 4) % 16;
128 stack_size = stack_size % 16;
131 x86_alu_reg_imm (p, X86_SUB, X86_ESP, stack_size);
134 * EDX has the pointer to the args.
136 x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
139 * Push arguments in reverse order.
142 for (i = sig->param_count; i; --i) {
143 arg_pos = ARG_SIZE * (i - 1);
144 if (sig->params [i - 1]->byref) {
146 x86_push_membase (p, X86_EDX, arg_pos);
149 if (sig->params [i - 1]->type == MONO_TYPE_SZARRAY &&
150 sig->params [i - 1]->data.type->type == MONO_TYPE_STRING) {
151 x86_mov_reg_membase (p, X86_EAX, X86_EDX, arg_pos, 4);
152 x86_push_regp (p, X86_EAX);
153 x86_mov_reg_imm (p, X86_EDX, mono_marshal_string_array);
154 x86_call_reg (p, X86_EDX);
155 x86_alu_reg_imm (p, X86_ADD, X86_ESP, 4);
157 * Store the pointer in a local we'll free later.
160 x86_mov_membase_reg (p, X86_EBP, LOC_POS * stringp, X86_EAX, 4);
161 free_locs = g_list_prepend (free_locs, GUINT_TO_POINTER (LOC_POS * stringp));
162 /* load the pointer and push it */
163 x86_lea_membase (p, X86_EAX, X86_EBP, LOC_POS * stringp);
164 x86_push_reg (p, X86_EAX);
165 /* restore pointer to args in EDX */
166 x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
168 x86_push_membase (p, X86_EDX, arg_pos);
172 simpletype = sig->params [i - 1]->type;
174 switch (simpletype) {
175 case MONO_TYPE_BOOLEAN:
186 case MONO_TYPE_SZARRAY:
187 case MONO_TYPE_CLASS:
188 case MONO_TYPE_OBJECT:
190 x86_push_membase (p, X86_EDX, arg_pos);
192 case MONO_TYPE_VALUETYPE:
193 if (!sig->params [i - 1]->data.klass->enumtype) {
194 /* it's a structure that fits in 4 bytes, need to push the value pointed to */
195 x86_mov_reg_membase (p, X86_EAX, X86_EDX, arg_pos, 4);
196 x86_push_regp (p, X86_EAX);
198 /* it's an enum value */
199 simpletype = sig->params [i - 1]->data.klass->enum_basetype->type;
203 case MONO_TYPE_STRING:
205 * If it is an internalcall we assume it's the object we want.
206 * Yet another reason why MONO_TYPE_STRING should not be used to indicate char*.
209 x86_push_membase (p, X86_EDX, arg_pos);
212 /*if (frame->method->flags & PINVOKE_ATTRIBUTE_CHAR_SET_ANSI*/
213 x86_push_membase (p, X86_EDX, arg_pos);
214 x86_mov_reg_imm (p, X86_EDX, mono_string_to_utf8);
215 x86_call_reg (p, X86_EDX);
216 x86_alu_reg_imm (p, X86_ADD, X86_ESP, 4);
217 x86_push_reg (p, X86_EAX);
219 * Store the pointer in a local we'll free later.
222 x86_mov_membase_reg (p, X86_EBP, LOC_POS * stringp, X86_EAX, 4);
223 free_locs = g_list_prepend (free_locs, GUINT_TO_POINTER (LOC_POS * stringp));
225 * we didn't save the reg: restore it here.
228 x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
233 x86_push_membase (p, X86_EDX, arg_pos + 4);
234 x86_push_membase (p, X86_EDX, arg_pos);
237 g_error ("Can't trampoline 0x%x", sig->params [i - 1]->type);
242 if (sig->call_convention != MONO_CALL_THISCALL) {
243 x86_mov_reg_membase (p, X86_EDX, X86_EBP, THIS_POS, 4);
244 x86_push_reg (p, X86_EDX);
246 x86_mov_reg_membase (p, X86_ECX, X86_EBP, THIS_POS, 4);
251 * Insert call to function
253 x86_mov_reg_membase (p, X86_EDX, X86_EBP, FUNC_ADDR_POS, 4);
254 x86_call_reg (p, X86_EDX);
258 * Small integer and pointer values are in EAX.
259 * Long integers are in EAX:EDX.
260 * FP values are on the FP stack.
263 if (sig->ret->byref ||
264 (method->klass == mono_defaults.string_class &&
265 *method->name == '.' && !strcmp (method->name, ".ctor"))) {
266 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
267 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
269 simpletype = sig->ret->type;
271 switch (simpletype) {
272 case MONO_TYPE_BOOLEAN:
275 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
276 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 1);
281 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
282 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 2);
288 case MONO_TYPE_CLASS:
289 case MONO_TYPE_OBJECT:
290 case MONO_TYPE_SZARRAY:
291 case MONO_TYPE_ARRAY:
292 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
293 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
295 case MONO_TYPE_STRING:
297 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
298 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
302 /* If the argument is non-null, then convert the value back */
303 x86_alu_reg_reg (p, X86_OR, X86_EAX, X86_EAX);
304 x86_branch8 (p, X86_CC_EQ, 11, FALSE);
305 x86_push_reg (p, X86_EAX);
306 x86_mov_reg_imm (p, X86_EDX, mono_string_new_wrapper);
307 x86_call_reg (p, X86_EDX);
308 x86_alu_reg_imm (p, X86_ADD, X86_ESP, 4);
310 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
311 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
314 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
315 x86_fst_membase (p, X86_ECX, 0, FALSE, TRUE);
318 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
319 x86_fst_membase (p, X86_ECX, 0, TRUE, TRUE);
322 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
323 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
324 x86_mov_membase_reg (p, X86_ECX, 4, X86_EDX, 4);
326 case MONO_TYPE_VALUETYPE:
327 if (sig->ret->data.klass->enumtype) {
328 simpletype = sig->ret->data.klass->enum_basetype->type;
334 g_error ("Can't handle as return value 0x%x", sig->ret->type);
339 * free the allocated strings.
343 for (tmp = free_locs; tmp; tmp = tmp->next) {
344 x86_mov_reg_imm (p, X86_EDX, g_free);
345 x86_push_membase (p, X86_EBP, GPOINTER_TO_UINT (tmp->data));
346 x86_call_reg (p, X86_EDX);
348 g_list_free (free_locs);
356 g_assert (p - code_buffer < code_size);
357 return g_memdup (code_buffer, p - code_buffer);
360 #define MINV_POS (- sizeof (MonoInvocation))
361 #define STACK_POS (MINV_POS - sizeof (stackval) * sig->param_count)
363 #define TYPE_OFFSET (G_STRUCT_OFFSET (stackval, type))
366 * Returns a pointer to a native function that can be used to
367 * call the specified method.
368 * The function created will receive the arguments according
369 * to the call convention specified in the method.
370 * This function works by creating a MonoInvocation structure,
371 * filling the fields in and calling ves_exec_method on it.
372 * Still need to figure out how to handle the exception stuff
373 * across the managed/unmanaged boundary.
376 mono_create_method_pointer (MonoMethod *method)
378 MonoMethodSignature *sig;
380 unsigned char *p, *code_buffer;
382 gint32 stackval_pos, arg_pos = 8;
386 * If it is a static P/Invoke method, we can just return the pointer
387 * to the method implementation.
389 sig = method->signature;
391 code_buffer = p = alloca (512); /* FIXME: check for overflows... */
393 local_size = sizeof (MonoInvocation) + sizeof (stackval) * (sig->param_count + 1);
394 stackval_pos = -local_size;
397 * Standard function prolog.
399 x86_push_reg (p, X86_EBP);
400 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
401 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size);
404 * Initialize MonoInvocation fields, first the ones known now.
406 x86_mov_reg_imm (p, X86_EAX, 0);
407 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex)), X86_EAX, 4);
408 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex_handler)), X86_EAX, 4);
409 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, child)), X86_EAX, 4);
410 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, parent)), X86_EAX, 4);
412 * Set the method pointer.
414 x86_mov_membase_imm (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, method)), (int)method, 4);
420 if (sig->call_convention != MONO_CALL_THISCALL) {
422 * Grab it from the stack, otherwise it's already in ECX.
424 x86_mov_reg_membase (p, X86_ECX, X86_EBP, OBJ_POS, 4);
427 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, obj)), X86_ECX, 4);
430 * Handle the arguments. stackval_pos is the posset of the stackval array from EBP.
431 * arg_pos is the offset from EBP to the incoming arg on the stack.
432 * We just call stackval_from_data to handle all the (nasty) issues....
434 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
435 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, stack_args)), X86_EAX, 4);
436 for (i = 0; i < sig->param_count; ++i) {
437 x86_mov_reg_imm (p, X86_ECX, stackval_from_data);
438 x86_lea_membase (p, X86_EDX, X86_EBP, arg_pos);
439 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
440 x86_push_reg (p, X86_EDX);
441 x86_push_reg (p, X86_EAX);
442 x86_push_imm (p, sig->params [i]);
443 x86_call_reg (p, X86_ECX);
444 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 12);
445 stackval_pos += sizeof (stackval);
446 arg_pos += mono_type_stack_size (sig->params [i], &align);
450 * Handle the return value storage area.
452 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
453 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, retval)), X86_EAX, 4);
458 x86_lea_membase (p, X86_EAX, X86_EBP, MINV_POS);
459 x86_push_reg (p, X86_EAX);
460 x86_mov_reg_imm (p, X86_EDX, ves_exec_method);
461 x86_call_reg (p, X86_EDX);
464 * Move the return value to the proper place.
466 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
467 if (sig->ret->byref) {
468 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
470 switch (sig->ret->type) {
473 case MONO_TYPE_BOOLEAN:
474 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 1);
480 case MONO_TYPE_OBJECT:
481 case MONO_TYPE_STRING:
482 case MONO_TYPE_CLASS:
483 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
486 x86_mov_reg_membase (p, X86_EDX, X86_EAX, 4, 4);
487 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
490 x86_fld_membase (p, X86_EAX, 0, TRUE);
493 g_error ("Type 0x%x not handled yet in thunk creation", sig->ret->type);
504 g_assert (p - code_buffer < 512);
506 ji = g_new0 (MonoJitInfo, 1);
508 ji->code_size = p - code_buffer;
509 ji->code_start = g_memdup (code_buffer, p - code_buffer);
511 mono_jit_info_table_add (mono_root_domain, ji);
513 return ji->code_start;