2 * Create trampolines to invoke arbitrary functions.
4 * Copyright (C) Ximian Inc.
7 * Paolo Molaro (lupus@ximian.com)
8 * Dietmar Maurer (dietmar@ximian.com)
15 #include "x86-codegen.h"
16 #include "mono/metadata/class.h"
17 #include "mono/metadata/tabledefs.h"
18 #include "mono/interpreter/interp.h"
19 #include "mono/metadata/appdomain.h"
20 #include "mono/metadata/marshal.h"
23 * The resulting function takes the form:
24 * void func (void (*callme)(), void *retval, void *this_obj, stackval *arguments);
26 #define FUNC_ADDR_POS 8
32 #define ARG_SIZE sizeof (stackval)
35 mono_create_trampoline (MonoMethodSignature *sig, gboolean string_ctor)
37 unsigned char *p, *code_buffer;
38 guint32 local_size = 0, stack_size = 0, code_size = 50;
39 guint32 arg_pos, simpletype;
41 static GHashTable *cache = NULL;
45 cache = g_hash_table_new ((GHashFunc)mono_signature_hash,
46 (GCompareFunc)mono_metadata_signature_equal);
48 if ((res = (MonoPIFunc)g_hash_table_lookup (cache, sig)))
52 stack_size += sizeof (gpointer);
56 if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref && !sig->ret->data.klass->enumtype) {
57 stack_size += sizeof (gpointer);
61 for (i = 0; i < sig->param_count; ++i) {
62 if (sig->params [i]->byref) {
63 stack_size += sizeof (gpointer);
68 simpletype = sig->params [i]->type;
71 case MONO_TYPE_BOOLEAN:
83 case MONO_TYPE_SZARRAY:
85 case MONO_TYPE_OBJECT:
87 code_size += i < 10 ? 5 : 8;
89 case MONO_TYPE_VALUETYPE: {
91 if (sig->params [i]->data.klass->enumtype) {
92 simpletype = sig->params [i]->data.klass->enum_basetype->type;
95 if ((size = mono_class_value_size (sig->params [i]->data.klass, NULL)) != 4) {
96 stack_size += size + 3;
101 code_size += i < 10 ? 5 : 8;
105 case MONO_TYPE_STRING:
112 code_size += i < 10 ? 5 : 8;
116 code_size += i < 10 ? 7 : 10;
119 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
123 * FIXME: take into account large return values.
126 code_buffer = p = alloca (code_size);
129 * Standard function prolog.
131 x86_push_reg (p, X86_EBP);
132 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
134 * We store some local vars here to handle string pointers.
135 * and align to 16 byte boundary...
138 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size * 4);
139 stack_size = (stack_size * local_size * 4) % 16;
141 stack_size = stack_size % 16;
144 x86_alu_reg_imm (p, X86_SUB, X86_ESP, stack_size);
147 * EDX has the pointer to the args.
149 x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
152 * Push arguments in reverse order.
155 for (i = sig->param_count; i; --i) {
156 arg_pos = ARG_SIZE * (i - 1);
157 if (sig->params [i - 1]->byref) {
158 x86_push_membase (p, X86_EDX, arg_pos);
161 simpletype = sig->params [i - 1]->type;
163 switch (simpletype) {
164 case MONO_TYPE_BOOLEAN:
175 case MONO_TYPE_OBJECT:
176 x86_push_membase (p, X86_EDX, arg_pos);
179 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 4);
180 x86_fld_membase (p, X86_EDX, arg_pos, TRUE);
181 x86_fst_membase (p, X86_ESP, 0, FALSE, TRUE);
183 case MONO_TYPE_CLASS:
184 x86_push_membase (p, X86_EDX, arg_pos);
186 case MONO_TYPE_SZARRAY:
187 x86_push_membase (p, X86_EDX, arg_pos);
189 case MONO_TYPE_VALUETYPE:
190 if (!sig->params [i - 1]->data.klass->enumtype) {
191 int size = mono_class_value_size (sig->params [i - 1]->data.klass, NULL);
193 /* it's a structure that fits in 4 bytes, need to push the value pointed to */
194 x86_mov_reg_membase (p, X86_EAX, X86_EDX, arg_pos, 4);
195 x86_push_regp (p, X86_EAX);
201 x86_alu_reg_imm (p, X86_SUB, X86_ESP, ss);
202 x86_push_imm (p, size);
203 x86_push_membase (p, X86_EDX, arg_pos);
204 x86_lea_membase (p, X86_EAX, X86_ESP, 2*4);
205 x86_push_reg (p, X86_EAX);
206 x86_mov_reg_imm (p, X86_EAX, memcpy);
207 x86_call_reg (p, X86_EAX);
208 x86_alu_reg_imm (p, X86_ADD, X86_ESP, 12);
211 /* it's an enum value */
212 simpletype = sig->params [i - 1]->data.klass->enum_basetype->type;
216 case MONO_TYPE_STRING:
217 x86_push_membase (p, X86_EDX, arg_pos);
222 x86_push_membase (p, X86_EDX, arg_pos + 4);
223 x86_push_membase (p, X86_EDX, arg_pos);
226 g_error ("Can't trampoline 0x%x", sig->params [i - 1]->type);
231 if (sig->call_convention != MONO_CALL_THISCALL) {
232 x86_mov_reg_membase (p, X86_EDX, X86_EBP, THIS_POS, 4);
233 x86_push_reg (p, X86_EDX);
235 x86_mov_reg_membase (p, X86_ECX, X86_EBP, THIS_POS, 4);
239 if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref) {
240 MonoClass *klass = sig->ret->data.klass;
241 if (!klass->enumtype) {
242 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
243 x86_push_membase (p, X86_ECX, 0);
248 * Insert call to function
250 x86_mov_reg_membase (p, X86_EDX, X86_EBP, FUNC_ADDR_POS, 4);
251 x86_call_reg (p, X86_EDX);
255 * Small integer and pointer values are in EAX.
256 * Long integers are in EAX:EDX.
257 * FP values are on the FP stack.
260 if (sig->ret->byref || string_ctor) {
261 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
262 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
264 simpletype = sig->ret->type;
266 switch (simpletype) {
267 case MONO_TYPE_BOOLEAN:
270 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
271 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 1);
276 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
277 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 2);
283 case MONO_TYPE_CLASS:
284 case MONO_TYPE_OBJECT:
285 case MONO_TYPE_SZARRAY:
286 case MONO_TYPE_ARRAY:
287 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
288 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
290 case MONO_TYPE_STRING:
291 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
292 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
295 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
296 x86_fst_membase (p, X86_ECX, 0, FALSE, TRUE);
299 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
300 x86_fst_membase (p, X86_ECX, 0, TRUE, TRUE);
303 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
304 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
305 x86_mov_membase_reg (p, X86_ECX, 4, X86_EDX, 4);
307 case MONO_TYPE_VALUETYPE:
308 if (sig->ret->data.klass->enumtype) {
309 simpletype = sig->ret->data.klass->enum_basetype->type;
315 g_error ("Can't handle as return value 0x%x", sig->ret->type);
325 g_assert (p - code_buffer < code_size);
326 res = (MonoPIFunc)g_memdup (code_buffer, p - code_buffer);
328 g_hash_table_insert (cache, sig, res);
333 #define MINV_POS (- sizeof (MonoInvocation))
334 #define STACK_POS (MINV_POS - sizeof (stackval) * sig->param_count)
335 #define TYPE_OFFSET (G_STRUCT_OFFSET (stackval, type))
338 * Returns a pointer to a native function that can be used to
339 * call the specified method.
340 * The function created will receive the arguments according
341 * to the call convention specified in the method.
342 * This function works by creating a MonoInvocation structure,
343 * filling the fields in and calling ves_exec_method on it.
344 * Still need to figure out how to handle the exception stuff
345 * across the managed/unmanaged boundary.
348 mono_create_method_pointer (MonoMethod *method)
350 MonoMethodSignature *sig;
352 unsigned char *p, *code_buffer;
354 gint32 stackval_pos, arg_pos = 8;
355 int i, size, align, cpos;
356 int vtbuf [sig->param_count];
359 * If it is a static P/Invoke method, we can just return the pointer
360 * to the method implementation.
362 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && method->addr) {
363 ji = g_new0 (MonoJitInfo, 1);
366 ji->code_start = method->addr;
368 mono_jit_info_table_add (mono_root_domain, ji);
372 sig = method->signature;
374 code_buffer = p = alloca (512); /* FIXME: check for overflows... */
376 local_size = sizeof (MonoInvocation) + sizeof (stackval) * (sig->param_count + 1);
381 stackval_pos = -local_size;
384 for (i = 0; i < sig->param_count; i++) {
385 MonoType *type = sig->params [i];
387 if (type->type == MONO_TYPE_VALUETYPE) {
388 MonoClass *klass = type->data.klass;
391 size = mono_class_native_size (klass, &align);
393 cpos &= ~(align - 1);
405 * Standard function prolog.
407 x86_push_reg (p, X86_EBP);
408 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
409 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size);
412 * Initialize MonoInvocation fields, first the ones known now.
414 x86_mov_reg_imm (p, X86_EAX, 0);
415 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex)), X86_EAX, 4);
416 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex_handler)), X86_EAX, 4);
417 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, child)), X86_EAX, 4);
418 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, parent)), X86_EAX, 4);
420 * Set the method pointer.
422 x86_mov_membase_imm (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, method)), (int)method, 4);
424 if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref && !sig->ret->data.klass->enumtype)
431 if (sig->call_convention != MONO_CALL_THISCALL) {
433 * Grab it from the stack, otherwise it's already in ECX.
435 x86_mov_reg_membase (p, X86_ECX, X86_EBP, arg_pos, 4);
438 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, obj)), X86_ECX, 4);
441 * Handle the arguments. stackval_pos is the posset of the stackval array from EBP.
442 * arg_pos is the offset from EBP to the incoming arg on the stack.
443 * We just call stackval_from_data to handle all the (nasty) issues....
445 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
446 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, stack_args)), X86_EAX, 4);
447 for (i = 0; i < sig->param_count; ++i) {
448 if (vtbuf [i] >= 0) {
449 x86_lea_membase (p, X86_EAX, X86_EBP, - local_size + vtbuf [i]);
450 x86_mov_membase_reg (p, X86_EBP, stackval_pos, X86_EAX, 4);
452 x86_mov_reg_imm (p, X86_ECX, stackval_from_data);
453 x86_lea_membase (p, X86_EDX, X86_EBP, arg_pos);
454 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
455 x86_push_imm (p, sig->pinvoke);
456 x86_push_reg (p, X86_EDX);
457 x86_push_reg (p, X86_EAX);
458 x86_push_imm (p, sig->params [i]);
459 x86_call_reg (p, X86_ECX);
460 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 16);
461 stackval_pos += sizeof (stackval);
462 /* fixme: alignment */
464 arg_pos += mono_type_native_stack_size (sig->params [i], &align);
466 arg_pos += mono_type_stack_size (sig->params [i], &align);
470 * Handle the return value storage area.
472 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
473 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, retval)), X86_EAX, 4);
474 if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref) {
475 MonoClass *klass = sig->ret->data.klass;
476 if (!klass->enumtype) {
477 x86_mov_reg_membase (p, X86_ECX, X86_EBP, 8, 4);
478 x86_mov_membase_reg (p, X86_EBP, stackval_pos, X86_ECX, 4);
485 x86_lea_membase (p, X86_EAX, X86_EBP, MINV_POS);
486 x86_push_reg (p, X86_EAX);
487 x86_mov_reg_imm (p, X86_EDX, ves_exec_method);
488 x86_call_reg (p, X86_EDX);
491 * Move the return value to the proper place.
493 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
494 if (sig->ret->byref) {
495 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
497 int simpletype = sig->ret->type;
499 switch (sig->ret->type) {
502 case MONO_TYPE_BOOLEAN:
503 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 1);
509 case MONO_TYPE_OBJECT:
510 case MONO_TYPE_STRING:
511 case MONO_TYPE_CLASS:
512 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
515 x86_mov_reg_membase (p, X86_EDX, X86_EAX, 4, 4);
516 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
519 x86_fld_membase (p, X86_EAX, 0, TRUE);
521 case MONO_TYPE_VALUETYPE:
522 if (sig->ret->data.klass->enumtype) {
523 simpletype = sig->ret->data.klass->enum_basetype->type;
527 x86_push_imm (p, sig->pinvoke);
528 x86_push_membase (p, X86_EBP, stackval_pos);
529 x86_push_reg (p, X86_EAX);
530 x86_push_imm (p, sig->ret);
531 x86_mov_reg_imm (p, X86_ECX, stackval_to_data);
532 x86_call_reg (p, X86_ECX);
533 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 16);
537 g_error ("Type 0x%x not handled yet in thunk creation", sig->ret->type);
548 g_assert (p - code_buffer < 512);
550 ji = g_new0 (MonoJitInfo, 1);
552 ji->code_size = p - code_buffer;
553 ji->code_start = g_memdup (code_buffer, p - code_buffer);
555 mono_jit_info_table_add (mono_root_domain, ji);
557 return ji->code_start;