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