1.1 --- a/sourcehook/sourcehook_hookmangen.cpp Mon Nov 19 13:39:44 2007 +0000
1.2 +++ b/sourcehook/sourcehook_hookmangen.cpp Mon Nov 19 14:36:42 2007 +0000
1.3 @@ -207,12 +207,6 @@
1.4 acc += GetStackSize(m_Proto.GetParam(i));
1.5 }
1.6
1.7 - // Memory return: address is first param
1.8 - if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
1.9 - acc += SIZE_PTR;
1.10 -
1.11 - // :TODO: cdecl: THIS POINTER AS FIRST PARAM!!!
1.12 -
1.13 return acc;
1.14 }
1.15
1.16 @@ -1030,13 +1024,28 @@
1.17
1.18 // thisptr
1.19 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
1.20 - if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1.21 + if (SH_COMP == SH_COMP_GCC)
1.22 {
1.23 - // on gcc/mingw or msvc with varargs, this is the first parameter
1.24 + // on gcc/mingw, this is the first parameter
1.25 IA32_Push_Reg(&m_HookFunc, REG_ECX);
1.26 // on msvc without varargs, simply leave it in ecx
1.27 +
1.28 + // actually, if we're returning in memory, this pointer is the second param
1.29 + // and the memret pointer is the real first parameter
1.30 + caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
1.31 }
1.32 - caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
1.33 + else
1.34 + {
1.35 + // On msvc, if we're returning in memory, the memret pointer is the first parameter
1.36 + caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
1.37 +
1.38 + // actually, with varargs, the this pointer is the first param and the memret ptr
1.39 + // is the second one
1.40 + if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1.41 + {
1.42 + IA32_Push_Reg(&m_HookFunc, REG_ECX);
1.43 + }
1.44 + }
1.45
1.46 // call
1.47 IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_vfnptr_origentry);
1.48 @@ -1239,6 +1248,9 @@
1.49 IA32_Push_Reg(&m_HookFunc, REG_EBX);
1.50 IA32_Mov_Reg_Rm(&m_HookFunc, REG_EBP, REG_ESP, MOD_REG);
1.51
1.52 + if ((m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs) && (m_Proto.GetRet().type == PassInfo::PassType_Object))
1.53 + IA32_Int3(&m_HookFunc);
1.54 +
1.55 jit_int32_t v_this = 0;
1.56 jit_int32_t param_base_offs = 0;
1.57 if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1.58 @@ -1303,7 +1315,7 @@
1.59 jit_int32_t v_memret_addr = 0;
1.60 if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
1.61 {
1.62 - if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
1.63 + if (SH_COMP == SH_COMP_GCC)
1.64 {
1.65 // gcc: now: first param = mem ret addr
1.66 // second param = this pointer
1.67 @@ -1312,10 +1324,25 @@
1.68 v_this += 4;
1.69 param_base_offs += SIZE_PTR;
1.70 }
1.71 - else
1.72 + else // MSVC
1.73 {
1.74 - v_memret_addr = param_base_offs;
1.75 - param_base_offs += SIZE_PTR;
1.76 + if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
1.77 + {
1.78 + // varargs -> cdecl
1.79 + // msvc: now:
1.80 + // first param = this pointer
1.81 + // second param = mem ret addr
1.82 + // third param = actual first param
1.83 +
1.84 + // params_base_offs is already updated to point to after the this pointer
1.85 + v_memret_addr = param_base_offs;
1.86 + param_base_offs += SIZE_PTR;
1.87 + }
1.88 + else
1.89 + {
1.90 + v_memret_addr = param_base_offs;
1.91 + param_base_offs += SIZE_PTR;
1.92 + }
1.93 }
1.94 }
1.95
1.96 @@ -1502,7 +1529,13 @@
1.97 {
1.98 // msvc without varargs:
1.99 // callee cleans the stack
1.100 - IA32_Return_Popstack(&m_HookFunc, GetParamsStackSize());
1.101 +
1.102 + short cleansize = GetParamsStackSize();
1.103 + // Memory return: address is first param
1.104 + if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
1.105 + cleansize += SIZE_PTR;
1.106 +
1.107 + IA32_Return_Popstack(&m_HookFunc, cleansize);
1.108 }
1.109 else
1.110 {