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"
18 * The resulting function takes the form:
19 * void func (void (*callme)(), void *retval, void *this_obj, stackval *arguments);
21 #define FUNC_ADDR_POS 8
27 #define ARG_SIZE sizeof (stackval)
30 mono_create_trampoline (MonoMethod *method)
32 MonoMethodSignature *sig;
33 unsigned char *p, *code_buffer;
34 guint32 local_size = 0, stack_size = 0, code_size = 30;
38 sig = method->signature;
41 stack_size += sizeof (gpointer);
45 for (i = 0; i < sig->param_count; ++i) {
46 if (sig->params [i]->byref) {
47 stack_size += sizeof (gpointer);
48 code_size += i < 10 ? 5 : 8;
51 switch (sig->params [i]->type) {
52 case MONO_TYPE_BOOLEAN:
64 case MONO_TYPE_SZARRAY:
66 case MONO_TYPE_OBJECT:
68 code_size += i < 10 ? 5 : 8;
70 case MONO_TYPE_VALUETYPE:
71 if (!sig->params [i]->data.klass->enumtype && (mono_class_value_size (sig->params [i]->data.klass, NULL) != 4))
72 g_error ("can only marshal enums, not generic structures (size: %d)", mono_class_value_size (sig->params [i]->data.klass, NULL));
74 code_size += i < 10 ? 5 : 8;
76 case MONO_TYPE_STRING:
83 code_size += i < 10 ? 5 : 8;
87 code_size += i < 10 ? 7 : 10;
90 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
94 * FIXME: take into account large return values.
97 code_buffer = p = alloca (code_size);
100 * Standard function prolog.
102 x86_push_reg (p, X86_EBP);
103 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
105 * We store some local vars here to handle string pointers.
106 * and align to 16 byte boundary...
109 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size * 4);
110 stack_size = (stack_size * local_size * 4) % 16;
112 stack_size = stack_size % 16;
115 x86_alu_reg_imm (p, X86_SUB, X86_ESP, stack_size);
118 * EDX has the pointer to the args.
120 x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
123 * Push arguments in reverse order.
126 for (i = sig->param_count; i; --i) {
127 arg_pos = ARG_SIZE * (i - 1);
128 if (sig->params [i - 1]->byref) {
129 x86_push_membase (p, X86_EDX, arg_pos);
132 switch (sig->params [i - 1]->type) {
133 case MONO_TYPE_BOOLEAN:
144 case MONO_TYPE_SZARRAY:
145 case MONO_TYPE_CLASS:
146 case MONO_TYPE_OBJECT:
148 x86_push_membase (p, X86_EDX, arg_pos);
150 case MONO_TYPE_VALUETYPE:
151 if (!sig->params [i - 1]->data.klass->enumtype) {
152 /* it's a structure that fits in 4 bytes, need to push the value pointed to */
153 x86_mov_reg_membase (p, X86_EAX, X86_EDX, arg_pos, 4);
154 x86_push_regp (p, X86_EAX);
156 /* it's an enum value */
157 x86_push_membase (p, X86_EDX, arg_pos);
161 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 8);
162 x86_fld_membase (p, X86_EDX, arg_pos, TRUE);
163 x86_fst_membase (p, X86_ESP, 0, TRUE, TRUE);
165 case MONO_TYPE_STRING:
167 * If it is an internalcall we assume it's the object we want.
168 * Yet another reason why MONO_TYPE_STRING should not be used to indicate char*.
170 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
171 x86_push_membase (p, X86_EDX, arg_pos);
174 /*if (frame->method->flags & PINVOKE_ATTRIBUTE_CHAR_SET_ANSI*/
175 x86_push_membase (p, X86_EDX, arg_pos);
176 x86_mov_reg_imm (p, X86_EDX, mono_string_to_utf8);
177 x86_call_reg (p, X86_EDX);
178 x86_alu_reg_imm (p, X86_ADD, X86_ESP, 4);
179 x86_push_reg (p, X86_EAX);
181 * Store the pointer in a local we'll free later.
184 x86_mov_membase_reg (p, X86_EBP, LOC_POS * stringp, X86_EAX, 4);
186 * we didn't save the reg: restore it here.
189 x86_mov_reg_membase (p, X86_EDX, X86_EBP, ARGP_POS, 4);
192 x86_push_membase (p, X86_EDX, arg_pos + 4);
193 x86_push_membase (p, X86_EDX, arg_pos);
196 g_error ("Can't trampoline 0x%x", sig->params [i - 1]->type);
201 if (sig->call_convention != MONO_CALL_THISCALL) {
202 x86_mov_reg_membase (p, X86_EDX, X86_EBP, THIS_POS, 4);
203 x86_push_reg (p, X86_EDX);
205 x86_mov_reg_membase (p, X86_ECX, X86_EBP, THIS_POS, 4);
210 * Insert call to function
212 x86_mov_reg_membase (p, X86_EDX, X86_EBP, FUNC_ADDR_POS, 4);
213 x86_call_reg (p, X86_EDX);
217 * Small integer and pointer values are in EAX.
218 * Long integers are in EAX:EDX.
219 * FP values are on the FP stack.
221 if (sig->ret->byref) {
222 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
223 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
225 switch (sig->ret->type) {
226 case MONO_TYPE_BOOLEAN:
229 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
230 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 1);
236 case MONO_TYPE_CLASS:
237 case MONO_TYPE_OBJECT:
238 case MONO_TYPE_STRING: /* this is going to cause large pains... */
239 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
240 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
243 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
244 x86_fst_membase (p, X86_ECX, 0, FALSE, TRUE);
247 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
248 x86_fst_membase (p, X86_ECX, 0, TRUE, TRUE);
251 x86_mov_reg_membase (p, X86_ECX, X86_EBP, RETVAL_POS, 4);
252 x86_mov_regp_reg (p, X86_ECX, X86_EAX, 4);
253 x86_mov_membase_reg (p, X86_ECX, 4, X86_EDX, 4);
258 g_error ("Can't handle as return value 0x%x", sig->ret->type);
263 * free the allocated strings.
265 if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
267 x86_mov_reg_imm (p, X86_EDX, g_free);
268 for (i = 1; i <= local_size; ++i) {
269 x86_push_membase (p, X86_EBP, LOC_POS * i);
270 x86_call_reg (p, X86_EDX);
279 return g_memdup (code_buffer, p - code_buffer);
282 #define MINV_POS (- sizeof (MonoInvocation))
283 #define STACK_POS (MINV_POS - sizeof (stackval) * sig->param_count)
285 #define TYPE_OFFSET (G_STRUCT_OFFSET (stackval, type))
288 * Returns a pointer to a native function that can be used to
289 * call the specified method.
290 * The function created will receive the arguments according
291 * to the call convention specified in the method.
292 * This function works by creating a MonoInvocation structure,
293 * filling the fields in and calling ves_exec_method on it.
294 * Still need to figure out how to handle the exception stuff
295 * across the managed/unmanaged boundary.
298 mono_create_method_pointer (MonoMethod *method)
300 MonoMethodSignature *sig;
301 unsigned char *p, *code_buffer;
303 gint32 stackval_pos, arg_pos = 8;
307 * If it is a static P/Invoke method, we can just return the pointer
308 * to the method implementation.
310 sig = method->signature;
312 code_buffer = p = alloca (512); /* FIXME: check for overflows... */
314 local_size = sizeof (MonoInvocation) + sizeof (stackval) * (sig->param_count + 1);
315 stackval_pos = -local_size;
318 * Standard function prolog with magic trick.
320 x86_jump_code (p, code_buffer + 8);
325 x86_push_reg (p, X86_EBP);
326 x86_mov_reg_reg (p, X86_EBP, X86_ESP, 4);
327 x86_alu_reg_imm (p, X86_SUB, X86_ESP, local_size);
330 * Initialize MonoInvocation fields, first the ones known now.
332 x86_mov_reg_imm (p, X86_EAX, 0);
333 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex)), X86_EAX, 4);
334 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, ex_handler)), X86_EAX, 4);
335 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, child)), X86_EAX, 4);
336 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, parent)), X86_EAX, 4);
338 * Set the method pointer.
340 x86_mov_membase_imm (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, method)), (int)method, 4);
346 if (sig->call_convention != MONO_CALL_THISCALL) {
348 * Grab it from the stack, otherwise it's already in ECX.
350 x86_mov_reg_membase (p, X86_ECX, X86_EBP, OBJ_POS, 4);
353 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, obj)), X86_ECX, 4);
356 * Handle the arguments. stackval_pos is the posset of the stackval array from EBP.
357 * arg_pos is the offset from EBP to the incoming arg on the stack.
358 * We just call stackval_from_data to handle all the (nasty) issues....
360 for (i = 0; i < sig->param_count; ++i) {
361 x86_mov_reg_imm (p, X86_ECX, stackval_from_data);
362 x86_lea_membase (p, X86_EDX, X86_EBP, arg_pos);
363 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
364 x86_push_reg (p, X86_EDX);
365 x86_push_reg (p, X86_EAX);
366 x86_push_imm (p, sig->params [i]);
367 x86_call_reg (p, X86_ECX);
368 x86_alu_reg_imm (p, X86_SUB, X86_ESP, 12);
369 stackval_pos += sizeof (stackval);
371 if (!sig->params [i]->byref) {
372 switch (sig->params [i]->type) {
377 case MONO_TYPE_VALUETYPE:
378 g_assert_not_reached (); /* Not implemented yet. */
386 * Handle the return value storage area.
388 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
389 x86_mov_membase_reg (p, X86_EBP, (MINV_POS + G_STRUCT_OFFSET (MonoInvocation, retval)), X86_EAX, 4);
394 x86_lea_membase (p, X86_EAX, X86_EBP, MINV_POS);
395 x86_push_reg (p, X86_EAX);
396 x86_mov_reg_imm (p, X86_EDX, ves_exec_method);
397 x86_call_reg (p, X86_EDX);
400 * Move the return value to the proper place.
402 x86_lea_membase (p, X86_EAX, X86_EBP, stackval_pos);
403 if (sig->ret->byref) {
404 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
406 switch (sig->ret->type) {
413 case MONO_TYPE_OBJECT:
414 case MONO_TYPE_STRING:
415 case MONO_TYPE_CLASS:
416 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
419 x86_mov_reg_membase (p, X86_EDX, X86_EAX, 4, 4);
420 x86_mov_reg_membase (p, X86_EAX, X86_EAX, 0, 4);
423 x86_fld_membase (p, X86_EAX, 0, TRUE);
426 g_error ("Type 0x%x not handled yet in thunk creation", sig->ret->type);
437 return g_memdup (code_buffer, p - code_buffer);