sourcehook/sourcehook_hookmangen.cpp
branchhookman_autogen
changeset 531 0180319136ba
parent 479 b43ebc944319
child 532 9729de2d7551
     1.1 --- a/sourcehook/sourcehook_hookmangen.cpp	Sun Nov 25 01:22:41 2007 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,2030 +0,0 @@
     1.4 -/* ======== SourceHook ========
     1.5 -* Copyright (C) 2004-2007 Metamod:Source Development Team
     1.6 -* No warranties of any kind
     1.7 -*
     1.8 -* License: zlib/libpng
     1.9 -*
    1.10 -* Author(s): Pavol "PM OnoTo" Marko
    1.11 -* Contributor(s): Borja "faluco" Ferav  (many thanks for assitance!)
    1.12 -*				  David "BAILOPAN" Anderson
    1.13 -* ============================
    1.14 -*/
    1.15 -
    1.16 -// recommended literature:
    1.17 -// http://www.cs.umbc.edu/~chang/cs313.s02/stack.shtml
    1.18 -// http://www.angelcode.com/dev/callconv/callconv.html
    1.19 -// http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-1.html
    1.20 -
    1.21 -#include "sourcehook_impl.h"
    1.22 -#include "sourcehook_hookmangen.h"
    1.23 -#include "sourcehook_hookmangen_x86.h"
    1.24 -#include "sh_memory.h"
    1.25 -#include <stdarg.h>							// we might need the address of vsnprintf
    1.26 -#include <stdio.h>
    1.27 -
    1.28 -#if SH_COMP == SH_COMP_MSVC
    1.29 -# define GCC_ONLY(x)
    1.30 -# define MSVC_ONLY(x) x
    1.31 -#elif SH_COMP == SH_COMP_GCC
    1.32 -# define GCC_ONLY(x) x
    1.33 -# define MSVC_ONLY(x)
    1.34 -#endif
    1.35 -
    1.36 -// :TODO: test BIG vtable indices
    1.37 -
    1.38 -namespace SourceHook
    1.39 -{
    1.40 -	namespace Impl
    1.41 -	{
    1.42 -		CPageAlloc GenBuffer::ms_Allocator(16);
    1.43 -
    1.44 -		template <class T>
    1.45 -		jit_int32_t DownCastPtr(T ptr)
    1.46 -		{
    1.47 -			return reinterpret_cast<jit_int32_t>(ptr);
    1.48 -		}
    1.49 -
    1.50 -		jit_uint32_t DownCastSize(size_t size)
    1.51 -		{
    1.52 -			return static_cast<jit_uint32_t>(size);
    1.53 -		}
    1.54 -
    1.55 -		GenContext::GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr)
    1.56 -			: m_GeneratedPubFunc(NULL), m_OrigProto(proto), m_Proto(proto), m_VtblOffs(vtbl_offs),
    1.57 -			  m_VtblIdx(vtbl_idx), m_SHPtr(pSHPtr), m_pHI(NULL), m_HookfuncVfnptr(NULL), m_RegCounter(0)
    1.58 -		{
    1.59 -			m_pHI = new void*;
    1.60 -			m_HookfuncVfnptr = new void*;
    1.61 -			m_BuiltPI = new ProtoInfo;
    1.62 -			m_BuiltPI_Params = NULL;
    1.63 -			m_BuiltPI_Params2 = NULL;
    1.64 -		}
    1.65 -
    1.66 -		GenContext::~GenContext()
    1.67 -		{
    1.68 -			Clear();
    1.69 -			delete m_pHI;
    1.70 -			delete m_HookfuncVfnptr;
    1.71 -			delete m_BuiltPI;
    1.72 -		}
    1.73 -
    1.74 -		void GenContext::Clear()
    1.75 -		{
    1.76 -			m_HookFunc.clear();
    1.77 -			m_PubFunc.clear();
    1.78 -			if (m_BuiltPI_Params)
    1.79 -			{
    1.80 -				delete m_BuiltPI_Params;
    1.81 -				m_BuiltPI_Params = NULL;
    1.82 -			}
    1.83 -			if (m_BuiltPI_Params2)
    1.84 -			{
    1.85 -				delete m_BuiltPI_Params2;
    1.86 -				m_BuiltPI_Params2 = NULL;
    1.87 -			}
    1.88 -		}
    1.89 -
    1.90 -		void GenContext::BuildProtoInfo()
    1.91 -		{
    1.92 -			m_BuiltPI->convention = m_Proto.GetConvention();
    1.93 -			m_BuiltPI->numOfParams = m_Proto.GetNumOfParams();
    1.94 -
    1.95 -			m_BuiltPI->retPassInfo.size = m_Proto.GetRet().size;
    1.96 -			m_BuiltPI->retPassInfo.type = m_Proto.GetRet().type;
    1.97 -			m_BuiltPI->retPassInfo.flags = m_Proto.GetRet().flags;
    1.98 -			m_BuiltPI->retPassInfo2.pNormalCtor = m_Proto.GetRet().pNormalCtor;
    1.99 -			m_BuiltPI->retPassInfo2.pCopyCtor = m_Proto.GetRet().pCopyCtor;
   1.100 -			m_BuiltPI->retPassInfo2.pDtor = m_Proto.GetRet().pDtor;
   1.101 -			m_BuiltPI->retPassInfo2.pAssignOperator = m_Proto.GetRet().pAssignOperator;
   1.102 -
   1.103 -			if (m_BuiltPI_Params)
   1.104 -				delete m_BuiltPI_Params;
   1.105 -			m_BuiltPI_Params = new PassInfo[m_BuiltPI->numOfParams + 1];
   1.106 -			if (m_BuiltPI_Params2)
   1.107 -				delete m_BuiltPI_Params2;
   1.108 -			m_BuiltPI_Params2 = new PassInfo::V2Info[m_BuiltPI->numOfParams + 1];
   1.109 -
   1.110 -			m_BuiltPI_Params[0].size = 1;			// Version 1
   1.111 -			m_BuiltPI_Params[0].type = 0;
   1.112 -			m_BuiltPI_Params[0].flags = 0;
   1.113 -			
   1.114 -			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
   1.115 -			{
   1.116 -				m_BuiltPI_Params[i+1].size = m_Proto.GetParam(i).size;
   1.117 -				m_BuiltPI_Params[i+1].type = m_Proto.GetParam(i).type;
   1.118 -				m_BuiltPI_Params[i+1].flags = m_Proto.GetParam(i).flags;
   1.119 -
   1.120 -				m_BuiltPI_Params2[i+1].pNormalCtor = m_Proto.GetParam(i).pNormalCtor;
   1.121 -				m_BuiltPI_Params2[i+1].pCopyCtor = m_Proto.GetParam(i).pCopyCtor;
   1.122 -				m_BuiltPI_Params2[i+1].pDtor = m_Proto.GetParam(i).pDtor;
   1.123 -				m_BuiltPI_Params2[i+1].pAssignOperator = m_Proto.GetParam(i).pAssignOperator;
   1.124 -			}
   1.125 -
   1.126 -			m_BuiltPI->paramsPassInfo = m_BuiltPI_Params;
   1.127 -			m_BuiltPI->paramsPassInfo2 = m_BuiltPI_Params2;
   1.128 -		}
   1.129 -
   1.130 -		jit_int32_t GenContext::GetRealSize(const IntPassInfo &info)
   1.131 -		{
   1.132 -			if (info.flags & (PassInfo::PassFlag_ByRef | PassFlag_ForcedByRef))
   1.133 -			{
   1.134 -				return SIZE_PTR;
   1.135 -			}
   1.136 -			return static_cast<jit_int32_t>(info.size);
   1.137 -		}
   1.138 -
   1.139 -		jit_int32_t GenContext::AlignSize(jit_int32_t x, jit_int32_t boundary)
   1.140 -		{
   1.141 -			if (x % boundary != 0)
   1.142 -				x = (x & ~(boundary-1)) + boundary;
   1.143 -			return x;
   1.144 -		}
   1.145 -		
   1.146 -		// Computes size on the stack
   1.147 -		jit_int32_t GenContext::GetParamStackSize(const IntPassInfo &info)
   1.148 -		{
   1.149 -			// Align up to 4 byte boundaries
   1.150 -			return AlignSize(GetRealSize(info), 4);
   1.151 -		}
   1.152 -
   1.153 -		jit_int8_t GenContext::NextRegEBX_ECX_EDX()
   1.154 -		{
   1.155 -			switch ((m_RegCounter++) % 3)
   1.156 -			{
   1.157 -			case 0:
   1.158 -				return REG_EBX;
   1.159 -			case 1:
   1.160 -				return REG_ECX;
   1.161 -			case 2:
   1.162 -			default:
   1.163 -				m_RegCounter = 0;
   1.164 -				return REG_EDX;
   1.165 -			}
   1.166 -		}
   1.167 -
   1.168 -		void GenContext::BitwiseCopy_Setup()
   1.169 -		{
   1.170 -			//cld
   1.171 -			//push edi
   1.172 -			//push esi
   1.173 -
   1.174 -			IA32_Cld(&m_HookFunc);
   1.175 -			IA32_Push_Reg(&m_HookFunc, REG_EDI);
   1.176 -			IA32_Push_Reg(&m_HookFunc, REG_ESI);
   1.177 -		}
   1.178 -
   1.179 -		void GenContext::BitwiseCopy_Do(size_t size)
   1.180 -		{
   1.181 -			jit_uint32_t dwords = DownCastSize(size) / 4;
   1.182 -			jit_uint32_t bytes = DownCastSize(size) % 4;
   1.183 -
   1.184 -			//if dwords
   1.185 -			// mov ecx, <dwords>
   1.186 -			// rep movsd
   1.187 -			//if bytes
   1.188 -			// mov ecx, <bytes>
   1.189 -			// rep movsb
   1.190 -			//pop esi
   1.191 -			//pop edi
   1.192 -
   1.193 -			if (dwords)
   1.194 -			{
   1.195 -				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, dwords);
   1.196 -				IA32_Rep(&m_HookFunc);
   1.197 -				IA32_Movsd(&m_HookFunc);
   1.198 -			}
   1.199 -			if (bytes)
   1.200 -			{
   1.201 -				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, bytes);
   1.202 -				IA32_Rep(&m_HookFunc);
   1.203 -				IA32_Movsb(&m_HookFunc);
   1.204 -			}
   1.205 -			IA32_Pop_Reg(&m_HookFunc, REG_ESI);
   1.206 -			IA32_Pop_Reg(&m_HookFunc, REG_EDI);
   1.207 -		}
   1.208 -
   1.209 -		jit_int32_t GenContext::AlignStackBeforeCall(int paramsize, int flags)
   1.210 -		{
   1.211 -			paramsize +=
   1.212 -				GCC_ONLY(	((flags & AlignStack_GCC_ThisOnStack)!=0	? SIZE_PTR : 0) + )
   1.213 -				MSVC_ONLY(	((flags & AlignStack_MSVC_ThisOnStack)!=0	? SIZE_PTR : 0) + )
   1.214 -							((flags & AlignStack_MemRet)!=0				? SIZE_PTR : 0);
   1.215 -
   1.216 -			// At the beginning of the hookfunc, the stack is aligned to a 16 bytes boundary.
   1.217 -			// Then, m_BytesPushedAfterInitialAlignment were pushed (can also be 0).
   1.218 -			
   1.219 -			// After this function is called, paramsize bytes will be pushed onto the stack
   1.220 -			// After that, the alignment has to be a 16 bytes boundary again.
   1.221 -
   1.222 -
   1.223 -			// How many bytes we would subtract if the alignment was alright now:
   1.224 -			int subtractFromEsp = 16 - (paramsize % 16);
   1.225 -			if (subtractFromEsp == 16)
   1.226 -				subtractFromEsp = 0;
   1.227 -
   1.228 -			// But: there might be bytes pushed alreay!
   1.229 -			subtractFromEsp -= m_BytesPushedAfterInitialAlignment;
   1.230 -
   1.231 -			// For example: paramsize was 0 and m_BytesPushedAfterInitialAlignment was 4.
   1.232 -			//	we then have to push another 12 bytes to reach 16 bytes alignment again.
   1.233 -
   1.234 -			if (subtractFromEsp < 0)
   1.235 -				subtractFromEsp = 16 - ((-subtractFromEsp) % 16);
   1.236 -
   1.237 -			if (subtractFromEsp != 0)
   1.238 -				IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, subtractFromEsp, MOD_REG);
   1.239 -
   1.240 -			return subtractFromEsp;
   1.241 -		}
   1.242 -
   1.243 -		void GenContext::AlignStackAfterCall(jit_int32_t numofbytes)
   1.244 -		{
   1.245 -			if (numofbytes != 0)
   1.246 -				IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, numofbytes, MOD_REG);
   1.247 -		}
   1.248 -
   1.249 -		void GenContext::CheckAlignmentBeforeCall()
   1.250 -		{
   1.251 -#if 0
   1.252 -			IA32_Test_Rm_Imm32(&m_HookFunc, REG_ESP, 15, MOD_REG);
   1.253 -			IA32_Jump_Cond_Imm8(&m_HookFunc, CC_Z, 1);
   1.254 -			IA32_Int3(&m_HookFunc);
   1.255 -#endif
   1.256 -		}
   1.257 -
   1.258 -		short GenContext::GetParamsTotalStackSize()
   1.259 -		{
   1.260 -			short acc = 0;
   1.261 -			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
   1.262 -			{
   1.263 -				acc += GetParamStackSize(m_Proto.GetParam(i));
   1.264 -			}
   1.265 -
   1.266 -			return acc;
   1.267 -		}
   1.268 -
   1.269 -		short GenContext::GetForcedByRefParamOffset(int p)
   1.270 -		{
   1.271 -			short off = 0;
   1.272 -			for (int i = 0; i < p; ++i)
   1.273 -			{
   1.274 -				if (m_Proto.GetParam(i).flags & PassFlag_ForcedByRef)
   1.275 -					off += AlignSize(static_cast<jit_int32_t>(m_Proto.GetParam(i).size), 4);
   1.276 -			}
   1.277 -			return off;
   1.278 -		}
   1.279 -
   1.280 -		short GenContext::GetForcedByRefParamsSize()
   1.281 -		{
   1.282 -			return GetForcedByRefParamOffset(m_Proto.GetNumOfParams());
   1.283 -		}
   1.284 -
   1.285 -		jit_int32_t GenContext::PushRef(jit_int32_t param_offset, const IntPassInfo &pi)
   1.286 -		{
   1.287 -			// push [ebp+<offset>]
   1.288 -			IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_offset);
   1.289 -
   1.290 -			return SIZE_PTR;
   1.291 -		}
   1.292 -
   1.293 -		jit_int32_t GenContext::PushBasic(jit_int32_t param_offset, const IntPassInfo &pi)
   1.294 -		{
   1.295 -			int reg;
   1.296 -			int reg2;
   1.297 -
   1.298 -			switch (pi.size)
   1.299 -			{
   1.300 -			default:
   1.301 -				SH_ASSERT(0, ("Unsupported!"));
   1.302 -				return 0;
   1.303 -			case 1:
   1.304 -				reg = NextRegEBX_ECX_EDX();
   1.305 -				//movzx reg, BYTE PTR [ebp+<offset>]
   1.306 -				//push reg
   1.307 -				IA32_Movzx_Reg32_Rm8_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
   1.308 -				IA32_Push_Reg(&m_HookFunc, reg);
   1.309 -
   1.310 -				return 4;
   1.311 -			case 2:
   1.312 -				reg = NextRegEBX_ECX_EDX();
   1.313 -				//movzx reg, WORD PTR [ebp+<offset>]
   1.314 -				//push reg
   1.315 -				m_HookFunc.write_ubyte(IA32_16BIT_PREFIX);
   1.316 -				IA32_Movzx_Reg32_Rm16_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
   1.317 -				IA32_Push_Reg(&m_HookFunc, reg);
   1.318 -
   1.319 -				return 4;
   1.320 -			case 4:
   1.321 -				reg = NextRegEBX_ECX_EDX();
   1.322 -				//mov reg, DWORD PTR [ebp+<offset>]
   1.323 -				//push reg
   1.324 -				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
   1.325 -				IA32_Push_Reg(&m_HookFunc, reg);
   1.326 -
   1.327 -				return 4;
   1.328 -			case 8:
   1.329 -				reg = NextRegEBX_ECX_EDX();
   1.330 -				reg2 = NextRegEBX_ECX_EDX();
   1.331 -				//mov reg, DWORD PTR [ebp+<offset>+4]
   1.332 -				//mov reg2, DWORD PTR [ebp+<offset>]
   1.333 -				//push reg
   1.334 -				//push reg2
   1.335 -
   1.336 -				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset+4);
   1.337 -				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg2, REG_EBP, param_offset);
   1.338 -				IA32_Push_Reg(&m_HookFunc, reg);
   1.339 -				IA32_Push_Reg(&m_HookFunc, reg2);
   1.340 -
   1.341 -				return 8;
   1.342 -			}
   1.343 -		}
   1.344 -
   1.345 -		jit_int32_t GenContext::PushFloat(jit_int32_t param_offset, const IntPassInfo &pi)
   1.346 -		{
   1.347 -			switch (pi.size)
   1.348 -			{
   1.349 -			default:
   1.350 -				SH_ASSERT(0, ("Unsupported!"));
   1.351 -				return 0;
   1.352 -			case 4:
   1.353 -				//fld DWORD PTR [ebp+<offset>]
   1.354 -				//push reg
   1.355 -				//fstp DWORD PTR [esp]
   1.356 -				IA32_Fld_Mem32_DispAuto(&m_HookFunc, REG_EBP, param_offset);
   1.357 -				IA32_Push_Reg(&m_HookFunc, NextRegEBX_ECX_EDX());
   1.358 -				IA32_Fstp_Mem32_ESP(&m_HookFunc);
   1.359 -				return 4;
   1.360 -			case 8:
   1.361 -				//fld QWORD PTR [ebp+<offset>]
   1.362 -				//sub esp, 8
   1.363 -				//fstp QWORD PTR [esp]
   1.364 -				IA32_Fld_Mem64_DispAuto(&m_HookFunc, REG_EBP, param_offset);
   1.365 -				IA32_Sub_Rm_Imm8(&m_HookFunc, REG_ESP, 8, MOD_REG);
   1.366 -				IA32_Fstp_Mem64_ESP(&m_HookFunc);
   1.367 -				return 8;
   1.368 -			}
   1.369 -		}
   1.370 -
   1.371 -		jit_int32_t GenContext::PushObject(jit_int32_t param_offset, const IntPassInfo &pi, jit_int32_t place_fbrr)
   1.372 -		{
   1.373 -			if ((pi.flags & PassFlag_ForcedByRef) == 0)
   1.374 -			{
   1.375 -				// make room on the stack
   1.376 -				// sub esp, <size>
   1.377 -				IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, GetParamStackSize(pi), MOD_REG);
   1.378 -			}
   1.379 -
   1.380 -			// if there is a copy constructor..
   1.381 -			if (pi.pCopyCtor)
   1.382 -			{
   1.383 -				// :TODO: alignment here?
   1.384 -				//  can't use normal alignment methods
   1.385 -				//  because an unknown number of bytes has been pushed already (the other params)
   1.386 -
   1.387 -				// save eax
   1.388 -				IA32_Push_Reg(&m_HookFunc, REG_EAX);
   1.389 -
   1.390 -				// compute dest addr to ECX
   1.391 -				// = forcedbyref ? ebp+place_fbrr : esp+12
   1.392 -				if (pi.flags & PassFlag_ForcedByRef)
   1.393 -					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
   1.394 -				else
   1.395 -					IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_ECX, REG_NOIDX, REG_ESP, NOSCALE, 4);
   1.396 -				
   1.397 -				// compute src addr to EAX
   1.398 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_offset);
   1.399 -
   1.400 -				// push params (gcc: also this)
   1.401 -				IA32_Push_Reg(&m_HookFunc, REG_EAX);
   1.402 -				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   1.403 -
   1.404 -				// call
   1.405 -				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(pi.pCopyCtor));
   1.406 -				IA32_Call_Reg(&m_HookFunc, REG_EDX);
   1.407 -
   1.408 -				// gcc: clean up stack
   1.409 -				GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
   1.410 -
   1.411 -				// restore eax
   1.412 -				IA32_Pop_Reg(&m_HookFunc, REG_EAX);
   1.413 -			}
   1.414 -			else
   1.415 -			{
   1.416 -				// bitwise copy
   1.417 -				
   1.418 -				BitwiseCopy_Setup();
   1.419 -
   1.420 -				//if forcedbyref:
   1.421 -				//  lea edi, [ebp_place_fbrr]
   1.422 -				//else
   1.423 -				//  lea edi, [esp+8]			-  bc_setup pushed two regs onto the stack!
   1.424 -				//lea esi, [ebp+<offs>]
   1.425 -				if (pi.flags & PassFlag_ForcedByRef)
   1.426 -					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, place_fbrr);
   1.427 -				else
   1.428 -					IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_EDI, REG_NOIDX, REG_ESP, NOSCALE, 8);
   1.429 -
   1.430 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, param_offset);
   1.431 -
   1.432 -				BitwiseCopy_Do(pi.size);
   1.433 -			}
   1.434 -
   1.435 -			// forcedref: push reference to ebp+place_fbrr
   1.436 -			if (pi.flags & PassFlag_ForcedByRef)
   1.437 -			{
   1.438 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
   1.439 -				IA32_Push_Reg(&m_HookFunc, REG_ECX);
   1.440 -				return SIZE_PTR;
   1.441 -			}
   1.442 -			
   1.443 -			return GetParamStackSize(pi);
   1.444 -		}
   1.445 -
   1.446 -		void GenContext::DestroyParams(jit_int32_t fbrr_base)
   1.447 -		{
   1.448 -			for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
   1.449 -			{
   1.450 -				const IntPassInfo &pi = m_Proto.GetParam(i);
   1.451 -				if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
   1.452 -					(pi.flags & PassInfo::PassFlag_ByVal) && (pi.flags & PassFlag_ForcedByRef))
   1.453 -				{
   1.454 -					// Actually, this is only for GCC (see line above: ForcedByRef)
   1.455 -					jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
   1.456 -					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, fbrr_base + GetForcedByRefParamOffset(i));
   1.457 -					IA32_Push_Reg(&m_HookFunc, REG_ECX);
   1.458 -					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
   1.459 -					CheckAlignmentBeforeCall();
   1.460 -					IA32_Call_Reg(&m_HookFunc, REG_EAX);
   1.461 -					IA32_Pop_Reg(&m_HookFunc, REG_ECX);
   1.462 -
   1.463 -					AlignStackAfterCall(tmpAlign);
   1.464 -				}
   1.465 -			}
   1.466 -		}
   1.467 -		
   1.468 -		// May not touch eax!
   1.469 -		jit_int32_t GenContext::PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to, int v_place_for_memret,
   1.470 -				jit_int32_t v_place_fbrr_base)
   1.471 -		{
   1.472 -			jit_int32_t added_to_stack = 0;
   1.473 -			jit_int32_t ret = 0;
   1.474 -
   1.475 -			// compute the offset _after_ the last parameter
   1.476 -			jit_int32_t cur_offset = param_base_offset;
   1.477 -			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
   1.478 -			{
   1.479 -				cur_offset += GetParamStackSize(m_Proto.GetParam(i));
   1.480 -			}
   1.481 -
   1.482 -			// push parameters in reverse order
   1.483 -			for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
   1.484 -			{
   1.485 -				const IntPassInfo &pi = m_Proto.GetParam(i);
   1.486 -				cur_offset -= GetParamStackSize(pi);
   1.487 -				if (pi.flags & PassInfo::PassFlag_ByVal)
   1.488 -				{
   1.489 -					switch (pi.type)
   1.490 -					{
   1.491 -					case PassInfo::PassType_Basic:
   1.492 -						ret = PushBasic(cur_offset, pi);
   1.493 -						break;
   1.494 -					case PassInfo::PassType_Float:
   1.495 -						ret = PushFloat(cur_offset, pi);
   1.496 -						break;
   1.497 -					case PassInfo::PassType_Object:
   1.498 -						ret = PushObject(cur_offset, pi, v_place_fbrr_base + GetForcedByRefParamOffset(i));
   1.499 -						break;
   1.500 -					}
   1.501 -				}
   1.502 -				else if (pi.flags & PassInfo::PassFlag_ByRef)
   1.503 -				{
   1.504 -					ret = PushRef(cur_offset, pi);
   1.505 -				}
   1.506 -				else
   1.507 -				{
   1.508 -					SH_ASSERT(0, ("Unsupported!"));
   1.509 -				}
   1.510 -				added_to_stack += ret;
   1.511 -			}
   1.512 -
   1.513 -			return added_to_stack;
   1.514 -		}
   1.515 -
   1.516 -		// It is IMPORTANT that PushMemRetPtr doesn't touch ecx and eax
   1.517 -		jit_int32_t GenContext::PushMemRetPtr(jit_int32_t save_ret_to, jit_int32_t v_place_for_memret)
   1.518 -		{
   1.519 -			// Memory return support
   1.520 -			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   1.521 -			{
   1.522 -				// push address where to save it!
   1.523 -				int reg = REG_EDX;
   1.524 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, reg, REG_EBP,
   1.525 -					MemRetWithTempObj() ? v_place_for_memret : save_ret_to);
   1.526 -				IA32_Push_Reg(&m_HookFunc, reg);
   1.527 -
   1.528 -				return (SH_COMP==SH_COMP_MSVC) ? 4 : 0;			// varargs funcs on msvc might need this.
   1.529 -																// gcc doesn't: callee cleans the memret ptr, caller the other params :s
   1.530 -			}
   1.531 -			return 0;
   1.532 -		}
   1.533 -
   1.534 -		void GenContext::SaveRetVal(int v_where, int v_place_for_memret)
   1.535 -		{
   1.536 -			size_t size = GetRealSize(m_Proto.GetRet());
   1.537 -			if (size == 0)
   1.538 -			{
   1.539 -				// No return value -> nothing
   1.540 -				return;
   1.541 -			}
   1.542 -
   1.543 -			if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
   1.544 -			{
   1.545 -				// mov [ebp + v_plugin_ret], eax
   1.546 -				IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   1.547 -				return;
   1.548 -			}
   1.549 -			// else: ByVal
   1.550 -
   1.551 -
   1.552 -			// Memory return:
   1.553 -			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   1.554 -			{
   1.555 -				if (MemRetWithTempObj())
   1.556 -				{
   1.557 -					// *v_where = *v_place_for_memret
   1.558 -
   1.559 -					// if we have an assign operator, use that
   1.560 -					if (m_Proto.GetRet().pAssignOperator)
   1.561 -					{
   1.562 -						// lea edx, [ebp + v_place_for_memret]  <-- src addr
   1.563 -						// lea ecx, [ebp + v_where]				<-- dest addr
   1.564 -						// push edx
   1.565 -						// gcc: push ecx
   1.566 -						// call it
   1.567 -						// gcc: clean up
   1.568 -
   1.569 -						jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
   1.570 -
   1.571 -						IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_place_for_memret);
   1.572 -						IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_where);
   1.573 -						IA32_Push_Reg(&m_HookFunc, REG_EDX);
   1.574 -						GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   1.575 -
   1.576 -						IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
   1.577 -						CheckAlignmentBeforeCall();
   1.578 -						IA32_Call_Reg(&m_HookFunc, REG_EAX);
   1.579 -						GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
   1.580 -
   1.581 -						AlignStackAfterCall(tmpAlign);
   1.582 -					}
   1.583 -					else
   1.584 -					{
   1.585 -						// bitwise copy
   1.586 -						BitwiseCopy_Setup();
   1.587 -
   1.588 -						//lea edi, [evp+v_where]		<-- destination
   1.589 -						//lea esi, [ebp+v_place_for_memret]	<-- src
   1.590 -						IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_where);
   1.591 -						IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_place_for_memret);
   1.592 -
   1.593 -						BitwiseCopy_Do(m_Proto.GetRet().size);
   1.594 -					}
   1.595 -
   1.596 -					// Then: destruct *v_place_for_memret if required
   1.597 -					if (m_Proto.GetRet().pDtor)
   1.598 -					{
   1.599 -						//lea ecx, [ebp+v_place_for_memret]
   1.600 -						//gcc: push ecx
   1.601 -						//call it
   1.602 -						//gcc: clean up
   1.603 -
   1.604 -						jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
   1.605 -
   1.606 -						IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_place_for_memret);
   1.607 -						GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   1.608 -						IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
   1.609 -						CheckAlignmentBeforeCall();
   1.610 -						IA32_Call_Reg(&m_HookFunc, REG_EAX);
   1.611 -						GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
   1.612 -
   1.613 -						AlignStackAfterCall(tmpAlign);
   1.614 -					}
   1.615 -				}
   1.616 -				else
   1.617 -				{
   1.618 -					// Already copied to correct address -> we're done
   1.619 -					return;
   1.620 -				}
   1.621 -			}
   1.622 -
   1.623 -			if (m_Proto.GetRet().type == PassInfo::PassType_Float)
   1.624 -			{
   1.625 -				if (size == 4)
   1.626 -					IA32_Fstp_Mem32_DispAuto(&m_HookFunc, REG_EBP, v_where);
   1.627 -				else if (size == 8)
   1.628 -					IA32_Fstp_Mem64_DispAuto(&m_HookFunc, REG_EBP, v_where);
   1.629 -			}
   1.630 -			else if (m_Proto.GetRet().type == PassInfo::PassType_Basic)
   1.631 -			{
   1.632 -				if (size <= 4)
   1.633 -				{
   1.634 -					// size <= 4: return in EAX
   1.635 -					//  We align <4 sizes up to 4
   1.636 -
   1.637 -
   1.638 -					// mov [ebp + v_plugin_ret], eax
   1.639 -					IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   1.640 -				}
   1.641 -				else if (size <= 8)
   1.642 -				{
   1.643 -					// size <= 4: return in EAX:EDX
   1.644 -					//  We align 4<x<8 sizes up to 8
   1.645 -
   1.646 -					// mov [ebp + v_plugin_ret], eax
   1.647 -					// mov [ebp + v_plugin_ret + 4], edx
   1.648 -					IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   1.649 -					IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
   1.650 -				}
   1.651 -			}
   1.652 -			else if (m_Proto.GetRet().type == PassInfo::PassType_Object)
   1.653 -			{
   1.654 -				if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)
   1.655 -				{
   1.656 -					if (size <= 4)
   1.657 -					{
   1.658 -						// size <= 4: return in EAX
   1.659 -						//  We align <4 sizes up to 4
   1.660 -
   1.661 -						// mov [ebp + v_plugin_ret], eax
   1.662 -						IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   1.663 -					}
   1.664 -					else if (size <= 8)
   1.665 -					{
   1.666 -						// size <= 4: return in EAX:EDX
   1.667 -						//  We align 4<x<8 sizes up to 8
   1.668 -
   1.669 -						// mov [ebp + v_plugin_ret], eax
   1.670 -						// mov [ebp + v_plugin_ret + 4], edx
   1.671 -						IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   1.672 -						IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
   1.673 -					}
   1.674 -					else
   1.675 -					{
   1.676 -						SH_ASSERT(0, ("RetReg and size > 8 !"));
   1.677 -					}
   1.678 -				}
   1.679 -			}
   1.680 -		}
   1.681 -
   1.682 -		bool GenContext::MemRetWithTempObj()
   1.683 -		{
   1.684 -			// Memory return AND (has destructor OR has assign operator)
   1.685 -			return ((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   1.686 -				&& (m_Proto.GetRet().flags & (PassInfo::PassFlag_ODtor | PassInfo::PassFlag_AssignOp)));
   1.687 -		}
   1.688 -
   1.689 -		void GenContext::ProcessPluginRetVal(int v_cur_res, int v_pContext, int v_plugin_ret)
   1.690 -		{
   1.691 -			// only for non-void functions!
   1.692 -			if (m_Proto.GetRet().size == 0)
   1.693 -				return;
   1.694 -
   1.695 -			// if (cur_res >= MRES_OVERRIDE)
   1.696 -			//   *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret;
   1.697 -
   1.698 -			//  eax = cur_res
   1.699 -			//  cmp eax,MRES_OVERRIDE
   1.700 -			//  jnge thelabel
   1.701 -			//    pContext->GetOverrideRetPtr()   -->  overrideretptr in eax
   1.702 -			//    *eax = plugin_ret
   1.703 -			//  thelabel:
   1.704 -			//
   1.705 -
   1.706 -			jitoffs_t tmppos, counter;
   1.707 -
   1.708 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
   1.709 -
   1.710 -			IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_OVERRIDE);
   1.711 -			tmppos = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NGE, 0);
   1.712 -			m_HookFunc.start_count(counter);
   1.713 -
   1.714 -			// eax = pContext->GetOverrideRetPtr()					no alignment needs
   1.715 -			//   ECX = pContext
   1.716 -			//   gcc: push ecx
   1.717 -			//   eax = [ecx]
   1.718 -			//   eax = [eax + 4]
   1.719 -			//   call eax
   1.720 -			//   gcc: clean up
   1.721 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
   1.722 -			GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   1.723 -
   1.724 -			// vtbloffs=0, vtblidx=1
   1.725 -			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   1.726 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 4);
   1.727 -			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   1.728 -			GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
   1.729 -
   1.730 -
   1.731 -			// *eax = plugin_ret
   1.732 -			if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
   1.733 -			{
   1.734 -				// mov ecx, [ebp+v_plugin_ret]
   1.735 -				// mov [eax], ecx
   1.736 -				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
   1.737 -				IA32_Mov_Rm_Reg(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   1.738 -			}
   1.739 -			else
   1.740 -			{
   1.741 -				if (m_Proto.GetRet().pAssignOperator)
   1.742 -				{
   1.743 -					// lea edx, [ebp + v_plugin_ret]
   1.744 -					// push edx					<-- src addr
   1.745 -					// msvc: ecx = eax				<-- dest addr
   1.746 -					// gcc: push eax				<-- dest addr
   1.747 -					// call it
   1.748 -					// gcc: clean up
   1.749 -
   1.750 -					jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
   1.751 -
   1.752 -					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_plugin_ret);
   1.753 -					IA32_Push_Reg(&m_HookFunc, REG_EDX);
   1.754 -#if SH_COMP == SH_COMP_MSVC
   1.755 -					IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
   1.756 -#elif SH_COMP == SH_COMP_GCC
   1.757 -					IA32_Push_Reg(&m_HookFunc, REG_EAX);
   1.758 -#endif
   1.759 -
   1.760 -					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
   1.761 -					CheckAlignmentBeforeCall();
   1.762 -					IA32_Call_Reg(&m_HookFunc, REG_EAX);
   1.763 -					GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
   1.764 -
   1.765 -					AlignStackAfterCall(tmpAlign);
   1.766 -				}
   1.767 -				else
   1.768 -				{
   1.769 -					// bitwise copy
   1.770 -					BitwiseCopy_Setup();
   1.771 -
   1.772 -					//mov edi, eax					<-- destination
   1.773 -					//lea esi, [ebp+v_plugin_ret]	<-- src
   1.774 -					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDI, REG_EAX, MOD_REG);
   1.775 -					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_plugin_ret);
   1.776 -
   1.777 -					BitwiseCopy_Do(m_Proto.GetRet().size);
   1.778 -				}
   1.779 -			}
   1.780 -
   1.781 -			m_HookFunc.end_count(counter);
   1.782 -			m_HookFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
   1.783 -		}
   1.784 -
   1.785 -		void GenContext::PrepareReturn(int v_status, int v_pContext, int v_retptr)
   1.786 -		{
   1.787 -			// only for non-void functions!
   1.788 -			if (m_Proto.GetRet().size == 0)
   1.789 -				return;
   1.790 -
   1.791 -			// retptr = status >= MRES_OVERRIDE ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()
   1.792 -
   1.793 -			// OverrideRetPtr: vtblidx = 1
   1.794 -			// OrigRetPtr: vtbldix = 2
   1.795 -			//  vtblidx = (status >= MRES_OVERRIDE) ? 1 : 2
   1.796 -
   1.797 -			// 
   1.798 -			// eax = pContext->GetOverrideRetPtr()
   1.799 -			//   ECX = pContext
   1.800 -			//   gcc: push ecx
   1.801 -
   1.802 -			// eax = (status < MRES_OVERRIDE) ? 1 : 0
   1.803 -			//   xor eax, eax
   1.804 -			//   cmp [ebp + v_status], MRES_OVERRIDE
   1.805 -			//   setl al								<-- setcc optimization for ternary operators, 
   1.806 -			
   1.807 -
   1.808 -			//   lea eax, [4*eax + 0x4]
   1.809 -
   1.810 -			//   edx = [ecx]
   1.811 -			//   add edx, eax
   1.812 -			//   mov edx, [edx]
   1.813 -
   1.814 -			//   call edx
   1.815 -			// gcc: clean up
   1.816 -
   1.817 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
   1.818 -			GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   1.819 -			IA32_Xor_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
   1.820 -			IA32_Cmp_Rm_Disp8_Imm8(&m_HookFunc, REG_EBP, v_status, MRES_OVERRIDE);
   1.821 -			IA32_SetCC_Rm8(&m_HookFunc, REG_EAX, CC_L);
   1.822 -
   1.823 -			IA32_Lea_Reg_RegMultImm32(&m_HookFunc, REG_EAX, REG_EAX, SCALE4, 4);
   1.824 -
   1.825 -			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
   1.826 -			IA32_Add_Reg_Rm(&m_HookFunc, REG_EDX, REG_EAX, MOD_REG);
   1.827 -
   1.828 -			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_MEM_REG);
   1.829 -			IA32_Call_Reg(&m_HookFunc, REG_EDX);
   1.830 -			GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
   1.831 -
   1.832 -			IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_retptr);
   1.833 -		}
   1.834 -
   1.835 -		void GenContext::DoReturn(int v_retptr, int v_memret_outaddr)
   1.836 -		{
   1.837 -			size_t size = m_Proto.GetRet().size;
   1.838 -			if (!size)
   1.839 -				return;
   1.840 -
   1.841 -			// Get real ret pointer into ecx
   1.842 -			// mov ecx, [ebp + v_ret_ptr]
   1.843 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_retptr);
   1.844 -
   1.845 -			if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
   1.846 -			{
   1.847 -				// mov eax, [ecx]
   1.848 -				IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   1.849 -				return;
   1.850 -			}
   1.851 -			// else: byval
   1.852 -
   1.853 -			if (m_Proto.GetRet().type == PassInfo::PassType_Float)
   1.854 -			{
   1.855 -				if (size == 4)
   1.856 -					IA32_Fld_Mem32(&m_HookFunc, REG_ECX);
   1.857 -				else if (size == 8)
   1.858 -					IA32_Fld_Mem64(&m_HookFunc, REG_ECX);
   1.859 -			}
   1.860 -			else if (m_Proto.GetRet().type == PassInfo::PassType_Basic || 
   1.861 -				((m_Proto.GetRet().type == PassInfo::PassType_Object) && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)) )
   1.862 -			{
   1.863 -				if (size <= 4)
   1.864 -				{
   1.865 -					// size <= 4: return in EAX
   1.866 -					//  We align <4 sizes up to 4
   1.867 -
   1.868 -					// mov eax, [ecx]
   1.869 -					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   1.870 -				}
   1.871 -				else if (size <= 8)
   1.872 -				{
   1.873 -					// size <= 4: return in EAX:EDX
   1.874 -					//  We align 4<x<8 sizes up to 8
   1.875 -
   1.876 -					// mov eax, [ecx]
   1.877 -					// mov edx, [ecx+4]
   1.878 -					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   1.879 -					IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, 4);
   1.880 -				}
   1.881 -				else
   1.882 -				{
   1.883 -					// size >8: return in memory
   1.884 -					//  handled later
   1.885 -				}
   1.886 -			}
   1.887 -
   1.888 -			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   1.889 -			{
   1.890 -				// *memret_outaddr = plugin_ret
   1.891 -				if (m_Proto.GetRet().pCopyCtor)
   1.892 -				{
   1.893 -					// mov edx, ecx				<-- src	( we set ecx to [ebp+v_retptr] before )
   1.894 -					// push edx					<-- src addr
   1.895 -					// msvc: ecx = [ebp + v_memret_outaddr]				<-- dest addr
   1.896 -					// gcc: push [ebp + v_memret_outaddr]				<-- dest addr
   1.897 -					// call it
   1.898 -					// gcc: clean up
   1.899 -
   1.900 -					jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
   1.901 -
   1.902 -					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_REG);
   1.903 -					IA32_Push_Reg(&m_HookFunc, REG_EDX);
   1.904 -
   1.905 -#if SH_COMP == SH_COMP_MSVC
   1.906 -					IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_memret_outaddr);
   1.907 -#elif SH_COMP == SH_COMP_GCC
   1.908 -					IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, v_memret_outaddr);
   1.909 -#endif
   1.910 -
   1.911 -					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pCopyCtor));
   1.912 -					CheckAlignmentBeforeCall();
   1.913 -					IA32_Call_Reg(&m_HookFunc, REG_EAX);
   1.914 -					GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
   1.915 -
   1.916 -					AlignStackAfterCall(tmpAlign);
   1.917 -				}
   1.918 -				else
   1.919 -				{
   1.920 -					// bitwise copy
   1.921 -					BitwiseCopy_Setup();
   1.922 -					
   1.923 -					//mov edi, [ebp+v_memret_outaddr]		<-- destination
   1.924 -					//mov esi, ecx						<-- src	( we set ecx to [ebp+v_retptr] before )
   1.925 -					IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDI, REG_EBP, v_memret_outaddr);
   1.926 -					IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESI, REG_ECX, MOD_REG);
   1.927 -
   1.928 -					BitwiseCopy_Do(m_Proto.GetRet().size);
   1.929 -				}
   1.930 -
   1.931 -				// In both cases: return the pointer in EAX
   1.932 -				// mov eax, [ebp + v_memret_outaddr]
   1.933 -				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_memret_outaddr);
   1.934 -			}
   1.935 -		}
   1.936 -
   1.937 -		void GenContext::GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
   1.938 -			int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base, jit_int32_t v_va_buf)
   1.939 -		{
   1.940 -			jitoffs_t counter, tmppos;
   1.941 -			jitoffs_t counter2, tmppos2;
   1.942 -
   1.943 -			jitoffs_t loop_begin_counter;
   1.944 -
   1.945 -			// prev_res = MRES_IGNORED
   1.946 -			IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_prev_res);
   1.947 -
   1.948 -			m_HookFunc.start_count(loop_begin_counter);
   1.949 -
   1.950 -			// eax = pContext->GetNext()
   1.951 -			//   ECX = pContext
   1.952 -			//   gcc: push ecx
   1.953 -			//   eax = [ecx]
   1.954 -			//   eax = [eax]
   1.955 -			//   call eax
   1.956 -			// gcc: clean up
   1.957 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
   1.958 -			GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   1.959 -
   1.960 -			// vtbloffs=0, vtblidx=0
   1.961 -			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   1.962 -			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_MEM_REG);
   1.963 -			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   1.964 -			GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
   1.965 -
   1.966 -			// quit on zero
   1.967 -			//  test eax, eax
   1.968 -			//  jz exit
   1.969 -			IA32_Test_Rm_Reg(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
   1.970 -			tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
   1.971 -			m_HookFunc.start_count(counter);
   1.972 -
   1.973 -			// prev_res = MRES_IGNORED
   1.974 -			IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_cur_res); 
   1.975 -
   1.976 -			// iter->call()
   1.977 -			//  push params
   1.978 -			//   ecx = eax
   1.979 -			//   gcc: push ecx
   1.980 -			//   eax = [ecx]
   1.981 -			//   eax = [eax+2*SIZE_PTR]
   1.982 -			//   call eax
   1.983 -			//   gcc: clean up 
   1.984 -			
   1.985 -			jit_int32_t caller_clean_bytes = 0;			// gcc always, msvc never (hooks never have varargs!)
   1.986 -
   1.987 -			jit_int32_t alignBytes = AlignStackBeforeCall(
   1.988 -				GetParamsTotalStackSize() + ((m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)!=0 ? SIZE_PTR : 0),
   1.989 -				AlignStack_GCC_ThisOnStack | ((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem) == 0 ? 0 : AlignStack_MemRet)
   1.990 -				);
   1.991 -
   1.992 -			// vafmt: push va_buf
   1.993 -			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
   1.994 -			{
   1.995 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
   1.996 -				IA32_Push_Reg(&m_HookFunc, REG_ECX);
   1.997 -				caller_clean_bytes += SIZE_PTR;
   1.998 -			}
   1.999 -
  1.1000 -			caller_clean_bytes += PushParams(base_param_offset, v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
  1.1001 -
  1.1002 -			IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
  1.1003 -			if (SH_COMP == SH_COMP_GCC)
  1.1004 -				IA32_Push_Reg(&m_HookFunc, REG_ECX);
  1.1005 -			caller_clean_bytes += PushMemRetPtr(v_plugin_ret, v_place_for_memret);
  1.1006 -			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
  1.1007 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 2*SIZE_PTR);
  1.1008 -			CheckAlignmentBeforeCall();
  1.1009 -			IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1010 -
  1.1011 -			AlignStackAfterCall(alignBytes);
  1.1012 -			
  1.1013 -			// cleanup (gcc only)
  1.1014 -			//   params + thisptr
  1.1015 -			if (SH_COMP == SH_COMP_GCC)
  1.1016 -				IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, caller_clean_bytes + SIZE_PTR, MOD_REG);
  1.1017 -
  1.1018 -			DestroyParams(v_place_fbrr_base);
  1.1019 -
  1.1020 -			SaveRetVal(v_plugin_ret, v_place_for_memret);
  1.1021 -
  1.1022 -			// process meta return:
  1.1023 -			//  prev_res = cur_res
  1.1024 -			//  if (cur_res > status) status = cur_res;
  1.1025 -			//
  1.1026 -			//  eax = cur_res
  1.1027 -			//  edx = status
  1.1028 -			//  prev_res = eax
  1.1029 -			//  cmp eax,edx
  1.1030 -			//  jng thelabel
  1.1031 -			//  status = eax
  1.1032 -			//  thelabel:
  1.1033 -			//
  1.1034 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
  1.1035 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDX, REG_EBP, v_status);
  1.1036 -			IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_prev_res);
  1.1037 -
  1.1038 -			IA32_Cmp_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_REG);
  1.1039 -			tmppos2 = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NG, 0);
  1.1040 -			m_HookFunc.start_count(counter2);
  1.1041 -
  1.1042 -			IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_status);
  1.1043 -
  1.1044 -			m_HookFunc.end_count(counter2);
  1.1045 -			m_HookFunc.rewrite(tmppos2, static_cast<jit_uint8_t>(counter2));
  1.1046 -
  1.1047 -			// process retval for non-void functions
  1.1048 -			ProcessPluginRetVal(v_cur_res, v_pContext, v_plugin_ret);
  1.1049 -
  1.1050 -			// jump back to loop begin
  1.1051 -			tmppos2 = IA32_Jump_Imm32(&m_HookFunc, 0);
  1.1052 -			m_HookFunc.end_count(loop_begin_counter);
  1.1053 -			m_HookFunc.rewrite(tmppos2, static_cast<jit_int32_t>(-loop_begin_counter));
  1.1054 -
  1.1055 -			m_HookFunc.end_count(counter);
  1.1056 -			m_HookFunc.rewrite(tmppos, static_cast<jit_int32_t>(counter));
  1.1057 -		}
  1.1058 -
  1.1059 -		void GenContext::GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
  1.1060 -			int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base, jit_int32_t v_va_buf)
  1.1061 -		{
  1.1062 -			jitoffs_t counter, tmppos;
  1.1063 -			jitoffs_t counter2, tmppos2;
  1.1064 -			jitoffs_t counter3, tmppos3;
  1.1065 -
  1.1066 -			// if (status != MRES_SUPERCEDE && pConteext->ShouldCallOrig())
  1.1067 -			//   *v_orig_ret = orig_call()
  1.1068 -			// else
  1.1069 -			//   *v_orig_ret = *v_override_ret
  1.1070 -
  1.1071 -			// mov eax, status
  1.1072 -			// cmp eax, MRES_SUPERCEDE
  1.1073 -			// je dont_call
  1.1074 -			// call pContext->ShouldCallOrig()
  1.1075 -			// test al, al						!! important: al, not eax! bool is only stored in the LSbyte
  1.1076 -			// jz dont_call
  1.1077 -			//
  1.1078 -			//  orig_call()
  1.1079 -			//  SaveRet(v_orig_ret)
  1.1080 -			//  jmp skip_dont_call:
  1.1081 -			//
  1.1082 -			// dont_call:
  1.1083 -			//  *v_orig_ret = *v_override_ret
  1.1084 -			// skip_dont_call:
  1.1085 -
  1.1086 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_status);
  1.1087 -			IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_SUPERCEDE);
  1.1088 -
  1.1089 -			tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_E, 0);
  1.1090 -			m_HookFunc.start_count(counter);
  1.1091 -
  1.1092 -			// eax = pContext->ShouldCallOrig()
  1.1093 -			//   ECX = pContext
  1.1094 -			//   gcc: push ecx
  1.1095 -			//   eax = [ecx]
  1.1096 -			//   eax = [eax + 3*SIZE_PTR]
  1.1097 -			//   call eax
  1.1098 -			// gcc: clean up
  1.1099 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
  1.1100 -			GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1.1101 -
  1.1102 -			// vtbloffs=0, vtblidx=3
  1.1103 -			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
  1.1104 -			IA32_Mov_Reg_Rm_Disp8(&m_HookFunc, REG_EAX, REG_EAX, 3*SIZE_PTR);
  1.1105 -			IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1106 -			GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1.1107 -
  1.1108 -			IA32_Test_Rm_Reg8(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
  1.1109 -			tmppos2 = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
  1.1110 -			m_HookFunc.start_count(counter2);
  1.1111 -
  1.1112 -			jit_int32_t caller_clean_bytes = 0;			// gcc always, msvc when cdecl-like (varargs)
  1.1113 -		
  1.1114 -			jit_int32_t alignBytes = AlignStackBeforeCall(
  1.1115 -				GetParamsTotalStackSize()  + ((m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)!=0 ? 2*SIZE_PTR : 0),
  1.1116 -				AlignStack_GCC_ThisOnStack |
  1.1117 -				((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem) == 0 ? 0 : AlignStack_MemRet) |
  1.1118 -				((m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs) == 0 ? 0 : AlignStack_MSVC_ThisOnStack)
  1.1119 -				);
  1.1120 -
  1.1121 -			// vafmt: push va_buf, then "%s"
  1.1122 -			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
  1.1123 -			{
  1.1124 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
  1.1125 -				IA32_Push_Reg(&m_HookFunc, REG_ECX);
  1.1126 -				IA32_Push_Imm32(&m_HookFunc, DownCastPtr("%s"));
  1.1127 -				caller_clean_bytes += 2*SIZE_PTR;
  1.1128 -			}
  1.1129 -
  1.1130 -			// push params
  1.1131 -			caller_clean_bytes += PushParams(param_base_offs, v_orig_ret, v_place_for_memret, v_place_fbrr_base);
  1.1132 -
  1.1133 -			// thisptr
  1.1134 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
  1.1135 -			if (SH_COMP == SH_COMP_GCC)
  1.1136 -			{
  1.1137 -				//  on gcc/mingw, this is the first parameter
  1.1138 -				IA32_Push_Reg(&m_HookFunc, REG_ECX);
  1.1139 -				//  on msvc without varargs, simply leave it in ecx
  1.1140 -
  1.1141 -				// actually, if we're returning in memory, this pointer is the second param
  1.1142 -				// and the memret pointer is the real first parameter
  1.1143 -				caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
  1.1144 -			}
  1.1145 -			else
  1.1146 -			{
  1.1147 -				// On msvc, if we're returning in memory, the memret pointer is the first parameter
  1.1148 -				caller_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
  1.1149 -
  1.1150 -				// actually, with varargs, the this pointer is the first param and the memret ptr
  1.1151 -				// is the second one
  1.1152 -				if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
  1.1153 -				{
  1.1154 -					IA32_Push_Reg(&m_HookFunc, REG_ECX);
  1.1155 -				}
  1.1156 -			}
  1.1157 -
  1.1158 -			// call
  1.1159 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_vfnptr_origentry);
  1.1160 -			CheckAlignmentBeforeCall();
  1.1161 -			IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1162 -
  1.1163 -			AlignStackAfterCall(alignBytes);
  1.1164 -
  1.1165 -			// cleanup
  1.1166 -			if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
  1.1167 -				IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, caller_clean_bytes + SIZE_PTR, MOD_REG);
  1.1168 -
  1.1169 -			DestroyParams(v_place_fbrr_base);
  1.1170 -
  1.1171 -			// save retval
  1.1172 -			SaveRetVal(v_orig_ret, v_place_for_memret);
  1.1173 -
  1.1174 -			// Skip don't call variant
  1.1175 -			tmppos3 = IA32_Jump_Imm32(&m_HookFunc, 0);
  1.1176 -			m_HookFunc.start_count(counter3);
  1.1177 -
  1.1178 -
  1.1179 -			// don't call:
  1.1180 -			m_HookFunc.end_count(counter);
  1.1181 -			m_HookFunc.rewrite(tmppos, static_cast<jit_uint32_t>(counter));
  1.1182 -
  1.1183 -			m_HookFunc.end_count(counter2);
  1.1184 -			m_HookFunc.rewrite(tmppos2, static_cast<jit_uint32_t>(counter2));
  1.1185 -
  1.1186 -			// *v_orig_ret = *v_override_ret
  1.1187 -			if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
  1.1188 -			{
  1.1189 -				// mov ecx, [ebp + v_override_ret]
  1.1190 -				// mov [ebp + v_orig_ret], ecx
  1.1191 -
  1.1192 -				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
  1.1193 -				IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_ECX, v_orig_ret);
  1.1194 -			}
  1.1195 -			else
  1.1196 -			{
  1.1197 -				if (m_Proto.GetRet().pAssignOperator)
  1.1198 -				{
  1.1199 -					// lea edx, [ebp + v_override_ret]			<-- src addr
  1.1200 -					// lea ecx, [ebp + v_orig_ret]				<-- dest addr
  1.1201 -					// push edx					<-- src addr
  1.1202 -					// gcc: push ecx
  1.1203 -					// call it
  1.1204 -					// gcc: clean up
  1.1205 -
  1.1206 -					jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR, AlignStack_GCC_ThisOnStack);
  1.1207 -
  1.1208 -					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_override_ret);
  1.1209 -					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
  1.1210 -					IA32_Push_Reg(&m_HookFunc, REG_EDX);
  1.1211 -					GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1.1212 -
  1.1213 -					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
  1.1214 -					CheckAlignmentBeforeCall();
  1.1215 -					IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1216 -					GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
  1.1217 -
  1.1218 -					AlignStackAfterCall(tmpAlign);
  1.1219 -				}
  1.1220 -				else
  1.1221 -				{
  1.1222 -					// bitwise copy
  1.1223 -					BitwiseCopy_Setup();
  1.1224 -
  1.1225 -					//lea edi, [ebp+v_orig_ret]			<-- destination
  1.1226 -					//lea esi, [ebp+v_override_ret]		<-- src
  1.1227 -					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_orig_ret);
  1.1228 -					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_override_ret);
  1.1229 -
  1.1230 -					BitwiseCopy_Do(m_Proto.GetRet().size);
  1.1231 -				}
  1.1232 -			}
  1.1233 -
  1.1234 -			// skip don't call label target:
  1.1235 -			m_HookFunc.end_count(counter3);
  1.1236 -			m_HookFunc.rewrite(tmppos3, static_cast<jit_uint32_t>(counter3));
  1.1237 -		}
  1.1238 -
  1.1239 -		// Sets *v_pContext to return value
  1.1240 -		void GenContext::CallSetupHookLoop(int v_orig_ret, int v_override_ret, 
  1.1241 -			int v_cur_res, int v_prev_res, int v_status, int v_vfnptr_origentry,
  1.1242 -			int v_this, int v_pContext)
  1.1243 -		{
  1.1244 -			// call shptr->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this),
  1.1245 -			//  &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
  1.1246 -			// The last two params are null for void funcs.
  1.1247 -
  1.1248 -			if (m_Proto.GetRet().size == 0)
  1.1249 -			{
  1.1250 -				// void
  1.1251 -				IA32_Push_Imm8(&m_HookFunc, 0);		// orig_ret
  1.1252 -				IA32_Push_Imm8(&m_HookFunc, 0);		// override_ret
  1.1253 -			}
  1.1254 -			else
  1.1255 -			{
  1.1256 -				// orig_ret and override_ret
  1.1257 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, v_override_ret);
  1.1258 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_orig_ret);
  1.1259 -				IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1.1260 -				IA32_Push_Reg(&m_HookFunc, REG_EDX);
  1.1261 -			}
  1.1262 -
  1.1263 -			// cur_res and prev_res
  1.1264 -			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
  1.1265 -			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_prev_res);
  1.1266 -			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1.1267 -			IA32_Push_Reg(&m_HookFunc, REG_EDX);
  1.1268 -
  1.1269 -			// status and vfnptr_origentry
  1.1270 -			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_status);
  1.1271 -			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_vfnptr_origentry);
  1.1272 -			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1.1273 -			IA32_Push_Reg(&m_HookFunc, REG_EDX);
  1.1274 -
  1.1275 -			// this
  1.1276 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_this);
  1.1277 -			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1.1278 -
  1.1279 -			// our vfn ptr
  1.1280 -			//  *(this + vtbloffs) + SIZE_PTR*vtblidx
  1.1281 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);			// get this into ecx (gcc!)
  1.1282 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, m_VtblOffs);
  1.1283 -			IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_EAX, m_VtblIdx * SIZE_PTR, MOD_REG);
  1.1284 -			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1.1285 -
  1.1286 -			// *m_pHI
  1.1287 -			IA32_Mov_Rm_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
  1.1288 -			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
  1.1289 -			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1.1290 -
  1.1291 -			// set up thisptr
  1.1292 -#if SH_COMP == SH_COMP_GCC
  1.1293 -			//  on gcc/mingw, this is the first parameter
  1.1294 -			GCC_ONLY(IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr)));
  1.1295 -#elif SH_COMP == SH_COMP_MSVC
  1.1296 -			//  on msvc, it's ecx
  1.1297 -			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
  1.1298 -#endif
  1.1299 -
  1.1300 -			// call the function. vtbloffs = 0, vtblidx = 19
  1.1301 -			// get vtptr into edx  --  we know shptr on jit time -> dereference it here!
  1.1302 -			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, 
  1.1303 -				(*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[19]);
  1.1304 -
  1.1305 -			IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1306 -
  1.1307 -			// on gcc/mingw, we have to clean up after the call
  1.1308 -			// 9 params + hidden thisptr param
  1.1309 -			GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 10*SIZE_PTR, MOD_REG));
  1.1310 -
  1.1311 -			// store return value
  1.1312 -			IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_pContext);
  1.1313 -		}
  1.1314 -
  1.1315 -		void GenContext::CallEndContext(int v_pContext)
  1.1316 -		{
  1.1317 -			// call endcontext:
  1.1318 -			// shptr->EndContext(pContex)
  1.1319 -			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_pContext);
  1.1320 -			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1.1321 -
  1.1322 -			// thisptr
  1.1323 -#if SH_COMP == SH_COMP_GCC
  1.1324 -			//  on gcc/mingw, this is the first parameter
  1.1325 -			IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr));
  1.1326 -#elif SH_COMP == SH_COMP_MSVC
  1.1327 -			//  on msvc, it's ecx
  1.1328 -			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
  1.1329 -#endif
  1.1330 -
  1.1331 -			// get vtptr into edx  --  we know shptr on jit time -> dereference it here!
  1.1332 -			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, 
  1.1333 -				(*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[20]);
  1.1334 -
  1.1335 -			IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1336 -
  1.1337 -			// on gcc/mingw, we have to clean up after the call
  1.1338 -			// 1 param + hidden thisptr param
  1.1339 -			GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
  1.1340 -		}
  1.1341 -
  1.1342 -		void GenContext::ResetFrame(jit_int32_t startOffset)
  1.1343 -		{
  1.1344 -			m_HookFunc_FrameOffset = startOffset;
  1.1345 -			m_HookFunc_FrameVarsSize = 0;
  1.1346 -		}
  1.1347 -		
  1.1348 -		jit_int32_t GenContext::AddVarToFrame(jit_int32_t size)
  1.1349 -		{
  1.1350 -			m_HookFunc_FrameOffset -= size;
  1.1351 -			m_HookFunc_FrameVarsSize += size;
  1.1352 -			return m_HookFunc_FrameOffset;
  1.1353 -		}
  1.1354 -
  1.1355 -		jit_int32_t GenContext::ComputeVarsSize()
  1.1356 -		{
  1.1357 -			return m_HookFunc_FrameVarsSize;
  1.1358 -		}
  1.1359 -		
  1.1360 -		void * GenContext::GenerateHookFunc()
  1.1361 -		{
  1.1362 -			// prologue
  1.1363 -			IA32_Push_Reg(&m_HookFunc, REG_EBP);
  1.1364 -			IA32_Push_Reg(&m_HookFunc, REG_EBX);
  1.1365 -			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EBP, REG_ESP, MOD_REG);
  1.1366 -
  1.1367 -			jit_int32_t v_this = 0;
  1.1368 -			jit_int32_t param_base_offs = 0;
  1.1369 -			if (SH_COMP == SH_COMP_GCC || (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
  1.1370 -			{
  1.1371 -				// gcc or msvc with varargs:
  1.1372 -				v_this = 12;		// first param
  1.1373 -				param_base_offs = 16;
  1.1374 -				ResetFrame(0);
  1.1375 -			}
  1.1376 -			else
  1.1377 -			{
  1.1378 -				// on msvc without varargs, save thisptr
  1.1379 -				v_this = -4;
  1.1380 -				param_base_offs = 12;
  1.1381 -				IA32_Push_Reg(&m_HookFunc, REG_ECX);
  1.1382 -				ResetFrame(-4);			// start placing local vars on offset -4
  1.1383 -										// because there already is the thisptr variable
  1.1384 -			}
  1.1385 -
  1.1386 -			// ********************** stack frame **********************
  1.1387 -			//															MSVC without varargs
  1.1388 -			//   second param (gcc: first real param)	ebp + 16
  1.1389 -			//   first param (gcc: thisptr)				ebp + 12
  1.1390 -			//   ret address:							ebp + 8
  1.1391 -			//   caller's ebp							ebp + 4
  1.1392 -			//   saved ebx								ebp
  1.1393 -			//   MSVC ONLY:	current this				ebp - 4
  1.1394 -			//   void *vfnptr_origentry					ebp - 4			-4
  1.1395 -			//   META_RES status = MRES_IGNORED			ebp - 8			-4
  1.1396 -			//   META_RES prev_res						ebp - 12		-4
  1.1397 -			//   META_RES cur_res						ebp - 16		-4
  1.1398 -			//   IMyDelegate *iter						ebp - 20		-4
  1.1399 -			//   IHookContext *pContext					ebp - 24		-4
  1.1400 -			//  == 3 ptrs + 3 enums = 24 bytes
  1.1401 -			//
  1.1402 -			// non-void: add:
  1.1403 -			//   my_rettype *ret_ptr					ebp - 28 		-4
  1.1404 -			//   my_rettype orig_ret					ebp - 28 - sizeof(my_rettype)			-4
  1.1405 -			//   my_rettype override_ret				ebp - 28 - sizeof(my_rettype)*2			-4
  1.1406 -			//   my_rettype plugin_ret					ebp - 28 - sizeof(my_rettype)*3			-4
  1.1407 -			//  == + 3 * sizeof(my_rettype) bytes
  1.1408 -
  1.1409 -			// if required:
  1.1410 -			//   my_rettype place_for_memret			ebp - 28 - sizeof(my_rettype)*4			-4
  1.1411 -
  1.1412 -			// gcc only: if required:
  1.1413 -			//   place forced byref params              ebp - 28 - sizeof(my_rettype)*{4 or 5}
  1.1414 -			//
  1.1415 -			// varargs:
  1.1416 -			//   va_list argptr
  1.1417 -			//   char va_buf[something];
  1.1418 -			
  1.1419 -
  1.1420 -			const jit_int8_t v_vfnptr_origentry =	AddVarToFrame(SIZE_PTR);
  1.1421 -			const jit_int8_t v_status =				AddVarToFrame(sizeof(META_RES));
  1.1422 -			const jit_int8_t v_prev_res =			AddVarToFrame(sizeof(META_RES));
  1.1423 -			const jit_int8_t v_cur_res =			AddVarToFrame(sizeof(META_RES));
  1.1424 -			const jit_int8_t v_iter =				AddVarToFrame(SIZE_PTR);
  1.1425 -			const jit_int8_t v_pContext =			AddVarToFrame(SIZE_PTR);
  1.1426 -
  1.1427 -			// Memory return: first param is the address
  1.1428 -			jit_int32_t v_memret_addr = 0;
  1.1429 -			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
  1.1430 -			{
  1.1431 -				if (SH_COMP == SH_COMP_GCC)
  1.1432 -				{
  1.1433 -					// gcc: now:	first param = mem ret addr
  1.1434 -					//				second param = this pointer
  1.1435 -					//				third param = actual first param
  1.1436 -					v_memret_addr = 12;
  1.1437 -					v_this += 4;
  1.1438 -					param_base_offs += SIZE_PTR;
  1.1439 -				}
  1.1440 -				else   // MSVC
  1.1441 -				{
  1.1442 -					if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
  1.1443 -					{
  1.1444 -						// varargs -> cdecl
  1.1445 -						// msvc: now:
  1.1446 -						//     first param = this pointer
  1.1447 -						//     second param = mem ret addr
  1.1448 -						//     third param = actual first param
  1.1449 -
  1.1450 -						// params_base_offs is already updated to point to after the this pointer
  1.1451 -						v_memret_addr = param_base_offs;
  1.1452 -						param_base_offs += SIZE_PTR;
  1.1453 -					}
  1.1454 -					else
  1.1455 -					{
  1.1456 -						v_memret_addr = param_base_offs;
  1.1457 -						param_base_offs += SIZE_PTR;
  1.1458 -					}
  1.1459 -				}
  1.1460 -			}
  1.1461 -
  1.1462 -			jit_int32_t v_ret_ptr = 0;
  1.1463 -			jit_int32_t v_orig_ret = 0;
  1.1464 -			jit_int32_t v_override_ret = 0;
  1.1465 -			jit_int32_t v_plugin_ret = 0;
  1.1466 -			
  1.1467 -			if (m_Proto.GetRet().size != 0)
  1.1468 -			{
  1.1469 -				v_ret_ptr =				AddVarToFrame(SIZE_PTR);
  1.1470 -				v_orig_ret =			AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
  1.1471 -				v_override_ret =		AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
  1.1472 -				v_plugin_ret =			AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
  1.1473 -			}
  1.1474 -
  1.1475 -			jit_int32_t v_place_for_memret = 0;
  1.1476 -			if (MemRetWithTempObj())
  1.1477 -			{
  1.1478 -				v_place_for_memret = 	AddVarToFrame(GetParamStackSize(m_Proto.GetRet()));
  1.1479 -			}
  1.1480 -
  1.1481 -			jit_int32_t v_place_fbrr_base  = 0;
  1.1482 -			if (SH_COMP == SH_COMP_GCC && GetForcedByRefParamsSize())
  1.1483 -			{
  1.1484 -				v_place_fbrr_base =		AddVarToFrame(GetForcedByRefParamsSize());
  1.1485 -			}
  1.1486 -
  1.1487 -			// Only exists for varargs functions
  1.1488 -			jit_int32_t v_va_argptr = 0;
  1.1489 -			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
  1.1490 -			{
  1.1491 -				v_va_argptr = AddVarToFrame(SIZE_PTR);
  1.1492 -			}
  1.1493 -			
  1.1494 -			jit_int32_t v_va_buf = 0;
  1.1495 -			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
  1.1496 -			{
  1.1497 -				v_va_buf = AddVarToFrame(SourceHook::STRBUF_LEN);
  1.1498 -			}
  1.1499 -			
  1.1500 -			IA32_Sub_Rm_Imm32(&m_HookFunc, REG_ESP, ComputeVarsSize(), MOD_REG);
  1.1501 -
  1.1502 -			// Initial stack alignment
  1.1503 -			IA32_And_Rm_Imm32(&m_HookFunc, REG_ESP, MOD_REG, -16);
  1.1504 -			m_BytesPushedAfterInitialAlignment = 0;
  1.1505 -
  1.1506 -			// init status localvar
  1.1507 -			IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_status);
  1.1508 -
  1.1509 -			// VarArgs: init argptr & format
  1.1510 -			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
  1.1511 -			{
  1.1512 -				// argptr = first vararg param
  1.1513 -				// lea eax, [ebp + param_base_offs + paramssize]
  1.1514 -				// mov argptr, eax
  1.1515 -
  1.1516 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_base_offs + GetParamsTotalStackSize() + SIZE_PTR);	// +SIZE_PTR: last const char * is not in protoinfo
  1.1517 -				IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_va_argptr);
  1.1518 -			}
  1.1519 -			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVafmt)
  1.1520 -			{
  1.1521 -				// vsnprintf
  1.1522 -				
  1.1523 -				jit_int32_t tmpAlign = AlignStackBeforeCall(SIZE_PTR*3 + sizeof(size_t), 0);
  1.1524 -
  1.1525 -				// push valist, fmt param, maxsize, buffer
  1.1526 -				IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1.1527 -				IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_base_offs + GetParamsTotalStackSize());		// last given param (+4-4, see above)
  1.1528 -				IA32_Push_Imm32(&m_HookFunc, SourceHook::STRBUF_LEN - 1);
  1.1529 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_va_buf);
  1.1530 -				IA32_Push_Reg(&m_HookFunc, REG_ECX);
  1.1531 -				
  1.1532 -				// call
  1.1533 -				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(&vsnprintf));
  1.1534 -				CheckAlignmentBeforeCall();
  1.1535 -				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1536 -
  1.1537 -				// Clean up (cdecl)
  1.1538 -				IA32_Add_Rm_Imm32(&m_HookFunc, REG_ESP, 0x10, MOD_REG);
  1.1539 -
  1.1540 -				AlignStackAfterCall(tmpAlign);
  1.1541 -
  1.1542 -				// Set trailing zero
  1.1543 -				IA32_Xor_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_REG);
  1.1544 -				IA32_Mov_Rm8_Reg8_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_va_buf + SourceHook::STRBUF_LEN - 1);
  1.1545 -			}
  1.1546 -			
  1.1547 -			// Call constructors for ret vars if required
  1.1548 -			if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
  1.1549 -				m_Proto.GetRet().pNormalCtor)
  1.1550 -			{
  1.1551 -				jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
  1.1552 -
  1.1553 -				// orig_reg
  1.1554 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
  1.1555 -				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1.1556 -				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
  1.1557 -				CheckAlignmentBeforeCall();
  1.1558 -				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1559 -				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1.1560 -
  1.1561 -				// override_reg
  1.1562 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
  1.1563 -				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1.1564 -				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
  1.1565 -				CheckAlignmentBeforeCall();
  1.1566 -				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1567 -				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1.1568 -
  1.1569 -				// plugin_ret
  1.1570 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
  1.1571 -				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1.1572 -				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
  1.1573 -				CheckAlignmentBeforeCall();
  1.1574 -				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1575 -				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1.1576 -
  1.1577 -				AlignStackAfterCall(tmpAlign);
  1.1578 -
  1.1579 -				// _don't_ call a constructor for v_place_for_memret !
  1.1580 -			}
  1.1581 -
  1.1582 -			// ********************** SetupHookLoop **********************
  1.1583 -			CallSetupHookLoop(v_orig_ret, v_override_ret, v_cur_res, v_prev_res, v_status, v_vfnptr_origentry,
  1.1584 -				v_this, v_pContext);
  1.1585 -
  1.1586 -			// ********************** call pre hooks **********************
  1.1587 -			GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
  1.1588 -				v_plugin_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
  1.1589 -
  1.1590 -			// ********************** call orig func **********************
  1.1591 -			GenerateCallOrig(v_status, v_pContext, param_base_offs, v_this, v_vfnptr_origentry, v_orig_ret,
  1.1592 -				v_override_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
  1.1593 -
  1.1594 -			// ********************** call post hooks **********************
  1.1595 -			GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
  1.1596 -				v_plugin_ret, v_place_for_memret, v_place_fbrr_base, v_va_buf);
  1.1597 -
  1.1598 -			// ********************** end context and return **********************
  1.1599 -
  1.1600 -			PrepareReturn(v_status, v_pContext, v_ret_ptr);
  1.1601 -
  1.1602 -			CallEndContext(v_pContext);
  1.1603 -
  1.1604 -			// Call destructors of byval object params which have a destructor
  1.1605 -
  1.1606 -			jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
  1.1607 -
  1.1608 -			jit_int32_t cur_param_pos = param_base_offs;
  1.1609 -			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
  1.1610 -			{
  1.1611 -				const IntPassInfo &pi = m_Proto.GetParam(i);
  1.1612 -				// GCC: NOT of forced byref params. the caller destructs those.
  1.1613 -				if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
  1.1614 -					(pi.flags & PassInfo::PassFlag_ByVal) && !(pi.flags & PassFlag_ForcedByRef))
  1.1615 -				{
  1.1616 -					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, cur_param_pos);
  1.1617 -					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
  1.1618 -					GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1.1619 -					CheckAlignmentBeforeCall();
  1.1620 -					IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1621 -					GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1.1622 -				}
  1.1623 -				cur_param_pos += GetParamStackSize(pi);
  1.1624 -			}
  1.1625 -
  1.1626 -			AlignStackAfterCall(tmpAlign);
  1.1627 -
  1.1628 -			DoReturn(v_ret_ptr, v_memret_addr);
  1.1629 -
  1.1630 -			// Call destructors of orig_ret/ ...
  1.1631 -			if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
  1.1632 -				m_Proto.GetRet().pDtor)
  1.1633 -			{
  1.1634 -				// Preserve return value in EAX(:EDX)
  1.1635 -				IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1.1636 -				IA32_Push_Reg(&m_HookFunc, REG_EDX);
  1.1637 -
  1.1638 -				m_BytesPushedAfterInitialAlignment += 8;
  1.1639 -
  1.1640 -				jit_int32_t tmpAlign = AlignStackBeforeCall(0, AlignStack_GCC_ThisOnStack);
  1.1641 -
  1.1642 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
  1.1643 -				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1.1644 -				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
  1.1645 -				CheckAlignmentBeforeCall();
  1.1646 -				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1647 -				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1.1648 -
  1.1649 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
  1.1650 -				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1.1651 -				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
  1.1652 -				CheckAlignmentBeforeCall();
  1.1653 -				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1654 -				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1.1655 -
  1.1656 -				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
  1.1657 -				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1.1658 -				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
  1.1659 -				CheckAlignmentBeforeCall();
  1.1660 -				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1.1661 -				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1.1662 -
  1.1663 -				AlignStackAfterCall(tmpAlign);
  1.1664 -
  1.1665 -				IA32_Pop_Reg(&m_HookFunc, REG_EDX);
  1.1666 -				IA32_Pop_Reg(&m_HookFunc, REG_EAX);
  1.1667 -
  1.1668 -				m_BytesPushedAfterInitialAlignment -= 8;
  1.1669 -			}
  1.1670 -
  1.1671 -			// epilogue
  1.1672 -			IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESP, REG_EBP, MOD_REG);
  1.1673 -			IA32_Pop_Reg(&m_HookFunc, REG_EBX);
  1.1674 -			IA32_Pop_Reg(&m_HookFunc, REG_EBP);
  1.1675 -
  1.1676 -			if (SH_COMP == SH_COMP_MSVC && !(m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs))
  1.1677 -			{
  1.1678 -				// msvc without varargs:
  1.1679 -				//  callee cleans the stack
  1.1680 -
  1.1681 -				short cleansize = GetParamsTotalStackSize();
  1.1682 -				// Memory return: address is first param
  1.1683 -				if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
  1.1684 -					cleansize += SIZE_PTR;
  1.1685 -				
  1.1686 -				IA32_Return_Popstack(&m_HookFunc, cleansize);
  1.1687 -			}
  1.1688 -			else
  1.1689 -			{
  1.1690 -				// gcc or msvc with varargs: caller cleans the stack
  1.1691 -				//  exception: gcc removes the memret addr on memret: 
  1.1692 -				if (SH_COMP == SH_COMP_GCC && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem))
  1.1693 -					IA32_Return_Popstack(&m_HookFunc, SIZE_PTR);
  1.1694 -				else
  1.1695 -					IA32_Return(&m_HookFunc);
  1.1696 -			}
  1.1697 -			
  1.1698 -			
  1.1699 -			// Store pointer for later use
  1.1700 -			// m_HookfuncVfnPtr is a pointer to a void* because SH expects a pointer
  1.1701 -			// into the hookman's vtable
  1.1702 -			*m_HookfuncVfnptr = reinterpret_cast<void*>(m_HookFunc.GetData());
  1.1703 -
  1.1704 -			m_HookFunc.SetRE();
  1.1705 -
  1.1706 -			return m_HookFunc.GetData();
  1.1707 -		}
  1.1708 -
  1.1709 -		// Pre-condition: GenerateHookFunc() has been called!
  1.1710 -		void * GenContext::GeneratePubFunc()
  1.1711 -		{
  1.1712 -			jitoffs_t counter, tmppos;
  1.1713 -
  1.1714 -			// The pubfunc is a static cdecl function.
  1.1715 -			// C Code:
  1.1716 -			//  int HookManPubFunc(
  1.1717 -			//     bool store,				ebp + 8
  1.1718 -			//     IHookManagerInfo *hi		ebp + 12
  1.1719 -			//     )
  1.1720 -			//  {
  1.1721 -			//    if (store)
  1.1722 -			//      *m_pHI = hi;
  1.1723 -			//    if (hi)
  1.1724 -			//      hi->SetInfo(HOOKMAN_VERSION, m_VtblOffs, m_VtblIdx, m_Proto.GetProto(), m_HookfuncVfnptr)
  1.1725 -			//  }
  1.1726 -
  1.1727 -			// prologue
  1.1728 -			IA32_Push_Reg(&m_PubFunc, REG_EBP);
  1.1729 -			IA32_Mov_Reg_Rm(&m_PubFunc, REG_EBP, REG_ESP, MOD_REG);
  1.1730 -
  1.1731 -			
  1.1732 -			// save store in eax, hi in ecx
  1.1733 -			IA32_Movzx_Reg32_Rm8_Disp8(&m_PubFunc, REG_EAX, REG_EBP, 8);
  1.1734 -			IA32_Mov_Reg_Rm_DispAuto(&m_PubFunc, REG_ECX, REG_EBP, 12);
  1.1735 -
  1.1736 -			// Check for store == 0
  1.1737 -			IA32_Test_Rm_Reg8(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
  1.1738 -			tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
  1.1739 -			m_PubFunc.start_count(counter);
  1.1740 -
  1.1741 -			// nonzero -> store hi
  1.1742 -			IA32_Mov_Rm_Imm32(&m_PubFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
  1.1743 -			IA32_Mov_Rm_Reg(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
  1.1744 -
  1.1745 -			// zero
  1.1746 -			m_PubFunc.end_count(counter);
  1.1747 -			m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
  1.1748 -
  1.1749 -			// check for hi == 0
  1.1750 -			IA32_Test_Rm_Reg(&m_PubFunc, REG_ECX, REG_ECX, MOD_REG);
  1.1751 -			tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
  1.1752 -			m_PubFunc.start_count(counter);
  1.1753 -
  1.1754 -			// nonzero -> call vfunc
  1.1755 -			//  push params in reverse order
  1.1756 -			IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_HookfuncVfnptr));
  1.1757 -			IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_BuiltPI));
  1.1758 -			IA32_Push_Imm32(&m_PubFunc, m_VtblIdx);
  1.1759 -			IA32_Push_Imm32(&m_PubFunc, m_VtblOffs);
  1.1760 -			IA32_Push_Imm32(&m_PubFunc, SH_HOOKMAN_VERSION);
  1.1761 -
  1.1762 -			//  hi == this is in ecx
  1.1763 -			//  on gcc/mingw, ecx is the first parameter
  1.1764 -#if SH_COMP == SH_COMP_GCC
  1.1765 -			IA32_Push_Reg(&m_PubFunc, REG_ECX);
  1.1766 -#endif
  1.1767 -			
  1.1768 -			// call the function. vtbloffs = 0, vtblidx = 0
  1.1769 -			// get vtptr into edx
  1.1770 -			IA32_Mov_Reg_Rm(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
  1.1771 -			// get funcptr into eax
  1.1772 -			IA32_Mov_Reg_Rm(&m_PubFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
  1.1773 -
  1.1774 -			IA32_Call_Reg(&m_PubFunc, REG_EAX);
  1.1775 -
  1.1776 -			// on gcc/mingw, we have to clean up after the call
  1.1777 -#if SH_COMP == SH_COMP_GCC
  1.1778 -			// 5 params + hidden thisptr param
  1.1779 -			IA32_Add_Rm_Imm8(&m_PubFunc, REG_ESP, 6*SIZE_MWORD, MOD_REG);
  1.1780 -#endif
  1.1781 -
  1.1782 -			// zero
  1.1783 -			m_PubFunc.end_count(counter);
  1.1784 -			m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
  1.1785 -
  1.1786 -			// return value
  1.1787 -			IA32_Xor_Reg_Rm(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
  1.1788 -
  1.1789 -			// epilogue
  1.1790 -			IA32_Mov_Reg_Rm(&m_PubFunc, REG_ESP, REG_EBP, MOD_REG);
  1.1791 -			IA32_Pop_Reg(&m_PubFunc, REG_EBP);
  1.1792 -			IA32_Return(&m_PubFunc);
  1.1793 -
  1.1794 -			m_PubFunc.SetRE();
  1.1795 -
  1.1796 -			return m_PubFunc;
  1.1797 -		}
  1.1798 -
  1.1799 -		bool GenContext::PassInfoSupported(const IntPassInfo &pi, bool is_ret)
  1.1800 -		{
  1.1801 -			// :TODO: Error returns
  1.1802 -			if (pi.type != PassInfo::PassType_Basic &&
  1.1803 -				pi.type != PassInfo::PassType_Float &&
  1.1804 -				pi.type != PassInfo::PassType_Object)
  1.1805 -			{
  1.1806 -				return false;
  1.1807 -			}
  1.1808 -
  1.1809 -			if (pi.type == PassInfo::PassType_Object &&
  1.1810 -				(pi.flags & PassInfo::PassFlag_ByVal))
  1.1811 -			{
  1.1812 -				if ((pi.flags & PassInfo::PassFlag_CCtor) && !pi.pCopyCtor)
  1.1813 -				{
  1.1814 -					return false;
  1.1815 -				}
  1.1816 -
  1.1817 -				if ((pi.flags & PassInfo::PassFlag_ODtor) && !pi.pDtor)
  1.1818 -				{
  1.1819 -					return false;
  1.1820 -				}
  1.1821 -				
  1.1822 -				if ((pi.flags & PassInfo::PassFlag_AssignOp) && !pi.pAssignOperator)
  1.1823 -				{
  1.1824 -					return false;
  1.1825 -				}
  1.1826 -
  1.1827 -				if ((pi.flags & PassInfo::PassFlag_OCtor) && !pi.pNormalCtor)
  1.1828 -				{
  1.1829 -					return false;
  1.1830 -				}
  1.1831 -			}
  1.1832 -
  1.1833 -			if ((pi.flags & (PassInfo::PassFlag_ByVal | PassInfo::PassFlag_ByRef)) == 0)
  1.1834 -			{
  1.1835 -				return false;			 // Neither byval nor byref!
  1.1836 -			}
  1.1837 -			return true;
  1.1838 -		}
  1.1839 -
  1.1840 -		void GenContext::AutoDetectRetType()
  1.1841 -		{
  1.1842 -			IntPassInfo &pi = m_Proto.GetRet();
  1.1843 -
  1.1844 -			// Only relevant for byval types
  1.1845 -			if (pi.flags & PassInfo::PassFlag_ByVal)
  1.1846 -			{
  1.1847 -				// Basic + float: 
  1.1848 -				if (pi.type == PassInfo::PassType_Basic ||
  1.1849 -					pi.type == PassInfo::PassType_Float)
  1.1850 -				{
  1.1851 -					// <= 8 bytes:
  1.1852 -					//    _always_ in registers, no matter what the user says
  1.1853 -					if (pi.size <= 8)
  1.1854 -					{
  1.1855 -						pi.flags &= ~PassInfo::PassFlag_RetMem;
  1.1856 -						pi.flags |= PassInfo::PassFlag_RetReg;
  1.1857 -					}
  1.1858 -					else
  1.1859 -					{
  1.1860 -						// Does this even exist? No idea, if it does: in memory!
  1.1861 -						pi.flags &= ~PassInfo::PassFlag_RetReg;
  1.1862 -						pi.flags |= PassInfo::PassFlag_RetMem;
  1.1863 -					}
  1.1864 -				}
  1.1865 -				// Object: 
  1.1866 -				else if (pi.type == PassInfo::PassType_Object)
  1.1867 -				{
  1.1868 -					// If the user says nothing, auto-detect
  1.1869 -					if ((pi.flags & (PassInfo::PassFlag_RetMem | PassInfo::PassFlag_RetReg)) == 0)
  1.1870 -					{
  1.1871 -#if SH_COMP == SH_COMP_MSVC
  1.1872 -						// MSVC seems to return _all_ structs, classes, unions in memory
  1.1873 -						pi.flags |= PassInfo::PassFlag_RetMem;
  1.1874 -#elif SH_COMP == SH_COMP_GCC
  1.1875 -						// Same goes for GCC :)
  1.1876 -						pi.flags |= PassInfo::PassFlag_RetMem;
  1.1877 -#endif
  1.1878 -					}
  1.1879 -				}
  1.1880 -			}
  1.1881 -			else
  1.1882 -			{
  1.1883 -				// byref: make sure that the flag is _not_ set
  1.1884 -				pi.flags &= ~PassInfo::PassFlag_RetMem;
  1.1885 -				pi.flags |= PassInfo::PassFlag_RetReg;
  1.1886 -			}
  1.1887 -		}
  1.1888 -
  1.1889 -		void GenContext::AutoDetectParamFlags()
  1.1890 -		{
  1.1891 -#if SH_COMP == SH_COMP_GCC
  1.1892 -			// On GCC, all objects are passed by reference if they have a destructor
  1.1893 -			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
  1.1894 -			{
  1.1895 -				IntPassInfo &pi = m_Proto.GetParam(i);
  1.1896 -				if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ByVal) && 
  1.1897 -					(pi.flags & PassInfo::PassFlag_ODtor))
  1.1898 -				{
  1.1899 -					pi.flags |= PassFlag_ForcedByRef;
  1.1900 -				}
  1.1901 -			}
  1.1902 -#endif
  1.1903 -		}
  1.1904 -
  1.1905 -		HookManagerPubFunc GenContext::Generate()
  1.1906 -		{
  1.1907 -			Clear();
  1.1908 -
  1.1909 -			// Check conditions:
  1.1910 -			// -1) good proto version
  1.1911 -			//  0) we don't support unknown passtypes, convention, ...
  1.1912 -			//  1) we don't support functions which return objects by value or take parameters by value
  1.1913 -			//     that have a constructor, a destructor or an overloaded assignment op
  1.1914 -			//     (we wouldn't know how to call it!)
  1.1915 -
  1.1916 -			if (m_Proto.GetVersion() < 1)
  1.1917 -			{
  1.1918 -				return NULL;
  1.1919 -			}
  1.1920 -
  1.1921 -			AutoDetectRetType();
  1.1922 -			AutoDetectParamFlags();
  1.1923 -
  1.1924 -			// Basically, we only support ThisCall/thiscall with varargs
  1.1925 -			if ((m_Proto.GetConvention() & (~ProtoInfo::CallConv_HasVafmt)) != ProtoInfo::CallConv_ThisCall)
  1.1926 -			{
  1.1927 -				return NULL;
  1.1928 -			}
  1.1929 -
  1.1930 -
  1.1931 -			if (m_Proto.GetRet().size != 0 && !PassInfoSupported(m_Proto.GetRet(), true))
  1.1932 -			{
  1.1933 -				return NULL;
  1.1934 -			}
  1.1935 -
  1.1936 -			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
  1.1937 -			{
  1.1938 -				if (!PassInfoSupported(m_Proto.GetParam(i), false))
  1.1939 -					return NULL;
  1.1940 -			}
  1.1941 -
  1.1942 -			BuildProtoInfo();
  1.1943 -			GenerateHookFunc();
  1.1944 -			return fastdelegate::detail::horrible_cast<HookManagerPubFunc>(GeneratePubFunc());
  1.1945 -		}
  1.1946 -
  1.1947 -		HookManagerPubFunc GenContext::GetPubFunc()
  1.1948 -		{
  1.1949 -			if (m_GeneratedPubFunc == 0)
  1.1950 -				m_GeneratedPubFunc = Generate();
  1.1951 -
  1.1952 -			return m_GeneratedPubFunc;
  1.1953 -		}
  1.1954 -
  1.1955 -		bool GenContext::Equal(const CProto &proto, int vtbl_offs, int vtbl_idx)
  1.1956 -		{
  1.1957 -			return (m_OrigProto.ExactlyEqual(proto) && m_VtblOffs == vtbl_offs && m_VtblIdx == vtbl_idx);
  1.1958 -		}
  1.1959 -
  1.1960 -		bool GenContext::Equal(HookManagerPubFunc other)
  1.1961 -		{
  1.1962 -			return m_GeneratedPubFunc == other;
  1.1963 -		}
  1.1964 -
  1.1965 -		// *********************************** class GenContextContainer
  1.1966 -		CHookManagerAutoGen::CHookManagerAutoGen(ISourceHook *pSHPtr) : m_pSHPtr(pSHPtr)
  1.1967 -		{
  1.1968 -		}
  1.1969 -
  1.1970 -		CHookManagerAutoGen::~CHookManagerAutoGen()
  1.1971 -		{
  1.1972 -			for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
  1.1973 -			{
  1.1974 -				delete iter->m_GenContext;
  1.1975 -			}
  1.1976 -		}
  1.1977 -
  1.1978 -		int CHookManagerAutoGen::GetIfaceVersion()
  1.1979 -		{
  1.1980 -			return SH_HOOKMANAUTOGEN_IFACE_VERSION;
  1.1981 -		}
  1.1982 -
  1.1983 -		int CHookManagerAutoGen::GetImplVersion()
  1.1984 -		{
  1.1985 -			return SH_HOOKMANAUTOGEN_IMPL_VERSION;
  1.1986 -		}
  1.1987 -
  1.1988 -		HookManagerPubFunc CHookManagerAutoGen::MakeHookMan(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx)
  1.1989 -		{
  1.1990 -			CProto mproto(proto);
  1.1991 -			for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
  1.1992 -			{
  1.1993 -				if (iter->m_GenContext->Equal(mproto, vtbl_offs, vtbl_idx))
  1.1994 -				{
  1.1995 -					iter->m_RefCnt++;
  1.1996 -					return iter->m_GenContext->GetPubFunc();
  1.1997 -				}
  1.1998 -			}
  1.1999 -
  1.2000 -			// Not found yet -> new one
  1.2001 -			StoredContext sctx;
  1.2002 -			sctx.m_RefCnt = 1;
  1.2003 -			sctx.m_GenContext = new GenContext(proto, vtbl_offs, vtbl_idx, m_pSHPtr);
  1.2004 -
  1.2005 -			if (sctx.m_GenContext->GetPubFunc() == NULL)
  1.2006 -			{
  1.2007 -				return NULL;
  1.2008 -			}
  1.2009 -			else
  1.2010 -			{
  1.2011 -				m_Contexts.push_back(sctx);
  1.2012 -				return sctx.m_GenContext->GetPubFunc();
  1.2013 -			}
  1.2014 -		}
  1.2015 -
  1.2016 -		void CHookManagerAutoGen::ReleaseHookMan(HookManagerPubFunc pubFunc)
  1.2017 -		{
  1.2018 -			for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
  1.2019 -			{
  1.2020 -				if (iter->m_GenContext->Equal(pubFunc))
  1.2021 -				{
  1.2022 -					iter->m_RefCnt--;
  1.2023 -					if (iter->m_RefCnt == 0)
  1.2024 -					{
  1.2025 -						delete iter->m_GenContext;
  1.2026 -						m_Contexts.erase(iter);
  1.2027 -					}
  1.2028 -					break;
  1.2029 -				}
  1.2030 -			}
  1.2031 -		}
  1.2032 -	}
  1.2033 -}