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 (MonoMethodSignature *sig, gboolean string_ctor)
35 unsigned char *p, *code_buffer;
36 guint32 local_size = 0, stack_size = 0, code_size = 50;
37 guint32 arg_pos, simpletype;
39 static GHashTable *cache = NULL;
43 cache = g_hash_table_new ((GHashFunc)mono_signature_hash,
44 (GCompareFunc)mono_metadata_signature_equal);
46 if ((res = (MonoPIFunc)g_hash_table_lookup (cache, sig)))
50 stack_size += sizeof (gpointer);
54 if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref && !sig->ret->data.klass->enumtype) {
55 stack_size += sizeof (gpointer);
59 for (i = 0; i < sig->param_count; ++i) {
60 if (sig->params [i]->byref) {
61 stack_size += sizeof (gpointer);
66 simpletype = sig->params [i]->type;
69 case MONO_TYPE_BOOLEAN:
81 case MONO_TYPE_SZARRAY:
83 case MONO_TYPE_OBJECT:
85 code_size += i < 10 ? 5 : 8;
87 case MONO_TYPE_VALUETYPE: {
89 if (sig->params [i]->data.klass->enumtype) {
90 simpletype = sig->params [i]->data.klass->enum_basetype->type;
93 if ((size = mono_class_value_size (sig->params [i]->data.klass, NULL)) != 4) {
94 stack_size += size + 3;
99 code_size += i < 10 ? 5 : 8;
103 case MONO_TYPE_STRING:
110 code_size += i < 10 ? 5 : 8;
114 code_size += i < 10 ? 7 : 10;
117 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
121 * FIXME: take into account large return values.
124 code_buffer = p = alloca (code_size);
127 * Standard function prolog.
129 x86_push_reg (p, X86_EBP);
130 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
132 * We store some local vars here to handle string pointers.
133 * and align to 16 byte boundary...
136 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size * 4);
137 stack_size = (stack_size * local_size * 4) % 16;
139 stack_size = stack_size % 16;
142 x86_alu_reg_imm (p, X86_SUB, X86_ESP, stack_size);
145 * EDX has the pointer to the args.
147 x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
150 * Push arguments in reverse order.
153 for (i = sig->param_count; i; --i) {
154 arg_pos = ARG_SIZE * (i - 1);
155 if (sig->params [i - 1]->byref) {
156 x86_push_membase (p, X86_EDX, arg_pos);
159 simpletype = sig->params [i - 1]->type;
161 switch (simpletype) {
162 case MONO_TYPE_BOOLEAN:
173 case MONO_TYPE_OBJECT:
174 x86_push_membase (p, X86_EDX, arg_pos);
177 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 4);
178 x86_fld_membase (p, X86_EDX, arg_pos, TRUE);
179 x86_fst_membase (p, X86_ESP, 0, FALSE, TRUE);
181 case MONO_TYPE_CLASS:
182 x86_push_membase (p, X86_EDX, arg_pos);
184 case MONO_TYPE_SZARRAY:
185 x86_push_membase (p, X86_EDX, arg_pos);
187 case MONO_TYPE_VALUETYPE:
188 if (!sig->params [i - 1]->data.klass->enumtype) {
189 int size = mono_class_value_size (sig->params [i - 1]->data.klass, NULL);
191 /* it's a structure that fits in 4 bytes, need to push the value pointed to */
192 x86_mov_reg_membase (p, X86_EAX, X86_EDX, arg_pos, 4);
193 x86_push_regp (p, X86_EAX);
199 x86_alu_reg_imm (p, X86_SUB, X86_ESP, ss);
200 x86_push_imm (p, size);
201 x86_push_membase (p, X86_EDX, arg_pos);
202 x86_lea_membase (p, X86_EAX, X86_ESP, 2*4);
203 x86_push_reg (p, X86_EAX);
204 x86_mov_reg_imm (p, X86_EAX, memcpy);
205 x86_call_reg (p, X86_EAX);
206 x86_alu_reg_imm (p, X86_ADD, X86_ESP, 12);
209 /* it's an enum value */
210 simpletype = sig->params [i - 1]->data.klass->enum_basetype->type;
214 case MONO_TYPE_STRING:
215 x86_push_membase (p, X86_EDX, arg_pos);
220 x86_push_membase (p, X86_EDX, arg_pos + 4);
221 x86_push_membase (p, X86_EDX, arg_pos);
224 g_error ("Can't trampoline 0x%x", sig->params [i - 1]->type);
229 if (sig->call_convention != MONO_CALL_THISCALL) {
230 x86_mov_reg_membase (p, X86_EDX, X86_EBP, THIS_POS, 4);
231 x86_push_reg (p, X86_EDX);
233 x86_mov_reg_membase (p, X86_ECX, X86_EBP, THIS_POS, 4);
237 if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref) {
238 MonoClass *klass = sig->ret->data.klass;
239 if (!klass->enumtype) {
240 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
241 x86_push_membase (p, X86_ECX, 0);
246 * Insert call to function
248 x86_mov_reg_membase (p, X86_EDX, X86_EBP, FUNC_ADDR_POS, 4);
249 x86_call_reg (p, X86_EDX);
253 * Small integer and pointer values are in EAX.
254 * Long integers are in EAX:EDX.
255 * FP values are on the FP stack.
258 if (sig->ret->byref || string_ctor) {
259 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
260 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
262 simpletype = sig->ret->type;
264 switch (simpletype) {
265 case MONO_TYPE_BOOLEAN:
268 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
269 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 1);
274 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
275 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 2);
281 case MONO_TYPE_CLASS:
282 case MONO_TYPE_OBJECT:
283 case MONO_TYPE_SZARRAY:
284 case MONO_TYPE_ARRAY:
285 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
286 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
288 case MONO_TYPE_STRING:
289 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
290 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
293 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
294 x86_fst_membase (p, X86_ECX, 0, FALSE, TRUE);
297 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
298 x86_fst_membase (p, X86_ECX, 0, TRUE, TRUE);
301 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
302 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
303 x86_mov_membase_reg (p, X86_ECX, 4, X86_EDX, 4);
305 case MONO_TYPE_VALUETYPE:
306 if (sig->ret->data.klass->enumtype) {
307 simpletype = sig->ret->data.klass->enum_basetype->type;
313 g_error ("Can't handle as return value 0x%x", sig->ret->type);
323 g_assert (p - code_buffer < code_size);
324 res = (MonoPIFunc)g_memdup (code_buffer, p - code_buffer);
326 g_hash_table_insert (cache, sig, res);
331 #define MINV_POS (- sizeof (MonoInvocation))
332 #define STACK_POS (MINV_POS - sizeof (stackval) * sig->param_count)
333 #define TYPE_OFFSET (G_STRUCT_OFFSET (stackval, type))
336 * Returns a pointer to a native function that can be used to
337 * call the specified method.
338 * The function created will receive the arguments according
339 * to the call convention specified in the method.
340 * This function works by creating a MonoInvocation structure,
341 * filling the fields in and calling ves_exec_method on it.
342 * Still need to figure out how to handle the exception stuff
343 * across the managed/unmanaged boundary.
346 mono_create_method_pointer (MonoMethod *method)
348 MonoMethodSignature *sig;
350 unsigned char *p, *code_buffer;
352 gint32 stackval_pos, arg_pos = 8;
353 int i, size, align, cpos;
354 int vtbuf [sig->param_count];
357 * If it is a static P/Invoke method, we can just return the pointer
358 * to the method implementation.
360 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && method->addr) {
361 ji = g_new0 (MonoJitInfo, 1);
364 ji->code_start = method->addr;
366 mono_jit_info_table_add (mono_root_domain, ji);
370 sig = method->signature;
372 code_buffer = p = alloca (512); /* FIXME: check for overflows... */
374 local_size = sizeof (MonoInvocation) + sizeof (stackval) * (sig->param_count + 1);
379 stackval_pos = -local_size;
382 for (i = 0; i < sig->param_count; i++) {
383 MonoType *type = sig->params [i];
385 if (type->type == MONO_TYPE_VALUETYPE) {
386 MonoClass *klass = type->data.klass;
389 size = mono_class_native_size (klass, &align);
391 cpos &= ~(align - 1);
403 * Standard function prolog.
405 x86_push_reg (p, X86_EBP);
406 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
407 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size);
410 * Initialize MonoInvocation fields, first the ones known now.
412 x86_mov_reg_imm (p, X86_EAX, 0);
413 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex)), X86_EAX, 4);
414 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex_handler)), X86_EAX, 4);
415 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, child)), X86_EAX, 4);
416 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, parent)), X86_EAX, 4);
418 * Set the method pointer.
420 x86_mov_membase_imm (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, method)), (int)method, 4);
422 if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref && !sig->ret->data.klass->enumtype)
429 if (sig->call_convention != MONO_CALL_THISCALL) {
431 * Grab it from the stack, otherwise it's already in ECX.
433 x86_mov_reg_membase (p, X86_ECX, X86_EBP, arg_pos, 4);
436 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, obj)), X86_ECX, 4);
439 * Handle the arguments. stackval_pos is the posset of the stackval array from EBP.
440 * arg_pos is the offset from EBP to the incoming arg on the stack.
441 * We just call stackval_from_data to handle all the (nasty) issues....
443 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
444 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, stack_args)), X86_EAX, 4);
445 for (i = 0; i < sig->param_count; ++i) {
446 if (vtbuf [i] >= 0) {
447 x86_lea_membase (p, X86_EAX, X86_EBP, - local_size + vtbuf [i]);
448 x86_mov_membase_reg (p, X86_EBP, stackval_pos, X86_EAX, 4);
450 x86_mov_reg_imm (p, X86_ECX, stackval_from_data);
451 x86_lea_membase (p, X86_EDX, X86_EBP, arg_pos);
452 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
453 x86_push_imm (p, sig->pinvoke);
454 x86_push_reg (p, X86_EDX);
455 x86_push_reg (p, X86_EAX);
456 x86_push_imm (p, sig->params [i]);
457 x86_call_reg (p, X86_ECX);
458 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 16);
459 stackval_pos += sizeof (stackval);
460 /* fixme: alignment */
462 arg_pos += mono_type_native_stack_size (sig->params [i], &align);
464 arg_pos += mono_type_stack_size (sig->params [i], &align);
468 * Handle the return value storage area.
470 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
471 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, retval)), X86_EAX, 4);
472 if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref) {
473 MonoClass *klass = sig->ret->data.klass;
474 if (!klass->enumtype) {
475 x86_mov_reg_membase (p, X86_ECX, X86_EBP, 8, 4);
476 x86_mov_membase_reg (p, X86_EBP, stackval_pos, X86_ECX, 4);
483 x86_lea_membase (p, X86_EAX, X86_EBP, MINV_POS);
484 x86_push_reg (p, X86_EAX);
485 x86_mov_reg_imm (p, X86_EDX, ves_exec_method);
486 x86_call_reg (p, X86_EDX);
489 * Move the return value to the proper place.
491 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
492 if (sig->ret->byref) {
493 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
495 int simpletype = sig->ret->type;
497 switch (sig->ret->type) {
500 case MONO_TYPE_BOOLEAN:
501 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 1);
507 case MONO_TYPE_OBJECT:
508 case MONO_TYPE_STRING:
509 case MONO_TYPE_CLASS:
510 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
513 x86_mov_reg_membase (p, X86_EDX, X86_EAX, 4, 4);
514 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
517 x86_fld_membase (p, X86_EAX, 0, TRUE);
519 case MONO_TYPE_VALUETYPE:
520 if (sig->ret->data.klass->enumtype) {
521 simpletype = sig->ret->data.klass->enum_basetype->type;
525 x86_push_imm (p, sig->pinvoke);
526 x86_push_membase (p, X86_EBP, stackval_pos);
527 x86_push_reg (p, X86_EAX);
528 x86_push_imm (p, sig->ret);
529 x86_mov_reg_imm (p, X86_ECX, stackval_to_data);
530 x86_call_reg (p, X86_ECX);
531 //x86_breakpoint (p);
532 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 16);
536 g_error ("Type 0x%x not handled yet in thunk creation", sig->ret->type);
547 g_assert (p - code_buffer < 512);
549 ji = g_new0 (MonoJitInfo, 1);
551 ji->code_size = p - code_buffer;
552 ji->code_start = g_memdup (code_buffer, p - code_buffer);
554 mono_jit_info_table_add (mono_root_domain, ji);
556 return ji->code_start;