sourcehook/sourcehook_hookmangen.cpp
author Pavol Marko <pavolmarko@gmail.com>
Thu Nov 01 01:58:44 2007 +0000 (2007-11-01)
branchhookman_autogen
changeset 435 35296642e147
parent 432 f26dd3e3030b
child 436 41f21d77c3c1
permissions -rw-r--r--
sh_memory now has CPageAlloc, testhookmangen.cpp contains a small test for it, GenBuffer uses CPageAlloc
(with this simple change, thesthookmangen uses 124K less memory!)
     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)
   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 			return added_to_stack;
   352 		}
   353 
   354 		void GenContext::SaveRetVal(int v_where)
   355 		{
   356 			// :TODO: assign op support
   357 			// :TODO: memory return support
   358 
   359 			size_t size = m_Proto.GetRet().size;
   360 			if (size == 0)
   361 			{
   362 				// No return value -> nothing
   363 				return;
   364 			}
   365 
   366 			if (m_Proto.GetRet().type == PassInfo::PassType_Float)
   367 			{
   368 				if (size == 4)
   369 					IA32_Fstp_Mem32_DispAuto(&m_HookFunc, REG_EBP, v_where);
   370 				else if (size == 8)
   371 					IA32_Fstp_Mem64_DispAuto(&m_HookFunc, REG_EBP, v_where);
   372 			}
   373 			else if (m_Proto.GetRet().type == PassInfo::PassType_Basic)
   374 			{
   375 				if (size <= 4)
   376 				{
   377 					// size <= 4: return in EAX
   378 					//  We align <4 sizes up to 4
   379 
   380 
   381 					// mov [ebp + v_plugin_ret], eax
   382 					IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   383 				}
   384 				else if (size <= 8)
   385 				{
   386 					// size <= 4: return in EAX:EDX
   387 					//  We align 4<x<8 sizes up to 8
   388 
   389 					// mov [ebp + v_plugin_ret], eax
   390 					// mov [ebp + v_plugin_ret + 4], edx
   391 					IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
   392 					IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
   393 				}
   394 				else
   395 				{
   396 					// size >8: return in memory
   397 					// :TODO:
   398 					//  add flag: MSVC_RetInMemory?
   399 				}
   400 			}
   401 			// :TODO: object
   402 		}
   403 
   404 		void GenContext::ProcessPluginRetVal(int v_cur_res, int v_pContext, int v_plugin_ret)
   405 		{
   406 			// only for non-void functions!
   407 			if (m_Proto.GetRet().size == 0)
   408 				return;
   409 
   410 			// if (cur_res >= MRES_OVERRIDE)
   411 			//   *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret;
   412 
   413 			//  eax = cur_res
   414 			//  cmp eax,MRES_OVERRIDE
   415 			//  jnge thelabel
   416 			//    pContext->GetOverrideRetPtr()   -->  overrideretptr in eax
   417 			//    *eax = plugin_ret
   418 			//  thelabel:
   419 			//
   420 
   421 			jitoffs_t tmppos, counter;
   422 
   423 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
   424 
   425 			IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_OVERRIDE);
   426 			tmppos = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NGE, 0);
   427 			m_HookFunc.start_count(counter);
   428 
   429 			// eax = pContext->GetOverrideRetPtr()
   430 			//   ECX = pContext
   431 			//   gcc: push ecx
   432 			//   eax = [ecx]
   433 			//   eax = [eax + 4]
   434 			//   call eax
   435 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
   436 #if SH_COMP == SH_COMP_GCC
   437 			IA32_Push_Reg(&m_HookFunc, REG_ECX);
   438 #endif
   439 
   440 			// vtbloffs=0, vtblidx=1
   441 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   442 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 4);
   443 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   444 
   445 
   446 			// *eax = plugin_ret
   447 			if (m_Proto.GetRet().pAssignOperator)
   448 			{
   449 				// lea edx, [ebp + v_plugin_ret]
   450 				// msvc: ecx = eax				<-- dest addr
   451 				// gcc: push eax				<-- dest addr
   452 				// push edx					<-- src addr
   453 				// call it
   454 
   455 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_plugin_ret);
   456 #if SH_COMP == SH_COMP_MSVC
   457 				IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
   458 #elif SH_COMP == SH_COMP_GCC
   459 				IA32_Push_Reg(&m_HookFunc, REG_EAX);
   460 #endif
   461 				IA32_Push_Reg(&m_HookFunc, REG_EDX);
   462 
   463 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
   464 				IA32_Call_Reg(&m_HookFunc, REG_EAX);
   465 			}
   466 			else
   467 			{
   468 				jit_uint32_t dwords = DownCastSize(m_Proto.GetRet().size) / 4;
   469 				jit_uint32_t bytes = DownCastSize(m_Proto.GetRet().size) % 4;
   470 
   471 				// bitwise copy
   472 				
   473 				//cld
   474 				//push edi
   475 				//push esi
   476 				//mov edi, eax					<-- destination
   477 				//lea esi, [ebp+v_plugin_ret]	<-- src
   478 				//if dwords
   479 				// mov ecx, <dwords>
   480 				// rep movsd
   481 				//if bytes
   482 				// mov ecx, <bytes>
   483 				// rep movsb
   484 				//pop esi
   485 				//pop edi
   486 
   487 				IA32_Cld(&m_HookFunc);
   488 				IA32_Push_Reg(&m_HookFunc, REG_EDI);
   489 				IA32_Push_Reg(&m_HookFunc, REG_ESI);
   490 				IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDI, REG_EAX, MOD_REG);
   491 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_plugin_ret);
   492 				if (dwords)
   493 				{
   494 					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, dwords);
   495 					IA32_Rep(&m_HookFunc);
   496 					IA32_Movsd(&m_HookFunc);
   497 				}
   498 				if (bytes)
   499 				{
   500 					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, bytes);
   501 					IA32_Rep(&m_HookFunc);
   502 					IA32_Movsb(&m_HookFunc);
   503 				}
   504 				IA32_Pop_Reg(&m_HookFunc, REG_ESI);
   505 				IA32_Pop_Reg(&m_HookFunc, REG_EDI);
   506 			}
   507 
   508 			m_HookFunc.end_count(counter);
   509 			m_HookFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
   510 		}
   511 
   512 		void GenContext::PrepareReturn(int v_status, int v_pContext, int v_retptr)
   513 		{
   514 			// only for non-void functions!
   515 			if (m_Proto.GetRet().size == 0)
   516 				return;
   517 
   518 			// retptr = status >= MRES_OVERRIDE ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()
   519 
   520 			// OverrideRetPtr: vtblidx = 1
   521 			// OrigRetPtr: vtbldix = 2
   522 			//  vtblidx = (status >= MRES_OVERRIDE) ? 1 : 2
   523 
   524 			// 
   525 			// eax = pContext->GetOverrideRetPtr()
   526 			//   ECX = pContext
   527 			//   gcc: push ecx
   528 
   529 			// eax = (status < MRES_OVERRIDE) ? 1 : 0
   530 			//   xor eax, eax
   531 			//   cmp [ebp + v_status], MRES_OVERRIDE
   532 			//   setl al								<-- setcc optimization for ternary operators, 
   533 			
   534 
   535 			//   lea eax, [4*eax + 0x4]
   536 
   537 			//   edx = [ecx]
   538 			//   add edx, eax
   539 			//   mov edx, [edx]
   540 
   541 			//   call edx
   542 
   543 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
   544 #if SH_COMP == SH_COMP_GCC
   545 			IA32_Push_Reg(&m_HookFunc, REG_ECX);
   546 #endif
   547 			IA32_Xor_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
   548 			IA32_Cmp_Rm_Disp8_Imm8(&m_HookFunc, REG_EBP, v_status, MRES_OVERRIDE);
   549 			IA32_SetCC_Rm8(&m_HookFunc, REG_EAX, CC_L);
   550 
   551 			IA32_Lea_Reg_RegMultImm32(&m_HookFunc, REG_EAX, REG_EAX, SCALE4, 4);
   552 
   553 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
   554 			IA32_Add_Reg_Rm(&m_HookFunc, REG_EDX, REG_EAX, MOD_REG);
   555 
   556 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_EDX, MOD_MEM_REG);
   557 			IA32_Call_Reg(&m_HookFunc, REG_EDX);
   558 
   559 			IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_retptr);
   560 		}
   561 
   562 		void GenContext::DoReturn(int v_retptr)
   563 		{
   564 			size_t size = m_Proto.GetRet().size;
   565 			if (!size)
   566 				return;
   567 
   568 			// :TODO: assign op support
   569 			// :TODO: memory return support
   570 
   571 			// Get real ret pointer into ecx
   572 			// mov ecx, [ebp + v_ret_ptr]
   573 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_retptr);
   574 
   575 			if (m_Proto.GetRet().type == PassInfo::PassType_Float)
   576 			{
   577 				if (size == 4)
   578 					IA32_Fld_Mem32(&m_HookFunc, REG_ECX);
   579 				else if (size == 8)
   580 					IA32_Fld_Mem64(&m_HookFunc, REG_ECX);
   581 			}
   582 			else if (m_Proto.GetRet().type == PassInfo::PassType_Basic)
   583 			{
   584 				if (size <= 4)
   585 				{
   586 					// size <= 4: return in EAX
   587 					//  We align <4 sizes up to 4
   588 
   589 
   590 					// mov eax, [ecx]
   591 					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   592 				}
   593 				else if (size <= 8)
   594 				{
   595 					// size <= 4: return in EAX:EDX
   596 					//  We align 4<x<8 sizes up to 8
   597 
   598 					// mov eax, [ecx]
   599 					// mov edx, [ecx+4]
   600 					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   601 					IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, 4);
   602 				}
   603 				else
   604 				{
   605 					// size >8: return in memory
   606 					// :TODO:
   607 					//  add flag: MSVC_RetInMemory?
   608 				}
   609 			}
   610 			// :TODO: object
   611 		}
   612 
   613 		void GenContext::GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
   614 			int v_pContext, int base_param_offset, int v_plugin_ret)
   615 		{
   616 			jitoffs_t counter, tmppos;
   617 			jitoffs_t counter2, tmppos2;
   618 
   619 			jitoffs_t loop_begin_counter;
   620 
   621 			// prev_res = MRES_IGNORED
   622 			IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_prev_res);
   623 
   624 			m_HookFunc.start_count(loop_begin_counter);
   625 
   626 			// eax = pContext->GetNext()
   627 			//   ECX = pContext
   628 			//   gcc: push ecx
   629 			//   eax = [ecx]
   630 			//   eax = [eax]
   631 			//   call eax
   632 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
   633 #if SH_COMP == SH_COMP_GCC
   634 			IA32_Push_Reg(&m_HookFunc, REG_ECX);
   635 #endif
   636 
   637 			// vtbloffs=0, vtblidx=0
   638 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   639 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EAX, MOD_MEM_REG);
   640 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   641 
   642 			// quit on zero
   643 			//  test eax, eax
   644 			//  jz exit
   645 			IA32_Test_Rm_Reg(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
   646 			tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
   647 			m_HookFunc.start_count(counter);
   648 
   649 			// prev_res = MRES_IGNORED
   650 			IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_cur_res); 
   651 
   652 			// iter->call()
   653 			//  push params
   654 			//   ecx = eax
   655 			//   gcc: push ecx
   656 			//   eax = [ecx]
   657 			//   eax = [eax+2*SIZE_PTR]
   658 			//   call eax
   659 			jit_int32_t gcc_clean_bytes = PushParams(base_param_offset);
   660 
   661 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
   662 #if SH_COMP == SH_COMP_GCC
   663 			IA32_Push_Reg(&m_HookFunc, REG_ECX);
   664 #endif
   665 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   666 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 2*SIZE_PTR);
   667 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   668 
   669 			SaveRetVal(v_plugin_ret);
   670 
   671 			// cleanup
   672 #if SH_COMP == SH_COMP_GCC
   673 			// params + thisptr
   674 			IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
   675 #endif
   676 
   677 			// process meta return:
   678 			//  prev_res = cur_res
   679 			//  if (cur_res > status) status = cur_res;
   680 			//
   681 			//  eax = cur_res
   682 			//  edx = status
   683 			//  prev_res = eax
   684 			//  cmp eax,edx
   685 			//  jng thelabel
   686 			//  status = eax
   687 			//  thelabel:
   688 			//
   689 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
   690 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDX, REG_EBP, v_status);
   691 			IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_prev_res);
   692 
   693 			IA32_Cmp_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_REG);
   694 			tmppos2 = IA32_Jump_Cond_Imm8(&m_HookFunc, CC_NG, 0);
   695 			m_HookFunc.start_count(counter2);
   696 
   697 			IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_status);
   698 
   699 			m_HookFunc.end_count(counter2);
   700 			m_HookFunc.rewrite(tmppos2, static_cast<jit_uint8_t>(counter2));
   701 
   702 			// process retval for non-void functions
   703 			ProcessPluginRetVal(v_cur_res, v_pContext, v_plugin_ret);
   704 
   705 			// jump back to loop begin
   706 			tmppos2 = IA32_Jump_Imm32(&m_HookFunc, 0);
   707 			m_HookFunc.end_count(loop_begin_counter);
   708 			m_HookFunc.rewrite(tmppos2, -static_cast<jit_int32_t>(loop_begin_counter));
   709 
   710 			m_HookFunc.end_count(counter);
   711 			m_HookFunc.rewrite(tmppos, static_cast<jit_int32_t>(counter));
   712 		}
   713 
   714 		short GenContext::GetParamsStackSize()
   715 		{
   716 			short acc = 0;
   717 			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
   718 			{
   719 				acc += GetStackSize(m_Proto.GetParam(i));
   720 			}
   721 			return acc;
   722 		}
   723 
   724 		void GenContext::GenerateCallOrig(int v_status, int v_pContext,
   725 			int param_base_offs, int v_this, int v_vfnptr_origentry, int v_orig_ret, int v_override_ret)
   726 		{
   727 			jitoffs_t counter, tmppos;
   728 			jitoffs_t counter2, tmppos2;
   729 			jitoffs_t counter3, tmppos3;
   730 
   731 			// if (status != MRES_SUPERCEDE && pConteext->ShouldCallOrig())
   732 			//   *v_orig_ret = orig_call()
   733 			// else
   734 			//   *v_orig_ret = *v_override_ret
   735 
   736 			// mov eax, status
   737 			// cmp eax, MRES_SUPERCEDE
   738 			// je dont_call
   739 			// call pContext->ShouldCallOrig()
   740 			// test al, al						!! important: al, not eax! bool is only stored in the LSbyte
   741 			// jz dont_call
   742 			//
   743 			//  orig_call()
   744 			//  SaveRet(v_orig_ret)
   745 			//  jmp skip_dont_call:
   746 			//
   747 			// dont_call:
   748 			//  *v_orig_ret = *v_override_ret
   749 			// skip_dont_call:
   750 
   751 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_status);
   752 			IA32_Cmp_Rm_Imm32(&m_HookFunc, MOD_REG, REG_EAX, MRES_SUPERCEDE);
   753 
   754 			tmppos = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_E, 0);
   755 			m_HookFunc.start_count(counter);
   756 
   757 			// eax = pContext->ShouldCallOrig()
   758 			//   ECX = pContext
   759 			//   gcc: push ecx
   760 			//   eax = [ecx]
   761 			//   eax = [eax + 3*PTR_SIZE]
   762 			//   call eax
   763 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_pContext);
   764 #if SH_COMP == SH_COMP_GCC
   765 			IA32_Push_Reg(&m_HookFunc, REG_ECX);
   766 #endif
   767 
   768 			// vtbloffs=0, vtblidx=3
   769 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   770 			IA32_Mov_Reg_Rm_Disp8(&m_HookFunc, REG_EAX, REG_EAX, 3*SIZE_PTR);
   771 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   772 
   773 			IA32_Test_Rm_Reg8(&m_HookFunc, REG_EAX, REG_EAX, MOD_REG);
   774 			tmppos2 = IA32_Jump_Cond_Imm32(&m_HookFunc, CC_Z, 0);
   775 			m_HookFunc.start_count(counter2);
   776 
   777 			// push params
   778 			jit_int32_t gcc_clean_bytes = PushParams(param_base_offs);
   779 
   780 			// thisptr
   781 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
   782 #if SH_COMP == SH_COMP_GCC
   783 			//  on gcc/mingw, this is the first parameter
   784 			IA32_Push_Reg(&m_HookFunc, REG_ECX);
   785 			//  on msvc, simply leave it in ecx
   786 #endif
   787 
   788 			// call
   789 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_vfnptr_origentry);
   790 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   791 
   792 			// cleanup
   793 #if SH_COMP == SH_COMP_GCC
   794 			// params + thisptr
   795 			IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
   796 #endif
   797 
   798 			// save retval
   799 			SaveRetVal(v_orig_ret);
   800 
   801 			// Skip don't call variant
   802 			tmppos3 = IA32_Jump_Imm32(&m_HookFunc, 0);
   803 			m_HookFunc.start_count(counter3);
   804 
   805 
   806 			// don't call:
   807 			m_HookFunc.end_count(counter);
   808 			m_HookFunc.rewrite(tmppos, static_cast<jit_uint32_t>(counter));
   809 
   810 			m_HookFunc.end_count(counter2);
   811 			m_HookFunc.rewrite(tmppos2, static_cast<jit_uint32_t>(counter2));
   812 
   813 			// *v_orig_ret = *v_override_ret
   814 			if (m_Proto.GetRet().pAssignOperator)
   815 			{
   816 				// lea edx, [ebp + v_override_ret]			<-- src addr
   817 				// lea ecx, [ebp + v_orig_ret]				<-- dest addr
   818 				// gcc: push ecx
   819 				// push edx					<-- src addr
   820 				// call it
   821 
   822 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_override_ret);
   823 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
   824 #if SH_COMP == SH_COMP_GCC
   825 				IA32_Push_Reg(&m_HookFunc, REG_ECX);
   826 #endif
   827 				IA32_Push_Reg(&m_HookFunc, REG_EDX);
   828 
   829 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
   830 				IA32_Call_Reg(&m_HookFunc, REG_EAX);
   831 			}
   832 			else
   833 			{
   834 				jit_uint32_t dwords = DownCastSize(m_Proto.GetRet().size) / 4;
   835 				jit_uint32_t bytes = DownCastSize(m_Proto.GetRet().size) % 4;
   836 
   837 				// bitwise copy
   838 
   839 				//cld
   840 				//push edi
   841 				//push esi
   842 				//lea edi, [ebp+v_orig_ret]			<-- destination
   843 				//lea esi, [ebp+v_override_ret]		<-- src
   844 				//if dwords
   845 				// mov ecx, <dwords>
   846 				// rep movsd
   847 				//if bytes
   848 				// mov ecx, <bytes>
   849 				// rep movsb
   850 				//pop esi
   851 				//pop edi
   852 
   853 				IA32_Cld(&m_HookFunc);
   854 				IA32_Push_Reg(&m_HookFunc, REG_EDI);
   855 				IA32_Push_Reg(&m_HookFunc, REG_ESI);
   856 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, v_orig_ret);
   857 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, v_override_ret);
   858 				if (dwords)
   859 				{
   860 					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, dwords);
   861 					IA32_Rep(&m_HookFunc);
   862 					IA32_Movsd(&m_HookFunc);
   863 				}
   864 				if (bytes)
   865 				{
   866 					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, bytes);
   867 					IA32_Rep(&m_HookFunc);
   868 					IA32_Movsb(&m_HookFunc);
   869 				}
   870 				IA32_Pop_Reg(&m_HookFunc, REG_ESI);
   871 				IA32_Pop_Reg(&m_HookFunc, REG_EDI);
   872 			}
   873 
   874 			// skip don't call label target:
   875 			m_HookFunc.end_count(counter3);
   876 			m_HookFunc.rewrite(tmppos3, static_cast<jit_uint32_t>(counter3));
   877 		}
   878 
   879 		// Sets *v_pContext to return value
   880 		void GenContext::CallSetupHookLoop(int v_orig_ret, int v_override_ret, 
   881 			int v_cur_res, int v_prev_res, int v_status, int v_vfnptr_origentry,
   882 			int v_this, int v_pContext)
   883 		{
   884 			// call shptr->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this),
   885 			//  &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
   886 			// The last two params are null for void funcs.
   887 
   888 			if (m_Proto.GetRet().size == 0)
   889 			{
   890 				// void
   891 				IA32_Push_Imm8(&m_HookFunc, 0);		// orig_ret
   892 				IA32_Push_Imm8(&m_HookFunc, 0);		// override_ret
   893 			}
   894 			else
   895 			{
   896 				// orig_ret and override_ret
   897 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, v_override_ret);
   898 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDX, REG_EBP, v_orig_ret);
   899 				IA32_Push_Reg(&m_HookFunc, REG_EAX);
   900 				IA32_Push_Reg(&m_HookFunc, REG_EDX);
   901 			}
   902 
   903 			// cur_res and prev_res
   904 			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_cur_res);
   905 			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_prev_res);
   906 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
   907 			IA32_Push_Reg(&m_HookFunc, REG_EDX);
   908 
   909 			// status and vfnptr_origentry
   910 			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EAX, REG_EBP, v_status);
   911 			IA32_Lea_DispRegImm8(&m_HookFunc, REG_EDX, REG_EBP, v_vfnptr_origentry);
   912 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
   913 			IA32_Push_Reg(&m_HookFunc, REG_EDX);
   914 
   915 			// this
   916 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_this);
   917 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
   918 
   919 			// our vfn ptr
   920 			//  *(this + vtbloffs) + SIZE_PTR*vtblidx
   921 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);			// get this into ecx (gcc!)
   922 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_ECX, m_VtblOffs);
   923 			IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_EAX, m_VtblIdx * SIZE_PTR, MOD_REG);
   924 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
   925 
   926 			// *m_pHI
   927 			IA32_Mov_Rm_Imm32(&m_HookFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
   928 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
   929 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
   930 
   931 			// set up thisptr
   932 #if SH_COMP == SH_COMP_GCC
   933 			//  on gcc/mingw, this is the first parameter
   934 			IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr));
   935 #elif SH_COMP == SH_COMP_MSVC
   936 			//  on msvc, it's ecx
   937 			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
   938 #endif
   939 
   940 			// call the function. vtbloffs = 0, vtblidx = 19
   941 			// get vtptr into edx  --  we know shptr on jit time -> dereference it here!
   942 			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, 
   943 				(*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[19]);
   944 
   945 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   946 
   947 			// on gcc/mingw, we have to clean up after the call
   948 #if SH_COMP == SH_COMP_GCC
   949 			// 9 params + hidden thisptr param
   950 			IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 10*SIZE_PTR, MOD_REG);
   951 #endif
   952 
   953 			// store return value
   954 			IA32_Mov_Rm_Reg_Disp8(&m_HookFunc, REG_EBP, REG_EAX, v_pContext);
   955 		}
   956 
   957 		void GenContext::CallEndContext(int v_pContext)
   958 		{
   959 			// call endcontext:
   960 			// shptr->EndContext(pContex)
   961 			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_pContext);
   962 			IA32_Push_Reg(&m_HookFunc, REG_EAX);
   963 
   964 			// thisptr
   965 #if SH_COMP == SH_COMP_GCC
   966 			//  on gcc/mingw, this is the first parameter
   967 			IA32_Push_Imm32(&m_HookFunc, DownCastPtr(m_SHPtr));
   968 #elif SH_COMP == SH_COMP_MSVC
   969 			//  on msvc, it's ecx
   970 			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, DownCastPtr(m_SHPtr));
   971 #endif
   972 
   973 			// get vtptr into edx  --  we know shptr on jit time -> dereference it here!
   974 			IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, 
   975 				(*reinterpret_cast<jit_uint32_t**>(m_SHPtr))[20]);
   976 
   977 			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   978 
   979 			// on gcc/mingw, we have to clean up after the call
   980 #if SH_COMP == SH_COMP_GCC
   981 			// 1 param + hidden thisptr param
   982 			IA32_Add_Rm_Imm8(&m_HookFunc, REG_ESP, 2*SIZE_PTR, MOD_REG);
   983 #endif
   984 		}
   985 
   986 		unsigned char * GenContext::GenerateHookFunc()
   987 		{
   988 			// prologue
   989 			IA32_Push_Reg(&m_HookFunc, REG_EBP);
   990 			IA32_Push_Reg(&m_HookFunc, REG_EBX);
   991 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_EBP, REG_ESP, MOD_REG);
   992 
   993 			// on msvc, save thisptr
   994 #if SH_COMP == SH_COMP_MSVC
   995 			const jit_int8_t v_this = -4;
   996 			const int addstackoffset = -4;
   997 			IA32_Push_Reg(&m_HookFunc, REG_ECX);
   998 #elif SH_COMP == SH_COMP_GCC
   999 			const jit_int8_t v_this = 8;		// first param
  1000 			const int addstackoffset = 0;
  1001 #endif
  1002 
  1003 
  1004 			// ********************** stack frame **********************
  1005 			//														MSVC
  1006 			//   second param (gcc: first real param)	ebp + 16
  1007 			//   first param (gcc: thisptr)				ebp + 12
  1008 			//   ret address:							ebp + 8
  1009 			//   caller's ebp							ebp + 4
  1010 			//   saved ebx								ebp
  1011 			//   MSVC ONLY:	current this				ebp - 4
  1012 			//   void *vfnptr_origentry					ebp - 4			-4
  1013 			//   META_RES status = MRES_IGNORED			ebp - 8			-4
  1014 			//   META_RES prev_res						ebp - 12		-4
  1015 			//   META_RES cur_res						ebp - 16		-4
  1016 			//   IMyDelegate *iter						ebp - 20		-4
  1017 			//   IHookContext *pContext					ebp - 24		-4
  1018 			//  == 3 ptrs + 3 enums = 24 bytes
  1019 			//
  1020 			// non-void: add:
  1021 			//   my_rettype *ret_ptr					ebp - 28		-4
  1022 			//   my_rettype orig_ret					ebp - 28 - sizeof(my_rettype)			-4
  1023 			//   my_rettype override_ret				ebp - 28 - sizeof(my_rettype)*2			-4
  1024 			//   my_rettype plugin_ret					ebp - 28 - sizeof(my_rettype)*3			-4
  1025 			//  == + 3 * sizeof(my_rettype) bytes
  1026 
  1027 			const jit_int8_t v_vfnptr_origentry =	-4	+ addstackoffset;
  1028 			const jit_int8_t v_status =				-8	+ addstackoffset;
  1029 			const jit_int8_t v_prev_res =			-12	+ addstackoffset;
  1030 			const jit_int8_t v_cur_res =			-16	+ addstackoffset;
  1031 			const jit_int8_t v_iter =				-20	+ addstackoffset;
  1032 			const jit_int8_t v_pContext =			-24 + addstackoffset;
  1033 			
  1034 #if SH_COMP == SH_COMP_GCC
  1035 			const jit_int32_t param_base_offs =		16;
  1036 #elif SH_COMP == SH_COMP_MSVC
  1037 			const jit_int32_t param_base_offs =		12;
  1038 #endif
  1039 
  1040 			jit_int32_t v_ret_ptr =					-28 + addstackoffset;
  1041 			jit_int32_t v_orig_ret =				-28	+ addstackoffset - GetStackSize(m_Proto.GetRet()) * 1;
  1042 			jit_int32_t v_override_ret =			-28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * 2;
  1043 			jit_int32_t v_plugin_ret =				-28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * 3;
  1044 
  1045 			// Hash for temporary storage for byval params with copy constructors
  1046 			// (param, offset into stack)
  1047 			short usedStackBytes = 3*SIZE_MWORD + 3*SIZE_PTR +		// vfnptr_origentry, status, prev_res, cur_res, iter, pContext
  1048 				3 * GetStackSize(m_Proto.GetRet()) + (m_Proto.GetRet().size == 0 ? 0 : SIZE_PTR)	// ret_ptr, orig_ret, override_ret, plugin_ret
  1049 				- addstackoffset;									// msvc: current thisptr	
  1050 
  1051 			IA32_Sub_Rm_Imm32(&m_HookFunc, REG_ESP, usedStackBytes, MOD_REG);
  1052 
  1053 			// init status localvar
  1054 			IA32_Mov_Rm_Imm32_Disp8(&m_HookFunc, REG_EBP, MRES_IGNORED, v_status);
  1055 
  1056 			// Call constructors for ret vars if required
  1057 			if(m_Proto.GetRet().pNormalCtor)
  1058 			{
  1059 				// :TODO: Gcc version
  1060 
  1061 				// orig_reg
  1062 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_orig_ret);
  1063 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
  1064 				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1065 
  1066 				// override_reg
  1067 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_override_ret);
  1068 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
  1069 				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1070 
  1071 				// plugin_ret
  1072 				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, v_plugin_ret);
  1073 				IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pNormalCtor));
  1074 				IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1075 			}
  1076 
  1077 			// ********************** SetupHookLoop **********************
  1078 			CallSetupHookLoop(v_orig_ret, v_override_ret, v_cur_res, v_prev_res, v_status, v_vfnptr_origentry,
  1079 				v_this, v_pContext);
  1080 
  1081 			// ********************** call pre hooks **********************
  1082 			GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
  1083 				v_plugin_ret);
  1084 
  1085 			// ********************** call orig func **********************
  1086 			GenerateCallOrig(v_status, v_pContext, param_base_offs, v_this, v_vfnptr_origentry, v_orig_ret,
  1087 				v_override_ret);
  1088 
  1089 			// ********************** call post hooks **********************
  1090 			GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
  1091 				v_plugin_ret);
  1092 
  1093 			// ********************** end context and return **********************
  1094 
  1095 			PrepareReturn(v_status, v_pContext, v_ret_ptr);
  1096 
  1097 			CallEndContext(v_pContext);
  1098 
  1099 			// Call destructors of byval object params which have a destructor
  1100 			jit_int32_t cur_param_pos = param_base_offs;
  1101 			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
  1102 			{
  1103 				const IntPassInfo &pi = m_Proto.GetParam(i);
  1104 				if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
  1105 					(pi.flags & PassInfo::PassFlag_ByVal))
  1106 				{
  1107 					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, cur_param_pos);
  1108 					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
  1109 					IA32_Call_Reg(&m_HookFunc, REG_EAX);
  1110 				}
  1111 				cur_param_pos += GetStackSize(pi);
  1112 			}
  1113 
  1114 			DoReturn(v_ret_ptr);
  1115 
  1116 			// !! :TODO: Call destructors of orig_ret/ ...
  1117 
  1118 			// epilogue
  1119 			IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESP, REG_EBP, MOD_REG);
  1120 			IA32_Pop_Reg(&m_HookFunc, REG_EBX);
  1121 			IA32_Pop_Reg(&m_HookFunc, REG_EBP);
  1122 			IA32_Return_Popstack(&m_HookFunc, GetParamsStackSize());
  1123 
  1124 			// Store pointer for later use
  1125 			// m_HookfuncVfnPtr is a pointer to a void* because SH expects a pointer
  1126 			// into the hookman's vtable
  1127 			*m_HookfuncVfnptr = reinterpret_cast<void*>(m_HookFunc.GetData());
  1128 
  1129 			m_HookFunc.SetRE();
  1130 
  1131 			return m_HookFunc.GetData();
  1132 		}
  1133 
  1134 		// Pre-condition: GenerateHookFunc() has been called!
  1135 		unsigned char * GenContext::GeneratePubFunc()
  1136 		{
  1137 			jitoffs_t counter, tmppos;
  1138 
  1139 			// The pubfunc is a static cdecl function.
  1140 			// C Code:
  1141 			//  int HookManPubFunc(
  1142 			//     bool store,				ebp + 8
  1143 			//     IHookManagerInfo *hi		ebp + 12
  1144 			//     )
  1145 			//  {
  1146 			//    if (store)
  1147 			//      *m_pHI = hi;
  1148 			//    if (hi)
  1149 			//      hi->SetInfo(HOOKMAN_VERSION, m_VtblOffs, m_VtblIdx, m_Proto.GetProto(), m_HookfuncVfnptr)
  1150 			//  }
  1151 
  1152 			// prologue
  1153 			IA32_Push_Reg(&m_PubFunc, REG_EBP);
  1154 			IA32_Mov_Reg_Rm(&m_PubFunc, REG_EBP, REG_ESP, MOD_REG);
  1155 
  1156 			
  1157 			// save store in eax, hi in ecx
  1158 			IA32_Movzx_Reg32_Rm8_Disp8(&m_PubFunc, REG_EAX, REG_EBP, 8);
  1159 			IA32_Mov_Reg_Rm_DispAuto(&m_PubFunc, REG_ECX, REG_EBP, 12);
  1160 
  1161 			// Check for store == 0
  1162 			IA32_Test_Rm_Reg8(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
  1163 			tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
  1164 			m_PubFunc.start_count(counter);
  1165 
  1166 			// nonzero -> store hi
  1167 			IA32_Mov_Rm_Imm32(&m_PubFunc, REG_EDX, DownCastPtr(m_pHI), MOD_REG);
  1168 			IA32_Mov_Rm_Reg(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
  1169 
  1170 			// zero
  1171 			m_PubFunc.end_count(counter);
  1172 			m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
  1173 
  1174 			// check for hi == 0
  1175 			IA32_Test_Rm_Reg(&m_PubFunc, REG_ECX, REG_ECX, MOD_REG);
  1176 			tmppos = IA32_Jump_Cond_Imm8(&m_PubFunc, CC_Z, 0);
  1177 			m_PubFunc.start_count(counter);
  1178 
  1179 			// nonzero -> call vfunc
  1180 			//  push params in reverse order
  1181 			IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_HookfuncVfnptr));
  1182 			IA32_Push_Imm32(&m_PubFunc, DownCastPtr(m_BuiltPI));
  1183 			IA32_Push_Imm32(&m_PubFunc, m_VtblIdx);
  1184 			IA32_Push_Imm32(&m_PubFunc, m_VtblOffs);
  1185 			IA32_Push_Imm32(&m_PubFunc, SH_HOOKMAN_VERSION);
  1186 
  1187 			//  hi == this is in ecx
  1188 			//  on gcc/mingw, ecx is the first parameter
  1189 #if SH_COMP == SH_COMP_GCC
  1190 			IA32_Push_Reg(&m_PubFunc, REG_ECX);
  1191 #endif
  1192 			
  1193 			// call the function. vtbloffs = 0, vtblidx = 0
  1194 			// get vtptr into edx
  1195 			IA32_Mov_Reg_Rm(&m_PubFunc, REG_EDX, REG_ECX, MOD_MEM_REG);
  1196 			// get funcptr into eax
  1197 			IA32_Mov_Reg_Rm(&m_PubFunc, REG_EAX, REG_EDX, MOD_MEM_REG);
  1198 
  1199 			IA32_Call_Reg(&m_PubFunc, REG_EAX);
  1200 
  1201 			// on gcc/mingw, we have to clean up after the call
  1202 #if SH_COMP == SH_COMP_GCC
  1203 			// 5 params + hidden thisptr param
  1204 			IA32_Add_Rm_Imm8(&m_PubFunc, REG_ESP, 6*SIZE_MWORD, MOD_REG);
  1205 #endif
  1206 
  1207 			// zero
  1208 			m_PubFunc.end_count(counter);
  1209 			m_PubFunc.rewrite(tmppos, static_cast<jit_uint8_t>(counter));
  1210 
  1211 			// return value
  1212 			IA32_Xor_Reg_Rm(&m_PubFunc, REG_EAX, REG_EAX, MOD_REG);
  1213 
  1214 			// epilogue
  1215 			IA32_Mov_Reg_Rm(&m_PubFunc, REG_ESP, REG_EBP, MOD_REG);
  1216 			IA32_Pop_Reg(&m_PubFunc, REG_EBP);
  1217 			IA32_Return(&m_PubFunc);
  1218 
  1219 			m_PubFunc.SetRE();
  1220 
  1221 			return m_PubFunc;
  1222 		}
  1223 
  1224 		bool GenContext::PassInfoSupported(const IntPassInfo &pi, bool is_ret)
  1225 		{
  1226 			if (pi.type != PassInfo::PassType_Basic &&
  1227 				pi.type != PassInfo::PassType_Float &&
  1228 				pi.type != PassInfo::PassType_Object)
  1229 			{
  1230 				return false;
  1231 			}
  1232 
  1233 			if (pi.type == PassInfo::PassType_Object &&
  1234 				(pi.flags & PassInfo::PassFlag_ByVal))
  1235 			{
  1236 				if ((pi.flags & PassInfo::PassFlag_CCtor) && !pi.pNormalCtor)
  1237 					return false;
  1238 
  1239 				if ((pi.flags & PassInfo::PassFlag_ODtor) && !pi.pDtor)
  1240 					return false;
  1241 
  1242 				// only care for assignop and normalctor for return types
  1243 				if (is_ret && (pi.flags & PassInfo::PassFlag_AssignOp) && !pi.pAssignOperator)
  1244 					return false;
  1245 
  1246 				if (is_ret && (pi.flags & PassInfo::PassFlag_CCtor) && !pi.pNormalCtor)
  1247 					return false;
  1248 			}
  1249 
  1250 			if ((pi.flags & (PassInfo::PassFlag_ByVal | PassInfo::PassFlag_ByRef)) == 0)
  1251 			{
  1252 				return false;			 // Neither byval nor byref!
  1253 			}
  1254 			return true;
  1255 		}
  1256 
  1257 		HookManagerPubFunc GenContext::Generate()
  1258 		{
  1259 			Clear();
  1260 
  1261 			// Check conditions:
  1262 			// -1) good proto version
  1263 			//  0) we don't support unknown passtypes, convention, ...
  1264 			//  1) we don't support functions which return objects by value or take parameters by value
  1265 			//     that have a constructor, a destructor or an overloaded assignment op
  1266 			//     (we wouldn't know how to call it!)
  1267 
  1268 			if (m_Proto.GetVersion() < 1)
  1269 			{
  1270 				return NULL;
  1271 			}
  1272 
  1273 			if (m_Proto.GetConvention() != ProtoInfo::CallConv_Cdecl &&
  1274 				m_Proto.GetConvention() != ProtoInfo::CallConv_ThisCall)
  1275 			{
  1276 				return NULL;
  1277 			}
  1278 
  1279 
  1280 			if (m_Proto.GetRet().size != 0 && !PassInfoSupported(m_Proto.GetRet(), true))
  1281 			{
  1282 				return NULL;
  1283 			}
  1284 
  1285 			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
  1286 			{
  1287 				if (!PassInfoSupported(m_Proto.GetParam(i), false))
  1288 					return NULL;
  1289 			}
  1290 
  1291 			BuildProtoInfo();
  1292 			GenerateHookFunc();
  1293 			return reinterpret_cast<HookManagerPubFunc>(GeneratePubFunc());
  1294 		}
  1295 	}
  1296 }