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