sourcehook/sourcehook_hookmangen.cpp
branchhookman_autogen
changeset 448 742b470c0e5c
parent 447 4dce98820544
child 449 78c6dfc7fff5
     1.1 --- a/sourcehook/sourcehook_hookmangen.cpp	Wed Nov 07 15:15:05 2007 +0000
     1.2 +++ b/sourcehook/sourcehook_hookmangen.cpp	Wed Nov 07 16:56:03 2007 +0000
     1.3 @@ -196,6 +196,39 @@
     1.4  			IA32_Pop_Reg(&m_HookFunc, REG_EDI);
     1.5  		}
     1.6  
     1.7 +		short GenContext::GetParamsStackSize()
     1.8 +		{
     1.9 +			short acc = 0;
    1.10 +			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
    1.11 +			{
    1.12 +				acc += GetStackSize(m_Proto.GetParam(i));
    1.13 +			}
    1.14 +
    1.15 +			// Memory return: address is first param
    1.16 +			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
    1.17 +				acc += SIZE_PTR;
    1.18 +
    1.19 +			// :TODO: cdecl: THIS POINTER AS FIRST PARAM!!!
    1.20 +
    1.21 +			return acc;
    1.22 +		}
    1.23 +
    1.24 +		short GenContext::GetForcedByRefParamOffset(int p)
    1.25 +		{
    1.26 +			short off = 0;
    1.27 +			for (int i = 0; i < p; ++i)
    1.28 +			{
    1.29 +				if (m_Proto.GetParam(i).flags & PassFlag_ForcedByRef)
    1.30 +					off += GetStackSize(m_Proto.GetParam(i));
    1.31 +			}
    1.32 +			return off;
    1.33 +		}
    1.34 +
    1.35 +		short GenContext::GetForcedByRefParamsSize()
    1.36 +		{
    1.37 +			return GetForcedByRefParamOffset(m_Proto.GetNumOfParams());
    1.38 +		}
    1.39 +
    1.40  		jit_int32_t GenContext::PushRef(jit_int32_t param_offset, const IntPassInfo &pi)
    1.41  		{
    1.42  			// push [ebp+<offset>]
    1.43 @@ -282,23 +315,31 @@
    1.44  			}
    1.45  		}
    1.46  
    1.47 -		jit_int32_t GenContext::PushObject(jit_int32_t param_offset, const IntPassInfo &pi)
    1.48 +		jit_int32_t GenContext::PushObject(jit_int32_t param_offset, const IntPassInfo &pi, jit_int32_t place_fbrr)
    1.49  		{
    1.50 -			// make room on the stack
    1.51 -			// sub esp, <size>
    1.52 -			IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, GetStackSize(pi), MOD_REG);
    1.53 +			if ((pi.flags & PassFlag_ForcedByRef) == 0)
    1.54 +			{
    1.55 +				// make room on the stack
    1.56 +				// sub esp, <size>
    1.57 +				IA32_Sub_Rm_ImmAuto(&m_HookFunc, REG_ESP, GetStackSize(pi), MOD_REG);
    1.58 +			}
    1.59  
    1.60  			// if there is a copy constructor..
    1.61  			if (pi.pCopyCtor)
    1.62  			{
    1.63  				// save eax
    1.64  				// push src addr
    1.65 -				// this= target addr = esp+12
    1.66 +				// this= target addr = forcedbyref ? ebp+place_fbrr : esp+12
    1.67  				// call copy constructor
    1.68  				// restore eax
    1.69  			
    1.70  				IA32_Push_Reg(&m_HookFunc, REG_EAX);
    1.71 -				IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_ECX, REG_NOIDX, REG_ESP, NOSCALE, 4);
    1.72 +				
    1.73 +				if (pi.flags & PassFlag_ForcedByRef)
    1.74 +					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
    1.75 +				else
    1.76 +					IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_ECX, REG_NOIDX, REG_ESP, NOSCALE, 4);
    1.77 +				
    1.78  				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EAX, REG_EBP, param_offset);
    1.79  				IA32_Push_Reg(&m_HookFunc, REG_EAX);
    1.80  				GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
    1.81 @@ -314,19 +355,50 @@
    1.82  				
    1.83  				BitwiseCopy_Setup();
    1.84  
    1.85 -				//lea edi, [esp+8]
    1.86 +				//if forcedbyref:
    1.87 +				//  lea edi, [ebp_place_fbrr]
    1.88 +				//else
    1.89 +				//  lea edi, [esp+8]			-  bc_setup pushed two regs onto the stack!
    1.90  				//lea esi, [ebp+<offs>]
    1.91 -				IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_EDI, REG_NOIDX, REG_ESP, NOSCALE, 8);
    1.92 +				if (pi.flags & PassFlag_ForcedByRef)
    1.93 +					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_EDI, REG_EBP, place_fbrr);
    1.94 +				else
    1.95 +					IA32_Lea_Reg_DispRegMultImm8(&m_HookFunc, REG_EDI, REG_NOIDX, REG_ESP, NOSCALE, 8);
    1.96 +
    1.97  				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ESI, REG_EBP, param_offset);
    1.98  
    1.99  				BitwiseCopy_Do(pi.size);
   1.100  			}
   1.101  
   1.102 +			// forcedref: push reference to ebp+place_fbrr
   1.103 +			if (pi.flags & PassFlag_ForcedByRef)
   1.104 +			{
   1.105 +				IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, place_fbrr);
   1.106 +				IA32_Push_Reg(&m_HookFunc, REG_ECX);
   1.107 +				return SIZE_PTR;
   1.108 +			}
   1.109 +			
   1.110  			return DownCastSize(pi.size);
   1.111  		}
   1.112  
   1.113 +		void GenContext::DestroyParams(jit_int32_t fbrr_base)
   1.114 +		{
   1.115 +			for (int i = m_Proto.GetNumOfParams() - 1; i >= 0; --i)
   1.116 +			{
   1.117 +				const IntPassInfo &pi = m_Proto.GetParam(i);
   1.118 +				if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
   1.119 +					(pi.flags & PassInfo::PassFlag_ByVal) && (pi.flags & PassFlag_ForcedByRef))
   1.120 +				{
   1.121 +					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, fbrr_base + GetForcedByRefParamOffset(i));
   1.122 +					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
   1.123 +					IA32_Call_Reg(&m_HookFunc, REG_EAX);
   1.124 +				}
   1.125 +			}
   1.126 +		}
   1.127 +		
   1.128  		// May not touch eax!
   1.129 -		jit_int32_t GenContext::PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to, int v_place_for_memret)
   1.130 +		jit_int32_t GenContext::PushParams(jit_int32_t param_base_offset, jit_int32_t save_ret_to, int v_place_for_memret,
   1.131 +				jit_int32_t v_place_fbrr_base)
   1.132  		{
   1.133  			jit_int32_t added_to_stack = 0;
   1.134  			jit_int32_t ret = 0;
   1.135 @@ -354,7 +426,7 @@
   1.136  						ret = PushFloat(cur_offset, pi);
   1.137  						break;
   1.138  					case PassInfo::PassType_Object:
   1.139 -						ret = PushObject(cur_offset, pi);
   1.140 +						ret = PushObject(cur_offset, pi, v_place_fbrr_base + GetForcedByRefParamOffset(i));
   1.141  						break;
   1.142  					}
   1.143  				}
   1.144 @@ -768,7 +840,7 @@
   1.145  		}
   1.146  
   1.147  		void GenContext::GenerateCallHooks(int v_status, int v_prev_res, int v_cur_res, int v_iter,
   1.148 -			int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret)
   1.149 +			int v_pContext, int base_param_offset, int v_plugin_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base)
   1.150  		{
   1.151  			jitoffs_t counter, tmppos;
   1.152  			jitoffs_t counter2, tmppos2;
   1.153 @@ -815,7 +887,7 @@
   1.154  			//   call eax
   1.155  			//   gcc: clean up 
   1.156  
   1.157 -			jit_int32_t gcc_clean_bytes = PushParams(base_param_offset, v_plugin_ret, v_place_for_memret);
   1.158 +			jit_int32_t gcc_clean_bytes = PushParams(base_param_offset, v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
   1.159  
   1.160  			IA32_Mov_Reg_Rm(&m_HookFunc, REG_ECX, REG_EAX, MOD_REG);
   1.161  			GCC_ONLY(IA32_Push_Reg(&m_HookFunc, REG_ECX));
   1.162 @@ -823,11 +895,16 @@
   1.163  			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_EAX, REG_EAX, 2*SIZE_PTR);
   1.164  			IA32_Call_Reg(&m_HookFunc, REG_EAX);
   1.165  
   1.166 +			DestroyParams(v_place_fbrr_base);
   1.167 +
   1.168  			SaveRetVal(v_plugin_ret, v_place_for_memret);
   1.169  
   1.170  			// cleanup
   1.171 +#if SH_COMP == SH_COMP_GCC
   1.172 +			IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
   1.173 +#endif
   1.174 +
   1.175  			// params + thisptr
   1.176 -			GCC_ONLY(IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG));
   1.177  
   1.178  			// process meta return:
   1.179  			//  prev_res = cur_res
   1.180 @@ -866,25 +943,8 @@
   1.181  			m_HookFunc.rewrite(tmppos, static_cast<jit_int32_t>(counter));
   1.182  		}
   1.183  
   1.184 -		short GenContext::GetParamsStackSize()
   1.185 -		{
   1.186 -			short acc = 0;
   1.187 -			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
   1.188 -			{
   1.189 -				acc += GetStackSize(m_Proto.GetParam(i));
   1.190 -			}
   1.191 -
   1.192 -			// Memory return: address is first param
   1.193 -			if (m_Proto.GetRet().flags & PassInfo::PassFlag_RetMem)
   1.194 -				acc += SIZE_PTR;
   1.195 -
   1.196 -			// :TODO: cdecl: THIS POINTER AS FIRST PARAM!!!
   1.197 -
   1.198 -			return acc;
   1.199 -		}
   1.200 -
   1.201  		void GenContext::GenerateCallOrig(int v_status, int v_pContext, int param_base_offs, int v_this,
   1.202 -			int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret)
   1.203 +			int v_vfnptr_origentry, int v_orig_ret, int v_override_ret, int v_place_for_memret, jit_int32_t v_place_fbrr_base)
   1.204  		{
   1.205  			jitoffs_t counter, tmppos;
   1.206  			jitoffs_t counter2, tmppos2;
   1.207 @@ -937,7 +997,7 @@
   1.208  			m_HookFunc.start_count(counter2);
   1.209  
   1.210  			// push params
   1.211 -			jit_int32_t gcc_clean_bytes = PushParams(param_base_offs, v_orig_ret, v_place_for_memret);
   1.212 +			jit_int32_t gcc_clean_bytes = PushParams(param_base_offs, v_orig_ret, v_place_for_memret, v_place_fbrr_base);
   1.213  
   1.214  			// thisptr
   1.215  			IA32_Mov_Reg_Rm_DispAuto(&m_HookFunc, REG_ECX, REG_EBP, v_this);
   1.216 @@ -953,10 +1013,11 @@
   1.217  
   1.218  			// cleanup
   1.219  #if SH_COMP == SH_COMP_GCC
   1.220 -			// params + thisptr
   1.221  			IA32_Add_Rm_ImmAuto(&m_HookFunc, REG_ESP, gcc_clean_bytes + SIZE_PTR, MOD_REG);
   1.222  #endif
   1.223  
   1.224 +			DestroyParams(v_place_fbrr_base);
   1.225 +
   1.226  			// save retval
   1.227  			SaveRetVal(v_orig_ret, v_place_for_memret);
   1.228  
   1.229 @@ -1166,6 +1227,9 @@
   1.230  
   1.231  			// if required:
   1.232  			//   my_rettype place_for_memret			ebp - 28 - sizeof(my_rettype)*4			-4
   1.233 +
   1.234 +			// gcc only: if required:
   1.235 +			//   place forced byref params              ebp - 28 - sizeof(my_rettype)*{4 or 5}
   1.236  			
   1.237  
   1.238  			const jit_int8_t v_vfnptr_origentry =	-4	+ addstackoffset;
   1.239 @@ -1196,11 +1260,14 @@
   1.240  
   1.241  			jit_int32_t v_place_for_memret =		-28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * 4;
   1.242  
   1.243 +			jit_int32_t v_place_fbrr_base  =		-28 + addstackoffset - GetStackSize(m_Proto.GetRet()) * (MemRetWithTempObj() ? 5 : 4);
   1.244 +
   1.245  			// Hash for temporary storage for byval params with copy constructors
   1.246  			// (param, offset into stack)
   1.247  			short usedStackBytes = 3*SIZE_MWORD + 3*SIZE_PTR		// vfnptr_origentry, status, prev_res, cur_res, iter, pContext
   1.248  				+ 3 * GetStackSize(m_Proto.GetRet()) + (m_Proto.GetRet().size == 0 ? 0 : SIZE_PTR)	// ret_ptr, orig_ret, override_ret, plugin_ret
   1.249  				+ (MemRetWithTempObj() ? GetStackSize(m_Proto.GetRet()) : 0)
   1.250 +				+ GetForcedByRefParamsSize()
   1.251  				- addstackoffset;									// msvc: current thisptr	
   1.252  
   1.253  			IA32_Sub_Rm_Imm32(&m_HookFunc, REG_ESP, usedStackBytes, MOD_REG);
   1.254 @@ -1244,15 +1311,15 @@
   1.255  
   1.256  			// ********************** call pre hooks **********************
   1.257  			GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
   1.258 -				v_plugin_ret, v_place_for_memret);
   1.259 +				v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
   1.260  
   1.261  			// ********************** call orig func **********************
   1.262  			GenerateCallOrig(v_status, v_pContext, param_base_offs, v_this, v_vfnptr_origentry, v_orig_ret,
   1.263 -				v_override_ret, v_place_for_memret);
   1.264 +				v_override_ret, v_place_for_memret, v_place_fbrr_base);
   1.265  
   1.266  			// ********************** call post hooks **********************
   1.267  			GenerateCallHooks(v_status, v_prev_res, v_cur_res, v_iter, v_pContext, param_base_offs,
   1.268 -				v_plugin_ret, v_place_for_memret);
   1.269 +				v_plugin_ret, v_place_for_memret, v_place_fbrr_base);
   1.270  
   1.271  			// ********************** end context and return **********************
   1.272  
   1.273 @@ -1265,8 +1332,9 @@
   1.274  			for (int i = 0; i < m_Proto.GetNumOfParams(); ++i)
   1.275  			{
   1.276  				const IntPassInfo &pi = m_Proto.GetParam(i);
   1.277 +				// GCC: NOT of forced byref params. the caller destructs those.
   1.278  				if (pi.type == PassInfo::PassType_Object && (pi.flags & PassInfo::PassFlag_ODtor) &&
   1.279 -					(pi.flags & PassInfo::PassFlag_ByVal))
   1.280 +					(pi.flags & PassInfo::PassFlag_ByVal) && !(pi.flags & PassFlag_ForcedByRef))
   1.281  				{
   1.282  					IA32_Lea_DispRegImmAuto(&m_HookFunc, REG_ECX, REG_EBP, cur_param_pos);
   1.283  					IA32_Mov_Reg_Imm32(&m_HookFunc, REG_EAX, DownCastPtr(pi.pDtor));
   1.284 @@ -1420,6 +1488,7 @@
   1.285  				pi.type != PassInfo::PassType_Float &&
   1.286  				pi.type != PassInfo::PassType_Object)
   1.287  			{
   1.288 +				printf("A\n");
   1.289  				return false;
   1.290  			}
   1.291  
   1.292 @@ -1427,16 +1496,28 @@
   1.293  				(pi.flags & PassInfo::PassFlag_ByVal))
   1.294  			{
   1.295  				if ((pi.flags & PassInfo::PassFlag_CCtor) && !pi.pCopyCtor)
   1.296 +				{
   1.297 +					printf("B\n");
   1.298  					return false;
   1.299 +				}
   1.300  
   1.301  				if ((pi.flags & PassInfo::PassFlag_ODtor) && !pi.pDtor)
   1.302 +				{
   1.303 +					printf("C\n");
   1.304  					return false;
   1.305 -
   1.306 +				}
   1.307 +				
   1.308  				if ((pi.flags & PassInfo::PassFlag_AssignOp) && !pi.pAssignOperator)
   1.309 +				{
   1.310 +					printf("D\n");
   1.311  					return false;
   1.312 +				}
   1.313  
   1.314  				if ((pi.flags & PassInfo::PassFlag_OCtor) && !pi.pNormalCtor)
   1.315 +				{
   1.316 +					printf("D\n");
   1.317  					return false;
   1.318 +				}
   1.319  			}
   1.320  
   1.321  			if ((pi.flags & (PassInfo::PassFlag_ByVal | PassInfo::PassFlag_ByRef)) == 0)
   1.322 @@ -1505,8 +1586,7 @@
   1.323  				if (pi.type == PassInfo::PassType_Object &&
   1.324  					(pi.flags & PassInfo::PassFlag_ODtor))
   1.325  				{
   1.326 -					pi.flags &= ~PassInfo::PassFlag_ByVal;
   1.327 -					pi.flags |= PassInfo::PassFlag_ByRef;
   1.328 +					pi.flags |= PassFlag_ForcedByRef;
   1.329  				}
   1.330  			}
   1.331  #endif