sourcehook/sourcehook_hookmangen.cpp
author Pavol Marko <pavolmarko@gmail.com>
Sat Nov 17 09:53:58 2007 +0000 (2007-11-17)
branchhookman_autogen
changeset 461 2fa049f36721
parent 456 9f19985c76aa
child 463 7a413a63426f
permissions -rw-r--r--
New hookfunc local vars code; first step to vafmt support
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2007 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko
     8 * Contributor(s): Borja "faluco" Ferav  (many thanks for assitance!)
     9 *				  David "BAILOPAN" Anderson
    10 * ============================
    11 */
    12 
    13 // recommended literature:
    14 // http://www.cs.umbc.edu/~chang/cs313.s02/stack.shtml
    15 // http://www.angelcode.com/dev/callconv/callconv.html
    16 // http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-1.html
    17 
    18 #include "sourcehook_impl.h"
    19 #include "sourcehook_hookmangen.h"
    20 #include "sourcehook_hookmangen_x86.h"
    21 #include "sh_memory.h"
    22 #include <stdarg.h>							// we might need the address of vsnprintf
    23 
    24 #if SH_COMP == SH_COMP_MSVC
    25 # define GCC_ONLY(x)
    26 # define MSVC_ONLY(x) x
    27 #elif SH_COMP == SH_COMP_GCC
    28 # define GCC_ONLY(x) x
    29 # define MSVC_ONLY(x)
    30 #endif
    31 
    32 // :TODO: test BIG vtable indices
    33 
    34 namespace SourceHook
    35 {
    36 	namespace Impl
    37 	{
    38 		CPageAlloc GenBuffer::ms_Allocator;
    39 
    40 		template <class T>
    41 		jit_int32_t DownCastPtr(T ptr)
    42 		{
    43 			return reinterpret_cast<jit_int32_t>(ptr);
    44 		}
    45 
    46 		jit_uint32_t DownCastSize(size_t size)
    47 		{
    48 			return static_cast<jit_uint32_t>(size);
    49 		}
    50 
    51 		GenContext::GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr)
    52 			: m_GeneratedPubFunc(NULL), m_OrigProto(proto), m_Proto(proto), m_VtblOffs(vtbl_offs),
    53 			  m_VtblIdx(vtbl_idx), m_SHPtr(pSHPtr), m_pHI(NULL), m_HookfuncVfnptr(NULL), m_RegCounter(0)
    54 		{
    55 			m_pHI = new void*;
    56 			m_HookfuncVfnptr = new void*;
    57 			m_BuiltPI = new ProtoInfo;
    58 			m_BuiltPI_Params = NULL;
    59 			m_BuiltPI_Params2 = NULL;
    60 		}
    61 
    62 		GenContext::~GenContext()
    63 		{
    64 			Clear();
    65 			delete m_pHI;
    66 			delete m_HookfuncVfnptr;
    67 			delete m_BuiltPI;
    68 		}
    69 
    70 		void GenContext::Clear()
    71 		{
    72 			m_HookFunc.clear();
    73 			m_PubFunc.clear();
    74 			if (m_BuiltPI_Params)
    75 			{
    76 				delete m_BuiltPI_Params;
    77 				m_BuiltPI_Params = NULL;
    78 			}
    79 			if (m_BuiltPI_Params2)
    80 			{
    81 				delete m_BuiltPI_Params2;
    82 				m_BuiltPI_Params2 = NULL;
    83 			}
    84 		}
    85 
    86 		void GenContext::BuildProtoInfo()
    87 		{
    88 			m_BuiltPI->convention = m_Proto.GetConvention();
    89 			m_BuiltPI->numOfParams = m_Proto.GetNumOfParams();
    90 
    91 			m_BuiltPI->retPassInfo.size = m_Proto.GetRet().size;
    92 			m_BuiltPI->retPassInfo.type = m_Proto.GetRet().type;
    93 			m_BuiltPI->retPassInfo.flags = m_Proto.GetRet().flags;
    94 			m_BuiltPI->retPassInfo2.pNormalCtor = m_Proto.GetRet().pNormalCtor;
    95 			m_BuiltPI->retPassInfo2.pCopyCtor = m_Proto.GetRet().pCopyCtor;
    96 			m_BuiltPI->retPassInfo2.pDtor = m_Proto.GetRet().pDtor;
    97 			m_BuiltPI->retPassInfo2.pAssignOperator = m_Proto.GetRet().pAssignOperator;
    98 
    99 			if (m_BuiltPI_Params)
   100 				delete m_BuiltPI_Params;
   101 			m_BuiltPI_Params = new PassInfo[m_BuiltPI->numOfParams + 1];
   102 			if (m_BuiltPI_Params2)
   103 				delete m_BuiltPI_Params2;
   104 			m_BuiltPI_Params2 = new PassInfo::V2Info[m_BuiltPI->numOfParams + 1];
   105 
   106 			m_BuiltPI_Params[0].size = 1;			// Version 1
   107 			m_BuiltPI_Params[0].type = 0;
   108 			m_BuiltPI_Params[0].flags = 0;
   109 			
   110 			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
   111 			{
   112 				m_BuiltPI_Params[i+1].size = m_Proto.GetParam(i).size;
   113 				m_BuiltPI_Params[i+1].type = m_Proto.GetParam(i).type;
   114 				m_BuiltPI_Params[i+1].flags = m_Proto.GetParam(i).flags;
   115 
   116 				m_BuiltPI_Params2[i+1].pNormalCtor = m_Proto.GetParam(i).pNormalCtor;
   117 				m_BuiltPI_Params2[i+1].pCopyCtor = m_Proto.GetParam(i).pCopyCtor;
   118 				m_BuiltPI_Params2[i+1].pDtor = m_Proto.GetParam(i).pDtor;
   119 				m_BuiltPI_Params2[i+1].pAssignOperator = m_Proto.GetParam(i).pAssignOperator;
   120 			}
   121 
   122 			m_BuiltPI->paramsPassInfo = m_BuiltPI_Params;
   123 			m_BuiltPI->paramsPassInfo2 = m_BuiltPI_Params2;
   124 		}
   125 
   126 		jit_int32_t GenContext::GetRealSize(const IntPassInfo &info)
   127 		{
   128 			if (info.flags & PassInfo::PassFlag_ByRef)
   129 			{
   130 				return SIZE_PTR;
   131 			}
   132 			return static_cast<jit_int32_t>(info.size);
   133 		}
   134 
   135 		// Computes size on the stack
   136 		jit_int32_t GenContext::GetStackSize(const IntPassInfo &info)
   137 		{
   138 			// Align up to 4 byte boundaries
   139 			jit_int32_t rs = GetRealSize(info);
   140 			if (rs % 4 != 0)
   141 				rs = (rs & ~(3)) + 4;
   142 			return rs;
   143 		}
   144 
   145 		jit_int8_t GenContext::NextRegEBX_ECX_EDX()
   146 		{
   147 			switch ((m_RegCounter++) % 3)
   148 			{
   149 			case 0:
   150 				return REG_EBX;
   151 			case 1:
   152 				return REG_ECX;
   153 			case 2:
   154 			default:
   155 				m_RegCounter = 0;
   156 				return REG_EDX;
   157 			}
   158 		}
   159 
   160 		void GenContext::BitwiseCopy_Setup()
   161 		{
   162 			//cld
   163 			//push edi
   164 			//push esi
   165 
   166 			IA32_Cld(&m_HookFunc);
   167 			IA32_Push_Reg(&m_HookFunc, REG_EDI);
   168 			IA32_Push_Reg(&m_HookFunc, REG_ESI);
   169 		}
   170 
   171 		void GenContext::BitwiseCopy_Do(size_t size)
   172 		{
   173 			jit_uint32_t dwords = DownCastSize(size) / 4;
   174 			jit_uint32_t bytes = DownCastSize(size) % 4;
   175 
   176 			//if dwords
   177 			// mov ecx, <dwords>
   178 			// rep movsd
   179 			//if bytes
   180 			// mov ecx, <bytes>
   181 			// rep movsb
   182 			//pop esi
   183 			//pop edi
   184 
   185 			if (dwords)
   186 			{
   187 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, dwords);
   188 				IA32_Rep(&m_HookFunc);
   189 				IA32_Movsd(&m_HookFunc);
   190 			}
   191 			if (bytes)
   192 			{
   193 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, bytes);
   194 				IA32_Rep(&m_HookFunc);
   195 				IA32_Movsb(&m_HookFunc);
   196 			}
   197 			IA32_Pop_Reg(&m_HookFunc, REG_ESI);
   198 			IA32_Pop_Reg(&m_HookFunc, REG_EDI);
   199 		}
   200 
   201 		short GenContext::GetParamsStackSize()
   202 		{
   203 			short acc = 0;
   204 			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
   205 			{
   206 				acc += GetStackSize(m_Proto.GetParam(i));
   207 			}
   208 
   209 			// Memory return: address is first param
   210 			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   211 				acc += SIZE_PTR;
   212 
   213 			// :TODO: cdecl: THIS POINTER AS FIRST PARAM!!!
   214 
   215 			return acc;
   216 		}
   217 
   218 		short GenContext::GetForcedByRefParamOffset(int p)
   219 		{
   220 			short off = 0;
   221 			for (int i = 0; i < p; ++i)
   222 			{
   223 				if (m_Proto.GetParam(i).flags & PassFlag_ForcedByRef)
   224 					off += GetStackSize(m_Proto.GetParam(i));
   225 			}
   226 			return off;
   227 		}
   228 
   229 		short GenContext::GetForcedByRefParamsSize()
   230 		{
   231 			return GetForcedByRefParamOffset(m_Proto.GetNumOfParams());
   232 		}
   233 
   234 		jit_int32_t GenContext::PushRef(jit_int32_t param_offset, const IntPassInfo &pi)
   235 		{
   236 			// push [ebp+<offset>]
   237 			IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, param_offset);
   238 
   239 			return SIZE_PTR;
   240 		}
   241 
   242 		jit_int32_t GenContext::PushBasic(jit_int32_t param_offset, const IntPassInfo &pi)
   243 		{
   244 			int reg;
   245 			int reg2;
   246 
   247 			switch (pi.size)
   248 			{
   249 			default:
   250 				SH_ASSERT(0, ("Unsupported!"));
   251 				return 0;
   252 			case 1:
   253 				reg = NextRegEBX_ECX_EDX();
   254 				//movzx reg, BYTE PTR [ebp+<offset>]
   255 				//push reg
   256 				IA32_Movzx_Reg32_Rm8_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
   257 				IA32_Push_Reg(&m_HookFunc, reg);
   258 
   259 				return 4;
   260 			case 2:
   261 				reg = NextRegEBX_ECX_EDX();
   262 				//movzx reg, WORD PTR [ebp+<offset>]
   263 				//push reg
   264 				m_HookFunc.write_ubyte(IA32_16BIT_PREFIX);
   265 				IA32_Movzx_Reg32_Rm16_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
   266 				IA32_Push_Reg(&m_HookFunc, reg);
   267 
   268 				return 4;
   269 			case 4:
   270 				reg = NextRegEBX_ECX_EDX();
   271 				//mov reg, DWORD PTR [ebp+<offset>]
   272 				//push reg
   273 				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset);
   274 				IA32_Push_Reg(&m_HookFunc, reg);
   275 
   276 				return 4;
   277 			case 8:
   278 				reg = NextRegEBX_ECX_EDX();
   279 				reg2 = NextRegEBX_ECX_EDX();
   280 				//mov reg, DWORD PTR [ebp+<offset>+4]
   281 				//mov reg2, DWORD PTR [ebp+<offset>]
   282 				//push reg
   283 				//push reg2
   284 
   285 				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg, REG_EBP, param_offset+4);
   286 				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, reg2, REG_EBP, param_offset);
   287 				IA32_Push_Reg(&m_HookFunc, reg);
   288 				IA32_Push_Reg(&m_HookFunc, reg2);
   289 
   290 				return 8;
   291 			}
   292 		}
   293 
   294 		jit_int32_t GenContext::PushFloat(jit_int32_t param_offset, const IntPassInfo &pi)
   295 		{
   296 			switch (pi.size)
   297 			{
   298 			default:
   299 				SH_ASSERT(0, ("Unsupported!"));
   300 				return 0;
   301 			case 4:
   302 				//fld DWORD PTR [ebp+<offset>]
   303 				//push reg
   304 				//fstp DWORD PTR [esp]
   305 				IA32_Fld_Mem32_DispAuto(&m_HookFunc, REG_EBP, param_offset);
   306 				IA32_Push_Reg(&m_HookFunc, NextRegEBX_ECX_EDX());
   307 				IA32_Fstp_Mem32_ESP(&m_HookFunc);
   308 				return 4;
   309 			case 8:
   310 				//fld QWORD PTR [ebp+<offset>]
   311 				//sub esp, 8
   312 				//fstp QWORD PTR [esp]
   313 				IA32_Fld_Mem64_DispAuto(&m_HookFunc, REG_EBP, param_offset);
   314 				IA32_Sub_Rm_Imm8(&m_HookFunc, REG_ESP, 8, MOD_REG);
   315 				IA32_Fstp_Mem64_ESP(&m_HookFunc);
   316 				return 8;
   317 			}
   318 		}
   319 
   320 		jit_int32_t GenContext::PushObject(jit_int32_t param_offset, const IntPassInfo &pi, jit_int32_t place_fbrr)
   321 		{
   322 			if ((pi.flags & PassFlag_ForcedByRef) == 0)
   323 			{
   324 				// make room on the stack
   325 				// sub esp, <size>
   326 				IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, GetStackSize(pi), MOD_REG);
   327 			}
   328 
   329 			// if there is a copy constructor..
   330 			if (pi.pCopyCtor)
   331 			{
   332 				// save eax
   333 				// push src addr
   334 				// this= target addr = forcedbyref ? ebp+place_fbrr : esp+12
   335 				// call copy constructor
   336 				// restore eax
   337 			
   338 				IA32_Push_Reg(&m_HookFunc, REG_EAX);
   339 				
   340 				if (pi.flags & PassFlag_ForcedByRef)
   341 					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
   342 				else
   343 					IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_ECX, REG_NOIDX, REG_ESP, NOSCALE, 4);
   344 				
   345 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_offset);
   346 				IA32_Push_Reg(&m_HookFunc, REG_EAX);
   347 				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   348 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(pi.pCopyCtor));
   349 				IA32_Call_Reg(&m_HookFunc, REG_EDX);
   350 				GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
   351 				IA32_Pop_Reg(&m_HookFunc, REG_EAX);
   352 			}
   353 			else
   354 			{
   355 				// bitwise copy
   356 
   357 				
   358 				BitwiseCopy_Setup();
   359 
   360 				//if forcedbyref:
   361 				//  lea edi, [ebp_place_fbrr]
   362 				//else
   363 				//  lea edi, [esp+8]			-  bc_setup pushed two regs onto the stack!
   364 				//lea esi, [ebp+<offs>]
   365 				if (pi.flags & PassFlag_ForcedByRef)
   366 					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, place_fbrr);
   367 				else
   368 					IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_EDI, REG_NOIDX, REG_ESP, NOSCALE, 8);
   369 
   370 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, param_offset);
   371 
   372 				BitwiseCopy_Do(pi.size);
   373 			}
   374 
   375 			// forcedref: push reference to ebp+place_fbrr
   376 			if (pi.flags & PassFlag_ForcedByRef)
   377 			{
   378 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
   379 				IA32_Push_Reg(&m_HookFunc, REG_ECX);
   380 				return SIZE_PTR;
   381 			}
   382 			
   383 			return DownCastSize(pi.size);
   384 		}
   385 
   386 		void GenContext::DestroyParams(jit_int32_t fbrr_base)
   387 		{
   388 			for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
   389 			{
   390 				const IntPassInfo &pi = m_Proto.GetParam(i);
   391 				if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
   392 					(pi.flags & PassInfo::PassFlag_ByVal) && (pi.flags & PassFlag_ForcedByRef))
   393 				{
   394 					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, fbrr_base + GetForcedByRefParamOffset(i));
   395 					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
   396 					IA32_Call_Reg(&m_HookFunc, REG_EAX);
   397 				}
   398 			}
   399 		}
   400 		
   401 		// May not touch eax!
   402 		jit_int32_t GenContext::PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to, int v_place_for_memret,
   403 				jit_int32_t v_place_fbrr_base)
   404 		{
   405 			jit_int32_t added_to_stack = 0;
   406 			jit_int32_t ret = 0;
   407 
   408 			// compute the offset _after_ the last parameter
   409 			jit_int32_t cur_offset = param_base_offset;
   410 			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
   411 			{
   412 				cur_offset += GetStackSize(m_Proto.GetParam(i));
   413 			}
   414 
   415 			// push parameters in reverse order
   416 			for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
   417 			{
   418 				const IntPassInfo &pi = m_Proto.GetParam(i);
   419 				cur_offset -= GetStackSize(pi);
   420 				if (pi.flags & PassInfo::PassFlag_ByVal)
   421 				{
   422 					switch (pi.type)
   423 					{
   424 					case PassInfo::PassType_Basic:
   425 						ret = PushBasic(cur_offset, pi);
   426 						break;
   427 					case PassInfo::PassType_Float:
   428 						ret = PushFloat(cur_offset, pi);
   429 						break;
   430 					case PassInfo::PassType_Object:
   431 						ret = PushObject(cur_offset, pi, v_place_fbrr_base + GetForcedByRefParamOffset(i));
   432 						break;
   433 					}
   434 				}
   435 				else if (pi.flags & PassInfo::PassFlag_ByRef)
   436 				{
   437 					PushRef(cur_offset, pi);
   438 				}
   439 				else
   440 				{
   441 					SH_ASSERT(0, ("Unsupported!"));
   442 				}
   443 				added_to_stack += ret;
   444 			}
   445 
   446 			return added_to_stack;
   447 		}
   448 
   449 		// It is IMPORTANT that PushMemRetPtr doesn't touch ecx and eax
   450 		jit_int32_t GenContext::PushMemRetPtr(jit_int32_t save_ret_to, jit_int32_t v_place_for_memret)
   451 		{
   452 			// Memory return support
   453 			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   454 			{
   455 				// push address where to save it!
   456 				int reg = REG_EDX;
   457 				IA32_Lea_DispRegImmAuto(&m_HookFunc, reg, REG_EBP,
   458 					MemRetWithTempObj() ? v_place_for_memret : save_ret_to);
   459 				IA32_Push_Reg(&m_HookFunc, reg);
   460 
   461 				return (SH_COMP==SH_COMP_MSVC) ? 4 : 0;			// varargs funcs on msvc might need this.
   462 																// gcc doesn't: callee cleans the memret ptr, caller the other params :s
   463 			}
   464 			return 0;
   465 		}
   466 
   467 		void GenContext::SaveRetVal(int v_where, int v_place_for_memret)
   468 		{
   469 			size_t size = GetRealSize(m_Proto.GetRet());
   470 			if (size == 0)
   471 			{
   472 				// No return value -> nothing
   473 				return;
   474 			}
   475 
   476 			if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
   477 			{
   478 				// mov [ebp + v_plugin_ret], eax
   479 				IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   480 				return;
   481 			}
   482 			// else: ByVal
   483 
   484 
   485 			// Memory return:
   486 			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   487 			{
   488 				if (MemRetWithTempObj())
   489 				{
   490 					// *v_where = *v_place_for_memret
   491 
   492 					// if we have an assign operator, use that
   493 					if (m_Proto.GetRet().pAssignOperator)
   494 					{
   495 						// lea edx, [ebp + v_place_for_memret]  <-- src addr
   496 						// lea ecx, [ebp + v_where]				<-- dest addr
   497 						// push edx
   498 						// gcc: push ecx
   499 						// call it
   500 						// gcc: clean up
   501 
   502 						IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_place_for_memret);
   503 						IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_where);
   504 						IA32_Push_Reg(&m_HookFunc, REG_EDX);
   505 						GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   506 
   507 						IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
   508 						IA32_Call_Reg(&m_HookFunc, REG_EAX);
   509 						GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
   510 					}
   511 					else
   512 					{
   513 						// bitwise copy
   514 						BitwiseCopy_Setup();
   515 
   516 						//lea edi, [evp+v_where]		<-- destination
   517 						//lea esi, [ebp+v_place_for_memret]	<-- src
   518 						IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_where);
   519 						IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_place_for_memret);
   520 
   521 						BitwiseCopy_Do(m_Proto.GetRet().size);
   522 					}
   523 
   524 					// Then: destruct *v_place_for_memret if required
   525 					if (m_Proto.GetRet().pDtor)
   526 					{
   527 						//lea ecx, [ebp+v_place_for_memret]
   528 						//gcc: push ecx
   529 						//call it
   530 						//gcc: clean up
   531 
   532 						IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_place_for_memret);
   533 						GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   534 						IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
   535 						IA32_Call_Reg(&m_HookFunc, REG_EAX);
   536 						GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
   537 					}
   538 				}
   539 				else
   540 				{
   541 					// Already copied to correct address -> we're done
   542 					return;
   543 				}
   544 			}
   545 
   546 			if (m_Proto.GetRet().type == PassInfo::PassType_Float)
   547 			{
   548 				if (size == 4)
   549 					IA32_Fstp_Mem32_DispAuto(&m_HookFunc, REG_EBP, v_where);
   550 				else if (size == 8)
   551 					IA32_Fstp_Mem64_DispAuto(&m_HookFunc, REG_EBP, v_where);
   552 			}
   553 			else if (m_Proto.GetRet().type == PassInfo::PassType_Basic)
   554 			{
   555 				if (size <= 4)
   556 				{
   557 					// size <= 4: return in EAX
   558 					//  We align <4 sizes up to 4
   559 
   560 
   561 					// mov [ebp + v_plugin_ret], eax
   562 					IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   563 				}
   564 				else if (size <= 8)
   565 				{
   566 					// size <= 4: return in EAX:EDX
   567 					//  We align 4<x<8 sizes up to 8
   568 
   569 					// mov [ebp + v_plugin_ret], eax
   570 					// mov [ebp + v_plugin_ret + 4], edx
   571 					IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   572 					IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
   573 				}
   574 			}
   575 			else if (m_Proto.GetRet().type == PassInfo::PassType_Object)
   576 			{
   577 				if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)
   578 				{
   579 					if (size <= 4)
   580 					{
   581 						// size <= 4: return in EAX
   582 						//  We align <4 sizes up to 4
   583 
   584 						// mov [ebp + v_plugin_ret], eax
   585 						IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   586 					}
   587 					else if (size <= 8)
   588 					{
   589 						// size <= 4: return in EAX:EDX
   590 						//  We align 4<x<8 sizes up to 8
   591 
   592 						// mov [ebp + v_plugin_ret], eax
   593 						// mov [ebp + v_plugin_ret + 4], edx
   594 						IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   595 						IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
   596 					}
   597 					else
   598 					{
   599 						SH_ASSERT(0, ("RetReg and size > 8 !"));
   600 					}
   601 				}
   602 			}
   603 		}
   604 
   605 		bool GenContext::MemRetWithTempObj()
   606 		{
   607 			// Memory return AND (has destructor OR has assign operator)
   608 			return ((m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   609 				&& (m_Proto.GetRet().flags & (PassInfo::PassFlag_ODtor | PassInfo::PassFlag_AssignOp)));
   610 		}
   611 
   612 		void GenContext::ProcessPluginRetVal(int v_cur_res, int v_pContext, int v_plugin_ret)
   613 		{
   614 			// only for non-void functions!
   615 			if (m_Proto.GetRet().size == 0)
   616 				return;
   617 
   618 			// if (cur_res >= MRES_OVERRIDE)
   619 			//   *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret;
   620 
   621 			//  eax = cur_res
   622 			//  cmp eax,MRES_OVERRIDE
   623 			//  jnge thelabel
   624 			//    pContext->GetOverrideRetPtr()   -->  overrideretptr in eax
   625 			//    *eax = plugin_ret
   626 			//  thelabel:
   627 			//
   628 
   629 			jitoffs_t tmppos, counter;
   630 
   631 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
   632 
   633 			IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_OVERRIDE);
   634 			tmppos = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NGE, 0);
   635 			m_HookFunc.start_count(counter);
   636 
   637 			// eax = pContext->GetOverrideRetPtr()
   638 			//   ECX = pContext
   639 			//   gcc: push ecx
   640 			//   eax = [ecx]
   641 			//   eax = [eax + 4]
   642 			//   call eax
   643 			//   gcc: clean up
   644 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
   645 			GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   646 
   647 			// vtbloffs=0, vtblidx=1
   648 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   649 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 4);
   650 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   651 			GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
   652 
   653 
   654 			// *eax = plugin_ret
   655 			if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
   656 			{
   657 				// mov ecx, [ebp+v_plugin_ret]
   658 				// mov [eax], ecx
   659 				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
   660 				IA32_Mov_Rm_Reg(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   661 			}
   662 			else
   663 			{
   664 				if (m_Proto.GetRet().pAssignOperator)
   665 				{
   666 					// lea edx, [ebp + v_plugin_ret]
   667 					// push edx					<-- src addr
   668 					// msvc: ecx = eax				<-- dest addr
   669 					// gcc: push eax				<-- dest addr
   670 					// call it
   671 					// gcc: clean up
   672 
   673 					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_plugin_ret);
   674 					IA32_Push_Reg(&m_HookFunc, REG_EDX);
   675 #if SH_COMP == SH_COMP_MSVC
   676 					IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
   677 #elif SH_COMP == SH_COMP_GCC
   678 					IA32_Push_Reg(&m_HookFunc, REG_EAX);
   679 #endif
   680 
   681 					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
   682 					IA32_Call_Reg(&m_HookFunc, REG_EAX);
   683 					GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
   684 				}
   685 				else
   686 				{
   687 					// bitwise copy
   688 					BitwiseCopy_Setup();
   689 
   690 					//mov edi, eax					<-- destination
   691 					//lea esi, [ebp+v_plugin_ret]	<-- src
   692 					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDI, REG_EAX, MOD_REG);
   693 					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_plugin_ret);
   694 
   695 					BitwiseCopy_Do(m_Proto.GetRet().size);
   696 				}
   697 			}
   698 
   699 			m_HookFunc.end_count(counter);
   700 			m_HookFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
   701 		}
   702 
   703 		void GenContext::PrepareReturn(int v_status, int v_pContext, int v_retptr)
   704 		{
   705 			// only for non-void functions!
   706 			if (m_Proto.GetRet().size == 0)
   707 				return;
   708 
   709 			// retptr = status >= MRES_OVERRIDE ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()
   710 
   711 			// OverrideRetPtr: vtblidx = 1
   712 			// OrigRetPtr: vtbldix = 2
   713 			//  vtblidx = (status >= MRES_OVERRIDE) ? 1 : 2
   714 
   715 			// 
   716 			// eax = pContext->GetOverrideRetPtr()
   717 			//   ECX = pContext
   718 			//   gcc: push ecx
   719 
   720 			// eax = (status < MRES_OVERRIDE) ? 1 : 0
   721 			//   xor eax, eax
   722 			//   cmp [ebp + v_status], MRES_OVERRIDE
   723 			//   setl al								<-- setcc optimization for ternary operators, 
   724 			
   725 
   726 			//   lea eax, [4*eax + 0x4]
   727 
   728 			//   edx = [ecx]
   729 			//   add edx, eax
   730 			//   mov edx, [edx]
   731 
   732 			//   call edx
   733 			// gcc: clean up
   734 
   735 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
   736 			GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   737 			IA32_Xor_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
   738 			IA32_Cmp_Rm_Disp8_Imm8(&m_HookFunc, REG_EBP, v_status, MRES_OVERRIDE);
   739 			IA32_SetCC_Rm8(&m_HookFunc, REG_EAX, CC_L);
   740 
   741 			IA32_Lea_Reg_RegMultImm32(&m_HookFunc, REG_EAX, REG_EAX, SCALE4, 4);
   742 
   743 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
   744 			IA32_Add_Reg_Rm(&m_HookFunc, REG_EDX, REG_EAX, MOD_REG);
   745 
   746 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_MEM_REG);
   747 			IA32_Call_Reg(&m_HookFunc, REG_EDX);
   748 			GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
   749 
   750 			IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_retptr);
   751 		}
   752 
   753 		void GenContext::DoReturn(int v_retptr, int v_memret_outaddr)
   754 		{
   755 			size_t size = m_Proto.GetRet().size;
   756 			if (!size)
   757 				return;
   758 
   759 			// Get real ret pointer into ecx
   760 			// mov ecx, [ebp + v_ret_ptr]
   761 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_retptr);
   762 
   763 			if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
   764 			{
   765 				// mov eax, [ecx]
   766 				IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   767 				return;
   768 			}
   769 			// else: byval
   770 
   771 			if (m_Proto.GetRet().type == PassInfo::PassType_Float)
   772 			{
   773 				if (size == 4)
   774 					IA32_Fld_Mem32(&m_HookFunc, REG_ECX);
   775 				else if (size == 8)
   776 					IA32_Fld_Mem64(&m_HookFunc, REG_ECX);
   777 			}
   778 			else if (m_Proto.GetRet().type == PassInfo::PassType_Basic || 
   779 				((m_Proto.GetRet().type == PassInfo::PassType_Object) && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)) )
   780 			{
   781 				if (size <= 4)
   782 				{
   783 					// size <= 4: return in EAX
   784 					//  We align <4 sizes up to 4
   785 
   786 					// mov eax, [ecx]
   787 					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   788 				}
   789 				else if (size <= 8)
   790 				{
   791 					// size <= 4: return in EAX:EDX
   792 					//  We align 4<x<8 sizes up to 8
   793 
   794 					// mov eax, [ecx]
   795 					// mov edx, [ecx+4]
   796 					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   797 					IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, 4);
   798 				}
   799 				else
   800 				{
   801 					// size >8: return in memory
   802 					//  handled later
   803 				}
   804 			}
   805 
   806 			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   807 			{
   808 				// *memret_outaddr = plugin_ret
   809 				if (m_Proto.GetRet().pCopyCtor)
   810 				{
   811 					// mov edx, ecx				<-- src	( we set ecx to [ebp+v_retptr] before )
   812 					// push edx					<-- src addr
   813 					// msvc: ecx = [ebp + v_memret_outaddr]				<-- dest addr
   814 					// gcc: push [ebp + v_memret_outaddr]				<-- dest addr
   815 					// call it
   816 					// gcc: clean up
   817 
   818 					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_REG);
   819 					IA32_Push_Reg(&m_HookFunc, REG_EDX);
   820 
   821 #if SH_COMP == SH_COMP_MSVC
   822 					IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_memret_outaddr);
   823 #elif SH_COMP == SH_COMP_GCC
   824 					IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, v_memret_outaddr);
   825 #endif
   826 
   827 					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pCopyCtor));
   828 					IA32_Call_Reg(&m_HookFunc, REG_EAX);
   829 					GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2 * SIZE_PTR, MOD_REG));
   830 				}
   831 				else
   832 				{
   833 					// bitwise copy
   834 					BitwiseCopy_Setup();
   835 					
   836 					//mov edi, [ebp+v_memret_outaddr]		<-- destination
   837 					//mov esi, ecx						<-- src	( we set ecx to [ebp+v_retptr] before )
   838 					IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDI, REG_EBP, v_memret_outaddr);
   839 					IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESI, REG_ECX, MOD_REG);
   840 
   841 					BitwiseCopy_Do(m_Proto.GetRet().size);
   842 				}
   843 
   844 				// In both cases: return the pointer in EAX
   845 				// mov eax, [ebp + v_memret_outaddr]
   846 				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_memret_outaddr);
   847 			}
   848 		}
   849 
   850 		void GenContext::GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
   851 			int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base)
   852 		{
   853 			jitoffs_t counter, tmppos;
   854 			jitoffs_t counter2, tmppos2;
   855 
   856 			jitoffs_t loop_begin_counter;
   857 
   858 			// prev_res = MRES_IGNORED
   859 			IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_prev_res);
   860 
   861 			m_HookFunc.start_count(loop_begin_counter);
   862 
   863 			// eax = pContext->GetNext()
   864 			//   ECX = pContext
   865 			//   gcc: push ecx
   866 			//   eax = [ecx]
   867 			//   eax = [eax]
   868 			//   call eax
   869 			// gcc: clean up
   870 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
   871 			GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   872 
   873 			// vtbloffs=0, vtblidx=0
   874 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   875 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_MEM_REG);
   876 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   877 			GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
   878 
   879 			// quit on zero
   880 			//  test eax, eax
   881 			//  jz exit
   882 			IA32_Test_Rm_Reg(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
   883 			tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
   884 			m_HookFunc.start_count(counter);
   885 
   886 			// prev_res = MRES_IGNORED
   887 			IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_cur_res); 
   888 
   889 			// iter->call()
   890 			//  push params
   891 			//   ecx = eax
   892 			//   gcc: push ecx
   893 			//   eax = [ecx]
   894 			//   eax = [eax+2*SIZE_PTR]
   895 			//   call eax
   896 			//   gcc: clean up 
   897 
   898 			jit_int32_t gcc_clean_bytes = PushParams(base_param_offset, v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
   899 
   900 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
   901 			GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   902 			gcc_clean_bytes += PushMemRetPtr(v_plugin_ret, v_place_for_memret);
   903 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   904 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 2*SIZE_PTR);
   905 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   906 
   907 			DestroyParams(v_place_fbrr_base);
   908 
   909 			SaveRetVal(v_plugin_ret, v_place_for_memret);
   910 
   911 			// cleanup
   912 #if SH_COMP == SH_COMP_GCC
   913 			IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
   914 #endif
   915 
   916 			// params + thisptr
   917 
   918 			// process meta return:
   919 			//  prev_res = cur_res
   920 			//  if (cur_res > status) status = cur_res;
   921 			//
   922 			//  eax = cur_res
   923 			//  edx = status
   924 			//  prev_res = eax
   925 			//  cmp eax,edx
   926 			//  jng thelabel
   927 			//  status = eax
   928 			//  thelabel:
   929 			//
   930 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
   931 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDX, REG_EBP, v_status);
   932 			IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_prev_res);
   933 
   934 			IA32_Cmp_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_REG);
   935 			tmppos2 = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NG, 0);
   936 			m_HookFunc.start_count(counter2);
   937 
   938 			IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_status);
   939 
   940 			m_HookFunc.end_count(counter2);
   941 			m_HookFunc.rewrite(tmppos2, static_cast<jit_uint8_t>(counter2));
   942 
   943 			// process retval for non-void functions
   944 			ProcessPluginRetVal(v_cur_res, v_pContext, v_plugin_ret);
   945 
   946 			// jump back to loop begin
   947 			tmppos2 = IA32_Jump_Imm32(&m_HookFunc, 0);
   948 			m_HookFunc.end_count(loop_begin_counter);
   949 			m_HookFunc.rewrite(tmppos2, -static_cast<jit_int32_t>(loop_begin_counter));
   950 
   951 			m_HookFunc.end_count(counter);
   952 			m_HookFunc.rewrite(tmppos, static_cast<jit_int32_t>(counter));
   953 		}
   954 
   955 		void GenContext::GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
   956 			int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base)
   957 		{
   958 			jitoffs_t counter, tmppos;
   959 			jitoffs_t counter2, tmppos2;
   960 			jitoffs_t counter3, tmppos3;
   961 
   962 			// if (status != MRES_SUPERCEDE && pConteext->ShouldCallOrig())
   963 			//   *v_orig_ret = orig_call()
   964 			// else
   965 			//   *v_orig_ret = *v_override_ret
   966 
   967 			// mov eax, status
   968 			// cmp eax, MRES_SUPERCEDE
   969 			// je dont_call
   970 			// call pContext->ShouldCallOrig()
   971 			// test al, al						!! important: al, not eax! bool is only stored in the LSbyte
   972 			// jz dont_call
   973 			//
   974 			//  orig_call()
   975 			//  SaveRet(v_orig_ret)
   976 			//  jmp skip_dont_call:
   977 			//
   978 			// dont_call:
   979 			//  *v_orig_ret = *v_override_ret
   980 			// skip_dont_call:
   981 
   982 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_status);
   983 			IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_SUPERCEDE);
   984 
   985 			tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_E, 0);
   986 			m_HookFunc.start_count(counter);
   987 
   988 			// eax = pContext->ShouldCallOrig()
   989 			//   ECX = pContext
   990 			//   gcc: push ecx
   991 			//   eax = [ecx]
   992 			//   eax = [eax + 3*SIZE_PTR]
   993 			//   call eax
   994 			// gcc: clean up
   995 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
   996 			GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   997 
   998 			// vtbloffs=0, vtblidx=3
   999 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
  1000 			IA32_Mov_Reg_Rm_Disp8(&m_HookFunc, REG_EAX, REG_EAX, 3*SIZE_PTR);
  1001 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1002 			GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1003 
  1004 			IA32_Test_Rm_Reg8(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
  1005 			tmppos2 = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
  1006 			m_HookFunc.start_count(counter2);
  1007 
  1008 			// push params
  1009 			jit_int32_t gcc_clean_bytes = PushParams(param_base_offs, v_orig_ret, v_place_for_memret, v_place_fbrr_base);
  1010 
  1011 			// thisptr
  1012 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
  1013 #if SH_COMP == SH_COMP_GCC
  1014 			//  on gcc/mingw, this is the first parameter
  1015 			IA32_Push_Reg(&m_HookFunc, REG_ECX);
  1016 			//  on msvc, simply leave it in ecx
  1017 #endif
  1018 			gcc_clean_bytes += PushMemRetPtr(v_orig_ret, v_place_for_memret);
  1019 
  1020 			// call
  1021 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_vfnptr_origentry);
  1022 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1023 
  1024 			// cleanup
  1025 #if SH_COMP == SH_COMP_GCC
  1026 			IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
  1027 #endif
  1028 
  1029 			DestroyParams(v_place_fbrr_base);
  1030 
  1031 			// save retval
  1032 			SaveRetVal(v_orig_ret, v_place_for_memret);
  1033 
  1034 			// Skip don't call variant
  1035 			tmppos3 = IA32_Jump_Imm32(&m_HookFunc, 0);
  1036 			m_HookFunc.start_count(counter3);
  1037 
  1038 
  1039 			// don't call:
  1040 			m_HookFunc.end_count(counter);
  1041 			m_HookFunc.rewrite(tmppos, static_cast<jit_uint32_t>(counter));
  1042 
  1043 			m_HookFunc.end_count(counter2);
  1044 			m_HookFunc.rewrite(tmppos2, static_cast<jit_uint32_t>(counter2));
  1045 
  1046 			// *v_orig_ret = *v_override_ret
  1047 			if (m_Proto.GetRet().flags & PassInfo::PassFlag_ByRef)
  1048 			{
  1049 				// mov ecx, [ebp + v_override_ret]
  1050 				// mov [ebp + v_orig_ret], ecx
  1051 
  1052 				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
  1053 				IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_ECX, v_orig_ret);
  1054 			}
  1055 			else
  1056 			{
  1057 				if (m_Proto.GetRet().pAssignOperator)
  1058 				{
  1059 					// lea edx, [ebp + v_override_ret]			<-- src addr
  1060 					// lea ecx, [ebp + v_orig_ret]				<-- dest addr
  1061 					// push edx					<-- src addr
  1062 					// gcc: push ecx
  1063 					// call it
  1064 					// gcc: clean up
  1065 
  1066 					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_override_ret);
  1067 					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
  1068 					IA32_Push_Reg(&m_HookFunc, REG_EDX);
  1069 					GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1070 
  1071 					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
  1072 					IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1073 					GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
  1074 				}
  1075 				else
  1076 				{
  1077 					// bitwise copy
  1078 					BitwiseCopy_Setup();
  1079 
  1080 					//lea edi, [ebp+v_orig_ret]			<-- destination
  1081 					//lea esi, [ebp+v_override_ret]		<-- src
  1082 					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_orig_ret);
  1083 					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_override_ret);
  1084 
  1085 					BitwiseCopy_Do(m_Proto.GetRet().size);
  1086 				}
  1087 			}
  1088 
  1089 			// skip don't call label target:
  1090 			m_HookFunc.end_count(counter3);
  1091 			m_HookFunc.rewrite(tmppos3, static_cast<jit_uint32_t>(counter3));
  1092 		}
  1093 
  1094 		// Sets *v_pContext to return value
  1095 		void GenContext::CallSetupHookLoop(int v_orig_ret, int v_override_ret, 
  1096 			int v_cur_res, int v_prev_res, int v_status, int v_vfnptr_origentry,
  1097 			int v_this, int v_pContext)
  1098 		{
  1099 			// call shptr->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this),
  1100 			//  &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
  1101 			// The last two params are null for void funcs.
  1102 
  1103 			if (m_Proto.GetRet().size == 0)
  1104 			{
  1105 				// void
  1106 				IA32_Push_Imm8(&m_HookFunc, 0);		// orig_ret
  1107 				IA32_Push_Imm8(&m_HookFunc, 0);		// override_ret
  1108 			}
  1109 			else
  1110 			{
  1111 				// orig_ret and override_ret
  1112 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, v_override_ret);
  1113 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_orig_ret);
  1114 				IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1115 				IA32_Push_Reg(&m_HookFunc, REG_EDX);
  1116 			}
  1117 
  1118 			// cur_res and prev_res
  1119 			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
  1120 			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_prev_res);
  1121 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1122 			IA32_Push_Reg(&m_HookFunc, REG_EDX);
  1123 
  1124 			// status and vfnptr_origentry
  1125 			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_status);
  1126 			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_vfnptr_origentry);
  1127 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1128 			IA32_Push_Reg(&m_HookFunc, REG_EDX);
  1129 
  1130 			// this
  1131 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_this);
  1132 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1133 
  1134 			// our vfn ptr
  1135 			//  *(this + vtbloffs) + SIZE_PTR*vtblidx
  1136 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);			// get this into ecx (gcc!)
  1137 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, m_VtblOffs);
  1138 			IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_EAX, m_VtblIdx * SIZE_PTR, MOD_REG);
  1139 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1140 
  1141 			// *m_pHI
  1142 			IA32_Mov_Rm_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
  1143 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
  1144 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1145 
  1146 			// set up thisptr
  1147 #if SH_COMP == SH_COMP_GCC
  1148 			//  on gcc/mingw, this is the first parameter
  1149 			GCC_ONLY(IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr)));
  1150 #elif SH_COMP == SH_COMP_MSVC
  1151 			//  on msvc, it's ecx
  1152 			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
  1153 #endif
  1154 
  1155 			// call the function. vtbloffs = 0, vtblidx = 19
  1156 			// get vtptr into edx  --  we know shptr on jit time -> dereference it here!
  1157 			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, 
  1158 				(*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[19]);
  1159 
  1160 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1161 
  1162 			// on gcc/mingw, we have to clean up after the call
  1163 			// 9 params + hidden thisptr param
  1164 			GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 10*SIZE_PTR, MOD_REG));
  1165 
  1166 			// store return value
  1167 			IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_pContext);
  1168 		}
  1169 
  1170 		void GenContext::CallEndContext(int v_pContext)
  1171 		{
  1172 			// call endcontext:
  1173 			// shptr->EndContext(pContex)
  1174 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_pContext);
  1175 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1176 
  1177 			// thisptr
  1178 #if SH_COMP == SH_COMP_GCC
  1179 			//  on gcc/mingw, this is the first parameter
  1180 			IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr));
  1181 #elif SH_COMP == SH_COMP_MSVC
  1182 			//  on msvc, it's ecx
  1183 			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
  1184 #endif
  1185 
  1186 			// get vtptr into edx  --  we know shptr on jit time -> dereference it here!
  1187 			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, 
  1188 				(*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[20]);
  1189 
  1190 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1191 
  1192 			// on gcc/mingw, we have to clean up after the call
  1193 			// 1 param + hidden thisptr param
  1194 			GCC_ONLY(IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG));
  1195 		}
  1196 
  1197 		void GenContext::ResetFrame(jit_int32_t startOffset)
  1198 		{
  1199 			m_HookFunc_FrameOffset = startOffset;
  1200 			m_HookFunc_FrameVarsSize = 0;
  1201 		}
  1202 		
  1203 		jit_int32_t GenContext::AddVarToFrame(jit_int32_t size)
  1204 		{
  1205 			m_HookFunc_FrameOffset -= size;
  1206 			m_HookFunc_FrameVarsSize += size;
  1207 			return m_HookFunc_FrameOffset;
  1208 		}
  1209 
  1210 		jit_int32_t GenContext::ComputeVarsSize()
  1211 		{
  1212 			return m_HookFunc_FrameVarsSize;
  1213 		}
  1214 		
  1215 		void * GenContext::GenerateHookFunc()
  1216 		{
  1217 			// prologue
  1218 			IA32_Push_Reg(&m_HookFunc, REG_EBP);
  1219 			IA32_Push_Reg(&m_HookFunc, REG_EBX);
  1220 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EBP, REG_ESP, MOD_REG);
  1221 
  1222 
  1223 			// on msvc, save thisptr
  1224 #if SH_COMP == SH_COMP_MSVC
  1225 			jit_int32_t v_this = -4;
  1226 			IA32_Push_Reg(&m_HookFunc, REG_ECX);
  1227 #elif SH_COMP == SH_COMP_GCC
  1228 			jit_int32_t v_this = 12;		// first param
  1229 #endif
  1230 
  1231 			ResetFrame(
  1232 					(SH_COMP==SH_COMP_MSVC) ? -4 : 0
  1233 					);			// on msvc, start on offset -4 because there already is the thisptr variable
  1234 
  1235 			// ********************** stack frame **********************
  1236 			//															MSVC
  1237 			//   second param (gcc: first real param)	ebp + 16
  1238 			//   first param (gcc: thisptr)				ebp + 12
  1239 			//   ret address:							ebp + 8
  1240 			//   caller's ebp							ebp + 4
  1241 			//   saved ebx								ebp
  1242 			//   MSVC ONLY:	current this				ebp - 4
  1243 			//   void *vfnptr_origentry					ebp - 4			-4
  1244 			//   META_RES status = MRES_IGNORED			ebp - 8			-4
  1245 			//   META_RES prev_res						ebp - 12		-4
  1246 			//   META_RES cur_res						ebp - 16		-4
  1247 			//   IMyDelegate *iter						ebp - 20		-4
  1248 			//   IHookContext *pContext					ebp - 24		-4
  1249 			//  == 3 ptrs + 3 enums = 24 bytes
  1250 			//
  1251 			// varargs:
  1252 			//   va_list argptr							ebp - 28		-4
  1253 			//
  1254 			// non-void: add:
  1255 			//   my_rettype *ret_ptr					ebp - 28 (va: -4)		-4
  1256 			//   my_rettype orig_ret					ebp - 28 (va: -4) - sizeof(my_rettype)			-4
  1257 			//   my_rettype override_ret				ebp - 28 (va: -4) - sizeof(my_rettype)*2			-4
  1258 			//   my_rettype plugin_ret					ebp - 28 (va: -4) - sizeof(my_rettype)*3			-4
  1259 			//  == + 3 * sizeof(my_rettype) bytes
  1260 
  1261 			// if required:
  1262 			//   my_rettype place_for_memret			ebp - 28 (va: -4) - sizeof(my_rettype)*4			-4
  1263 
  1264 			// gcc only: if required:
  1265 			//   place forced byref params              ebp - 28 (va: -4) - sizeof(my_rettype)*{4 or 5}
  1266 			
  1267 
  1268 			const jit_int8_t v_vfnptr_origentry =	AddVarToFrame(SIZE_PTR);
  1269 			const jit_int8_t v_status =				AddVarToFrame(sizeof(META_RES));
  1270 			const jit_int8_t v_prev_res =			AddVarToFrame(sizeof(META_RES));
  1271 			const jit_int8_t v_cur_res =			AddVarToFrame(sizeof(META_RES));
  1272 			const jit_int8_t v_iter =				AddVarToFrame(SIZE_PTR);
  1273 			const jit_int8_t v_pContext =			AddVarToFrame(SIZE_PTR);
  1274 			// Only exists for varargs functions
  1275 			const jit_int8_t v_va_argptr =			(m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs) ?
  1276 														AddVarToFrame(sizeof(va_list)) :
  1277 														0;
  1278 			
  1279 #if SH_COMP == SH_COMP_GCC
  1280 			jit_int32_t param_base_offs =		16;
  1281 #elif SH_COMP == SH_COMP_MSVC
  1282 			jit_int32_t param_base_offs =		12;
  1283 #endif
  1284 
  1285 			// Memory return: first param is the address
  1286 			jit_int32_t v_memret_addr = 0;
  1287 			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
  1288 			{
  1289 #if SH_COMP == SH_COMP_GCC
  1290 				// gcc: now:	first param = mem ret addr
  1291 				//				second param = this pointer
  1292 				//				third param = actual first param
  1293 				v_memret_addr = 12;
  1294 				v_this += 4;
  1295 				param_base_offs += SIZE_PTR;
  1296 #elif SH_COMP == SH_COMP_MSVC
  1297 				v_memret_addr = param_base_offs;
  1298 				param_base_offs += SIZE_PTR;
  1299 #endif
  1300 			}
  1301 
  1302 			jit_int32_t v_ret_ptr = 0;
  1303 			jit_int32_t v_orig_ret = 0;
  1304 			jit_int32_t v_override_ret = 0;
  1305 			jit_int32_t v_plugin_ret = 0;
  1306 			
  1307 			if (m_Proto.GetRet().size != 0)
  1308 			{
  1309 				v_ret_ptr =				AddVarToFrame(SIZE_PTR);
  1310 				v_orig_ret =			AddVarToFrame(GetStackSize(m_Proto.GetRet()));
  1311 				v_override_ret =		AddVarToFrame(GetStackSize(m_Proto.GetRet()));
  1312 				v_plugin_ret =			AddVarToFrame(GetStackSize(m_Proto.GetRet()));
  1313 			}
  1314 
  1315 			jit_int32_t v_place_for_memret = 0;
  1316 			if (MemRetWithTempObj())
  1317 			{
  1318 				v_place_for_memret = 	AddVarToFrame(GetStackSize(m_Proto.GetRet()));
  1319 			}
  1320 
  1321 			jit_int32_t v_place_fbrr_base  = 0;
  1322 			if (GetForcedByRefParamsSize())
  1323 			{
  1324 				v_place_fbrr_base =		AddVarToFrame(GetForcedByRefParamsSize());
  1325 			}
  1326 
  1327 			IA32_Sub_Rm_Imm32(&m_HookFunc, REG_ESP, ComputeVarsSize(), MOD_REG);
  1328 
  1329 			// init status localvar
  1330 			IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_status);
  1331 
  1332 			// VarArgs: init argptr
  1333 			if (m_Proto.GetConvention() & ProtoInfo::CallConv_HasVarArgs)
  1334 			{
  1335 				// argptr = first vararg param
  1336 				// lea eax, [esp + param_base_offs + paramssize]
  1337 				// mov argptr, eax
  1338 
  1339 				IA32_Lea_Reg_DispRegMultImm32(&m_HookFunc, REG_EAX, REG_NOIDX, REG_ESP, NOSCALE, param_base_offs + GetParamsStackSize());
  1340 				IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, v_va_argptr, REG_EAX);
  1341 			}
  1342 			
  1343 			// Call constructors for ret vars if required
  1344 			if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
  1345 				m_Proto.GetRet().pNormalCtor)
  1346 			{
  1347 				// orig_reg
  1348 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
  1349 				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1350 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
  1351 				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1352 				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1353 
  1354 				// override_reg
  1355 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
  1356 				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1357 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
  1358 				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1359 				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1360 
  1361 				// plugin_ret
  1362 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
  1363 				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1364 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
  1365 				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1366 				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1367 
  1368 				// _don't_ call a constructor for v_place_for_memret !
  1369 			}
  1370 
  1371 			// ********************** SetupHookLoop **********************
  1372 			CallSetupHookLoop(v_orig_ret, v_override_ret, v_cur_res, v_prev_res, v_status, v_vfnptr_origentry,
  1373 				v_this, v_pContext);
  1374 
  1375 			// ********************** call pre hooks **********************
  1376 			GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
  1377 				v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
  1378 
  1379 			// ********************** call orig func **********************
  1380 			GenerateCallOrig(v_status, v_pContext, param_base_offs, v_this, v_vfnptr_origentry, v_orig_ret,
  1381 				v_override_ret, v_place_for_memret, v_place_fbrr_base);
  1382 
  1383 			// ********************** call post hooks **********************
  1384 			GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
  1385 				v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
  1386 
  1387 			// ********************** end context and return **********************
  1388 
  1389 			PrepareReturn(v_status, v_pContext, v_ret_ptr);
  1390 
  1391 			CallEndContext(v_pContext);
  1392 
  1393 			// Call destructors of byval object params which have a destructor
  1394 			jit_int32_t cur_param_pos = param_base_offs;
  1395 			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
  1396 			{
  1397 				const IntPassInfo &pi = m_Proto.GetParam(i);
  1398 				// GCC: NOT of forced byref params. the caller destructs those.
  1399 				if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
  1400 					(pi.flags & PassInfo::PassFlag_ByVal) && !(pi.flags & PassFlag_ForcedByRef))
  1401 				{
  1402 					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, cur_param_pos);
  1403 					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
  1404 					IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1405 				}
  1406 				cur_param_pos += GetStackSize(pi);
  1407 			}
  1408 
  1409 			DoReturn(v_ret_ptr, v_memret_addr);
  1410 
  1411 			// Call destructors of orig_ret/ ...
  1412 			if((m_Proto.GetRet().flags & PassInfo::PassFlag_ByVal) &&
  1413 				m_Proto.GetRet().pDtor)
  1414 			{
  1415 				// Preserve return value in EAX(:EDX)
  1416 				IA32_Push_Reg(&m_HookFunc, REG_EAX);
  1417 				IA32_Push_Reg(&m_HookFunc, REG_EDX);
  1418 
  1419 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
  1420 				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1421 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
  1422 				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1423 				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1424 
  1425 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
  1426 				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1427 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
  1428 				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1429 				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1430 
  1431 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
  1432 				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
  1433 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pDtor));
  1434 				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1435 				GCC_ONLY(IA32_Pop_Reg(&m_HookFunc, REG_ECX));
  1436 
  1437 				IA32_Pop_Reg(&m_HookFunc, REG_EDX);
  1438 				IA32_Pop_Reg(&m_HookFunc, REG_EAX);
  1439 			}
  1440 
  1441 			// epilogue
  1442 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESP, REG_EBP, MOD_REG);
  1443 			IA32_Pop_Reg(&m_HookFunc, REG_EBX);
  1444 			IA32_Pop_Reg(&m_HookFunc, REG_EBP);
  1445 			MSVC_ONLY(IA32_Return_Popstack(&m_HookFunc, GetParamsStackSize()));
  1446 			// gcc: Remove 4 bytes from stack on memory return
  1447 #if SH_COMP == SH_COMP_GCC
  1448 			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
  1449 				IA32_Return_Popstack(&m_HookFunc, SIZE_PTR);
  1450 			else
  1451 				IA32_Return(&m_HookFunc);
  1452 #endif
  1453 			
  1454 			// Store pointer for later use
  1455 			// m_HookfuncVfnPtr is a pointer to a void* because SH expects a pointer
  1456 			// into the hookman's vtable
  1457 			*m_HookfuncVfnptr = reinterpret_cast<void*>(m_HookFunc.GetData());
  1458 
  1459 			m_HookFunc.SetRE();
  1460 
  1461 			return m_HookFunc.GetData();
  1462 		}
  1463 
  1464 		// Pre-condition: GenerateHookFunc() has been called!
  1465 		void * GenContext::GeneratePubFunc()
  1466 		{
  1467 			jitoffs_t counter, tmppos;
  1468 
  1469 			// The pubfunc is a static cdecl function.
  1470 			// C Code:
  1471 			//  int HookManPubFunc(
  1472 			//     bool store,				ebp + 8
  1473 			//     IHookManagerInfo *hi		ebp + 12
  1474 			//     )
  1475 			//  {
  1476 			//    if (store)
  1477 			//      *m_pHI = hi;
  1478 			//    if (hi)
  1479 			//      hi->SetInfo(HOOKMAN_VERSION, m_VtblOffs, m_VtblIdx, m_Proto.GetProto(), m_HookfuncVfnptr)
  1480 			//  }
  1481 
  1482 			// prologue
  1483 			IA32_Push_Reg(&m_PubFunc, REG_EBP);
  1484 			IA32_Mov_Reg_Rm(&m_PubFunc, REG_EBP, REG_ESP, MOD_REG);
  1485 
  1486 			
  1487 			// save store in eax, hi in ecx
  1488 			IA32_Movzx_Reg32_Rm8_Disp8(&m_PubFunc, REG_EAX, REG_EBP, 8);
  1489 			IA32_Mov_Reg_Rm_DispAuto(&m_PubFunc, REG_ECX, REG_EBP, 12);
  1490 
  1491 			// Check for store == 0
  1492 			IA32_Test_Rm_Reg8(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
  1493 			tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
  1494 			m_PubFunc.start_count(counter);
  1495 
  1496 			// nonzero -> store hi
  1497 			IA32_Mov_Rm_Imm32(&m_PubFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
  1498 			IA32_Mov_Rm_Reg(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
  1499 
  1500 			// zero
  1501 			m_PubFunc.end_count(counter);
  1502 			m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
  1503 
  1504 			// check for hi == 0
  1505 			IA32_Test_Rm_Reg(&m_PubFunc, REG_ECX, REG_ECX, MOD_REG);
  1506 			tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
  1507 			m_PubFunc.start_count(counter);
  1508 
  1509 			// nonzero -> call vfunc
  1510 			//  push params in reverse order
  1511 			IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_HookfuncVfnptr));
  1512 			IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_BuiltPI));
  1513 			IA32_Push_Imm32(&m_PubFunc, m_VtblIdx);
  1514 			IA32_Push_Imm32(&m_PubFunc, m_VtblOffs);
  1515 			IA32_Push_Imm32(&m_PubFunc, SH_HOOKMAN_VERSION);
  1516 
  1517 			//  hi == this is in ecx
  1518 			//  on gcc/mingw, ecx is the first parameter
  1519 #if SH_COMP == SH_COMP_GCC
  1520 			IA32_Push_Reg(&m_PubFunc, REG_ECX);
  1521 #endif
  1522 			
  1523 			// call the function. vtbloffs = 0, vtblidx = 0
  1524 			// get vtptr into edx
  1525 			IA32_Mov_Reg_Rm(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
  1526 			// get funcptr into eax
  1527 			IA32_Mov_Reg_Rm(&m_PubFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
  1528 
  1529 			IA32_Call_Reg(&m_PubFunc, REG_EAX);
  1530 
  1531 			// on gcc/mingw, we have to clean up after the call
  1532 #if SH_COMP == SH_COMP_GCC
  1533 			// 5 params + hidden thisptr param
  1534 			IA32_Add_Rm_Imm8(&m_PubFunc, REG_ESP, 6*SIZE_MWORD, MOD_REG);
  1535 #endif
  1536 
  1537 			// zero
  1538 			m_PubFunc.end_count(counter);
  1539 			m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
  1540 
  1541 			// return value
  1542 			IA32_Xor_Reg_Rm(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
  1543 
  1544 			// epilogue
  1545 			IA32_Mov_Reg_Rm(&m_PubFunc, REG_ESP, REG_EBP, MOD_REG);
  1546 			IA32_Pop_Reg(&m_PubFunc, REG_EBP);
  1547 			IA32_Return(&m_PubFunc);
  1548 
  1549 			m_PubFunc.SetRE();
  1550 
  1551 			return m_PubFunc;
  1552 		}
  1553 
  1554 		bool GenContext::PassInfoSupported(const IntPassInfo &pi, bool is_ret)
  1555 		{
  1556 			// :TODO: Error returns
  1557 			if (pi.type != PassInfo::PassType_Basic &&
  1558 				pi.type != PassInfo::PassType_Float &&
  1559 				pi.type != PassInfo::PassType_Object)
  1560 			{
  1561 				return false;
  1562 			}
  1563 
  1564 			if (pi.type == PassInfo::PassType_Object &&
  1565 				(pi.flags & PassInfo::PassFlag_ByVal))
  1566 			{
  1567 				if ((pi.flags & PassInfo::PassFlag_CCtor) && !pi.pCopyCtor)
  1568 				{
  1569 					return false;
  1570 				}
  1571 
  1572 				if ((pi.flags & PassInfo::PassFlag_ODtor) && !pi.pDtor)
  1573 				{
  1574 					return false;
  1575 				}
  1576 				
  1577 				if ((pi.flags & PassInfo::PassFlag_AssignOp) && !pi.pAssignOperator)
  1578 				{
  1579 					return false;
  1580 				}
  1581 
  1582 				if ((pi.flags & PassInfo::PassFlag_OCtor) && !pi.pNormalCtor)
  1583 				{
  1584 					return false;
  1585 				}
  1586 			}
  1587 
  1588 			if ((pi.flags & (PassInfo::PassFlag_ByVal | PassInfo::PassFlag_ByRef)) == 0)
  1589 			{
  1590 				return false;			 // Neither byval nor byref!
  1591 			}
  1592 			return true;
  1593 		}
  1594 
  1595 		void GenContext::AutoDetectRetType()
  1596 		{
  1597 			IntPassInfo &pi = m_Proto.GetRet();
  1598 
  1599 			// Only relevant for byval types
  1600 			if (pi.flags & PassInfo::PassFlag_ByVal)
  1601 			{
  1602 				// Basic + float: 
  1603 				if (pi.type == PassInfo::PassType_Basic ||
  1604 					pi.type == PassInfo::PassType_Float)
  1605 				{
  1606 					// <= 8 bytes:
  1607 					//    _always_ in registers, no matter what the user says
  1608 					if (pi.size <= 8)
  1609 					{
  1610 						pi.flags &= ~PassInfo::PassFlag_RetMem;
  1611 						pi.flags |= PassInfo::PassFlag_RetReg;
  1612 					}
  1613 					else
  1614 					{
  1615 						// Does this even exist? No idea, if it does: in memory!
  1616 						pi.flags &= ~PassInfo::PassFlag_RetReg;
  1617 						pi.flags |= PassInfo::PassFlag_RetMem;
  1618 					}
  1619 				}
  1620 				// Object: 
  1621 				else if (pi.type == PassInfo::PassType_Object)
  1622 				{
  1623 					// If the user says nothing, auto-detect
  1624 					if ((pi.flags & (PassInfo::PassFlag_RetMem | PassInfo::PassFlag_RetReg)) == 0)
  1625 					{
  1626 #if SH_COMP == SH_COMP_MSVC
  1627 						// MSVC seems to return _all_ structs, classes, unions in memory
  1628 						pi.flags |= PassInfo::PassFlag_RetMem;
  1629 #elif SH_COMP == SH_COMP_GCC
  1630 						// Same goes for GCC :)
  1631 						pi.flags |= PassInfo::PassFlag_RetMem;
  1632 #endif
  1633 					}
  1634 				}
  1635 			}
  1636 			else
  1637 			{
  1638 				// byref: make sure that the flag is _not_ set
  1639 				pi.flags &= ~PassInfo::PassFlag_RetMem;
  1640 				pi.flags |= PassInfo::PassFlag_RetReg;
  1641 			}
  1642 		}
  1643 
  1644 		void GenContext::AutoDetectParamFlags()
  1645 		{
  1646 #if SH_COMP == SH_COMP_GCC
  1647 			// On GCC, all objects are passed by reference if they have a destructor
  1648 			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
  1649 			{
  1650 				IntPassInfo &pi = m_Proto.GetParam(i);
  1651 				if (pi.type == PassInfo::PassType_Object &&
  1652 					(pi.flags & PassInfo::PassFlag_ODtor))
  1653 				{
  1654 					pi.flags |= PassFlag_ForcedByRef;
  1655 				}
  1656 			}
  1657 #endif
  1658 		}
  1659 
  1660 		HookManagerPubFunc GenContext::Generate()
  1661 		{
  1662 			Clear();
  1663 
  1664 			// Check conditions:
  1665 			// -1) good proto version
  1666 			//  0) we don't support unknown passtypes, convention, ...
  1667 			//  1) we don't support functions which return objects by value or take parameters by value
  1668 			//     that have a constructor, a destructor or an overloaded assignment op
  1669 			//     (we wouldn't know how to call it!)
  1670 
  1671 			if (m_Proto.GetVersion() < 1)
  1672 			{
  1673 				return NULL;
  1674 			}
  1675 
  1676 			AutoDetectRetType();
  1677 			AutoDetectParamFlags();
  1678 
  1679 			// Basically, we only support ThisCall/thiscall with varargs
  1680 			if ((m_Proto.GetConvention() & (~ProtoInfo::CallConv_HasVafmt)) != ProtoInfo::CallConv_ThisCall)
  1681 			{
  1682 				return NULL;
  1683 			}
  1684 
  1685 
  1686 			if (m_Proto.GetRet().size != 0 && !PassInfoSupported(m_Proto.GetRet(), true))
  1687 			{
  1688 				return NULL;
  1689 			}
  1690 
  1691 			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
  1692 			{
  1693 				if (!PassInfoSupported(m_Proto.GetParam(i), false))
  1694 					return NULL;
  1695 			}
  1696 
  1697 			BuildProtoInfo();
  1698 			GenerateHookFunc();
  1699 			return fastdelegate::detail::horrible_cast<HookManagerPubFunc>(GeneratePubFunc());
  1700 		}
  1701 
  1702 		HookManagerPubFunc GenContext::GetPubFunc()
  1703 		{
  1704 			if (m_GeneratedPubFunc == 0)
  1705 				m_GeneratedPubFunc = Generate();
  1706 
  1707 			return m_GeneratedPubFunc;
  1708 		}
  1709 
  1710 		bool GenContext::Equal(const CProto &proto, int vtbl_offs, int vtbl_idx)
  1711 		{
  1712 			return (m_OrigProto.ExactlyEqual(proto) && m_VtblOffs == vtbl_offs && m_VtblIdx == vtbl_idx);
  1713 		}
  1714 
  1715 		bool GenContext::Equal(HookManagerPubFunc other)
  1716 		{
  1717 			return m_GeneratedPubFunc == other;
  1718 		}
  1719 
  1720 		// *********************************** class GenContextContainer
  1721 		CHookManagerAutoGen::CHookManagerAutoGen(ISourceHook *pSHPtr) : m_pSHPtr(pSHPtr)
  1722 		{
  1723 		}
  1724 
  1725 		CHookManagerAutoGen::~CHookManagerAutoGen()
  1726 		{
  1727 			for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
  1728 			{
  1729 				delete iter->m_GenContext;
  1730 			}
  1731 		}
  1732 
  1733 		int CHookManagerAutoGen::GetIfaceVersion()
  1734 		{
  1735 			return SH_HOOKMANAUTOGEN_IFACE_VERSION;
  1736 		}
  1737 
  1738 		int CHookManagerAutoGen::GetImplVersion()
  1739 		{
  1740 			return SH_HOOKMANAUTOGEN_IMPL_VERSION;
  1741 		}
  1742 
  1743 		HookManagerPubFunc CHookManagerAutoGen::MakeHookMan(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx)
  1744 		{
  1745 			CProto mproto(proto);
  1746 			for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
  1747 			{
  1748 				if (iter->m_GenContext->Equal(mproto, vtbl_offs, vtbl_idx))
  1749 				{
  1750 					iter->m_RefCnt++;
  1751 					return iter->m_GenContext->GetPubFunc();
  1752 				}
  1753 			}
  1754 
  1755 			// Not found yet -> new one
  1756 			StoredContext sctx;
  1757 			sctx.m_RefCnt = 1;
  1758 			sctx.m_GenContext = new GenContext(proto, vtbl_offs, vtbl_idx, m_pSHPtr);
  1759 
  1760 			if (sctx.m_GenContext->GetPubFunc() == NULL)
  1761 			{
  1762 				return NULL;
  1763 			}
  1764 			else
  1765 			{
  1766 				m_Contexts.push_back(sctx);
  1767 				return sctx.m_GenContext->GetPubFunc();
  1768 			}
  1769 		}
  1770 
  1771 		void CHookManagerAutoGen::ReleaseHookMan(HookManagerPubFunc pubFunc)
  1772 		{
  1773 			for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
  1774 			{
  1775 				if (iter->m_GenContext->Equal(pubFunc))
  1776 				{
  1777 					iter->m_RefCnt--;
  1778 					if (iter->m_RefCnt == 0)
  1779 					{
  1780 						delete iter->m_GenContext;
  1781 						m_Contexts.erase(iter);
  1782 					}
  1783 					break;
  1784 				}
  1785 			}
  1786 		}
  1787 	}
  1788 }