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