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 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);
67 simpletype = sig->params [i]->type;
70 case MONO_TYPE_BOOLEAN:
82 case MONO_TYPE_SZARRAY:
84 case MONO_TYPE_OBJECT:
85 case MONO_TYPE_STRING:
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;
107 code_size += i < 10 ? 5 : 8;
111 code_size += i < 10 ? 7 : 10;
114 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
118 * FIXME: take into account large return values.
121 code_buffer = p = alloca (code_size);
124 * Standard function prolog.
126 x86_push_reg (p, X86_EBP);
127 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
129 * and align to 16 byte boundary...
135 x86_alu_reg_imm (p, X86_SUB, X86_ESP, stack_size);
138 * EDX has the pointer to the args.
140 x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
143 * Push arguments in reverse order.
146 for (i = sig->param_count; i; --i) {
147 arg_pos = ARG_SIZE * (i - 1);
148 if (sig->params [i - 1]->byref) {
149 x86_push_membase (p, X86_EDX, arg_pos);
152 simpletype = sig->params [i - 1]->type;
154 switch (simpletype) {
155 case MONO_TYPE_BOOLEAN:
166 case MONO_TYPE_OBJECT:
167 case MONO_TYPE_STRING:
168 x86_push_membase (p, X86_EDX, arg_pos);
171 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 4);
172 x86_fld_membase (p, X86_EDX, arg_pos, TRUE);
173 x86_fst_membase (p, X86_ESP, 0, FALSE, TRUE);
175 case MONO_TYPE_CLASS:
176 x86_push_membase (p, X86_EDX, arg_pos);
178 case MONO_TYPE_SZARRAY:
179 x86_push_membase (p, X86_EDX, arg_pos);
181 case MONO_TYPE_VALUETYPE:
182 if (!sig->params [i - 1]->data.klass->enumtype) {
183 int size = mono_class_value_size (sig->params [i - 1]->data.klass, NULL);
185 /* it's a structure that fits in 4 bytes, need to push the value pointed to */
186 x86_mov_reg_membase (p, X86_EAX, X86_EDX, arg_pos, 4);
187 x86_push_regp (p, X86_EAX);
193 x86_alu_reg_imm (p, X86_SUB, X86_ESP, ss);
194 x86_push_imm (p, size);
195 x86_push_membase (p, X86_EDX, arg_pos);
196 x86_lea_membase (p, X86_EAX, X86_ESP, 2*4);
197 x86_push_reg (p, X86_EAX);
198 x86_mov_reg_imm (p, X86_EAX, memcpy);
199 x86_call_reg (p, X86_EAX);
200 x86_alu_reg_imm (p, X86_ADD, X86_ESP, 12);
203 /* it's an enum value */
204 simpletype = sig->params [i - 1]->data.klass->enum_basetype->type;
211 x86_push_membase (p, X86_EDX, arg_pos + 4);
212 x86_push_membase (p, X86_EDX, arg_pos);
215 g_error ("Can't trampoline 0x%x", sig->params [i - 1]->type);
220 if (sig->call_convention != MONO_CALL_THISCALL) {
221 x86_mov_reg_membase (p, X86_EDX, X86_EBP, THIS_POS, 4);
222 x86_push_reg (p, X86_EDX);
224 x86_mov_reg_membase (p, X86_ECX, X86_EBP, THIS_POS, 4);
228 if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref) {
229 MonoClass *klass = sig->ret->data.klass;
230 if (!klass->enumtype) {
231 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
232 x86_push_membase (p, X86_ECX, 0);
237 * Insert call to function
239 x86_mov_reg_membase (p, X86_EDX, X86_EBP, FUNC_ADDR_POS, 4);
240 x86_call_reg (p, X86_EDX);
244 * Small integer and pointer values are in EAX.
245 * Long integers are in EAX:EDX.
246 * FP values are on the FP stack.
249 if (sig->ret->byref || string_ctor) {
250 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
251 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
253 simpletype = sig->ret->type;
255 switch (simpletype) {
256 case MONO_TYPE_BOOLEAN:
259 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
260 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 1);
265 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
266 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 2);
272 case MONO_TYPE_CLASS:
273 case MONO_TYPE_OBJECT:
274 case MONO_TYPE_SZARRAY:
275 case MONO_TYPE_ARRAY:
276 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
277 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
279 case MONO_TYPE_STRING:
280 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
281 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
284 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
285 x86_fst_membase (p, X86_ECX, 0, FALSE, TRUE);
288 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
289 x86_fst_membase (p, X86_ECX, 0, TRUE, TRUE);
292 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
293 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
294 x86_mov_membase_reg (p, X86_ECX, 4, X86_EDX, 4);
296 case MONO_TYPE_VALUETYPE:
297 if (sig->ret->data.klass->enumtype) {
298 simpletype = sig->ret->data.klass->enum_basetype->type;
304 g_error ("Can't handle as return value 0x%x", sig->ret->type);
314 g_assert (p - code_buffer < code_size);
315 res = (MonoPIFunc)g_memdup (code_buffer, p - code_buffer);
317 g_hash_table_insert (cache, sig, res);
322 #define MINV_POS (- sizeof (MonoInvocation))
323 #define STACK_POS (MINV_POS - sizeof (stackval) * sig->param_count)
324 #define TYPE_OFFSET (G_STRUCT_OFFSET (stackval, type))
327 * Returns a pointer to a native function that can be used to
328 * call the specified method.
329 * The function created will receive the arguments according
330 * to the call convention specified in the method.
331 * This function works by creating a MonoInvocation structure,
332 * filling the fields in and calling ves_exec_method on it.
333 * Still need to figure out how to handle the exception stuff
334 * across the managed/unmanaged boundary.
337 mono_create_method_pointer (MonoMethod *method)
339 MonoMethodSignature *sig;
341 unsigned char *p, *code_buffer;
343 gint32 stackval_pos, arg_pos = 8;
344 int i, size, align, cpos;
345 int vtbuf [sig->param_count];
348 * If it is a static P/Invoke method, we can just return the pointer
349 * to the method implementation.
351 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && method->addr) {
352 ji = g_new0 (MonoJitInfo, 1);
355 ji->code_start = method->addr;
357 mono_jit_info_table_add (mono_root_domain, ji);
361 sig = method->signature;
363 code_buffer = p = alloca (512); /* FIXME: check for overflows... */
365 local_size = sizeof (MonoInvocation) + sizeof (stackval) * (sig->param_count + 1);
370 stackval_pos = -local_size;
373 for (i = 0; i < sig->param_count; i++) {
374 MonoType *type = sig->params [i];
376 if (type->type == MONO_TYPE_VALUETYPE) {
377 MonoClass *klass = type->data.klass;
380 size = mono_class_native_size (klass, &align);
382 cpos &= ~(align - 1);
394 * Standard function prolog.
396 x86_push_reg (p, X86_EBP);
397 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
398 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size);
401 * Initialize MonoInvocation fields, first the ones known now.
403 x86_mov_reg_imm (p, X86_EAX, 0);
404 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex)), X86_EAX, 4);
405 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex_handler)), X86_EAX, 4);
406 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, child)), X86_EAX, 4);
407 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, parent)), X86_EAX, 4);
409 * Set the method pointer.
411 x86_mov_membase_imm (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, method)), (int)method, 4);
413 if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref && !sig->ret->data.klass->enumtype)
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, arg_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 if (vtbuf [i] >= 0) {
438 x86_lea_membase (p, X86_EAX, X86_EBP, - local_size + vtbuf [i]);
439 x86_mov_membase_reg (p, X86_EBP, stackval_pos, X86_EAX, 4);
441 x86_mov_reg_imm (p, X86_ECX, stackval_from_data);
442 x86_lea_membase (p, X86_EDX, X86_EBP, arg_pos);
443 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
444 x86_push_imm (p, sig->pinvoke);
445 x86_push_reg (p, X86_EDX);
446 x86_push_reg (p, X86_EAX);
447 x86_push_imm (p, sig->params [i]);
448 x86_call_reg (p, X86_ECX);
449 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 16);
450 stackval_pos += sizeof (stackval);
451 /* fixme: alignment */
453 arg_pos += mono_type_native_stack_size (sig->params [i], &align);
455 arg_pos += mono_type_stack_size (sig->params [i], &align);
459 * Handle the return value storage area.
461 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
462 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, retval)), X86_EAX, 4);
463 if (sig->ret->type == MONO_TYPE_VALUETYPE && !sig->ret->byref) {
464 MonoClass *klass = sig->ret->data.klass;
465 if (!klass->enumtype) {
466 x86_mov_reg_membase (p, X86_ECX, X86_EBP, 8, 4);
467 x86_mov_membase_reg (p, X86_EBP, stackval_pos, X86_ECX, 4);
474 x86_lea_membase (p, X86_EAX, X86_EBP, MINV_POS);
475 x86_push_reg (p, X86_EAX);
476 x86_mov_reg_imm (p, X86_EDX, ves_exec_method);
477 x86_call_reg (p, X86_EDX);
480 * Move the return value to the proper place.
482 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
483 if (sig->ret->byref) {
484 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
486 int simpletype = sig->ret->type;
488 switch (sig->ret->type) {
491 case MONO_TYPE_BOOLEAN:
492 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 1);
498 case MONO_TYPE_OBJECT:
499 case MONO_TYPE_STRING:
500 case MONO_TYPE_CLASS:
501 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
504 x86_mov_reg_membase (p, X86_EDX, X86_EAX, 4, 4);
505 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
508 x86_fld_membase (p, X86_EAX, 0, TRUE);
510 case MONO_TYPE_VALUETYPE:
511 if (sig->ret->data.klass->enumtype) {
512 simpletype = sig->ret->data.klass->enum_basetype->type;
516 x86_push_imm (p, sig->pinvoke);
517 x86_push_membase (p, X86_EBP, stackval_pos);
518 x86_push_reg (p, X86_EAX);
519 x86_push_imm (p, sig->ret);
520 x86_mov_reg_imm (p, X86_ECX, stackval_to_data);
521 x86_call_reg (p, X86_ECX);
522 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 16);
526 g_error ("Type 0x%x not handled yet in thunk creation", sig->ret->type);
537 g_assert (p - code_buffer < 512);
539 ji = g_new0 (MonoJitInfo, 1);
541 ji->code_size = p - code_buffer;
542 ji->code_start = g_memdup (code_buffer, p - code_buffer);
544 mono_jit_info_table_add (mono_root_domain, ji);
546 return ji->code_start;