2 * Create trampolines to invoke arbitrary functions.
4 * Copyright (C) Ximian Inc.
6 * Author: Paolo Molaro (lupus@ximian.com)
13 #include "x86-codegen.h"
14 #include "mono/metadata/class.h"
15 #include "mono/metadata/tabledefs.h"
16 #include "mono/interpreter/interp.h"
17 #include "mono/metadata/appdomain.h"
18 #include "mono/metadata/marshal.h"
21 * The resulting function takes the form:
22 * void func (void (*callme)(), void *retval, void *this_obj, stackval *arguments);
24 #define FUNC_ADDR_POS 8
30 #define ARG_SIZE sizeof (stackval)
33 mono_create_trampoline (MonoMethod *method, int runtime)
35 MonoMethodSignature *sig;
36 unsigned char *p, *code_buffer;
37 guint32 local_size = 0, stack_size = 0, code_size = 50;
38 guint32 arg_pos, simpletype;
41 GList *free_locs = NULL;
43 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || runtime)
48 sig = method->signature;
51 stack_size += sizeof (gpointer);
55 for (i = 0; i < sig->param_count; ++i) {
56 if (sig->params [i]->byref) {
57 stack_size += sizeof (gpointer);
62 simpletype = sig->params [i]->type;
65 case MONO_TYPE_BOOLEAN:
77 case MONO_TYPE_SZARRAY:
79 case MONO_TYPE_OBJECT:
81 code_size += i < 10 ? 5 : 8;
83 case MONO_TYPE_VALUETYPE:
84 if (sig->params [i]->data.klass->enumtype) {
85 simpletype = sig->params [i]->data.klass->enum_basetype->type;
88 if (mono_class_value_size (sig->params [i]->data.klass, NULL) != 4)
89 g_error ("can only marshal enums, not generic structures (size: %d)", mono_class_value_size (sig->params [i]->data.klass, NULL));
91 code_size += i < 10 ? 5 : 8;
93 case MONO_TYPE_STRING:
100 code_size += i < 10 ? 5 : 8;
104 code_size += i < 10 ? 7 : 10;
107 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
111 * FIXME: take into account large return values.
114 code_buffer = p = alloca (code_size);
117 * Standard function prolog.
119 x86_push_reg (p, X86_EBP);
120 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
122 * We store some local vars here to handle string pointers.
123 * and align to 16 byte boundary...
126 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size * 4);
127 stack_size = (stack_size * local_size * 4) % 16;
129 stack_size = stack_size % 16;
132 x86_alu_reg_imm (p, X86_SUB, X86_ESP, stack_size);
135 * EDX has the pointer to the args.
137 x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
140 * Push arguments in reverse order.
143 for (i = sig->param_count; i; --i) {
144 arg_pos = ARG_SIZE * (i - 1);
145 if (sig->params [i - 1]->byref) {
147 x86_push_membase (p, X86_EDX, arg_pos);
150 if (sig->params [i - 1]->type == MONO_TYPE_SZARRAY &&
151 sig->params [i - 1]->data.type->type == MONO_TYPE_STRING) {
152 x86_mov_reg_membase (p, X86_EAX, X86_EDX, arg_pos, 4);
153 x86_push_regp (p, X86_EAX);
154 x86_mov_reg_imm (p, X86_EDX, mono_marshal_string_array);
155 x86_call_reg (p, X86_EDX);
156 x86_alu_reg_imm (p, X86_ADD, X86_ESP, 4);
158 * Store the pointer in a local we'll free later.
161 x86_mov_membase_reg (p, X86_EBP, LOC_POS * stringp, X86_EAX, 4);
162 free_locs = g_list_prepend (free_locs, GUINT_TO_POINTER (LOC_POS * stringp));
163 /* load the pointer and push it */
164 x86_lea_membase (p, X86_EAX, X86_EBP, LOC_POS * stringp);
165 x86_push_reg (p, X86_EAX);
166 /* restore pointer to args in EDX */
167 x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
169 x86_push_membase (p, X86_EDX, arg_pos);
173 simpletype = sig->params [i - 1]->type;
175 switch (simpletype) {
176 case MONO_TYPE_BOOLEAN:
187 case MONO_TYPE_SZARRAY:
188 case MONO_TYPE_CLASS:
189 case MONO_TYPE_OBJECT:
191 x86_push_membase (p, X86_EDX, arg_pos);
193 case MONO_TYPE_VALUETYPE:
194 if (!sig->params [i - 1]->data.klass->enumtype) {
195 /* it's a structure that fits in 4 bytes, need to push the value pointed to */
196 x86_mov_reg_membase (p, X86_EAX, X86_EDX, arg_pos, 4);
197 x86_push_regp (p, X86_EAX);
199 /* it's an enum value */
200 simpletype = sig->params [i - 1]->data.klass->enum_basetype->type;
204 case MONO_TYPE_STRING:
206 * If it is an internalcall we assume it's the object we want.
207 * Yet another reason why MONO_TYPE_STRING should not be used to indicate char*.
210 x86_push_membase (p, X86_EDX, arg_pos);
213 /*if (frame->method->flags & PINVOKE_ATTRIBUTE_CHAR_SET_ANSI*/
214 x86_push_membase (p, X86_EDX, arg_pos);
215 x86_mov_reg_imm (p, X86_EDX, mono_string_to_utf8);
216 x86_call_reg (p, X86_EDX);
217 x86_alu_reg_imm (p, X86_ADD, X86_ESP, 4);
218 x86_push_reg (p, X86_EAX);
220 * Store the pointer in a local we'll free later.
223 x86_mov_membase_reg (p, X86_EBP, LOC_POS * stringp, X86_EAX, 4);
224 free_locs = g_list_prepend (free_locs, GUINT_TO_POINTER (LOC_POS * stringp));
226 * we didn't save the reg: restore it here.
229 x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
234 x86_push_membase (p, X86_EDX, arg_pos + 4);
235 x86_push_membase (p, X86_EDX, arg_pos);
238 g_error ("Can't trampoline 0x%x", sig->params [i - 1]->type);
243 if (sig->call_convention != MONO_CALL_THISCALL) {
244 x86_mov_reg_membase (p, X86_EDX, X86_EBP, THIS_POS, 4);
245 x86_push_reg (p, X86_EDX);
247 x86_mov_reg_membase (p, X86_ECX, X86_EBP, THIS_POS, 4);
252 * Insert call to function
254 x86_mov_reg_membase (p, X86_EDX, X86_EBP, FUNC_ADDR_POS, 4);
255 x86_call_reg (p, X86_EDX);
259 * Small integer and pointer values are in EAX.
260 * Long integers are in EAX:EDX.
261 * FP values are on the FP stack.
264 if (sig->ret->byref ||
265 (method->klass == mono_defaults.string_class &&
266 *method->name == '.' && !strcmp (method->name, ".ctor"))) {
267 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
268 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
270 simpletype = sig->ret->type;
272 switch (simpletype) {
273 case MONO_TYPE_BOOLEAN:
276 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
277 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 1);
282 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
283 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 2);
289 case MONO_TYPE_CLASS:
290 case MONO_TYPE_OBJECT:
291 case MONO_TYPE_SZARRAY:
292 case MONO_TYPE_ARRAY:
293 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
294 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
296 case MONO_TYPE_STRING:
298 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
299 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
303 /* If the argument is non-null, then convert the value back */
304 x86_alu_reg_reg (p, X86_OR, X86_EAX, X86_EAX);
305 x86_branch8 (p, X86_CC_EQ, 11, FALSE);
306 x86_push_reg (p, X86_EAX);
307 x86_mov_reg_imm (p, X86_EDX, mono_string_new_wrapper);
308 x86_call_reg (p, X86_EDX);
309 x86_alu_reg_imm (p, X86_ADD, X86_ESP, 4);
311 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
312 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
315 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
316 x86_fst_membase (p, X86_ECX, 0, FALSE, TRUE);
319 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
320 x86_fst_membase (p, X86_ECX, 0, TRUE, TRUE);
323 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
324 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
325 x86_mov_membase_reg (p, X86_ECX, 4, X86_EDX, 4);
327 case MONO_TYPE_VALUETYPE:
328 if (sig->ret->data.klass->enumtype) {
329 simpletype = sig->ret->data.klass->enum_basetype->type;
335 g_error ("Can't handle as return value 0x%x", sig->ret->type);
340 * free the allocated strings.
344 for (tmp = free_locs; tmp; tmp = tmp->next) {
345 x86_mov_reg_imm (p, X86_EDX, g_free);
346 x86_push_membase (p, X86_EBP, GPOINTER_TO_UINT (tmp->data));
347 x86_call_reg (p, X86_EDX);
349 g_list_free (free_locs);
357 g_assert (p - code_buffer < code_size);
358 return g_memdup (code_buffer, p - code_buffer);
361 #define MINV_POS (- sizeof (MonoInvocation))
362 #define STACK_POS (MINV_POS - sizeof (stackval) * sig->param_count)
364 #define TYPE_OFFSET (G_STRUCT_OFFSET (stackval, type))
367 * Returns a pointer to a native function that can be used to
368 * call the specified method.
369 * The function created will receive the arguments according
370 * to the call convention specified in the method.
371 * This function works by creating a MonoInvocation structure,
372 * filling the fields in and calling ves_exec_method on it.
373 * Still need to figure out how to handle the exception stuff
374 * across the managed/unmanaged boundary.
377 mono_create_method_pointer (MonoMethod *method)
379 MonoMethodSignature *sig;
381 unsigned char *p, *code_buffer;
383 gint32 stackval_pos, arg_pos = 8;
387 * If it is a static P/Invoke method, we can just return the pointer
388 * to the method implementation.
390 sig = method->signature;
392 code_buffer = p = alloca (512); /* FIXME: check for overflows... */
394 local_size = sizeof (MonoInvocation) + sizeof (stackval) * (sig->param_count + 1);
395 stackval_pos = -local_size;
398 * Standard function prolog.
400 x86_push_reg (p, X86_EBP);
401 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
402 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size);
405 * Initialize MonoInvocation fields, first the ones known now.
407 x86_mov_reg_imm (p, X86_EAX, 0);
408 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex)), X86_EAX, 4);
409 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex_handler)), X86_EAX, 4);
410 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, child)), X86_EAX, 4);
411 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, parent)), X86_EAX, 4);
413 * Set the method pointer.
415 x86_mov_membase_imm (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, method)), (int)method, 4);
421 if (sig->call_convention != MONO_CALL_THISCALL) {
423 * Grab it from the stack, otherwise it's already in ECX.
425 x86_mov_reg_membase (p, X86_ECX, X86_EBP, OBJ_POS, 4);
428 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, obj)), X86_ECX, 4);
431 * Handle the arguments. stackval_pos is the posset of the stackval array from EBP.
432 * arg_pos is the offset from EBP to the incoming arg on the stack.
433 * We just call stackval_from_data to handle all the (nasty) issues....
435 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
436 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, stack_args)), X86_EAX, 4);
437 for (i = 0; i < sig->param_count; ++i) {
438 x86_mov_reg_imm (p, X86_ECX, stackval_from_data);
439 x86_lea_membase (p, X86_EDX, X86_EBP, arg_pos);
440 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
441 x86_push_reg (p, X86_EDX);
442 x86_push_reg (p, X86_EAX);
443 x86_push_imm (p, sig->params [i]);
444 x86_call_reg (p, X86_ECX);
445 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 12);
446 stackval_pos += sizeof (stackval);
447 arg_pos += mono_type_stack_size (sig->params [i], &align);
451 * Handle the return value storage area.
453 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
454 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, retval)), X86_EAX, 4);
459 x86_lea_membase (p, X86_EAX, X86_EBP, MINV_POS);
460 x86_push_reg (p, X86_EAX);
461 x86_mov_reg_imm (p, X86_EDX, ves_exec_method);
462 x86_call_reg (p, X86_EDX);
465 * Move the return value to the proper place.
467 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
468 if (sig->ret->byref) {
469 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
471 switch (sig->ret->type) {
474 case MONO_TYPE_BOOLEAN:
475 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 1);
481 case MONO_TYPE_OBJECT:
482 case MONO_TYPE_STRING:
483 case MONO_TYPE_CLASS:
484 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
487 x86_mov_reg_membase (p, X86_EDX, X86_EAX, 4, 4);
488 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
491 x86_fld_membase (p, X86_EAX, 0, TRUE);
494 g_error ("Type 0x%x not handled yet in thunk creation", sig->ret->type);
505 g_assert (p - code_buffer < 512);
507 ji = g_new0 (MonoJitInfo, 1);
509 ji->code_size = p - code_buffer;
510 ji->code_start = g_memdup (code_buffer, p - code_buffer);
512 mono_jit_info_table_add (mono_root_domain, ji);
514 return ji->code_start;