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