sourcehook/sourcehook_hookmangen.cpp
branchhookman_autogen
changeset 436 41f21d77c3c1
parent 435 35296642e147
child 437 89cf494f6088
     1.1 --- a/sourcehook/sourcehook_hookmangen.cpp	Thu Nov 01 01:58:44 2007 +0000
     1.2 +++ b/sourcehook/sourcehook_hookmangen.cpp	Thu Nov 01 17:29:39 2007 +0000
     1.3 @@ -306,7 +306,7 @@
     1.4  		}
     1.5  
     1.6  		// May not touch eax!
     1.7 -		jit_int32_t GenContext::PushParams(jit_int32_t param_base_offset)
     1.8 +		jit_int32_t GenContext::PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to)
     1.9  		{
    1.10  			jit_int32_t added_to_stack = 0;
    1.11  			jit_int32_t ret = 0;
    1.12 @@ -348,14 +348,23 @@
    1.13  				}
    1.14  				added_to_stack += ret;
    1.15  			}
    1.16 +
    1.17 +			// Memory return support
    1.18 +			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
    1.19 +			{
    1.20 +				// push address where to save it!
    1.21 +				int reg = NextRegEBX_ECX_EDX();
    1.22 +				IA32_Lea_DispRegImmAuto(&m_HookFunc, reg, REG_EBP, save_ret_to);
    1.23 +				IA32_Push_Reg(&m_HookFunc, reg);
    1.24 +
    1.25 +				added_to_stack += SIZE_PTR;
    1.26 +			}
    1.27 +
    1.28  			return added_to_stack;
    1.29  		}
    1.30  
    1.31  		void GenContext::SaveRetVal(int v_where)
    1.32  		{
    1.33 -			// :TODO: assign op support
    1.34 -			// :TODO: memory return support
    1.35 -
    1.36  			size_t size = m_Proto.GetRet().size;
    1.37  			if (size == 0)
    1.38  			{
    1.39 @@ -363,6 +372,11 @@
    1.40  				return;
    1.41  			}
    1.42  
    1.43 +			// Memory return:
    1.44 +			//   PushParams already did everything that was neccessary
    1.45 +			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
    1.46 +				return;
    1.47 +
    1.48  			if (m_Proto.GetRet().type == PassInfo::PassType_Float)
    1.49  			{
    1.50  				if (size == 4)
    1.51 @@ -391,14 +405,35 @@
    1.52  					IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
    1.53  					IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
    1.54  				}
    1.55 -				else
    1.56 +			}
    1.57 +			else if (m_Proto.GetRet().type == PassInfo::PassType_Object)
    1.58 +			{
    1.59 +				if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)
    1.60  				{
    1.61 -					// size >8: return in memory
    1.62 -					// :TODO:
    1.63 -					//  add flag: MSVC_RetInMemory?
    1.64 +					if (size <= 4)
    1.65 +					{
    1.66 +						// size <= 4: return in EAX
    1.67 +						//  We align <4 sizes up to 4
    1.68 +
    1.69 +						// mov [ebp + v_plugin_ret], eax
    1.70 +						IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
    1.71 +					}
    1.72 +					else if (size <= 8)
    1.73 +					{
    1.74 +						// size <= 4: return in EAX:EDX
    1.75 +						//  We align 4<x<8 sizes up to 8
    1.76 +
    1.77 +						// mov [ebp + v_plugin_ret], eax
    1.78 +						// mov [ebp + v_plugin_ret + 4], edx
    1.79 +						IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_where);
    1.80 +						IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EDX, v_where + 4);
    1.81 +					}
    1.82 +					else
    1.83 +					{
    1.84 +						SH_ASSERT(0, ("RetReg and size > 8 !"));
    1.85 +					}
    1.86  				}
    1.87  			}
    1.88 -			// :TODO: object
    1.89  		}
    1.90  
    1.91  		void GenContext::ProcessPluginRetVal(int v_cur_res, int v_pContext, int v_plugin_ret)
    1.92 @@ -559,7 +594,7 @@
    1.93  			IA32_Mov_Rm_Reg_DispAuto(&m_HookFunc, REG_EBP, REG_EAX, v_retptr);
    1.94  		}
    1.95  
    1.96 -		void GenContext::DoReturn(int v_retptr)
    1.97 +		void GenContext::DoReturn(int v_retptr, int v_memret_outaddr)
    1.98  		{
    1.99  			size_t size = m_Proto.GetRet().size;
   1.100  			if (!size)
   1.101 @@ -579,14 +614,14 @@
   1.102  				else if (size == 8)
   1.103  					IA32_Fld_Mem64(&m_HookFunc, REG_ECX);
   1.104  			}
   1.105 -			else if (m_Proto.GetRet().type == PassInfo::PassType_Basic)
   1.106 +			else if (m_Proto.GetRet().type == PassInfo::PassType_Basic || 
   1.107 +				((m_Proto.GetRet().type == PassInfo::PassType_Object) && (m_Proto.GetRet().flags & PassInfo::PassFlag_RetReg)) )
   1.108  			{
   1.109  				if (size <= 4)
   1.110  				{
   1.111  					// size <= 4: return in EAX
   1.112  					//  We align <4 sizes up to 4
   1.113  
   1.114 -
   1.115  					// mov eax, [ecx]
   1.116  					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EAX, REG_ECX, MOD_MEM_REG);
   1.117  				}
   1.118 @@ -603,11 +638,79 @@
   1.119  				else
   1.120  				{
   1.121  					// size >8: return in memory
   1.122 -					// :TODO:
   1.123 -					//  add flag: MSVC_RetInMemory?
   1.124 +					//  handled later
   1.125  				}
   1.126  			}
   1.127 -			// :TODO: object
   1.128 +
   1.129 +			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   1.130 +			{
   1.131 +				// *memret_outaddr = plugin_ret
   1.132 +				if (m_Proto.GetRet().pAssignOperator)
   1.133 +				{
   1.134 +					// mov edx, ecx				<-- src	( we set ecx to [ebp+v_retptr] before )
   1.135 +					// msvc: ecx = [ebp + v_memret_outaddr]				<-- dest addr
   1.136 +					// gcc: push [ebp + v_memret_outaddr]				<-- dest addr
   1.137 +					// push edx					<-- src addr
   1.138 +					// call it
   1.139 +
   1.140 +					IA32_Mov_Reg_Rm(&m_HookFunc, REG_EDX, REG_ECX, MOD_REG);
   1.141 +
   1.142 +#if SH_COMP == SH_COMP_MSVC
   1.143 +					IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_memret_outaddr);
   1.144 +#elif SH_COMP == SH_COMP_GCC
   1.145 +					IA32_Push_Rm_DispAuto(&m_HookFunc, REG_EBP, v_memret_outaddr);
   1.146 +#endif
   1.147 +					IA32_Push_Reg(&m_HookFunc, REG_EDX);
   1.148 +
   1.149 +					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(m_Proto.GetRet().pAssignOperator));
   1.150 +					IA32_Call_Reg(&m_HookFunc, REG_EAX);
   1.151 +				}
   1.152 +				else
   1.153 +				{
   1.154 +					jit_uint32_t dwords = DownCastSize(m_Proto.GetRet().size) / 4;
   1.155 +					jit_uint32_t bytes = DownCastSize(m_Proto.GetRet().size) % 4;
   1.156 +
   1.157 +					// bitwise copy
   1.158 +					
   1.159 +					//cld
   1.160 +					//push edi
   1.161 +					//push esi
   1.162 +					//mov edi, [ebp+v_memret_outaddr]		<-- destination
   1.163 +					//mov esi, ecx						<-- src	( we set ecx to [ebp+v_retptr] before )
   1.164 +					//if dwords
   1.165 +					// mov ecx, <dwords>
   1.166 +					// rep movsd
   1.167 +					//if bytes
   1.168 +					// mov ecx, <bytes>
   1.169 +					// rep movsb
   1.170 +					//pop esi
   1.171 +					//pop edi
   1.172 +
   1.173 +					IA32_Cld(&m_HookFunc);
   1.174 +					IA32_Push_Reg(&m_HookFunc, REG_EDI);
   1.175 +					IA32_Push_Reg(&m_HookFunc, REG_ESI);
   1.176 +					IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EDI, REG_EBP, v_memret_outaddr);
   1.177 +					IA32_Mov_Reg_Rm(&m_HookFunc, REG_ESI, REG_ECX, MOD_REG);
   1.178 +					if (dwords)
   1.179 +					{
   1.180 +						IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, dwords);
   1.181 +						IA32_Rep(&m_HookFunc);
   1.182 +						IA32_Movsd(&m_HookFunc);
   1.183 +					}
   1.184 +					if (bytes)
   1.185 +					{
   1.186 +						IA32_Mov_Reg_Imm32(&m_HookFunc, REG_ECX, bytes);
   1.187 +						IA32_Rep(&m_HookFunc);
   1.188 +						IA32_Movsb(&m_HookFunc);
   1.189 +					}
   1.190 +					IA32_Pop_Reg(&m_HookFunc, REG_ESI);
   1.191 +					IA32_Pop_Reg(&m_HookFunc, REG_EDI);
   1.192 +				}
   1.193 +
   1.194 +				// In both cases: return the pointer in EAX
   1.195 +				// mov eax, [ebp + v_memret_outaddr]
   1.196 +				IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EBP, v_memret_outaddr);
   1.197 +			}
   1.198  		}
   1.199  
   1.200  		void GenContext::GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
   1.201 @@ -656,7 +759,7 @@
   1.202  			//   eax = [ecx]
   1.203  			//   eax = [eax+2*SIZE_PTR]
   1.204  			//   call eax
   1.205 -			jit_int32_t gcc_clean_bytes = PushParams(base_param_offset);
   1.206 +			jit_int32_t gcc_clean_bytes = PushParams(base_param_offset, v_plugin_ret);
   1.207  
   1.208  			IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
   1.209  #if SH_COMP == SH_COMP_GCC
   1.210 @@ -718,6 +821,13 @@
   1.211  			{
   1.212  				acc += GetStackSize(m_Proto.GetParam(i));
   1.213  			}
   1.214 +
   1.215 +			// Memory return: address is first param
   1.216 +			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   1.217 +				acc += SIZE_PTR;
   1.218 +
   1.219 +			// :TODO: cdecl: THIS POINTER AS FIRST PARAM!!!
   1.220 +
   1.221  			return acc;
   1.222  		}
   1.223  
   1.224 @@ -775,7 +885,7 @@
   1.225  			m_HookFunc.start_count(counter2);
   1.226  
   1.227  			// push params
   1.228 -			jit_int32_t gcc_clean_bytes = PushParams(param_base_offs);
   1.229 +			jit_int32_t gcc_clean_bytes = PushParams(param_base_offs, v_orig_ret);
   1.230  
   1.231  			// thisptr
   1.232  			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
   1.233 @@ -1032,11 +1142,19 @@
   1.234  			const jit_int8_t v_pContext =			-24 + addstackoffset;
   1.235  			
   1.236  #if SH_COMP == SH_COMP_GCC
   1.237 -			const jit_int32_t param_base_offs =		16;
   1.238 +			jit_int32_t param_base_offs =		16;
   1.239  #elif SH_COMP == SH_COMP_MSVC
   1.240 -			const jit_int32_t param_base_offs =		12;
   1.241 +			jit_int32_t param_base_offs =		12;
   1.242  #endif
   1.243  
   1.244 +			// Memory return: first param is the address
   1.245 +			jit_int32_t v_memret_addr = 0;
   1.246 +			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   1.247 +			{
   1.248 +				v_memret_addr = param_base_offs;
   1.249 +				param_base_offs += SIZE_PTR;
   1.250 +			}
   1.251 +
   1.252  			jit_int32_t v_ret_ptr =					-28 + addstackoffset;
   1.253  			jit_int32_t v_orig_ret =				-28	+ addstackoffset - GetStackSize(m_Proto.GetRet()) * 1;
   1.254  			jit_int32_t v_override_ret =			-28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * 2;
   1.255 @@ -1111,7 +1229,7 @@
   1.256  				cur_param_pos += GetStackSize(pi);
   1.257  			}
   1.258  
   1.259 -			DoReturn(v_ret_ptr);
   1.260 +			DoReturn(v_ret_ptr, v_memret_addr);
   1.261  
   1.262  			// !! :TODO: Call destructors of orig_ret/ ...
   1.263  
   1.264 @@ -1254,6 +1372,49 @@
   1.265  			return true;
   1.266  		}
   1.267  
   1.268 +		void GenContext::AutoDetectRetType()
   1.269 +		{
   1.270 +			IntPassInfo &pi = m_Proto.GetRet();
   1.271 +
   1.272 +			// Only relevant for byval types
   1.273 +			if (pi.flags & PassInfo::PassFlag_ByVal)
   1.274 +			{
   1.275 +				// Basic + float: 
   1.276 +				if (pi.type == PassInfo::PassType_Basic ||
   1.277 +					pi.type == PassInfo::PassType_Float)
   1.278 +				{
   1.279 +					// <= 8 bytes:
   1.280 +					//    _always_ in registers, no matter what the user says
   1.281 +					if (pi.size <= 8)
   1.282 +					{
   1.283 +						pi.flags &= ~PassInfo::PassFlag_RetMem;
   1.284 +						pi.flags |= PassInfo::PassFlag_RetReg;
   1.285 +					}
   1.286 +					else
   1.287 +					{
   1.288 +						// Does this even exist? No idea, if it does: in memory!
   1.289 +						pi.flags &= ~PassInfo::PassFlag_RetReg;
   1.290 +						pi.flags |= PassInfo::PassFlag_RetMem;
   1.291 +					}
   1.292 +				}
   1.293 +				// Object: 
   1.294 +				else if (pi.type == PassInfo::PassType_Object)
   1.295 +				{
   1.296 +					// If the user says nothing, auto-detect
   1.297 +					if ((pi.flags & (PassInfo::PassFlag_RetMem | PassInfo::PassFlag_RetReg)) == 0)
   1.298 +					{
   1.299 +#if SH_COMP == SH_COMP_MSVC
   1.300 +						// MSVC seems to return _all_ structs, classes, unions in memory
   1.301 +						pi.flags |= PassInfo::PassFlag_RetMem;
   1.302 +#elif SH_COMP == SH_COMP_GCC
   1.303 +						// Same goes for GCC :)
   1.304 +						pi.flags |= PassInfo::PassFlag_RetMem;
   1.305 +#endif
   1.306 +					}
   1.307 +				}
   1.308 +			}
   1.309 +		}
   1.310 +
   1.311  		HookManagerPubFunc GenContext::Generate()
   1.312  		{
   1.313  			Clear();
   1.314 @@ -1270,6 +1431,8 @@
   1.315  				return NULL;
   1.316  			}
   1.317  
   1.318 +			AutoDetectRetType();
   1.319 +
   1.320  			if (m_Proto.GetConvention() != ProtoInfo::CallConv_Cdecl &&
   1.321  				m_Proto.GetConvention() != ProtoInfo::CallConv_ThisCall)
   1.322  			{