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