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