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